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 #define LOG_TAG "SoundTriggerHidlHalTest"
18 #include <stdlib.h>
19 #include <time.h>
20 
21 #include <condition_variable>
22 #include <mutex>
23 
24 #include <android/log.h>
25 #include <cutils/native_handle.h>
26 #include <gtest/gtest.h>
27 #include <hidl/GtestPrinter.h>
28 #include <hidl/ServiceManagement.h>
29 #include <log/log.h>
30 
31 #include <android/hardware/audio/common/2.0/types.h>
32 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h>
33 #include <android/hardware/soundtrigger/2.0/types.h>
34 
35 #define SHORT_TIMEOUT_PERIOD (1)
36 
37 using ::android::hardware::audio::common::V2_0::AudioDevice;
38 using ::android::hardware::soundtrigger::V2_0::SoundModelHandle;
39 using ::android::hardware::soundtrigger::V2_0::SoundModelType;
40 using ::android::hardware::soundtrigger::V2_0::RecognitionMode;
41 using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
42 using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
43 using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
44 using ::android::hardware::Return;
45 using ::android::hardware::Void;
46 using ::android::sp;
47 
48 /**
49  * Test code uses this class to wait for notification from callback.
50  */
51 class Monitor {
52  public:
Monitor()53   Monitor() : mCount(0) {}
54 
55   /**
56    * Adds 1 to the internal counter and unblocks one of the waiting threads.
57    */
notify()58   void notify() {
59     std::unique_lock<std::mutex> lock(mMtx);
60     mCount++;
61     mCv.notify_one();
62   }
63 
64   /**
65    * Blocks until the internal counter becomes greater than 0.
66    *
67    * If notified, this method decreases the counter by 1 and returns true.
68    * If timeout, returns false.
69    */
wait(int timeoutSeconds)70   bool wait(int timeoutSeconds) {
71     std::unique_lock<std::mutex> lock(mMtx);
72     auto deadline = std::chrono::system_clock::now() +
73         std::chrono::seconds(timeoutSeconds);
74     while (mCount == 0) {
75       if (mCv.wait_until(lock, deadline) == std::cv_status::timeout) {
76         return false;
77       }
78     }
79     mCount--;
80     return true;
81   }
82 
83  private:
84   std::mutex mMtx;
85   std::condition_variable mCv;
86   int mCount;
87 };
88 
89 // The main test class for Sound Trigger HIDL HAL.
90 class SoundTriggerHidlTest : public ::testing::TestWithParam<std::string> {
91  public:
SetUp()92   virtual void SetUp() override {
93       mSoundTriggerHal = ISoundTriggerHw::getService(GetParam());
94       ASSERT_NE(nullptr, mSoundTriggerHal.get());
95       mCallback = new SoundTriggerHwCallback(*this);
96       ASSERT_NE(nullptr, mCallback.get());
97   }
98 
SetUpTestCase()99   static void SetUpTestCase() {
100     srand(time(nullptr));
101   }
102 
103   class SoundTriggerHwCallback : public ISoundTriggerHwCallback {
104    private:
105     SoundTriggerHidlTest& mParent;
106 
107    public:
SoundTriggerHwCallback(SoundTriggerHidlTest & parent)108     SoundTriggerHwCallback(SoundTriggerHidlTest& parent) : mParent(parent) {}
109 
recognitionCallback(const ISoundTriggerHwCallback::RecognitionEvent & event __unused,int32_t cookie __unused)110     virtual Return<void> recognitionCallback(
111         const ISoundTriggerHwCallback::RecognitionEvent& event __unused,
112         int32_t cookie __unused) {
113       ALOGI("%s", __FUNCTION__);
114       return Void();
115     }
116 
phraseRecognitionCallback(const ISoundTriggerHwCallback::PhraseRecognitionEvent & event __unused,int32_t cookie __unused)117     virtual Return<void> phraseRecognitionCallback(
118         const ISoundTriggerHwCallback::PhraseRecognitionEvent& event __unused,
119         int32_t cookie __unused) {
120       ALOGI("%s", __FUNCTION__);
121       return Void();
122     }
123 
soundModelCallback(const ISoundTriggerHwCallback::ModelEvent & event,int32_t cookie __unused)124     virtual Return<void> soundModelCallback(
125         const ISoundTriggerHwCallback::ModelEvent& event,
126         int32_t cookie __unused) {
127       ALOGI("%s", __FUNCTION__);
128       mParent.lastModelEvent = event;
129       mParent.monitor.notify();
130       return Void();
131     }
132   };
133 
TearDown()134   virtual void TearDown() override {}
135 
136   Monitor monitor;
137   // updated by soundModelCallback()
138   ISoundTriggerHwCallback::ModelEvent lastModelEvent;
139 
140  protected:
141   sp<ISoundTriggerHw> mSoundTriggerHal;
142   sp<SoundTriggerHwCallback> mCallback;
143 };
144 
145 /**
146  * Test ISoundTriggerHw::getProperties() method
147  *
148  * Verifies that:
149  *  - the implementation implements the method
150  *  - the method returns 0 (no error)
151  *  - the implementation supports at least one sound model and one key phrase
152  *  - the implementation supports at least VOICE_TRIGGER recognition mode
153  */
TEST_P(SoundTriggerHidlTest,GetProperties)154 TEST_P(SoundTriggerHidlTest, GetProperties) {
155   ISoundTriggerHw::Properties halProperties;
156   Return<void> hidlReturn;
157   int ret = -ENODEV;
158 
159   hidlReturn = mSoundTriggerHal->getProperties([&](int rc, auto res) {
160       ret = rc;
161       halProperties = res;
162   });
163 
164   EXPECT_TRUE(hidlReturn.isOk());
165   EXPECT_EQ(0, ret);
166   EXPECT_GT(halProperties.maxSoundModels, 0u);
167   EXPECT_GT(halProperties.maxKeyPhrases, 0u);
168   EXPECT_NE(0u, (halProperties.recognitionModes & (uint32_t)RecognitionMode::VOICE_TRIGGER));
169 }
170 
171 /**
172  * Test ISoundTriggerHw::loadPhraseSoundModel() method
173  *
174  * Verifies that:
175  *  - the implementation implements the method
176  *  - the implementation returns an error when passed a malformed sound model
177  *
178  * There is no way to verify that implementation actually can load a sound model because each
179  * sound model is vendor specific.
180  */
TEST_P(SoundTriggerHidlTest,LoadInvalidModelFail)181 TEST_P(SoundTriggerHidlTest, LoadInvalidModelFail) {
182   Return<void> hidlReturn;
183   int ret = -ENODEV;
184   ISoundTriggerHw::PhraseSoundModel model;
185   SoundModelHandle handle;
186 
187   model.common.type = SoundModelType::UNKNOWN;
188 
189   hidlReturn = mSoundTriggerHal->loadPhraseSoundModel(
190           model,
191           mCallback, 0, [&](int32_t retval, auto res) {
192       ret = retval;
193       handle = res;
194   });
195 
196   EXPECT_TRUE(hidlReturn.isOk());
197   EXPECT_NE(0, ret);
198   EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
199 }
200 
201 /**
202  * Test ISoundTriggerHw::loadSoundModel() method
203  *
204  * Verifies that:
205  *  - the implementation returns error when passed a sound model with random data.
206  */
TEST_P(SoundTriggerHidlTest,LoadGenericSoundModelFail)207 TEST_P(SoundTriggerHidlTest, LoadGenericSoundModelFail) {
208   int ret = -ENODEV;
209   ISoundTriggerHw::SoundModel model;
210   SoundModelHandle handle = 0;
211 
212   model.type = SoundModelType::GENERIC;
213   model.data.resize(100);
214   for (auto& d : model.data) {
215     d = rand();
216   }
217 
218   Return<void> loadReturn = mSoundTriggerHal->loadSoundModel(
219       model,
220       mCallback, 0, [&](int32_t retval, auto res) {
221     ret = retval;
222     handle = res;
223   });
224 
225   EXPECT_TRUE(loadReturn.isOk());
226   EXPECT_NE(0, ret);
227   EXPECT_FALSE(monitor.wait(SHORT_TIMEOUT_PERIOD));
228 }
229 
230 /**
231  * Test ISoundTriggerHw::unloadSoundModel() method
232  *
233  * Verifies that:
234  *  - the implementation implements the method
235  *  - the implementation returns an error when called without a valid loaded sound model
236  *
237  */
TEST_P(SoundTriggerHidlTest,UnloadModelNoModelFail)238 TEST_P(SoundTriggerHidlTest, UnloadModelNoModelFail) {
239   Return<int32_t> hidlReturn(0);
240   SoundModelHandle halHandle = 0;
241 
242   hidlReturn = mSoundTriggerHal->unloadSoundModel(halHandle);
243 
244   EXPECT_TRUE(hidlReturn.isOk());
245   EXPECT_NE(0, hidlReturn);
246 }
247 
248 /**
249  * Test ISoundTriggerHw::startRecognition() method
250  *
251  * Verifies that:
252  *  - the implementation implements the method
253  *  - the implementation returns an error when called without a valid loaded sound model
254  *
255  * There is no way to verify that implementation actually starts recognition because no model can
256  * be loaded.
257  */
TEST_P(SoundTriggerHidlTest,StartRecognitionNoModelFail)258 TEST_P(SoundTriggerHidlTest, StartRecognitionNoModelFail) {
259     Return<int32_t> hidlReturn(0);
260     SoundModelHandle handle = 0;
261     PhraseRecognitionExtra phrase;
262     ISoundTriggerHw::RecognitionConfig config;
263 
264     config.captureHandle = 0;
265     config.captureDevice = AudioDevice::IN_BUILTIN_MIC;
266     phrase.id = 0;
267     phrase.recognitionModes = (uint32_t)RecognitionMode::VOICE_TRIGGER;
268     phrase.confidenceLevel = 0;
269 
270     config.phrases.setToExternal(&phrase, 1);
271 
272     hidlReturn = mSoundTriggerHal->startRecognition(handle, config, mCallback, 0);
273 
274     EXPECT_TRUE(hidlReturn.isOk());
275     EXPECT_NE(0, hidlReturn);
276 }
277 
278 /**
279  * Test ISoundTriggerHw::stopRecognition() method
280  *
281  * Verifies that:
282  *  - the implementation implements the method
283  *  - the implementation returns an error when called without an active recognition running
284  *
285  */
TEST_P(SoundTriggerHidlTest,StopRecognitionNoAStartFail)286 TEST_P(SoundTriggerHidlTest, StopRecognitionNoAStartFail) {
287     Return<int32_t> hidlReturn(0);
288     SoundModelHandle handle = 0;
289 
290     hidlReturn = mSoundTriggerHal->stopRecognition(handle);
291 
292     EXPECT_TRUE(hidlReturn.isOk());
293     EXPECT_NE(0, hidlReturn);
294 }
295 
296 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SoundTriggerHidlTest);
297 INSTANTIATE_TEST_SUITE_P(
298         PerInstance, SoundTriggerHidlTest,
299         testing::ValuesIn(android::hardware::getAllHalInstanceNames(ISoundTriggerHw::descriptor)),
300         android::hardware::PrintInstanceNameToString);
301