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
This commit is contained in:
KirisameMashiro 2025-02-20 17:44:28 +08:00
parent 4358ce4d9f
commit 4d1ecbd6ec
8 changed files with 493 additions and 486 deletions

View File

@ -129,7 +129,7 @@ struct NetworkInfo {
enum CarrierType carrier_type; enum CarrierType carrier_type;
int signal_strength; int signal_strength;
char ip_address[16]; char ip_address[16];
#ifdef ADAPTER_GM800TF #if defined(ADAPTER_GM800TF) || defined(ADAPTER_EC801E)
unsigned char workMode; // 工作模式1为NET2为MQTT,NOR unsigned char workMode; // 工作模式1为NET2为MQTT,NOR
unsigned short port; // 目的端口号 unsigned short port; // 目的端口号
unsigned char is_connected; // SOCKET连接状态0为失败1为成功 unsigned char is_connected; // SOCKET连接状态0为失败1为成功

View File

@ -654,7 +654,7 @@ static int ATAgentInit(ATAgentType agent)
pthread_attr_t attr; pthread_attr_t attr;
attr.schedparam.sched_priority = 25; attr.schedparam.sched_priority = 25;
attr.stacksize = 4096; attr.stacksize = 4096;
#ifdef ADAPTER_GM800TF #if defined(ADAPTER_GM800TF) || defined(ADAPTER_EC801E)
attr.schedparam.sched_priority = 16; attr.schedparam.sched_priority = 16;
attr.stacksize = 1800; attr.stacksize = 1800;
#endif #endif

View File

@ -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 SRC_DIR := Core User Debug
# interrupt_switch.S # interrupt_switch.S
ifeq ($(CONFIG_BSP_USING_BLE),y)
SRC_FILES += ble_handler.S
endif
include $(KERNEL_ROOT)/compiler.mk include $(KERNEL_ROOT)/compiler.mk

View File

@ -1,56 +1,5 @@
.global Ecall_M_Mode_Handler
.global Ecall_U_Mode_Handler
.global LLE_IRQHandler .global LLE_IRQHandler
.extern g_LLE_IRQLibHandlerLocation .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 .section .highcode.LLE_IRQHandler,"ax",@progbits
.align 2 .align 2
.func .func

View File

@ -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

View File

@ -11,6 +11,7 @@ ifeq ($(CONFIG_BSP_USING_ADC),y)
SRC_DIR += adc SRC_DIR += adc
endif endif
ifeq ($(CONFIG_BSP_USING_BLE),y) ifeq ($(CONFIG_BSP_USING_BLE),y)
SRC_FILES += config_ble.c
SRC_DIR += ble SRC_DIR += ble
endif endif
ifeq ($(CONFIG_BSP_USING_CAN),y) ifeq ($(CONFIG_BSP_USING_CAN),y)

View File

@ -11,8 +11,6 @@
#include <adapter.h> #include <adapter.h>
#include <connect_ether.h> #include <connect_ether.h>
#include <peripheral.h> #include <peripheral.h>
#include <stdio.h>
#include <string.h>
#include <transform.h> #include <transform.h>
#include <wchble.h> #include <wchble.h>
@ -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 * @brief
* @return int * @return int

View File

@ -0,0 +1,436 @@
#include <ModuleConfig.h>
#include <adapter.h>
#include <connect_ether.h>
#include <peripheral.h>
#include <transform.h>
#include <wchble.h>
/**
* @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);
}