• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
17  * @addtogroup PanSensor
18  * @{
19  *
20  * @brief Provides standard open APIs for you to use common capabilities of sensors.
21  *
22  * For example, you can call these APIs to obtain sensor information,
23  * subscribe to or unsubscribe from sensor data, enable or disable a sensor,
24  * and set the sensor data reporting mode.
25  *
26  * @since 5
27  */
28 
29 /**
30  * @file sensor_agent_type.h
31  *
32  * @brief Defines the basic data used by the sensor agent to manage sensors.
33  *
34  * @since 5
35  */
36 
37 #ifndef SENSOR_AGENT_TYPE_H
38 #define SENSOR_AGENT_TYPE_H
39 
40 #include <stdint.h>
41 
42 #ifdef __cplusplus
43 #if __cplusplus
44 extern "C" {
45 #endif
46 #endif
47 
48 /** Maximum length of the sensor name */
49 #ifndef NAME_MAX_LEN
50 #define NAME_MAX_LEN 128
51 #endif /* NAME_MAX_LEN */
52 /** Size of sensor data */
53 #ifndef SENSOR_USER_DATA_SIZE
54 #define SENSOR_USER_DATA_SIZE 104
55 #endif /* SENSOR_USER_DATA_SIZE */
56 /** Maximum length of the sensor version */
57 #ifndef VERSION_MAX_LEN
58 #define VERSION_MAX_LEN 16
59 #endif /* SENSOR_USER_DATA_SIZE */
60 
61 /**
62  * @brief Enumerates sensor types.
63  *
64  * @since 5
65  */
66 typedef enum SensorTypeId {
67     SENSOR_TYPE_ID_NONE = 0,                   /**< None */
68     SENSOR_TYPE_ID_ACCELEROMETER = 1,          /**< Acceleration sensor */
69     SENSOR_TYPE_ID_GYROSCOPE = 2,              /**< Gyroscope sensor */
70     SENSOR_TYPE_ID_AMBIENT_LIGHT = 5,          /**< Ambient light sensor */
71     SENSOR_TYPE_ID_MAGNETIC_FIELD = 6,         /**< Magnetic field sensor */
72     SENSOR_TYPE_ID_CAPACITIVE = 7,             /**< Capacitive sensor */
73     SENSOR_TYPE_ID_BAROMETER = 8,              /**< Barometric pressure sensor */
74     SENSOR_TYPE_ID_TEMPERATURE = 9,            /**< Temperature sensor */
75     SENSOR_TYPE_ID_HALL = 10,                  /**< Hall effect sensor */
76     SENSOR_TYPE_ID_GESTURE = 11,               /**< Gesture sensor */
77     SENSOR_TYPE_ID_PROXIMITY = 12,             /**< Proximity sensor */
78     SENSOR_TYPE_ID_HUMIDITY = 13,              /**< Humidity sensor */
79     SENSOR_TYPE_ID_COLOR = 14,                 /**< Color sensor */
80     SENSOR_TYPE_ID_SAR = 15,                   /**< Sar sensor */
81     SENSOR_TYPE_ID_AMBIENT_LIGHT1 = 16,        /**< Secondary ambient light sensor */
82     SENSOR_TYPE_ID_HALL_EXT = 17,              /**< Extended hall effect sensor */
83     SENSOR_TYPE_ID_PHYSICAL_MAX = 0xFF,        /**< Maximum type ID of a physical sensor */
84     SENSOR_TYPE_ID_ORIENTATION = 256,          /**< Orientation sensor */
85     SENSOR_TYPE_ID_GRAVITY = 257,              /**< Gravity sensor */
86     SENSOR_TYPE_ID_LINEAR_ACCELERATION = 258,  /**< Linear acceleration sensor */
87     SENSOR_TYPE_ID_ROTATION_VECTOR = 259,      /**< Rotation vector sensor */
88     SENSOR_TYPE_ID_AMBIENT_TEMPERATURE = 260,  /**< Ambient temperature sensor */
89     SENSOR_TYPE_ID_MAGNETIC_FIELD_UNCALIBRATED = 261,  /**< Uncalibrated magnetic field sensor */
90     SENSOR_TYPE_ID_GAME_ROTATION_VECTOR = 262,    /**< Game rotation vector sensor */
91     SENSOR_TYPE_ID_GYROSCOPE_UNCALIBRATED = 263,  /**< Uncalibrated gyroscope sensor */
92     SENSOR_TYPE_ID_SIGNIFICANT_MOTION = 264,    /**< Significant motion sensor */
93     SENSOR_TYPE_ID_PEDOMETER_DETECTION = 265,   /**< Pedometer detection sensor */
94     SENSOR_TYPE_ID_PEDOMETER = 266,             /**< Pedometer sensor */
95     SENSOR_TYPE_ID_POSTURE = 267,               /**< Posture sensor */
96     SENSOR_TYPE_ID_HEADPOSTURE = 268,           /**< Head posture sensor */
97     SENSOR_TYPE_ID_DROP_DETECTION = 269,       /**< Drop detection sensor */
98     SENSOR_TYPE_ID_GEOMAGNETIC_ROTATION_VECTOR = 277,  /**< Geomagnetic rotation vector sensor */
99     SENSOR_TYPE_ID_HEART_RATE = 278,            /**< Heart rate sensor */
100     SENSOR_TYPE_ID_DEVICE_ORIENTATION = 279,    /**< Device orientation sensor */
101     SENSOR_TYPE_ID_WEAR_DETECTION = 280,        /**< Wear detection sensor */
102     SENSOR_TYPE_ID_ACCELEROMETER_UNCALIBRATED = 281,   /**< Uncalibrated acceleration sensor */
103     SENSOR_TYPE_ID_MAX = 30,      /**< Maximum number of sensor type IDs*/
104 } SensorTypeId;
105 
106 /**
107  * @brief Defines sensor information.
108  *
109  * @since 5
110  */
111 typedef struct SensorInfo {
112     char sensorName[NAME_MAX_LEN];   /**< Sensor name */
113     char vendorName[NAME_MAX_LEN];   /**< Sensor vendor */
114     char firmwareVersion[VERSION_MAX_LEN];  /**< Sensor firmware version */
115     char hardwareVersion[VERSION_MAX_LEN];  /**< Sensor hardware version */
116     int32_t sensorTypeId = -1;  /**< Sensor type ID */
117     int32_t sensorId = -1;      /**< Sensor ID */
118     float maxRange = 0.0;        /**< Maximum measurement range of the sensor */
119     float precision = 0.0;       /**< Sensor accuracy */
120     float power = 0.0;           /**< Sensor power */
121     int64_t minSamplePeriod = -1; /**< Minimum sample period allowed, in ns */
122     int64_t maxSamplePeriod = -1; /**< Maximum sample period allowed, in ns */
123 } SensorInfo;
124 
125 /**
126  * @brief Enumerates the accuracy levels of data reported by a sensor.
127  *
128  * @since 11
129  */
130 typedef enum SensorAccuracy {
131     /**< The sensor data is unreliable.
132      * It is possible that the sensor does not contact with the device to measure.*/
133     ACCURACY_UNRELIABLE = 0,
134     /**< The sensor data is at a low accuracy level.
135      * You are required to calibrate the data based on the environment before using it. */
136     ACCURACY_LOW = 1,
137     /**< The sensor data is at a medium accuracy level.
138      * You are advised to calibrate the data based on the environment before using it. */
139     ACCURACY_MEDIUM = 2,
140     /**< The sensor data is at a high accuracy level.
141      * The data can be used directly. */
142     ACCURACY_HIGH = 3,
143 } SensorAccuracy;
144 
145 /**
146  * @brief Defines the data reported by the sensor.
147  *
148  * @since 5
149  */
150 typedef struct SensorEvent {
151     int32_t sensorTypeId = -1;  /**< Sensor type ID */
152     int32_t version = -1;       /**< Sensor algorithm version */
153     int64_t timestamp = -1;     /**< Time when sensor data was reported */
154     int32_t option = -1;       /**< Sensor data options, including the measurement range and accuracy */
155     int32_t mode = -1;          /**< Sensor data reporting mode (described in {@link SensorMode}) */
156     uint8_t *data = nullptr;         /**< Sensor data */
157     uint32_t dataLen = 0;      /**< Sensor data length */
158 } SensorEvent;
159 
160 /**
161  * @brief Defines the callback for data reporting by the sensor agent.
162  *
163  * @since 5
164  */
165 typedef void (*RecordSensorCallback)(SensorEvent *event);
166 
167 /**
168  * @brief Defines a reserved field for the sensor data subscriber.
169  *
170  * @since 5
171  */
172 typedef struct UserData {
173     char userData[SENSOR_USER_DATA_SIZE];  /**< Reserved for the sensor data subscriber */
174 } UserData;
175 
176 /**
177  * @brief Defines information about the sensor data subscriber.
178  *
179  * @since 5
180  */
181 typedef struct SensorUser {
182     char name[NAME_MAX_LEN];  /**< Name of the sensor data subscriber */
183     RecordSensorCallback callback;   /**< Callback for reporting sensor data */
184     UserData *userData = nullptr;              /**< Reserved field for the sensor data subscriber */
185 } SensorUser;
186 
187 /**
188  * @brief Enumerates data reporting modes of sensors.
189  *
190  * @since 5
191  */
192 typedef enum SensorMode {
193     SENSOR_DEFAULT_MODE = 0,   /**< Default data reporting mode */
194     SENSOR_REALTIME_MODE = 1,  /**< Real-time data reporting mode to report a group of data each time */
195     SENSOR_ON_CHANGE = 2,   /**< Real-time data reporting mode to report data upon status changes */
196     SENSOR_ONE_SHOT = 3,    /**< Real-time data reporting mode to report data only once */
197     SENSOR_FIFO_MODE = 4,   /**< FIFO-based data reporting mode to report data based on the <b>BatchCnt</b> setting */
198     SENSOR_MODE_MAX2,        /**< Maximum sensor data reporting mode */
199 } SensorMode;
200 
201 /**
202  * @brief Defines the struct of the data reported by the acceleration sensor.
203  * This sensor measures the acceleration applied to the device on three physical axes (x, y, and z), in m/s2.
204  *
205  */
206 typedef struct AccelData {
207     float x = 0.0;
208     float y = 0.0;
209     float z = 0.0;
210 } AccelData;
211 
212 /**
213  * @brief Defines the struct of the data reported by the linear acceleration sensor.
214  * This sensor measures the linear acceleration applied to the device on three physical axes (x, y, and z), in m/s2.
215  */
216 typedef struct LinearAccelData {
217     float x = 0.0;
218     float y = 0.0;
219     float z = 0.0;
220 } LinearAccelData;
221 
222 /**
223  * @brief Defines the struct of the data reported by the gyroscope sensor.
224  * This sensor measures the angular velocity of the device on three physical axes (x, y, and z), in rad/s.
225  */
226 typedef struct GyroscopeData {
227     float x = 0.0;
228     float y = 0.0;
229     float z = 0.0;
230 } GyroscopeData;
231 
232 /**
233  * @brief Defines the struct of the data reported by the gravity sensor.
234  * This sensor measures the acceleration of gravity applied to the device on three physical axes (x, y, and z), in m/s2.
235  */
236 typedef struct GravityData {
237     float x = 0.0;
238     float y = 0.0;
239     float z = 0.0;
240 } GravityData;
241 
242 /**
243  * @brief Defines the struct of the data reported by the uncalibrated acceleration sensor.
244  * This sensor measures the uncalibrated acceleration applied to the device on three physical axes (x, y, and z),
245  * in m/s2.
246  */
247 typedef struct AccelUncalibratedData {
248     float x = 0.0;
249     float y = 0.0;
250     float z = 0.0;
251     float biasX = 0.0;
252     float biasY = 0.0;
253     float biasZ = 0.0;
254 } AccelUncalibratedData;
255 
256 /**
257  * @brief Defines the struct of the data reported by the uncalibrated gyroscope sensor.
258  * This sensor measures the uncalibrated angular velocity of the device on three physical axes (x, y, and z), in rad/s.
259  */
260 typedef struct GyroUncalibratedData {
261     float x = 0.0;
262     float y = 0.0;
263     float z = 0.0;
264     float biasX = 0.0;
265     float biasY = 0.0;
266     float biasZ = 0.0;
267 } GyroUncalibratedData;
268 
269 /**
270  * @brief Defines the struct of the data reported by the significant motion sensor.
271  * This sensor detects whether there is substantial motion in the device on the three physical axes (x, y, and z).
272  * The value <b>1</b> means that there is substantial motion, and <b>0</b> means the opposite.
273  */
274 typedef struct SignificantMotionData {
275     float scalar = 0.0;
276 } SignificantMotionData;
277 
278 /**
279  * @brief Defines the struct of the data reported by the pedometer detection sensor.
280  * This sensor detects whether a user is walking.
281  * The value <b>1</b> means that the user is walking, and <b>0</b> means the opposite.
282  */
283 typedef struct PedometerDetectData {
284     float scalar = 0.0;
285 } PedometerDetectData;
286 
287 /**
288  * @brief Defines the struct of the data reported by the pedometer sensor.
289  * This sensor counts the number of steps taken by a user.
290  */
291 typedef struct PedometerData {
292     float steps = 0.0;
293 } PedometerData;
294 
295 /**
296  * @brief Defines the struct of the data reported by the ambient temperature sensor.
297  * This sensor measures the ambient temperature, in degrees Celsius (°C).
298  */
299 typedef struct AmbientTemperatureData {
300     float temperature = 0.0;
301 } AmbientTemperatureData;
302 
303 /**
304  * @brief Defines the struct of the data reported by the humidity sensor.
305  * This sensor measures the relative humidity of the environment,
306  * expressed as a percentage (%).
307  */
308 typedef struct HumidityData {
309     float humidity = 0.0;
310 } HumidityData;
311 
312 /**
313  * @brief Defines the struct of the data reported by the temperature sensor.
314  * This sensor measures the relative temperature of the environment, in degrees Celsius (°C).
315  */
316 typedef struct TemperatureData {
317     float temperature = 0.0;
318 } TemperatureData;
319 
320 /**
321  * @brief Defines the struct of the data reported by the magnetic field sensor.
322  * This sensor measures the ambient geomagnetic field in three physical axes (x, y, z), in μT.
323  */
324 typedef struct MagneticFieldData {
325     float x = 0.0;
326     float y = 0.0;
327     float z = 0.0;
328 } MagneticFieldData;
329 
330 /**
331  * @brief Defines the struct of the data reported by the uncalibrated magnetic field sensor.
332  * This sensor measures the uncalibrated ambient geomagnetic field in three physical axes (x, y, z), in μT.
333  */
334 typedef struct MagneticFieldUncalibratedData {
335     float x = 0.0;
336     float y = 0.0;
337     float z = 0.0;
338     float biasX = 0.0;
339     float biasY = 0.0;
340     float biasZ = 0.0;
341 } MagneticFieldUncalibratedData;
342 
343 /**
344  * @brief Defines the struct of the data reported by the barometer sensor.
345  * This sensor measures the atmospheric pressure, in hPa or mb.
346  */
347 typedef struct BarometerData {
348     float pressure = 0.0;
349 } BarometerData;
350 
351 /**
352  * @brief Defines the struct of the data reported by the device orientation sensor.
353  * This sensor measures the direction of rotation of the device, in rad.
354  */
355 typedef struct DeviceOrientationData {
356     float scalar = 0.0;
357 } DeviceOrientationData;
358 
359 /**
360  * @brief Defines the struct of the data reported by the orientation sensor.
361  * This sensor measures the angle of rotation of the device around all three physical axes (z, x, y), in rad.
362  */
363 typedef struct OrientationData {
364     float alpha = 0.0; /**< The device rotates at an angle around the Z axis. */
365     float beta = 0.0;  /**< The device rotates at an angle around the X axis. */
366     float gamma = 0.0; /**< The device rotates at an angle around the Y axis. */
367 } OrientationData;
368 
369 /**
370  * @brief Defines the struct of the data reported by the rotation vector sensor.
371  * This sensor measures the rotation vector of the device.
372  * It is synthesized by the acceleration sensor and gyroscope sensor.
373  */
374 typedef struct RotationVectorData {
375     float x = 0.0;
376     float y = 0.0;
377     float z = 0.0;
378     float w = 0.0;
379 } RotationVectorData;
380 
381 /**
382  * @brief Defines the struct of the data reported by the game rotation vector sensor.
383  * This sensor measures the game rotation vector of the device.
384  * It is synthesized by the acceleration sensor and gyroscope sensor.
385  */
386 typedef struct GameRotationVectorData {
387     float x = 0.0;
388     float y = 0.0;
389     float z = 0.0;
390     float w = 0.0;
391 } GameRotationVectorData;
392 
393 /**
394  * @brief Defines the struct of the data reported by the geomagnetic rotation vector sensor.
395  * This sensor measures the geomagnetic rotation vector of the device.
396  * It is synthesized by the acceleration sensor and magnetic field sensor.
397  */
398 typedef struct GeomagneticRotaVectorData {
399     float x = 0.0;
400     float y = 0.0;
401     float z = 0.0;
402     float w = 0.0;
403 } GeomagneticRotaVectorData;
404 
405 /**
406  * @brief Defines the struct of the data reported by the proximity light sensor.
407  * This sensor measures the proximity or distance of visible objects relative to the device display,
408  * where 0 indicates proximity and 1 indicates distance.
409  */
410 typedef struct ProximityData {
411     float distance = 0.0;
412 } ProximityData;
413 
414 /**
415  * @brief Defines the struct of the data reported by the ambient light sensor.
416  * This sensor measures the intensity of light around the device, in lux.
417  */
418 typedef struct AmbientLightData {
419     float intensity = 0.0;
420 } AmbientLightData;
421 
422 /**
423  * @brief Defines the struct of the data reported by the hall effect sensor.
424  * This sensor measures whether there is magnetic attraction around the device.
425  * The value <b>1</b> means that there is magnet attraction, and <b>0</b> means the opposite.
426  */
427 typedef struct HallData {
428     float status = 0.0;
429 } HallData;
430 
431 /**
432  * @brief Defines the struct of the data reported by the heart rate sensor.
433  * This sensor measures a user's heart rate, in bpm.
434  */
435 typedef struct HeartRateData {
436     float heartRate = 0.0;
437 } HeartRateData;
438 
439 /**
440  * @brief Defines the struct of the data reported by the wear detection sensor.
441  * This sensor detects whether a user is wearing a wearable device.
442  * The value <b>1</b> means that the user is wearing a wearable device, and <b>0</b> means the opposite.
443  */
444 typedef struct WearDetectionData {
445     float value = 0.0;
446 } WearDetectionData;
447 
448 /**
449  * @brief Defines the struct of the data reported by the color sensor.
450  * This sensor is used to measure the luminous intensity (in lux) and color temperature (in Kelvin).
451  */
452 typedef struct ColorData {
453     float lightIntensity = 0.0;
454     float colorTemperature = 0.0;
455 } ColorData;
456 
457 /**
458  * @brief Defines the struct of the data reported by the SAR sensor.
459  * This sensor measures the SAR, in W/kg.
460  */
461 typedef struct SarData {
462     float absorptionRatio = 0.0;
463 } SarData;
464 
465 /**
466  * @brief Defines the struct of the data reported by the posture sensor.
467  * This sensor measures the angle between two screens, in degrees. The angle ranges from 0 to 180.
468  */
469 typedef struct PostureData {
470     float gxm = 0.0; /**< The main screen acceleration on the x axis */
471     float gym = 0.0; /**< The main screen acceleration on the y axis */
472     float gzm = 0.0; /**< The main screen acceleration on the z axis */
473     float gxs = 0.0; /**< The second screen acceleration on the x axis */
474     float gys = 0.0; /**< The second screen acceleration on the y axis */
475     float gzs = 0.0; /**< The second screen acceleration on the z axis */
476     float angle = 0.0; /**< The angle between two screens. The angle ranges from 0 to 180 degrees. */
477 } PostureData;
478 
479 /**
480  * @brief Defines the struct of the data reported by the head posture sensor.
481  * This sensor measures the head posture of user.
482  */
483 typedef struct HeadPostureData {
484     int32_t order = 0;
485     float w = 0.0;
486     float x = 0.0;
487     float y = 0.0;
488     float z = 0.0;
489 } HeadPostureData;
490 
491 typedef struct DropDetectionData {
492     float status = 0.0;
493 } DropDetectionData;
494 
495 typedef struct SensorActiveInfo {
496     int32_t pid = -1;        /**< PID */
497     int32_t sensorId = -1;   /**< Sensor ID */
498     int64_t samplingPeriodNs = -1;  /**< Sample period, in ns */
499     int64_t maxReportDelayNs = -1;  /**< Maximum Report Delay, in ns */
500 } SensorActiveInfo;
501 
502 typedef void (*SensorActiveInfoCB)(SensorActiveInfo &sensorActiveInfo);
503 
504 #ifdef __cplusplus
505 #if __cplusplus
506 }
507 #endif
508 #endif
509 #endif /* SENSOR_AGENT_TYPE_H */
510 /**< @} */
511