1 /* 2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited 3 */ 4 5 #ifndef SENSOR_H 6 #define SENSOR_H 7 8 #ifdef __cplusplus 9 extern "C" { 10 #endif 11 12 #include <stddef.h> 13 #include <stdint.h> 14 #include <unistd.h> 15 #include <sys/types.h> 16 17 #include "aos/hal/gpio.h" 18 #include "aos/hal/i2c.h" 19 20 #ifndef UNUSED 21 #if defined (__CC_ARM) 22 #define UNUSED __attribute__((unused)) 23 #elif defined (__ICCARM__) 24 #define UNUSED __attribute__((unused)) 25 #elif defined (__GNUC__) 26 #define UNUSED __attribute__((unused)) 27 #else 28 #define UNUSED 29 #endif 30 #endif 31 32 #define SENSOR_MAX_NUM 16 33 #define SENSOR_NAME_LEN 32 34 35 #define I2C_REG_LEN 1 36 #define I2C_DATA_LEN 1 37 #define I2C_OP_RETRIES AOS_WAIT_FOREVER 38 39 /* ioctl cmd list for sensor */ 40 typedef enum { 41 SENSOR_IOCTL_ODR_SET = 1, 42 SENSOR_IOCTL_RANGE_SET, 43 SENSOR_IOCTL_GET_INFO, 44 SENSOR_IOCTL_BIST_PROCESS, 45 SENSOR_IOCTL_WHO_AM_I, 46 SENSOR_IOCTL_SET_POWER, 47 SENSOR_IOCTL_GET_SENSOR_LIST, 48 SENSOR_IOCTL_DTC_CYCLE_SET, 49 SENSOR_IOCTL_GET_SENSOR_MODE, 50 SENSOR_IOCTL_SET_SENSOR_IRQ_CB, 51 SENSOR_IOCTL_SELF_TEST, 52 SENSOR_IOCTL_MAX 53 } sensor_cmd_type; 54 55 #ifndef likely 56 #if defined (__CC_ARM) 57 #define likely(x) __builtin_expect(!!(x), 1) 58 #elif defined (__ICCARM__) 59 #define likely(x) (x) 60 #elif defined (__GNUC__) 61 #define likely(x) __builtin_expect(!!(x), 1) 62 #else 63 #define likely(x) (x) 64 #endif 65 #endif 66 67 #ifndef unlikely 68 #if defined (__CC_ARM) 69 #define unlikely(x) __builtin_expect(!!(x), 0) 70 #elif defined (__ICCARM__) 71 #define unlikely(x) (x) 72 #elif defined (__GNUC__) 73 #define unlikely(x) __builtin_expect(!!(x), 0) 74 #else 75 #define unlikely(x) (x) 76 #endif 77 #endif 78 79 /* Physical generic sensor data defs generic structs here */ 80 #define DATA_AXIS_X 0 81 #define DATA_AXIS_Y 1 82 #define DATA_AXIS_Z 2 83 84 #define ToString(x) #x 85 86 #define dev_acc_path "/dev/acc" 87 #define dev_mag_path "/dev/mag" 88 #define dev_gyro_path "/dev/gyro" 89 #define dev_als_path "/dev/als" 90 #define dev_ps_path "/dev/ps" 91 #define dev_baro_path "/dev/baro" 92 #define dev_temp_path "/dev/temp" 93 #define dev_uv_path "/dev/uv" 94 #define dev_humi_path "/dev/humi" 95 #define dev_hall_path "/dev/hall" 96 #define dev_hr_path "/dev/hr" 97 #define dev_gps_path "/dev/gps" 98 #define dev_noise_path "/dev/noise" 99 #define dev_pm25_path "/dev/pm25" 100 #define dev_co2_path "/dev/co2" 101 #define dev_hcho_path "/dev/hcho" 102 #define dev_tvoc_path "/dev/tvoc" 103 #define dev_pm10_path "/dev/pm10" 104 #define dev_pm1_path "/dev/pm1" 105 #define dev_ph_path "/dev/ph" 106 #define dev_vwc_path "/dev/vwc" 107 #define dev_ec_path "/dev/ec" /* electric conductivity */ 108 #define dev_salinity_path "/dev/salinity" 109 #define dev_tds_path "/dev/tds" /* total dissolved solids */ 110 #define dev_windspeed_path "/dev/windspeed" 111 #define dev_winddirection_path "/dev/winddirection" 112 #define dev_rainfall_path "/dev/rainfall" 113 #define dev_rgb_path "/dev/rgb" 114 #define dev_gs_path "/dev/gs" 115 #define dev_ir_path "/dev/ir" 116 #define dev_rtc_path "/dev/rtc" 117 118 #define sensor_node_path "/dev/sensor" 119 120 #define GPS_STR "gps: " 121 #define RTC_STR "rtc: " 122 #define SENSOR_STR "sensor: " /* sensor debug header */ 123 #define ERROR_LINE "error on line is " 124 125 #define ACCELEROMETER_UNIT_FACTOR 1000 /* mg */ 126 #define MAGNETOMETER_UNIT_FACTOR 1000 /* mGauss */ 127 #define GYROSCOPE_UNIT_FACTOR 1000000 /* uDPS */ 128 129 #define DEV_ACC_PATH(x) dev_acc_path "/" ToString(x) 130 #define DEV_HUMI_PATH(x) dev_humi_path "/" ToString(x) 131 #define DEV_TEMP_PATH(x) dev_temp_path "/" ToString(x) 132 #define DEV_NOISE_PATH(x) dev_noise_path "/" ToString(x) 133 #define DEV_BARO_PATH(x) dev_baro_path "/" ToString(x) 134 #define DEV_HCHO_PATH(x) dev_hcho_path "/" ToString(x) 135 #define DEV_PM25_PATH(x) dev_pm25_path "/" ToString(x) 136 #define DEV_PM10_PATH(x) dev_pm10_path "/" ToString(x) 137 #define DEV_PM1_PATH(x) dev_pm1_path "/" ToString(x) 138 #define DEV_CO2_PATH(x) dev_co2_path "/" ToString(x) 139 #define DEV_TVOC_PATH(x) dev_tvoc_path "/" ToString(x) 140 141 /* add the new sensor type into the last position */ 142 typedef enum { 143 TAG_DEV_ACC = 0, /* Accelerometer */ 144 TAG_DEV_MAG, /* Magnetometer */ 145 TAG_DEV_GYRO, /* Gyroscope */ 146 TAG_DEV_ALS, /* Ambient light sensor */ 147 TAG_DEV_PS, /* Proximity */ 148 TAG_DEV_BARO, /* Barometer */ 149 TAG_DEV_TEMP, /* Temperature */ 150 TAG_DEV_UV, /* Ultraviolet */ 151 TAG_DEV_HUMI, /* Humidity */ 152 TAG_DEV_NOISE, /* NoiseLoudness */ 153 TAG_DEV_PM25, /* PM2.5 */ 154 TAG_DEV_PM1P0, /* PM1.0 */ 155 TAG_DEV_PM10, /* PM10 */ 156 TAG_DEV_CO2, /* CO2 Level */ 157 TAG_DEV_HCHO, /* HCHO Level */ 158 TAG_DEV_TVOC, /* TVOC Level */ 159 TAG_DEV_PH, /* PH value */ 160 TAG_DEV_VWC, /*volumetric water content*/ 161 TAG_DEV_EC, /* EC value */ 162 TAG_DEV_SALINITY, /* SALINITY value */ 163 TAG_DEV_TDS, /* Total dissolved solids */ 164 TAG_DEV_WINDSPD, /* Total dissolved solids */ 165 TAG_DEV_WINDDIR, /* Total dissolved solids */ 166 TAG_DEV_RAIN, /* Total dissolved solids */ 167 TAG_DEV_HALL, /* HALL */ 168 TAG_DEV_HR, /* Heart Rate */ 169 TAG_DEV_RGB, /* RGB sensor */ 170 TAG_DEV_GS, /* Gesture sensor */ 171 TAG_DEV_IR, /* IR sensor */ 172 TAG_DEV_GPS, 173 TAG_DEV_RTC, 174 TAG_DEV_SENSOR_NUM_MAX, 175 } sensor_tag_e; 176 177 typedef enum { 178 DEV_SENSOR_VENDOR_STM = 0, 179 DEV_SENSOR_VENDOR_SEMTECH, 180 DEV_SENSOR_VENDOR_AKM, 181 DEV_SENSOR_VENDOR_CAPELLA, 182 DEV_SENSOR_VENDOR_INVENSENSE, 183 DEV_SENSOR_VENDOR_ROHM, 184 DEV_SENSOR_VENDOR_BOSCH, 185 DEV_SENSOR_VENDOR_KIONIX, 186 DEV_SENSOR_VENDOR_LITEON, 187 DEV_SENSOR_VENDOR_AMS, 188 DEV_SENSOR_VENDOR_CNT_MAXIM, 189 } vendor_id_e; 190 191 typedef enum { 192 I2C_PORT, 193 SPI_PORT, 194 I2S_PORT, 195 UART_PORT, 196 MODBUS_PORT, 197 CAN_PORT, 198 } dev_io_port_e; 199 200 typedef enum { 201 DEV_POWER_OFF = 0, 202 DEV_POWER_ON, 203 DEV_SLEEP, 204 DEV_SUSPEND, 205 DEV_DEEP_SUSPEND, 206 } dev_power_mode_e; 207 208 typedef enum { 209 DEV_HEALTH_GOOD, 210 DEV_HEALTH_SICK, 211 } dev_health_state_e; 212 213 typedef enum { 214 DEV_POLLING = 0, 215 DEV_INT, 216 DEV_DATA_READY, 217 DEV_FIFO, 218 DEV_MODE_INVALID 219 } work_mode_e; 220 221 typedef enum { 222 mg = 0, 223 mGauss, 224 udps, 225 lux, 226 cm, 227 pa, 228 permillage, 229 bpm, 230 dCelsius, 231 } value_unit_e; 232 233 typedef enum { 234 GS_SENSOR_UP, 235 GS_SENSOR_DOWN, 236 GS_SENSOR_LEFT, 237 GS_SENSOR_RIGHT, 238 GS_SENSOR_CLOCKWISE, 239 GS_SENSOR_ANTICLOCKWISE, 240 GS_SENSOR_WAVE, 241 GS_SENSOR_INVALID, 242 } gs_type_e; 243 244 typedef struct _dev_accel_data_t { 245 uint64_t timestamp; 246 int32_t data[3]; 247 #ifdef AOS_SENSOR_ACC_SUPPORT_STEP 248 uint32_t step; 249 #endif 250 } accel_data_t; 251 252 typedef struct _dev_gyro_data_t { 253 uint64_t timestamp; 254 int32_t data[3]; 255 } gyro_data_t; 256 257 typedef struct _dev_mag_data_t { 258 uint64_t timestamp; 259 int32_t data[3]; 260 } mag_data_t; 261 262 typedef struct _dev_barometer_data_t { 263 uint64_t timestamp; 264 uint32_t p; 265 } barometer_data_t; 266 267 typedef struct _dev_temperature_data_t { 268 uint64_t timestamp; 269 int32_t t; 270 } temperature_data_t; 271 272 typedef struct _dev_humidity_data_t { 273 uint64_t timestamp; 274 uint32_t h; 275 } humidity_data_t; 276 277 typedef struct _dev_integer_data_t { 278 uint64_t timestamp; 279 int32_t data; 280 } integer_data_t; 281 282 typedef struct _dev_als_data_t { 283 uint64_t timestamp; 284 uint32_t lux; 285 } als_data_t; 286 287 typedef struct _dev_uv_data_t { 288 uint64_t timestamp; 289 uint16_t uvi; 290 } uv_data_t; 291 292 typedef struct _dev_proximity_data_t { 293 uint64_t timestamp; 294 uint32_t present; 295 } proximity_data_t; 296 297 typedef struct _dev_hall_data_t { 298 uint64_t timestamp; 299 uint8_t hall_level; 300 } hall_data_t; 301 302 typedef struct _dev_rgb_data_t { 303 uint64_t timestamp; 304 uint32_t data[3]; 305 } rgb_data_t; 306 307 typedef struct _dev_ir_data_t { 308 uint64_t timestamp; 309 uint32_t ir; 310 } ir_data_t; 311 312 typedef struct _dev_ecg_data_t { 313 uint64_t timestamp; 314 int16_t raw_data; 315 } ecg_data_t; 316 317 typedef struct _dev_heart_rate_data_t { 318 uint64_t timestamp; 319 uint8_t hear_rate; 320 } heart_rate_data_t; 321 322 typedef struct _dev_rtc_data_t { 323 uint64_t timestamp; 324 uint8_t seconds; /* !< Seconds parameter, from 00 to 59 */ 325 uint8_t minutes; /* !< Minutes parameter, from 01 to 59 */ 326 uint8_t hours; /* !< Hours parameter, 24Hour mode, 00 to 23 */ 327 uint8_t day; /* !< Day in a week, from 1 to 7 */ 328 uint8_t date; /* !< Date in a month, 1 to 31 */ 329 uint8_t month; /* !< Month in a year, 1 to 12 */ 330 uint8_t year; /* !< Year parameter, 00 to 99, 00 is 2000 and 99 is 2099 */ 331 } rtc_data_t; 332 333 typedef struct _dev_blood_pressure_data_t { 334 uint64_t timestamp; 335 uint16_t systolic; 336 uint16_t diastolic; 337 } blood_pressure_t; 338 339 typedef struct _dev_gs_data_t { 340 uint64_t timestamp; 341 gs_type_e gs_type; 342 } gs_data_t; 343 344 typedef void (*SENSOR_IRQ_CALLBACK)(sensor_tag_e tag, uint8_t instance); 345 346 typedef struct _dev_sensor_config_t { 347 uint8_t id; 348 uint32_t range; 349 uint32_t inerval; /* polling interval */ 350 work_mode_e mode; 351 void *data_buf; 352 uint32_t data_len; 353 SENSOR_IRQ_CALLBACK irq_callback; 354 } dev_sensor_config_t; 355 356 typedef struct _sensor_identity_t { 357 sensor_tag_e tag; 358 uint8_t instance; 359 dev_io_port_e io_port; 360 }sensor_identity_t; 361 362 typedef struct _sensor_list_t { 363 uint32_t cnt; 364 sensor_identity_t list[SENSOR_MAX_NUM]; 365 } sensor_list_t; 366 367 typedef struct _dev_sensor_info_t { 368 vendor_id_e vendor; 369 char *model; 370 value_unit_e unit; 371 uint32_t range_max; 372 uint32_t range_min; 373 dev_health_state_e health; 374 int data[3]; 375 } dev_sensor_info_t; 376 377 typedef struct _dev_sensor_full_info_t { 378 dev_sensor_info_t info; 379 dev_sensor_config_t config; 380 } dev_sensor_full_info_t; 381 382 typedef struct _dev_sensor_data_t { 383 uint64_t timestamp; 384 int32_t data[3]; 385 } dev_sensor_data_t; 386 387 typedef struct _dev_sensor_pkg_t { 388 sensor_tag_e tag; 389 390 union { 391 dev_sensor_info_t info; 392 dev_sensor_config_t config; 393 dev_sensor_data_t data; 394 } allocator; 395 } dev_sensor_pkg_t; 396 397 typedef struct _sensor_obj_t { 398 char *path; 399 sensor_tag_e tag; 400 uint8_t instance; 401 dev_io_port_e io_port; 402 work_mode_e mode; 403 void *data_buf; 404 uint32_t data_len; 405 dev_power_mode_e power; 406 gpio_dev_t gpio; 407 dev_sensor_full_info_t info; 408 uint8_t ref; 409 uint8_t drv_index; 410 411 int (*open)(void); 412 int (*close)(void); 413 int (*read)(void *, size_t); 414 int (*write)(const void *buf, size_t len); 415 int (*ioctl)(int cmd, unsigned long arg); 416 void (*irq_handle)(void); 417 } sensor_obj_t; 418 419 typedef struct _sensor_node_t { 420 sensor_tag_e tag; 421 char *path; 422 int fd; 423 } sensor_node_t; 424 425 typedef enum { 426 ACC_RANGE_2G, 427 ACC_RANGE_4G, 428 ACC_RANGE_8G, 429 ACC_RANGE_16G, 430 ACC_RANGE_6G, 431 ACC_RANGE_12G, 432 ACC_RANGE_24G, 433 ACC_RANGE_100G, 434 ACC_RANGE_200G, 435 ACC_RANGE_400G, 436 ACC_RANGE_MAX 437 } acc_range_e; 438 439 typedef enum { 440 GYRO_RANGE_125DPS, 441 GYRO_RANGE_250DPS, 442 GYRO_RANGE_500DPS, 443 GYRO_RANGE_1000DPS, 444 GYRO_RANGE_2000DPS, 445 GYRO_RANGE_MAX 446 } gyro_range_e; 447 448 typedef enum { 449 MAG_RANGE_4GAUSS, 450 MAG_RANGE_8GAUSS, 451 MAG_RANGE_12GAUSS, 452 MAG_RANGE_16GAUSS, 453 MAG_RANGE_MAX 454 } mag_range_e; 455 456 typedef struct _gps_time { 457 int year; 458 int mon; 459 int day; 460 int hour; 461 int min; 462 int sec; 463 int hsec; 464 } gps_time_t; 465 466 typedef struct _dev_gps_data_t { 467 uint64_t timestamp; 468 gps_time_t utc; 469 float lat; 470 float lon; 471 float elv; 472 } gps_data_t; 473 474 int sensor_hal_init(void); 475 476 int sensor_hal_open(sensor_tag_e tag, uint8_t instance); 477 478 int sensor_hal_close(sensor_tag_e tag, uint8_t instance); 479 480 ssize_t sensor_hal_read(sensor_tag_e tag, uint8_t instance, void *buf, size_t len); 481 482 ssize_t sensor_hal_write(sensor_tag_e tag, uint8_t instance, const void *buf, size_t len); 483 484 int sensor_hal_ioctl(sensor_tag_e tag, uint8_t instance, sensor_cmd_type cmd, unsigned long arg); 485 486 #ifdef __cplusplus 487 } 488 #endif 489 490 #endif /* HAL_SENSOR_H */ 491 492