• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cmath>
17 #include <cstdio>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include "hdf_base.h"
22 #include "hdf_log.h"
23 #include "osal_time.h"
24 #include "sensor_if.h"
25 #include "sensor_type.h"
26 
27 using namespace testing::ext;
28 
29 namespace {
30     struct SensorValueRange {
31         float highThreshold;
32         float lowThreshold;
33     };
34 
35     struct SensorDevelopmentList {
36         int32_t sensorTypeId;
37         char sensorName[SENSOR_NAME_MAX_LEN];
38         int32_t dataForm;    // 0: fixed, 1: range
39         int32_t dataDimension;
40         struct SensorValueRange *valueRange;
41     };
42 
43     struct SensorValueRange g_testRange[] = {{1e5, 0.0}};
44     struct SensorValueRange g_accelRange[] = {{78.0, -78.0}, {78.0, -78.0}, {78.0, -78.0}};
45     struct SensorValueRange g_alsRange[] = {{10000.0, 0.0}};
46     struct SensorValueRange g_pedometerRange[] = {{10000.0, 0.0}};
47     struct SensorValueRange g_proximityRange[] = {{5.0, 0.0}};
48     struct SensorValueRange g_hallRange[] = {{1.0, 0.0}};
49     struct SensorValueRange g_barometerRange[] = {{1100.0, -1100.0}, {1100.0, -1100.0}};
50     struct SensorValueRange g_magneticRange[] = {{2000.0, -2000.0}, {2000.0, -2000.0}, {2000.0, -2000.0}};
51     struct SensorValueRange g_gyroscopeRange[] = {{35.0, -35.0}, {35.0, -35.0}, {35.0, -35.0}};
52     struct SensorValueRange g_gravityRange[] = {{78.0, -78.0}, {78.0, -78.0}, {78.0, -78.0}};
53 
54     struct SensorDevelopmentList g_sensorList[] = {
55         {SENSOR_TYPE_NONE, "sensor_test", 1, 1, g_testRange},
56         {SENSOR_TYPE_ACCELEROMETER, "accelerometer", 1, 3, g_accelRange},
57         {SENSOR_TYPE_PEDOMETER, "pedometer", 1, 1, g_pedometerRange},
58         {SENSOR_TYPE_PROXIMITY, "proximity", 0, 1, g_proximityRange},
59         {SENSOR_TYPE_HALL, "hallrometer", 0, 1, g_hallRange},
60         {SENSOR_TYPE_BAROMETER, "barometer", 1, 2, g_barometerRange},
61         {SENSOR_TYPE_AMBIENT_LIGHT, "als", 1, 1, g_alsRange},
62         {SENSOR_TYPE_MAGNETIC_FIELD, "magnetometer", 1, 3, g_magneticRange},
63         {SENSOR_TYPE_GYROSCOPE, "gyroscope", 1, 3, g_gyroscopeRange},
64         {SENSOR_TYPE_GRAVITY, "gravity", 1, 3, g_gravityRange}
65     };
66 
67     constexpr int g_listNum = sizeof(g_sensorList) / sizeof(g_sensorList[0]);
68     uint32_t g_sensorDataFlag = 1;
69     constexpr int32_t SENSOR_INTERVAL1 = 200000000;
70     constexpr int32_t SENSOR_INTERVAL2 = 20000000;
71     constexpr int32_t SENSOR_POLL_TIME = 1;
72     constexpr int32_t SENSOR_WAIT_TIME = 100;
73     constexpr float EPSINON = 1e-6;
74     constexpr int32_t ABNORMAL_SENSORID = -1;
75     const struct SensorInterface *g_sensorDev = nullptr;
76     int32_t g_count = 0;
77     struct SensorInformation *g_sensorInfo = nullptr;
78 
SensorDataVerification(const float & data,const struct SensorDevelopmentList & sensorNode)79     void SensorDataVerification(const float &data, const struct SensorDevelopmentList &sensorNode)
80     {
81         for (int32_t j = 0; j < sensorNode.dataDimension; ++j) {
82             printf("sensor id :[%d], data[%d]: %f\n\r", sensorNode.sensorTypeId, j + 1, *(&data + j));
83             if (sensorNode.dataForm == 0) {
84                 if (abs(*(&data + j) - sensorNode.valueRange[j].highThreshold) < EPSINON ||
85                     abs(*(&data + j) - sensorNode.valueRange[j].lowThreshold) < EPSINON) {
86                     g_sensorDataFlag &= 1;
87                 } else {
88                     g_sensorDataFlag = 0;
89                     printf("%s: %s Not expected\n\r", __func__, sensorNode.sensorName);
90                 }
91             }
92 
93             if (sensorNode.dataForm == 1) {
94                 if (*(&data + j) > sensorNode.valueRange[j].lowThreshold &&
95                     *(&data + j) < sensorNode.valueRange[j].highThreshold) {
96                     g_sensorDataFlag &= 1;
97                 } else {
98                     g_sensorDataFlag = 0;
99                     printf("%s: %s Not expected\n\r", __func__, sensorNode.sensorName);
100                 }
101             }
102         }
103     }
104 
TraditionalSensorTestDataCallback(const struct SensorEvents * event)105     int32_t TraditionalSensorTestDataCallback(const struct SensorEvents *event)
106     {
107         if (event == nullptr || event->data == nullptr) {
108             return SENSOR_FAILURE;
109         }
110 
111         for (int32_t i = 0; i < g_listNum; ++i) {
112             if (event->sensorId == g_sensorList[i].sensorTypeId) {
113                 float *data = reinterpret_cast<float*>(event->data);
114                 SensorDataVerification(*data, g_sensorList[i]);
115             }
116         }
117         return SENSOR_SUCCESS;
118     }
119 
MedicalSensorTestDataCallback(const struct SensorEvents * event)120     int32_t MedicalSensorTestDataCallback(const struct SensorEvents *event)
121     {
122         return SENSOR_SUCCESS;
123     }
124 }
125 
126 class HdfSensorTest : public testing::Test {
127 public:
128     static void SetUpTestCase();
129     static void TearDownTestCase();
130     void SetUp();
131     void TearDown();
132 };
133 
SetUpTestCase()134 void HdfSensorTest::SetUpTestCase()
135 {
136     g_sensorDev = NewSensorInterfaceInstance();
137     if (g_sensorDev == nullptr) {
138         printf("test sensorHdi get Module instance failed\n\r");
139         return;
140     }
141     int32_t ret = g_sensorDev->GetAllSensors(&g_sensorInfo, &g_count);
142     if (ret == -1) {
143         printf("get sensor information failed\n\r");
144     }
145 }
146 
TearDownTestCase()147 void HdfSensorTest::TearDownTestCase()
148 {
149     if (g_sensorDev != nullptr) {
150         FreeSensorInterfaceInstance();
151         g_sensorDev = nullptr;
152     }
153 }
154 
SetUp()155 void HdfSensorTest::SetUp()
156 {
157 }
158 
TearDown()159 void HdfSensorTest::TearDown()
160 {
161 }
162 
163 /**
164   * @tc.name: GetSensorInstance001
165   * @tc.desc: Create a sensor instance and check whether the instance is empty.
166   * @tc.type: FUNC
167   */
168 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0100, TestSize.Level1)
169 {
170     if (g_sensorDev == nullptr) {
171         printf("test sensorHdi get Module instance failed\n\r");
172         return;
173     }
174 
175     const struct SensorInterface *sensorDev = NewSensorInterfaceInstance();
176     EXPECT_EQ(sensorDev, g_sensorDev);
177 }
178 
179 /**
180   * @tc.name: RemoveSensorInstance001
181   * @tc.desc: The sensor instance is successfully removed.
182   * @tc.type: FUNC
183   */
184 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0200, TestSize.Level1)
185 {
186     int32_t ret = FreeSensorInterfaceInstance();
187     ASSERT_EQ(SENSOR_SUCCESS, ret);
188     ret = FreeSensorInterfaceInstance();
189     EXPECT_EQ(SENSOR_SUCCESS, ret);
190     g_sensorDev = NewSensorInterfaceInstance();
191     if (g_sensorDev == nullptr) {
192         ASSERT_EQ(SENSOR_SUCCESS, ret);
193         return;
194     }
195     ret = g_sensorDev->GetAllSensors(&g_sensorInfo, &g_count);
196     EXPECT_EQ(SENSOR_SUCCESS, ret);
197 }
198 
199 /**
200   * @tc.name: RegisterSensorDataCb001
201   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
202   * @tc.type: FUNC
203   */
204 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0300, TestSize.Level1)
205 {
206     if (g_sensorDev == nullptr) {
207         EXPECT_NE(nullptr, g_sensorDev);
208         return;
209     }
210     int32_t ret = g_sensorDev->Register(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
211     EXPECT_EQ(SENSOR_SUCCESS, ret);
212     ret = g_sensorDev->Unregister(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
213     EXPECT_EQ(SENSOR_SUCCESS, ret);
214 }
215 
216 /**
217   * @tc.name: RegisterSensorDataCb002
218   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
219   * @tc.type: FUNC
220   */
221 HWTEST_F(HdfSensorTest, SSUB_Driver_Sensor_SmallHdiSensor_0400, TestSize.Level1)
222 {
223     if (g_sensorDev == nullptr) {
224         EXPECT_NE(nullptr, g_sensorDev);
225         return;
226     }
227     int32_t ret = g_sensorDev->Register(TRADITIONAL_SENSOR_TYPE, nullptr);
228     EXPECT_EQ(SENSOR_NULL_PTR, ret);
229     ret = g_sensorDev->Unregister(0, nullptr);
230     EXPECT_EQ(SENSOR_NULL_PTR, ret);
231 }
232 
233 /**
234   * @tc.name: RegisterSensorDataCb003
235   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
236   * @tc.type: FUNC
237   */
238 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0500, TestSize.Level1)
239 {
240     if (g_sensorDev == nullptr) {
241         EXPECT_NE(nullptr, g_sensorDev);
242         return;
243     }
244     int32_t ret = g_sensorDev->Register(MEDICAL_SENSOR_TYPE, MedicalSensorTestDataCallback);
245     EXPECT_EQ(SENSOR_SUCCESS, ret);
246     ret = g_sensorDev->Unregister(MEDICAL_SENSOR_TYPE, MedicalSensorTestDataCallback);
247     EXPECT_EQ(SENSOR_SUCCESS, ret);
248 }
249 
250 
251 /**
252   * @tc.name: RegisterSensorDataCb004
253   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
254   * @tc.type: FUNC
255   */
256 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0600, TestSize.Level1)
257 {
258     if (g_sensorDev == nullptr) {
259         EXPECT_NE(nullptr, g_sensorDev);
260         return;
261     }
262     int32_t ret = g_sensorDev->Register(MEDICAL_SENSOR_TYPE, nullptr);
263     EXPECT_EQ(SENSOR_NULL_PTR, ret);
264     ret = g_sensorDev->Unregister(MEDICAL_SENSOR_TYPE, nullptr);
265     EXPECT_EQ(SENSOR_NULL_PTR, ret);
266 }
267 
268 /**
269   * @tc.name: RegisterSensorDataCb005
270   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
271   * @tc.type: FUNC
272   */
273 HWTEST_F(HdfSensorTest, SSUB_Driver_Sensor_SmallHdiSensor_0700, TestSize.Level1)
274 {
275     if (g_sensorDev == nullptr) {
276         EXPECT_NE(nullptr, g_sensorDev);
277         return;
278     }
279     int32_t ret = g_sensorDev->Register(SENSOR_TYPE_MAX, nullptr);
280     EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
281     ret = g_sensorDev->Unregister(SENSOR_TYPE_MAX, nullptr);
282     EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
283 }
284 
285 
286 /**
287   * @tc.name: GetSensorList001
288   * @tc.desc: Obtains information about all sensors in the system. Validity check of input parameters.
289   * @tc.type: FUNC
290   */
291 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0800, TestSize.Level1)
292 {
293     if (g_sensorInfo == nullptr) {
294         EXPECT_NE(nullptr, g_sensorInfo);
295         return;
296     }
297     EXPECT_GT(g_count, 0);
298 }
299 
300 /**
301   * @tc.name: GetSensorList002
302   * @tc.desc: Obtains information about all sensors in the system. Validity check of input parameters.
303   * @tc.type: FUNC
304   */
305 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_0900, TestSize.Level1)
306 {
307     struct SensorInformation *info = nullptr;
308     int j;
309 
310     if (g_sensorInfo == nullptr) {
311         EXPECT_NE(nullptr, g_sensorInfo);
312         return;
313     }
314     EXPECT_GT(g_count, 0);
315 
316     printf("get sensor list num[%d]\n\r", g_count);
317     info = g_sensorInfo;
318 
319     for (int32_t i = 0; i < g_count; ++i) {
320         printf("get sensoriId[%d], info name[%s], power[%f]\n\r", info->sensorId, info->sensorName, info->power);
321         for (j = 0; j < g_listNum; ++j) {
322             if (info->sensorId == g_sensorList[j].sensorTypeId) {
323                 EXPECT_STRNE("", info->sensorName);
324                 break;
325             }
326         }
327 
328         info++;
329     }
330 }
331 
332 /**
333   * @tc.name: GetSensorList003
334   * @tc.desc: Obtains information about all sensors in the system. The operations include obtaining sensor information,
335   * subscribing to or unsubscribing from sensor data, enabling or disabling a sensor,
336   * setting the sensor data reporting mode, and setting sensor options such as the accuracy and measurement range.
337   * @tc.type: FUNC
338   */
339 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1000, TestSize.Level1)
340 {
341     if (g_sensorDev == nullptr) {
342         EXPECT_NE(nullptr, g_sensorDev);
343         return;
344     }
345     int32_t ret = g_sensorDev->GetAllSensors(nullptr, &g_count);
346     EXPECT_EQ(SENSOR_NULL_PTR, ret);
347     ret = g_sensorDev->GetAllSensors(&g_sensorInfo, nullptr);
348     EXPECT_EQ(SENSOR_NULL_PTR, ret);
349     ret = g_sensorDev->GetAllSensors(nullptr, nullptr);
350     EXPECT_EQ(SENSOR_NULL_PTR, ret);
351 }
352 
353 /**
354   * @tc.name: EnableSensor001
355   * @tc.desc: Enables the sensor unavailable in the sensor list based on the specified sensor ID.
356   * @tc.type: FUNC
357   */
358 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1100, TestSize.Level1)
359 {
360     if (g_sensorDev == nullptr) {
361         EXPECT_NE(nullptr, g_sensorDev);
362         return;
363     }
364     int32_t ret = g_sensorDev->Register(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
365     EXPECT_EQ(SENSOR_SUCCESS, ret);
366 
367     struct SensorInformation *info = nullptr;
368 
369     if (g_sensorInfo == nullptr) {
370         EXPECT_NE(nullptr, g_sensorInfo);
371         return;
372     }
373 
374     info = g_sensorInfo;
375     for (int32_t i = 0; i < g_count; i++) {
376         ret = g_sensorDev->SetBatch(info->sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
377         EXPECT_EQ(SENSOR_SUCCESS, ret);
378         ret = g_sensorDev->Enable(info->sensorId);
379         EXPECT_EQ(SENSOR_SUCCESS, ret);
380         OsalSleep(SENSOR_POLL_TIME);
381         ret = g_sensorDev->Disable(info->sensorId);
382         EXPECT_EQ(SENSOR_SUCCESS, ret);
383         info++;
384     }
385 
386     ret = g_sensorDev->Unregister(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
387     EXPECT_EQ(SENSOR_SUCCESS, ret);
388 
389     EXPECT_EQ(g_sensorDataFlag, 1);
390     g_sensorDataFlag = 1;
391 }
392 
393 /**
394   * @tc.name: EnableSensor002
395   * @tc.desc: Enables the sensor available in the sensor list based on the specified sensor ID.
396   * @tc.type: FUNC
397   */
398 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1200, TestSize.Level1)
399 {
400     if (g_sensorDev == nullptr) {
401         EXPECT_NE(nullptr, g_sensorDev);
402         return;
403     }
404 
405     if (g_sensorInfo == nullptr) {
406         EXPECT_NE(nullptr, g_sensorInfo);
407         return;
408     }
409 
410     int32_t ret = g_sensorDev->Enable(ABNORMAL_SENSORID);
411     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
412     ret = g_sensorDev->Disable(ABNORMAL_SENSORID);
413     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
414 }
415 
416 /**
417   * @tc.name: SetSensorBatch001
418   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
419   * @tc.type: FUNC
420   */
421 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1300, TestSize.Level1)
422 {
423     if (g_sensorDev == nullptr) {
424         EXPECT_NE(nullptr, g_sensorDev);
425         return;
426     }
427 
428     struct SensorInformation *info = nullptr;
429 
430     int32_t ret = g_sensorDev->Register(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
431     EXPECT_EQ(SENSOR_SUCCESS, ret);
432 
433     if (g_sensorInfo == nullptr) {
434         EXPECT_NE(nullptr, g_sensorInfo);
435         return;
436     }
437 
438     info = g_sensorInfo;
439     for (int32_t i = 0; i < g_count; i++) {
440         ret = g_sensorDev->SetBatch(info->sensorId, SENSOR_INTERVAL2, SENSOR_POLL_TIME);
441         EXPECT_EQ(SENSOR_SUCCESS, ret);
442         ret = g_sensorDev->Enable(info->sensorId);
443         EXPECT_EQ(SENSOR_SUCCESS, ret);
444         OsalMSleep(SENSOR_WAIT_TIME);
445         ret = g_sensorDev->Disable(info->sensorId);
446         EXPECT_EQ(SENSOR_SUCCESS, ret);
447         info++;
448     }
449 
450     ret = g_sensorDev->Unregister(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
451     EXPECT_EQ(SENSOR_SUCCESS, ret);
452     EXPECT_EQ(g_sensorDataFlag, 1);
453     g_sensorDataFlag = 1;
454 }
455 
456 /** @tc.name: SetSensorBatch002
457     @tc.desc: Sets the sampling time and data report interval for sensors in batches.
458     @tc.type: FUNC
459     */
460 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1400, TestSize.Level1)
461 {
462     if (g_sensorDev == nullptr) {
463         EXPECT_NE(nullptr, g_sensorDev);
464         return;
465     }
466     int32_t ret = g_sensorDev->SetBatch(ABNORMAL_SENSORID, 0, 0);
467     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
468 }
469 
470 /** @tc.name: SetSensorBatch003
471     @tc.desc: Sets the sampling time and data report interval for sensors in batches.
472     @tc.type: FUNC
473     */
474 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1500, TestSize.Level1)
475 {
476     if (g_sensorDev == nullptr) {
477         EXPECT_NE(nullptr, g_sensorDev);
478         return;
479     }
480 
481     struct SensorInformation *info = nullptr;
482 
483     if (g_sensorInfo == nullptr) {
484         EXPECT_NE(nullptr, g_sensorInfo);
485         return;
486     }
487 
488     info = g_sensorInfo;
489     for (int32_t i = 0; i < g_count; i++) {
490         int32_t ret = g_sensorDev->SetBatch(info->sensorId, -1, SENSOR_POLL_TIME);
491         EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
492         info++;
493     }
494 }
495 /**
496   * @tc.name: SetSensorMode001
497   * @tc.desc: Sets the data reporting mode for the specified sensor.
498   * @tc.type: FUNC
499   */
500 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1600, TestSize.Level1)
501 {
502     if (g_sensorDev == nullptr) {
503         EXPECT_NE(nullptr, g_sensorDev);
504         return;
505     }
506 
507     struct SensorInformation *info = nullptr;
508 
509     int32_t ret = g_sensorDev->Register(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
510     EXPECT_EQ(SENSOR_SUCCESS, ret);
511 
512     if (g_sensorInfo == nullptr) {
513         EXPECT_NE(nullptr, g_sensorInfo);
514         return;
515     }
516 
517     info = g_sensorInfo;
518     for (int32_t i = 0; i < g_count; i++) {
519         ret = g_sensorDev->SetBatch(info->sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
520         EXPECT_EQ(SENSOR_SUCCESS, ret);
521         if (info->sensorId == SENSOR_TYPE_HALL) {
522             ret = g_sensorDev->SetMode(info->sensorId, SENSOR_MODE_ON_CHANGE);
523             EXPECT_EQ(SENSOR_SUCCESS, ret);
524         } else {
525             ret = g_sensorDev->SetMode(info->sensorId, SENSOR_MODE_REALTIME);
526             EXPECT_EQ(SENSOR_SUCCESS, ret);
527         }
528 
529         ret = g_sensorDev->Enable(info->sensorId);
530         EXPECT_EQ(SENSOR_SUCCESS, ret);
531         OsalMSleep(SENSOR_WAIT_TIME);
532         ret = g_sensorDev->Disable(info->sensorId);
533         EXPECT_EQ(SENSOR_SUCCESS, ret);
534         info++;
535     }
536 
537     ret = g_sensorDev->Unregister(TRADITIONAL_SENSOR_TYPE, TraditionalSensorTestDataCallback);
538     EXPECT_EQ(SENSOR_SUCCESS, ret);
539     EXPECT_EQ(1, g_sensorDataFlag);
540     g_sensorDataFlag = 1;
541 }
542 
543 /**
544   * @tc.name: SetSensorMode002
545   * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid.
546   * Other values are invalid.
547   * @tc.type: FUNC
548   */
549 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1700, TestSize.Level1)
550 {
551     if (g_sensorDev == nullptr) {
552         EXPECT_NE(nullptr, g_sensorDev);
553         return;
554     }
555 
556     int32_t ret = g_sensorDev->SetBatch(ABNORMAL_SENSORID, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
557     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
558 }
559 
560 /**
561   * @tc.name: SetSensorMode002
562   * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid.
563   * Other values are invalid.
564   * @tc.type: FUNC
565   */
566 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1800, TestSize.Level1)
567 {
568     if (g_sensorDev == nullptr) {
569         EXPECT_NE(nullptr, g_sensorDev);
570         return;
571     }
572 
573     struct SensorInformation *info = nullptr;
574 
575     if (g_sensorInfo == nullptr) {
576         EXPECT_NE(nullptr, g_sensorInfo);
577         return;
578     }
579 
580     info = g_sensorInfo;
581     for (int32_t i = 0; i < g_count; i++) {
582         int32_t ret = g_sensorDev->SetBatch(info->sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
583         EXPECT_EQ(SENSOR_SUCCESS, ret);
584         ret = g_sensorDev->SetMode(info->sensorId, SENSOR_MODE_DEFAULT);
585         EXPECT_EQ(SENSOR_FAILURE, ret);
586         ret = g_sensorDev->Enable(info->sensorId);
587         EXPECT_EQ(SENSOR_SUCCESS, ret);
588         OsalMSleep(SENSOR_WAIT_TIME);
589         ret = g_sensorDev->Disable(info->sensorId);
590         EXPECT_EQ(SENSOR_SUCCESS, ret);
591         info++;
592     }
593 }
594 
595 /**
596   * @tc.name: SetSensorOption001
597   * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy.
598   * @tc.type: FUNC
599   */
600 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_1900, TestSize.Level1)
601 {
602     if (g_sensorDev == nullptr) {
603         EXPECT_NE(nullptr, g_sensorDev);
604         return;
605     }
606 
607     struct SensorInformation *info = nullptr;
608 
609     if (g_sensorInfo == nullptr) {
610         EXPECT_NE(nullptr, g_sensorInfo);
611         return;
612     }
613 
614     info = g_sensorInfo;
615     for (int32_t i = 0; i < g_count; i++) {
616         int32_t ret = g_sensorDev->SetOption(info->sensorId, 0);
617         EXPECT_EQ(SENSOR_SUCCESS, ret);
618         info++;
619     }
620 }
621 
622 /**
623   * @tc.name: SetSensorOption001
624   * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy.
625   * @tc.type: FUNC
626   */
627 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_2000, TestSize.Level1)
628 {
629     if (g_sensorDev == nullptr) {
630         EXPECT_NE(nullptr, g_sensorDev);
631         return;
632     }
633 
634     int32_t ret = g_sensorDev->SetOption(ABNORMAL_SENSORID, 0);
635     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
636 }
637 
638 /**
639   * @tc.name: ReadSensorData001
640   * @tc.desc: Read event data for the specified sensor.
641   * @tc.type: FUNC
642   */
643 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_2100, TestSize.Level1)
644 {
645     ASSERT_NE(nullptr, g_sensorDev);
646 
647     g_sensorEvents = (struct SensorEvents*)OsalMemCalloc(sizeof(*g_sensorEvents));
648     ASSERT_NE(nullptr, g_sensorEvents);
649     g_sensorEvents->data = (uint8_t *)OsalMemCalloc(SENSOR_DATA_LEN);
650     ASSERT_NE(nullptr, g_sensorEvents->data);
651     g_sensorEvents->dataLen = SENSOR_DATA_LEN;
652 
653     int32_t ret = g_sensorDev->Enable(SENSOR_TYPE_AMBIENT_LIGHT);
654     EXPECT_EQ(SENSOR_SUCCESS, ret);
655     ret = g_sensorDev->ReadData(SENSOR_TYPE_AMBIENT_LIGHT, g_sensorEvents);
656     EXPECT_EQ(SENSOR_SUCCESS, ret);
657 
658     printf("sensorId[%d], mode[%d], option[%u]\n\r",
659         g_sensorEvents->sensorId, g_sensorEvents->mode, g_sensorEvents->option);
660     for (int32_t i = 0; i < g_listNum; i++) {
661         if (g_sensorEvents->sensorId == g_sensorList[i].sensorTypeId) {
662             float *data = reinterpret_cast<float*>(g_sensorEvents->data);
663             SensorDataVerification(*data, g_sensorList[i]);
664         }
665     }
666 
667     ret = g_sensorDev->Disable(SENSOR_TYPE_AMBIENT_LIGHT);
668     EXPECT_EQ(SENSOR_SUCCESS, ret);
669 
670     OsalMemFree(g_sensorEvents->data);
671     OsalMemFree(g_sensorEvents);
672 }
673 
674 /**
675   * @tc.name: ReadSensorData002
676   * @tc.desc: Read event data for the specified sensor.
677   * @tc.type: FUNC
678   */
679 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_2200, TestSize.Level1)
680 {
681     ASSERT_NE(nullptr, g_sensorDev);
682 
683     int32_t ret = g_sensorDev->ReadData(SENSOR_TYPE_AMBIENT_LIGHT, nullptr);
684     EXPECT_EQ(SENSOR_NULL_PTR, ret);
685 }
686 
687 /**
688   * @tc.name: ReadSensorData003
689   * @tc.desc: Read event data for the specified sensor.
690   * @tc.type: FUNC
691   */
692 HWTEST_F(HdfSensorTest, SUB_Driver_Sensor_SmallHdiSensor_2300, TestSize.Level1)
693 {
694     ASSERT_NE(nullptr, g_sensorDev);
695 
696     g_sensorEvents = (struct SensorEvents*)OsalMemCalloc(sizeof(*g_sensorEvents));
697     ASSERT_NE(nullptr, g_sensorEvents);
698     g_sensorEvents->data = (uint8_t *)OsalMemCalloc(SENSOR_DATA_LEN);
699     ASSERT_NE(nullptr, g_sensorEvents->data);
700     g_sensorEvents->dataLen = SENSOR_DATA_LEN;
701 
702     int32_t ret = g_sensorDev->ReadData(ABNORMAL_SENSORID, g_sensorEvents);
703     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
704 
705     OsalMemFree(g_sensorEvents->data);
706     OsalMemFree(g_sensorEvents);
707 }
708 
709