实现连接确定wifi通讯还是蓝牙通讯

This commit is contained in:
OPTOC
2025-08-20 16:09:37 +08:00
parent ff7e6754f4
commit d23cce85ba
2 changed files with 61 additions and 8 deletions

View File

@@ -8,7 +8,6 @@ uint8_t device_init(device_t *port)
res = uart_init(&port->init_device); res = uart_init(&port->init_device);
if(res) if(res)
{ {
SYS_LOG_WRN("device start uart error");
return DEVICE_UART_ERROR; return DEVICE_UART_ERROR;
} }
#endif #endif
@@ -16,7 +15,6 @@ uint8_t device_init(device_t *port)
res = ble_init(&port->init_device); res = ble_init(&port->init_device);
if(res) if(res)
{ {
SYS_LOG_WRN("ble init error");
return DEVICE_BLE_ERROR; return DEVICE_BLE_ERROR;
} }
#endif #endif
@@ -24,7 +22,6 @@ uint8_t device_init(device_t *port)
res = wifi_init(&port->init_device); res = wifi_init(&port->init_device);
if(res) if(res)
{ {
SYS_LOG_WRN("wifi init error");
return DEVICE_WIFI_ERROR; return DEVICE_WIFI_ERROR;
} }
#endif #endif
@@ -36,6 +33,7 @@ uint8_t device_init(device_t *port)
} }
//默认蓝牙 val //默认蓝牙 val
res = pc_device_choice(port, DATA_PORT_TYPE_WIFI); res = pc_device_choice(port, DATA_PORT_TYPE_WIFI);
res = pc_device_choice_inside(port, DATA_PORT_TYPE_BLE_VAL, 0);
if(res == DEVICE_PC_ERROR) if(res == DEVICE_PC_ERROR)
{ {
SYS_LOG_ERR("pc device choice error"); SYS_LOG_ERR("pc device choice error");
@@ -64,8 +62,14 @@ static void ble_server_connect_handler(ble_server_status_t status)
{ {
// 处理连接状态,例如: // 处理连接状态,例如:
if (status == BLE_SERVER_STATUS_CONNECTED) { 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){
}
SYS_LOG_INF("ble Connected"); SYS_LOG_INF("ble Connected");
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){ } else if(status == BLE_SERVER_STATUS_DISCONNECTED){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, 0);
if(res == DEVICE_PC_ERROR){
}
SYS_LOG_INF("ble dis Connected"); SYS_LOG_INF("ble dis Connected");
// disconnected / other // disconnected / other
} else { } else {
@@ -104,13 +108,23 @@ uint8_t ble_init(init_device_t *port)
return DEVICE_OK; return DEVICE_OK;
} }
// wifi //WIFI连接回调函数
socket_listen_tcp_t tcp_listen;
static void wifi_event_handler(bool is_connect, sb_data_port_t *port) static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
{ {
//实现自动切换发送对象
if(is_connect){ if(is_connect){
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI, CONNECT_WIFI);
if(res == DEVICE_PC_ERROR)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi connect"); SYS_LOG_INF("wifi connect");
}else{ }else{
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI, 0);
if(res == DEVICE_PC_ERROR)
{
SYS_LOG_ERR("wifi pc device choice error");
}
SYS_LOG_INF("wifi disconnect"); SYS_LOG_INF("wifi disconnect");
} }
} }
@@ -145,7 +159,7 @@ uint8_t wifi_init(init_device_t *port)
// 初始化socket // 初始化socket
socket_inet_init(); socket_inet_init();
tcp_listen = socket_inet_server_listen_tcp(4278, 2, wifi_event_handler); port->tcp_listen = socket_inet_server_listen_tcp(4278, 2, wifi_event_handler);
socket_server_bind_t bind_param = { socket_server_bind_t bind_param = {
.rx_buf_size = 0x1000, .rx_buf_size = 0x1000,
@@ -153,7 +167,7 @@ uint8_t wifi_init(init_device_t *port)
.rx_resume_work = NULL, .rx_resume_work = NULL,
}; };
port->wifi = socket_inet_server_bind_tcp(tcp_listen, &bind_param); port->wifi = socket_inet_server_bind_tcp(port->tcp_listen, &bind_param);
if (port->wifi) if (port->wifi)
{ {
@@ -215,6 +229,23 @@ uint32_t embedded_device_get_rx_length(device_t *port)
return sb_data_port_get_rx_length(port->embedded_device); return sb_data_port_get_rx_length(port->embedded_device);
} }
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect)
{
static device_t* port_device_inside = NULL;
// if(port_device_inside == NULL && port == NULL)
// {
// SYS_LOG_WRN("pc_device_choice_inside error port is null");
// return DEVICE_PC_ERROR;
// }
if(port == NULL && port_device_inside != NULL){
pc_device_choice(port_device_inside, type);
port_device_inside->connect_pc = connect;
}else{
SYS_LOG_INF("pc_device_choice_inside");
port_device_inside = port;
}
return DEVICE_OK;
}
uint8_t pc_device_choice(device_t *port, uint8_t type) uint8_t pc_device_choice(device_t *port, uint8_t type)
{ {
//端口与embedded一致 //端口与embedded一致
@@ -258,7 +289,10 @@ int pc_device_read(device_t *port, void *buffer, uint32_t length)
int pc_device_write(device_t *port, void *buffer, uint32_t length) int pc_device_write(device_t *port, void *buffer, uint32_t length)
{ {
return sb_data_port_write(port->pc_device, buffer, length, 0); if(port->connect_pc)
return sb_data_port_write(port->pc_device, buffer, length, 0);
return 0;
} }
uint32_t pc_device_get_rx_length(device_t *port) uint32_t pc_device_get_rx_length(device_t *port)
{ {

View File

@@ -33,6 +33,13 @@ enum DATA_PORT_TYPE
DATA_PORT_TYPE_BLE_CMD, DATA_PORT_TYPE_BLE_CMD,
DATA_PORT_TYPE_BLE_VAL DATA_PORT_TYPE_BLE_VAL
}; };
enum CONNECT_TYPE
{
DISCONNECT = 0,
CONNECT_UART,
CONNECT_BLE,
CONNECT_WIFI
};
typedef struct typedef struct
{ {
sb_data_port_t* uart_port; sb_data_port_t* uart_port;
@@ -41,6 +48,8 @@ typedef struct
sb_data_port_t* ble_spp_client_cmd; sb_data_port_t* ble_spp_client_cmd;
sb_data_port_t* ble_spp_client_val; sb_data_port_t* ble_spp_client_val;
sb_data_port_t* wifi; sb_data_port_t* wifi;
socket_listen_tcp_t tcp_listen;
}init_device_t; }init_device_t;
typedef struct typedef struct
@@ -51,6 +60,8 @@ typedef struct
sb_data_port_t* pc_device; sb_data_port_t* pc_device;
init_device_t init_device; init_device_t init_device;
uint8_t connect_embedded;
uint8_t connect_pc;
// int (*embedded_read)(device_t *port, void *buffer, uint32_t length); // int (*embedded_read)(device_t *port, void *buffer, uint32_t length);
// int (*embedded_write)(device_t *port, const void *data, uint32_t size); // int (*embedded_write)(device_t *port, const void *data, uint32_t size);
@@ -93,6 +104,14 @@ uint8_t wifi_init(init_device_t *port);
* @retval 0 成功 * @retval 0 成功
*/ */
uint8_t embedded_device_choice(device_t *port, uint8_t type); uint8_t embedded_device_choice(device_t *port, uint8_t type);
/**
* @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);
/** /**
* @brief PC OR 手机数据接口 * @brief PC OR 手机数据接口
* *