2025-08-20 11:28:11 +08:00
|
|
|
#include "device.h"
|
|
|
|
|
#include "config/board_config.h"
|
|
|
|
|
|
2025-08-20 16:46:15 +08:00
|
|
|
/**
|
|
|
|
|
* @brief PC OR 手机数据内部接口
|
|
|
|
|
*
|
|
|
|
|
* @param port 由对应的驱动提供的绑定接口获得的句柄
|
|
|
|
|
* @param type 对应的数据接口类型
|
|
|
|
|
* @retval 0 成功
|
|
|
|
|
*/
|
|
|
|
|
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect);
|
|
|
|
|
|
2025-10-20 12:06:29 +08:00
|
|
|
// 内部全局变量
|
|
|
|
|
static uint8_t* rf_flag_off_overall = NULL;
|
|
|
|
|
static device_t *device_inside;
|
|
|
|
|
|
2025-08-20 11:28:11 +08:00
|
|
|
uint8_t device_init(device_t *port)
|
|
|
|
|
{
|
|
|
|
|
uint8_t res = DEVICE_OK;
|
|
|
|
|
#if UART_ENABLE
|
|
|
|
|
res = uart_init(&port->init_device);
|
|
|
|
|
if(res)
|
|
|
|
|
{
|
|
|
|
|
return DEVICE_UART_ERROR;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if BLE_ENABLE
|
|
|
|
|
res = ble_init(&port->init_device);
|
|
|
|
|
if(res)
|
|
|
|
|
{
|
|
|
|
|
return DEVICE_BLE_ERROR;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#if WIFI_ENABLE
|
|
|
|
|
res = wifi_init(&port->init_device);
|
|
|
|
|
if(res)
|
|
|
|
|
{
|
|
|
|
|
return DEVICE_WIFI_ERROR;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
//默认选择串口
|
|
|
|
|
res = embedded_device_choice(port, DATA_PORT_TYPE_UART);
|
|
|
|
|
if(res == DEVICE_EMBEDDED_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("embedded device choice error");
|
|
|
|
|
}
|
2025-08-28 16:04:01 +08:00
|
|
|
//默认BLE模式
|
|
|
|
|
res = pc_device_choice_inside(port, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
2025-08-20 11:50:01 +08:00
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("pc device choice error");
|
|
|
|
|
}
|
2025-09-15 16:48:12 +08:00
|
|
|
|
|
|
|
|
res = app_device_choice(port, DATA_PORT_TYPE_BLE_CMD);
|
|
|
|
|
if(res == DEVICE_EMBEDDED_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("app device choice error");
|
|
|
|
|
}
|
2025-08-28 16:04:01 +08:00
|
|
|
SYS_LOG_INF("device init success");
|
2025-10-20 12:06:29 +08:00
|
|
|
// 赋值全局变量 RF开关
|
|
|
|
|
rf_flag_off_overall = &port->rf_flag_off;
|
|
|
|
|
// 赋值设备
|
|
|
|
|
device_inside = port;
|
|
|
|
|
|
2025-08-20 11:28:11 +08:00
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
|
|
|
|
uint8_t uart_init(init_device_t *port)
|
|
|
|
|
{
|
|
|
|
|
// port->uart_port = sb_uart_port_bind(g_cfg_board->uart_fc.id, g_cfg_board->uart_fc.br, g_cfg_board->uart_fc.pin_txd.pin, g_cfg_board->uart_fc.pin_rxd.pin, g_cfg_board->uart_fc.irq_prior, 1024, 0, NULL);
|
2025-10-21 16:18:57 +08:00
|
|
|
port->uart_port = sb_uart_port_bind(1, 115200, 21, 20, 21, 2048, 0, NULL);
|
2025-08-20 11:28:11 +08:00
|
|
|
|
|
|
|
|
if(port->uart_port == NULL)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("uart init error");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(sb_data_port_start(port->uart_port))
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("uart start error");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
2025-08-20 15:10:26 +08:00
|
|
|
// 蓝牙连接回调函数
|
|
|
|
|
static void ble_server_connect_handler(ble_server_status_t status)
|
2025-08-20 11:50:01 +08:00
|
|
|
{
|
2025-10-20 12:06:29 +08:00
|
|
|
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0){
|
|
|
|
|
// 处理连接状态,例如:
|
|
|
|
|
if (status == BLE_SERVER_STATUS_CONNECTED) {
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, CONNECT_BLE);
|
|
|
|
|
if(res == DEVICE_PC_ERROR){
|
|
|
|
|
}
|
|
|
|
|
device_wifi_stop();
|
|
|
|
|
SYS_LOG_INF("ble Connected");
|
|
|
|
|
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
|
|
|
|
if(res == DEVICE_PC_ERROR){
|
|
|
|
|
}
|
|
|
|
|
device_wifi_start();
|
|
|
|
|
SYS_LOG_INF("ble dis Connected");
|
|
|
|
|
// disconnected / other
|
|
|
|
|
} else {
|
|
|
|
|
SYS_LOG_INF("ble stop");
|
2025-08-20 16:09:37 +08:00
|
|
|
}
|
2025-08-20 11:50:01 +08:00
|
|
|
}
|
2025-10-20 12:06:29 +08:00
|
|
|
|
2025-08-20 11:50:01 +08:00
|
|
|
}
|
2025-09-17 15:16:18 +08:00
|
|
|
static void _sb_manufacturer_encode(uint8_t manufacturer_data[20])
|
|
|
|
|
{
|
|
|
|
|
memset(manufacturer_data, 0, 20);
|
2025-09-19 14:51:49 +08:00
|
|
|
manufacturer_data[0] = 0x59;
|
|
|
|
|
manufacturer_data[1] = 0x00;
|
2025-09-17 15:16:18 +08:00
|
|
|
manufacturer_data[2] = PRODUCT0;
|
|
|
|
|
manufacturer_data[3] = PRODUCT1;
|
|
|
|
|
manufacturer_data[4] = VERSIONS;
|
|
|
|
|
|
|
|
|
|
}
|
2025-08-20 11:28:11 +08:00
|
|
|
uint8_t ble_init(init_device_t *port)
|
|
|
|
|
{
|
2025-08-20 11:50:01 +08:00
|
|
|
ble_server_init_t ble_init_param = {
|
2025-09-18 15:55:24 +08:00
|
|
|
.device_name = g_cfg_app->device_name_ble, // 设备名
|
2025-09-17 15:16:18 +08:00
|
|
|
.manufacturer_data = _sb_manufacturer_encode, // 回调函数,生成 manufacturer[20] 设置厂商数据
|
2025-08-20 15:10:26 +08:00
|
|
|
.connect_cb = ble_server_connect_handler, // 回调函数, NULL 值可用
|
2025-08-20 11:50:01 +08:00
|
|
|
};
|
|
|
|
|
//初始化参数
|
|
|
|
|
sb_ble_server_port_init(&ble_init_param);
|
|
|
|
|
|
|
|
|
|
port->ble_spp_server_cmd = ble_server_port_bind_cmd(0x2000, NULL);
|
|
|
|
|
port->ble_spp_server_val = ble_server_port_bind_val(0x400, NULL);
|
|
|
|
|
|
|
|
|
|
if(port->ble_spp_server_cmd == NULL || port->ble_spp_server_val == NULL)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("ble init error");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(sb_data_port_start(port->ble_spp_server_cmd))
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("ble cmd start error");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(sb_data_port_start(port->ble_spp_server_val))
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("ble val start error");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 11:28:11 +08:00
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 16:09:37 +08:00
|
|
|
//WIFI连接回调函数
|
2025-08-20 15:10:26 +08:00
|
|
|
static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
|
|
|
|
|
{
|
2025-08-20 16:09:37 +08:00
|
|
|
//实现自动切换发送对象
|
2025-08-20 15:10:26 +08:00
|
|
|
if(is_connect){
|
2025-08-28 16:04:01 +08:00
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_TCP, CONNECT_WIFI_TCP);
|
2025-08-20 16:09:37 +08:00
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("wifi pc device choice error");
|
|
|
|
|
}
|
2025-08-20 15:10:26 +08:00
|
|
|
SYS_LOG_INF("wifi connect");
|
|
|
|
|
}else{
|
2025-09-19 14:51:49 +08:00
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
2025-08-28 16:04:01 +08:00
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("wifi pc device choice error");
|
|
|
|
|
}
|
|
|
|
|
SYS_LOG_INF("wifi disconnect");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static void wifi_ap_connect_handler(wifi_ap_connect_status_t status, uint8_t connect_cnt)
|
|
|
|
|
{
|
2025-10-20 12:06:29 +08:00
|
|
|
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("wifi ap connect status %d %d", (int)status, (int)connect_cnt);
|
|
|
|
|
//实现自动切换发送对象
|
|
|
|
|
if((connect_cnt == 1 && status == WIFI_AP_CONNECT_GOT_STA)){
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
|
|
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("wifi pc device choice error");
|
|
|
|
|
}
|
|
|
|
|
device_ble_stop();
|
|
|
|
|
SYS_LOG_INF("wifi connect");
|
|
|
|
|
}else{
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
|
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("wifi pc device choice error");
|
|
|
|
|
}
|
|
|
|
|
device_ble_start();
|
|
|
|
|
SYS_LOG_INF("wifi disconnect");
|
2025-08-28 16:04:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static void wifi_sta_connect_handler(wifi_sta_connect_status_t status, uint8_t ip_v4[4])
|
|
|
|
|
{
|
2025-10-20 12:06:29 +08:00
|
|
|
if(rf_flag_off_overall != NULL && *rf_flag_off_overall == 0)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("wifi ap connect status %d", (int)status);
|
|
|
|
|
//实现自动切换发送对象
|
|
|
|
|
if(status == WIFI_STA_CONNECT_GOT_IP){
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, CONNECT_WIFI_UDP);
|
|
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("wifi pc device choice error");
|
|
|
|
|
}
|
|
|
|
|
SYS_LOG_INF("wifi connect");
|
|
|
|
|
}else{
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_UDP, DISCONNECT);
|
|
|
|
|
if(res == DEVICE_PC_ERROR)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_ERR("wifi pc device choice error");
|
|
|
|
|
}
|
|
|
|
|
SYS_LOG_INF("wifi disconnect");
|
2025-08-20 16:09:37 +08:00
|
|
|
}
|
2025-08-20 15:10:26 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-20 11:28:11 +08:00
|
|
|
uint8_t wifi_init(init_device_t *port)
|
|
|
|
|
{
|
2025-08-20 15:10:26 +08:00
|
|
|
/* 初始化 WIFI */
|
|
|
|
|
wifi_init_t init_struct = {
|
|
|
|
|
.ap = {
|
2025-09-18 15:55:24 +08:00
|
|
|
.ssid = {0},
|
|
|
|
|
.password = {0}, /**< Password of soft-AP. */
|
|
|
|
|
.ip_v4 = {0},
|
|
|
|
|
.gw_v4 = {0},
|
2025-08-20 15:10:26 +08:00
|
|
|
.mask_v4 = {255, 255, 255, 0},
|
|
|
|
|
.max_connection = 1,
|
2025-08-28 16:04:01 +08:00
|
|
|
.connect_cb = wifi_ap_connect_handler,
|
2025-08-20 15:10:26 +08:00
|
|
|
},
|
|
|
|
|
.sta = {
|
2025-09-18 15:55:24 +08:00
|
|
|
.ssid = {0},
|
|
|
|
|
.password = {0},
|
2025-08-28 16:04:01 +08:00
|
|
|
.connect_cb = wifi_sta_connect_handler,
|
2025-08-20 15:10:26 +08:00
|
|
|
},
|
|
|
|
|
};
|
2025-09-18 15:55:24 +08:00
|
|
|
//设置wifi参数
|
|
|
|
|
memcpy(&init_struct.ap.ssid, g_cfg_app->app_config_wifi_para.wifi_ap_ssid, 32);
|
|
|
|
|
memcpy(&init_struct.ap.password, g_cfg_app->app_config_wifi_para.wifi_ap_password, 64);
|
|
|
|
|
memcpy(&init_struct.ap.ip_v4, g_cfg_app->wifi_ap_ipv4, 4);
|
|
|
|
|
memcpy(&init_struct.ap.gw_v4, g_cfg_app->wifi_ap_ipv4, 4);
|
|
|
|
|
|
|
|
|
|
memcpy(&init_struct.sta.ssid, g_cfg_app->app_config_wifi_para.wifi_sta_ssid, 32);
|
|
|
|
|
memcpy(&init_struct.sta.password, g_cfg_app->app_config_wifi_para.wifi_sta_password, 64);
|
|
|
|
|
|
|
|
|
|
|
2025-08-20 15:10:26 +08:00
|
|
|
wifi_netif_init(&init_struct);
|
|
|
|
|
|
|
|
|
|
wifi_set_mode(WIFI_NETIF_MODE_AP);
|
2025-08-28 16:04:01 +08:00
|
|
|
// wifi_set_mode(WIFI_NETIF_MODE_STA);
|
2025-08-20 15:10:26 +08:00
|
|
|
|
|
|
|
|
// 初始化socket
|
|
|
|
|
socket_inet_init();
|
|
|
|
|
|
2025-08-20 16:09:37 +08:00
|
|
|
port->tcp_listen = socket_inet_server_listen_tcp(4278, 2, wifi_event_handler);
|
2025-08-20 15:10:26 +08:00
|
|
|
|
|
|
|
|
socket_server_bind_t bind_param = {
|
|
|
|
|
.rx_buf_size = 0x1000,
|
|
|
|
|
.rx_event = NULL,
|
|
|
|
|
.rx_resume_work = NULL,
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-28 16:04:01 +08:00
|
|
|
port->wifi_tcp = socket_inet_server_bind_tcp(port->tcp_listen, &bind_param);
|
|
|
|
|
|
|
|
|
|
port->wifi_udp = socket_inet_server_bind_udp(14550, &bind_param);
|
2025-08-20 15:10:26 +08:00
|
|
|
|
2025-08-28 16:04:01 +08:00
|
|
|
if (port->wifi_tcp)
|
2025-08-20 15:10:26 +08:00
|
|
|
{
|
2025-08-28 16:04:01 +08:00
|
|
|
sb_data_port_start(port->wifi_tcp);
|
2025-08-20 15:10:26 +08:00
|
|
|
}
|
2025-08-28 16:04:01 +08:00
|
|
|
if (port->wifi_udp)
|
|
|
|
|
{
|
|
|
|
|
sb_data_port_start(port->wifi_udp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wifi_mode_switch(port);
|
2025-08-20 15:10:26 +08:00
|
|
|
|
|
|
|
|
wifi_start();
|
2025-08-20 11:28:11 +08:00
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
2025-08-28 16:04:01 +08:00
|
|
|
void wifi_mode_switch(init_device_t *port)
|
|
|
|
|
{
|
|
|
|
|
static init_device_t* port_lat = NULL;
|
|
|
|
|
static bool wifi_mode_flag = false;
|
|
|
|
|
|
|
|
|
|
if(port_lat == NULL && port != NULL)
|
|
|
|
|
{
|
|
|
|
|
port_lat = port;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-20 11:28:11 +08:00
|
|
|
|
2025-08-28 16:04:01 +08:00
|
|
|
if (wifi_mode_flag)
|
|
|
|
|
{
|
|
|
|
|
wifi_mode_flag = false;
|
|
|
|
|
wifi_set_mode(WIFI_NETIF_MODE_AP);
|
|
|
|
|
port_lat->wifi_mode = WIFI_NETIF_MODE_AP;
|
|
|
|
|
SYS_LOG_INF("wifi mode switch to ap");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
wifi_mode_flag = true;
|
|
|
|
|
wifi_set_mode(WIFI_NETIF_MODE_STA);
|
|
|
|
|
port_lat->wifi_mode = WIFI_NETIF_MODE_STA;
|
|
|
|
|
SYS_LOG_INF("wifi mode switch to sta");
|
|
|
|
|
}
|
|
|
|
|
wifi_start();
|
|
|
|
|
}
|
2025-10-20 12:06:29 +08:00
|
|
|
void device_wifi_start(void)
|
|
|
|
|
{
|
|
|
|
|
device_inside->wifi_flag_off = 0;
|
|
|
|
|
sb_data_port_start(device_inside->init_device.wifi_tcp);
|
|
|
|
|
sb_data_port_start(device_inside->init_device.wifi_udp);
|
|
|
|
|
}
|
|
|
|
|
void device_wifi_stop(void)
|
|
|
|
|
{
|
|
|
|
|
device_inside->wifi_flag_off = 1;
|
|
|
|
|
sb_data_port_stop(device_inside->init_device.wifi_tcp);
|
|
|
|
|
sb_data_port_stop(device_inside->init_device.wifi_udp);
|
|
|
|
|
}
|
|
|
|
|
void device_ble_start(void)
|
|
|
|
|
{
|
|
|
|
|
device_inside->ble_flag_off = 0;
|
|
|
|
|
sb_data_port_start(device_inside->init_device.ble_spp_server_cmd);
|
|
|
|
|
sb_data_port_start(device_inside->init_device.ble_spp_server_val);
|
|
|
|
|
}
|
|
|
|
|
void device_ble_stop(void)
|
|
|
|
|
{
|
|
|
|
|
device_inside->ble_flag_off = 1;
|
|
|
|
|
sb_data_port_stop(device_inside->init_device.ble_spp_server_cmd);
|
|
|
|
|
sb_data_port_stop(device_inside->init_device.ble_spp_server_val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rf_start(device_t *port)
|
|
|
|
|
{
|
|
|
|
|
port->rf_flag_off = 0;
|
|
|
|
|
sb_data_port_start(port->init_device.wifi_tcp);
|
|
|
|
|
sb_data_port_start(port->init_device.wifi_udp);
|
|
|
|
|
sb_data_port_start(port->init_device.ble_spp_server_cmd);
|
|
|
|
|
sb_data_port_start(port->init_device.ble_spp_server_val);
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rf_stop(device_t *port)
|
|
|
|
|
{
|
|
|
|
|
port->rf_flag_off = 1;
|
|
|
|
|
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_RF_OFF, CONNECT_RF_OFF);
|
|
|
|
|
sb_data_port_stop(port->init_device.wifi_tcp);
|
|
|
|
|
sb_data_port_stop(port->init_device.wifi_udp);
|
|
|
|
|
sb_data_port_stop(port->init_device.ble_spp_server_cmd);
|
|
|
|
|
sb_data_port_stop(port->init_device.ble_spp_server_val);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 11:28:11 +08:00
|
|
|
uint8_t embedded_device_choice(device_t *port, uint8_t type)
|
|
|
|
|
{
|
|
|
|
|
//端口与PC一致
|
2025-09-15 16:48:12 +08:00
|
|
|
if((port->pc_device_type == type || port->app_device_type == type) && type != DATA_PORT_TYPE_NONE)
|
2025-08-20 11:28:11 +08:00
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("pc Port consistency");
|
|
|
|
|
return DEVICE_WRN_EMBEDDED_TYPE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->embedded_device_type = type;
|
|
|
|
|
switch (port->embedded_device_type)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
case DATA_PORT_TYPE_NONE:
|
|
|
|
|
port->embedded_device = NULL;
|
|
|
|
|
break;
|
2025-08-20 11:28:11 +08:00
|
|
|
case DATA_PORT_TYPE_UART:
|
|
|
|
|
port->embedded_device = port->init_device.uart_port;
|
|
|
|
|
break;
|
2025-08-28 16:04:01 +08:00
|
|
|
case DATA_PORT_TYPE_WIFI_TCP:
|
|
|
|
|
port->embedded_device = port->init_device.wifi_tcp;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_WIFI_UDP:
|
|
|
|
|
port->embedded_device = port->init_device.wifi_udp;
|
2025-08-20 11:28:11 +08:00
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_BLE_CMD:
|
|
|
|
|
port->embedded_device = port->init_device.ble_spp_server_cmd;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_BLE_VAL:
|
|
|
|
|
port->embedded_device = port->init_device.ble_spp_server_val;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->embedded_device == NULL && type != DATA_PORT_TYPE_NONE)
|
2025-08-20 11:28:11 +08:00
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("embedded device choice error");
|
|
|
|
|
return DEVICE_EMBEDDED_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-09 18:16:48 +08:00
|
|
|
int embedded_device_read(device_t *port, void *buffer, uint32_t length, uint32_t timeout)
|
2025-08-20 11:28:11 +08:00
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->embedded_device == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2025-09-09 18:16:48 +08:00
|
|
|
return sb_data_port_read(port->embedded_device, buffer, length, timeout);
|
2025-08-20 11:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int embedded_device_write(device_t *port, void *buffer, uint32_t length)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->embedded_device == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2025-08-20 11:28:11 +08:00
|
|
|
return sb_data_port_write(port->embedded_device, buffer, length, 0);
|
|
|
|
|
}
|
|
|
|
|
uint32_t embedded_device_get_rx_length(device_t *port)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->embedded_device == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2025-08-20 11:28:11 +08:00
|
|
|
return sb_data_port_get_rx_length(port->embedded_device);
|
2025-08-20 11:50:01 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-20 16:09:37 +08:00
|
|
|
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect)
|
|
|
|
|
{
|
|
|
|
|
static device_t* port_device_inside = NULL;
|
2025-08-28 16:04:01 +08:00
|
|
|
if(port != NULL && port_device_inside == NULL){
|
2025-08-20 16:09:37 +08:00
|
|
|
SYS_LOG_INF("pc_device_choice_inside");
|
2025-08-28 16:04:01 +08:00
|
|
|
pc_device_choice(port, type);
|
2025-08-20 16:09:37 +08:00
|
|
|
port_device_inside = port;
|
2025-08-28 16:04:01 +08:00
|
|
|
port_device_inside->connect_pc = connect;
|
2025-10-20 12:06:29 +08:00
|
|
|
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT || port_device_inside->pc_device_type == DATA_PORT_TYPE_RF_OFF)){
|
2025-08-28 16:04:01 +08:00
|
|
|
pc_device_choice(port_device_inside, type);
|
|
|
|
|
port_device_inside->connect_pc = connect;
|
2025-08-20 16:09:37 +08:00
|
|
|
}
|
|
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
2025-08-20 11:50:01 +08:00
|
|
|
uint8_t pc_device_choice(device_t *port, uint8_t type)
|
|
|
|
|
{
|
|
|
|
|
//端口与embedded一致
|
2025-09-15 16:48:12 +08:00
|
|
|
if((port->embedded_device_type == type || port->app_device_type == type) && type != DATA_PORT_TYPE_NONE)
|
2025-08-20 11:50:01 +08:00
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("embedded Port consistency");
|
|
|
|
|
return DEVICE_WRN_PC_TYPE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->pc_device_type = type;
|
|
|
|
|
switch (port->pc_device_type)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
case DATA_PORT_TYPE_NONE:
|
|
|
|
|
port->pc_device = NULL;
|
|
|
|
|
break;
|
2025-08-20 11:50:01 +08:00
|
|
|
case DATA_PORT_TYPE_UART:
|
|
|
|
|
port->pc_device = port->init_device.uart_port;
|
|
|
|
|
break;
|
2025-08-28 16:04:01 +08:00
|
|
|
case DATA_PORT_TYPE_WIFI_TCP:
|
|
|
|
|
port->pc_device = port->init_device.wifi_tcp;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_WIFI_UDP:
|
|
|
|
|
port->pc_device = port->init_device.wifi_udp;
|
2025-08-20 11:50:01 +08:00
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_BLE_CMD:
|
|
|
|
|
port->pc_device = port->init_device.ble_spp_server_cmd;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_BLE_VAL:
|
|
|
|
|
port->pc_device = port->init_device.ble_spp_server_val;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->pc_device == NULL && type != DATA_PORT_TYPE_NONE)
|
2025-08-20 11:50:01 +08:00
|
|
|
{
|
2025-08-28 16:04:01 +08:00
|
|
|
SYS_LOG_WRN("pc device choice error");
|
2025-08-20 11:50:01 +08:00
|
|
|
return DEVICE_PC_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
|
|
|
|
int pc_device_read(device_t *port, void *buffer, uint32_t length)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
if (port->pc_device == NULL)
|
|
|
|
|
return -1;
|
2025-10-20 12:06:29 +08:00
|
|
|
//判断是否开启射频
|
|
|
|
|
if(port->rf_flag_off && port->pc_device_type > 1)
|
|
|
|
|
return -2;
|
2025-09-15 16:48:12 +08:00
|
|
|
|
2025-08-20 11:50:01 +08:00
|
|
|
return sb_data_port_read(port->pc_device, buffer, length, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pc_device_write(device_t *port, void *buffer, uint32_t length)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->pc_device == NULL)
|
|
|
|
|
return -1;
|
2025-10-20 12:06:29 +08:00
|
|
|
//判断是否开启射频
|
|
|
|
|
if(port->rf_flag_off && port->pc_device_type > 1)
|
|
|
|
|
return -2;
|
|
|
|
|
|
2025-08-20 16:09:37 +08:00
|
|
|
if(port->connect_pc)
|
|
|
|
|
return sb_data_port_write(port->pc_device, buffer, length, 0);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2025-08-20 11:50:01 +08:00
|
|
|
}
|
|
|
|
|
uint32_t pc_device_get_rx_length(device_t *port)
|
|
|
|
|
{
|
2025-09-15 16:48:12 +08:00
|
|
|
if(port->pc_device == NULL)
|
|
|
|
|
return 0;
|
2025-10-20 12:06:29 +08:00
|
|
|
//判断是否开启射频
|
|
|
|
|
if(port->rf_flag_off && port->pc_device_type > 1)
|
|
|
|
|
return 0;
|
2025-08-20 11:50:01 +08:00
|
|
|
return sb_data_port_get_rx_length(port->pc_device);
|
2025-09-15 16:48:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t app_device_choice(device_t *port, uint8_t type)
|
|
|
|
|
{
|
|
|
|
|
//端口与PC一致
|
|
|
|
|
if((port->pc_device_type == type || port->embedded_device_type == type) && type != DATA_PORT_TYPE_NONE)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("pc Port consistency");
|
|
|
|
|
return DEVICE_WRN_EMBEDDED_TYPE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->app_device_type = type;
|
|
|
|
|
switch (port->app_device_type)
|
|
|
|
|
{
|
|
|
|
|
case DATA_PORT_TYPE_NONE:
|
|
|
|
|
port->app_device = NULL;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_UART:
|
|
|
|
|
port->app_device = port->init_device.uart_port;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_WIFI_TCP:
|
|
|
|
|
port->app_device = port->init_device.wifi_tcp;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_WIFI_UDP:
|
|
|
|
|
port->app_device = port->init_device.wifi_udp;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_BLE_CMD:
|
|
|
|
|
port->app_device = port->init_device.ble_spp_server_cmd;
|
|
|
|
|
break;
|
|
|
|
|
case DATA_PORT_TYPE_BLE_VAL:
|
|
|
|
|
port->app_device = port->init_device.ble_spp_server_val;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(port->app_device == NULL && type != DATA_PORT_TYPE_NONE)
|
|
|
|
|
{
|
|
|
|
|
SYS_LOG_WRN("embedded device choice error");
|
|
|
|
|
return DEVICE_EMBEDDED_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DEVICE_OK;
|
|
|
|
|
}
|
|
|
|
|
int app_device_read(device_t *port, void *buffer, uint32_t length, uint32_t timeout)
|
|
|
|
|
{
|
|
|
|
|
if(port->app_device == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2025-10-20 12:06:29 +08:00
|
|
|
//判断是否开启射频
|
|
|
|
|
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
|
|
|
|
return -2;
|
|
|
|
|
|
2025-09-15 16:48:12 +08:00
|
|
|
return sb_data_port_read(port->app_device, buffer, length, timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int app_device_write(device_t *port, void *buffer, uint32_t length)
|
|
|
|
|
{
|
|
|
|
|
if(port->app_device == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2025-10-20 12:06:29 +08:00
|
|
|
//判断是否开启射频
|
|
|
|
|
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
|
|
|
|
return -2;
|
|
|
|
|
|
2025-09-15 16:48:12 +08:00
|
|
|
return sb_data_port_write(port->app_device, buffer, length, 0);
|
|
|
|
|
}
|
|
|
|
|
uint32_t app_device_get_rx_length(device_t *port)
|
|
|
|
|
{
|
|
|
|
|
if(port->app_device == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2025-10-20 12:06:29 +08:00
|
|
|
//判断是否开启射频
|
|
|
|
|
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
|
|
|
|
return -2;
|
|
|
|
|
|
2025-09-15 16:48:12 +08:00
|
|
|
return sb_data_port_get_rx_length(port->app_device);
|
2025-08-20 11:28:11 +08:00
|
|
|
}
|