Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "SoundTriggerHwService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <pthread.h> |
| 24 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 25 | #include <audio_utils/clock.h> |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 26 | #include <system/sound_trigger.h> |
| 27 | #include <cutils/atomic.h> |
| 28 | #include <cutils/properties.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 29 | #include <hardware/hardware.h> |
| 30 | #include <media/AudioSystem.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 31 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 32 | #include <utils/Errors.h> |
| 33 | #include <utils/Log.h> |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 34 | #include <binder/IServiceManager.h> |
| 35 | #include <binder/MemoryBase.h> |
| 36 | #include <binder/MemoryHeapBase.h> |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 37 | #include <system/sound_trigger.h> |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 38 | #include "SoundTriggerHwService.h" |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 39 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 40 | #define HW_MODULE_PREFIX "primary" |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 41 | namespace android { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 42 | |
| 43 | SoundTriggerHwService::SoundTriggerHwService() |
| 44 | : BnSoundTriggerHwService(), |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 45 | mNextUniqueId(1), |
| 46 | mMemoryDealer(new MemoryDealer(1024 * 1024, "SoundTriggerHwService")), |
| 47 | mCaptureState(false) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
| 51 | void SoundTriggerHwService::onFirstRef() |
| 52 | { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 53 | int rc; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 54 | |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 55 | sp<SoundTriggerHalInterface> halInterface = |
| 56 | SoundTriggerHalInterface::connectModule(HW_MODULE_PREFIX); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 57 | |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 58 | if (halInterface == 0) { |
| 59 | ALOGW("could not connect to HAL"); |
| 60 | return; |
| 61 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 62 | sound_trigger_module_descriptor descriptor; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 63 | rc = halInterface->getProperties(&descriptor.properties); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 64 | if (rc != 0) { |
| 65 | ALOGE("could not read implementation properties"); |
| 66 | return; |
| 67 | } |
| 68 | descriptor.handle = |
| 69 | (sound_trigger_module_handle_t)android_atomic_inc(&mNextUniqueId); |
| 70 | ALOGI("loaded default module %s, handle %d", descriptor.properties.description, |
| 71 | descriptor.handle); |
| 72 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 73 | sp<Module> module = new Module(this, halInterface, descriptor); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 74 | mModules.add(descriptor.handle, module); |
| 75 | mCallbackThread = new CallbackThread(this); |
| 76 | } |
| 77 | |
| 78 | SoundTriggerHwService::~SoundTriggerHwService() |
| 79 | { |
| 80 | if (mCallbackThread != 0) { |
| 81 | mCallbackThread->exit(); |
| 82 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 83 | } |
| 84 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 85 | status_t SoundTriggerHwService::listModules(const String16& opPackageName, |
| 86 | struct sound_trigger_module_descriptor *modules, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 87 | uint32_t *numModules) |
| 88 | { |
| 89 | ALOGV("listModules"); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 90 | if (!captureHotwordAllowed(opPackageName, |
| 91 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 92 | IPCThreadState::self()->getCallingUid())) { |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 93 | return PERMISSION_DENIED; |
| 94 | } |
| 95 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 96 | AutoMutex lock(mServiceLock); |
| 97 | if (numModules == NULL || (*numModules != 0 && modules == NULL)) { |
| 98 | return BAD_VALUE; |
| 99 | } |
| 100 | size_t maxModules = *numModules; |
| 101 | *numModules = mModules.size(); |
| 102 | for (size_t i = 0; i < mModules.size() && i < maxModules; i++) { |
| 103 | modules[i] = mModules.valueAt(i)->descriptor(); |
| 104 | } |
| 105 | return NO_ERROR; |
| 106 | } |
| 107 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 108 | status_t SoundTriggerHwService::attach(const String16& opPackageName, |
| 109 | const sound_trigger_module_handle_t handle, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 110 | const sp<ISoundTriggerClient>& client, |
| 111 | sp<ISoundTrigger>& moduleInterface) |
| 112 | { |
| 113 | ALOGV("attach module %d", handle); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 114 | if (!captureHotwordAllowed(opPackageName, |
| 115 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 116 | IPCThreadState::self()->getCallingUid())) { |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 117 | return PERMISSION_DENIED; |
| 118 | } |
| 119 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 120 | AutoMutex lock(mServiceLock); |
| 121 | moduleInterface.clear(); |
| 122 | if (client == 0) { |
| 123 | return BAD_VALUE; |
| 124 | } |
| 125 | ssize_t index = mModules.indexOfKey(handle); |
| 126 | if (index < 0) { |
| 127 | return BAD_VALUE; |
| 128 | } |
| 129 | sp<Module> module = mModules.valueAt(index); |
| 130 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 131 | sp<ModuleClient> moduleClient = module->addClient(client, opPackageName); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 132 | if (moduleClient == 0) { |
| 133 | return NO_INIT; |
| 134 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 135 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 136 | moduleClient->setCaptureState_l(mCaptureState); |
| 137 | moduleInterface = moduleClient; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 138 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 139 | return NO_ERROR; |
| 140 | } |
| 141 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 142 | status_t SoundTriggerHwService::setCaptureState(bool active) |
| 143 | { |
| 144 | ALOGV("setCaptureState %d", active); |
| 145 | AutoMutex lock(mServiceLock); |
| 146 | mCaptureState = active; |
| 147 | for (size_t i = 0; i < mModules.size(); i++) { |
| 148 | mModules.valueAt(i)->setCaptureState_l(active); |
| 149 | } |
| 150 | return NO_ERROR; |
| 151 | } |
| 152 | |
| 153 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 154 | static const int kDumpLockTimeoutNs = 1 * NANOS_PER_SECOND; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 155 | |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 156 | static bool dumpTryLock(Mutex& mutex) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 157 | { |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 158 | status_t err = mutex.timedLock(kDumpLockTimeoutNs); |
| 159 | return err == NO_ERROR; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | status_t SoundTriggerHwService::dump(int fd, const Vector<String16>& args __unused) { |
| 163 | String8 result; |
| 164 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 165 | result.appendFormat("Permission Denial: can't dump SoundTriggerHwService"); |
| 166 | write(fd, result.string(), result.size()); |
| 167 | } else { |
Mikhail Naganov | 959e2d0 | 2019-03-28 11:08:19 -0700 | [diff] [blame] | 168 | bool locked = dumpTryLock(mServiceLock); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 169 | // failed to lock - SoundTriggerHwService is probably deadlocked |
| 170 | if (!locked) { |
| 171 | result.append("SoundTriggerHwService may be deadlocked\n"); |
| 172 | write(fd, result.string(), result.size()); |
| 173 | } |
| 174 | |
| 175 | if (locked) mServiceLock.unlock(); |
| 176 | } |
| 177 | return NO_ERROR; |
| 178 | } |
| 179 | |
| 180 | status_t SoundTriggerHwService::onTransact( |
| 181 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 182 | return BnSoundTriggerHwService::onTransact(code, data, reply, flags); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | // static |
| 187 | void SoundTriggerHwService::recognitionCallback(struct sound_trigger_recognition_event *event, |
| 188 | void *cookie) |
| 189 | { |
| 190 | Module *module = (Module *)cookie; |
| 191 | if (module == NULL) { |
| 192 | return; |
| 193 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 194 | sp<SoundTriggerHwService> service = module->service().promote(); |
| 195 | if (service == 0) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | service->sendRecognitionEvent(event, module); |
| 200 | } |
| 201 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 202 | sp<IMemory> SoundTriggerHwService::prepareRecognitionEvent( |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 203 | struct sound_trigger_recognition_event *event) |
| 204 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 205 | AutoMutex lock(mMemoryDealerLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 206 | sp<IMemory> eventMemory; |
| 207 | |
| 208 | //sanitize event |
| 209 | switch (event->type) { |
| 210 | case SOUND_MODEL_TYPE_KEYPHRASE: |
| 211 | ALOGW_IF(event->data_size != 0 && event->data_offset != |
| 212 | sizeof(struct sound_trigger_phrase_recognition_event), |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 213 | "prepareRecognitionEvent(): invalid data offset %u for keyphrase event type", |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 214 | event->data_offset); |
| 215 | event->data_offset = sizeof(struct sound_trigger_phrase_recognition_event); |
| 216 | break; |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 217 | case SOUND_MODEL_TYPE_GENERIC: |
| 218 | ALOGW_IF(event->data_size != 0 && event->data_offset != |
| 219 | sizeof(struct sound_trigger_generic_recognition_event), |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 220 | "prepareRecognitionEvent(): invalid data offset %u for generic event type", |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 221 | event->data_offset); |
| 222 | event->data_offset = sizeof(struct sound_trigger_generic_recognition_event); |
| 223 | break; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 224 | case SOUND_MODEL_TYPE_UNKNOWN: |
| 225 | ALOGW_IF(event->data_size != 0 && event->data_offset != |
| 226 | sizeof(struct sound_trigger_recognition_event), |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 227 | "prepareRecognitionEvent(): invalid data offset %u for unknown event type", |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 228 | event->data_offset); |
| 229 | event->data_offset = sizeof(struct sound_trigger_recognition_event); |
| 230 | break; |
| 231 | default: |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 232 | return eventMemory; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | size_t size = event->data_offset + event->data_size; |
| 236 | eventMemory = mMemoryDealer->allocate(size); |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 237 | if (eventMemory == 0 || eventMemory->unsecurePointer() == NULL) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 238 | eventMemory.clear(); |
| 239 | return eventMemory; |
| 240 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 241 | memcpy(eventMemory->unsecurePointer(), event, size); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 242 | |
| 243 | return eventMemory; |
| 244 | } |
| 245 | |
| 246 | void SoundTriggerHwService::sendRecognitionEvent(struct sound_trigger_recognition_event *event, |
| 247 | Module *module) |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 248 | { |
| 249 | if (module == NULL) { |
| 250 | return; |
| 251 | } |
| 252 | sp<IMemory> eventMemory = prepareRecognitionEvent(event); |
| 253 | if (eventMemory == 0) { |
| 254 | return; |
| 255 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 256 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 257 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_RECOGNITION, |
| 258 | eventMemory); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 259 | callbackEvent->setModule(module); |
| 260 | sendCallbackEvent(callbackEvent); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // static |
| 264 | void SoundTriggerHwService::soundModelCallback(struct sound_trigger_model_event *event, |
| 265 | void *cookie) |
| 266 | { |
| 267 | Module *module = (Module *)cookie; |
| 268 | if (module == NULL) { |
| 269 | return; |
| 270 | } |
| 271 | sp<SoundTriggerHwService> service = module->service().promote(); |
| 272 | if (service == 0) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | service->sendSoundModelEvent(event, module); |
| 277 | } |
| 278 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 279 | sp<IMemory> SoundTriggerHwService::prepareSoundModelEvent(struct sound_trigger_model_event *event) |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 280 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 281 | AutoMutex lock(mMemoryDealerLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 282 | sp<IMemory> eventMemory; |
| 283 | |
| 284 | size_t size = event->data_offset + event->data_size; |
| 285 | eventMemory = mMemoryDealer->allocate(size); |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 286 | if (eventMemory == 0 || eventMemory->unsecurePointer() == NULL) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 287 | eventMemory.clear(); |
| 288 | return eventMemory; |
| 289 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 290 | memcpy(eventMemory->unsecurePointer(), event, size); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 291 | |
| 292 | return eventMemory; |
| 293 | } |
| 294 | |
| 295 | void SoundTriggerHwService::sendSoundModelEvent(struct sound_trigger_model_event *event, |
| 296 | Module *module) |
| 297 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 298 | sp<IMemory> eventMemory = prepareSoundModelEvent(event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 299 | if (eventMemory == 0) { |
| 300 | return; |
| 301 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 302 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_SOUNDMODEL, |
| 303 | eventMemory); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 304 | callbackEvent->setModule(module); |
| 305 | sendCallbackEvent(callbackEvent); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 309 | sp<IMemory> SoundTriggerHwService::prepareServiceStateEvent(sound_trigger_service_state_t state) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 310 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 311 | AutoMutex lock(mMemoryDealerLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 312 | sp<IMemory> eventMemory; |
| 313 | |
| 314 | size_t size = sizeof(sound_trigger_service_state_t); |
| 315 | eventMemory = mMemoryDealer->allocate(size); |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 316 | if (eventMemory == 0 || eventMemory->unsecurePointer() == NULL) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 317 | eventMemory.clear(); |
| 318 | return eventMemory; |
| 319 | } |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 320 | *((sound_trigger_service_state_t *)eventMemory->unsecurePointer()) = state; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 321 | return eventMemory; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 324 | void SoundTriggerHwService::sendServiceStateEvent(sound_trigger_service_state_t state, |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 325 | Module *module) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 326 | { |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 327 | sp<IMemory> eventMemory = prepareServiceStateEvent(state); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 328 | if (eventMemory == 0) { |
| 329 | return; |
| 330 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 331 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_SERVICE_STATE, |
| 332 | eventMemory); |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 333 | callbackEvent->setModule(module); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 334 | sendCallbackEvent(callbackEvent); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 337 | void SoundTriggerHwService::sendServiceStateEvent(sound_trigger_service_state_t state, |
| 338 | ModuleClient *moduleClient) |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 339 | { |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 340 | sp<IMemory> eventMemory = prepareServiceStateEvent(state); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 341 | if (eventMemory == 0) { |
| 342 | return; |
| 343 | } |
| 344 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_SERVICE_STATE, |
| 345 | eventMemory); |
| 346 | callbackEvent->setModuleClient(moduleClient); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 347 | sendCallbackEvent(callbackEvent); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 350 | void SoundTriggerHwService::sendCallbackEvent(const sp<CallbackEvent>& event) |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 351 | { |
| 352 | mCallbackThread->sendCallbackEvent(event); |
| 353 | } |
| 354 | |
| 355 | void SoundTriggerHwService::onCallbackEvent(const sp<CallbackEvent>& event) |
| 356 | { |
| 357 | ALOGV("onCallbackEvent"); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 358 | sp<Module> module; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 359 | sp<ModuleClient> moduleClient; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 360 | { |
| 361 | AutoMutex lock(mServiceLock); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 362 | //CallbackEvent is either for Module or ModuleClient |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 363 | module = event->mModule.promote(); |
| 364 | if (module == 0) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 365 | moduleClient = event->mModuleClient.promote(); |
| 366 | if (moduleClient == 0) { |
| 367 | return; |
| 368 | } |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 369 | } else { |
| 370 | // Sanity check on this being a Module we know about. |
| 371 | bool foundModule = false; |
| 372 | for (size_t i = 0; i < mModules.size(); i++) { |
| 373 | if (mModules.valueAt(i).get() == module.get()) { |
| 374 | foundModule = true; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | if (!foundModule) { |
| 379 | ALOGE("onCallbackEvent for unknown module"); |
| 380 | return; |
| 381 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 384 | if (module != 0) { |
| 385 | ALOGV("onCallbackEvent for module"); |
| 386 | module->onCallbackEvent(event); |
| 387 | } else if (moduleClient != 0) { |
| 388 | ALOGV("onCallbackEvent for moduleClient"); |
| 389 | moduleClient->onCallbackEvent(event); |
| 390 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 391 | { |
| 392 | AutoMutex lock(mServiceLock); |
| 393 | // clear now to execute with mServiceLock locked |
| 394 | event->mMemory.clear(); |
| 395 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | #undef LOG_TAG |
| 399 | #define LOG_TAG "SoundTriggerHwService::CallbackThread" |
| 400 | |
| 401 | SoundTriggerHwService::CallbackThread::CallbackThread(const wp<SoundTriggerHwService>& service) |
| 402 | : mService(service) |
| 403 | { |
| 404 | } |
| 405 | |
| 406 | SoundTriggerHwService::CallbackThread::~CallbackThread() |
| 407 | { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 408 | while (!mEventQueue.isEmpty()) { |
| 409 | mEventQueue[0]->mMemory.clear(); |
| 410 | mEventQueue.removeAt(0); |
| 411 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void SoundTriggerHwService::CallbackThread::onFirstRef() |
| 415 | { |
| 416 | run("soundTrigger cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 417 | } |
| 418 | |
| 419 | bool SoundTriggerHwService::CallbackThread::threadLoop() |
| 420 | { |
| 421 | while (!exitPending()) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 422 | sp<CallbackEvent> event; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 423 | sp<SoundTriggerHwService> service; |
| 424 | { |
| 425 | Mutex::Autolock _l(mCallbackLock); |
| 426 | while (mEventQueue.isEmpty() && !exitPending()) { |
| 427 | ALOGV("CallbackThread::threadLoop() sleep"); |
| 428 | mCallbackCond.wait(mCallbackLock); |
| 429 | ALOGV("CallbackThread::threadLoop() wake up"); |
| 430 | } |
| 431 | if (exitPending()) { |
| 432 | break; |
| 433 | } |
| 434 | event = mEventQueue[0]; |
| 435 | mEventQueue.removeAt(0); |
| 436 | service = mService.promote(); |
| 437 | } |
| 438 | if (service != 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 439 | service->onCallbackEvent(event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | void SoundTriggerHwService::CallbackThread::exit() |
| 446 | { |
| 447 | Mutex::Autolock _l(mCallbackLock); |
| 448 | requestExit(); |
| 449 | mCallbackCond.broadcast(); |
| 450 | } |
| 451 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 452 | void SoundTriggerHwService::CallbackThread::sendCallbackEvent( |
| 453 | const sp<SoundTriggerHwService::CallbackEvent>& event) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 454 | { |
| 455 | AutoMutex lock(mCallbackLock); |
| 456 | mEventQueue.add(event); |
| 457 | mCallbackCond.signal(); |
| 458 | } |
| 459 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 460 | SoundTriggerHwService::CallbackEvent::CallbackEvent(event_type type, sp<IMemory> memory) |
| 461 | : mType(type), mMemory(memory) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 462 | { |
| 463 | } |
| 464 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 465 | SoundTriggerHwService::CallbackEvent::~CallbackEvent() |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 466 | { |
| 467 | } |
| 468 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 469 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 470 | #undef LOG_TAG |
| 471 | #define LOG_TAG "SoundTriggerHwService::Module" |
| 472 | |
| 473 | SoundTriggerHwService::Module::Module(const sp<SoundTriggerHwService>& service, |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 474 | const sp<SoundTriggerHalInterface>& halInterface, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 475 | sound_trigger_module_descriptor descriptor) |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 476 | : mService(service), mHalInterface(halInterface), mDescriptor(descriptor), |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 477 | mServiceState(SOUND_TRIGGER_STATE_NO_INIT) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 478 | { |
| 479 | } |
| 480 | |
| 481 | SoundTriggerHwService::Module::~Module() { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 482 | mModuleClients.clear(); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 485 | sp<SoundTriggerHwService::ModuleClient> |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 486 | SoundTriggerHwService::Module::addClient(const sp<ISoundTriggerClient>& client, |
| 487 | const String16& opPackageName) |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 488 | { |
| 489 | AutoMutex lock(mLock); |
| 490 | sp<ModuleClient> moduleClient; |
| 491 | |
| 492 | for (size_t i = 0; i < mModuleClients.size(); i++) { |
| 493 | if (mModuleClients[i]->client() == client) { |
| 494 | // Client already present, reuse client |
| 495 | return moduleClient; |
| 496 | } |
| 497 | } |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 498 | moduleClient = new ModuleClient(this, client, opPackageName); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 499 | |
| 500 | ALOGV("addClient() client %p", moduleClient.get()); |
| 501 | mModuleClients.add(moduleClient); |
| 502 | |
| 503 | return moduleClient; |
| 504 | } |
| 505 | |
| 506 | void SoundTriggerHwService::Module::detach(const sp<ModuleClient>& moduleClient) |
| 507 | { |
| 508 | ALOGV("Module::detach()"); |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 509 | Vector<audio_session_t> releasedSessions; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 510 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 511 | { |
| 512 | AutoMutex lock(mLock); |
| 513 | ssize_t index = -1; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 514 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 515 | for (size_t i = 0; i < mModuleClients.size(); i++) { |
| 516 | if (mModuleClients[i] == moduleClient) { |
| 517 | index = i; |
| 518 | break; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 519 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 520 | } |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 521 | if (index == -1) { |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | ALOGV("remove client %p", moduleClient.get()); |
| 526 | mModuleClients.removeAt(index); |
| 527 | |
| 528 | // Iterate in reverse order as models are removed from list inside the loop. |
| 529 | for (size_t i = mModels.size(); i > 0; i--) { |
| 530 | sp<Model> model = mModels.valueAt(i - 1); |
| 531 | if (moduleClient == model->mModuleClient) { |
| 532 | mModels.removeItemsAt(i - 1); |
| 533 | ALOGV("detach() unloading model %d", model->mHandle); |
| 534 | if (mHalInterface != 0) { |
| 535 | if (model->mState == Model::STATE_ACTIVE) { |
| 536 | mHalInterface->stopRecognition(model->mHandle); |
| 537 | } |
| 538 | mHalInterface->unloadSoundModel(model->mHandle); |
| 539 | } |
| 540 | releasedSessions.add(model->mCaptureSession); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | for (size_t i = 0; i < releasedSessions.size(); i++) { |
| 546 | // do not call AudioSystem methods with mLock held |
| 547 | AudioSystem::releaseSoundTriggerSession(releasedSessions[i]); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 548 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | status_t SoundTriggerHwService::Module::loadSoundModel(const sp<IMemory>& modelMemory, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 552 | sp<ModuleClient> moduleClient, |
| 553 | sound_model_handle_t *handle) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 554 | { |
| 555 | ALOGV("loadSoundModel() handle"); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 556 | if (mHalInterface == 0) { |
| 557 | return NO_INIT; |
| 558 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 559 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 560 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 561 | // (see declaration for details). |
| 562 | // Either document why it is safe in this case or address the |
| 563 | // issue (e.g. by copying). |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 564 | struct sound_trigger_sound_model *sound_model = |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 565 | (struct sound_trigger_sound_model *)modelMemory->unsecurePointer(); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 566 | |
Eric Laurent | bb00d8f | 2016-08-17 06:19:32 -0700 | [diff] [blame] | 567 | size_t structSize; |
| 568 | if (sound_model->type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 569 | structSize = sizeof(struct sound_trigger_phrase_sound_model); |
| 570 | } else { |
| 571 | structSize = sizeof(struct sound_trigger_sound_model); |
| 572 | } |
| 573 | |
| 574 | if (sound_model->data_offset < structSize || |
| 575 | sound_model->data_size > (UINT_MAX - sound_model->data_offset) || |
| 576 | modelMemory->size() < sound_model->data_offset || |
| 577 | sound_model->data_size > (modelMemory->size() - sound_model->data_offset)) { |
| 578 | android_errorWriteLog(0x534e4554, "30148546"); |
| 579 | ALOGE("loadSoundModel() data_size is too big"); |
| 580 | return BAD_VALUE; |
| 581 | } |
| 582 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 583 | audio_session_t session; |
| 584 | audio_io_handle_t ioHandle; |
| 585 | audio_devices_t device; |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 586 | // do not call AudioSystem methods with mLock held |
| 587 | status_t status = AudioSystem::acquireSoundTriggerSession(&session, &ioHandle, &device); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 588 | if (status != NO_ERROR) { |
| 589 | return status; |
| 590 | } |
| 591 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 592 | { |
| 593 | AutoMutex lock(mLock); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 594 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 595 | if (mModels.size() >= mDescriptor.properties.max_sound_models) { |
| 596 | ALOGW("loadSoundModel(): Not loading, max number of models (%d) would be exceeded", |
| 597 | mDescriptor.properties.max_sound_models); |
| 598 | status = INVALID_OPERATION; |
| 599 | goto exit; |
| 600 | } |
| 601 | |
| 602 | status = mHalInterface->loadSoundModel(sound_model, |
| 603 | SoundTriggerHwService::soundModelCallback, |
| 604 | this, handle); |
| 605 | if (status != NO_ERROR) { |
| 606 | goto exit; |
| 607 | } |
| 608 | |
| 609 | sp<Model> model = new Model(*handle, session, ioHandle, device, sound_model->type, |
| 610 | moduleClient); |
| 611 | mModels.replaceValueFor(*handle, model); |
| 612 | } |
| 613 | exit: |
| 614 | if (status != NO_ERROR) { |
| 615 | // do not call AudioSystem methods with mLock held |
| 616 | AudioSystem::releaseSoundTriggerSession(session); |
| 617 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 618 | return status; |
| 619 | } |
| 620 | |
| 621 | status_t SoundTriggerHwService::Module::unloadSoundModel(sound_model_handle_t handle) |
| 622 | { |
| 623 | ALOGV("unloadSoundModel() model handle %d", handle); |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 624 | status_t status; |
| 625 | audio_session_t session; |
Eric Laurent | 02eb47c | 2014-11-20 10:10:20 -0800 | [diff] [blame] | 626 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 627 | { |
| 628 | AutoMutex lock(mLock); |
| 629 | if (mHalInterface == 0) { |
| 630 | return NO_INIT; |
| 631 | } |
| 632 | ssize_t index = mModels.indexOfKey(handle); |
| 633 | if (index < 0) { |
| 634 | return BAD_VALUE; |
| 635 | } |
| 636 | sp<Model> model = mModels.valueAt(index); |
| 637 | mModels.removeItem(handle); |
| 638 | if (model->mState == Model::STATE_ACTIVE) { |
| 639 | mHalInterface->stopRecognition(model->mHandle); |
| 640 | model->mState = Model::STATE_IDLE; |
| 641 | } |
| 642 | status = mHalInterface->unloadSoundModel(handle); |
| 643 | session = model->mCaptureSession; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 644 | } |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 645 | // do not call AudioSystem methods with mLock held |
| 646 | AudioSystem::releaseSoundTriggerSession(session); |
| 647 | return status; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | status_t SoundTriggerHwService::Module::startRecognition(sound_model_handle_t handle, |
Eric Laurent | 0832b2d | 2014-07-06 16:17:25 -0700 | [diff] [blame] | 651 | const sp<IMemory>& dataMemory) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 652 | { |
| 653 | ALOGV("startRecognition() model handle %d", handle); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 654 | if (mHalInterface == 0) { |
| 655 | return NO_INIT; |
| 656 | } |
Eric Laurent | bb00d8f | 2016-08-17 06:19:32 -0700 | [diff] [blame] | 657 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 658 | // TODO: Using unsecurePointer() has some associated security pitfalls |
| 659 | // (see declaration for details). |
| 660 | // Either document why it is safe in this case or address the |
| 661 | // issue (e.g. by copying). |
Eric Laurent | bb00d8f | 2016-08-17 06:19:32 -0700 | [diff] [blame] | 662 | struct sound_trigger_recognition_config *config = |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 663 | (struct sound_trigger_recognition_config *)dataMemory->unsecurePointer(); |
Eric Laurent | bb00d8f | 2016-08-17 06:19:32 -0700 | [diff] [blame] | 664 | |
| 665 | if (config->data_offset < sizeof(struct sound_trigger_recognition_config) || |
| 666 | config->data_size > (UINT_MAX - config->data_offset) || |
| 667 | dataMemory->size() < config->data_offset || |
| 668 | config->data_size > (dataMemory->size() - config->data_offset)) { |
| 669 | ALOGE("startRecognition() data_size is too big"); |
| 670 | return BAD_VALUE; |
| 671 | } |
| 672 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 673 | AutoMutex lock(mLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 674 | if (mServiceState == SOUND_TRIGGER_STATE_DISABLED) { |
| 675 | return INVALID_OPERATION; |
| 676 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 677 | sp<Model> model = getModel(handle); |
| 678 | if (model == 0) { |
| 679 | return BAD_VALUE; |
| 680 | } |
| 681 | |
| 682 | if (model->mState == Model::STATE_ACTIVE) { |
| 683 | return INVALID_OPERATION; |
| 684 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 685 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 686 | |
| 687 | //TODO: get capture handle and device from audio policy service |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 688 | config->capture_handle = model->mCaptureIOHandle; |
| 689 | config->capture_device = model->mCaptureDevice; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 690 | status_t status = mHalInterface->startRecognition(handle, config, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 691 | SoundTriggerHwService::recognitionCallback, |
Eric Laurent | 0832b2d | 2014-07-06 16:17:25 -0700 | [diff] [blame] | 692 | this); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 693 | |
| 694 | if (status == NO_ERROR) { |
| 695 | model->mState = Model::STATE_ACTIVE; |
| 696 | model->mConfig = *config; |
| 697 | } |
| 698 | |
| 699 | return status; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | status_t SoundTriggerHwService::Module::stopRecognition(sound_model_handle_t handle) |
| 703 | { |
| 704 | ALOGV("stopRecognition() model handle %d", handle); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 705 | if (mHalInterface == 0) { |
| 706 | return NO_INIT; |
| 707 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 708 | AutoMutex lock(mLock); |
| 709 | sp<Model> model = getModel(handle); |
| 710 | if (model == 0) { |
| 711 | return BAD_VALUE; |
| 712 | } |
| 713 | |
| 714 | if (model->mState != Model::STATE_ACTIVE) { |
| 715 | return INVALID_OPERATION; |
| 716 | } |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 717 | mHalInterface->stopRecognition(handle); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 718 | model->mState = Model::STATE_IDLE; |
| 719 | return NO_ERROR; |
| 720 | } |
| 721 | |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 722 | status_t SoundTriggerHwService::Module::getModelState(sound_model_handle_t handle) |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 723 | { |
| 724 | ALOGV("getModelState() model handle %d", handle); |
| 725 | if (mHalInterface == 0) { |
| 726 | return NO_INIT; |
| 727 | } |
| 728 | AutoMutex lock(mLock); |
| 729 | sp<Model> model = getModel(handle); |
| 730 | if (model == 0) { |
| 731 | return BAD_VALUE; |
| 732 | } |
| 733 | |
| 734 | if (model->mState != Model::STATE_ACTIVE) { |
| 735 | return INVALID_OPERATION; |
| 736 | } |
| 737 | |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 738 | return mHalInterface->getModelState(handle); |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 741 | void SoundTriggerHwService::Module::onCallbackEvent(const sp<CallbackEvent>& event) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 742 | { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 743 | ALOGV("onCallbackEvent type %d", event->mType); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 744 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 745 | // Memory is coming from a trusted process. |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 746 | sp<IMemory> eventMemory = event->mMemory; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 747 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 748 | if (eventMemory == 0 || eventMemory->unsecurePointer() == NULL) { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 749 | return; |
| 750 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 751 | if (mModuleClients.isEmpty()) { |
| 752 | ALOGI("%s no clients", __func__); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 753 | return; |
| 754 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 755 | |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 756 | Vector< sp<ModuleClient> > clients; |
| 757 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 758 | switch (event->mType) { |
| 759 | case CallbackEvent::TYPE_RECOGNITION: { |
| 760 | struct sound_trigger_recognition_event *recognitionEvent = |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 761 | (struct sound_trigger_recognition_event *)eventMemory->unsecurePointer(); |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 762 | { |
| 763 | AutoMutex lock(mLock); |
| 764 | sp<Model> model = getModel(recognitionEvent->model); |
| 765 | if (model == 0) { |
| 766 | ALOGW("%s model == 0", __func__); |
| 767 | return; |
| 768 | } |
| 769 | if (model->mState != Model::STATE_ACTIVE) { |
| 770 | ALOGV("onCallbackEvent model->mState %d != Model::STATE_ACTIVE", model->mState); |
| 771 | return; |
| 772 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 773 | |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 774 | recognitionEvent->capture_session = model->mCaptureSession; |
mike dooley | fd01872 | 2019-01-04 09:31:31 +0100 | [diff] [blame] | 775 | model->mState = Model::STATE_IDLE; |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 776 | clients.add(model->mModuleClient); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 777 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 778 | } break; |
| 779 | case CallbackEvent::TYPE_SOUNDMODEL: { |
| 780 | struct sound_trigger_model_event *soundmodelEvent = |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 781 | (struct sound_trigger_model_event *)eventMemory->unsecurePointer(); |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 782 | { |
| 783 | AutoMutex lock(mLock); |
| 784 | sp<Model> model = getModel(soundmodelEvent->model); |
| 785 | if (model == 0) { |
| 786 | ALOGW("%s model == 0", __func__); |
| 787 | return; |
| 788 | } |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 789 | clients.add(model->mModuleClient); |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 790 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 791 | } break; |
| 792 | case CallbackEvent::TYPE_SERVICE_STATE: { |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 793 | { |
| 794 | AutoMutex lock(mLock); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 795 | for (size_t i = 0; i < mModuleClients.size(); i++) { |
| 796 | if (mModuleClients[i] != 0) { |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 797 | clients.add(mModuleClients[i]); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 800 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 801 | } break; |
| 802 | default: |
| 803 | LOG_ALWAYS_FATAL("onCallbackEvent unknown event type %d", event->mType); |
| 804 | } |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 805 | |
| 806 | for (size_t i = 0; i < clients.size(); i++) { |
| 807 | clients[i]->onCallbackEvent(event); |
| 808 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | sp<SoundTriggerHwService::Model> SoundTriggerHwService::Module::getModel( |
| 812 | sound_model_handle_t handle) |
| 813 | { |
| 814 | sp<Model> model; |
| 815 | ssize_t index = mModels.indexOfKey(handle); |
| 816 | if (index >= 0) { |
| 817 | model = mModels.valueAt(index); |
| 818 | } |
| 819 | return model; |
| 820 | } |
| 821 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 822 | // Called with mServiceLock held |
| 823 | void SoundTriggerHwService::Module::setCaptureState_l(bool active) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 824 | { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 825 | ALOGV("Module::setCaptureState_l %d", active); |
| 826 | sp<SoundTriggerHwService> service; |
| 827 | sound_trigger_service_state_t state; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 828 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 829 | Vector< sp<IMemory> > events; |
| 830 | { |
| 831 | AutoMutex lock(mLock); |
| 832 | state = (active && !mDescriptor.properties.concurrent_capture) ? |
| 833 | SOUND_TRIGGER_STATE_DISABLED : SOUND_TRIGGER_STATE_ENABLED; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 834 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 835 | if (state == mServiceState) { |
| 836 | return; |
| 837 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 838 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 839 | mServiceState = state; |
| 840 | |
| 841 | service = mService.promote(); |
| 842 | if (service == 0) { |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | if (state == SOUND_TRIGGER_STATE_ENABLED) { |
| 847 | goto exit; |
| 848 | } |
| 849 | |
Chris Thornton | efcf16c | 2016-03-27 17:13:28 -0700 | [diff] [blame] | 850 | const bool supports_stop_all = |
Chris Thornton | de22f8a | 2017-08-29 16:46:37 -0700 | [diff] [blame] | 851 | (mHalInterface != 0) && (mHalInterface->stopAllRecognitions() != -ENOSYS); |
Chris Thornton | efcf16c | 2016-03-27 17:13:28 -0700 | [diff] [blame] | 852 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 853 | for (size_t i = 0; i < mModels.size(); i++) { |
| 854 | sp<Model> model = mModels.valueAt(i); |
| 855 | if (model->mState == Model::STATE_ACTIVE) { |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 856 | if (mHalInterface != 0 && !supports_stop_all) { |
| 857 | mHalInterface->stopRecognition(model->mHandle); |
Chris Thornton | efcf16c | 2016-03-27 17:13:28 -0700 | [diff] [blame] | 858 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 859 | // keep model in ACTIVE state so that event is processed by onCallbackEvent() |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 860 | if (model->mType == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 861 | struct sound_trigger_phrase_recognition_event event; |
| 862 | memset(&event, 0, sizeof(struct sound_trigger_phrase_recognition_event)); |
| 863 | event.num_phrases = model->mConfig.num_phrases; |
| 864 | for (size_t i = 0; i < event.num_phrases; i++) { |
| 865 | event.phrase_extras[i] = model->mConfig.phrases[i]; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 866 | } |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 867 | event.common.status = RECOGNITION_STATUS_ABORT; |
| 868 | event.common.type = model->mType; |
| 869 | event.common.model = model->mHandle; |
| 870 | event.common.data_size = 0; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 871 | sp<IMemory> eventMemory = service->prepareRecognitionEvent(&event.common); |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 872 | if (eventMemory != 0) { |
| 873 | events.add(eventMemory); |
| 874 | } |
| 875 | } else if (model->mType == SOUND_MODEL_TYPE_GENERIC) { |
| 876 | struct sound_trigger_generic_recognition_event event; |
| 877 | memset(&event, 0, sizeof(struct sound_trigger_generic_recognition_event)); |
| 878 | event.common.status = RECOGNITION_STATUS_ABORT; |
| 879 | event.common.type = model->mType; |
| 880 | event.common.model = model->mHandle; |
| 881 | event.common.data_size = 0; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 882 | sp<IMemory> eventMemory = service->prepareRecognitionEvent(&event.common); |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 883 | if (eventMemory != 0) { |
| 884 | events.add(eventMemory); |
| 885 | } |
Ryan Bavetta | 9609a91 | 2016-01-28 19:22:29 -0800 | [diff] [blame] | 886 | } else if (model->mType == SOUND_MODEL_TYPE_UNKNOWN) { |
| 887 | struct sound_trigger_phrase_recognition_event event; |
| 888 | memset(&event, 0, sizeof(struct sound_trigger_phrase_recognition_event)); |
| 889 | event.common.status = RECOGNITION_STATUS_ABORT; |
| 890 | event.common.type = model->mType; |
| 891 | event.common.model = model->mHandle; |
| 892 | event.common.data_size = 0; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 893 | sp<IMemory> eventMemory = service->prepareRecognitionEvent(&event.common); |
Ryan Bavetta | 9609a91 | 2016-01-28 19:22:29 -0800 | [diff] [blame] | 894 | if (eventMemory != 0) { |
| 895 | events.add(eventMemory); |
| 896 | } |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 897 | } else { |
| 898 | goto exit; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 902 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 903 | |
| 904 | for (size_t i = 0; i < events.size(); i++) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 905 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_RECOGNITION, |
| 906 | events[i]); |
| 907 | callbackEvent->setModule(this); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 908 | service->sendCallbackEvent(callbackEvent); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | exit: |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 912 | service->sendServiceStateEvent(state, this); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 915 | |
| 916 | SoundTriggerHwService::Model::Model(sound_model_handle_t handle, audio_session_t session, |
| 917 | audio_io_handle_t ioHandle, audio_devices_t device, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 918 | sound_trigger_sound_model_type_t type, |
| 919 | sp<ModuleClient>& moduleClient) : |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 920 | mHandle(handle), mState(STATE_IDLE), mCaptureSession(session), |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 921 | mCaptureIOHandle(ioHandle), mCaptureDevice(device), mType(type), |
| 922 | mModuleClient(moduleClient) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 923 | { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 926 | #undef LOG_TAG |
| 927 | #define LOG_TAG "SoundTriggerHwService::ModuleClient" |
| 928 | |
| 929 | SoundTriggerHwService::ModuleClient::ModuleClient(const sp<Module>& module, |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 930 | const sp<ISoundTriggerClient>& client, |
| 931 | const String16& opPackageName) |
| 932 | : mModule(module), mClient(client), mOpPackageName(opPackageName) |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 933 | { |
| 934 | } |
| 935 | |
| 936 | void SoundTriggerHwService::ModuleClient::onFirstRef() |
| 937 | { |
Chris Thornton | c8a9f4a | 2017-02-06 18:31:42 -0800 | [diff] [blame] | 938 | sp<IBinder> binder = IInterface::asBinder(mClient); |
| 939 | if (binder != 0) { |
| 940 | binder->linkToDeath(this); |
| 941 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | SoundTriggerHwService::ModuleClient::~ModuleClient() |
| 945 | { |
| 946 | } |
| 947 | |
| 948 | status_t SoundTriggerHwService::ModuleClient::dump(int fd __unused, |
| 949 | const Vector<String16>& args __unused) { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 950 | String8 result; |
| 951 | return NO_ERROR; |
| 952 | } |
| 953 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 954 | void SoundTriggerHwService::ModuleClient::detach() { |
| 955 | ALOGV("detach()"); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 956 | if (!captureHotwordAllowed(mOpPackageName, |
| 957 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 958 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 959 | return; |
| 960 | } |
| 961 | |
| 962 | { |
| 963 | AutoMutex lock(mLock); |
| 964 | if (mClient != 0) { |
| 965 | IInterface::asBinder(mClient)->unlinkToDeath(this); |
| 966 | mClient.clear(); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | sp<Module> module = mModule.promote(); |
| 971 | if (module == 0) { |
| 972 | return; |
| 973 | } |
| 974 | module->detach(this); |
| 975 | } |
| 976 | |
| 977 | status_t SoundTriggerHwService::ModuleClient::loadSoundModel(const sp<IMemory>& modelMemory, |
| 978 | sound_model_handle_t *handle) |
| 979 | { |
| 980 | ALOGV("loadSoundModel() handle"); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 981 | if (!captureHotwordAllowed(mOpPackageName, |
| 982 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 983 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 984 | return PERMISSION_DENIED; |
| 985 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 986 | if (checkIMemory(modelMemory) != NO_ERROR) { |
| 987 | return BAD_VALUE; |
| 988 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 989 | |
| 990 | sp<Module> module = mModule.promote(); |
| 991 | if (module == 0) { |
| 992 | return NO_INIT; |
| 993 | } |
| 994 | return module->loadSoundModel(modelMemory, this, handle); |
| 995 | } |
| 996 | |
| 997 | status_t SoundTriggerHwService::ModuleClient::unloadSoundModel(sound_model_handle_t handle) |
| 998 | { |
| 999 | ALOGV("unloadSoundModel() model handle %d", handle); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 1000 | if (!captureHotwordAllowed(mOpPackageName, |
| 1001 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 1002 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1003 | return PERMISSION_DENIED; |
| 1004 | } |
| 1005 | |
| 1006 | sp<Module> module = mModule.promote(); |
| 1007 | if (module == 0) { |
| 1008 | return NO_INIT; |
| 1009 | } |
| 1010 | return module->unloadSoundModel(handle); |
| 1011 | } |
| 1012 | |
| 1013 | status_t SoundTriggerHwService::ModuleClient::startRecognition(sound_model_handle_t handle, |
| 1014 | const sp<IMemory>& dataMemory) |
| 1015 | { |
| 1016 | ALOGV("startRecognition() model handle %d", handle); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 1017 | if (!captureHotwordAllowed(mOpPackageName, |
| 1018 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 1019 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1020 | return PERMISSION_DENIED; |
| 1021 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 1022 | if (checkIMemory(dataMemory) != NO_ERROR) { |
| 1023 | return BAD_VALUE; |
| 1024 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1025 | |
| 1026 | sp<Module> module = mModule.promote(); |
| 1027 | if (module == 0) { |
| 1028 | return NO_INIT; |
| 1029 | } |
| 1030 | return module->startRecognition(handle, dataMemory); |
| 1031 | } |
| 1032 | |
| 1033 | status_t SoundTriggerHwService::ModuleClient::stopRecognition(sound_model_handle_t handle) |
| 1034 | { |
| 1035 | ALOGV("stopRecognition() model handle %d", handle); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 1036 | if (!captureHotwordAllowed(mOpPackageName, |
| 1037 | IPCThreadState::self()->getCallingPid(), |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 1038 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1039 | return PERMISSION_DENIED; |
| 1040 | } |
| 1041 | |
| 1042 | sp<Module> module = mModule.promote(); |
| 1043 | if (module == 0) { |
| 1044 | return NO_INIT; |
| 1045 | } |
| 1046 | return module->stopRecognition(handle); |
| 1047 | } |
| 1048 | |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 1049 | status_t SoundTriggerHwService::ModuleClient::getModelState(sound_model_handle_t handle) |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 1050 | { |
| 1051 | ALOGV("getModelState() model handle %d", handle); |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 1052 | if (!captureHotwordAllowed(mOpPackageName, |
| 1053 | IPCThreadState::self()->getCallingPid(), |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 1054 | IPCThreadState::self()->getCallingUid())) { |
| 1055 | return PERMISSION_DENIED; |
| 1056 | } |
| 1057 | |
| 1058 | sp<Module> module = mModule.promote(); |
| 1059 | if (module == 0) { |
| 1060 | return NO_INIT; |
| 1061 | } |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 1062 | return module->getModelState(handle); |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1065 | void SoundTriggerHwService::ModuleClient::setCaptureState_l(bool active) |
| 1066 | { |
| 1067 | ALOGV("ModuleClient::setCaptureState_l %d", active); |
| 1068 | sp<SoundTriggerHwService> service; |
| 1069 | sound_trigger_service_state_t state; |
| 1070 | |
| 1071 | sp<Module> module = mModule.promote(); |
| 1072 | if (module == 0) { |
| 1073 | return; |
| 1074 | } |
| 1075 | { |
| 1076 | AutoMutex lock(mLock); |
| 1077 | state = (active && !module->isConcurrentCaptureAllowed()) ? |
| 1078 | SOUND_TRIGGER_STATE_DISABLED : SOUND_TRIGGER_STATE_ENABLED; |
| 1079 | |
| 1080 | service = module->service().promote(); |
| 1081 | if (service == 0) { |
| 1082 | return; |
| 1083 | } |
| 1084 | } |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 1085 | service->sendServiceStateEvent(state, this); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | void SoundTriggerHwService::ModuleClient::onCallbackEvent(const sp<CallbackEvent>& event) |
| 1089 | { |
| 1090 | ALOGV("ModuleClient onCallbackEvent type %d", event->mType); |
| 1091 | |
| 1092 | sp<IMemory> eventMemory = event->mMemory; |
| 1093 | |
Ytai Ben-Tsvi | 7dd3972 | 2019-09-05 15:14:30 -0700 | [diff] [blame] | 1094 | // Memory is coming from a trusted process. |
| 1095 | if (eventMemory == 0 || eventMemory->unsecurePointer() == NULL) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 1099 | sp<ISoundTriggerClient> client; |
| 1100 | { |
| 1101 | AutoMutex lock(mLock); |
| 1102 | client = mClient; |
| 1103 | } |
| 1104 | |
| 1105 | if (client != 0) { |
| 1106 | switch (event->mType) { |
| 1107 | case CallbackEvent::TYPE_RECOGNITION: { |
| 1108 | client->onRecognitionEvent(eventMemory); |
| 1109 | } break; |
| 1110 | case CallbackEvent::TYPE_SOUNDMODEL: { |
| 1111 | client->onSoundModelEvent(eventMemory); |
| 1112 | } break; |
| 1113 | case CallbackEvent::TYPE_SERVICE_STATE: { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1114 | client->onServiceStateChange(eventMemory); |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 1115 | } break; |
| 1116 | default: |
| 1117 | LOG_ALWAYS_FATAL("onCallbackEvent unknown event type %d", event->mType); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1118 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | void SoundTriggerHwService::ModuleClient::binderDied( |
| 1123 | const wp<IBinder> &who __unused) { |
| 1124 | ALOGW("client binder died for client %p", this); |
| 1125 | detach(); |
| 1126 | } |
| 1127 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1128 | }; // namespace android |