添加STAwifi模式以及修改灯状态
This commit is contained in:
@@ -40,19 +40,19 @@ uint8_t device_init(device_t *port)
|
||||
{
|
||||
SYS_LOG_ERR("embedded device choice error");
|
||||
}
|
||||
//默认蓝牙 val
|
||||
res = pc_device_choice(port, DATA_PORT_TYPE_WIFI);
|
||||
res = pc_device_choice_inside(port, DATA_PORT_TYPE_BLE_VAL, 0);
|
||||
//默认BLE模式
|
||||
res = pc_device_choice_inside(port, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR)
|
||||
{
|
||||
SYS_LOG_ERR("pc device choice error");
|
||||
}
|
||||
SYS_LOG_INF("device init success");
|
||||
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);
|
||||
port->uart_port = sb_uart_port_bind(1, 115200, 0, 10, 21, 1024, 0, NULL);
|
||||
port->uart_port = sb_uart_port_bind(1, 115200, 21, 20, 21, 1024, 0, NULL);
|
||||
|
||||
if(port->uart_port == NULL)
|
||||
{
|
||||
@@ -76,7 +76,7 @@ static void ble_server_connect_handler(ble_server_status_t status)
|
||||
}
|
||||
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);
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_BLE_VAL, DISCONNECT);
|
||||
if(res == DEVICE_PC_ERROR){
|
||||
}
|
||||
SYS_LOG_INF("ble dis Connected");
|
||||
@@ -122,14 +122,54 @@ 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);
|
||||
uint8_t res = pc_device_choice_inside(NULL, DATA_PORT_TYPE_WIFI_TCP, CONNECT_WIFI_TCP);
|
||||
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);
|
||||
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 disconnect");
|
||||
}
|
||||
}
|
||||
static void wifi_ap_connect_handler(wifi_ap_connect_status_t status, uint8_t connect_cnt)
|
||||
{
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
static void wifi_sta_connect_handler(wifi_sta_connect_status_t status, uint8_t ip_v4[4])
|
||||
{
|
||||
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");
|
||||
@@ -148,22 +188,23 @@ uint8_t wifi_init(init_device_t *port)
|
||||
.gw_v4 = {192, 168, 1, 1},
|
||||
.mask_v4 = {255, 255, 255, 0},
|
||||
.max_connection = 1,
|
||||
.connect_cb = NULL,
|
||||
.connect_cb = wifi_ap_connect_handler,
|
||||
},
|
||||
.sta = {
|
||||
#if 0
|
||||
.ssid = "KFXJ",
|
||||
.password = "Kfdx201*",
|
||||
#else
|
||||
.ssid = "SDWAN",
|
||||
.password = "Dxltkj201",
|
||||
.ssid = "Sertorf_sta",
|
||||
.password = "12345678",
|
||||
#endif
|
||||
.connect_cb = NULL,
|
||||
.connect_cb = wifi_sta_connect_handler,
|
||||
},
|
||||
};
|
||||
wifi_netif_init(&init_struct);
|
||||
|
||||
wifi_set_mode(WIFI_NETIF_MODE_AP);
|
||||
// wifi_set_mode(WIFI_NETIF_MODE_STA);
|
||||
|
||||
// 初始化socket
|
||||
socket_inet_init();
|
||||
@@ -176,17 +217,51 @@ uint8_t wifi_init(init_device_t *port)
|
||||
.rx_resume_work = NULL,
|
||||
};
|
||||
|
||||
port->wifi = socket_inet_server_bind_tcp(port->tcp_listen, &bind_param);
|
||||
port->wifi_tcp = socket_inet_server_bind_tcp(port->tcp_listen, &bind_param);
|
||||
|
||||
if (port->wifi)
|
||||
port->wifi_udp = socket_inet_server_bind_udp(14550, &bind_param);
|
||||
|
||||
if (port->wifi_tcp)
|
||||
{
|
||||
sb_data_port_start(port->wifi);
|
||||
sb_data_port_start(port->wifi_tcp);
|
||||
}
|
||||
if (port->wifi_udp)
|
||||
{
|
||||
sb_data_port_start(port->wifi_udp);
|
||||
}
|
||||
|
||||
wifi_mode_switch(port);
|
||||
|
||||
wifi_start();
|
||||
return DEVICE_OK;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
uint8_t embedded_device_choice(device_t *port, uint8_t type)
|
||||
{
|
||||
//端口与PC一致
|
||||
@@ -202,8 +277,11 @@ uint8_t embedded_device_choice(device_t *port, uint8_t type)
|
||||
case DATA_PORT_TYPE_UART:
|
||||
port->embedded_device = port->init_device.uart_port;
|
||||
break;
|
||||
case DATA_PORT_TYPE_WIFI:
|
||||
port->embedded_device = port->init_device.wifi;
|
||||
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;
|
||||
break;
|
||||
case DATA_PORT_TYPE_BLE_CMD:
|
||||
port->embedded_device = port->init_device.ble_spp_server_cmd;
|
||||
@@ -241,17 +319,15 @@ uint32_t embedded_device_get_rx_length(device_t *port)
|
||||
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){
|
||||
SYS_LOG_WRN("pc_device_choice_inside type %d connect %d", type, connect);
|
||||
if(port != NULL && port_device_inside == NULL){
|
||||
SYS_LOG_INF("pc_device_choice_inside");
|
||||
pc_device_choice(port, type);
|
||||
port_device_inside = port;
|
||||
port_device_inside->connect_pc = connect;
|
||||
}else if(port == NULL && port_device_inside != NULL && (port_device_inside->pc_device_type == type || connect != DISCONNECT)){
|
||||
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;
|
||||
}
|
||||
@@ -270,8 +346,11 @@ uint8_t pc_device_choice(device_t *port, uint8_t type)
|
||||
case DATA_PORT_TYPE_UART:
|
||||
port->pc_device = port->init_device.uart_port;
|
||||
break;
|
||||
case DATA_PORT_TYPE_WIFI:
|
||||
port->pc_device = port->init_device.wifi;
|
||||
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;
|
||||
break;
|
||||
case DATA_PORT_TYPE_BLE_CMD:
|
||||
port->pc_device = port->init_device.ble_spp_server_cmd;
|
||||
@@ -285,7 +364,7 @@ uint8_t pc_device_choice(device_t *port, uint8_t type)
|
||||
|
||||
if(port->pc_device == NULL)
|
||||
{
|
||||
SYS_LOG_WRN("embedded device choice error");
|
||||
SYS_LOG_WRN("pc device choice error");
|
||||
return DEVICE_PC_ERROR;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user