• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Sensor
2
3
4## Overview
5
6### Function
7
8The sensor driver model shields the hardware difference and provides interfaces for the upper-layer sensor service to implement basic sensor capabilities, including querying the sensor list, enabling or disabling a sensor, subscribing to or unsubscribing from sensor data changes, and setting sensor attributes. Developed based on the Hardware Driver Foundation (HDF), the sensor driver model leverages the capabilities of the OS adaptation layer (OSAL) and platform driver interfaces (such as I2C, SPI, and UART buses) to shield the difference between OSs and platform bus resources, achieving "one-time development and multi-system deployment" of the sensor driver. The figure below shows the architecture of the sensor driver model.
9
10**Figure 1** Sensor driver model
11
12![Sensor driver model](figures/sensor_driver_model.png)
13
14### Basic Concepts
15
16Sensors are classified into the following types by sensor ID:
17
18- Medical sensors: The sensor IDs range from 128 to 160.
19
20- Traditional sensors: The sensor IDs are out of the range of 128 to 160.
21
22### Working Principles
23
24The following figure shows how a sensor driver works.
25
26**Figure 2** How a sensor driver works
27
28![How sensor driver works](figures/sensor_working.png)
29
30The following uses the acceleration sensor driver on the Hi3516D V300 development board of the standard system as an example to describe the driver loading and running process.
31
321. The sensor host reads the sensor device management configuration from **Sensor Host** in the **device_info.hcs** file.
332. The sensor host parses the sensor management configuration from the HCB database and associates the configuration with the sensor driver.
343. The sensor host loads and initializes the sensor manager driver.
354. The sensor manager driver publishes the sensor APIs for the hardware driver interface (HDI).
365. The sensor host reads the acceleration sensor driver configuration information from **Sensor Host** in the **device_info.hcs** configuration file.
376. The sensor host loads the acceleration sensor abstract driver and calls the initialization interface to allocate the sensor driver resources and create the data processing queue.
387. The sensor host reads the chipset driver configuration and private configuration of the acceleration sensor from the **accel_xxx_config.hcs** file.
398. The acceleration sensor chipset driver calls the common configuration parsing interface to parse the sensor attributes and registers.
409. The chipset driver detects sensors, allocates configuration resources to the acceleration sensor, and registers the acceleration sensor chipset interfaces.
4110. Upon successful sensor detection, the chipset driver instructs the abstract driver to register the acceleration sensor to the sensor manager driver.
42
43## Development Guidelines
44
45### When to Use
46
47- Data provided by the gravity and gyroscope sensors denotes the tilt and rotation of the device, which helps your application improve user experience in games.
48- Data provided by the proximity sensor denotes the distance between the device and a visible object, which enables the device to automatically turn on or off its screen accordingly to prevent accidental touch on the screen. For example, when the proximity sensor detects the user face approaches the earpiece during a call, it triggers backlight of the screen to be turned off. This prevents the screen from being accidentally touched and further reduces power consumption.
49- Data provided by the barometric pressure sensor helps your application accurately determine the altitude of the device.
50- Data provided by the ambient light sensor helps your device automatically adjust its backlight.
51- Data provided by the Hall effect sensor implements the smart cover mode of your device. When the smart cover is closed, a small window is opened on the phone to reduce power consumption.
52
53### Available APIs
54
55The sensor driver model offers the following APIs:
56
57- Sensor HDI APIs, for easier sensor service development
58
59- APIs for implementing sensor driver model capabilities
60  - APIs for loading, registering, and deregitering sensor drivers, and detecting sensors based on the HDF
61  - Unified driver API, register configuration parsing API, bus access abstract API, and platform abstract API for the same type of sensors
62
63- APIs to be implemented by developers
64
65  Based on the HDF Configuration Source (HCS) and differentiated configuration for sensors of the same type, you need to implement serialized configuration of sensor device parameters and some sensor device operation interfaces to simplify sensor driver development.
66
67The sensor driver model provides APIs for the hardware service to make sensor service development easier. See the table below.
68
69**Table 1** APIs of the sensor driver model
70
71| API| Description|
72| ----- | -------- |
73| int32_t GetAllSensors(struct SensorInformation **sensorInfo, int32_t *count) | Obtains information about all registered sensors in the system. The sensor information includes the sensor name, sensor vendor, firmware version, hardware version, sensor type ID, sensor ID, maximum range, accuracy, and power consumption.|
74| int32_t Enable(int32_t sensorId) | Enables a sensor. The subscriber can obtain sensor data only after the sensor is enabled.|
75| int32_t Disable(int32_t sensorId) | Disables a sensor.|
76| int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) | Sets the sampling interval and data reporting interval for a sensor.|
77| int32_t SetMode(int32_t sensorId, int32_t mode) | Sets the data reporting mode for a sensor.|
78| int32_t SetOption(int32_t sensorId, uint32_t option) | Sets options for a sensor, including its range and accuracy.|
79| int32_t Register(int32_t groupId, RecordDataCallback cb) | Registers a sensor data callback based on the group ID.|
80| int32_t Unregister(int32_t groupId, RecordDataCallback cb) | Deregisters a sensor data callback based on the group ID.|
81
82
83
84The sensor driver model provides driver development APIs that do not require further implementation. See the table below.
85
86 **Table 2** Sensor driver development APIs
87
88| API| Description|
89| ----- | -------- |
90| int32_t AddSensorDevice(const struct SensorDeviceInfo *deviceInfo) | Adds a sensor of the current type to the sensor management module.|
91| int32_t DeleteSensorDevice(const struct SensorBasicInfo *sensorBaseInfo) | Deletes a sensor from the sensor management module.|
92| int32_t ReportSensorEvent(const struct SensorReportEvent *events) | Reports data of a specified sensor type.|
93| int32_t ReadSensor(struct SensorBusCfg *busCfg, uint16_t regAddr, uint8_t *data, uint16_t dataLen) | Reads sensor configuration data from the sensor register based on the bus configuration.|
94| int32_t WriteSensor(struct SensorBusCfg *busCfg, uint8_t *writeData, uint16_t len) | Writes sensor configuration data to the sensor register based on the bus configuration.|
95| int32_t SetSensorRegCfgArray(struct SensorBusCfg *busCfg, const struct SensorRegCfgGroupNode *group); | Sets the sensor register group configuration based on the sensor bus type.|
96| int32_t GetSensorBaseConfigData(const struct DeviceResourceNode *node, struct SensorCfgData *config) | Obtains basic configuration information such as sensor, bus, and attribute configurations based on the device information HCS configuration, and initializes the basic configuration data structure.|
97| int32_t ParseSensorRegConfig(struct SensorCfgData *config) | Parses the register group information based on the device information HCS configuration and initializes the configuration data structure.|
98| void ReleaseSensorAllRegConfig(struct SensorCfgData *config) | Releases the resources allocated to the sensor configuration data structure.|
99| int32_t GetSensorBusHandle(struct SensorBusCfg *busCfg) | Obtains the sensor bus handle information.|
100| int32_t ReleaseSensorBusHandle(struct SensorBusCfg *busCfg) | Releases the sensor bus handle information.|
101
102
103
104The sensor driver model also provides certain driver development APIs that need to be implemented by driver developers. See the table below.
105
106**Table 3** APIs to be implemented by driver developers
107
108| API| Description|
109| ----- | -------- |
110| int32_t init(void) | Initializes the sensor device configuration after a sensor is detected.|
111| int32_t Enable(void) | Enables the current sensor by delivering the register configuration in the enabling operation group based on the device information HCS configuration.|
112| int32_t Disable(void) | Disables the current sensor by delivering the register configuration in the disabling operation group based on the device information HCS configuration.|
113| int32_t SetBatch(int64_t samplingInterval, int64_t reportInterval) | Sets the processing time of the data reporting thread for the current sensor based on the sampling interval and data reporting interval.|
114| int32_t SetMode(int32_t mode) | Sets the data reporting mode of the current sensor device.|
115| int32_t SetOption(uint32_t option) | Sets the register configuration such as the range and accuracy based on sensor options.|
116| void ReadSensorData(void) | Reads sensor data.|
117
118
119### How to Develop
1201. Develop the acceleration sensor abstract driver. Specifically, implement the **Bind**, **Init**, **Release**, and **Dispatch** functions.
121
122   - Implement the entry function for the acceleration sensor.
123
124     ```c
125     /* Register the entry structure object of the acceleration sensor. */
126     struct HdfDriverEntry g_sensorAccelDevEntry = {
127         .moduleVersion = 1,                // Version of the acceleration sensor module.
128         .moduleName = "HDF_SENSOR_ACCEL",  // Name of the acceleration sensor module. The value must be the same as that of moduleName in the device_info.hcs file.
129         .Bind = BindAccelDriver,           // Function for binding an acceleration sensor.
130         .Init = InitAccelDriver,           // Function for initializing an acceleration sensor.
131         .Release = ReleaseAccelDriver      // Function for releasing acceleration sensor resources.
132     };
133
134     /* Call HDF_INIT to register the driver entry with the HDF. When loading the driver, the HDF calls Bind() and then Init() to load the driver. If Init() fails to be called, the HDF calls Release() to release resources and exit. */
135     HDF_INIT(g_sensorAccelDevEntry);
136     ```
137
138   - Implement interfaces for acceleration sensor driver operations.
139
140     ```c
141     /* Bind the service provided by the acceleration sensor driver to the HDF. */
142     int32_t AccelBindDriver(struct HdfDeviceObject *device)
143     {
144         CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
145
146         struct AccelDrvData *drvData = (struct AccelDrvData *)OsalMemCalloc(sizeof(*drvData));
147         if (drvData == NULL) {
148             HDF_LOGE("%s: Malloc accel drv data fail!", __func__);
149             return HDF_ERR_MALLOC_FAIL;
150         }
151
152         drvData->ioService.Dispatch = DispatchAccel;
153         drvData->device = device;
154         device->service = &drvData->ioService;
155         g_accelDrvData = drvData;
156         return HDF_SUCCESS;
157     }
158
159     /* Register the normalization functions of the acceleration sensor driver. */
160     static int32_t InitAccelOps(struct SensorCfgData *config, struct SensorDeviceInfo *deviceInfo)
161     {
162         CHECK_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
163
164         deviceInfo->ops.Enable = SetAccelEnable;
165         deviceInfo->ops.Disable = SetAccelDisable;
166         deviceInfo->ops.SetBatch = SetAccelBatch;
167         deviceInfo->ops.SetMode = SetAccelMode;
168         deviceInfo->ops.SetOption = SetAccelOption;
169
170         if (memcpy_s(&deviceInfo->sensorInfo, sizeof(deviceInfo->sensorInfo),
171             &config->sensorInfo, sizeof(config->sensorInfo)) != EOK) {
172             HDF_LOGE("%s: Copy sensor info failed", __func__);
173             return HDF_FAILURE;
174         }
175
176         return HDF_SUCCESS;
177     }
178     /* Provide the initialization interface for the chipset driver to parse the basic acceleration sensor configuration (acceleration information, bus configuration, and sensor detection register configuration), detect sensors, and parse sensor registers. */
179     static int32_t InitAccelAfterDetected(struct SensorCfgData *config)
180     {
181         struct SensorDeviceInfo deviceInfo;
182         CHECK_NULL_PTR_RETURN_VALUE(config, HDF_ERR_INVALID_PARAM);
183         /* Initialize the acceleration sensor function. */
184         if (InitAccelOps(config, &deviceInfo) != HDF_SUCCESS) {
185             HDF_LOGE("%s: Init accel ops failed", __func__);
186             return HDF_FAILURE;
187         }
188         /* Register the acceleration sensor with the sensor management module. */
189         if (AddSensorDevice(&deviceInfo) != HDF_SUCCESS) {
190             HDF_LOGE("%s: Add accel device failed", __func__);
191             return HDF_FAILURE;
192         }
193         /* Parse the sensor register. */
194         if (ParseSensorRegConfig(config) != HDF_SUCCESS) {
195             HDF_LOGE("%s: Parse sensor register failed", __func__);
196             (void)DeleteSensorDevice(&config->sensorInfo);
197             ReleaseSensorAllRegConfig(config);
198             return HDF_FAILURE;
199         }
200         return HDF_SUCCESS;
201     }
202     struct SensorCfgData *AccelCreateCfgData(const struct DeviceResourceNode *node)
203     {
204         ...
205         /* Continue the next detection if the sensor is not detected. */
206         if (drvData->detectFlag) {
207             HDF_LOGE("%s: Accel sensor have detected", __func__);
208             return NULL;
209         }
210         if (drvData->accelCfg == NULL) {
211             HDF_LOGE("%s: Accel accelCfg pointer NULL", __func__);
212             return NULL;
213         }
214         /* Parse the basic sensor configuration. */
215         if (GetSensorBaseConfigData(node, drvData->accelCfg) != HDF_SUCCESS) {
216             HDF_LOGE("%s: Get sensor base config failed", __func__);
217             goto BASE_CONFIG_EXIT;
218         }
219         /* Continue the next detection if the sensor is not detected. */
220         if (DetectSensorDevice(drvData->accelCfg) != HDF_SUCCESS) {
221             HDF_LOGI("%s: Accel sensor detect device no exist", __func__);
222             drvData->detectFlag = false;
223             goto BASE_CONFIG_EXIT;
224         }
225         drvData->detectFlag = true;
226         /* Parse the sensor register. */
227         if (InitAccelAfterDetected(drvData->accelCfg) != HDF_SUCCESS) {
228             HDF_LOGE("%s: Accel sensor detect device no exist", __func__);
229             goto INIT_EXIT;
230         }
231         return drvData->accelCfg;
232         ...
233     }
234     /* The entry function of the acceleration sensor driver is used to initialize the sensor private data structure object, allocate space for the sensor HCS data configuration object, call the entry function for initializing the sensor HCS data configuration, detect whether the sensor device is in position, create a timer for sensor data reporting, register the sensor normalization APIs, and register the sensor device. */
235     int32_t AccelInitDriver(struct HdfDeviceObject *device)
236     {
237         ...
238         /* Initialize work queue resources. */
239         if (InitAccelData(drvData) != HDF_SUCCESS) {
240             HDF_LOGE("%s: Init accel config failed", __func__);
241             return HDF_FAILURE;
242         }
243         /* Allocate acceleration configuration resources. */
244         drvData->accelCfg = (struct SensorCfgData *)OsalMemCalloc(sizeof(*drvData->accelCfg));
245         if (drvData->accelCfg == NULL) {
246             HDF_LOGE("%s: Malloc accel config data failed", __func__);
247             return HDF_FAILURE;
248         }
249         /* Register the register group information. */
250         drvData->accelCfg->regCfgGroup = &g_regCfgGroup[0];
251         ...
252         return HDF_SUCCESS;
253     }
254     /* Release the resources allocated during driver initialization. */
255     void AccelReleaseDriver(struct HdfDeviceObject *device)
256     {
257         CHECK_NULL_PTR_RETURN(device);
258         struct AccelDrvData *drvData = (struct AccelDrvData *)device->service;
259         CHECK_NULL_PTR_RETURN(drvData);
260         /* Release the resources if the sensor is in position. */
261         if (drvData->detectFlag) {
262             AccelReleaseCfgData(drvData->accelCfg);
263         }
264         OsalMemFree(drvData->accelCfg);
265         drvData->accelCfg = NULL;
266         /* Destroy the work queue resource if the sensor is in position. */
267         HdfWorkDestroy(&drvData->accelWork);
268         HdfWorkQueueDestroy(&drvData->accelWorkQueue);
269         OsalMemFree(drvData);
270     }
271     ```
272
2732. Configure the device information about the acceleration sensor driver.
274
275   The acceleration sensor model uses the HCS as the configuration source code. For details about the HCS configuration fields, see [Driver Configuration Management](driver-hdf-manage.md).
276
277   ```hcs
278   /* Device information HCS configuration of the acceleration sensor. */
279   device_sensor_accel :: device {
280       device0 :: deviceNode {
281           policy = 1;                                  // Policy for the driver to publish services.
282           priority = 100;                              // Priority (0–200) for starting the driver. A larger value indicates a lower priority. The recommended value is 100. If the priorities are the same, the device loading sequence is not ensured.
283           preload = 0;                                 // The value 0 means to load the driver by default during the startup of the system. The value 2 means the opposite.
284           permission = 0664;                           // Permission for the device node created.
285           moduleName = "HDF_SENSOR_ACCEL";             // Driver name. It must be the same as moduleName in the driver entry structure.
286           serviceName = "sensor_accel";                // Name of the service published by the driver. The name must be unique.
287           deviceMatchAttr = "hdf_sensor_accel_driver"; // Keyword matching the private data of the driver. The value must be the same as that of match_attr in the private data configuration table of the driver.
288       }
289   }
290   ```
291
2923. Develop the internal interfaces of the acceleration sensor abstract driver. Specifically, implement the **Enable**, **Disable**, **SetBatch**, **SetMode**, **SetOption**, **AccelCreateCfgData**, **AccelReleaseCfgData**, and **AccelRegisterChipOps** functions.
293
294   ```c
295   /* Leave a function empty if it is not used. */
296   static int32_t SetAccelInfo(struct SensorBasicInfo *info)
297   {
298       (void)info;
299
300       return HDF_ERR_NOT_SUPPORT;
301   }
302   /* Deliver the configuration of enabling the register groups. */
303   static int32_t SetAccelEnable(void)
304   {
305       int32_t ret;
306       struct AccelDrvData *drvData = AccelGetDrvData();
307
308       CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
309       CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
310
311       if (drvData->enable) {
312           HDF_LOGE("%s: Accel sensor is enabled", __func__);
313           return HDF_SUCCESS;
314       }
315
316       ret = SetSensorRegCfgArray(&drvData->accelCfg->busCfg, drvData->accelCfg->regCfgGroup[SENSOR_ENABLE_GROUP]);
317       if (ret != HDF_SUCCESS) {
318           HDF_LOGE("%s: Accel sensor enable config failed", __func__);
319           return ret;
320       }
321
322       ret = OsalTimerCreate(&drvData->accelTimer, SENSOR_TIMER_MIN_TIME, AccelTimerEntry, (uintptr_t)drvData);
323       if (ret != HDF_SUCCESS) {
324           HDF_LOGE("%s: Accel create timer failed[%d]", __func__, ret);
325           return ret;
326       }
327
328       ret = OsalTimerStartLoop(&drvData->accelTimer);
329       if (ret != HDF_SUCCESS) {
330           HDF_LOGE("%s: Accel start timer failed[%d]", __func__, ret);
331           return ret;
332       }
333       drvData->enable = true;
334
335       return HDF_SUCCESS;
336   }
337   /* Deliver the configuration of disabling the register groups. */
338   static int32_t SetAccelDisable(void)
339   {
340       int32_t ret;
341       struct AccelDrvData *drvData = AccelGetDrvData();
342
343       CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
344       CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
345
346       if (!drvData->enable) {
347           HDF_LOGE("%s: Accel sensor had disable", __func__);
348           return HDF_SUCCESS;
349       }
350
351       ret = SetSensorRegCfgArray(&drvData->accelCfg->busCfg, drvData->accelCfg->regCfgGroup[SENSOR_DISABLE_GROUP]);
352       if (ret != HDF_SUCCESS) {
353           HDF_LOGE("%s: Accel sensor disable config failed", __func__);
354           return ret;
355       }
356
357       ret = OsalTimerDelete(&drvData->accelTimer);
358       if (ret != HDF_SUCCESS) {
359           HDF_LOGE("%s: Accel delete timer failed", __func__);
360           return ret;
361       }
362       drvData->enable = false;
363
364       return HDF_SUCCESS;
365   }
366   /* Set the sampling interval and data reporting interval of the sensor. */
367   static int32_t SetAccelBatch(int64_t samplingInterval, int64_t interval)
368   {
369       (void)interval;
370
371       struct AccelDrvData *drvData = NULL;
372
373       drvData = AccelGetDrvData();
374       CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
375
376       drvData->interval = samplingInterval;
377
378       return HDF_SUCCESS;
379   }
380   /* Set the data reporting mode of the sensor. Currently, the real-time mode is supported. */
381   static int32_t SetAccelMode(int32_t mode)
382   {
383       return (mode == SENSOR_WORK_MODE_REALTIME) ? HDF_SUCCESS : HDF_FAILURE;
384   }
385
386   static int32_t SetAccelOption(uint32_t option)
387   {
388       (void)option;
389       return HDF_SUCCESS;
390   }
391   /* Set the sensor options. */
392   static int32_t SetAccelOption(uint32_t option)
393   {
394       (void)option;
395       return HDF_ERR_NOT_SUPPORT;
396   }
397   ```
398
3994. Develop the acceleration sensor chipset driver. Specifically, implement the **Bind**, **Init**, **Release**, and **Dispatch** functions.
400
401   ```c
402   /* Message interaction of the acceleration sensor chipset driver */
403   static int32_t DispatchBMI160(struct HdfDeviceIoClient *client,
404       int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
405   {
406       (void)client;
407       (void)cmd;
408       (void)data;
409       (void)reply;
410
411       return HDF_SUCCESS;
412   }
413   /* Bind the service provided by the acceleration sensor chipset driver to the HDF. */
414   int32_t Bmi160BindDriver(struct HdfDeviceObject *device)
415   {
416       CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
417
418       struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)OsalMemCalloc(sizeof(*drvData));
419       if (drvData == NULL) {
420           HDF_LOGE("%s: Malloc Bmi160 drv data fail", __func__);
421           return HDF_ERR_MALLOC_FAIL;
422       }
423
424       drvData->ioService.Dispatch = DispatchBMI160;
425       drvData->device = device;
426       device->service = &drvData->ioService;
427       g_bmi160DrvData = drvData;
428
429       return HDF_SUCCESS;
430   }
431   /* Initialize the acceleration sensor chipset driver. */
432   int32_t Bmi160InitDriver(struct HdfDeviceObject *device)
433   {
434       int32_t ret;
435       struct AccelOpsCall ops;
436
437       CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
438       struct Bmi160DrvData *drvData = (struct Bmi160DrvData *)device->service;
439       CHECK_NULL_PTR_RETURN_VALUE(drvData, HDF_ERR_INVALID_PARAM);
440
441       ret = InitAccelPreConfig();
442       if (ret != HDF_SUCCESS) {
443           HDF_LOGE("%s: Init  BMI160 bus mux config", __func__);
444           return HDF_FAILURE;
445       }
446
447       drvData->sensorCfg = AccelCreateCfgData(device->property);
448       if (drvData->sensorCfg == NULL || drvData->sensorCfg->root == NULL) {
449           HDF_LOGD("%s: Creating accelcfg failed because detection failed", __func__);
450           return HDF_ERR_NOT_SUPPORT;
451       }
452
453       ops.Init = NULL;
454       ops.ReadData = ReadBmi160Data;
455       ret = AccelRegisterChipOps(&ops);
456       if (ret != HDF_SUCCESS) {
457           HDF_LOGE("%s: Register BMI160 accel failed", __func__);
458           return HDF_FAILURE;
459       }
460
461       ret = InitBmi160(drvData->sensorCfg);
462       if (ret != HDF_SUCCESS) {
463           HDF_LOGE("%s: Init BMI160 accel failed", __func__);
464           return HDF_FAILURE;
465       }
466
467       return HDF_SUCCESS;
468   }
469   /* Release the resources allocated during driver initialization. */
470   void Bmi160ReleaseDriver(struct HdfDeviceObject *device)
471   {
472   	...
473       if (drvData->sensorCfg != NULL) {
474           AccelReleaseCfgData(drvData->sensorCfg);
475           drvData->sensorCfg = NULL;
476       }
477       OsalMemFree(drvData);
478   }
479   /*HdfDriverEntry object corresponding to the acceleration sensor chipset driver */
480   struct HdfDriverEntry g_accelBmi160DevEntry = {
481       .moduleVersion = 1,
482       .moduleName = "HDF_SENSOR_ACCEL_BMI160",
483       .Bind = Bmi160BindDriver,
484       .Init = Bmi160InitDriver,
485       .Release = Bmi160ReleaseDriver,
486   };
487   HDF_INIT(g_accelBmi160DevEntry);
488   ```
489
4905. Implement the **ReadData** function of the acceleration sensor chipset driver.
491
492   ```c
493   int32_t ReadBmi160Data(struct SensorCfgData *data)
494   {
495       int32_t ret;
496       struct AccelData rawData = { 0, 0, 0 };
497       int32_t tmp[ACCEL_AXIS_NUM];
498       struct SensorReportEvent event;
499       (void)memset_s(&event, sizeof(event), 0, sizeof(event));
500       ret = ReadBmi160RawData(data, &rawData, &event.timestamp);
501       if (ret != HDF_SUCCESS) {
502           HDF_LOGE("%s: BMI160 read raw data failed", __func__);
503           return HDF_FAILURE;
504       }
505       event.sensorId = SENSOR_TAG_ACCELEROMETER;
506       event.option = 0;
507       event.mode = SENSOR_WORK_MODE_REALTIME;
508       ...
509       ret = ReportSensorEvent(&event);
510       if (ret != HDF_SUCCESS) {
511           HDF_LOGE("%s: BMI160 report data failed", __func__);
512       }
513       return ret;
514   }
515   ```
516
517### Verification
518
519After the driver is developed, develop test cases in the sensor unit test to verify the basic functions of the driver. Use your test platform to set up the test environment.
520
521```c++
522static int32_t g_sensorDataFlag = 0;                        // Whether to report sensor data.
523static const struct SensorInterface *g_sensorDev = nullptr; // Retain the obtained sensor interface instance address.
524
525/* Register the data reporting function. */
526static int SensorTestDataCallback(struct SensorEvents *event)
527{
528    if (event == nullptr) {
529        return -1;
530    }
531    float *data = (float*)event->data;
532    printf("time [%lld] sensor id [%d] x-[%f] y-[%f] z-[%f]\n\r", event->timestamp,
533        event->sensorId, (*data), *(data + 1), *(data + g_axisZ));
534    if (*data > 1e-5) {
535        g_sensorDataFlag = 1;
536    }
537    return 0;
538}
539/* Initialize the sensor interface instance before executing the test cases. */
540void HdfSensorTest::SetUpTestCase()
541{
542    g_sensorDev = NewSensorInterfaceInstance();
543    if (g_sensorDev == nullptr) {
544        printf("test sensor get module instance failed\n\r");
545    }
546}
547/* Release case resources. */
548void HdfSensorTest::TearDownTestCase()
549{
550    if (g_sensorDev != nullptr) {
551        FreeSensorInterfaceInstance();
552        g_sensorDev = nullptr;
553    }
554}
555/* Verify the sensor driver. */
556HWTEST_F(HdfSensorTest,TestAccelDriver_001, TestSize.Level0)
557{
558    int32_t sensorInterval = 1000000000;    // Data sampling interval, in nanoseconds.
559    int32_t pollTime = 5;                   // Data sampling time, in seconds.
560    int32_t accelSensorId = 1;              // Acceleration sensor ID, which specifies the sensor type.
561    int32_t count = 0;
562    int ret;
563    struct SensorInformation *sensorInfo = nullptr;
564
565    ret = g_sensorDev->Register(0, TraditionSensorTestDataCallback)
566    EXPECT_EQ(SENSOR_NULL_PTR, ret);
567
568    ret = g_sensorDev->GetAllSensors(&sensorInfo, &count);
569    EXPECT_EQ(0, ret);
570    if (sensorInfo == nullptr) {
571        EXPECT_NE(nullptr, sensorInfo);
572        return;
573    }
574    /* Print the obtained sensor list. */
575    for (int i = 0; i < count; i++) {
576        printf("get sensorId[%d], info name[%s]\n\r", sensorInfo[i]->sensorId, sensorInfo[i]->sensorName);
577    }
578    ret = g_sensorDev->Enable(accelSensorId);
579    EXPECT_EQ(0, ret);
580    g_sensorDataFlag = 0;
581
582    ret = g_sensorDev->SetBatch(accelSensorId, sensorInterval, pollTime);
583    EXPECT_EQ(0, ret);
584    /* Observe the printed data within the period specified by pollTime. */
585    OsalSleep(pollTime);
586    EXPECT_EQ(1, g_sensorDataFlag);
587
588    ret = g_sensorDev->Disable(accelSensorId);
589    g_sensorDataFlag = 0;
590    EXPECT_EQ(0, ret);
591
592    ret = g_sensorDev->Unregister(0, TraditionSensorTestDataCallback);
593    EXPECT_EQ(0, ret);
594}
595```
596