1 /*
2  * Copyright (c) 2021-2023 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 "osal_time.h"
23 #include "v2_0/isensor_interface.h"
24 #include "sensor_type.h"
25 #include "sensor_callback_impl.h"
26 #include "sensor_callback_impl_test.h"
27 #include "sensor_uhdf_log.h"
28 #include "sensor_trace.h"
29 
30 using namespace OHOS::HDI::Sensor::V2_0;
31 using namespace testing::ext;
32 
33 namespace {
34     sptr<ISensorInterface>  g_sensorInterface = nullptr;
35     sptr<ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
36     sptr<ISensorCallback> g_traditionalCallbackTest = new SensorCallbackImplTest();
37     sptr<ISensorCallback> g_medicalCallback = new SensorCallbackImpl();
38     std::vector<HdfSensorInformation> g_info;
39     std::vector<HdfSensorEvents> g_events;
40     struct SensorValueRange {
41         float highThreshold;
42         float lowThreshold;
43     };
44 
45     struct SensorDevelopmentList {
46         int32_t sensorTypeId;
47         char sensorName[SENSOR_NAME_MAX_LEN];
48         int32_t dataForm;    // 0: fixed, 1: range
49         int32_t dataDimension;
50         struct SensorValueRange *valueRange;
51     };
52 
53     struct SensorValueRange g_testRange[] = {{1e5, 0}};
54     struct SensorValueRange g_accelRange[] = {{78, -78}, {78, -78}, {78, -78}};
55     struct SensorValueRange g_alsRange[] = {{10000000, 0}};
56     struct SensorValueRange g_pedometerRange[] = {{10000, 0}};
57     struct SensorValueRange g_proximityRange[] = {{5, 0}};
58     struct SensorValueRange g_hallRange[] = {{2, 0}};
59     struct SensorValueRange g_barometerRange[] = {{1100, -1100}, {1100, -1100}};
60     struct SensorValueRange g_magneticRange[] = {{2000, -2000}, {2000, -2000}, {2000, -2000}};
61     struct SensorValueRange g_gyroscopeRange[] = {{35, -35}, {35, -35}, {35, -35}};
62     struct SensorValueRange g_gravityRange[] = {{78, -78}, {78, -78}, {78, -78}};
63     struct SensorValueRange g_humidityRange[] = {{100, 0}};
64     struct SensorValueRange g_temperatureRange[] = {{125, -40}};
65 
66     struct SensorDevelopmentList g_sensorList[] = {
67         {SENSOR_TYPE_NONE, "sensor_test",  1, 1, g_testRange},
68         {SENSOR_TYPE_ACCELEROMETER, "accelerometer",  1, 3, g_accelRange},
69         {SENSOR_TYPE_PEDOMETER, "pedometer", 1, 1, g_pedometerRange},
70         {SENSOR_TYPE_PROXIMITY, "proximity",  0, 1, g_proximityRange},
71         {SENSOR_TYPE_HALL, "hallrometer",  1, 1, g_hallRange},
72         {SENSOR_TYPE_BAROMETER, "barometer",  1, 2, g_barometerRange},
73         {SENSOR_TYPE_AMBIENT_LIGHT, "als", 1, 1, g_alsRange},
74         {SENSOR_TYPE_MAGNETIC_FIELD, "magnetometer",  1, 3, g_magneticRange},
75         {SENSOR_TYPE_GYROSCOPE, "gyroscope", 1, 3, g_gyroscopeRange},
76         {SENSOR_TYPE_GRAVITY, "gravity", 1, 3, g_gravityRange},
77         {SENSOR_TYPE_HUMIDITY, "humidity", 1, 1, g_humidityRange},
78         {SENSOR_TYPE_TEMPERATURE, "tenperature", 1, 1, g_temperatureRange}
79     };
80 
81     constexpr int g_listNum = sizeof(g_sensorList) / sizeof(g_sensorList[0]);
82     constexpr int64_t SENSOR_INTERVAL1 = 200000000;
83     constexpr int64_t SENSOR_INTERVAL2 = 20000000;
84     constexpr int64_t SENSOR_INTERVAL3 = 40000000;
85     constexpr int64_t SENSOR_INTERVAL4 = 20000000;
86     constexpr int32_t SENSOR_POLL_TIME = 1;
87     constexpr int32_t SENSOR_WAIT_TIME = 100;
88     constexpr int32_t SENSOR_WAIT_TIME2 = 1000;
89     constexpr int32_t ABNORMAL_SENSORID = -1;
90     constexpr int32_t RATE_LEVEL = 50;
91 }
92 
93 class HdfSensorHdiTest : public testing::Test {
94 public:
95     static void SetUpTestCase();
96     static void TearDownTestCase();
97     void SetUp();
98     void TearDown();
99 };
100 
SetUpTestCase()101 void HdfSensorHdiTest::SetUpTestCase()
102 {
103     g_sensorInterface = ISensorInterface::Get();
104 }
105 
TearDownTestCase()106 void HdfSensorHdiTest::TearDownTestCase()
107 {
108 }
109 
SetUp()110 void HdfSensorHdiTest::SetUp()
111 {
112 }
113 
TearDown()114 void HdfSensorHdiTest::TearDown()
115 {
116 }
117 
118 /**
119   * @tc.name: GetSensorClient0001
120   * @tc.desc: Get a client and check whether the client is empty.
121   * @tc.type: FUNC
122   * @tc.require: #I4L3LF
123   */
124 HWTEST_F(HdfSensorHdiTest, GetSensorClient0001, TestSize.Level1)
125 {
126     ASSERT_NE(nullptr, g_sensorInterface);
127 }
128 
129 /**
130   * @tc.name: GetSensorList0001
131   * @tc.desc: Obtains information about all sensors in the system.
132   * @tc.type: FUNC
133   * @tc.require: #I4L3LF
134   */
135 HWTEST_F(HdfSensorHdiTest, GetSensorList0001, TestSize.Level1)
136 {
137     if (g_sensorInterface == nullptr) {
138         ASSERT_NE(nullptr, g_sensorInterface);
139         return;
140     }
141     int32_t ret = g_sensorInterface->GetAllSensorInfo(g_info);
142     EXPECT_EQ(SENSOR_SUCCESS, ret);
143     EXPECT_GT(g_info.size(), 0);
144     printf("get sensor list num[%zu]\n\r", g_info.size());
145 
146     for (auto iter : g_info) {
147         printf("get sensoriId[%d], info name[%s], power[%f]\n\r", iter.sensorId, iter.sensorName.c_str(), iter.power);
148         for (int j =0; j < g_listNum; ++j) {
149             if (iter.sensorId == g_sensorList[j].sensorTypeId) {
150                 EXPECT_GT(iter.sensorName.size(), 0);
151                 break;
152             }
153         }
154     }
155 }
156 
157 /**
158   * @tc.name: RegisterSensorDataCb0001
159   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
160   * @tc.type: FUNC
161   * @tc.require: SR000F869M, AR000F869P, AR000F8QNL
162   */
163 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0001, TestSize.Level1)
164 {
165     if (g_sensorInterface == nullptr) {
166         ASSERT_NE(nullptr, g_sensorInterface);
167         return;
168     }
169     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
170     EXPECT_EQ(SENSOR_SUCCESS, ret);
171     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
172     EXPECT_EQ(SENSOR_SUCCESS, ret);
173 }
174 
175 /**
176   * @tc.name: RegisterSensorDataCb0002
177   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
178   * @tc.type: FUNC
179   * @tc.require: SR000F869M, AR000F869P, AR000F8QNL
180   */
181 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0002, TestSize.Level1)
182 {
183     if (g_sensorInterface == nullptr) {
184         ASSERT_NE(nullptr, g_sensorInterface);
185         return;
186     }
187     int32_t ret = g_sensorInterface->Register(MEDICAL_SENSOR_TYPE, g_medicalCallback);
188     EXPECT_EQ(SENSOR_SUCCESS, ret);
189     ret = g_sensorInterface->Unregister(MEDICAL_SENSOR_TYPE, g_medicalCallback);
190     EXPECT_EQ(SENSOR_SUCCESS, ret);
191 }
192 
193 /**
194   * @tc.name: RegisterDataCb001
195   * @tc.desc: Returns 0 if the callback is successfully registered; returns a negative value otherwise.
196   * @tc.type: FUNC
197   * @tc.require: SR000F869M, AR000F869P, AR000F8QNL
198   */
199 HWTEST_F(HdfSensorHdiTest, RegisterSensorDataCb0003, TestSize.Level1)
200 {
201     if (g_sensorInterface == nullptr) {
202         ASSERT_NE(nullptr, g_sensorInterface);
203         return;
204     }
205     int32_t ret = g_sensorInterface->Register(SENSOR_GROUP_TYPE_MAX, g_medicalCallback);
206     EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
207     ret = g_sensorInterface->Unregister(SENSOR_GROUP_TYPE_MAX, g_medicalCallback);
208     EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
209 }
210 
211 /**
212   * @tc.name: EnableSensor0001
213   * @tc.desc: Enables the sensor unavailable in the sensor list based on the specified sensor ID.
214   * @tc.type: FUNC
215   * @tc.require: #I4L3LF
216   */
217 HWTEST_F(HdfSensorHdiTest, EnableSensor0001, TestSize.Level1)
218 {
219     if (g_sensorInterface == nullptr) {
220         ASSERT_NE(nullptr, g_sensorInterface);
221         return;
222     }
223     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
224     EXPECT_EQ(SENSOR_SUCCESS, ret);
225 
226     EXPECT_GT(g_info.size(), 0);
227 
228     for (auto iter : g_info) {
229         ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
230         EXPECT_EQ(SENSOR_SUCCESS, ret);
231         ret = g_sensorInterface->Enable(iter.sensorId);
232         EXPECT_EQ(SENSOR_SUCCESS, ret);
233         OsalMSleep(SENSOR_WAIT_TIME);
234         ret = g_sensorInterface->Disable(iter.sensorId);
235         EXPECT_EQ(SENSOR_SUCCESS, ret);
236     }
237     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
238     EXPECT_EQ(0, ret);
239     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
240     SensorCallbackImpl::sensorDataFlag = 1;
241 }
242 
243 /**
244   * @tc.name: EnableSensor0002
245   * @tc.desc: Enables the sensor available in the sensor list based on the specified sensor ID.
246   * @tc.type: FUNC
247   * @tc.require: #I4L3LF #I8FJ2I
248   */
249 HWTEST_F(HdfSensorHdiTest, EnableSensor0002, TestSize.Level1)
250 {
251     if (g_sensorInterface == nullptr) {
252         ASSERT_NE(nullptr, g_sensorInterface);
253         return;
254     }
255     int32_t ret = g_sensorInterface->Enable(ABNORMAL_SENSORID);
256     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
257     ret = g_sensorInterface->Disable(ABNORMAL_SENSORID);
258     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
259 }
260 
261 /**
262   * @tc.name: SetSensorBatch0001
263   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
264   * @tc.type: FUNC
265   * @tc.require: #I4L3LF
266   */
267 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0001, TestSize.Level1)
268 {
269     if (g_sensorInterface == nullptr) {
270         ASSERT_NE(nullptr, g_sensorInterface);
271         return;
272     }
273     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
274     EXPECT_EQ(SENSOR_SUCCESS, ret);
275 
276     for (auto iter : g_info) {
277         ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL2, SENSOR_POLL_TIME);
278         EXPECT_EQ(SENSOR_SUCCESS, ret);
279         ret = g_sensorInterface->Enable(iter.sensorId);
280         EXPECT_EQ(SENSOR_SUCCESS, ret);
281         OsalMSleep(SENSOR_WAIT_TIME);
282         ret = g_sensorInterface->Disable(iter.sensorId);
283         EXPECT_EQ(SENSOR_SUCCESS, ret);
284     }
285 
286     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
287     EXPECT_EQ(SENSOR_SUCCESS, ret);
288     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
289     SensorCallbackImpl::sensorDataFlag = 1;
290 }
291 
292 /** @tc.name: SetSensorBatch0002
293     @tc.desc: Sets the sampling time and data report interval for sensors in batches.
294     @tc.type: FUNC
295     @tc.requrire: #I4L3LF
296     */
297 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0002, TestSize.Level1)
298 {
299     if (g_sensorInterface == nullptr) {
300         ASSERT_NE(nullptr, g_sensorInterface);
301         return;
302     }
303     int32_t ret = g_sensorInterface->SetBatch(ABNORMAL_SENSORID, 0, 0);
304     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
305 }
306 
307 /**
308   * @tc.name: SetSensorBatch0003
309   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
310   * @tc.type: FUNC
311   * @tc.require: #I4L3LF
312   */
313 HWTEST_F(HdfSensorHdiTest, SetSensorBatch0003, TestSize.Level1)
314 {
315     if (g_sensorInterface == nullptr) {
316         ASSERT_NE(nullptr, g_sensorInterface);
317         return;
318     }
319     for (auto iter : g_info) {
320         int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, -1, SENSOR_POLL_TIME);
321         EXPECT_EQ(SENSOR_INVALID_PARAM, ret);
322     }
323 }
324 
325 /**
326   * @tc.name: SetSensorMode0001
327   * @tc.desc: Sets the data reporting mode for the specified sensor.
328   * @tc.type: FUNC
329   * @tc.require: #I4L3LF
330   */
331 HWTEST_F(HdfSensorHdiTest, SetSensorMode0001, TestSize.Level1)
332 {
333     if (g_sensorInterface == nullptr) {
334         ASSERT_NE(nullptr, g_sensorInterface);
335         return;
336     }
337     EXPECT_GT(g_info.size(), 0);
338     for (auto iter : g_info) {
339         int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
340         EXPECT_EQ(SENSOR_SUCCESS, ret);
341         if (iter.sensorId == SENSOR_TYPE_HALL) {
342             ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_ON_CHANGE);
343             EXPECT_EQ(SENSOR_SUCCESS, ret);
344         } else {
345             ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_REALTIME);
346             EXPECT_EQ(SENSOR_SUCCESS, ret);
347         }
348         ret = g_sensorInterface->Enable(iter.sensorId);
349         EXPECT_EQ(SENSOR_SUCCESS, ret);
350         OsalMSleep(SENSOR_WAIT_TIME);
351         ret = g_sensorInterface->Disable(iter.sensorId);
352         EXPECT_EQ(SENSOR_SUCCESS, ret);
353     }
354 }
355 
356 /**
357   * @tc.name: SetSensorMode0002
358   * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid.
359   * Other values are invalid.
360   * @tc.type: FUNC
361   * @tc.require: #I4L3LF
362   */
363 HWTEST_F(HdfSensorHdiTest, SetSensorMode0002, TestSize.Level1)
364 {
365     if (g_sensorInterface == nullptr) {
366         ASSERT_NE(nullptr, g_sensorInterface);
367         return;
368     }
369     int32_t ret = g_sensorInterface->SetMode(ABNORMAL_SENSORID, SENSOR_MODE_REALTIME);
370     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
371 }
372 
373 /**
374   * @tc.name: SetSensorMode0003
375   * @tc.desc: Sets the data reporting mode for the specified sensor.The current real-time polling mode is valid.
376   * Other values are invalid.
377   * @tc.type: FUNC
378   * @tc.require: #I4L3LF
379   */
380 HWTEST_F(HdfSensorHdiTest, SetSensorMode0003, TestSize.Level1)
381 {
382     if (g_sensorInterface == nullptr) {
383         ASSERT_NE(nullptr, g_sensorInterface);
384         return;
385     }
386     EXPECT_GT(g_info.size(), 0);
387     for (auto iter : g_info) {
388         int32_t ret = g_sensorInterface->SetBatch(iter.sensorId, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
389         EXPECT_EQ(SENSOR_SUCCESS, ret);
390         ret = g_sensorInterface->SetMode(iter.sensorId, SENSOR_MODE_DEFAULT);
391         EXPECT_EQ(SENSOR_FAILURE, ret);
392         ret = g_sensorInterface->Enable(iter.sensorId);
393         EXPECT_EQ(SENSOR_SUCCESS, ret);
394         OsalMSleep(SENSOR_WAIT_TIME);
395         ret = g_sensorInterface->Disable(iter.sensorId);
396         EXPECT_EQ(SENSOR_SUCCESS, ret);
397     }
398 }
399 
400 /**
401   * @tc.name: SetSensorOption0001
402   * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy.
403   * @tc.type: FUNC
404   * @tc.require: #I4L3LF
405   */
406 HWTEST_F(HdfSensorHdiTest, SetSensorOption0001, TestSize.Level1)
407 {
408     if (g_sensorInterface == nullptr) {
409         ASSERT_NE(nullptr, g_sensorInterface);
410         return;
411     }
412     EXPECT_GT(g_info.size(), 0);
413     for (auto iter : g_info) {
414         int32_t ret = g_sensorInterface->SetOption(iter.sensorId, 0);
415         EXPECT_EQ(SENSOR_SUCCESS, ret);
416     }
417 }
418 
419 /**
420   * @tc.name: SetSensorOption0002
421   * @tc.desc: Sets options for the specified sensor, including its measurement range and accuracy.
422   * @tc.type: FUNC
423   * @tc.require: #I4L3LF
424   */
425 HWTEST_F(HdfSensorHdiTest, SetSensorOption0002, TestSize.Level1)
426 {
427     if (g_sensorInterface == nullptr) {
428         ASSERT_NE(nullptr, g_sensorInterface);
429         return;
430     }
431     int32_t ret = g_sensorInterface->SetOption(ABNORMAL_SENSORID, 0);
432     EXPECT_EQ(SENSOR_NOT_SUPPORT, ret);
433 }
434 
435 /**
436   * @tc.name: ReadSensorData0001
437   * @tc.desc: Read event data for the specified sensor.
438   * @tc.type: FUNC
439   * @tc.require: #I4L3LF
440   */
441 HWTEST_F(HdfSensorHdiTest, ReadSensorData0001, TestSize.Level1)
442 {
443     ASSERT_NE(nullptr, g_sensorInterface);
444 
445     EXPECT_GT(g_info.size(), 0);
446     for (auto iter : g_info) {
447         int32_t ret = g_sensorInterface->Enable(iter.sensorId);
448         EXPECT_EQ(SENSOR_SUCCESS, ret);
449         OsalMSleep(SENSOR_WAIT_TIME);
450         ret = g_sensorInterface->ReadData(iter.sensorId, g_events);
451         EXPECT_EQ(SENSOR_SUCCESS, ret);
452         ret = g_sensorInterface->Disable(iter.sensorId);
453         EXPECT_EQ(SENSOR_SUCCESS, ret);
454     }
455 }
456 
457 /**
458   * @tc.name: SetSdcSensor
459   * @tc.desc: Read event data for the specified sensor.
460   * @tc.type: FUNC
461   * @tc.require: #I4L3LF
462   */
463 HWTEST_F(HdfSensorHdiTest, SetSdcSensor, TestSize.Level1)
464 {
465     ASSERT_NE(nullptr, g_sensorInterface);
466 
467     EXPECT_GT(g_info.size(), 0);
468     for (auto iter : g_info) {
469         int32_t ret = g_sensorInterface->SetSdcSensor(iter.sensorId, true, RATE_LEVEL);
470         EXPECT_EQ(SENSOR_SUCCESS, ret);
471         OsalMSleep(SENSOR_WAIT_TIME);
472         ret = g_sensorInterface->SetSdcSensor(iter.sensorId, false, RATE_LEVEL);
473         EXPECT_EQ(SENSOR_SUCCESS, ret);
474     }
475 }
476 
477 /**
478   * @tc.name: GetSdcSensorInfo
479   * @tc.desc: Read event data for the specified sensor.
480   * @tc.type: FUNC
481   * @tc.require: #I4L3LF
482   */
483 HWTEST_F(HdfSensorHdiTest, GetSdcSensorInfo, TestSize.Level1)
484 {
485     ASSERT_NE(nullptr, g_sensorInterface);
486 
487     EXPECT_GT(g_info.size(), 0);
488     std::vector<OHOS::HDI::Sensor::V2_0::SdcSensorInfo> sdcSensorInfo;
489     int32_t ret = g_sensorInterface->GetSdcSensorInfo(sdcSensorInfo);
490     EXPECT_EQ(SENSOR_SUCCESS, ret);
491     std::string infoMsg = "[";
492     for (auto it : sdcSensorInfo) {
493         if (infoMsg != "[") {
494             infoMsg += ", ";
495         }
496         infoMsg += "{";
497         infoMsg += "offset = " + std::to_string(it.offset) + ", ";
498         infoMsg += "sensorId = " + std::to_string(it.sensorId) + ", ";
499         infoMsg += "ddrSize = " + std::to_string(it.ddrSize) + ", ";
500         infoMsg += "minRateLevel = " + std::to_string(it.minRateLevel) + ", ";
501         infoMsg += "maxRateLevel = " + std::to_string(it.maxRateLevel) + ", ";
502         infoMsg += "memAddr = " + std::to_string(it.memAddr) + ", ";
503         infoMsg += "reserved = " + std::to_string(it.reserved);
504         infoMsg += "}";
505     }
506     infoMsg += "]";
507     HDF_LOGI("%{public}s: sdcSensorInfo = %{public}s", __func__, infoMsg.c_str());
508 }
509 
510 /**
511   * @tc.name: ReportFrequencyTest0001
512   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
513   * @tc.type: FUNC
514   * @tc.require: #I4L3LF
515   */
516 HWTEST_F(HdfSensorHdiTest, ReportFrequencyTest0001, TestSize.Level1)
517 {
518     HDF_LOGI("enter the ReportFrequencyTest0001 function");
519     ASSERT_NE(nullptr, g_sensorInterface);
520 
521     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
522     EXPECT_EQ(SENSOR_SUCCESS, ret);
523 
524     EXPECT_GT(g_info.size(), 0);
525     int32_t sensorId = g_info[0].sensorId;
526     HDF_LOGI("sensorId is %{public}d", sensorId);
527 
528     ret = g_sensorInterface->SetBatch(sensorId, SENSOR_INTERVAL1, SENSOR_INTERVAL1);
529     EXPECT_EQ(SENSOR_SUCCESS, ret);
530 
531     ret = g_sensorInterface->Enable(sensorId);
532     EXPECT_EQ(SENSOR_SUCCESS, ret);
533 
534     OsalMSleep(SENSOR_WAIT_TIME2);
535 
536     ret = g_sensorInterface->Disable(sensorId);
537     EXPECT_EQ(SENSOR_SUCCESS, ret);
538 
539     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
540     EXPECT_EQ(SENSOR_SUCCESS, ret);
541 
542     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
543     SensorCallbackImpl::sensorDataFlag = 1;
544 }
545 
546 /**
547   * @tc.name: ReportFrequencyTest0002
548   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
549   * @tc.type: FUNC
550   * @tc.require: #I4L3LF
551   */
552 HWTEST_F(HdfSensorHdiTest, ReportFrequencyTest0002, TestSize.Level1)
553 {
554     HDF_LOGI("enter the ReportFrequencyTest0002 function");
555     ASSERT_NE(nullptr, g_sensorInterface);
556 
557     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
558     EXPECT_EQ(SENSOR_SUCCESS, ret);
559 
560     EXPECT_GT(g_info.size(), 0);
561     int32_t sensorId = g_info[0].sensorId;
562     HDF_LOGI("sensorId is %{public}d", sensorId);
563 
564     ret = g_sensorInterface->SetBatch(sensorId, SENSOR_INTERVAL3, SENSOR_INTERVAL1);
565     EXPECT_EQ(SENSOR_SUCCESS, ret);
566 
567     ret = g_sensorInterface->Enable(sensorId);
568     EXPECT_EQ(SENSOR_SUCCESS, ret);
569 
570     OsalMSleep(SENSOR_WAIT_TIME2);
571 
572     ret = g_sensorInterface->Disable(sensorId);
573     EXPECT_EQ(SENSOR_SUCCESS, ret);
574 
575     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
576     EXPECT_EQ(SENSOR_SUCCESS, ret);
577 
578     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
579     SensorCallbackImpl::sensorDataFlag = 1;
580 }
581 
582 /**
583   * @tc.name: ReportFrequencyTest0003
584   * @tc.desc: Sets the sampling time and data report interval for sensors in batches.
585   * @tc.type: FUNC
586   * @tc.require: #I4L3LF
587   */
588 HWTEST_F(HdfSensorHdiTest, ReportFrequencyTest0003, TestSize.Level1)
589 {
590     HDF_LOGI("enter the ReportFrequencyTest0003 function");
591     ASSERT_NE(nullptr, g_sensorInterface);
592 
593     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
594     EXPECT_EQ(SENSOR_SUCCESS, ret);
595 
596     EXPECT_GT(g_info.size(), 0);
597     int32_t sensorId = g_info[0].sensorId;
598     HDF_LOGI("sensorId is %{public}d", sensorId);
599 
600     ret = g_sensorInterface->SetBatch(sensorId, SENSOR_INTERVAL4, SENSOR_INTERVAL1);
601     EXPECT_EQ(SENSOR_SUCCESS, ret);
602 
603     ret = g_sensorInterface->Enable(sensorId);
604     EXPECT_EQ(SENSOR_SUCCESS, ret);
605 
606     OsalMSleep(SENSOR_WAIT_TIME2);
607 
608     ret = g_sensorInterface->Disable(sensorId);
609     EXPECT_EQ(SENSOR_SUCCESS, ret);
610 
611     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
612     EXPECT_EQ(SENSOR_SUCCESS, ret);
613 
614     EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
615     SensorCallbackImpl::sensorDataFlag = 1;
616 }
617 
618 /**
619   * @tc.name: SetSdcSensor_001
620   * @tc.desc: Read event data for the specified sensor.
621   * @tc.type: FUNC
622   * @tc.require: #I4L3LF
623   */
624 HWTEST_F(HdfSensorHdiTest, SetSdcSensor_001, TestSize.Level1)
625 {
626     SENSOR_TRACE;
627     HDF_LOGI("enter the SetSdcSensor_001 function");
628     ASSERT_NE(nullptr, g_sensorInterface);
629     int32_t ret;
630     EXPECT_GT(g_info.size(), 0);
631     for (auto iter : g_info) {
632         ret = g_sensorInterface->SetSdcSensor(iter.sensorId, true, RATE_LEVEL);
633         EXPECT_EQ(SENSOR_SUCCESS, ret);
634         ret = g_sensorInterface->Disable(iter.sensorId);
635         EXPECT_EQ(SENSOR_SUCCESS, ret);
636         ret = g_sensorInterface->SetSdcSensor(iter.sensorId, false, RATE_LEVEL);
637         EXPECT_EQ(SENSOR_SUCCESS, ret);
638     }
639 }
640 
641 /**
642   * @tc.name: EnableButUnregisterTest
643   * @tc.desc: Read event data for the specified sensor.
644   * @tc.type: FUNC
645   * @tc.require: #I4L3LF
646   */
647 HWTEST_F(HdfSensorHdiTest, EnableButUnregisterTest, TestSize.Level1)
648 {
649     SENSOR_TRACE;
650     ASSERT_NE(nullptr, g_sensorInterface);
651     HDF_LOGI("enter the EnableButUnregisterTest function");
652 
653     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
654     EXPECT_EQ(SENSOR_SUCCESS, ret);
655     EXPECT_GT(g_info.size(), 0);
656     for (auto iter : g_info) {
657         int32_t ret = g_sensorInterface->Enable(iter.sensorId);
658         EXPECT_EQ(SENSOR_SUCCESS, ret);
659     }
660     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
661     EXPECT_EQ(SENSOR_SUCCESS, ret);
662     OsalMSleep(SENSOR_WAIT_TIME2);
663     for (auto iter : g_info) {
664         int32_t ret = g_sensorInterface->Disable(iter.sensorId);
665         EXPECT_EQ(SENSOR_SUCCESS, ret);
666     }
667 }
668 
669 /**
670   * @tc.name: SensorCallbackImplFailureTest
671   * @tc.desc: Read event data for the specified sensor.
672   * @tc.type: FUNC
673   * @tc.require: #I4L3LF
674   */
675 HWTEST_F(HdfSensorHdiTest, SensorCallbackImplFailureTest, TestSize.Level1)
676 {
677     SENSOR_TRACE;
678     ASSERT_NE(nullptr, g_sensorInterface);
679     HDF_LOGI("enter the SensorCallbackImplFailureTest function");
680 
681     int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallbackTest);
682     EXPECT_EQ(SENSOR_SUCCESS, ret);
683     EXPECT_GT(g_info.size(), 0);
684     for (auto iter : g_info) {
685         int32_t ret = g_sensorInterface->Enable(iter.sensorId);
686         EXPECT_EQ(SENSOR_SUCCESS, ret);
687     }
688     OsalMSleep(SENSOR_WAIT_TIME2);
689     for (auto iter : g_info) {
690         int32_t ret = g_sensorInterface->Disable(iter.sensorId);
691         EXPECT_EQ(SENSOR_SUCCESS, ret);
692     }
693     ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
694     EXPECT_EQ(SENSOR_SUCCESS, ret);
695 }