SLE网关透传测试
该教程演示如何通过两块开发板实现SLE数据传输测试,A开发板通过串口接收数据,然后通过SLE传输给B开发板,B开发板将接收到的数据通过UDP协议发送到UDP服务端;B开发板接收UDP服务端发来的数据,然后通过SLE传输给A开发板,A开发板通过串口将接收到的数据打印出来。在测试中需要一块开发板做为Server端,另外一块开发板做为Client端,两块开发板配对后就可以互发消息了。注意B开发板和UDP服务器需要处于同一个局域网。
一、准备工作
- 准备2块BearPi-Pico H3863开发板
二、编译 SLE GATEWAY Server代码
点击工具栏中的“系统配置”,打开配置界面.
选择Enable SLE GATEWAY Server sample
Application ---> [*] Enable Sample. [ ] Enable the Sample of peripheral. [*] Enable the Sample of products. [ ] Enable all the sample of product, it's just for build. [ ] Support BLE UART sample. [ ] Support SUARTLE sample. [*] Support SLE GATEWAY sample. SLE GATEWAY Sample Configuration ---> Select sle gateway type (Enable SLE GATEWAY Server sample.) ---> (X) Enable SLE GATEWAY Server sample. ( ) Enable SLE GATEWAY Client sample.
按下"Save"键保存配置。
关键代码,位于
application\samples\products\sle_gateway\sle_gateway.c
static void ssaps_server_write_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para, errcode_t status) { osal_printk("%s ssaps write request callback cbk server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, server_id, conn_id, write_cb_para->handle, status); if ((write_cb_para->length > 0) && write_cb_para->value) { osal_printk("\n sle uart recived data : %s\r\n", write_cb_para->value); uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)write_cb_para->value, write_cb_para->length, 0); //将服务端接收到的数据通过UART发送出来 } } static void sle_uart_server_read_int_handler(const void *buffer, uint16_t length, bool error) { unused(error); if (sle_uart_client_is_connected()) { #ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE g_buff_data_valid = 1; g_uart_buff_len = 0; (void)memcpy_s(g_buff, SLE_UART_SERVER_SEND_BUFF_MAX_LEN, buffer, length); g_uart_buff_len = length; #else sle_uart_server_send_report_by_handle(buffer, length); //将UART中断回调接收到的数据发送给客户端 #endif } else { osal_printk("%s sle client is not connected! \r\n", SLE_UART_SERVER_LOG); } } static void *sle_uart_server_task(const char *arg) { ... sle_uart_server_init(ssaps_server_read_request_cbk, ssaps_server_write_request_cbk); //初始化服务端并注册接收中断回调 ... errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, //注册UART中断回调 UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, 1, sle_uart_server_read_int_handler); ... }
编译烧录固件
参考环境搭建教程编译烧录代码
在MobaXterm中输入:
./build.py menuconfig ws63-liteos-app
选择Enable SLE GATEWAY Server sample
Application ---> [*] Enable Sample. [ ] Enable the Sample of peripheral. [*] Enable the Sample of products. [ ] Enable all the sample of product, it's just for build. [ ] Support BLE UART sample. [ ] Support SUARTLE sample. [*] Support SLE GATEWAY sample. SLE GATEWAY Sample Configuration ---> Select sle uart type (Enable SLE GATEWAY Server sample.) ---> (X) Enable SLE GATEWAY Server sample. ( ) Enable SLE GATEWAY Client sample.
按下"ESC"键退出并保存配置。
关键代码,位于
application\samples\products\sle_gateway\sle_gateway.c
static void ssaps_server_write_request_cbk(uint8_t server_id, uint16_t conn_id, ssaps_req_write_cb_t *write_cb_para, errcode_t status) { osal_printk("%s ssaps write request callback cbk server_id:%x, conn_id:%x, handle:%x, status:%x\r\n", SLE_UART_SERVER_LOG, server_id, conn_id, write_cb_para->handle, status); if ((write_cb_para->length > 0) && write_cb_para->value) { osal_printk("\n sle uart recived data : %s\r\n", write_cb_para->value); uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)write_cb_para->value, write_cb_para->length, 0); //将服务端接收到的数据通过UART发送出来 } } static void sle_uart_server_read_int_handler(const void *buffer, uint16_t length, bool error) { unused(error); if (sle_uart_client_is_connected()) { #ifdef CONFIG_SAMPLE_SUPPORT_LOW_LATENCY_TYPE g_buff_data_valid = 1; g_uart_buff_len = 0; (void)memcpy_s(g_buff, SLE_UART_SERVER_SEND_BUFF_MAX_LEN, buffer, length); g_uart_buff_len = length; #else sle_uart_server_send_report_by_handle(buffer, length); //将UART中断回调接收到的数据发送给客户端 #endif } else { osal_printk("%s sle client is not connected! \r\n", SLE_UART_SERVER_LOG); } } static void *sle_uart_server_task(const char *arg) { ... sle_uart_server_init(ssaps_server_read_request_cbk, ssaps_server_write_request_cbk); //初始化服务端并注册接收中断回调 ... errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, //注册UART中断回调 UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, 1, sle_uart_server_read_int_handler); ... }
编译烧录固件
参考环境搭建教程编译烧录代码
三、编译 SLE GATEWAY Client代码
点击工具栏中的“系统配置”,打开配置界面.
选择Enable SLE GATEWAY Client sample
Application ---> [*] Enable Sample. [ ] Enable the Sample of peripheral. [*] Enable the Sample of products. [ ] Enable all the sample of product, it's just for build. [ ] Support BLE UART sample. [ ] Support SUARTLE sample. [*] Support SLE GATEWAY sample. SLE GATEWAY Sample Configuration ---> Select sle uart type (Enable SLE GATEWAY Server sample.) ---> ( ) Enable SLE GATEWAY Server sample. (X) Enable SLE GATEWAY Client sample.
按下"Save"键保存配置。
打开sle_gateway.c,将Wi-Fi的连接信息修改成自己手机开启热点的Wi-Fi账号和密码,服务器IP改成自己电脑的IP,此处注意要将电脑提前接入自己手机开启热点,这样能让开发板与电脑处于同个局域网。
关键代码,位于
application\samples\products\sle_gateway\sle_gateway.c
void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status) { unused(client_id); unused(conn_id); unused(status); osal_printk("\n sle uart recived data : %s\r\n", data->data); uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0); //将客户端接收到的数据通过UART发送出来 sendto(g_sock_fd, (uint8_t *)(data->data), data->data_len, 0, (struct sockaddr *)&g_send_addr, g_addr_length); //将客户端接收到的数据发送到UDP服务端 } void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status) { unused(client_id); unused(conn_id); unused(status); osal_printk("\n sle uart recived data : %s\r\n", data->data); uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0); } static void sle_uart_client_read_int_handler(const void *buffer, uint16_t length, bool error) { unused(error); ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param(); uint16_t g_sle_uart_conn_id = get_g_sle_uart_conn_id(); sle_uart_send_param->data_len = length; sle_uart_send_param->data = (uint8_t *)buffer; ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param); //将UART中断回调接收到的数据发送给服务端 } static void *sle_uart_client_task(const char *arg) { unused(arg); /* UART init config. */ uart_init_config(); /* Wifi connect */ wifi_connect(CONFIG_WIFI_SSID, CONFIG_WIFI_PWD); //连接wifi /* create socket */ printf("create socket start!\r\n"); if ((g_sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { //创建socket printf("create socket failed!\r\n"); return 0; } g_send_addr.sin_family = AF_INET; g_send_addr.sin_port = htons(CONFIG_SERVER_PORT); g_send_addr.sin_addr.s_addr = inet_addr(CONFIG_SERVER_IP); g_addr_length = sizeof(g_send_addr); printf("create socket end!\r\n"); uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS); errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, //注册UART中断回调 UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, 1, sle_uart_client_read_int_handler); sle_uart_client_init(sle_uart_notification_cb, sle_uart_indication_cb); //初始化客户端并注册接收中断回调 if (ret != ERRCODE_SUCC) { osal_printk("Register uart callback fail."); return NULL; } return NULL; }
编译烧录固件
参考环境搭建教程编译烧录代码
在MobaXterm中输入:
./build.py menuconfig ws63-liteos-app
选择Enable SLE GATEWAY Client sample
Application ---> [*] Enable Sample. [ ] Enable the Sample of peripheral. [*] Enable the Sample of products. [ ] Enable all the sample of product, it's just for build. [ ] Support BLE UART sample. [ ] Support SUARTLE sample. [*] Support SLE GATEWAY sample. SLE GATEWAY Sample Configuration ---> Select sle uart type (Enable SLE GATEWAY Server sample.) ---> ( ) Enable SLE GATEWAY Server sample. (X) Enable SLE GATEWAY Client sample.
按下"ESC"键退出并保存配置。
打开sle_gateway.c,将Wi-Fi的连接信息修改成自己手机开启热点的Wi-Fi账号和密码,服务器IP改成自己电脑的IP,此处注意要将电脑提前接入自己手机开启热点,这样能让开发板与电脑处于同个局域网。
关键代码,位于
application\samples\products\sle_gateway\sle_gateway.c
void sle_uart_notification_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status) { unused(client_id); unused(conn_id); unused(status); osal_printk("\n sle uart recived data : %s\r\n", data->data); uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0); //将客户端接收到的数据通过UART发送出来 sendto(g_sock_fd, (uint8_t *)(data->data), data->data_len, 0, (struct sockaddr *)&g_send_addr, g_addr_length); //将客户端接收到的数据发送到UDP服务端 } void sle_uart_indication_cb(uint8_t client_id, uint16_t conn_id, ssapc_handle_value_t *data, errcode_t status) { unused(client_id); unused(conn_id); unused(status); osal_printk("\n sle uart recived data : %s\r\n", data->data); uapi_uart_write(CONFIG_SLE_UART_BUS, (uint8_t *)(data->data), data->data_len, 0); } static void sle_uart_client_read_int_handler(const void *buffer, uint16_t length, bool error) { unused(error); ssapc_write_param_t *sle_uart_send_param = get_g_sle_uart_send_param(); uint16_t g_sle_uart_conn_id = get_g_sle_uart_conn_id(); sle_uart_send_param->data_len = length; sle_uart_send_param->data = (uint8_t *)buffer; ssapc_write_req(0, g_sle_uart_conn_id, sle_uart_send_param); //将UART中断回调接收到的数据发送给服务端 } static void *sle_uart_client_task(const char *arg) { unused(arg); /* UART init config. */ uart_init_config(); /* Wifi connect */ wifi_connect(CONFIG_WIFI_SSID, CONFIG_WIFI_PWD); //连接wifi /* create socket */ printf("create socket start!\r\n"); if ((g_sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { //创建socket printf("create socket failed!\r\n"); return 0; } g_send_addr.sin_family = AF_INET; g_send_addr.sin_port = htons(CONFIG_SERVER_PORT); g_send_addr.sin_addr.s_addr = inet_addr(CONFIG_SERVER_IP); g_addr_length = sizeof(g_send_addr); printf("create socket end!\r\n"); uapi_uart_unregister_rx_callback(CONFIG_SLE_UART_BUS); errcode_t ret = uapi_uart_register_rx_callback(CONFIG_SLE_UART_BUS, //注册UART中断回调 UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, 1, sle_uart_client_read_int_handler); sle_uart_client_init(sle_uart_notification_cb, sle_uart_indication_cb); //初始化客户端并注册接收中断回调 if (ret != ERRCODE_SUCC) { osal_printk("Register uart callback fail."); return NULL; } return NULL; }
编译烧录固件
参考环境搭建教程编译烧录代码
四、测试
使用 Socket 调试工具 创建UDP服务端用于测试。
Socket 调试工具下载地址: 点击跳转
烧录固件后先启动server设备,再启动client设备,在server端串口工具的输入框中输入数据并发送,在Socket 调试工具输入框中输入数据并发送,测试server和client设备之间的数据收发,如下图所示。
注:如果开发板连不上这个UDP服务端,请关闭电脑所有的防火墙和杀毒软件后再尝试重新连接