From fc505837b95b097fd9f67a5e2f7286a4bca85899 Mon Sep 17 00:00:00 2001 From: Liu_Weichao Date: Wed, 16 Jun 2021 18:35:33 +0800 Subject: [PATCH] add sensor framework --- APP_Framework/Framework/Kconfig | 5 +- APP_Framework/Framework/sensor/Kconfig | 40 ++ APP_Framework/Framework/sensor/Makefile | 22 +- .../Framework/sensor}/co2/Kconfig | 4 +- .../Framework/sensor}/co2/Makefile | 2 +- .../Framework/sensor}/co2/zg09/Makefile | 0 .../Framework/sensor}/co2/zg09/zg09.c | 61 +-- .../Framework/sensor}/humidity/Kconfig | 0 .../Framework/sensor}/humidity/Makefile | 2 +- .../sensor}/humidity/hs300x_humi/Makefile | 0 .../humidity/hs300x_humi/hs300x_humi.c | 36 +- .../Framework/sensor}/pm/Kconfig | 4 +- .../Framework/sensor}/pm/Makefile | 2 +- .../Framework/sensor}/pm/ps5308/Makefile | 0 .../Framework/sensor}/pm/ps5308/ps5308.c | 70 ++-- .../Framework/sensor}/temperature/Kconfig | 4 +- .../Framework/sensor}/temperature/Makefile | 2 +- .../sensor}/temperature/hs300x_temp/Makefile | 0 .../temperature/hs300x_temp/hs300x_temp.c | 36 +- .../Framework/sensor}/voice/Kconfig | 4 +- .../Framework/sensor}/voice/Makefile | 2 +- .../Framework/sensor}/voice/d124/Makefile | 0 .../Framework/sensor}/voice/d124/d124.c | 43 +- .../transform_layer/xiuos/transform.c | 54 +-- .../transform_layer/xiuos/transform.h | 11 + Ubiquitous/XiUOS/framework/Kconfig | 2 - Ubiquitous/XiUOS/framework/Makefile | 2 +- Ubiquitous/XiUOS/framework/perception/Kconfig | 46 -- .../XiUOS/framework/perception/Makefile | 25 -- .../XiUOS/framework/perception/sensor.c | 393 ------------------ .../XiUOS/framework/perception/sensor.h | 128 ------ 31 files changed, 222 insertions(+), 778 deletions(-) create mode 100644 APP_Framework/Framework/sensor/Kconfig rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/co2/Kconfig (93%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/co2/Makefile (62%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/co2/zg09/Makefile (100%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/co2/zg09/zg09.c (76%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/humidity/Kconfig (100%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/humidity/Makefile (64%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/humidity/hs300x_humi/Makefile (100%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/humidity/hs300x_humi/hs300x_humi.c (84%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/pm/Kconfig (95%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/pm/Makefile (62%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/pm/ps5308/Makefile (100%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/pm/ps5308/ps5308.c (85%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/temperature/Kconfig (90%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/temperature/Makefile (64%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/temperature/hs300x_temp/Makefile (100%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/temperature/hs300x_temp/hs300x_temp.c (84%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/voice/Kconfig (93%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/voice/Makefile (62%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/voice/d124/Makefile (100%) rename {Ubiquitous/XiUOS/framework/perception => APP_Framework/Framework/sensor}/voice/d124/d124.c (85%) delete mode 100644 Ubiquitous/XiUOS/framework/perception/Kconfig delete mode 100644 Ubiquitous/XiUOS/framework/perception/Makefile delete mode 100644 Ubiquitous/XiUOS/framework/perception/sensor.c delete mode 100644 Ubiquitous/XiUOS/framework/perception/sensor.h diff --git a/APP_Framework/Framework/Kconfig b/APP_Framework/Framework/Kconfig index 58a87154..a77bfa67 100644 --- a/APP_Framework/Framework/Kconfig +++ b/APP_Framework/Framework/Kconfig @@ -1,6 +1,9 @@ -config SUPPORT_SENSOR_FRAMEWORK +menuconfig SUPPORT_SENSOR_FRAMEWORK bool "select sensor framework" default y + if SUPPORT_SENSOR_FRAMEWORK + source "$APP_DIR/Framework/sensor/Kconfig" + endif config TRANSFORM_LAYER_ATTRIUBUTE bool "select transform layer" diff --git a/APP_Framework/Framework/sensor/Kconfig b/APP_Framework/Framework/sensor/Kconfig new file mode 100644 index 00000000..d1a58daf --- /dev/null +++ b/APP_Framework/Framework/sensor/Kconfig @@ -0,0 +1,40 @@ +menuconfig SENSOR_SENSORDEVICE + bool "Using sensor devices" + default y + +if SENSOR_SENSORDEVICE + menuconfig SENSOR_CO2 + bool "Using CO2 sensor device" + default n + if SENSOR_CO2 + source "$APP_DIR/Framework/sensor/co2/Kconfig" + endif + + menuconfig SENSOR_PM + bool "Using PM sensor device" + default n + if SENSOR_PM + source "$APP_DIR/Framework/sensor/pm/Kconfig" + endif + + menuconfig SENSOR_VOICE + bool "Using voice sensor device" + default n + if SENSOR_VOICE + source "$APP_DIR/Framework/sensor/voice/Kconfig" + endif + + menuconfig SENSOR_TEMPERATURE + bool "Using temperature sensor device" + default n + if SENSOR_TEMPERATURE + source "$APP_DIR/Framework/sensor/temperature/Kconfig" + endif + + menuconfig SENSOR_HUMIDITY + bool "Using humidity sensor device" + default n + if SENSOR_HUMIDITY + source "$APP_DIR/Framework/sensor/humidity/Kconfig" + endif +endif diff --git a/APP_Framework/Framework/sensor/Makefile b/APP_Framework/Framework/sensor/Makefile index b3b226a8..d3772dcb 100644 --- a/APP_Framework/Framework/sensor/Makefile +++ b/APP_Framework/Framework/sensor/Makefile @@ -1,3 +1,23 @@ SRC_FILES := sensor.c -include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file +ifeq ($(CONFIG_SENSOR_CO2),y) + SRC_DIR += co2 +endif + +ifeq ($(CONFIG_SENSOR_PM),y) + SRC_DIR += pm +endif + +ifeq ($(CONFIG_SENSOR_VOICE),y) + SRC_DIR += voice +endif + +ifeq ($(CONFIG_SENSOR_TEMPERATURE),y) + SRC_DIR += temperature +endif + +ifeq ($(CONFIG_SENSOR_HUMIDITY),y) + SRC_DIR += humidity +endif + +include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/framework/perception/co2/Kconfig b/APP_Framework/Framework/sensor/co2/Kconfig similarity index 93% rename from Ubiquitous/XiUOS/framework/perception/co2/Kconfig rename to APP_Framework/Framework/sensor/co2/Kconfig index 52b4f846..9335d354 100644 --- a/Ubiquitous/XiUOS/framework/perception/co2/Kconfig +++ b/APP_Framework/Framework/sensor/co2/Kconfig @@ -1,9 +1,9 @@ -config PERCEPTION_ZG09 +config SENSOR_ZG09 bool "Using zg09" default n - if PERCEPTION_ZG09 + if SENSOR_ZG09 config SENSOR_DEVICE_ZG09 string "zg09 sensor name" default "zg09_1" diff --git a/Ubiquitous/XiUOS/framework/perception/co2/Makefile b/APP_Framework/Framework/sensor/co2/Makefile similarity index 62% rename from Ubiquitous/XiUOS/framework/perception/co2/Makefile rename to APP_Framework/Framework/sensor/co2/Makefile index 7aeced63..40ab8d76 100644 --- a/Ubiquitous/XiUOS/framework/perception/co2/Makefile +++ b/APP_Framework/Framework/sensor/co2/Makefile @@ -1,4 +1,4 @@ -ifeq ($(CONFIG_PERCEPTION_ZG09),y) +ifeq ($(CONFIG_SENSOR_ZG09),y) SRC_DIR += zg09 endif diff --git a/Ubiquitous/XiUOS/framework/perception/co2/zg09/Makefile b/APP_Framework/Framework/sensor/co2/zg09/Makefile similarity index 100% rename from Ubiquitous/XiUOS/framework/perception/co2/zg09/Makefile rename to APP_Framework/Framework/sensor/co2/zg09/Makefile diff --git a/Ubiquitous/XiUOS/framework/perception/co2/zg09/zg09.c b/APP_Framework/Framework/sensor/co2/zg09/zg09.c similarity index 76% rename from Ubiquitous/XiUOS/framework/perception/co2/zg09/zg09.c rename to APP_Framework/Framework/sensor/co2/zg09/zg09.c index 76034ae2..887670c6 100644 --- a/Ubiquitous/XiUOS/framework/perception/co2/zg09/zg09.c +++ b/APP_Framework/Framework/sensor/co2/zg09/zg09.c @@ -20,9 +20,9 @@ #include -static uint8 zg09_set_passive[8]={0xFE, 0x06, 0x00, 0x05, 0x00, 0x00, 0x8D, 0xC4}; -static uint8 zg09_set_active[8]={0xFE, 0x06, 0x00, 0x05, 0x00, 0x01, 0x4C, 0x04}; -static uint8 zg09_read_instruction[8]={0xFE, 0x03, 0x00, 0x0B, 0x00, 0x01, 0xE1, 0xC7}; +static uint8_t zg09_set_passive[8]={0xFE, 0x06, 0x00, 0x05, 0x00, 0x00, 0x8D, 0xC4}; +static uint8_t zg09_set_active[8]={0xFE, 0x06, 0x00, 0x05, 0x00, 0x01, 0x4C, 0x04}; +static uint8_t zg09_read_instruction[8]={0xFE, 0x03, 0x00, 0x0B, 0x00, 0x01, 0xE1, 0xC7}; static struct SensorDevice zg09; @@ -36,13 +36,13 @@ static struct SensorProductInfo info = /** * @description: Open ZG09 sensor device * @param sdev - sensor device pointer - * @return success: EOK , failure: other + * @return success: 1 , failure: other */ static int SensorDeviceOpen(struct SensorDevice *sdev) { - int result = EOK; + int result = 1; - sdev->fd = open(SENSOR_DEVICE_ZG09_DEV, O_RDWR); + sdev->fd = PrivOpen(SENSOR_DEVICE_ZG09_DEV, O_RDWR); struct SerialDataCfg cfg; cfg.serial_baud_rate = BAUD_RATE_9600; @@ -55,7 +55,10 @@ static int SensorDeviceOpen(struct SensorDevice *sdev) cfg.ext_uart_no = SENSOR_DEVICE_ZG09_DEV_EXT_PORT; cfg.port_configure = PORT_CFG_INIT; - ioctl(sdev->fd, OPE_INT, &cfg); + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = &cfg; + result = PrivIoctl(sdev->fd, OPE_INT, &ioctl_cfg); sdev->done->ioctl(sdev, SENSOR_DEVICE_PASSIVE); @@ -68,12 +71,12 @@ static int SensorDeviceOpen(struct SensorDevice *sdev) * @param len - the length of the read data * @return get data length */ -static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) { - uint8 tmp = 0; + uint8_t tmp = 0; int timeout = 0; while (1) { - read(sdev->fd, &tmp, 1); + PrivRead(sdev->fd, &tmp, 1); if ((tmp == 0xFE) || (timeout >= 1000)) break; UserTaskDelay(10); @@ -81,13 +84,13 @@ static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) } if(tmp != 0xFE) - return -ERROR; + return -1; - uint8 idx = 0; + uint8_t idx = 0; sdev->buffer[idx++] = tmp; while ((idx < len) && (timeout < 1000)) { - if (read(sdev->fd, &tmp, 1) == 1) { + if (PrivRead(sdev->fd, &tmp, 1) == 1) { timeout = 0; sdev->buffer[idx++] = tmp; } @@ -101,27 +104,27 @@ static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) * @description: set sensor work mode * @param sdev - sensor device pointer * @param cmd - mode command - * @return success: EOK , failure: -ERROR + * @return success: 1 , failure: -1 */ static int SensorDeviceIoctl(struct SensorDevice *sdev, int cmd) { switch (cmd) { case SENSOR_DEVICE_PASSIVE: - write(sdev->fd, zg09_set_passive, 8); + PrivWrite(sdev->fd, zg09_set_passive, 8); sdev->done->read(sdev, 8); if (memcmp(sdev->buffer, zg09_set_passive, 8) == 0) { sdev->status = SENSOR_DEVICE_PASSIVE; - return EOK; + return 1; } break; case SENSOR_DEVICE_ACTIVE: - write(sdev->fd, zg09_set_active, 8); + PrivWrite(sdev->fd, zg09_set_active, 8); sdev->done->read(sdev, 8); if (memcmp(sdev->buffer, zg09_set_active, 8) == 0) { sdev->status = SENSOR_DEVICE_ACTIVE; - return EOK; + return 1; } break; @@ -130,15 +133,15 @@ static int SensorDeviceIoctl(struct SensorDevice *sdev, int cmd) break; } - return -ERROR; + return -1; } static struct SensorDone done = { SensorDeviceOpen, - NONE, + NULL, SensorDeviceRead, - NONE, + NULL, SensorDeviceIoctl, }; @@ -164,18 +167,18 @@ static struct SensorQuantity zg09_co2; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 QuantityRead(struct SensorQuantity *quant) +static int32_t QuantityRead(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; - uint32 result; - if (quant->sdev->done->read != NONE) { + uint32_t result; + if (quant->sdev->done->read != NULL) { if(quant->sdev->status == SENSOR_DEVICE_PASSIVE) { - write(quant->sdev->fd, zg09_read_instruction, 8); + PrivWrite(quant->sdev->fd, zg09_read_instruction, 8); quant->sdev->done->read(quant->sdev, 7); - if(Crc16(quant->sdev->buffer, 5) == ((uint32)quant->sdev->buffer[6] << 8) + ((uint32)quant->sdev->buffer[5])) { - result = (uint32)quant->sdev->buffer[3] * 256 + (uint32)quant->sdev->buffer[4]; + if(Crc16(quant->sdev->buffer, 5) == ((uint32_t)quant->sdev->buffer[6] << 8) + ((uint32_t)quant->sdev->buffer[5])) { + result = (uint32_t)quant->sdev->buffer[3] * 256 + (uint32_t)quant->sdev->buffer[4]; if (result > quant->value.max_value) quant->value.max_value = result; else if (result < quant->value.min_value) @@ -196,7 +199,7 @@ static int32 QuantityRead(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** diff --git a/Ubiquitous/XiUOS/framework/perception/humidity/Kconfig b/APP_Framework/Framework/sensor/humidity/Kconfig similarity index 100% rename from Ubiquitous/XiUOS/framework/perception/humidity/Kconfig rename to APP_Framework/Framework/sensor/humidity/Kconfig diff --git a/Ubiquitous/XiUOS/framework/perception/humidity/Makefile b/APP_Framework/Framework/sensor/humidity/Makefile similarity index 64% rename from Ubiquitous/XiUOS/framework/perception/humidity/Makefile rename to APP_Framework/Framework/sensor/humidity/Makefile index 9b1fd3bd..5e89f9cf 100644 --- a/Ubiquitous/XiUOS/framework/perception/humidity/Makefile +++ b/APP_Framework/Framework/sensor/humidity/Makefile @@ -1,4 +1,4 @@ -ifeq ($(CONFIG_PERCEPTION_HS300X),y) +ifeq ($(CONFIG_SENSOR_HS300X),y) SRC_DIR += hs300x_humi endif diff --git a/Ubiquitous/XiUOS/framework/perception/humidity/hs300x_humi/Makefile b/APP_Framework/Framework/sensor/humidity/hs300x_humi/Makefile similarity index 100% rename from Ubiquitous/XiUOS/framework/perception/humidity/hs300x_humi/Makefile rename to APP_Framework/Framework/sensor/humidity/hs300x_humi/Makefile diff --git a/Ubiquitous/XiUOS/framework/perception/humidity/hs300x_humi/hs300x_humi.c b/APP_Framework/Framework/sensor/humidity/hs300x_humi/hs300x_humi.c similarity index 84% rename from Ubiquitous/XiUOS/framework/perception/humidity/hs300x_humi/hs300x_humi.c rename to APP_Framework/Framework/sensor/humidity/hs300x_humi/hs300x_humi.c index 21571774..e9eea71a 100644 --- a/Ubiquitous/XiUOS/framework/perception/humidity/hs300x_humi/hs300x_humi.c +++ b/APP_Framework/Framework/sensor/humidity/hs300x_humi/hs300x_humi.c @@ -32,41 +32,41 @@ static struct SensorProductInfo info = /** * @description: Open HS300x sensor device * @param sdev - sensor device pointer - * @return EOK + * @return 1 */ static int SensorDeviceOpen(struct SensorDevice *sdev) { - sdev->fd = open(SENSOR_DEVICE_HS300X_DEV, O_RDWR); + sdev->fd = PrivOpen(SENSOR_DEVICE_HS300X_DEV, O_RDWR); - return EOK; + return 1; } /** * @description: Read sensor device * @param sdev - sensor device pointer * @param len - the length of the read data - * @return success: EOK , failure: -ERROR + * @return success: 1 , failure: -1 */ -static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) { - if (write(sdev->fd, NONE, 0) != 1) - return -ERROR; + if (PrivWrite(sdev->fd, NULL, 0) != 1) + return -1; UserTaskDelay(50); - if (read(sdev->fd, sdev->buffer, len) != 1) - return -ERROR; + if (PrivRead(sdev->fd, sdev->buffer, len) != 1) + return -1; - return EOK; + return 1; } static struct SensorDone done = { SensorDeviceOpen, - NONE, + NULL, SensorDeviceRead, - NONE, - NONE, + NULL, + NULL, }; /** @@ -92,19 +92,19 @@ static struct SensorQuantity hs300x_humidity; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 ReadHumidity(struct SensorQuantity *quant) +static int32_t ReadHumidity(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; float result = 0.0; - if (quant->sdev->done->read != NONE) { + if (quant->sdev->done->read != NULL) { if (quant->sdev->status == SENSOR_DEVICE_PASSIVE) { quant->sdev->done->read(quant->sdev, 4); quant->sdev->done->read(quant->sdev, 4); /* It takes two reads to get the data right */ result = ((quant->sdev->buffer[0] << 8 | quant->sdev->buffer[1] ) & 0x3fff) * 100.0 / ( (1 << 14) - 1); - return (int32)(result * 10); + return (int32_t)(result * 10); } if (quant->sdev->status == SENSOR_DEVICE_ACTIVE) { printf("Please set passive mode.\n"); @@ -113,7 +113,7 @@ static int32 ReadHumidity(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** diff --git a/Ubiquitous/XiUOS/framework/perception/pm/Kconfig b/APP_Framework/Framework/sensor/pm/Kconfig similarity index 95% rename from Ubiquitous/XiUOS/framework/perception/pm/Kconfig rename to APP_Framework/Framework/sensor/pm/Kconfig index f1fd0295..11a3d549 100644 --- a/Ubiquitous/XiUOS/framework/perception/pm/Kconfig +++ b/APP_Framework/Framework/sensor/pm/Kconfig @@ -1,9 +1,9 @@ -config PERCEPTION_PS5308 +config SENSOR_PS5308 bool "Using PS5308" default n - if PERCEPTION_PS5308 + if SENSOR_PS5308 config SENSOR_DEVICE_PS5308 string "PS5308 sensor name" default "ps5308_1" diff --git a/Ubiquitous/XiUOS/framework/perception/pm/Makefile b/APP_Framework/Framework/sensor/pm/Makefile similarity index 62% rename from Ubiquitous/XiUOS/framework/perception/pm/Makefile rename to APP_Framework/Framework/sensor/pm/Makefile index 9715bbce..fb6558a0 100644 --- a/Ubiquitous/XiUOS/framework/perception/pm/Makefile +++ b/APP_Framework/Framework/sensor/pm/Makefile @@ -1,4 +1,4 @@ -ifeq ($(CONFIG_PERCEPTION_PS5308),y) +ifeq ($(CONFIG_SENSOR_PS5308),y) SRC_DIR += ps5308 endif diff --git a/Ubiquitous/XiUOS/framework/perception/pm/ps5308/Makefile b/APP_Framework/Framework/sensor/pm/ps5308/Makefile similarity index 100% rename from Ubiquitous/XiUOS/framework/perception/pm/ps5308/Makefile rename to APP_Framework/Framework/sensor/pm/ps5308/Makefile diff --git a/Ubiquitous/XiUOS/framework/perception/pm/ps5308/ps5308.c b/APP_Framework/Framework/sensor/pm/ps5308/ps5308.c similarity index 85% rename from Ubiquitous/XiUOS/framework/perception/pm/ps5308/ps5308.c rename to APP_Framework/Framework/sensor/pm/ps5308/ps5308.c index a7f2d9ca..8b1e790d 100644 --- a/Ubiquitous/XiUOS/framework/perception/pm/ps5308/ps5308.c +++ b/APP_Framework/Framework/sensor/pm/ps5308/ps5308.c @@ -48,11 +48,11 @@ static void ReadTask(struct SensorDevice *sdev) /** * @description: Open PS5308 sensor device * @param sdev - sensor device pointer - * @return success: EOK , failure: other + * @return success: 1 , failure: other */ static int SensorDeviceOpen(struct SensorDevice *sdev) { - int result = EOK; + int result = 1; buff_lock = UserMutexCreate(); @@ -89,13 +89,13 @@ static int SensorDeviceOpen(struct SensorDevice *sdev) /** * @description: Close PS5308 sensor device * @param sdev - sensor device pointer - * @return EOK + * @return 1 */ static int SensorDeviceClose(struct SensorDevice *sdev) { UserTaskDelete(active_task_id); UserMutexDelete(buff_lock); - return EOK; + return 1; } /** @@ -104,9 +104,9 @@ static int SensorDeviceClose(struct SensorDevice *sdev) * @param len - the length of the read data * @return get data length */ -static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) { - uint8 tmp = 0; + uint8_t tmp = 0; int timeout = 0; while (1) { read(sdev->fd, &tmp, 1); @@ -117,9 +117,9 @@ static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) } if (tmp != 0x44) - return -ERROR; + return -1; - uint8 idx = 0; + uint8_t idx = 0; sdev->buffer[idx++] = tmp; while ((idx < len) && (timeout < 1000)) { @@ -138,8 +138,8 @@ static struct SensorDone SensorDeviceDone = SensorDeviceOpen, SensorDeviceClose, SensorDeviceRead, - NONE, - NONE, + NULL, + NULL, }; /** @@ -165,21 +165,21 @@ static struct SensorQuantity ps5308_pm1_0; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 ReadPm1_0(struct SensorQuantity *quant) +static int32_t ReadPm1_0(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; - uint32 result; - if (quant->sdev->done->read != NONE) { - uint16 checksum = 0; + uint32_t result; + if (quant->sdev->done->read != NULL) { + uint16_t checksum = 0; UserMutexObtain(buff_lock, WAITING_FOREVER); - for (uint8 i = 0; i < 30; i++) + for (uint8_t i = 0; i < 30; i++) checksum += quant->sdev->buffer[i]; - if (checksum == (((uint16)quant->sdev->buffer[30] << 8) + ((uint16)quant->sdev->buffer[31]))) { - result = ((uint16)quant->sdev->buffer[4] << 8) + (uint16)quant->sdev->buffer[5]; + if (checksum == (((uint16_t)quant->sdev->buffer[30] << 8) + ((uint16_t)quant->sdev->buffer[31]))) { + result = ((uint16_t)quant->sdev->buffer[4] << 8) + (uint16_t)quant->sdev->buffer[5]; if (result > quant->value.max_value) quant->value.max_value = result; else if (result < quant->value.min_value) @@ -196,7 +196,7 @@ static int32 ReadPm1_0(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** @@ -232,21 +232,21 @@ static struct SensorQuantity ps5308_pm2_5; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 ReadPm2_5(struct SensorQuantity *quant) +static int32_t ReadPm2_5(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; - uint32 result; - if (quant->sdev->done->read != NONE) { - uint16 checksum = 0; + uint32_t result; + if (quant->sdev->done->read != NULL) { + uint16_t checksum = 0; UserMutexObtain(buff_lock, WAITING_FOREVER); for (uint i = 0; i < 30; i++) checksum += quant->sdev->buffer[i]; - if (checksum == (((uint16)quant->sdev->buffer[30] << 8) + ((uint16)quant->sdev->buffer[31]))) { - result = ((uint16)quant->sdev->buffer[6] << 8) + (uint16)quant->sdev->buffer[7]; + if (checksum == (((uint16_t)quant->sdev->buffer[30] << 8) + ((uint16_t)quant->sdev->buffer[31]))) { + result = ((uint16_t)quant->sdev->buffer[6] << 8) + (uint16_t)quant->sdev->buffer[7]; if (result > quant->value.max_value) quant->value.max_value = result; else if (result < quant->value.min_value) @@ -263,7 +263,7 @@ static int32 ReadPm2_5(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** @@ -299,21 +299,21 @@ static struct SensorQuantity ps5308_pm10; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 ReadPm10(struct SensorQuantity *quant) +static int32_t ReadPm10(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; - uint32 result; - if (quant->sdev->done->read != NONE) { - uint16 checksum = 0; + uint32_t result; + if (quant->sdev->done->read != NULL) { + uint16_t checksum = 0; UserMutexObtain(buff_lock, WAITING_FOREVER); for (uint i = 0; i < 30; i++) checksum += quant->sdev->buffer[i]; - if (checksum == (((uint16)quant->sdev->buffer[30] << 8) + ((uint16)quant->sdev->buffer[31]))) { - result = ((uint16)quant->sdev->buffer[8] << 8) + (uint16)quant->sdev->buffer[9]; + if (checksum == (((uint16_t)quant->sdev->buffer[30] << 8) + ((uint16_t)quant->sdev->buffer[31]))) { + result = ((uint16_t)quant->sdev->buffer[8] << 8) + (uint16_t)quant->sdev->buffer[9]; if (result > quant->value.max_value) quant->value.max_value = result; else if (result < quant->value.min_value) @@ -330,7 +330,7 @@ static int32 ReadPm10(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** diff --git a/Ubiquitous/XiUOS/framework/perception/temperature/Kconfig b/APP_Framework/Framework/sensor/temperature/Kconfig similarity index 90% rename from Ubiquitous/XiUOS/framework/perception/temperature/Kconfig rename to APP_Framework/Framework/sensor/temperature/Kconfig index 7cd0e991..028b508d 100644 --- a/Ubiquitous/XiUOS/framework/perception/temperature/Kconfig +++ b/APP_Framework/Framework/sensor/temperature/Kconfig @@ -1,9 +1,9 @@ -config PERCEPTION_HS300X +config SENSOR_HS300X bool "Using HS300x" default n - if PERCEPTION_HS300X + if SENSOR_HS300X config SENSOR_DEVICE_HS300X string "HS300x sensor name" default "hs300x_1" diff --git a/Ubiquitous/XiUOS/framework/perception/temperature/Makefile b/APP_Framework/Framework/sensor/temperature/Makefile similarity index 64% rename from Ubiquitous/XiUOS/framework/perception/temperature/Makefile rename to APP_Framework/Framework/sensor/temperature/Makefile index 217867cc..b7e113e3 100644 --- a/Ubiquitous/XiUOS/framework/perception/temperature/Makefile +++ b/APP_Framework/Framework/sensor/temperature/Makefile @@ -1,4 +1,4 @@ -ifeq ($(CONFIG_PERCEPTION_HS300X),y) +ifeq ($(CONFIG_SENSOR_HS300X),y) SRC_DIR += hs300x_temp endif diff --git a/Ubiquitous/XiUOS/framework/perception/temperature/hs300x_temp/Makefile b/APP_Framework/Framework/sensor/temperature/hs300x_temp/Makefile similarity index 100% rename from Ubiquitous/XiUOS/framework/perception/temperature/hs300x_temp/Makefile rename to APP_Framework/Framework/sensor/temperature/hs300x_temp/Makefile diff --git a/Ubiquitous/XiUOS/framework/perception/temperature/hs300x_temp/hs300x_temp.c b/APP_Framework/Framework/sensor/temperature/hs300x_temp/hs300x_temp.c similarity index 84% rename from Ubiquitous/XiUOS/framework/perception/temperature/hs300x_temp/hs300x_temp.c rename to APP_Framework/Framework/sensor/temperature/hs300x_temp/hs300x_temp.c index 0d3ae703..dcb2fa25 100644 --- a/Ubiquitous/XiUOS/framework/perception/temperature/hs300x_temp/hs300x_temp.c +++ b/APP_Framework/Framework/sensor/temperature/hs300x_temp/hs300x_temp.c @@ -32,41 +32,41 @@ static struct SensorProductInfo info = /** * @description: Open HS300x sensor device * @param sdev - sensor device pointer - * @return EOK + * @return 1 */ static int SensorDeviceOpen(struct SensorDevice *sdev) { - sdev->fd = open(SENSOR_DEVICE_HS300X_DEV, O_RDWR); + sdev->fd = PrivOpen(SENSOR_DEVICE_HS300X_DEV, O_RDWR); - return EOK; + return 1; } /** * @description: Read sensor device * @param sdev - sensor device pointer * @param len - the length of the read data - * @return success: EOK , failure: -ERROR + * @return success: 1 , failure: -1 */ -static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) { - if (write(sdev->fd, NONE, 0) != 1) - return -ERROR; + if (PrivWrite(sdev->fd, NULL, 0) != 1) + return -1; UserTaskDelay(50); - if (read(sdev->fd, sdev->buffer, len) != 1) - return -ERROR; + if (PrivRead(sdev->fd, sdev->buffer, len) != 1) + return -1; - return EOK; + return 1; } static struct SensorDone done = { SensorDeviceOpen, - NONE, + NULL, SensorDeviceRead, - NONE, - NONE, + NULL, + NULL, }; /** @@ -92,19 +92,19 @@ static struct SensorQuantity hs300x_temperature; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 ReadTemperature(struct SensorQuantity *quant) +static int32_t ReadTemperature(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; float result; - if (quant->sdev->done->read != NONE) { + if (quant->sdev->done->read != NULL) { if (quant->sdev->status == SENSOR_DEVICE_PASSIVE) { quant->sdev->done->read(quant->sdev, 4); quant->sdev->done->read(quant->sdev, 4); /* It takes two reads to get the data right */ result = ((quant->sdev->buffer[2] << 8 | quant->sdev->buffer[3]) >> 2) * 165.0 /( (1 << 14) - 1) - 40.0; - return (int32)(result * 10); + return (int32_t)(result * 10); } if (quant->sdev->status == SENSOR_DEVICE_ACTIVE) { printf("Please set passive mode.\n"); @@ -113,7 +113,7 @@ static int32 ReadTemperature(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** diff --git a/Ubiquitous/XiUOS/framework/perception/voice/Kconfig b/APP_Framework/Framework/sensor/voice/Kconfig similarity index 93% rename from Ubiquitous/XiUOS/framework/perception/voice/Kconfig rename to APP_Framework/Framework/sensor/voice/Kconfig index 2416d51b..944d2807 100644 --- a/Ubiquitous/XiUOS/framework/perception/voice/Kconfig +++ b/APP_Framework/Framework/sensor/voice/Kconfig @@ -1,9 +1,9 @@ -config PERCEPTION_D124 +config SENSOR_D124 bool "Using D124" default n - if PERCEPTION_D124 + if SENSOR_D124 config SENSOR_DEVICE_D124 string "D124 sensor name" default "d124_1" diff --git a/Ubiquitous/XiUOS/framework/perception/voice/Makefile b/APP_Framework/Framework/sensor/voice/Makefile similarity index 62% rename from Ubiquitous/XiUOS/framework/perception/voice/Makefile rename to APP_Framework/Framework/sensor/voice/Makefile index 052e92a6..a7c40567 100644 --- a/Ubiquitous/XiUOS/framework/perception/voice/Makefile +++ b/APP_Framework/Framework/sensor/voice/Makefile @@ -1,4 +1,4 @@ -ifeq ($(CONFIG_PERCEPTION_D124),y) +ifeq ($(CONFIG_SENSOR_D124),y) SRC_DIR += d124 endif diff --git a/Ubiquitous/XiUOS/framework/perception/voice/d124/Makefile b/APP_Framework/Framework/sensor/voice/d124/Makefile similarity index 100% rename from Ubiquitous/XiUOS/framework/perception/voice/d124/Makefile rename to APP_Framework/Framework/sensor/voice/d124/Makefile diff --git a/Ubiquitous/XiUOS/framework/perception/voice/d124/d124.c b/APP_Framework/Framework/sensor/voice/d124/d124.c similarity index 85% rename from Ubiquitous/XiUOS/framework/perception/voice/d124/d124.c rename to APP_Framework/Framework/sensor/voice/d124/d124.c index 7367fa32..a5f56a51 100644 --- a/Ubiquitous/XiUOS/framework/perception/voice/d124/d124.c +++ b/APP_Framework/Framework/sensor/voice/d124/d124.c @@ -48,15 +48,15 @@ static void ReadTask(struct SensorDevice *sdev) /** * @description: Open D124 voice device * @param sdev - sensor device pointer - * @return success: EOK , failure: other + * @return success: 1 , failure: other */ static int SensorDeviceOpen(struct SensorDevice *sdev) { - int result = EOK; + int result = 1; buff_lock = UserMutexCreate(); - sdev->fd = open(SENSOR_DEVICE_D124_DEV, O_RDWR); + sdev->fd = PrivOpen(SENSOR_DEVICE_D124_DEV, O_RDWR); struct SerialDataCfg cfg; cfg.serial_baud_rate = BAUD_RATE_9600; @@ -69,7 +69,10 @@ static int SensorDeviceOpen(struct SensorDevice *sdev) cfg.ext_uart_no = SENSOR_DEVICE_D124_DEV_EXT_PORT; cfg.port_configure = PORT_CFG_INIT; - result = ioctl(sdev->fd, OPE_INT, &cfg); + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = &cfg; + result = PrivIoctl(sdev->fd, OPE_INT, &ioctl_cfg); UtaskType active_task; const char name[NAME_NUM_MAX] = "d124_task"; @@ -89,13 +92,13 @@ static int SensorDeviceOpen(struct SensorDevice *sdev) /** * @description: Close D124 sensor device * @param sdev - sensor device pointer - * @return EOK + * @return 1 */ static int SensorDeviceClose(struct SensorDevice *sdev) { UserTaskDelete(active_task_id); UserMutexDelete(buff_lock); - return EOK; + return 1; } /** @@ -104,12 +107,12 @@ static int SensorDeviceClose(struct SensorDevice *sdev) * @param len - the length of the read data * @return get data length */ -static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) { - uint8 tmp = 0; + uint8_t tmp = 0; int timeout = 0; while (1) { - read(sdev->fd, &tmp, 1); + PrivRead(sdev->fd, &tmp, 1); if ((tmp == 0xAA) || (timeout >= 1000)) break; UserTaskDelay(10); @@ -117,13 +120,13 @@ static x_size_t SensorDeviceRead(struct SensorDevice *sdev, size_t len) } if(tmp != 0xAA) - return -ERROR; + return -1; - uint8 idx = 0; + uint8_t idx = 0; sdev->buffer[idx++] = tmp; while ((idx < len) && (timeout < 1000)) { - if (read(sdev->fd, &tmp, 1) == 1) { + if (PrivRead(sdev->fd, &tmp, 1) == 1) { timeout = 0; sdev->buffer[idx++] = tmp; } @@ -138,8 +141,8 @@ static struct SensorDone done = SensorDeviceOpen, SensorDeviceClose, SensorDeviceRead, - NONE, - NONE, + NULL, + NULL, }; /** @@ -164,17 +167,17 @@ static struct SensorQuantity d124_voice; * @param quant - sensor quantity pointer * @return quantity value */ -static int32 ReadVoice(struct SensorQuantity *quant) +static int32_t ReadVoice(struct SensorQuantity *quant) { if (!quant) - return -ERROR; + return -1; - uint32 result; - if (quant->sdev->done->read != NONE) { + uint32_t result; + if (quant->sdev->done->read != NULL) { UserMutexObtain(buff_lock, WAITING_FOREVER); if (quant->sdev->buffer[3] == quant->sdev->buffer[1] + quant->sdev->buffer[2]) { - result = ((uint16)quant->sdev->buffer[1] << 8) + (uint16)quant->sdev->buffer[2]; + result = ((uint16_t)quant->sdev->buffer[1] << 8) + (uint16_t)quant->sdev->buffer[2]; if (result > quant->value.max_value) quant->value.max_value = result; else if (result < quant->value.min_value) @@ -191,7 +194,7 @@ static int32 ReadVoice(struct SensorQuantity *quant) printf("%s don't have read done.\n", quant->name); } - return -ERROR; + return -1; } /** diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.c b/APP_Framework/Framework/transform_layer/xiuos/transform.c index faaddc26..59254bca 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.c +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.c @@ -26,50 +26,6 @@ #define XIUOS_OS #ifdef XIUOS_OS -/************************Kernel Posix Transform***********************/ - - -/************************Driver Posix Transform***********************/ -int PrivOpen(const char *path, int flags, ...) -{ - return open(path, flags, ...); -} - -int PrivClose(int fd) -{ - return close(fd); -} - -int PrivRead(int fd, void *buf, size_t len) -{ - return read(fd, buf, len); -} - -int PrivWrite(int fd, const void *buf, size_t len) -{ - return write(fd, buf, len); -} - -static int PrivSerialIoctl(int fd, void *args) -{ - struct SerialDataCfg *serial_cfg = (struct SerialDataCfg *)args; - - return ioctl(fd, OPE_INT, &serial_cfg); -} - -int PrivIoctl(int fd, int cmd, void *args) -{ - struct PrivIoctlCfg *ioctl_cfg = (struct PrivIoctlCfg *)args; - - switch (ioctl_cfg->ioctl_driver_type) - { - case SERIAL_TYPE: - PrivSerialIoctl(fd, ioctl_cfg->args); - break; - default: - break; - } -} /* private mutex API */ int PrivMutexCreate(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *attr) @@ -168,11 +124,11 @@ int PrivWrite(int fd, const void *buf, size_t len) return write(fd, buf, len); } -static int PrivSerialIoctl(int fd, void *args) +static int PrivSerialIoctl(int fd, int cmd, void *args) { struct SerialDataCfg *serial_cfg = (struct SerialDataCfg *)args; - return ioctl(fd, OPE_INT, &serial_cfg); + return ioctl(fd, cmd, &serial_cfg); } int PrivIoctl(int fd, int cmd, void *args) @@ -182,7 +138,7 @@ int PrivIoctl(int fd, int cmd, void *args) switch (ioctl_cfg->ioctl_driver_type) { case SERIAL_TYPE: - PrivSerialIoctl(fd, ioctl_cfg->args); + PrivSerialIoctl(fd, cmd, ioctl_cfg->args); break; default: break; @@ -206,4 +162,6 @@ void *PrivCalloc(size_t count, size_t size) void PrivFree(void *pointer) { UserFree(pointer); -} \ No newline at end of file +} + +#endif diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.h b/APP_Framework/Framework/transform_layer/xiuos/transform.h index fc7ec67d..8e5df63a 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.h +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.h @@ -75,6 +75,14 @@ extern "C" { #define SERIAL_RB_BUFSZ 128 #endif +enum ExtSerialPortConfigure +{ + PORT_CFG_INIT = 0, + PORT_CFG_PARITY_CHECK, + PORT_CFG_DISABLE, + PORT_CFG_DIV, +}; + struct SerialDataCfg { uint32_t serial_baud_rate; @@ -84,6 +92,9 @@ struct SerialDataCfg uint8_t serial_bit_order; uint8_t serial_invert_mode; uint16_t serial_buffer_size; + + uint8 ext_uart_no; + enum ExtSerialPortConfigure port_configure; }; enum IoctlDriverType diff --git a/Ubiquitous/XiUOS/framework/Kconfig b/Ubiquitous/XiUOS/framework/Kconfig index 420b3ac4..6fe49559 100644 --- a/Ubiquitous/XiUOS/framework/Kconfig +++ b/Ubiquitous/XiUOS/framework/Kconfig @@ -1,7 +1,5 @@ menu "APP Framework" -source "$KERNEL_DIR/framework/perception/Kconfig" - source "$KERNEL_DIR/framework/connection/Kconfig" source "$KERNEL_DIR/framework/intelligent/Kconfig" diff --git a/Ubiquitous/XiUOS/framework/Makefile b/Ubiquitous/XiUOS/framework/Makefile index c06ed4a9..e6b702ac 100644 --- a/Ubiquitous/XiUOS/framework/Makefile +++ b/Ubiquitous/XiUOS/framework/Makefile @@ -1,3 +1,3 @@ -SRC_DIR := connection perception intelligent security +SRC_DIR := connection intelligent security include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/framework/perception/Kconfig b/Ubiquitous/XiUOS/framework/perception/Kconfig deleted file mode 100644 index e52c38c7..00000000 --- a/Ubiquitous/XiUOS/framework/perception/Kconfig +++ /dev/null @@ -1,46 +0,0 @@ - -menu "Perception" - -menuconfig PERCEPTION_SENSORDEVICE - bool "Using sensor devices" - default n - -if PERCEPTION_SENSORDEVICE - menuconfig PERCEPTION_CO2 - bool "Using CO2 sensor device" - default n - if PERCEPTION_CO2 - source "$KERNEL_DIR/framework/perception/co2/Kconfig" - endif - - menuconfig PERCEPTION_PM - bool "Using PM sensor device" - default n - if PERCEPTION_PM - source "$KERNEL_DIR/framework/perception/pm/Kconfig" - endif - - menuconfig PERCEPTION_VOICE - bool "Using voice sensor device" - default n - if PERCEPTION_VOICE - source "$KERNEL_DIR/framework/perception/voice/Kconfig" - endif - - menuconfig PERCEPTION_TEMPERATURE - bool "Using temperature sensor device" - default n - if PERCEPTION_TEMPERATURE - source "$KERNEL_DIR/framework/perception/temperature/Kconfig" - endif - - menuconfig PERCEPTION_HUMIDITY - bool "Using humidity sensor device" - default n - if PERCEPTION_HUMIDITY - source "$KERNEL_DIR/framework/perception/humidity/Kconfig" - endif -endif - -endmenu - diff --git a/Ubiquitous/XiUOS/framework/perception/Makefile b/Ubiquitous/XiUOS/framework/perception/Makefile deleted file mode 100644 index 3fb2af35..00000000 --- a/Ubiquitous/XiUOS/framework/perception/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -ifeq ($(CONFIG_PERCEPTION_SENSORDEVICE),y) - SRC_FILES := sensor.c -endif - -ifeq ($(CONFIG_PERCEPTION_CO2),y) - SRC_DIR += co2 -endif - -ifeq ($(CONFIG_PERCEPTION_PM),y) - SRC_DIR += pm -endif - -ifeq ($(CONFIG_PERCEPTION_VOICE),y) - SRC_DIR += voice -endif - -ifeq ($(CONFIG_PERCEPTION_TEMPERATURE),y) - SRC_DIR += temperature -endif - -ifeq ($(CONFIG_PERCEPTION_HUMIDITY),y) - SRC_DIR += humidity -endif - -include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiUOS/framework/perception/sensor.c b/Ubiquitous/XiUOS/framework/perception/sensor.c deleted file mode 100644 index 284dbfee..00000000 --- a/Ubiquitous/XiUOS/framework/perception/sensor.c +++ /dev/null @@ -1,393 +0,0 @@ -/* -* Copyright (c) 2020 AIIT XUOS Lab -* XiUOS is licensed under Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* http://license.coscl.org.cn/MulanPSL2 -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -/** - * @file sensor.c - * @brief Implement the sensor framework management, registration and done - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2021.03.24 - */ - -#include - -/* Sensor quantity list table */ -static DoubleLinklistType quant_table[SENSOR_QUANTITY_END]; - -/* Sensor device list */ -static DoubleLinklistType sensor_device_list; - -/* Sensor quantity list lock */ -static int quant_table_lock; - -/* Sensor device list lock */ -static int sensor_device_list_lock; - -/** - * @description: Init perception framework - * @return 0 - */ -int SensorFrameworkInit(void) -{ - for (int i = 0; i < SENSOR_QUANTITY_END; i++) - InitDoubleLinkList(&quant_table[i]); - InitDoubleLinkList(&sensor_device_list); - - quant_table_lock = UserMutexCreate(); - sensor_device_list_lock = UserMutexCreate(); - - return 0; -} - -/* ============================= Sensor device interface operations ============================= */ - -/** - * @description: Find sensor device by name - * @param name - name string - * @return sensor device pointer - */ -static struct SensorDevice *SensorDeviceFindByName(const char *name) -{ - struct SensorDevice *ret = NONE; - struct SysDoubleLinklistNode *node; - - if (name == NONE) - return NONE; - - UserMutexObtain(sensor_device_list_lock, WAITING_FOREVER); - DOUBLE_LINKLIST_FOR_EACH(node, &sensor_device_list) { - struct SensorDevice *sdev =CONTAINER_OF(node, - struct SensorDevice, link); - if (strncmp(sdev->name, name, NAME_NUM_MAX) == 0) { - ret = sdev; - break; - } - } - UserMutexAbandon(sensor_device_list_lock); - - return ret; -} - -/** - * @description: Check whether the sensor is capable - * @param sdev - sensor device pointer - * @param ability - the ability to detect certain data - * @return success: true , failure: false - */ -inline int SensorDeviceCheckAbility(struct SensorDevice *sdev, uint32_t ability) -{ - return (sdev->info->ability & ability) != 0; -} - -/** - * @description: Register the sensor to the linked list - * @param sdev - sensor device pointer - * @return success: EOK , failure: -ERROR - */ -int SensorDeviceRegister(struct SensorDevice *sdev) -{ - if (sdev == NONE) - return -ERROR; - - if (SensorDeviceFindByName(sdev->name) != NONE) { - printf("%s: sensor with the same name already registered\n", __func__); - return -ERROR; - } - - sdev->ref_cnt = 0; - InitDoubleLinkList(&sdev->quant_list); - - UserMutexObtain(sensor_device_list_lock, WAITING_FOREVER); - DoubleLinkListInsertNodeAfter(&sensor_device_list, &sdev->link); - UserMutexAbandon(sensor_device_list_lock); - - return EOK; -} - -/** - * @description: Unregister the sensor from the linked list - * @param sdev - sensor device pointer - * @return EOK - */ -int SensorDeviceUnregister(struct SensorDevice *sdev) -{ - if (!sdev) - return -ERROR; - UserMutexObtain(sensor_device_list_lock, WAITING_FOREVER); - DoubleLinkListRmNode(&sdev->link); - UserMutexAbandon(sensor_device_list_lock); - - return EOK; -} - -/** - * @description: Open sensor device - * @param sdev - sensor device pointer - * @return success: EOK , failure: other - */ -static int SensorDeviceOpen(struct SensorDevice *sdev) -{ - if (!sdev) - return -ERROR; - - int result = EOK; - - if (sdev->done->open != NONE) - result = sdev->done->open(sdev); - - if (result == EOK) { - printf("Device %s open success.\n", sdev->name); - }else{ - if (sdev->fd) - close(sdev->fd); - - printf("Device %s open failed(%d).\n", sdev->name, result); - memset(sdev, 0, sizeof(struct SensorDevice)); - } - - return result; -} - -/** - * @description: Close sensor device - * @param sdev - sensor device pointer - * @return success: EOK , failure: other - */ -static int SensorDeviceClose(struct SensorDevice *sdev) -{ - int result = EOK; - - if (sdev->fd) - close(sdev->fd); - - if (sdev->done->close != NONE) - result = sdev->done->close(sdev); - - if (result == EOK) - printf("%s successfully closed.\n", sdev->name); - else - printf("Closed %s failure.\n", sdev->name); - - return result; -} - -/* ============================= Sensor quantity interface operations ============================= */ - -/** - * @description: Find sensor quantity by name - * @param name - name string - * @param type - the quantity required - * @return sensor quantity pointer - */ -struct SensorQuantity *SensorQuantityFind(const char *name, - enum SensorQuantityType type) -{ - struct SensorQuantity *ret = NONE; - struct SysDoubleLinklistNode *node; - - if (name == NONE || type < 0 || type >= SENSOR_QUANTITY_END) - return NONE; - - UserMutexObtain(quant_table_lock, WAITING_FOREVER); - DOUBLE_LINKLIST_FOR_EACH(node, &quant_table[type]) { - struct SensorQuantity *quant =CONTAINER_OF(node, - struct SensorQuantity, link); - if (strncmp(quant->name, name, NAME_NUM_MAX) == 0) { - ret = quant; - break; - } - } - UserMutexAbandon(quant_table_lock); - - return ret; -} - -/** - * @description: Register the quantity to the linked list - * @param quant - sensor quantity pointer - * @return success: EOK , failure: -ERROR - */ -int SensorQuantityRegister(struct SensorQuantity *quant) -{ - if (quant == NONE) - return -ERROR; - - if (SensorDeviceFindByName(quant->sdev->name) == NONE) { - if(SensorDeviceRegister(quant->sdev) != EOK) - return -ERROR; - } - - UserMutexObtain(quant_table_lock, WAITING_FOREVER); - DoubleLinkListInsertNodeAfter(&quant->sdev->quant_list, &quant->quant_link); - DoubleLinkListInsertNodeAfter(&quant_table[quant->type], &quant->link); - UserMutexAbandon(quant_table_lock); - - return EOK; -} - -/** - * @description: Unregister the quantity from the linked list - * @param quant - sensor quantity pointer - * @return EOK - */ -int SensorQuantityUnregister(struct SensorQuantity *quant) -{ - if (!quant) - return -ERROR; - UserMutexObtain(quant_table_lock, WAITING_FOREVER); - DoubleLinkListRmNode(&quant->quant_link); - DoubleLinkListRmNode(&quant->link); - UserMutexAbandon(quant_table_lock); - - return EOK; -} - -/** - * @description: Open the sensor quantity - * @param quant - sensor quantity pointer - * @return success: EOK , failure: other - */ -int SensorQuantityOpen(struct SensorQuantity *quant) -{ - if (!quant) - return -ERROR; - - int ret = EOK; - struct SensorDevice *sdev = quant->sdev; - - if (!sdev) - return -ERROR; - - if (sdev->ref_cnt == 0) { - ret = SensorDeviceOpen(sdev); - if (ret != EOK) { - printf("%s: open sensor device failed\n", __func__); - return ret; - } - } - sdev->ref_cnt++; - - return ret; -} - -/** - * @description: Close sensor quantity - * @param quant - sensor quantity pointer - * @return success: EOK , failure: other - */ -int SensorQuantityClose(struct SensorQuantity *quant) -{ - if (!quant) - return -ERROR; - - int ret = EOK; - struct SensorDevice *sdev = quant->sdev; - - if (!sdev) - return -ERROR; - - if (sdev->ref_cnt == 0) - return ret; - - sdev->ref_cnt--; - if (sdev->ref_cnt == 0) - ret = SensorDeviceClose(sdev); - - return ret; -} - -/** - * @description: Read quantity current value - * @param quant - sensor quantity pointer - * @return quantity value - */ -int32 SensorQuantityRead(struct SensorQuantity *quant) -{ - if (!quant) - return -ERROR; - - int32 result = 0; - struct SensorDevice *sdev = quant->sdev; - - if (!sdev) - return -ERROR; - - if (quant->ReadValue != NONE) { - result = quant->ReadValue(quant); - } - - return result; -} - -/** - * @description: Configure quantity mode - * @param quant - sensor quantity pointer - * @param cmd - mode command - * @return success: EOK , failure: other - */ -int SensorQuantityControl(struct SensorQuantity *quant, int cmd) -{ - if (!quant) - return -ERROR; - - if (quant->sdev->done->ioctl != NONE) { - return quant->sdev->done->ioctl(quant->sdev, cmd); - } - - return -ENONESYS; -} - - -/* ============================= Check function ============================= */ - -/** - * @description: CRC16 check - * @param data sensor receive buffer - * @param length sensor receive buffer minus check code - * @return check code - */ -uint32 Crc16(uint8 * data, uint8 length) -{ - int j; - unsigned int reg_crc=0xFFFF; - - while (length--) { - reg_crc ^= *data++; - for (j=0;j<8;j++) { - if(reg_crc & 0x01) - reg_crc=reg_crc >>1 ^ 0xA001; - else - reg_crc=reg_crc >>1; - } - } - - return reg_crc; -} - -/** - * @description: The checksum - * @param data sensor receive buffer - * @param head not check head length - * @param length sensor receive buffer minus check code - * @return check code - */ -uint8 GetCheckSum(uint8 *data, uint8 head, uint8 length) -{ - uint8 i; - uint8 checksum = 0; - for (i = head; i < length; i++) { - checksum += data[i]; - } - checksum = ~checksum + 1; - - return checksum; -} diff --git a/Ubiquitous/XiUOS/framework/perception/sensor.h b/Ubiquitous/XiUOS/framework/perception/sensor.h deleted file mode 100644 index a4e57123..00000000 --- a/Ubiquitous/XiUOS/framework/perception/sensor.h +++ /dev/null @@ -1,128 +0,0 @@ -/* -* Copyright (c) 2020 AIIT XUOS Lab -* XiUOS is licensed under Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* http://license.coscl.org.cn/MulanPSL2 -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -/** - * @file sensor.h - * @brief Structure and function declarations of the sensor framework - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2021.03.24 - */ - -#ifndef SENSOR_H -#define SENSOR_H - -#include -#include -#include -#include -#include - -#define SENSOR_QUANTITY_VALUE_ERROR ((uint32)0xffffffff) - -/* Sensor quantity report mode */ -#define SENSOR_DEVICE_PASSIVE 0x00 -#define SENSOR_DEVICE_ACTIVE 0x01 - -#define SENSOR_RECEIVE_BUFFSIZE 32 - - -#if SENSOR_TYPE_END > 32 -#error "Too many sensor types" -#endif - -/* Sensor ability */ -#define SENSOR_ABILITY_CO2 ((uint32_t)(1 << SENSOR_QUANTITY_CO2)) -#define SENSOR_ABILITY_TEMP ((uint32_t)(1 << SENSOR_QUANTITY_TEMP)) -#define SENSOR_ABILITY_HUMI ((uint32_t)(1 << SENSOR_QUANTITY_HUMI)) -#define SENSOR_ABILITY_HCHO ((uint32_t)(1 << SENSOR_QUANTITY_HCHO)) -#define SENSOR_ABILITY_CO ((uint32_t)(1 << SENSOR_QUANTITY_CO)) -#define SENSOR_ABILITY_PM ((uint32_t)(1 << SENSOR_QUANTITY_PM)) -#define SENSOR_ABILITY_VOICE ((uint32_t)(1 << SENSOR_QUANTITY_VOICE)) - -struct SensorProductInfo { - uint32_t ability; /* Bitwise OR of sensor ability */ - const char *vendor_name; - const char *model_name; -}; - -struct SensorDevice; - -struct SensorDone { - int (*open)(struct SensorDevice *sdev); - int (*close)(struct SensorDevice *sdev); - x_size_t (*read)(struct SensorDevice *sdev, size_t len); - int (*write)(struct SensorDevice *sdev, const void *buf, size_t len); - int (*ioctl)(struct SensorDevice *sdev, int cmd); -}; - -struct SensorDevice { - char *name; /* Name of sensor */ - struct SensorProductInfo *info; /* Sensor model info */ - struct SensorDone *done; - int fd; /* File descriptor */ - int status; /* Sensor work mode */ - uint8 buffer[SENSOR_RECEIVE_BUFFSIZE]; /* Buffer for read data */ - - int ref_cnt; /* Reference count */ - DoubleLinklistType quant_list; /* Sensor quantity link */ - struct SysDoubleLinklistNode link; /* Sensors link node */ -}; - -enum SensorQuantityType { - SENSOR_QUANTITY_CO2 = 0, - SENSOR_QUANTITY_TEMP, - SENSOR_QUANTITY_HUMI, - SENSOR_QUANTITY_HCHO, - SENSOR_QUANTITY_CO, - SENSOR_QUANTITY_PM, - SENSOR_QUANTITY_VOICE, - /* ...... */ - SENSOR_QUANTITY_END, -}; - -struct SensorQuantityValue { - uint8 decimal_places; /* The decimal place of the result */ - uint32 last_value; /* The last read value, if it does not exist, is SENSOR_QUANTITY_VALUE_ERROR */ - uint32 min_value; /* The minimum read value, if it does not exist, is SENSOR_QUANTITY_VALUE_ERROR */ - uint32 max_value; /* The maximum read value, if it does not exist, is SENSOR_QUANTITY_VALUE_ERROR */ - uint32 min_std; /* The minimum standard value, if it does not exist, is SENSOR_QUANTITY_VALUE_ERROR */ - uint32 max_std; /* The maximum standard value, if it does not exist, is SENSOR_QUANTITY_VALUE_ERROR */ -}; - -struct SensorQuantity { - char *name; - enum SensorQuantityType type; - struct SensorQuantityValue value; - struct SensorDevice *sdev; - - int32 (*ReadValue)(struct SensorQuantity *quant); - - struct SysDoubleLinklistNode quant_link; - struct SysDoubleLinklistNode link; -}; - -int SensorDeviceRegister(struct SensorDevice *sdev); -int SensorDeviceUnregister(struct SensorDevice *sdev); - -struct SensorQuantity *SensorQuantityFind(const char *name, enum SensorQuantityType type); -int SensorQuantityRegister(struct SensorQuantity *quant); -int SensorQuantityUnregister(struct SensorQuantity *quant); -int SensorQuantityOpen(struct SensorQuantity *quant); -int SensorQuantityClose(struct SensorQuantity *quant); -int32 SensorQuantityRead(struct SensorQuantity *quant); -int SensorQuantityControl(struct SensorQuantity *quant, int cmd); - -uint32 Crc16(uint8 * data, uint8 length); -uint8 GetCheckSum(uint8 *data, uint8 head, uint8 length); - -#endif