From 4d1ecbd6ec394a1bc31aaea7be8d05f5c5a09c01 Mon Sep 17 00:00:00 2001 From: KirisameMashiro <47731209+Sssssaltyfish@users.noreply.github.com> Date: Thu, 20 Feb 2025 17:44:28 +0800 Subject: [PATCH] Fixed compilation errors when certain features are (not) enabled 1. Fixed compilation error when LTE device is specified as EC801E 2. Refactored codes to fix compilation error when BLE feature is not enabled --- APP_Framework/Framework/connection/adapter.h | 2 +- .../Framework/connection/adapter_agent.c | 2 +- .../arch/risc-v/ch32v208rbt6/Makefile | 7 +- .../{ble_task_scheduler.S => ble_handler.S} | 51 -- .../arch/risc-v/ch32v208rbt6/task_scheduler.S | 48 ++ .../ch32v208rbt6/third_party_driver/Makefile | 1 + .../third_party_driver/ModuleConfig.c | 432 ----------------- .../third_party_driver/config_ble.c | 436 ++++++++++++++++++ 8 files changed, 493 insertions(+), 486 deletions(-) rename Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/{ble_task_scheduler.S => ble_handler.S} (63%) create mode 100644 Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/task_scheduler.S create mode 100644 Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/config_ble.c diff --git a/APP_Framework/Framework/connection/adapter.h b/APP_Framework/Framework/connection/adapter.h index 27ef3031e..0c93a40c8 100644 --- a/APP_Framework/Framework/connection/adapter.h +++ b/APP_Framework/Framework/connection/adapter.h @@ -129,7 +129,7 @@ struct NetworkInfo { enum CarrierType carrier_type; int signal_strength; char ip_address[16]; -#ifdef ADAPTER_GM800TF +#if defined(ADAPTER_GM800TF) || defined(ADAPTER_EC801E) unsigned char workMode; // 工作模式,1为NET,2为MQTT,NOR unsigned short port; // 目的端口号 unsigned char is_connected; // SOCKET连接状态,0为失败,1为成功 diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index 2e072451f..a8ba78321 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -654,7 +654,7 @@ static int ATAgentInit(ATAgentType agent) pthread_attr_t attr; attr.schedparam.sched_priority = 25; attr.stacksize = 4096; -#ifdef ADAPTER_GM800TF +#if defined(ADAPTER_GM800TF) || defined(ADAPTER_EC801E) attr.schedparam.sched_priority = 16; attr.stacksize = 1800; #endif diff --git a/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/Makefile b/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/Makefile index ca8a90e5a..7676867e0 100644 --- a/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/Makefile +++ b/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/Makefile @@ -1,4 +1,9 @@ -SRC_FILES := boot.S interrupt.c tick.c switch.S prepare_rhwstack.c interrupt_switch.S ble_task_scheduler.S +SRC_FILES := boot.S interrupt.c tick.c switch.S prepare_rhwstack.c interrupt_switch.S task_scheduler.S SRC_DIR := Core User Debug # interrupt_switch.S + +ifeq ($(CONFIG_BSP_USING_BLE),y) + SRC_FILES += ble_handler.S +endif + include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/ble_task_scheduler.S b/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/ble_handler.S similarity index 63% rename from Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/ble_task_scheduler.S rename to Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/ble_handler.S index be4e2e2d0..e2a63e1b5 100644 --- a/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/ble_task_scheduler.S +++ b/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/ble_handler.S @@ -1,56 +1,5 @@ -.global Ecall_M_Mode_Handler -.global Ecall_U_Mode_Handler .global LLE_IRQHandler - .extern g_LLE_IRQLibHandlerLocation - -.section .highcode,"ax",@progbits -.align 2 -.func -Ecall_M_Mode_Handler: -Ecall_U_Mode_Handler: - - addi a1, x0, 0x20 - csrs 0x804, a1 - - lw a1, 0 * 4( sp ) - csrw mepc, a1 - - lw x1, 1 * 4( sp ) - lw x4, 2 * 4( sp ) - lw x5, 3 * 4( sp ) - lw x6, 4 * 4( sp ) - lw x7, 5 * 4( sp ) - lw x8, 6 * 4( sp ) - lw x9, 7 * 4( sp ) - lw x10, 8 * 4( sp ) - lw x11, 9 * 4( sp ) - lw x12, 10 * 4( sp ) - lw x13, 11 * 4( sp ) - lw x14, 12 * 4( sp ) - lw x15, 13 * 4( sp ) - lw x16, 14 * 4( sp ) - lw x17, 15 * 4( sp ) - lw x18, 16 * 4( sp ) - lw x19, 17 * 4( sp ) - lw x20, 18 * 4( sp ) - lw x21, 19 * 4( sp ) - lw x22, 20 * 4( sp ) - lw x23, 21 * 4( sp ) - lw x24, 22 * 4( sp ) - lw x25, 23 * 4( sp ) - lw x26, 24 * 4( sp ) - lw x27, 25 * 4( sp ) - lw x28, 26 * 4( sp ) - lw x29, 27 * 4( sp ) - lw x30, 28 * 4( sp ) - lw x31, 29 * 4( sp ) - - addi sp, sp, 32*4 - - mret - .endfunc - .section .highcode.LLE_IRQHandler,"ax",@progbits .align 2 .func diff --git a/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/task_scheduler.S b/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/task_scheduler.S new file mode 100644 index 000000000..15ad333a5 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/arch/risc-v/ch32v208rbt6/task_scheduler.S @@ -0,0 +1,48 @@ +.global Ecall_M_Mode_Handler +.global Ecall_U_Mode_Handler +.section .highcode,"ax",@progbits +.align 2 +.func +Ecall_M_Mode_Handler: +Ecall_U_Mode_Handler: + + addi a1, x0, 0x20 + csrs 0x804, a1 + + lw a1, 0 * 4( sp ) + csrw mepc, a1 + + lw x1, 1 * 4( sp ) + lw x4, 2 * 4( sp ) + lw x5, 3 * 4( sp ) + lw x6, 4 * 4( sp ) + lw x7, 5 * 4( sp ) + lw x8, 6 * 4( sp ) + lw x9, 7 * 4( sp ) + lw x10, 8 * 4( sp ) + lw x11, 9 * 4( sp ) + lw x12, 10 * 4( sp ) + lw x13, 11 * 4( sp ) + lw x14, 12 * 4( sp ) + lw x15, 13 * 4( sp ) + lw x16, 14 * 4( sp ) + lw x17, 15 * 4( sp ) + lw x18, 16 * 4( sp ) + lw x19, 17 * 4( sp ) + lw x20, 18 * 4( sp ) + lw x21, 19 * 4( sp ) + lw x22, 20 * 4( sp ) + lw x23, 21 * 4( sp ) + lw x24, 22 * 4( sp ) + lw x25, 23 * 4( sp ) + lw x26, 24 * 4( sp ) + lw x27, 25 * 4( sp ) + lw x28, 26 * 4( sp ) + lw x29, 27 * 4( sp ) + lw x30, 28 * 4( sp ) + lw x31, 29 * 4( sp ) + + addi sp, sp, 32*4 + + mret + .endfunc diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/Makefile b/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/Makefile index 21d487fdc..ef6e1beeb 100644 --- a/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/Makefile @@ -11,6 +11,7 @@ ifeq ($(CONFIG_BSP_USING_ADC),y) SRC_DIR += adc endif ifeq ($(CONFIG_BSP_USING_BLE),y) + SRC_FILES += config_ble.c SRC_DIR += ble endif ifeq ($(CONFIG_BSP_USING_CAN),y) diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/ModuleConfig.c b/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/ModuleConfig.c index aee71b00d..1bf847e48 100644 --- a/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/ModuleConfig.c +++ b/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/ModuleConfig.c @@ -11,8 +11,6 @@ #include #include #include -#include -#include #include #include @@ -103,436 +101,6 @@ void CFG_READ(u32 StartAddr, u8 *Buffer, u32 Length) { } } -/** - * @brief 尝试使用配置通过4G连接到服务器,并且蓝牙响应连接状态报文。 - * @param requestId 蓝牙请求报文ID - * @note 在解析完成蓝牙请求和写入FLASH配置之后调用该函数 - */ -void connectServer_4G(uint8_t requestId) { - uint8_t destinationIpAddress[16] = {}; // 字符串形式的目的IP地址 - uint8_t destinationPort[6] = {}; // 字符串形式的目的端口号 - - struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); // 查找以太网模块适配器 - - /* 如果4G模块仍未初始化,则返回连接失败报文 */ - if (adapter->agent == NULL) { - uint8_t notificationReport[4]; - notificationReport[0] = requestId; - notificationReport[1] = 0x02; - notificationReport[2] = 0x01; - notificationReport[3] = 0x02; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - return; - } - - /* 获取互斥锁 */ - PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待 - PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待 - - /* 尝试连接到服务器 */ - sprintf(destinationIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_Ethernet[1], - CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]); - sprintf(destinationPort, "%hu", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8); - printf("-*-*-*-*-AdapterDeviceConnect*-*-*-*-\n"); - printf("destinationIpAddress: %s\n", destinationIpAddress); - printf("destinationPort: %s\n", destinationPort); - printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*\n"); - if (CFG->mqttSwitch_4G == 1) { // 如果使能MQTT - AdapterDeviceMqttConnect(adapter, destinationIpAddress, destinationPort, CFG->mqttClientId_4G, CFG->mqttUsername_4G, - CFG->mqttPassword_4G); - } else { // 如果禁用MQTT - AdapterDeviceConnect(adapter, CLIENT, destinationIpAddress, destinationPort, IPV4); - } - - /* 蓝牙反馈连接状态 */ - AdapterDeviceNetstat(adapter); // 读取网络状态 - if (CFG->mqttSwitch_4G == 0 && adapter->network_info.is_connected || - CFG->mqttSwitch_4G == 1 && adapter->network_info.mqttIsConnected) { // 如果连接成功 - /* 释放互斥锁 */ - PrivMutexAbandon(&romConfigurationMutex); - PrivMutexAbandon(&adapter->lock); - /* 发送网络连接成功报文 */ - uint8_t notificationReport[4]; - notificationReport[0] = requestId; - notificationReport[1] = 0x02; - notificationReport[2] = 0x01; - notificationReport[3] = 0x01; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - } else { // 如果连接失败 - printf("4G connect to server failed\n"); - /* 释放互斥锁 */ - PrivMutexAbandon(&romConfigurationMutex); - PrivMutexAbandon(&adapter->lock); - /* 发送网络连接失败报文 */ - uint8_t notificationReport[4]; - notificationReport[0] = requestId; - notificationReport[1] = 0x02; - notificationReport[2] = 0x01; - notificationReport[3] = 0x02; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - } - - return; -} - -/** - * @brief 尝试使用配置通过以太网连接到服务器,并且蓝牙响应连接状态报文。 - * @param requestId 蓝牙请求报文ID - * @note 在解析完成蓝牙请求和写入FLASH配置之后调用该函数 - */ -void connectServer_Ethernet(uint8_t requestId) { - uint8_t destinationIpAddress[16] = {}; // 字符串形式的目的IP地址 - uint8_t destinationPort[6] = {}; // 字符串形式的目的端口号 - - struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME); // 查找以太网模块适配器 - - /* 如果以太网主任务线程仍未启动,则返回连接失败报文 */ - if (adapter->adapter_param == NULL) { - uint8_t notificationReport[4]; - notificationReport[0] = requestId; - notificationReport[1] = 0x02; - notificationReport[2] = 0x02; - notificationReport[3] = 0x02; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - return; - } - - /* 获取互斥锁 */ - PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待 - PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待 - - /* DHCP配置 */ - if (CFG->dhcpSwitch_Ethernet == 1) { // 使能DHCP - AdapterDeviceSetDhcp(adapter, 1); - } else { // 禁用DHCP - uint8_t sourceIpAddress[16] = {}; - uint8_t sourcePort[6] = {}; - uint8_t sourceSubnetMask[16] = {}; - uint8_t sourceGateway[16] = {}; - memcpy(((struct WchnetSocketConfiguration *)adapter->adapter_param)->sourcePort, CFG->sourcePort_Ethernet, 2); - sprintf(sourceIpAddress, "%u.%u.%u.%u", CFG->sourceIpAddress_Ethernet[0], CFG->sourceIpAddress_Ethernet[1], - CFG->sourceIpAddress_Ethernet[2], CFG->sourceIpAddress_Ethernet[3]); - sprintf(sourcePort, "%u", (unsigned short)CFG->sourcePort_Ethernet[0] | CFG->sourcePort_Ethernet[1] << 8); - sprintf(sourceSubnetMask, "%u.%u.%u.%u", CFG->sourceSubnetMask_Ethernet[0], CFG->sourceSubnetMask_Ethernet[1], - CFG->sourceSubnetMask_Ethernet[2], CFG->sourceSubnetMask_Ethernet[3]); - sprintf(sourceGateway, "%u.%u.%u.%u", CFG->sourceGateway_Ethernet[0], CFG->sourceGateway_Ethernet[1], - CFG->sourceGateway_Ethernet[2], CFG->sourceGateway_Ethernet[3]); - AdapterDeviceSetAddr(adapter, sourceIpAddress, sourceGateway, sourceSubnetMask); // 配置静态IP地址 - AdapterDeviceSetDhcp(adapter, 0); // 禁用DHCP - } - - /* 尝试连接到服务器 */ - sprintf(destinationIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1], - CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]); - sprintf(destinationPort, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8); - printf("-*-*-*-*-AdapterDeviceConnect*-*-*-*-\n"); - printf("destinationIpAddress: %s\n", destinationIpAddress); - printf("destinationPort: %s\n", destinationPort); - printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*\n"); - int res = AdapterDeviceConnect(adapter, CLIENT, destinationIpAddress, destinationPort, IPV4); - - /* 释放互斥锁 */ - PrivMutexAbandon(&romConfigurationMutex); - PrivMutexAbandon(&adapter->lock); - - /* 蓝牙反馈连接状态 */ - if (res == 0 || res == 0x1D) { // 网络连接成功或之前已经连接 - /* 发送网络连接成功报文 */ - uint8_t notificationReport[4]; - notificationReport[0] = requestId; - notificationReport[1] = 0x02; - notificationReport[2] = 0x02; - notificationReport[3] = 0x01; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - } else { // 网络连接失败 - printf("Ethernet connect to server failed\n"); - /* 发送网络连接失败报文 */ - uint8_t notificationReport[4]; - notificationReport[0] = requestId; - notificationReport[1] = 0x02; - notificationReport[2] = 0x02; - notificationReport[3] = 0x02; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - } - - return; -} - -/** - * @brief 解析蓝牙请求报文线程 - * @param arg 指向蓝牙请求报文的指针 - * @return void* 返回值暂无意义 - */ -void *parseBleRequest(void *arg) { - uint8_t *request = (uint8_t *)arg; // 蓝牙请求报文指针 - uint8_t requestId = request[0]; // 请求ID - uint8_t reportType = request[1]; // 报文类型 - uint8_t moduleType = request[2]; // 模块类型 - - /* 蓝牙反馈ch32v208已收到蓝牙请求 */ - uint8_t notificationReport[3]; - notificationReport[0] = requestId; - notificationReport[1] = 0x01; - notificationReport[2] = 0x02; - bStatus_t res; - while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { - if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 - printf("peripheralNotify failed, res: 0x%02x\n", res); - break; - } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 - PrivTaskDelay(10); - } - } - - /* 解析蓝牙请求报文 */ - PrivMutexObtain(&romConfigurationMutex); // // 若其他线程正在读取或者写入CFG,则阻塞等待 - switch (reportType) { - /* 4G模块配置目的IP地址、目的端口号、禁用MQTT */ - case 0x01: { - uint8_t *destinationIpAddress_4G = request + 3; - uint8_t *destinationPort_4G = request + 7; - uint8_t *mqttSwitch_4G = request + 9; - memcpy(CFG->destinationIpAddress_4G, destinationIpAddress_4G, 4); - memcpy(CFG->destinationPort_4G, destinationPort_4G, 2); - CFG->mqttSwitch_4G = *mqttSwitch_4G; - /* 配置写入FLASH */ - CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); - CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); - PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 - connectServer_4G(requestId); // 尝试连接服务器 - break; - } - - /* 4G模块配置目的IP地址、目的端口号、使能MQTT、MQTT发布主题、MQTT用户名、MQTT密码、MQTT客户端ID */ - case 0x02: { - uint8_t *destinationIpAddress_4G = request + 3; - uint8_t *destinationPort_4G = request + 7; - uint8_t *mqttSwitch_4G = request + 9; - uint8_t *mqttTopic_4G = request + 10; - uint8_t *mqttUsername_4G = request + 74; - uint8_t *mqttPassword_4G = request + 138; - uint8_t *mqttClientId_4G = request + 202; - memcpy(CFG->destinationIpAddress_4G, destinationIpAddress_4G, 4); - memcpy(CFG->destinationPort_4G, destinationPort_4G, 2); - CFG->mqttSwitch_4G = *mqttSwitch_4G; - memcpy(CFG->mqttTopic_4G, mqttTopic_4G, 64); - memcpy(CFG->mqttUsername_4G, mqttUsername_4G, 64); - memcpy(CFG->mqttPassword_4G, mqttPassword_4G, 64); - memcpy(CFG->mqttClientId_4G, mqttClientId_4G, 24); - /* 配置写入FLASH */ - CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); - CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); - PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 - connectServer_4G(requestId); // 尝试连接服务器 - break; - } - - /* 以太网模块配置目的IP地址、目的端口号、使能DHCP */ - case 0x03: { - uint8_t *destinationIpAddress_Ethernet = request + 3; - uint8_t *destinationPort_Ethernet = request + 7; - uint8_t *dhcpSwitch_Ethernet = request + 9; - memcpy(CFG->destinationIpAddress_Ethernet, destinationIpAddress_Ethernet, 4); - memcpy(CFG->destinationPort_Ethernet, destinationPort_Ethernet, 2); - CFG->dhcpSwitch_Ethernet = *dhcpSwitch_Ethernet; - /* 配置写入FLASH */ - CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); - CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); - PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 - connectServer_Ethernet(requestId); // 尝试连接服务器 - break; - } - - /* 以太网模块配置目的IP地址、目的端口号、禁用DHCP、源IP地址、源端口号、子网掩码、网关地址 */ - case 0x04: { - uint8_t *destinationIpAddress_Ethernet = request + 3; - uint8_t *destinationPort_Ethernet = request + 7; - uint8_t *dhcpSwitch_Ethernet = request + 9; - uint8_t *sourceIpAddress_Ethernet = request + 10; - uint8_t *sourcePort_Ethernet = request + 14; - uint8_t *sourceSubnetMask_Ethernet = request + 16; - uint8_t *sourceGateway_Ethernet = request + 20; - memcpy(CFG->destinationIpAddress_Ethernet, destinationIpAddress_Ethernet, 4); - memcpy(CFG->destinationPort_Ethernet, destinationPort_Ethernet, 2); - CFG->dhcpSwitch_Ethernet = *dhcpSwitch_Ethernet; - memcpy(CFG->sourceIpAddress_Ethernet, sourceIpAddress_Ethernet, 4); - memcpy(CFG->sourcePort_Ethernet, sourcePort_Ethernet, 2); - memcpy(CFG->sourceSubnetMask_Ethernet, sourceSubnetMask_Ethernet, 4); - memcpy(CFG->sourceGateway_Ethernet, sourceGateway_Ethernet, 4); - /* 配置写入FLASH */ - CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); - CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); - PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 - connectServer_Ethernet(requestId); // 尝试连接服务器 - break; - } - - /* RS485模块配置波特率、数据位、停止位、校验方式 */ - case 0x10: { - struct SerialDataCfg rs485Configuration; - memset(&rs485Configuration, 0, sizeof(struct SerialDataCfg)); - int baudRateOption = request[3]; - int dataBitsOption = request[4]; - int stopBitsOption = request[5]; - int parityOption = request[6]; - switch (baudRateOption) { - case 1: - rs485Configuration.serial_baud_rate = BAUD_RATE_2400; - break; - case 2: - rs485Configuration.serial_baud_rate = BAUD_RATE_4800; - break; - case 3: - rs485Configuration.serial_baud_rate = BAUD_RATE_9600; - break; - case 4: - rs485Configuration.serial_baud_rate = BAUD_RATE_19200; - break; - case 5: - rs485Configuration.serial_baud_rate = BAUD_RATE_38400; - break; - case 6: - rs485Configuration.serial_baud_rate = BAUD_RATE_57600; - break; - case 7: - rs485Configuration.serial_baud_rate = BAUD_RATE_115200; - break; - case 8: - rs485Configuration.serial_baud_rate = BAUD_RATE_230400; - break; - default: - rs485Configuration.serial_baud_rate = BAUD_RATE_9600; - break; - } - switch (dataBitsOption) { - case 1: - rs485Configuration.serial_data_bits = DATA_BITS_8; - break; - case 2: - rs485Configuration.serial_data_bits = DATA_BITS_9; - break; - default: - rs485Configuration.serial_data_bits = DATA_BITS_8; - break; - } - switch (stopBitsOption) { - case 1: - rs485Configuration.serial_stop_bits = STOP_BITS_1; - break; - case 2: - rs485Configuration.serial_stop_bits = STOP_BITS_2; - break; - default: - rs485Configuration.serial_stop_bits = STOP_BITS_1; - break; - } - switch (parityOption) { - case 1: - rs485Configuration.serial_parity_mode = PARITY_NONE; - break; - case 2: - rs485Configuration.serial_parity_mode = PARITY_ODD; - break; - case 3: - rs485Configuration.serial_parity_mode = PARITY_EVEN; - break; - } - struct PrivIoctlCfg ioctl_cfg; - ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; - ioctl_cfg.args = (void *)&rs485Configuration; - - int rs485_fd = PrivOpen("/dev/rs485_dev1", O_RDWR); // 打开设备文件 - if (0 != PrivIoctl(rs485_fd, OPE_INT, &ioctl_cfg)) { // 进行配置 - printf("ioctl uart fd error %d\n", rs485_fd); - PrivClose(rs485_fd); - return NULL; - } - - /* 配置写入FLASH */ - CFG->baudRate_Rs485 = baudRateOption; - CFG->dataBits_Rs485 = dataBitsOption; - CFG->stopBits_Rs485 = stopBitsOption; - CFG->parity_Rs485 = parityOption; - CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); - CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); - PrivMutexAbandon(&romConfigurationMutex); - break; - } - - /* 报文类型识别失败 */ - default: - PrivMutexAbandon(&romConfigurationMutex); - break; - } - PrivFree(request); // 释放内存 - return NULL; -} - -/** - * @brief 启动解析蓝牙请求报文的线程 - * @param request 指向蓝牙请求报文的指针 - */ -void startParseBleRequestTask(uint8_t *request) { - /* 启动蓝牙交互的线程 */ - pthread_attr_t parseBleRequestTaskAttr; - pthread_args_t parseBleRequestTaskArgs; - parseBleRequestTaskAttr.schedparam.sched_priority = 16; // 线程优先级 - parseBleRequestTaskAttr.stacksize = 2048; // 线程栈大小 - parseBleRequestTaskArgs.pthread_name = "parseBleRequestTask"; // 线程名字 - parseBleRequestTaskArgs.arg = request; // 线程参数 - pthread_t parseBleRequestThread; // 线程ID - PrivTaskCreate(&parseBleRequestThread, &parseBleRequestTaskAttr, parseBleRequest, &parseBleRequestTaskArgs); - PrivTaskStartup(&parseBleRequestThread); -} - /** * @brief 命令行显示配置信息 * @return int 返回值暂无意义 diff --git a/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/config_ble.c b/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/config_ble.c new file mode 100644 index 000000000..629b943fb --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/ch32v208rbt6/third_party_driver/config_ble.c @@ -0,0 +1,436 @@ +#include +#include +#include +#include +#include +#include + +/** + * @brief 尝试使用配置通过4G连接到服务器,并且蓝牙响应连接状态报文。 + * @param requestId 蓝牙请求报文ID + * @note 在解析完成蓝牙请求和写入FLASH配置之后调用该函数 + */ +void connectServer_4G(uint8_t requestId) { + uint8_t destinationIpAddress[16] = {}; // 字符串形式的目的IP地址 + uint8_t destinationPort[6] = {}; // 字符串形式的目的端口号 + + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); // 查找以太网模块适配器 + + /* 如果4G模块仍未初始化,则返回连接失败报文 */ + if (adapter->agent == NULL) { + uint8_t notificationReport[4]; + notificationReport[0] = requestId; + notificationReport[1] = 0x02; + notificationReport[2] = 0x01; + notificationReport[3] = 0x02; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + return; + } + + /* 获取互斥锁 */ + PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待 + PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待 + + /* 尝试连接到服务器 */ + sprintf(destinationIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_4G[0], CFG->destinationIpAddress_Ethernet[1], + CFG->destinationIpAddress_4G[2], CFG->destinationIpAddress_4G[3]); + sprintf(destinationPort, "%hu", (unsigned short)CFG->destinationPort_4G[0] | CFG->destinationPort_4G[1] << 8); + printf("-*-*-*-*-AdapterDeviceConnect*-*-*-*-\n"); + printf("destinationIpAddress: %s\n", destinationIpAddress); + printf("destinationPort: %s\n", destinationPort); + printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*\n"); + if (CFG->mqttSwitch_4G == 1) { // 如果使能MQTT + AdapterDeviceMqttConnect(adapter, destinationIpAddress, destinationPort, CFG->mqttClientId_4G, CFG->mqttUsername_4G, + CFG->mqttPassword_4G); + } else { // 如果禁用MQTT + AdapterDeviceConnect(adapter, CLIENT, destinationIpAddress, destinationPort, IPV4); + } + + /* 蓝牙反馈连接状态 */ + AdapterDeviceNetstat(adapter); // 读取网络状态 + if (CFG->mqttSwitch_4G == 0 && adapter->network_info.is_connected || + CFG->mqttSwitch_4G == 1 && adapter->network_info.mqttIsConnected) { // 如果连接成功 + /* 释放互斥锁 */ + PrivMutexAbandon(&romConfigurationMutex); + PrivMutexAbandon(&adapter->lock); + /* 发送网络连接成功报文 */ + uint8_t notificationReport[4]; + notificationReport[0] = requestId; + notificationReport[1] = 0x02; + notificationReport[2] = 0x01; + notificationReport[3] = 0x01; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + } else { // 如果连接失败 + printf("4G connect to server failed\n"); + /* 释放互斥锁 */ + PrivMutexAbandon(&romConfigurationMutex); + PrivMutexAbandon(&adapter->lock); + /* 发送网络连接失败报文 */ + uint8_t notificationReport[4]; + notificationReport[0] = requestId; + notificationReport[1] = 0x02; + notificationReport[2] = 0x01; + notificationReport[3] = 0x02; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + } + + return; +} + +/** + * @brief 尝试使用配置通过以太网连接到服务器,并且蓝牙响应连接状态报文。 + * @param requestId 蓝牙请求报文ID + * @note 在解析完成蓝牙请求和写入FLASH配置之后调用该函数 + */ +void connectServer_Ethernet(uint8_t requestId) { + uint8_t destinationIpAddress[16] = {}; // 字符串形式的目的IP地址 + uint8_t destinationPort[6] = {}; // 字符串形式的目的端口号 + + struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME); // 查找以太网模块适配器 + + /* 如果以太网主任务线程仍未启动,则返回连接失败报文 */ + if (adapter->adapter_param == NULL) { + uint8_t notificationReport[4]; + notificationReport[0] = requestId; + notificationReport[1] = 0x02; + notificationReport[2] = 0x02; + notificationReport[3] = 0x02; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + return; + } + + /* 获取互斥锁 */ + PrivMutexObtain(&adapter->lock); // 若其他线程正在使用adapter,则阻塞等待 + PrivMutexObtain(&romConfigurationMutex); // 若其他线程正在读取或者写入CFG,则阻塞等待 + + /* DHCP配置 */ + if (CFG->dhcpSwitch_Ethernet == 1) { // 使能DHCP + AdapterDeviceSetDhcp(adapter, 1); + } else { // 禁用DHCP + uint8_t sourceIpAddress[16] = {}; + uint8_t sourcePort[6] = {}; + uint8_t sourceSubnetMask[16] = {}; + uint8_t sourceGateway[16] = {}; + memcpy(((struct WchnetSocketConfiguration *)adapter->adapter_param)->sourcePort, CFG->sourcePort_Ethernet, 2); + sprintf(sourceIpAddress, "%u.%u.%u.%u", CFG->sourceIpAddress_Ethernet[0], CFG->sourceIpAddress_Ethernet[1], + CFG->sourceIpAddress_Ethernet[2], CFG->sourceIpAddress_Ethernet[3]); + sprintf(sourcePort, "%u", (unsigned short)CFG->sourcePort_Ethernet[0] | CFG->sourcePort_Ethernet[1] << 8); + sprintf(sourceSubnetMask, "%u.%u.%u.%u", CFG->sourceSubnetMask_Ethernet[0], CFG->sourceSubnetMask_Ethernet[1], + CFG->sourceSubnetMask_Ethernet[2], CFG->sourceSubnetMask_Ethernet[3]); + sprintf(sourceGateway, "%u.%u.%u.%u", CFG->sourceGateway_Ethernet[0], CFG->sourceGateway_Ethernet[1], + CFG->sourceGateway_Ethernet[2], CFG->sourceGateway_Ethernet[3]); + AdapterDeviceSetAddr(adapter, sourceIpAddress, sourceGateway, sourceSubnetMask); // 配置静态IP地址 + AdapterDeviceSetDhcp(adapter, 0); // 禁用DHCP + } + + /* 尝试连接到服务器 */ + sprintf(destinationIpAddress, "%u.%u.%u.%u", CFG->destinationIpAddress_Ethernet[0], CFG->destinationIpAddress_Ethernet[1], + CFG->destinationIpAddress_Ethernet[2], CFG->destinationIpAddress_Ethernet[3]); + sprintf(destinationPort, "%u", (unsigned short)CFG->destinationPort_Ethernet[0] | CFG->destinationPort_Ethernet[1] << 8); + printf("-*-*-*-*-AdapterDeviceConnect*-*-*-*-\n"); + printf("destinationIpAddress: %s\n", destinationIpAddress); + printf("destinationPort: %s\n", destinationPort); + printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*\n"); + int res = AdapterDeviceConnect(adapter, CLIENT, destinationIpAddress, destinationPort, IPV4); + + /* 释放互斥锁 */ + PrivMutexAbandon(&romConfigurationMutex); + PrivMutexAbandon(&adapter->lock); + + /* 蓝牙反馈连接状态 */ + if (res == 0 || res == 0x1D) { // 网络连接成功或之前已经连接 + /* 发送网络连接成功报文 */ + uint8_t notificationReport[4]; + notificationReport[0] = requestId; + notificationReport[1] = 0x02; + notificationReport[2] = 0x02; + notificationReport[3] = 0x01; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + } else { // 网络连接失败 + printf("Ethernet connect to server failed\n"); + /* 发送网络连接失败报文 */ + uint8_t notificationReport[4]; + notificationReport[0] = requestId; + notificationReport[1] = 0x02; + notificationReport[2] = 0x02; + notificationReport[3] = 0x02; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + } + + return; +} + +/** + * @brief 解析蓝牙请求报文线程 + * @param arg 指向蓝牙请求报文的指针 + * @return void* 返回值暂无意义 + */ +void *parseBleRequest(void *arg) { + uint8_t *request = (uint8_t *)arg; // 蓝牙请求报文指针 + uint8_t requestId = request[0]; // 请求ID + uint8_t reportType = request[1]; // 报文类型 + uint8_t moduleType = request[2]; // 模块类型 + + /* 蓝牙反馈ch32v208已收到蓝牙请求 */ + uint8_t notificationReport[3]; + notificationReport[0] = requestId; + notificationReport[1] = 0x01; + notificationReport[2] = 0x02; + bStatus_t res; + while ((res = peripheralNotify(notificationReport, sizeof(notificationReport))) != SUCCESS) { + if (res != blePending) { // 发送失败的原因不是阻塞,放弃继续发送 + printf("peripheralNotify failed, res: 0x%02x\n", res); + break; + } else { // 发送失败的原因是阻塞,则等待10ms后再次发送 + PrivTaskDelay(10); + } + } + + /* 解析蓝牙请求报文 */ + PrivMutexObtain(&romConfigurationMutex); // // 若其他线程正在读取或者写入CFG,则阻塞等待 + switch (reportType) { + /* 4G模块配置目的IP地址、目的端口号、禁用MQTT */ + case 0x01: { + uint8_t *destinationIpAddress_4G = request + 3; + uint8_t *destinationPort_4G = request + 7; + uint8_t *mqttSwitch_4G = request + 9; + memcpy(CFG->destinationIpAddress_4G, destinationIpAddress_4G, 4); + memcpy(CFG->destinationPort_4G, destinationPort_4G, 2); + CFG->mqttSwitch_4G = *mqttSwitch_4G; + /* 配置写入FLASH */ + CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); + CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); + PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 + connectServer_4G(requestId); // 尝试连接服务器 + break; + } + + /* 4G模块配置目的IP地址、目的端口号、使能MQTT、MQTT发布主题、MQTT用户名、MQTT密码、MQTT客户端ID */ + case 0x02: { + uint8_t *destinationIpAddress_4G = request + 3; + uint8_t *destinationPort_4G = request + 7; + uint8_t *mqttSwitch_4G = request + 9; + uint8_t *mqttTopic_4G = request + 10; + uint8_t *mqttUsername_4G = request + 74; + uint8_t *mqttPassword_4G = request + 138; + uint8_t *mqttClientId_4G = request + 202; + memcpy(CFG->destinationIpAddress_4G, destinationIpAddress_4G, 4); + memcpy(CFG->destinationPort_4G, destinationPort_4G, 2); + CFG->mqttSwitch_4G = *mqttSwitch_4G; + memcpy(CFG->mqttTopic_4G, mqttTopic_4G, 64); + memcpy(CFG->mqttUsername_4G, mqttUsername_4G, 64); + memcpy(CFG->mqttPassword_4G, mqttPassword_4G, 64); + memcpy(CFG->mqttClientId_4G, mqttClientId_4G, 24); + /* 配置写入FLASH */ + CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); + CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); + PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 + connectServer_4G(requestId); // 尝试连接服务器 + break; + } + + /* 以太网模块配置目的IP地址、目的端口号、使能DHCP */ + case 0x03: { + uint8_t *destinationIpAddress_Ethernet = request + 3; + uint8_t *destinationPort_Ethernet = request + 7; + uint8_t *dhcpSwitch_Ethernet = request + 9; + memcpy(CFG->destinationIpAddress_Ethernet, destinationIpAddress_Ethernet, 4); + memcpy(CFG->destinationPort_Ethernet, destinationPort_Ethernet, 2); + CFG->dhcpSwitch_Ethernet = *dhcpSwitch_Ethernet; + /* 配置写入FLASH */ + CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); + CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); + PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 + connectServer_Ethernet(requestId); // 尝试连接服务器 + break; + } + + /* 以太网模块配置目的IP地址、目的端口号、禁用DHCP、源IP地址、源端口号、子网掩码、网关地址 */ + case 0x04: { + uint8_t *destinationIpAddress_Ethernet = request + 3; + uint8_t *destinationPort_Ethernet = request + 7; + uint8_t *dhcpSwitch_Ethernet = request + 9; + uint8_t *sourceIpAddress_Ethernet = request + 10; + uint8_t *sourcePort_Ethernet = request + 14; + uint8_t *sourceSubnetMask_Ethernet = request + 16; + uint8_t *sourceGateway_Ethernet = request + 20; + memcpy(CFG->destinationIpAddress_Ethernet, destinationIpAddress_Ethernet, 4); + memcpy(CFG->destinationPort_Ethernet, destinationPort_Ethernet, 2); + CFG->dhcpSwitch_Ethernet = *dhcpSwitch_Ethernet; + memcpy(CFG->sourceIpAddress_Ethernet, sourceIpAddress_Ethernet, 4); + memcpy(CFG->sourcePort_Ethernet, sourcePort_Ethernet, 2); + memcpy(CFG->sourceSubnetMask_Ethernet, sourceSubnetMask_Ethernet, 4); + memcpy(CFG->sourceGateway_Ethernet, sourceGateway_Ethernet, 4); + /* 配置写入FLASH */ + CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); + CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); + PrivMutexAbandon(&romConfigurationMutex); // 释放互斥锁 + connectServer_Ethernet(requestId); // 尝试连接服务器 + break; + } + + /* RS485模块配置波特率、数据位、停止位、校验方式 */ + case 0x10: { + struct SerialDataCfg rs485Configuration; + memset(&rs485Configuration, 0, sizeof(struct SerialDataCfg)); + int baudRateOption = request[3]; + int dataBitsOption = request[4]; + int stopBitsOption = request[5]; + int parityOption = request[6]; + switch (baudRateOption) { + case 1: + rs485Configuration.serial_baud_rate = BAUD_RATE_2400; + break; + case 2: + rs485Configuration.serial_baud_rate = BAUD_RATE_4800; + break; + case 3: + rs485Configuration.serial_baud_rate = BAUD_RATE_9600; + break; + case 4: + rs485Configuration.serial_baud_rate = BAUD_RATE_19200; + break; + case 5: + rs485Configuration.serial_baud_rate = BAUD_RATE_38400; + break; + case 6: + rs485Configuration.serial_baud_rate = BAUD_RATE_57600; + break; + case 7: + rs485Configuration.serial_baud_rate = BAUD_RATE_115200; + break; + case 8: + rs485Configuration.serial_baud_rate = BAUD_RATE_230400; + break; + default: + rs485Configuration.serial_baud_rate = BAUD_RATE_9600; + break; + } + switch (dataBitsOption) { + case 1: + rs485Configuration.serial_data_bits = DATA_BITS_8; + break; + case 2: + rs485Configuration.serial_data_bits = DATA_BITS_9; + break; + default: + rs485Configuration.serial_data_bits = DATA_BITS_8; + break; + } + switch (stopBitsOption) { + case 1: + rs485Configuration.serial_stop_bits = STOP_BITS_1; + break; + case 2: + rs485Configuration.serial_stop_bits = STOP_BITS_2; + break; + default: + rs485Configuration.serial_stop_bits = STOP_BITS_1; + break; + } + switch (parityOption) { + case 1: + rs485Configuration.serial_parity_mode = PARITY_NONE; + break; + case 2: + rs485Configuration.serial_parity_mode = PARITY_ODD; + break; + case 3: + rs485Configuration.serial_parity_mode = PARITY_EVEN; + break; + } + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = (void *)&rs485Configuration; + + int rs485_fd = PrivOpen("/dev/rs485_dev1", O_RDWR); // 打开设备文件 + if (0 != PrivIoctl(rs485_fd, OPE_INT, &ioctl_cfg)) { // 进行配置 + printf("ioctl uart fd error %d\n", rs485_fd); + PrivClose(rs485_fd); + return NULL; + } + + /* 配置写入FLASH */ + CFG->baudRate_Rs485 = baudRateOption; + CFG->dataBits_Rs485 = dataBitsOption; + CFG->stopBits_Rs485 = stopBitsOption; + CFG->parity_Rs485 = parityOption; + CFG_ERASE(PAGE_WRITE_START_ADDR, FLASH_PAGE_SIZE); + CFG_WRITE(PAGE_WRITE_START_ADDR, Configbuf, MODULE_CFG_LEN); + PrivMutexAbandon(&romConfigurationMutex); + break; + } + + /* 报文类型识别失败 */ + default: + PrivMutexAbandon(&romConfigurationMutex); + break; + } + PrivFree(request); // 释放内存 + return NULL; +} + +/** + * @brief 启动解析蓝牙请求报文的线程 + * @param request 指向蓝牙请求报文的指针 + */ +void startParseBleRequestTask(uint8_t *request) { + /* 启动蓝牙交互的线程 */ + pthread_attr_t parseBleRequestTaskAttr; + pthread_args_t parseBleRequestTaskArgs; + parseBleRequestTaskAttr.schedparam.sched_priority = 16; // 线程优先级 + parseBleRequestTaskAttr.stacksize = 2048; // 线程栈大小 + parseBleRequestTaskArgs.pthread_name = "parseBleRequestTask"; // 线程名字 + parseBleRequestTaskArgs.arg = request; // 线程参数 + pthread_t parseBleRequestThread; // 线程ID + PrivTaskCreate(&parseBleRequestThread, &parseBleRequestTaskAttr, parseBleRequest, &parseBleRequestTaskArgs); + PrivTaskStartup(&parseBleRequestThread); +}