# sensor - [Introduction](#section11660541593) - [Directory Structure](#section161941989596) - [Usage](#section45490312053) - [Available APIs](#section14840627279) - [Usage Guidelines](#section460510231888) - [Repositories Involved](#section12495154795416) ## Introduction The sensor driver module provides and implements sensor-related Hardware Driver Interfaces \(HDIs\), including obtaining sensor information, enabling or disabling a sensor, subscribing to or unsubscribing from sensor data, and setting sensor options. These APIs make service development easier. **Figure 1** Sensor driver module architecture ![](figures/sensor-driver-module-architecture.png "sensor-driver-module-architecture") ## Directory Structure The directory structure of the sensor driver module is as follows: ``` /drivers/peripheral/sensor ├── hal # HAL code │ └── include # HAL header files │ └── src # HAL code implementation ├── interfaces # Driver capability APIs provided for upper-layer services │ └── include # APIs exposed externally ├── test # Test code │ └── unittest # Unit test code ``` ## Usage This section uses the acceleration sensor as an example to describe how to use sensor APIs. ### Available APIs The HAL module of the sensor driver provides APIs that can be directly called by sensor services to obtain, set, and subscribe to or unsubscribe from sensor data. The following table lists the APIs provided by the sensor driver module. **Table 1** Major HDIs of the sensor module

Category

API

Description

Query

int32_t GetAllSensors(struct SensorInformation **sensorInfo, int32_t *count)

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.

Setting

int32_t Enable(int32_t sensorId)

Enables the sensor that has been subscribed to. The subscriber can obtain the sensor data only after the sensor is enabled.

int32_t Disable(int32_t sensorId)

Disables a sensor.

int32_t SetBatch(iint32_t sensorId, int64_t samplingInterval, int64_t reportInterval)

Sets the data sampling interval and data reporting interval for the specified sensor.

int32_t SetMode(int32_t sensorTypeId, SensorUser *user, int32_t mode)

Sets the data reporting mode for the specified sensor.

int32_t SetOption(int32_t sensorId, uint32_t option)

Sets options for the specified sensor, including its measurement range and accuracy.

Data subscription and unsubscription

int32_t Register(RecordDataCallback cb)

Registers the callback for reporting sensor data to the subscriber.

int32_t Unregister(void)

Unregisters the callback for reporting sensor data.

Instance creation

const struct SensorInterface *NewSensorInterfaceInstance(void)

Creates a SensorInterface instance.

int32_t FreeSensorInterfaceInstance(void)

Releases the SensorInterface instance.

### Usage Guidelines Sample code ``` #include "sensor_if.h" #include "sensor_type.h" /* Create a callback. */ void SensorDataCallback(struct SensorEvents *event) { if(event == NULL){ return; } float *sensorData=(float *)event->data; printf("sensor data[%f]", *sensorData); } void SensorSample(void) { int ret; struct SensorInformation *sensorInfo = NULL; int32_t count = 0; int32_t sensorInterval = 200000000; /* Set the data sampling rate to 200000000, in the unit of nanoseconds (200 ms). */ /* 1. Create a SensorInterface instance. */ const struct SensorInterface *sensorDev = NewSensorInterfaceInstance(); if (sensorDev == NULL) { return; } /* 2. Register a sensor data callback. */ ret = sensorDev->Register(0, SensorDataCallback); if (ret != 0) { return; } /* 3. Obtain the list of sensors supported by the device. */ ret = sensorDev->GetAllSensors(&sensorInfo, &count); if (ret != 0) { return; } /* 4. Set the sensor sampling rate. */ ret = sensorDev->SetBatch(SENSOR_TYPE_ACCELEROMETER, sensorInterval, 0); if (ret != 0) { return; } /* 5. Enable the sensor. */ ret = sensorDev->Enable(SENSOR_TYPE_ACCELEROMETER); if (ret != 0) { return; } /* 6. Disable the sensor. */ ret = sensorDev->Disable(SENSOR_TYPE_ACCELEROMETER); if (ret != 0) { return; } /* 7. Unregister the sensor data callback. */ ret = sensorDev->Unregister(0); if (ret != 0) { return; } /* 8. Release the SensorInterface instance. ret = FreeSensorInterfaceInstance(); if (ret != 0) { return; } } ``` ## Repositories Involved [Driver subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/driver.md) [drivers\_framework](https://gitee.com/openharmony/drivers_framework/blob/master/README.md) [drivers\_adapter](https://gitee.com/openharmony/drivers_adapter/blob/master/README.md) [drivers\_adapter\_khdf\_linux](https://gitee.com/openharmony/drivers_adapter_khdf_linux/blob/master/README.md) [drivers\_peripheral](https://gitee.com/openharmony/drivers_peripheral)