添加RF、wifi、ble功能开关,以及在连接ble关闭wifi、连接wifi关闭ble的功能
This commit is contained in:
@@ -10,6 +10,10 @@
|
||||
*/
|
||||
static uint8_t pc_device_choice_inside(device_t *port, uint8_t type, uint8_t connect);
|
||||
|
||||
// 内部全局变量
|
||||
static uint8_t* rf_flag_off_overall = NULL;
|
||||
static device_t *device_inside;
|
||||
|
||||
uint8_t device_init(device_t *port)
|
||||
{
|
||||
uint8_t res = DEVICE_OK;
|
||||
@@ -53,6 +57,11 @@ uint8_t device_init(device_t *port)
|
||||
SYS_LOG_ERR("app device choice error");
|
||||
}
|
||||
SYS_LOG_INF("device init success");
|
||||
// 赋值全局变量 RF开关
|
||||
rf_flag_off_overall = &port->rf_flag_off;
|
||||
// 赋值设备
|
||||
device_inside = port;
|
||||
|
||||
return DEVICE_OK;
|
||||
}
|
||||
uint8_t uart_init(init_device_t *port)
|
||||
@@ -75,21 +84,26 @@ uint8_t uart_init(init_device_t *port)
|
||||
// 蓝牙连接回调函数
|
||||
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){
|
||||
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");
|
||||
}
|
||||
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){
|
||||
}
|
||||
SYS_LOG_INF("ble dis Connected");
|
||||
// disconnected / other
|
||||
} else {
|
||||
SYS_LOG_INF("ble stop");
|
||||
}
|
||||
|
||||
}
|
||||
static void _sb_manufacturer_encode(uint8_t manufacturer_data[20])
|
||||
{
|
||||
@@ -155,42 +169,50 @@ static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
|
||||
}
|
||||
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");
|
||||
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");
|
||||
}
|
||||
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");
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
uint8_t wifi_init(init_device_t *port)
|
||||
@@ -283,6 +305,51 @@ void wifi_mode_switch(init_device_t *port)
|
||||
}
|
||||
wifi_start();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
uint8_t embedded_device_choice(device_t *port, uint8_t type)
|
||||
{
|
||||
//端口与PC一致
|
||||
@@ -352,13 +419,12 @@ 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;
|
||||
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)){
|
||||
}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)){
|
||||
pc_device_choice(port_device_inside, type);
|
||||
port_device_inside->connect_pc = connect;
|
||||
}
|
||||
@@ -410,6 +476,9 @@ int pc_device_read(device_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
if (port->pc_device == NULL)
|
||||
return -1;
|
||||
//判断是否开启射频
|
||||
if(port->rf_flag_off && port->pc_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_read(port->pc_device, buffer, length, 0);
|
||||
}
|
||||
@@ -418,7 +487,10 @@ int pc_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
if(port->pc_device == NULL)
|
||||
return -1;
|
||||
|
||||
//判断是否开启射频
|
||||
if(port->rf_flag_off && port->pc_device_type > 1)
|
||||
return -2;
|
||||
|
||||
if(port->connect_pc)
|
||||
return sb_data_port_write(port->pc_device, buffer, length, 0);
|
||||
|
||||
@@ -428,7 +500,9 @@ uint32_t pc_device_get_rx_length(device_t *port)
|
||||
{
|
||||
if(port->pc_device == NULL)
|
||||
return 0;
|
||||
|
||||
//判断是否开启射频
|
||||
if(port->rf_flag_off && port->pc_device_type > 1)
|
||||
return 0;
|
||||
return sb_data_port_get_rx_length(port->pc_device);
|
||||
}
|
||||
|
||||
@@ -479,6 +553,10 @@ int app_device_read(device_t *port, void *buffer, uint32_t length, uint32_t time
|
||||
if(port->app_device == NULL)
|
||||
return -1;
|
||||
|
||||
//判断是否开启射频
|
||||
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_read(port->app_device, buffer, length, timeout);
|
||||
}
|
||||
|
||||
@@ -487,6 +565,10 @@ int app_device_write(device_t *port, void *buffer, uint32_t length)
|
||||
if(port->app_device == NULL)
|
||||
return -1;
|
||||
|
||||
//判断是否开启射频
|
||||
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_write(port->app_device, buffer, length, 0);
|
||||
}
|
||||
uint32_t app_device_get_rx_length(device_t *port)
|
||||
@@ -494,5 +576,9 @@ uint32_t app_device_get_rx_length(device_t *port)
|
||||
if(port->app_device == NULL)
|
||||
return 0;
|
||||
|
||||
//判断是否开启射频
|
||||
if((port->rf_flag_off || port->ble_flag_off) && port->app_device_type > 1)
|
||||
return -2;
|
||||
|
||||
return sb_data_port_get_rx_length(port->app_device);
|
||||
}
|
||||
Reference in New Issue
Block a user