Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2014, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "BpSoundTriggerHwService" |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Errors.h> |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <binder/IMemory.h> |
| 27 | #include <binder/Parcel.h> |
| 28 | #include <binder/IPCThreadState.h> |
| 29 | #include <binder/IServiceManager.h> |
| 30 | |
| 31 | #include <soundtrigger/ISoundTriggerHwService.h> |
| 32 | #include <soundtrigger/ISoundTrigger.h> |
| 33 | #include <soundtrigger/ISoundTriggerClient.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | enum { |
| 38 | LIST_MODULES = IBinder::FIRST_CALL_TRANSACTION, |
| 39 | ATTACH, |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | SET_CAPTURE_STATE, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Eric Laurent | b9096dc | 2015-05-04 15:29:36 -0700 | [diff] [blame] | 43 | #define MAX_ITEMS_PER_LIST 1024 |
| 44 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 45 | class BpSoundTriggerHwService: public BpInterface<ISoundTriggerHwService> |
| 46 | { |
| 47 | public: |
Chih-Hung Hsieh | 090ef60 | 2016-04-27 10:39:54 -0700 | [diff] [blame] | 48 | explicit BpSoundTriggerHwService(const sp<IBinder>& impl) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 49 | : BpInterface<ISoundTriggerHwService>(impl) |
| 50 | { |
| 51 | } |
| 52 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 53 | virtual status_t listModules(const String16& opPackageName, |
| 54 | struct sound_trigger_module_descriptor *modules, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 55 | uint32_t *numModules) |
| 56 | { |
| 57 | if (numModules == NULL || (*numModules != 0 && modules == NULL)) { |
| 58 | return BAD_VALUE; |
| 59 | } |
| 60 | Parcel data, reply; |
| 61 | data.writeInterfaceToken(ISoundTriggerHwService::getInterfaceDescriptor()); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 62 | data.writeString16(opPackageName); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 63 | unsigned int numModulesReq = (modules == NULL) ? 0 : *numModules; |
| 64 | data.writeInt32(numModulesReq); |
| 65 | status_t status = remote()->transact(LIST_MODULES, data, &reply); |
| 66 | if (status == NO_ERROR) { |
| 67 | status = (status_t)reply.readInt32(); |
| 68 | *numModules = (unsigned int)reply.readInt32(); |
| 69 | } |
| 70 | ALOGV("listModules() status %d got *numModules %d", status, *numModules); |
| 71 | if (status == NO_ERROR) { |
| 72 | if (numModulesReq > *numModules) { |
| 73 | numModulesReq = *numModules; |
| 74 | } |
| 75 | if (numModulesReq > 0) { |
| 76 | reply.read(modules, numModulesReq * sizeof(struct sound_trigger_module_descriptor)); |
| 77 | } |
| 78 | } |
| 79 | return status; |
| 80 | } |
| 81 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 82 | virtual status_t attach(const String16& opPackageName, |
| 83 | const sound_trigger_module_handle_t handle, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 84 | const sp<ISoundTriggerClient>& client, |
| 85 | sp<ISoundTrigger>& module) |
| 86 | { |
| 87 | Parcel data, reply; |
| 88 | data.writeInterfaceToken(ISoundTriggerHwService::getInterfaceDescriptor()); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 89 | data.writeString16(opPackageName); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 90 | data.write(&handle, sizeof(sound_trigger_module_handle_t)); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 91 | data.writeStrongBinder(IInterface::asBinder(client)); |
Eric Laurent | 014620f | 2015-10-05 17:21:00 -0700 | [diff] [blame] | 92 | status_t status = remote()->transact(ATTACH, data, &reply); |
| 93 | if (status != NO_ERROR) { |
| 94 | return status; |
| 95 | } |
| 96 | status = reply.readInt32(); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 97 | if (reply.readInt32() != 0) { |
| 98 | module = interface_cast<ISoundTrigger>(reply.readStrongBinder()); |
| 99 | } |
| 100 | return status; |
| 101 | } |
| 102 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 103 | virtual status_t setCaptureState(bool active) |
| 104 | { |
| 105 | Parcel data, reply; |
| 106 | data.writeInterfaceToken(ISoundTriggerHwService::getInterfaceDescriptor()); |
| 107 | data.writeInt32(active); |
| 108 | status_t status = remote()->transact(SET_CAPTURE_STATE, data, &reply); |
| 109 | if (status == NO_ERROR) { |
| 110 | status = reply.readInt32(); |
| 111 | } |
| 112 | return status; |
| 113 | } |
| 114 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | IMPLEMENT_META_INTERFACE(SoundTriggerHwService, "android.hardware.ISoundTriggerHwService"); |
| 118 | |
| 119 | // ---------------------------------------------------------------------- |
| 120 | |
| 121 | status_t BnSoundTriggerHwService::onTransact( |
| 122 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 123 | { |
| 124 | switch(code) { |
| 125 | case LIST_MODULES: { |
| 126 | CHECK_INTERFACE(ISoundTriggerHwService, data, reply); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 127 | String16 opPackageName; |
| 128 | status_t status = data.readString16(&opPackageName); |
| 129 | if (status != NO_ERROR) { |
| 130 | return status; |
| 131 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 132 | unsigned int numModulesReq = data.readInt32(); |
Eric Laurent | b9096dc | 2015-05-04 15:29:36 -0700 | [diff] [blame] | 133 | if (numModulesReq > MAX_ITEMS_PER_LIST) { |
| 134 | numModulesReq = MAX_ITEMS_PER_LIST; |
| 135 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 136 | unsigned int numModules = numModulesReq; |
| 137 | struct sound_trigger_module_descriptor *modules = |
| 138 | (struct sound_trigger_module_descriptor *)calloc(numModulesReq, |
| 139 | sizeof(struct sound_trigger_module_descriptor)); |
Eric Laurent | b9096dc | 2015-05-04 15:29:36 -0700 | [diff] [blame] | 140 | if (modules == NULL) { |
| 141 | reply->writeInt32(NO_MEMORY); |
| 142 | reply->writeInt32(0); |
| 143 | return NO_ERROR; |
| 144 | } |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 145 | status = listModules(opPackageName, modules, &numModules); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 146 | reply->writeInt32(status); |
| 147 | reply->writeInt32(numModules); |
| 148 | ALOGV("LIST_MODULES status %d got numModules %d", status, numModules); |
| 149 | |
| 150 | if (status == NO_ERROR) { |
| 151 | if (numModulesReq > numModules) { |
| 152 | numModulesReq = numModules; |
| 153 | } |
| 154 | reply->write(modules, |
| 155 | numModulesReq * sizeof(struct sound_trigger_module_descriptor)); |
| 156 | } |
| 157 | free(modules); |
| 158 | return NO_ERROR; |
| 159 | } |
| 160 | |
| 161 | case ATTACH: { |
| 162 | CHECK_INTERFACE(ISoundTriggerHwService, data, reply); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 163 | String16 opPackageName; |
| 164 | status_t status = data.readString16(&opPackageName); |
| 165 | if (status != NO_ERROR) { |
| 166 | return status; |
| 167 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 168 | sound_trigger_module_handle_t handle; |
| 169 | data.read(&handle, sizeof(sound_trigger_module_handle_t)); |
| 170 | sp<ISoundTriggerClient> client = |
| 171 | interface_cast<ISoundTriggerClient>(data.readStrongBinder()); |
| 172 | sp<ISoundTrigger> module; |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 173 | status = attach(opPackageName, handle, client, module); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 174 | reply->writeInt32(status); |
| 175 | if (module != 0) { |
| 176 | reply->writeInt32(1); |
Marco Nelissen | 06b4606 | 2014-11-14 07:58:25 -0800 | [diff] [blame] | 177 | reply->writeStrongBinder(IInterface::asBinder(module)); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 178 | } else { |
| 179 | reply->writeInt32(0); |
| 180 | } |
| 181 | return NO_ERROR; |
| 182 | } break; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 183 | |
| 184 | case SET_CAPTURE_STATE: { |
| 185 | CHECK_INTERFACE(ISoundTriggerHwService, data, reply); |
| 186 | reply->writeInt32(setCaptureState((bool)data.readInt32())); |
| 187 | return NO_ERROR; |
| 188 | } break; |
| 189 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 190 | default: |
| 191 | return BBinder::onTransact(code, data, reply, flags); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // ---------------------------------------------------------------------------- |
| 196 | |
| 197 | }; // namespace android |