1 /* 2 * Copyright (C) 2016 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 #ifndef ANDROID_HARDWARE_SOUNDTRIGGER_V2_0_IMPLEMENTATION_H 18 #define ANDROID_HARDWARE_SOUNDTRIGGER_V2_0_IMPLEMENTATION_H 19 20 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h> 21 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h> 22 #include <hidl/Status.h> 23 #include <stdatomic.h> 24 #include <utils/threads.h> 25 #include <utils/KeyedVector.h> 26 #include <system/sound_trigger.h> 27 #include <hardware/sound_trigger.h> 28 29 namespace android { 30 namespace hardware { 31 namespace soundtrigger { 32 namespace V2_0 { 33 namespace implementation { 34 35 using ::android::hardware::audio::common::V2_0::Uuid; 36 using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback; 37 38 39 class SoundTriggerHalImpl : public ISoundTriggerHw { 40 public: 41 SoundTriggerHalImpl(); 42 43 // Methods from ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw follow. 44 Return<void> getProperties(getProperties_cb _hidl_cb) override; 45 Return<void> loadSoundModel(const ISoundTriggerHw::SoundModel& soundModel, 46 const sp<ISoundTriggerHwCallback>& callback, 47 ISoundTriggerHwCallback::CallbackCookie cookie, 48 loadSoundModel_cb _hidl_cb) override; 49 Return<void> loadPhraseSoundModel(const ISoundTriggerHw::PhraseSoundModel& soundModel, 50 const sp<ISoundTriggerHwCallback>& callback, 51 ISoundTriggerHwCallback::CallbackCookie cookie, 52 loadPhraseSoundModel_cb _hidl_cb) override; 53 54 Return<int32_t> unloadSoundModel(SoundModelHandle modelHandle) override; 55 Return<int32_t> startRecognition(SoundModelHandle modelHandle, 56 const ISoundTriggerHw::RecognitionConfig& config, 57 const sp<ISoundTriggerHwCallback>& callback, 58 ISoundTriggerHwCallback::CallbackCookie cookie) override; 59 Return<int32_t> stopRecognition(SoundModelHandle modelHandle) override; 60 Return<int32_t> stopAllRecognitions() override; 61 62 // RefBase 63 virtual void onFirstRef(); 64 65 static void soundModelCallback(struct sound_trigger_model_event *halEvent, 66 void *cookie); 67 static void recognitionCallback(struct sound_trigger_recognition_event *halEvent, 68 void *cookie); 69 70 private: 71 72 class SoundModelClient : public RefBase { 73 public: 74 SoundModelClient(uint32_t id, sp<ISoundTriggerHwCallback> callback, 75 ISoundTriggerHwCallback::CallbackCookie cookie) 76 : mId(id), mCallback(callback), mCookie(cookie) {} 77 virtual ~SoundModelClient() {} 78 79 uint32_t mId; 80 sound_model_handle_t mHalHandle; 81 sp<ISoundTriggerHwCallback> mCallback; 82 ISoundTriggerHwCallback::CallbackCookie mCookie; 83 }; 84 85 uint32_t nextUniqueId(); 86 void convertUuidFromHal(Uuid *uuid, 87 const sound_trigger_uuid_t *halUuid); 88 void convertUuidToHal(sound_trigger_uuid_t *halUuid, 89 const Uuid *uuid); 90 void convertPropertiesFromHal(ISoundTriggerHw::Properties *properties, 91 const struct sound_trigger_properties *halProperties); 92 void convertTriggerPhraseToHal(struct sound_trigger_phrase *halTriggerPhrase, 93 const ISoundTriggerHw::Phrase *triggerPhrase); 94 // returned HAL sound model must be freed by caller 95 struct sound_trigger_sound_model *convertSoundModelToHal( 96 const ISoundTriggerHw::SoundModel *soundModel); 97 void convertPhraseRecognitionExtraToHal( 98 struct sound_trigger_phrase_recognition_extra *halExtra, 99 const PhraseRecognitionExtra *extra); 100 // returned recognition config must be freed by caller 101 struct sound_trigger_recognition_config *convertRecognitionConfigToHal( 102 const ISoundTriggerHw::RecognitionConfig *config); 103 104 105 static void convertSoundModelEventFromHal(ISoundTriggerHwCallback::ModelEvent *event, 106 const struct sound_trigger_model_event *halEvent); 107 static ISoundTriggerHwCallback::RecognitionEvent *convertRecognitionEventFromHal( 108 const struct sound_trigger_recognition_event *halEvent); 109 static void convertPhraseRecognitionExtraFromHal(PhraseRecognitionExtra *extra, 110 const struct sound_trigger_phrase_recognition_extra *halExtra); 111 112 int doLoadSoundModel(const ISoundTriggerHw::SoundModel& soundModel, 113 const sp<ISoundTriggerHwCallback>& callback, 114 ISoundTriggerHwCallback::CallbackCookie cookie, 115 uint32_t *modelId); 116 117 virtual ~SoundTriggerHalImpl(); 118 119 const char * mModuleName; 120 struct sound_trigger_hw_device* mHwDevice; 121 volatile atomic_uint_fast32_t mNextModelId; 122 DefaultKeyedVector<int32_t, sp<SoundModelClient> > mClients; 123 Mutex mLock; 124 }; 125 126 extern "C" ISoundTriggerHw *HIDL_FETCH_ISoundTriggerHw(const char *name); 127 128 } // namespace implementation 129 } // namespace V2_0 130 } // namespace soundtrigger 131 } // namespace hardware 132 } // namespace android 133 134 #endif // ANDROID_HARDWARE_SOUNDTRIGGER_V2_0_IMPLEMENTATION_H 135 136