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_HAL_HIDL_H
18#define ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H
19
20#include <utility>
21
22#include <stdatomic.h>
23#include <utils/RefBase.h>
24#include <utils/KeyedVector.h>
25#include <utils/Vector.h>
26#include <utils/threads.h>
27#include "SoundTriggerHalInterface.h"
28#include <android/hardware/soundtrigger/2.0/types.h>
29#include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
30#include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
31#include <android/hardware/soundtrigger/2.1/ISoundTriggerHwCallback.h>
32
33namespace android {
34
35using ::android::hardware::audio::common::V2_0::Uuid;
36using ::android::hardware::hidl_vec;
37using ::android::hardware::soundtrigger::V2_0::ConfidenceLevel;
38using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
39using ::android::hardware::soundtrigger::V2_0::SoundModelType;
40using ::android::hardware::soundtrigger::V2_0::SoundModelHandle;
41using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
42using V2_0_ISoundTriggerHwCallback =
43        ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
44using V2_1_ISoundTriggerHw =
45        ::android::hardware::soundtrigger::V2_1::ISoundTriggerHw;
46using V2_1_ISoundTriggerHwCallback =
47        ::android::hardware::soundtrigger::V2_1::ISoundTriggerHwCallback;
48using ::android::hidl::memory::V1_0::IMemory;
49
50class SoundTriggerHalHidl : public SoundTriggerHalInterface,
51                            public virtual V2_1_ISoundTriggerHwCallback
52
53{
54public:
55        virtual int getProperties(struct sound_trigger_properties *properties);
56
57        /*
58         * Load a sound model. Once loaded, recognition of this model can be started and stopped.
59         * Only one active recognition per model at a time. The SoundTrigger service will handle
60         * concurrent recognition requests by different users/applications on the same model.
61         * The implementation returns a unique handle used by other functions (unload_sound_model(),
62         * start_recognition(), etc...
63         */
64        virtual int loadSoundModel(struct sound_trigger_sound_model *sound_model,
65                                sound_model_callback_t callback,
66                                void *cookie,
67                                sound_model_handle_t *handle);
68
69        /*
70         * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome
71         * implementation limitations.
72         */
73        virtual int unloadSoundModel(sound_model_handle_t handle);
74
75        /* Start recognition on a given model. Only one recognition active at a time per model.
76         * Once recognition succeeds of fails, the callback is called.
77         * TODO: group recognition configuration parameters into one struct and add key phrase options.
78         */
79        virtual int startRecognition(sound_model_handle_t handle,
80                                 const struct sound_trigger_recognition_config *config,
81                                 recognition_callback_t callback,
82                                 void *cookie);
83
84        /* Stop recognition on a given model.
85         * The implementation does not have to call the callback when stopped via this method.
86         */
87        virtual int stopRecognition(sound_model_handle_t handle);
88
89        /* Stop recognition on all models.
90         * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_1 or above.
91         * If no implementation is provided, stop_recognition will be called for each running model.
92         */
93        virtual int stopAllRecognitions();
94
95        // ISoundTriggerHwCallback
96        virtual ::android::hardware::Return<void> recognitionCallback(
97                const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
98        virtual ::android::hardware::Return<void> phraseRecognitionCallback(
99                const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
100        virtual ::android::hardware::Return<void> soundModelCallback(
101                const V2_0_ISoundTriggerHwCallback::ModelEvent& event, CallbackCookie cookie);
102        virtual ::android::hardware::Return<void> recognitionCallback_2_1(
103                const RecognitionEvent& event, CallbackCookie cookie);
104        virtual ::android::hardware::Return<void> phraseRecognitionCallback_2_1(
105                const PhraseRecognitionEvent& event, int32_t cookie);
106        virtual ::android::hardware::Return<void> soundModelCallback_2_1(
107                const ModelEvent& event, CallbackCookie cookie);
108private:
109        class SoundModel : public RefBase {
110        public:
111            SoundModel(sound_model_handle_t handle, sound_model_callback_t callback,
112                       void *cookie, android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle)
113                 : mHandle(handle), mHalHandle(halHandle),
114                   mSoundModelCallback(callback), mSoundModelCookie(cookie),
115                   mRecognitionCallback(NULL), mRecognitionCookie(NULL) {}
116            ~SoundModel() {}
117
118            sound_model_handle_t   mHandle;
119            android::hardware::soundtrigger::V2_0::SoundModelHandle mHalHandle;
120            sound_model_callback_t mSoundModelCallback;
121            void *                 mSoundModelCookie;
122            recognition_callback_t mRecognitionCallback;
123            void *                 mRecognitionCookie;
124        };
125
126        friend class SoundTriggerHalInterface;
127
128        explicit SoundTriggerHalHidl(const char *moduleName = NULL);
129        virtual  ~SoundTriggerHalHidl();
130
131        void convertUuidToHal(Uuid *halUuid,
132                              const sound_trigger_uuid_t *uuid);
133        void convertUuidFromHal(sound_trigger_uuid_t *uuid,
134                                const Uuid *halUuid);
135
136        void convertPropertiesFromHal(
137                struct sound_trigger_properties *properties,
138                const ISoundTriggerHw::Properties *halProperties);
139
140        void convertTriggerPhraseToHal(
141                ISoundTriggerHw::Phrase *halTriggerPhrase,
142                const struct sound_trigger_phrase *triggerPhrase);
143        void convertTriggerPhrasesToHal(
144                hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases,
145                struct sound_trigger_phrase_sound_model *keyPhraseModel);
146        void convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel,
147                const struct sound_trigger_sound_model *soundModel);
148        std::pair<bool, sp<IMemory>> convertSoundModelToHal(
149                V2_1_ISoundTriggerHw::SoundModel *halModel,
150                const struct sound_trigger_sound_model *soundModel)
151                __attribute__((warn_unused_result));
152        void convertPhraseSoundModelToHal(ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel,
153                const struct sound_trigger_sound_model *soundModel);
154        std::pair<bool, sp<IMemory>> convertPhraseSoundModelToHal(
155                V2_1_ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel,
156                const struct sound_trigger_sound_model *soundModel)
157                __attribute__((warn_unused_result));
158
159        void convertPhraseRecognitionExtraToHal(
160                PhraseRecognitionExtra *halExtra,
161                const struct sound_trigger_phrase_recognition_extra *extra);
162        void convertRecognitionConfigToHal(ISoundTriggerHw::RecognitionConfig *halConfig,
163                const struct sound_trigger_recognition_config *config);
164        std::pair<bool, sp<IMemory>> convertRecognitionConfigToHal(
165                V2_1_ISoundTriggerHw::RecognitionConfig *halConfig,
166                const struct sound_trigger_recognition_config *config)
167                __attribute__((warn_unused_result));
168
169        struct sound_trigger_model_event *convertSoundModelEventFromHal(
170                                              const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent);
171        void convertPhraseRecognitionExtraFromHal(
172                struct sound_trigger_phrase_recognition_extra *extra,
173                const PhraseRecognitionExtra *halExtra);
174        struct sound_trigger_phrase_recognition_event* convertPhraseRecognitionEventFromHal(
175                const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent);
176        struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
177                const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
178        void fillRecognitionEventFromHal(
179                struct sound_trigger_recognition_event *event,
180                const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
181
182        uint32_t nextUniqueId();
183        sp<ISoundTriggerHw> getService();
184        sp<V2_1_ISoundTriggerHw> toService2_1(const sp<ISoundTriggerHw>& s);
185        sp<SoundModel> getModel(sound_model_handle_t handle);
186        sp<SoundModel> removeModel(sound_model_handle_t handle);
187
188        static pthread_once_t sOnceControl;
189        static void sOnceInit();
190
191        Mutex mLock;
192        Mutex mHalLock;
193        const char *mModuleName;
194        volatile atomic_uint_fast32_t  mNextUniqueId;
195        // Effect chains without a valid thread
196        DefaultKeyedVector< sound_model_handle_t , sp<SoundModel> > mSoundModels;
197        sp<::android::hardware::soundtrigger::V2_0::ISoundTriggerHw> mISoundTrigger;
198};
199
200} // namespace android
201
202#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H
203