• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Sensor<a name="EN-US_TOPIC_0000001078401780"></a>
2
3-   [Overview](#section3634112111)
4    -   [Available APIs](#section188213414114)
5
6-   [Development Guidelines](#section1140943382)
7    -   [How to Develop](#section7893102915819)
8
9-   [Development Example](#section257750691)
10-   [Test Guidelines](#section106021256121219)
11
12## Overview<a name="section3634112111"></a>
13
14The sensor driver module provides APIs for upper-layer sensor services to implement basic sensor capabilities, including querying the sensor list, enabling or disabling a sensor, subscribing to or unsubscribing from sensor data, and setting sensor options. The sensor driver model is developed based on the Hardware Driver Foundation \(HDF\) and supports functions such as cross-OS migration and differentiated device configuration. The following figure shows the architecture of the sensor driver model.
15
16**Figure  1**  Architecture of the sensor driver model<a name="fig10451455446"></a>
17![](figure/architecture-of-the-sensor-driver-model.png "architecture-of-the-sensor-driver-model")
18
19The sensor driver model offers the following APIs:
20
21-   Hardware Driver Interfaces \(HDIs\) for sensors: These HDIs facilitate service development.
22-   APIs for implementing sensor driver model capabilities: These APIs implement the capabilities of registering, loading, and unregistering sensor drivers as well as detecting sensor devices depending on the HDF. The APIs include normalized APIs for sensor devices of the same type, APIs for parsing register configurations, abstract APIs for bus access, and abstract platform APIs.
23-   APIs to be implemented by developers: Based on the HDF Configuration Source \(HCS\), developers can implement differentiated configuration for sensors of the same type and serialized configuration of sensor device parameters. Some sensor device operations can be abstracted as APIs to simplify sensor driver development.
24
25### Available APIs<a name="section188213414114"></a>
26
27The following table lists the APIs provided by the sensor driver model.
28
29**Table  1**  External APIs provided by the sensor driver model
30
31<a name="table203963834718"></a>
32<table><thead align="left"><tr id="row173964834716"><th class="cellrowborder" valign="top" width="8.260000000000002%" id="mcps1.2.4.1.1"><p id="p17401913133218"><a name="p17401913133218"></a><a name="p17401913133218"></a>Category</p>
33</th>
34<th class="cellrowborder" valign="top" width="45.4%" id="mcps1.2.4.1.2"><p id="p20921103144918"><a name="p20921103144918"></a><a name="p20921103144918"></a>API</p>
35</th>
36<th class="cellrowborder" valign="top" width="46.339999999999996%" id="mcps1.2.4.1.3"><p id="p109216317495"><a name="p109216317495"></a><a name="p109216317495"></a>Description</p>
37</th>
38</tr>
39</thead>
40<tbody><tr id="row4397198154712"><td class="cellrowborder" valign="top" width="8.260000000000002%" headers="mcps1.2.4.1.1 "><p id="p8437193673211"><a name="p8437193673211"></a><a name="p8437193673211"></a>Query</p>
41</td>
42<td class="cellrowborder" valign="top" width="45.4%" headers="mcps1.2.4.1.2 "><p id="p11001322173912"><a name="p11001322173912"></a><a name="p11001322173912"></a>int32_t <strong id="b935414557240"><a name="b935414557240"></a><a name="b935414557240"></a>GetAllSensors</strong>(struct SensorInformation **sensorInfo, int32_t *count)</p>
43</td>
44<td class="cellrowborder" valign="top" width="46.339999999999996%" headers="mcps1.2.4.1.3 "><p id="p199227318499"><a name="p199227318499"></a><a name="p199227318499"></a>Obtains information about all sensors in the system. The information about a sensor generally includes the sensor name, sensor vendor, firmware version, hardware version, sensor type ID, sensor ID, maximum measurement range, accuracy, and power.</p>
45</td>
46</tr>
47<tr id="row1839716854716"><td class="cellrowborder" rowspan="5" valign="top" width="8.260000000000002%" headers="mcps1.2.4.1.1 "><p id="p06071477324"><a name="p06071477324"></a><a name="p06071477324"></a>Setting</p>
48</td>
49<td class="cellrowborder" valign="top" width="45.4%" headers="mcps1.2.4.1.2 "><p id="p38874252376"><a name="p38874252376"></a><a name="p38874252376"></a>int32_t <strong id="b199602219271"><a name="b199602219271"></a><a name="b199602219271"></a>Enable</strong>(int32_t sensorId)</p>
50</td>
51<td class="cellrowborder" valign="top" width="46.339999999999996%" headers="mcps1.2.4.1.3 "><p id="p5922331114916"><a name="p5922331114916"></a><a name="p5922331114916"></a>Enables the sensor that has been subscribed to. The subscriber can obtain the sensor data only after the sensor is enabled.</p>
52</td>
53</tr>
54<tr id="row6397138134713"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p6923143184914"><a name="p6923143184914"></a><a name="p6923143184914"></a>int32_t <strong id="b84601875330"><a name="b84601875330"></a><a name="b84601875330"></a>Disable</strong>(int32_t sensorId)</p>
55</td>
56<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p139231531184912"><a name="p139231531184912"></a><a name="p139231531184912"></a>Disables a sensor.</p>
57</td>
58</tr>
59<tr id="row43981283476"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p992473112496"><a name="p992473112496"></a><a name="p992473112496"></a>int32_t <strong id="b16691194511438"><a name="b16691194511438"></a><a name="b16691194511438"></a>SetBatch</strong>(iint32_t sensorId, int64_t samplingInterval, int64_t reportInterval)</p>
60</td>
61<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p14924203134910"><a name="p14924203134910"></a><a name="p14924203134910"></a>Sets the data sampling interval and data reporting interval for the specified sensor.</p>
62</td>
63</tr>
64<tr id="row439813812472"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p170411511281"><a name="p170411511281"></a><a name="p170411511281"></a>int32_t <strong id="b170414153284"><a name="b170414153284"></a><a name="b170414153284"></a>SetMode</strong>(int32_t sensorTypeId, SensorUser *user, int32_t mode)</p>
65</td>
66<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p107051159281"><a name="p107051159281"></a><a name="p107051159281"></a>Sets the data reporting mode for the specified sensor.</p>
67</td>
68</tr>
69<tr id="row123998813470"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p492513120494"><a name="p492513120494"></a><a name="p492513120494"></a>int32_t <strong id="b7501191019330"><a name="b7501191019330"></a><a name="b7501191019330"></a>SetOption</strong>(int32_t sensorId, uint32_t option)</p>
70</td>
71<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p5926031124914"><a name="p5926031124914"></a><a name="p5926031124914"></a>Sets options for the specified sensor, including its measurement range and accuracy.</p>
72</td>
73</tr>
74<tr id="row939914814478"><td class="cellrowborder" rowspan="2" valign="top" width="8.260000000000002%" headers="mcps1.2.4.1.1 "><p id="p1039815743211"><a name="p1039815743211"></a><a name="p1039815743211"></a>Data subscription and unsubscription</p>
75</td>
76<td class="cellrowborder" valign="top" width="45.4%" headers="mcps1.2.4.1.2 "><p id="p11530101054411"><a name="p11530101054411"></a><a name="p11530101054411"></a>int32_t <strong id="b0569161217334"><a name="b0569161217334"></a><a name="b0569161217334"></a>Register</strong>(RecordDataCallback cb)</p>
77</td>
78<td class="cellrowborder" valign="top" width="46.339999999999996%" headers="mcps1.2.4.1.3 "><p id="p892633118493"><a name="p892633118493"></a><a name="p892633118493"></a>Registers the callback for reporting sensor data to the subscriber.</p>
79</td>
80</tr>
81<tr id="row10716713314"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p196491214133110"><a name="p196491214133110"></a><a name="p196491214133110"></a>int32_t <strong id="b13758151483317"><a name="b13758151483317"></a><a name="b13758151483317"></a>Unregister</strong>(void)</p>
82</td>
83<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p5817133119"><a name="p5817133119"></a><a name="p5817133119"></a>Unregisters the callback for reporting sensor data.</p>
84</td>
85</tr>
86<tr id="row15679121655614"><td class="cellrowborder" rowspan="2" valign="top" width="8.260000000000002%" headers="mcps1.2.4.1.1 "><p id="p6596162112564"><a name="p6596162112564"></a><a name="p6596162112564"></a>Instance creation</p>
87</td>
88<td class="cellrowborder" valign="top" width="45.4%" headers="mcps1.2.4.1.2 "><p id="p13679191616561"><a name="p13679191616561"></a><a name="p13679191616561"></a>const struct SensorInterface *<strong id="b292451515"><a name="b292451515"></a><a name="b292451515"></a>NewSensorInterfaceInstance</strong>(void)</p>
89</td>
90<td class="cellrowborder" valign="top" width="46.339999999999996%" headers="mcps1.2.4.1.3 "><p id="p19679181612563"><a name="p19679181612563"></a><a name="p19679181612563"></a>Creates a <strong id="b10138658185717"><a name="b10138658185717"></a><a name="b10138658185717"></a>SensorInterface</strong> instance.</p>
91</td>
92</tr>
93<tr id="row9680191675612"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p15680111655611"><a name="p15680111655611"></a><a name="p15680111655611"></a>int32_t <strong id="b1144297156"><a name="b1144297156"></a><a name="b1144297156"></a>FreeSensorInterfaceInstance</strong>(void)</p>
94</td>
95<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p18680916165620"><a name="p18680916165620"></a><a name="p18680916165620"></a>Releases the <strong id="b4150124205810"><a name="b4150124205810"></a><a name="b4150124205810"></a>SensorInterface</strong> instance.</p>
96</td>
97</tr>
98</tbody>
99</table>
100
101The following table lists the APIs provided by the sensor driver model for driver developers. You can directly call these APIs without any implementations.
102
103**Table  2**  APIs provided by the sensor driver model for driver developers
104
105<a name="table1156812588320"></a>
106<table><thead align="left"><tr id="row756817584327"><th class="cellrowborder" valign="top" width="8.550855085508552%" id="mcps1.2.4.1.1"><p id="p7568125873219"><a name="p7568125873219"></a><a name="p7568125873219"></a>Category</p>
107</th>
108<th class="cellrowborder" valign="top" width="45.53455345534553%" id="mcps1.2.4.1.2"><p id="p1756812582328"><a name="p1756812582328"></a><a name="p1756812582328"></a>API</p>
109</th>
110<th class="cellrowborder" valign="top" width="45.91459145914592%" id="mcps1.2.4.1.3"><p id="p35681558183210"><a name="p35681558183210"></a><a name="p35681558183210"></a>Description</p>
111</th>
112</tr>
113</thead>
114<tbody><tr id="row756875811329"><td class="cellrowborder" rowspan="3" valign="top" width="8.550855085508552%" headers="mcps1.2.4.1.1 "><p id="p5974193991911"><a name="p5974193991911"></a><a name="p5974193991911"></a>Device management</p>
115</td>
116<td class="cellrowborder" valign="top" width="45.53455345534553%" headers="mcps1.2.4.1.2 "><p id="p18569158173210"><a name="p18569158173210"></a><a name="p18569158173210"></a>int32_t <strong id="b4994112595516"><a name="b4994112595516"></a><a name="b4994112595516"></a>AddSensorDevice</strong>(const struct SensorDeviceInfo *deviceInfo)</p>
117</td>
118<td class="cellrowborder" valign="top" width="45.91459145914592%" headers="mcps1.2.4.1.3 "><p id="p356935816328"><a name="p356935816328"></a><a name="p356935816328"></a>Adds a sensor of the current type to the sensor management module.</p>
119</td>
120</tr>
121<tr id="row195691858113219"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1156945883215"><a name="p1156945883215"></a><a name="p1156945883215"></a>int32_t <strong id="b1736762975518"><a name="b1736762975518"></a><a name="b1736762975518"></a>DeleteSensorDevice</strong>(int32_t sensorId)</p>
122</td>
123<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p2569145833214"><a name="p2569145833214"></a><a name="p2569145833214"></a>Deletes a specified sensor from the sensor management module.</p>
124</td>
125</tr>
126<tr id="row15699589321"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p6569105811328"><a name="p6569105811328"></a><a name="p6569105811328"></a>int32_t <strong id="b1174510321555"><a name="b1174510321555"></a><a name="b1174510321555"></a>ReportSensorEvent</strong>(const struct SensorReportEvent *events)</p>
127</td>
128<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p15691858193220"><a name="p15691858193220"></a><a name="p15691858193220"></a>Reports data of a specified sensor type.</p>
129</td>
130</tr>
131<tr id="row17569145814329"><td class="cellrowborder" rowspan="4" valign="top" width="8.550855085508552%" headers="mcps1.2.4.1.1 "><p id="p10589113932619"><a name="p10589113932619"></a><a name="p10589113932619"></a>Abstract bus and platform operations</p>
132</td>
133<td class="cellrowborder" valign="top" width="45.53455345534553%" headers="mcps1.2.4.1.2 "><p id="p145705585322"><a name="p145705585322"></a><a name="p145705585322"></a>int32_t <strong id="b15560203515558"><a name="b15560203515558"></a><a name="b15560203515558"></a>ReadSensor</strong>(struct SensorBusCfg *busCfg, uint16_t regAddr, uint8_t *data, uint16_t dataLen)</p>
134</td>
135<td class="cellrowborder" valign="top" width="45.91459145914592%" headers="mcps1.2.4.1.3 "><p id="p1657018586322"><a name="p1657018586322"></a><a name="p1657018586322"></a>Reads sensor configuration data from the sensor register based on the bus configuration.</p>
136</td>
137</tr>
138<tr id="row28712021112011"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p38722218200"><a name="p38722218200"></a><a name="p38722218200"></a>int32_t <strong id="b573774595514"><a name="b573774595514"></a><a name="b573774595514"></a>WriteSensor</strong>(struct SensorBusCfg *busCfg, uint8_t *writeData, uint16_t len)</p>
139</td>
140<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p6872112112201"><a name="p6872112112201"></a><a name="p6872112112201"></a>Writes sensor configuration data to the sensor register based on the bus configuration.</p>
141</td>
142</tr>
143<tr id="row1667512682014"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p146751426192014"><a name="p146751426192014"></a><a name="p146751426192014"></a>int32_t <strong id="b4725135865517"><a name="b4725135865517"></a><a name="b4725135865517"></a>CreateSensorThread</strong>(struct OsalThread *thread, OsalThreadEntry threadEntry, char *name, void *entryPara)</p>
144</td>
145<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p10676112612013"><a name="p10676112612013"></a><a name="p10676112612013"></a>Creates a scheduled thread for a specified sensor to process sensor data reporting.</p>
146</td>
147</tr>
148<tr id="row1594017462410"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p49401442414"><a name="p49401442414"></a><a name="p49401442414"></a>void <strong id="b17586316565"><a name="b17586316565"></a><a name="b17586316565"></a>DestroySensorThread</strong>(struct OsalThread *thread, uint8_t *status);</p>
149</td>
150<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p69406415243"><a name="p69406415243"></a><a name="p69406415243"></a>Destroys the scheduled thread created for the sensor.</p>
151</td>
152</tr>
153<tr id="row19401042245"><td class="cellrowborder" valign="top" width="8.550855085508552%" headers="mcps1.2.4.1.1 "><p id="p694020492417"><a name="p694020492417"></a><a name="p694020492417"></a>Common configuration</p>
154</td>
155<td class="cellrowborder" valign="top" width="45.53455345534553%" headers="mcps1.2.4.1.2 "><p id="p1794064142418"><a name="p1794064142418"></a><a name="p1794064142418"></a>int32_t <strong id="b1740939195611"><a name="b1740939195611"></a><a name="b1740939195611"></a>SetSensorRegCfgArray</strong>(struct SensorBusCfg *busCfg, const struct SensorRegCfgGroupNode *group);</p>
156</td>
157<td class="cellrowborder" valign="top" width="45.91459145914592%" headers="mcps1.2.4.1.3 "><p id="p49409417249"><a name="p49409417249"></a><a name="p49409417249"></a>Sets the sensor register group configuration based on the sensor bus type.</p>
158</td>
159</tr>
160<tr id="row1494015418246"><td class="cellrowborder" rowspan="5" valign="top" width="8.550855085508552%" headers="mcps1.2.4.1.1 "><p id="p185291624202618"><a name="p185291624202618"></a><a name="p185291624202618"></a>Configuration parsing</p>
161<p id="p978482418524"><a name="p978482418524"></a><a name="p978482418524"></a></p>
162</td>
163<td class="cellrowborder" valign="top" width="45.53455345534553%" headers="mcps1.2.4.1.2 "><p id="p994194132410"><a name="p994194132410"></a><a name="p994194132410"></a>int32_t <strong id="b109631149171514"><a name="b109631149171514"></a><a name="b109631149171514"></a>GetSensorBaseConfigData</strong>(const struct DeviceResourceNode *node, struct SensorCfgData *config)</p>
164</td>
165<td class="cellrowborder" valign="top" width="45.91459145914592%" headers="mcps1.2.4.1.3 "><p id="p79411640248"><a name="p79411640248"></a><a name="p79411640248"></a>Obtains basic configuration information such as sensor, bus, and attribute configurations based on the HCS resource configuration of the sensor device, and initializes the basic configuration data structure.</p>
166</td>
167</tr>
168<tr id="row1171817565518"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p7718165615113"><a name="p7718165615113"></a><a name="p7718165615113"></a>int32_t <strong id="b14392155271515"><a name="b14392155271515"></a><a name="b14392155271515"></a>ParseSensorRegConfig</strong>(struct SensorCfgData *config)</p>
169</td>
170<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p171885685120"><a name="p171885685120"></a><a name="p171885685120"></a>Parses the register group information based on the HCS resource configuration of the sensor device and initializes the configuration data structure.</p>
171</td>
172</tr>
173<tr id="row394144192414"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p99411144241"><a name="p99411144241"></a><a name="p99411144241"></a>void <strong id="b1322165619152"><a name="b1322165619152"></a><a name="b1322165619152"></a>ReleaseSensorAllRegConfig</strong>(struct SensorCfgData *config)</p>
174</td>
175<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p17941154152419"><a name="p17941154152419"></a><a name="p17941154152419"></a>Releases the resources allocated to the sensor configuration data structure.</p>
176</td>
177</tr>
178<tr id="row10589154102611"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p4557141217521"><a name="p4557141217521"></a><a name="p4557141217521"></a>int32_t <strong id="b1206195914157"><a name="b1206195914157"></a><a name="b1206195914157"></a>GetSensorBusHandle</strong>(struct SensorBusCfg *busCfg)</p>
179</td>
180<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p953821245219"><a name="p953821245219"></a><a name="p953821245219"></a>Obtains the sensor bus handle information.</p>
181</td>
182</tr>
183<tr id="row6784142455212"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p478492410522"><a name="p478492410522"></a><a name="p478492410522"></a>int32_t <strong id="b9443344101610"><a name="b9443344101610"></a><a name="b9443344101610"></a>ReleaseSensorBusHandle</strong>(struct SensorBusCfg *busCfg)</p>
184</td>
185<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1878422485212"><a name="p1878422485212"></a><a name="p1878422485212"></a>Releases the sensor bus handle information.</p>
186</td>
187</tr>
188</tbody>
189</table>
190
191The following table lists the APIs that need to be implemented by driver developers.
192
193**Table  3**  APIs that need to be implemented by driver developers
194
195<a name="table1083014911336"></a>
196<table><thead align="left"><tr id="row208301997332"><th class="cellrowborder" valign="top" width="8.41084108410841%" id="mcps1.2.4.1.1"><p id="p1777364318152"><a name="p1777364318152"></a><a name="p1777364318152"></a>Category</p>
197</th>
198<th class="cellrowborder" valign="top" width="45.77457745774577%" id="mcps1.2.4.1.2"><p id="p5773174317157"><a name="p5773174317157"></a><a name="p5773174317157"></a>API</p>
199</th>
200<th class="cellrowborder" valign="top" width="45.81458145814582%" id="mcps1.2.4.1.3"><p id="p1777319437155"><a name="p1777319437155"></a><a name="p1777319437155"></a>Description</p>
201</th>
202</tr>
203</thead>
204<tbody><tr id="row1880425111572"><td class="cellrowborder" rowspan="8" valign="top" width="8.41084108410841%" headers="mcps1.2.4.1.1 "><p id="p598171454520"><a name="p598171454520"></a><a name="p598171454520"></a>Basic functions</p>
205</td>
206<td class="cellrowborder" valign="top" width="45.77457745774577%" headers="mcps1.2.4.1.2 "><p id="p880485195711"><a name="p880485195711"></a><a name="p880485195711"></a>int32_t <strong id="b16497123107"><a name="b16497123107"></a><a name="b16497123107"></a>init</strong>(void)</p>
207</td>
208<td class="cellrowborder" valign="top" width="45.81458145814582%" headers="mcps1.2.4.1.3 "><p id="p1480465165710"><a name="p1480465165710"></a><a name="p1480465165710"></a>Initializes the configuration of a sensor device after it is detected.</p>
209</td>
210</tr>
211<tr id="row10831296333"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1783115910333"><a name="p1783115910333"></a><a name="p1783115910333"></a>int32_t <strong id="b146831650010"><a name="b146831650010"></a><a name="b146831650010"></a>GetInfo</strong>(struct SensorBasicInfo *info)</p>
212</td>
213<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p4831139153316"><a name="p4831139153316"></a><a name="p4831139153316"></a>Obtains the basic information about the current sensor device from the HCS of sensor devices.</p>
214</td>
215</tr>
216<tr id="row178311493339"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p208318963320"><a name="p208318963320"></a><a name="p208318963320"></a>int32_t <strong id="b189921571402"><a name="b189921571402"></a><a name="b189921571402"></a>Enable</strong>(void)</p>
217</td>
218<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p68310953312"><a name="p68310953312"></a><a name="p68310953312"></a>Enables the current sensor device by delivering the register configuration in the operation group based on the HCS of the current sensor device.</p>
219</td>
220</tr>
221<tr id="row0831129153318"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p0831169183314"><a name="p0831169183314"></a><a name="p0831169183314"></a>int32_t <strong id="b13541291106"><a name="b13541291106"></a><a name="b13541291106"></a>Disable</strong>(void)</p>
222</td>
223<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p138314912336"><a name="p138314912336"></a><a name="p138314912336"></a>Disables the current sensor device by delivering the register configuration in the operation group based on the HCS of the current sensor device.</p>
224</td>
225</tr>
226<tr id="row178311093334"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p583118920338"><a name="p583118920338"></a><a name="p583118920338"></a>int32_t <strong id="b111117118017"><a name="b111117118017"></a><a name="b111117118017"></a>SetBatch</strong>(int64_t samplingInterval, int64_t reportInterval)</p>
227</td>
228<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p08311903315"><a name="p08311903315"></a><a name="p08311903315"></a>Sets the processing time of the data reporting thread for the current sensor device based on the data sampling interval and data reporting interval.</p>
229</td>
230</tr>
231<tr id="row1356419421422"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p9565174218421"><a name="p9565174218421"></a><a name="p9565174218421"></a>int32_t <strong id="b13702551143412"><a name="b13702551143412"></a><a name="b13702551143412"></a>SetMode</strong>(int32_t mode)</p>
232</td>
233<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p356524224213"><a name="p356524224213"></a><a name="p356524224213"></a>Sets the data reporting mode of the current sensor device.</p>
234</td>
235</tr>
236<tr id="row12565104264215"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p17565104210429"><a name="p17565104210429"></a><a name="p17565104210429"></a>int32_t <strong id="b96811512011"><a name="b96811512011"></a><a name="b96811512011"></a>SetOption</strong>(uint32_t option)</p>
237</td>
238<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p4565104214213"><a name="p4565104214213"></a><a name="p4565104214213"></a>Sets the register configuration such as the measurement range and accuracy based on sensor options.</p>
239</td>
240</tr>
241<tr id="row380240111218"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p1181184016120"><a name="p1181184016120"></a><a name="p1181184016120"></a>void <strong id="b1613451717016"><a name="b1613451717016"></a><a name="b1613451717016"></a>ReadSensorData</strong>(void)</p>
242</td>
243<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p7815401121"><a name="p7815401121"></a><a name="p7815401121"></a>Reads sensor data.</p>
244</td>
245</tr>
246</tbody>
247</table>
248
249For details about the API implementation, see  [Development Example](#section257750691).
250
251## Development Guidelines<a name="section1140943382"></a>
252
253Regardless of the OS and system on a chip \(SoC\), the sensor driver is developed based on the HDF, platform, and OSAL APIs to provide a unified driver model for sensor devices. This section uses the acceleration sensor as an example to describe how to develop a sensor driver.
254
255### How to Develop<a name="section7893102915819"></a>
256
2571.  Register the acceleration sensor driver. The HDF provides a unified driver management model. The HDF identifies and loads the target module driver based on the configuration of the acceleration sensor module.
2582.  Initialize and deinitialize the acceleration sensor driver. Using the  **init**  function, the HDF starts loading the sensor device driver and allocating configuration resources for sensor device data, respectively. Using the  **release**  function, the HDF releases the resources and configurations loaded by the driver.
2593.  Parse the configurations of the acceleration sensor register group. For different types of sensors, you must configure their respective HCS configuration files in the HCS, check whether the sensor device is in position during the device driver startup, and then load the corresponding configuration file to generate the configuration structure object.
2604.  Implement APIs for acceleration sensor driver operations. The driver APIs for various types of sensors, such as  **init**,  **GetInfo**,  **Enable**,  **Disable**,  **SetBatch**,  **SetMode**,  **SetOption**, and  **ReadSensorData**, are normalized to deliver sensor driver configurations and report sensor data.
261
262>![](../public_sys-resources/icon-note.gif) **NOTE:**
263>The sensor driver model provides a collection of APIs to implement sensor driver capabilities, including the driver device management, abstract bus and platform operation, general configuration, and configuration parsing capabilities. For details about the APIs, see  [Table 2](#table1156812588320). You need to implement the following APIs: some operations to perform on sensors \([Table 3](#table1083014911336)\), differentiated data configuration of the sensor HCS, and verification of basic driver functions.
264
265## Development Example<a name="section257750691"></a>
266
267This section uses a code example to demonstrate how to load and start the acceleration sensor driver based on the HDF driver model. For details about the mechanism, see  [Driver Development](driver-hdf-development.md). This example uses the Bosch BMI160 acceleration sensor that communicates over I2C.
268
2691.  Register the driver entry of the acceleration sensor.
270
271-   Implementation of the entry function
272
273```
274/* Register the entry structure object of the acceleration sensor. */
275struct HdfDriverEntry g_sensorAccelDevEntry = {
276    .moduleVersion = 1, /* Version of the acceleration sensor module */
277    .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. */
278    .Bind = BindAccelDriver, /* Binding function of the acceleration sensor */
279    .Init = InitAccelDriver, /* Initialization function of the acceleration sensor */
280    .Release = ReleaseAccelDriver, /* Resource release function of the acceleration sensor */
281};
282
283/* Call HDF_INIT to register the driver entry with the HDF. When loading the driver, the HDF calls the Bind function first and then the Init function. If the Init function fails to be called, the HDF will call Release to release the driver resource and exit.
284HDF_INIT(g_sensorAccelDevEntry);
285```
286
287-   Acceleration sensor configuration
288
289The 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).
290
291```
292/* HCS configuration of the acceleration sensor device */
293device_sensor_accel :: device {
294    device0 :: deviceNode {
295        policy = 1; /* Policy for providing the driver service */
296        priority = 105; /* Driver startup priority (0–200). A larger value indicates a lower priority. The default value 100 is recommended. The sequence for loading devices with the same priority is random. */
297        preload = 2; /* Field for specifying whether to load the driver. The value 0 means to load the driver, and 2 means the opposite. */
298        permission = 0664; /* Permission for the driver to create device nodes */
299        moduleName = "HDF_SENSOR_ACCEL"; /* Driver name. The value must be the same as that of moduleName in the driver entry structure. */
300        serviceName = "sensor_accel"; /* Name of the service provided by the driver. The name must be unique. */
301        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. */
302    }
303}
304```
305
3061.  Initialize and deinitialize the acceleration sensor driver.
307
308-   Initialization entry function  **init**
309
310```
311/* Bind the service provided by the acceleration sensor driver to the HDF. */
312int32_t BindAccelDriver(struct HdfDeviceObject *device)
313{
314    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
315
316    static struct IDeviceIoService service = {
317        .object = {0},
318        .Dispatch = DispatchAccel,
319    };
320    device->service = &service;
321
322    return HDF_SUCCESS;
323}
324/* After detecting that the device is in position, call RegisterAccelChipOps to register the differentiation adaptation function. */
325int32_t RegisterAccelChipOps(struct AccelOpsCall *ops)
326{
327    struct AccelDrvData *drvData = NULL;
328
329    CHECK_NULL_PTR_RETURN_VALUE(ops, HDF_ERR_INVALID_PARAM);
330
331    drvData = AccelGetDrvData();
332    drvData->ops.Init = ops->Init;
333    drvData->ops.ReadData = ops->ReadData;
334    return HDF_SUCCESS;
335}
336/* Hook the acceleration sensor driver normalization function. */
337static int32_t InitAccelOps(struct SensorDeviceInfo *deviceInfo)
338{
339    struct AccelDrvData *drvData = AccelGetDrvData();
340
341    (void)memset_s((void *)deviceInfo, sizeof(*deviceInfo), 0, sizeof(*deviceInfo));
342    deviceInfo->ops.GetInfo = SetAccelInfo;
343    deviceInfo->ops.Enable = SetAccelEnable;
344    deviceInfo->ops.Disable = SetAccelDisable;
345    deviceInfo->ops.SetBatch = SetAccelBatch;
346    deviceInfo->ops.SetMode = SetAccelMode;
347    deviceInfo->ops.SetOption = SetAccelOption;
348
349    if (memcpy_s(&deviceInfo->sensorInfo, sizeof(deviceInfo->sensorInfo),
350        &drvData->accelCfg->sensorInfo, sizeof(drvData->accelCfg->sensorInfo)) != EOK) {
351        HDF_LOGE("%s: copy sensor info failed", __func__);
352        return HDF_FAILURE;
353    }
354    /* The sensor type ID can be configured in the HCS configuration file or here. */
355    drvData->accelCfg->sensorInfo.sensorTypeId = SENSOR_TAG_ACCELEROMETER;
356    drvData->accelCfg->sensorInfo.sensorId = SENSOR_TAG_ACCELEROMETER;
357
358    return HDF_SUCCESS;
359}
360/* Initialize the sensor register. */
361static int32_t InitAccelAfterConfig(void)
362{
363    struct SensorDeviceInfo deviceInfo;
364
365    if (InitAccelConfig() != HDF_SUCCESS) {
366        HDF_LOGE("%s: init accel config failed", __func__);
367        return HDF_FAILURE;
368    }
369
370    if (InitAccelOps(&deviceInfo) != HDF_SUCCESS) {
371        HDF_LOGE("%s: init accel ops failed", __func__);
372        return HDF_FAILURE;
373    }
374
375    if (AddSensorDevice(&deviceInfo) != HDF_SUCCESS) {
376        HDF_LOGE("%s: add accel device failed", __func__);
377        return HDF_FAILURE;
378    }
379
380    return HDF_SUCCESS;
381}
382/* Call the device detection function to hook the differentiated device function. */
383static int32_t DetectAccelChip(void)
384{
385    int32_t num;
386    int32_t ret;
387    int32_t loop;
388    struct AccelDrvData *drvData = AccelGetDrvData();
389    CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
390
391    num = sizeof(g_accelDetectIfList) / sizeof(g_accelDetectIfList[0]);
392    for (loop = 0; loop < num; ++loop) {
393        if (g_accelDetectIfList[loop].DetectChip != NULL) {
394            ret = g_accelDetectIfList[loop].DetectChip(drvData->accelCfg);
395            if (ret == HDF_SUCCESS) {
396                drvData->detectFlag = true;
397                break;
398            }
399        }
400    }
401
402    if (loop == num) {
403        HDF_LOGE("%s: detect accel device failed", __func__);
404        drvData->detectFlag = false;
405        return HDF_FAILURE;
406    }
407    return HDF_SUCCESS;
408}
409/* The entry function of the acceleration sensor driver is used to initialize the structure object of the sensor private data, allocate space for the HCS data configuration object of the sensor, invoke the entry function for initializing the sensor HCS data configuration, detect whether the sensor device is in position, create the sensor data reporting timer, implement the sensor normalization API, and register the sensor device. */
410int32_t InitAccelDriver(struct HdfDeviceObject *device)
411{
412    /* Obtain the private data structure object of the sensor. */
413    struct AccelDrvData *drvData = AccelGetDrvData();
414
415   /* When detecting sensors of the same type from different vendors, the function checks whether this type of sensors is in position. If yes, it no longer detects the other sensors of this type and directly returns the result. */
416    if (drvData->detectFlag) {
417        HDF_LOGE("%s: accel sensor have detected", __func__);
418        return HDF_SUCCESS;
419    }
420
421    CHECK_NULL_PTR_RETURN_VALUE(device, HDF_ERR_INVALID_PARAM);
422    /* Allocate space for the private data structure objects for storing sensor data configurations. The allocated space needs to be released when the driver is released. */
423        drvData->accelCfg = (struct SensorCfgData *)OsalMemCalloc(sizeof(*cfg));
424        if (drvData->accelCfg == NULL) {
425            HDF_LOGE("%s: malloc sensor config data failed", __func__);
426            return HDF_FAILURE;
427        }
428
429    drvData->accelCfg->regCfgGroup = &g_regCfgGroup[0];
430    /* Initializing the sensor configuration data aims to parse the configuration information of the sensor communication bus, basic sensor information, sensor attributes, whether the sensor is in position, and register group information. */
431    if (GetSensorBaseConfigData(device->property, drvData->accelCfg) != HDF_SUCCESS) {
432        HDF_LOGE("%s: get sensor base config failed", __func__);
433        goto Base_CONFIG_EXIT;
434    }
435
436    if (DetectAccelChip() != HDF_SUCCESS) {
437        HDF_LOGE("%s: accel sensor detect device no exist", __func__);
438        goto DETECT_CHIP_EXIT;
439    }
440    drvData->detectFlag = true;
441    if (ParseSensorRegConfig(drvData->accelCfg) != HDF_SUCCESS) {
442        HDF_LOGE("%s: detect sensor device failed", __func__);
443        goto REG_CONFIG_EXIT;
444    }
445
446    if (InitAccelAfterConfig() != HDF_SUCCESS) {
447        HDF_LOGE("%s: init accel after config failed", __func__);
448        goto INIT_EXIT;
449    }
450
451    HDF_LOGI("%s: init accel driver success", __func__);
452    return HDF_SUCCESS;
453
454INIT_EXIT:
455    DestroySensorThread(&drvData->thread, &drvData->threadStatus);
456    (void)DeleteSensorDevice(SENSOR_TAG_ACCELEROMETER);
457REG_CONFIG_EXIT:
458    ReleaseSensorAllRegConfig(drvData->accelCfg);
459    (void)ReleaseSensorBusHandle(&drvData->accelCfg->busCfg);
460DETECT_CHIP_EXIT:
461    drvData->detectFlag = false;
462BASE_CONFIG_EXIT:
463    drvData->accelCfg->root = NULL;
464    drvData->accelCfg->regCfgGroup = NULL;
465    OsalMemFree(drvData->accelCfg);
466    drvData->accelCfg = NULL;
467    return HDF_FAILURE;
468}
469
470/* Release the resources allocated during driver initialization. */
471void ReleaseAccelDriver(struct HdfDeviceObject *device)
472{
473    (void)device;
474    struct AccelDrvData *drvData = NULL;
475
476    drvData = AccelGetDrvData();
477    (void)DestroySensorThread(&drvData->thread, &drvData->threadStatus);
478    (void)DeleteSensorDevice(SENSOR_TAG_ACCELEROMETER);
479    drvData->detectFlag = false;
480
481    if (drvData->accelCfg != NULL) {
482        drvData->accelCfg->root = NULL;
483        drvData->accelCfg->regCfgGroup = NULL;
484        ReleaseSensorAllRegConfig(drvData->accelCfg);
485        (void)ReleaseSensorBusHandle(&drvData->accelCfg->busCfg);
486        OsalMemFree(drvData->accelCfg);
487        drvData->accelCfg = NULL;
488    }
489
490    drvData->initStatus = false;
491}
492```
493
4941.  Configure the acceleration sensor register group.
495
496You only need to configure the acceleration sensor data according to the template. Template configuration parsing has been implemented via the  **InitSensorConfigData**  function and only needs to be called during initialization. If new configuration items are added, you need to modify this function accordingly.
497
498```
499Acceleration sensor data configuration template (accel_config.hcs)
500root {
501    sensorAccelConfig {
502        accelChipConfig {
503            /* Sensor information template */
504            template sensorInfo {
505                sensorName = "accelerometer"; /* Acceleration sensor name. The value contains a maximum of 16 bytes. */
506                vendorName = "borsh_bmi160"; /* Sensor vendor name. The value contains a maximum of 16 bytes. */
507                firmwareVersion = "1.0"; /* Sensor firmware version. The default value is 1.0. The value contains a maximum of 16 bytes. */
508                hardwareVersion = "1.0"; /* Sensor hardware version. The default value is 1.0. The value contains a maximum of 16 bytes. */
509                sensorTypeId = 1; /* Sensor type ID. For details, see SensorTypeTag. */
510                sensorId = 1; /* Sensor ID, which is defined by the sensor driver developer. The SensorTypeTag enums are recommended. */
511                maxRange = 8; /* Maximum measurement range of the sensor. Set this parameter based on your business requirements. */
512                precision = 0; /* Sensor accuracy, which is used together with sensor data reporting. For details, see SensorEvents. */
513                power = 230; /* Power consumption of the sensor */
514            }
515            /* Template of the bus type and configuration information used by the sensor */
516            template sensorBusConfig {
517                busType = 0; /* 0:i2c 1:spi */
518                busNum = 6; /* Device ID allocated to the sensor on the chip */
519                busAddr = 0; /* Address allocated to the sensor on the chip */
520                regWidth = 1; /* Width of the sensor register address */
521                regBigEndian = 0; /* Endian mode of the sensor register */
522            }
523            /* Sensor attribute template */
524            template sensorAttr {
525                chipName = ""; /* Sensor chip name */
526                chipIdRegister = 0xf; /* Address of the register detecting whether the sensor is in position */
527                chipIdValue = 0xd1; /* Value of the register detecting whether the sensor is in position */
528            }
529        }
530    }
531}
532
533/* You can modify the template configuration based on the differences of sensor devices. If no modification is made, the default template configuration is used. */
534root {
535    sensorAccelConfig {
536        accel_bmi160_chip_config : accelChipConfig {
537            match_attr = "hdf_sensor_accel_driver"; /* The value must be the same as the match_attr field configured for the acceleration sensor. */
538            accelInfo :: sensorInfo {
539                vendorName = "borsh_bmi160";
540                sensorTypeId = 1;
541                sensorId = 1;
542            }
543            accelBusConfig :: sensorBusConfig {
544                busType = 0; /* I2C communication mode */
545                busNum = 6;
546                busAddr = 0x68;
547                regWidth = 1; /* 1-byte bit width */
548            }
549            accelAttr :: sensorAttr {
550                chipName = "bmi160";
551                chipIdRegister = 0x00;
552                chipIdValue = 0xd1;
553            }
554            accelRegConfig {
555                /*  regAddr: Register address
556                    value: Register value
557                    mask: Mask of the register value
558                    len: Length (in bytes) of the register value
559                    delay: Register delay (in milliseconds)
560                    opsType: Operation type. The options can be 0 (no operation), 1 (read), 2 (write), 3 (read and check), and 4 (bit update).
561                    calType: Calculation type. The options can be 0 (none), 1 (write), 2 (negate), 3 (XOR) 4, (left shift), and 5 (right shift).
562                    shiftNum: Number of shifts
563                    debug: Debugging switch. The value can be 0 (disabled) or 1 (enabled).
564                    save: Data saving switch. The value can be 0 (not save data) or 1 (save data).
565                */
566                /* Groups of sensor register operations. Registers can be configured in sequence based on the groups. */
567                /* Register address, register value, mask of the register value, data length of the register value, register delay, operation type, calculation type, number of shifts, debugging switch, data saving switch */
568                /* Initialize the register groups. */
569                initSeqConfig = [
570                    0x7e,    0xb6, 0xff,   1,     5,       2,       0,        0,     0,    0,
571                    0x7e,    0x10, 0xff,   1,     5,       2,       0,        0,     0,    0
572                ];
573                /* Enable the register groups. */
574                enableSeqConfig = [
575                    0x7e,    0x11, 0xff,   1,     5,       2,       0,        0,     0,    0,
576                    0x41,    0x03, 0xff,   1,     0,       2,       0,        0,     0,    0,
577                    0x40,    0x08, 0xff,   1,     0,       2,       0,        0,     0,    0
578                ];
579                /* Disable the register groups. */
580                disableSeqConfig = [
581                    0x7e,    0x10, 0xff,   1,     5,       2,       0,        0,     0,    0
582                ];
583            }
584        }
585    }
586}
587```
588
5891.  Implement APIs for acceleration sensor driver operations.
590
591You need to implement normalized APIs based on sensor types.
592
593```
594/* Leave a function empty if it is not used. */
595static int32_t SetAccelInfo(struct SensorBasicInfo *info)
596{
597    (void)info;
598
599    return HDF_ERR_NOT_SUPPORT;
600}
601/* Deliver the configuration of enabling the register groups. */
602static int32_t SetAccelEnable(void)
603{
604    int32_t ret;
605    struct AccelDrvData *drvData = AccelGetDrvData();
606
607    CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
608    ret = SetSensorRegCfgArray(&drvData->accelCfg->busCfg, drvData->accelCfg->regCfgGroup[SENSOR_ENABLE_GROUP]);
609    if (ret != HDF_SUCCESS) {
610        HDF_LOGE("%s: accel sensor disable config failed", __func__);
611        return HDF_FAILURE;
612    }
613
614    drvData->threadStatus = SENSOR_THREAD_RUNNING;
615
616    return HDF_SUCCESS;
617}
618/* Deliver the configuration of disabling the register groups. */
619static int32_t SetAccelDisable(void)
620{
621    int32_t ret;
622    struct AccelDrvData *drvData = AccelGetDrvData();
623
624    CHECK_NULL_PTR_RETURN_VALUE(drvData->accelCfg, HDF_ERR_INVALID_PARAM);
625
626    ret = SetSensorRegCfgArray(&drvData->accelCfg->busCfg, drvData->accelCfg->regCfgGroup[SENSOR_DISABLE_GROUP]);
627    if (ret != HDF_SUCCESS) {
628        HDF_LOGE("%s: accel sensor disable config failed", __func__);
629        return HDF_FAILURE;
630        }
631
632    drvData->threadStatus = SENSOR_THREAD_STOPPED;
633
634    return HDF_SUCCESS;
635}
636/* Set the sampling interval and data reporting interval of the sensor. */
637static int32_t SetAccelBatch(int64_t samplingInterval, int64_t interval)
638{
639    (void)interval;
640
641    struct AccelDrvData *drvData = AccelGetDrvData();
642    drvData->interval = samplingInterval;
643
644    return HDF_SUCCESS;
645}
646/* Set the data reporting mode of the sensor. Currently, the real-time mode is supported. */
647static int32_t SetAccelMode(int32_t mode)
648{
649    return (mode == SENSOR_WORK_MODE_REALTIME) ? HDF_SUCCESS : HDF_FAILURE;
650}
651/* Set the sensor options. */
652static int32_t SetAccelOption(uint32_t option)
653{
654    (void)option;
655    return HDF_ERR_NOT_SUPPORT;
656}
657```
658
659-   Differentiated processing APIs
660
661    ```
662    /* If a device is detected, register the differentiated processing function to the accel driver model. */
663    int32_t DetectAccelBim160Chip(struct SensorCfgData *data)
664    {
665        int32_t ret;
666        struct AccelOpsCall ops;
667        CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
668
669        if (strcmp(ACCEL_CHIP_NAME_BMI160, data->sensorAttr.chipName) != 0) {
670            return HDF_SUCCESS;
671        }
672        ret = InitAccelPreConfig();
673        if (ret != HDF_SUCCESS) {
674            HDF_LOGE("%s: init  BMI160 bus mux config", __func__);
675            return HDF_FAILURE;
676        }
677        if (DetectSensorDevice(data) != HDF_SUCCESS) {
678            return HDF_FAILURE;
679        }
680
681       /* Differentiated processing function */
682        ops.Init = InitBmi160;
683        ops.ReadData = ReadBmi160Data;
684        ret = RegisterAccelChipOps(&ops);
685        if (ret != HDF_SUCCESS) {
686            HDF_LOGE("%s: register BMI160 accel failed", __func__);
687            (void)ReleaseSensorBusHandle(&data->busCfg);
688            return HDF_FAILURE;
689        }
690        return HDF_SUCCESS;
691    }
692    /* Initialization processing function */
693    static int32_t InitBmi160(struct SensorCfgData *data)
694    {
695        int32_t ret;
696
697        CHECK_NULL_PTR_RETURN_VALUE(data, HDF_ERR_INVALID_PARAM);
698        ret = SetSensorRegCfgArray(&data->busCfg, data->regCfgGroup[SENSOR_INIT_GROUP]);
699        if (ret != HDF_SUCCESS) {
700            HDF_LOGE("%s: bmi160 sensor init config failed", __func__);
701            return HDF_FAILURE;
702        }
703        return HDF_SUCCESS;
704    }
705    /* Data processing function */
706    int32_t ReadBmi160Data(struct SensorCfgData *data)
707    {
708        int32_t ret;
709        struct AccelData  rawData = { 0, 0, 0 };
710        int32_t tmp[ACCEL_AXIS_NUM];
711        struct SensorReportEvent event;
712
713        (void)memset_s(&event, sizeof(event), 0, sizeof(event));
714
715        ret = ReadBmi160RawData(data, &rawData, &event.timestamp);
716        if (ret != HDF_SUCCESS) {
717            return HDF_FAILURE;
718        }
719
720        event.sensorId = SENSOR_TAG_ACCELEROMETER;
721        event.option = 0;
722        event.mode = SENSOR_WORK_MODE_REALTIME;
723
724        rawData.x = rawData.x * BMI160_ACC_SENSITIVITY_2G;
725        rawData.y = rawData.y * BMI160_ACC_SENSITIVITY_2G;
726        rawData.z = rawData.z * BMI160_ACC_SENSITIVITY_2G;
727
728        tmp[ACCEL_X_AXIS] = (rawData.x * SENSOR_1K_UNIT) / SENSOR_CONVERT_UNIT;
729        tmp[ACCEL_Y_AXIS] = (rawData.y * SENSOR_1K_UNIT) / SENSOR_CONVERT_UNIT;
730        tmp[ACCEL_Z_AXIS] = (rawData.z * SENSOR_1K_UNIT) / SENSOR_CONVERT_UNIT;
731
732        event.dataLen = sizeof(tmp);
733        event.data = (uint8_t *)&tmp;
734        ret = ReportSensorEvent(&event);
735        return ret;
736    }
737    ```
738
739-   Data processing function
740
741Create a sensor timer to periodically sample data based on the configured sampling interval and report the data to the data subscriber.
742
743```
744/* Scheduled working thread of the sensor */
745static int32_t ReadAccelDataThreadWorker(void *arg)
746{
747    (void)arg;
748    int64_t interval;
749    struct AccelDrvData *drvData = AccelGetDrvData();
750
751    drvData->threadStatus = SENSOR_THREAD_START;
752    while (true) {
753        if (drvData->threadStatus == SENSOR_THREAD_RUNNING) {
754            if (drvData->ops.ReadData != NULL) {
755                (void)drvData->ops.ReadData(drvData->accelCfg);
756            }
757            interval = OsalDivS64(drvData->interval, (SENSOR_CONVERT_UNIT * SENSOR_CONVERT_UNIT));
758            OsalMSleep(interval);
759        } else if (drvData->threadStatus == SENSOR_THREAD_DESTROY) {
760            break;
761        } else {
762            OsalMSleep(ACC_DEFAULT_SAMPLING_200_MS / SENSOR_CONVERT_UNIT / SENSOR_CONVERT_UNIT);
763        }
764
765        if ((!drvData->initStatus) || (drvData->interval < 0) || drvData->threadStatus != SENSOR_THREAD_RUNNING) {
766            continue;
767        }
768    }
769
770    return HDF_SUCCESS;
771}
772/* Create a sensor timer and initialize the sensor device. */
773static int32_t InitAccelConfig(void)
774{
775    int32_t ret;
776    struct AccelDrvData *drvData = AccelGetDrvData();
777
778    if (drvData->threadStatus != SENSOR_THREAD_NONE && drvData->threadStatus != SENSOR_THREAD_DESTROY) {
779        HDF_LOGE("%s: accel thread have created", __func__);
780        return HDF_SUCCESS;
781    }
782
783    ret = CreateSensorThread(&drvData->thread, ReadAccelDataThreadWorker, "hdf_sensor_accel", drvData);
784    if (ret != HDF_SUCCESS) {
785        HDF_LOGE("%s: accel create thread failed", __func__);
786        drvData->threadStatus = SENSOR_THREAD_NONE;
787        return HDF_FAILURE;
788    }
789
790    CHECK_NULL_PTR_RETURN_VALUE(drvData->ops.Init, HDF_ERR_INVALID_PARAM);
791
792    ret = drvData->ops.Init(drvData->accelCfg);
793    if (ret != HDF_SUCCESS) {
794        HDF_LOGE("%s: accel create thread failed", __func__);
795        drvData->threadStatus = SENSOR_THREAD_NONE;
796        return HDF_FAILURE;
797    }
798    drvData->initStatus = true;
799    return HDF_SUCCESS;
800}
801```
802
803-   Major data structures
804
805```
806/* Sensor conversion units */
807#define SENSOR_CONVERT_UNIT             1000
808#define SENSOR_1K_UNIT                  1024
809/* Sensitivity conversion value of the sensor with a 2g measurement range */
810#define BMI160_ACC_SENSITIVITY_2G       61
811/* Address of the sensor data sampling register */
812#define BMI160_ACCEL_X_LSB_ADDR              0X12
813#define BMI160_ACCEL_X_MSB_ADDR              0X13
814#define BMI160_ACCEL_Y_LSB_ADDR              0X14
815#define BMI160_ACCEL_Y_MSB_ADDR              0X15
816#define BMI160_ACCEL_Z_LSB_ADDR              0X16
817#define BMI160_ACCEL_Z_MSB_ADDR              0X17
818/* Data dimension of the sensor */
819enum AccelAxisNum {
820    ACCEL_X_AXIS   = 0,
821    ACCEL_Y_AXIS   = 1,
822    ACCEL_Z_AXIS   = 2,
823    ACCEL_AXIS_NUM = 3,
824};
825/* Each dimension of the sensor */
826struct AccelData {
827    int32_t x;
828    int32_t y;
829    int32_t z;
830};
831/* Private data structure of the sensor */
832struct AccelDrvData {
833    bool detectFlag;
834    uint8_t threadStatus;
835    uint8_t initStatus;
836    int64_t interval;
837    struct SensorCfgData *accelCfg;
838    struct OsalThread thread;
839    struct AccelOpsCall ops;
840};
841/* Differentiation adaptation function */
842struct AccelOpsCall {
843    int32_t (*Init)(struct SensorCfgData *data);
844    int32_t (*ReadData)(struct SensorCfgData *data);
845};
846```
847
848## Test Guidelines<a name="section106021256121219"></a>
849
850After the driver is developed, you can develop self-test cases in the sensor unit test to verify the basic functions of the driver. The developer self-test platform is used as the test environment.
851
852```
853/* Specify whether to report sensor data. */
854static int32_t g_sensorDataFlag = 0;
855/* Retain the address of the sensor interface instance. */
856static const struct SensorInterface *g_sensorDev = nullptr;
857
858/* Register the data reporting function. */
859static int SensorTestDataCallback(struct SensorEvents *event)
860{
861    if (event == nullptr) {
862        return -1;
863    }
864    float *data = (float*)event->data;
865    printf("time [%lld] sensor id [%d] x-[%f] y-[%f] z-[%f]\n\r", event->timestamp,
866        event->sensorId, (*data), *(data + 1), *(data + g_axisZ));
867    if (*data > 1e-5) {
868        g_sensorDataFlag = 1;
869    }
870    return 0;
871}
872/* Initialize the sensor interface instance before executing the test cases. */
873void HdfSensorTest::SetUpTestCase()
874{
875    g_sensorDev = NewSensorInterfaceInstance();
876    if (g_sensorDev == nullptr) {
877        printf("test sensorHdi get Module instace failed\n\r");
878    }
879}
880/* Release case resources. */
881void HdfSensorTest::TearDownTestCase()
882{
883    if (g_sensorDev != nullptr) {
884        FreeSensorInterfaceInstance();
885        g_sensorDev = nullptr;
886    }
887}
888/* Verify the sensor driver. */
889HWTEST_F(HdfSensorTest,TestAccelDriver_001, TestSize.Level0)
890{
891    int32_t sensorInterval = 1000000000; /* Data sampling interval, in nanoseconds */
892    int32_t pollTime = 5; /* Data sampling duration, in seconds */
893    int32_t accelSensorId = 1; /* Acceleration sensor type ID, which is 1 */
894    int32_t count = 0;
895    int ret;
896    struct SensorInformation *sensorInfo = nullptr;
897
898    ret = g_sensorDev->Register(SensorTestDataCallback)
899    EXPECT_EQ(SENSOR_NULL_PTR, ret);
900
901    ret = g_sensorDev->GetAllSensors(&sensorInfo, &count);
902    EXPECT_EQ(0, ret);
903    if (sensorInfo == nullptr) {
904        EXPECT_NE(nullptr, sensorInfo);
905        return;
906    }
907    /* Print the obtained sensor list. */
908    for (int i = 0; i < count; i++) {
909        printf("get sensoriId[%d], info name[%s]\n\r", sensorInfo[i]->sensorId, sensorInfo[i]->sensorName);
910    }
911    ret = g_sensorDev->Enable(accelSensorId);
912    EXPECT_EQ(0, ret);
913    g_sensorDataFlag = 0;
914
915    ret = g_sensorDev->SetBatch(accelSensorId, sensorInterval, pollTime);
916    EXPECT_EQ(0, ret);
917    /* Observe the printed data within the period specified by pollTime. */
918    OsalSleep(pollTime);
919    EXPECT_EQ(1, g_sensorDataFlag);
920
921    ret = g_sensorDev->Disable(accelSensorId);
922    g_sensorDataFlag = 0;
923    EXPECT_EQ(0, ret);
924
925    ret = g_sensorDev->Unregister();
926    EXPECT_EQ(0, ret);
927}
928```
929
930