From d23cce85ba00f0479dbd5033da5987b7bd6ada1b Mon Sep 17 00:00:00 2001 From: OPTOC <9159397+optoc@user.noreply.gitee.com> Date: Wed, 20 Aug 2025 16:09:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=BF=9E=E6=8E=A5=E7=A1=AE?= =?UTF-8?q?=E5=AE=9Awifi=E9=80=9A=E8=AE=AF=E8=BF=98=E6=98=AF=E8=93=9D?= =?UTF-8?q?=E7=89=99=E9=80=9A=E8=AE=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/drivers/sertrf/device.c | 50 +++++++++++++++++++++++++++++++------ app/drivers/sertrf/device.h | 19 ++++++++++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/app/drivers/sertrf/device.c b/app/drivers/sertrf/device.c index 7692129..837e88f 100644 --- a/app/drivers/sertrf/device.c +++ b/app/drivers/sertrf/device.c @@ -8,7 +8,6 @@ uint8_t device_init(device_t *port) res = uart_init(&port->init_device); if(res) { - SYS_LOG_WRN("device start uart error"); return DEVICE_UART_ERROR; } #endif @@ -16,7 +15,6 @@ uint8_t device_init(device_t *port) res = ble_init(&port->init_device); if(res) { - SYS_LOG_WRN("ble init error"); return DEVICE_BLE_ERROR; } #endif @@ -24,7 +22,6 @@ uint8_t device_init(device_t *port) res = wifi_init(&port->init_device); if(res) { - SYS_LOG_WRN("wifi init error"); return DEVICE_WIFI_ERROR; } #endif @@ -36,6 +33,7 @@ uint8_t device_init(device_t *port) } //默认蓝牙 val 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) { 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) { + 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"); } 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"); // disconnected / other } else { @@ -104,13 +108,23 @@ uint8_t ble_init(init_device_t *port) return DEVICE_OK; } -// wifi -socket_listen_tcp_t tcp_listen; +//WIFI连接回调函数 static void wifi_event_handler(bool is_connect, sb_data_port_t *port) { + //实现自动切换发送对象 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"); }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"); } } @@ -145,7 +159,7 @@ uint8_t wifi_init(init_device_t *port) // 初始化socket 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 = { .rx_buf_size = 0x1000, @@ -153,7 +167,7 @@ uint8_t wifi_init(init_device_t *port) .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) { @@ -215,6 +229,23 @@ uint32_t embedded_device_get_rx_length(device_t *port) 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) { //端口与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) { - 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) { diff --git a/app/drivers/sertrf/device.h b/app/drivers/sertrf/device.h index 54618f1..f43931b 100644 --- a/app/drivers/sertrf/device.h +++ b/app/drivers/sertrf/device.h @@ -33,6 +33,13 @@ enum DATA_PORT_TYPE DATA_PORT_TYPE_BLE_CMD, DATA_PORT_TYPE_BLE_VAL }; +enum CONNECT_TYPE +{ + DISCONNECT = 0, + CONNECT_UART, + CONNECT_BLE, + CONNECT_WIFI +}; typedef struct { 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_val; sb_data_port_t* wifi; + + socket_listen_tcp_t tcp_listen; }init_device_t; typedef struct @@ -51,6 +60,8 @@ typedef struct sb_data_port_t* pc_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_write)(device_t *port, const void *data, uint32_t size); @@ -93,6 +104,14 @@ uint8_t wifi_init(init_device_t *port); * @retval 0 成功 */ 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 手机数据接口 *