实现wifi初始化,以及使用wifi的AP模式实现串口转wifi功能

This commit is contained in:
OPTOC
2025-08-20 15:10:26 +08:00
parent 9abdbeb771
commit ff7e6754f4
3 changed files with 73 additions and 13 deletions

View File

@@ -35,7 +35,7 @@ uint8_t device_init(device_t *port)
SYS_LOG_ERR("embedded device choice error"); SYS_LOG_ERR("embedded device choice error");
} }
//默认蓝牙 val //默认蓝牙 val
res = pc_device_choice(port, DATA_PORT_TYPE_BLE_VAL); res = pc_device_choice(port, DATA_PORT_TYPE_WIFI);
if(res == DEVICE_PC_ERROR) if(res == DEVICE_PC_ERROR)
{ {
SYS_LOG_ERR("pc device choice error"); SYS_LOG_ERR("pc device choice error");
@@ -59,20 +59,25 @@ uint8_t uart_init(init_device_t *port)
} }
return DEVICE_OK; return DEVICE_OK;
} }
// 蓝牙连接回调函数
static void _sb_manufacturer_encode(uint8_t manufacturer_data[20]) static void ble_server_connect_handler(ble_server_status_t status)
{ {
for (int i = 0; i < 20; i++) // 处理连接状态,例如:
{ if (status == BLE_SERVER_STATUS_CONNECTED) {
manufacturer_data[i] = i + 1; SYS_LOG_INF("ble Connected");
} else if(status == BLE_SERVER_STATUS_DISCONNECTED){
SYS_LOG_INF("ble dis Connected");
// disconnected / other
} else {
SYS_LOG_INF("ble stop");
} }
} }
uint8_t ble_init(init_device_t *port) uint8_t ble_init(init_device_t *port)
{ {
ble_server_init_t ble_init_param = { ble_server_init_t ble_init_param = {
.device_name = "Sertorf", // 设备名 .device_name = "Sertorf", // 设备名
.manufacturer_data = _sb_manufacturer_encode, // 回调函数,生成 manufacturer[20] .manufacturer_data = NULL, // 回调函数,生成 manufacturer[20]
.connect_cb = NULL, // 回调函数, NULL 值可用 .connect_cb = ble_server_connect_handler, // 回调函数, NULL 值可用
}; };
//初始化参数 //初始化参数
sb_ble_server_port_init(&ble_init_param); sb_ble_server_port_init(&ble_init_param);
@@ -99,8 +104,63 @@ uint8_t ble_init(init_device_t *port)
return DEVICE_OK; return DEVICE_OK;
} }
// wifi
socket_listen_tcp_t tcp_listen;
static void wifi_event_handler(bool is_connect, sb_data_port_t *port)
{
if(is_connect){
SYS_LOG_INF("wifi connect");
}else{
SYS_LOG_INF("wifi disconnect");
}
}
uint8_t wifi_init(init_device_t *port) uint8_t wifi_init(init_device_t *port)
{ {
/* 初始化 WIFI */
wifi_init_t init_struct = {
.ap = {
.ssid = "Sertorf", /**< SSID of soft-AP. If ssid_len field is 0, this must be a Null terminated string. Otherwise, length is set according to ssid_len. */
.password = "12345678", /**< Password of soft-AP. */
.ip_v4 = {192, 168, 1, 1},
.gw_v4 = {192, 168, 1, 1},
.mask_v4 = {255, 255, 255, 0},
.max_connection = 1,
.connect_cb = NULL,
},
.sta = {
#if 0
.ssid = "KFXJ",
.password = "Kfdx201*",
#else
.ssid = "SDWAN",
.password = "Dxltkj201",
#endif
.connect_cb = NULL,
},
};
wifi_netif_init(&init_struct);
wifi_set_mode(WIFI_NETIF_MODE_AP);
// 初始化socket
socket_inet_init();
tcp_listen = socket_inet_server_listen_tcp(4278, 2, wifi_event_handler);
socket_server_bind_t bind_param = {
.rx_buf_size = 0x1000,
.rx_event = NULL,
.rx_resume_work = NULL,
};
port->wifi = socket_inet_server_bind_tcp(tcp_listen, &bind_param);
if (port->wifi)
{
sb_data_port_start(port->wifi);
}
wifi_start();
return DEVICE_OK; return DEVICE_OK;
} }
@@ -191,7 +251,6 @@ uint8_t pc_device_choice(device_t *port, uint8_t type)
return DEVICE_OK; return DEVICE_OK;
} }
int pc_device_read(device_t *port, void *buffer, uint32_t length) int pc_device_read(device_t *port, void *buffer, uint32_t length)
{ {
return sb_data_port_read(port->pc_device, buffer, length, 0); return sb_data_port_read(port->pc_device, buffer, length, 0);

View File

@@ -6,11 +6,12 @@
#include "../data_port/ble_spp/ble_spp_client.h" #include "../data_port/ble_spp/ble_spp_client.h"
#include "../data_port/socket_inet/socket_inet_server_shell.h" #include "../data_port/socket_inet/socket_inet_server_shell.h"
#include "../data_port/socket_inet/wifi.h" #include "../data_port/socket_inet/wifi.h"
#include "../data_port/socket_inet/socket_inet.h"
#include "sys_log.h" #include "sys_log.h"
#define UART_ENABLE 1 #define UART_ENABLE 1
#define BLE_ENABLE 1 #define BLE_ENABLE 1
#define WIFI_ENABLE 0 #define WIFI_ENABLE 1
enum DEVICE_ERROR enum DEVICE_ERROR
{ {

View File

@@ -6,7 +6,7 @@ void sertrf_init(void)
{ {
uint8_t res = 0; uint8_t res = 0;
res = device_init(&sertrf.device); res = device_init(&sertrf.device);
if(!res) if(res)
{ {
SYS_LOG_WRN("device init error"); SYS_LOG_WRN("device init error");
} }
@@ -32,7 +32,7 @@ void embedded_thread(void* arg)
{ {
uint8_t data[embedded_size]; uint8_t data[embedded_size];
embedded_device_read(&sertrf.device, data, embedded_size); embedded_device_read(&sertrf.device, data, embedded_size);
SYS_LOG_INF("data : %s", data); // SYS_LOG_INF("data : %s", data);
pc_device_write(&sertrf.device, data, embedded_size); pc_device_write(&sertrf.device, data, embedded_size);
} }
embedded_size = pc_device_get_rx_length(&sertrf.device); embedded_size = pc_device_get_rx_length(&sertrf.device);
@@ -40,7 +40,7 @@ void embedded_thread(void* arg)
{ {
uint8_t data[embedded_size]; uint8_t data[embedded_size];
pc_device_read(&sertrf.device, data, embedded_size); pc_device_read(&sertrf.device, data, embedded_size);
SYS_LOG_INF("data : %s", data); // SYS_LOG_INF("data : %s", data);
embedded_device_write(&sertrf.device, data, embedded_size); embedded_device_write(&sertrf.device, data, embedded_size);
} }
//需要添加一些延时,否则会卡死,导致看门狗复位 //需要添加一些延时,否则会卡死,导致看门狗复位