diff --git a/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c b/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c index a09866fa9..18fcc9e8c 100755 --- a/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c +++ b/APP_Framework/Applications/connection_app/socket_demo/lwip_tcp_socket_demo.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2020 AIIT XUOS Lab +* Copyright (c) 2022 AIIT XUOS Lab * XiUOS is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -11,16 +11,14 @@ */ /** -* @file tcp_echo_socket_demo.c -* @brief One UDP demo based on LwIP +* @file lwip_tcp_socket_demo.c +* @brief TCP socket demo based on LwIP * @version 1.0 * @author AIIT XUOS Lab -* @date 2021-05-29 +* @date 2022-03-21 */ #include -#include -#include "board.h" #include "sys_arch.h" #include #include "lwip/sys.h" @@ -28,6 +26,7 @@ #define TCP_DEMO_BUF_SIZE 65535 char tcp_socket_ip[] = {192, 168, 250, 252}; +u16_t tcp_socket_port = LWIP_TARGET_PORT; /******************************************************************************/ @@ -44,36 +43,41 @@ static void TCPSocketRecvTask(void *arg) recv_buf = (char *)malloc(TCP_DEMO_BUF_SIZE); if (recv_buf == NULL) { - lw_print("No memory\n"); - goto __exit; + lw_error("No memory\n"); + continue; } fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { - lw_print("Socket error\n"); - goto __exit; + lw_error("Socket error\n"); + free(recv_buf); + continue; } tcp_addr.sin_family = AF_INET; tcp_addr.sin_addr.s_addr = INADDR_ANY; - tcp_addr.sin_port = htons(LWIP_LOCAL_PORT); + tcp_addr.sin_port = htons(tcp_socket_port); memset(&(tcp_addr.sin_zero), 0, sizeof(tcp_addr.sin_zero)); if (bind(fd, (struct sockaddr *)&tcp_addr, sizeof(struct sockaddr)) == -1) { - lw_print("Unable to bind\n"); - goto __exit; + lw_error("Unable to bind\n"); + closesocket(fd); + free(recv_buf); + continue; } lw_print("tcp bind success, start to receive.\n"); - lw_notice("\n\nLocal Port:%d\n\n", LWIP_LOCAL_PORT); + lw_notice("\n\nLocal Port:%d\n\n", tcp_socket_port); // setup socket fd as listening mode if (listen(fd, 5) != 0 ) { - lw_print("Unable to listen\n"); - goto __exit; + lw_error("Unable to listen\n"); + closesocket(fd); + free(recv_buf); + continue; } // accept client connection @@ -91,26 +95,23 @@ static void TCPSocketRecvTask(void *arg) } sendto(clientfd, recv_buf, recv_len, 0, (struct sockaddr*)&tcp_addr, addr_len); } - - __exit: - if (fd >= 0) - closesocket(fd); - - if (recv_buf) - free(recv_buf); } + + closesocket(fd); + free(recv_buf); } void TCPSocketRecvTest(int argc, char *argv[]) { int result = 0; - pthread_t th_id; - pthread_attr_t attr; - if(argc == 2) + if(argc >= 2) { - lw_print("lw: [%s] gw %s\n", __func__, argv[1]); - sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]); + lw_print("lw: [%s] target ip %s\n", __func__, argv[1]); + if(sscanf(argv[1], "%d.%d.%d.%d:%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3], &tcp_socket_port) == EOK) + { + sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]); + } } lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_socket_ip); @@ -138,7 +139,7 @@ static void TCPSocketSendTask(void *arg) struct sockaddr_in tcp_sock; tcp_sock.sin_family = AF_INET; - tcp_sock.sin_port = htons(LWIP_TARGET_PORT); + tcp_sock.sin_port = htons(tcp_socket_port); tcp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(tcp_socket_ip[0], tcp_socket_ip[1], tcp_socket_ip[2], tcp_socket_ip[3])); memset(&(tcp_sock.sin_zero), 0, sizeof(tcp_sock.sin_zero)); @@ -149,8 +150,7 @@ static void TCPSocketSendTask(void *arg) return; } - lw_print("tcp connect success, start to send.\n"); - lw_notice("\n\nTarget Port:%d\n\n", LWIP_TARGET_PORT); + lw_notice("\n\nTarget Port:%d\n\n", tcp_socket_port); while (cnt --) { @@ -168,14 +168,17 @@ static void TCPSocketSendTask(void *arg) void TCPSocketSendTest(int argc, char *argv[]) { - if(argc == 2) + if(argc >= 2) { - lw_print("lw: [%s] gw %s\n", __func__, argv[1]); - sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]); + lw_print("lw: [%s] target ip %s\n", __func__, argv[1]); + if(sscanf(argv[1], "%d.%d.%d.%d:%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3], &tcp_socket_port) == EOK) + { + sscanf(argv[1], "%d.%d.%d.%d", &tcp_socket_ip[0], &tcp_socket_ip[1], &tcp_socket_ip[2], &tcp_socket_ip[3]); + } } lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_socket_ip); - sys_thread_new("tcp socket", TCPSocketSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); + sys_thread_new("TCP Socket Send", TCPSocketSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), diff --git a/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c b/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c index 24a1a374f..fcc5be0cd 100755 --- a/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c +++ b/APP_Framework/Applications/connection_app/socket_demo/lwip_udp_socket_demo.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2020 AIIT XUOS Lab +* Copyright (c) 2022 AIIT XUOS Lab * XiUOS is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -12,33 +12,25 @@ /** * @file lwip_udp_socket_demo.c -* @brief One UDP demo based on LwIP +* @brief UDP demo based on LwIP * @version 1.0 * @author AIIT XUOS Lab -* @date 2021-05-29 +* @date 2022-03-21 */ #include -#include -#include "board.h" #include "sys_arch.h" -#include "lwip/udp.h" -#include "lwip/opt.h" -#include -#include "lwip/sys.h" +#include "lwip/sockets.h" #define UDP_BUF_SIZE 65536 -extern char udp_target[]; -static struct udp_pcb *udpecho_raw_pcb; char udp_socket_ip[] = {192, 168, 250, 252}; +u16_t udp_socket_port = LWIP_LOCAL_PORT; -/******************************************************************************/ +/*****************************************************************************/ static void UdpSocketRecvTask(void *arg) { - lw_print("UdpSocketRecvTask start.\n"); - - int socket_fd = -1; + int fd = -1; char *recv_buf; struct sockaddr_in udp_addr, server_addr; int recv_len; @@ -49,134 +41,124 @@ static void UdpSocketRecvTask(void *arg) recv_buf = (char *)malloc(UDP_BUF_SIZE); if(recv_buf == NULL) { - lw_print("No memory\n"); - goto __exit; + lw_error("No memory\n"); + continue; } - socket_fd = socket(AF_INET, SOCK_DGRAM, 0); - if(socket_fd < 0) + fd = socket(AF_INET, SOCK_DGRAM, 0); + if(fd < 0) { - lw_print("Socket error\n"); - goto __exit; + lw_error("Socket error\n"); + free(recv_buf); + continue; } udp_addr.sin_family = AF_INET; udp_addr.sin_addr.s_addr = INADDR_ANY; - udp_addr.sin_port = htons(LWIP_LOCAL_PORT); + udp_addr.sin_port = htons(udp_socket_port); memset(&(udp_addr.sin_zero), 0, sizeof(udp_addr.sin_zero)); - if(bind(socket_fd, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1) + if(bind(fd, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr)) == -1) { - lw_print("Unable to bind\n"); - goto __exit; + lw_error("Unable to bind\n"); + closesocket(fd); + free(recv_buf); + continue; } - lw_print("UDP bind sucess, start to receive.\n"); - lw_print("\n\nLocal Port:%d\n\n", LWIP_LOCAL_PORT); + lw_notice("UDP bind sucess, start to receive.\n"); + lw_notice("\n\nLocal Port:%d\n\n", udp_socket_port); while(1) { memset(recv_buf, 0, UDP_BUF_SIZE); - recv_len = recvfrom(socket_fd, recv_buf, UDP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len); - lw_notice("Receive from : %s\n", inet_ntoa(server_addr.sin_addr)); - lw_notice("Receive data : %s\n\n", recv_buf); - sendto(socket_fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len); + recv_len = recvfrom(fd, recv_buf, UDP_BUF_SIZE, 0, (struct sockaddr *)&server_addr, &addr_len); + if(recv_len > 0) + { + lw_notice("Receive from : %s\n", inet_ntoa(server_addr.sin_addr)); + lw_notice("Receive data : %s\n\n", recv_buf); + } + sendto(fd, recv_buf, recv_len, 0, (struct sockaddr*)&server_addr, addr_len); } - __exit: - if(socket_fd >= 0) - { - closesocket(socket_fd); - } - - if(recv_buf) - { - free(recv_buf); - } + closesocket(fd); + free(recv_buf); } } -void UdpSocketRecvTask(int argc, char *argv[]) +void UdpSocketRecvTest(int argc, char *argv[]) { - int result = 0; - pthread_t th_id; - pthread_attr_t attr; - - if(argc == 2) + if(argc >= 2) { - lw_print("lw: [%s] gw %s\n", __func__, argv[1]); - sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]); + lw_notice("lw: [%s] target ip %s\n", __func__, argv[1]); + if(sscanf(argv[1], "%d.%d.%d.%d:%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3], &udp_socket_port) == EOK) + { + sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]); + } } - lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr); + lwip_config_tcp(lwip_ipaddr, lwip_netmask, udp_socket_ip); sys_thread_new("UdpSocketRecvTask", UdpSocketRecvTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), - UDPSocketRecv, UdpSocketRecvTask, UDP recv echo); + UDPSocketRecv, UdpSocketRecvTest, UDP Receive DEMO); static void UdpSocketSendTask(void *arg) { int cnt = LWIP_DEMO_TIMES; char send_str[128]; + int fd = -1; - lw_print("UdpSocketSendTask start.\n"); - - int socket_fd = -1; memset(send_str, 0, sizeof(send_str)); - socket_fd = socket(AF_INET, SOCK_DGRAM, 0); - if(socket_fd < 0) + fd = socket(AF_INET, SOCK_DGRAM, 0); + if(fd < 0) { - lw_print("Socket error\n"); - goto __exit; + lw_error("Socket error\n"); + return; } struct sockaddr_in udp_sock; udp_sock.sin_family = AF_INET; - udp_sock.sin_port = htons(LWIP_TARGET_PORT); - udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_target[0], udp_target[1], udp_target[2], udp_target[3])); + udp_sock.sin_port = htons(udp_socket_port); + udp_sock.sin_addr.s_addr = PP_HTONL(LWIP_MAKEU32(udp_socket_ip[0], udp_socket_ip[1], udp_socket_ip[2], udp_socket_ip[3])); memset(&(udp_sock.sin_zero), 0, sizeof(udp_sock.sin_zero)); - if(connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) + if(connect(fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) { - lw_print("Unable to connect\n"); - goto __exit; + lw_error("Unable to connect\n"); + closesocket(fd); + return; } lw_print("UDP connect success, start to send.\n"); - lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port); + lw_notice("\n\nTarget Port:%d\n\n", udp_sock.sin_port); while (cnt --) { snprintf(send_str, sizeof(send_str), "UDP test package times %d\r\n", cnt); - sendto(socket_fd, send_str, strlen(send_str), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr)); + sendto(fd, send_str, strlen(send_str), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr)); lw_notice("Send UDP msg: %s ", send_str); MdelayKTask(1000); } -__exit: - if(socket_fd >= 0) - { - closesocket(socket_fd); - } - + closesocket(fd); return; } void UdpSocketSendTest(int argc, char *argv[]) { - int result = 0; - pthread_t th_id; - pthread_attr_t attr; - - if(argc == 2) + if(argc >= 2) { - lw_print("lw: [%s] gw %s\n", __func__, argv[1]); - sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]); + lw_notice("lw: [%s] target ip %s\n", __func__, argv[1]); + if(sscanf(argv[1], "%d.%d.%d.%d:%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3], &udp_socket_port) == EOK) + { + sscanf(argv[1], "%d.%d.%d.%d", &udp_socket_ip[0], &udp_socket_ip[1], &udp_socket_ip[2], &udp_socket_ip[3]); + } } - lwip_config_tcp(lwip_ipaddr, lwip_netmask, lwip_gwaddr); + lwip_config_tcp(lwip_ipaddr, lwip_netmask, udp_socket_ip); sys_thread_new("UdpSocketSendTask", UdpSocketSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } diff --git a/APP_Framework/Applications/control_app/opcua_demo/README.md b/APP_Framework/Applications/control_app/opcua_demo/README.md new file mode 100755 index 000000000..9b809af14 --- /dev/null +++ b/APP_Framework/Applications/control_app/opcua_demo/README.md @@ -0,0 +1,16 @@ +# OPCUA DEMO README + +## 文件说明 + +用于OPCUA 相关测试命令演示,需要开启LWIP和OPCUA协议. + +### 命令行 + +UaConnect [IP] + +用于测试与OPCUA服务器连接,连接成功应显示OK + +UaObject [IP] + +用于显示对应的OPCUA设备的节点信息 + diff --git a/APP_Framework/Applications/control_app/plc_demo/README.md b/APP_Framework/Applications/control_app/plc_demo/README.md new file mode 100755 index 000000000..1d361b998 --- /dev/null +++ b/APP_Framework/Applications/control_app/plc_demo/README.md @@ -0,0 +1,48 @@ +# PLC DEMO README + +## 文件说明 + +用于PLC设备相关测试命令演示,目前支持OPCUA协议对PLC进行远程控制,该命令基于LWIP和OPCUA,需要开启相关开关。 + +多个PLC设备可以组成一个channel,用于一条相关业务线控制。 + +### 命令行 + +ShowChannel + +显示注册到channel上的PLC设备,范例如下: + + ch_type ch_name drv_name dev_name cnt +----------------------------------------------------------------- + PLC_Channel PLC OPCUA PLC Demo 4 1 + PLC Demo 3 2 + PLC Demo 2 3 + PLC Demo 1 4 + PLC Demo 0 5 + +ShowPLC + +用于显示PLC,范例如下: + + device vendor model product id +----------------------------------------------------------------- + PLC Demo 4 B&R X20 X20 CP1381 5 + PLC Demo 3 B&R X20 X20 CP1586 4 + PLC Demo 2 SIEMSNS S7-200 CPU SR60 3 + PLC Demo 1 SIEMENS S7-1200 CPU 1215C 2 + PLC Demo 0 SIEMENS S7-1500 CPU 1512SP-1PN 1 + + PlcRead [NodeID] + + 用于读取PLC节点信息 + + - [NodeID]: 如n4,1, 其中4代表namespace,1代表节点号 + + + PlcWrite + + 用于写入PLC节点数值 + + - [NodeID]: 如n4,1, 其中4代表namespace,1代表节点号 + + - [value]: 为写入数值,目前支持bool类型,和int类型。bool型应为0b(代表false), 1b(代表true) diff --git a/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c b/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c index d8f480d59..64cb7d913 100755 --- a/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c +++ b/APP_Framework/Applications/control_app/plc_demo/plc_control_demo.c @@ -101,8 +101,6 @@ void PlcReadUATask(void* arg) if(EOK != ret) { plc_print("plc: [%s] open failed %#x\n", __func__, ret); -// free(plc_demo_dev.priv_data); -// plc_demo_dev.priv_data = NULL; return; } @@ -163,8 +161,6 @@ void PlcWriteUATask(void* arg) if(EOK != ret) { plc_print("plc: [%s] open failed %#x\n", __func__, ret); -// free(plc_demo_dev.priv_data); -// plc_demo_dev.priv_data = NULL; return; } diff --git a/APP_Framework/Framework/control/plc/interoperability/socket/README.md b/APP_Framework/Framework/control/plc/interoperability/socket/README.md new file mode 100755 index 000000000..798e81e3f --- /dev/null +++ b/APP_Framework/Framework/control/plc/interoperability/socket/README.md @@ -0,0 +1,18 @@ +# PLC SOCKET README + +## 文件说明 + +用于测试PLC socket通信. 通过建立与制定IP的PLC设备的socket连接, 发送命令给PLC设备, 实现相关功能. 实现该功能需要开启LWIP, 同时需要扩大shell的栈大小和内存空间。 + +### 命令行 + +PLCSocket ip=[PLC IP] port=[PLC port] tcp=[1: TCP; 0: UDP] cmd=[相关命令] file=[制定配置文件] + +配置文件支持json格式, 默认文件名为socket_param.json, 放置于plc目录下, 文件内容如下: + +{ + "ip": "192.168.250.6", + "port": 102, + "tcp": 1, + "cmd": [x, x, x] +} \ No newline at end of file diff --git a/APP_Framework/Framework/control/plc/interoperability/socket/br_socket.c b/APP_Framework/Framework/control/plc/interoperability/socket/br_socket.c deleted file mode 100755 index a655a0a67..000000000 --- a/APP_Framework/Framework/control/plc/interoperability/socket/br_socket.c +++ /dev/null @@ -1,1867 +0,0 @@ -#include -#include -#include "plc_socket.h" - -#define xs_kprintf KPrintf -#define xs_device_t uint32_t - - -static unsigned char data_head = 0x5A; -static char device_s14[] = "S14"; -static char device_s15[] = "S15"; -static char device_s16[] = "S16"; -static char device_s17[] = "S17"; -static char device_s18[] = "S18"; -static char data_end[] = "#"; -unsigned char redis_data[1024]; - -// 创建一个信号量,用于接收消息的同步 -static sem_t Ch_Sem = NULL; -static sem_t Rx_Sem = NULL; - -extern xs_device_t ec200t; - -//for SIEMENS TCP read data cmd -const unsigned char handshake_first[] = -{ - 0x3, 0x00, 0x00, 0x16, 0x11, 0xe0, 0x00, 0x00, 0x02, - 0xc8,0x00,0xc1,0x02,0x02,0x01,0xc2,0x02,0x02,0x01,0xc0,0x01,0x0a -}; - -const unsigned char handshake_second[] = -{ - 0x3,0x00,0x00,0x19,0x02,0xf0,0x80,0x32,0x01,0x00,0x00,0x00,0x0d,0x00,0x08,0x00,0x00,0xf0,0x00,0x00,0x01,0x00,0x01,0x00,0xf0 -}; - -const unsigned char siemens_read_data[] = -{ - 0x3,0x00,0x00, 0x1f,0x02,0xf0,0x80,0x32,0x01,0x00,0x00,0x33,0x01,0x00,0x0e,0x00,0x00,0x04,0x01,0x12,0x0a,0x10,0x02,0x00,0xD2,0x00,0x34,0x84,0x00,0x00,0x00 -}; - -//for OML UDP read data cmd -const unsigned char UDP_read_data[] = -{ - 0x80,0x00,0x02,0x00,0x03,0x00,0x00,0x7E,0x00,0x00,0x01,0x01,0x82,0x0F,0xA0,0x00,0x00,0x20 -}; - -//for SIEMENS 1200 read data cmd -const unsigned char handshake_1200_first[] = -{ - 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, 0x02, 0xC8, 0x00, 0xC1, 0x02, 0x02, 0x01, 0xC2, 0x02, 0x02, 0x01, 0xC0, 0x01, 0x0A -}; - -const unsigned char handshake_1200_second[] = -{ - 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x08, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xF0 -}; - -const unsigned char siemens_1200_read_data[] = -{ - 0x03, 0x00, 0x00, 0x1F, 0x02, 0xF0, 0x80, 0x32, 0x01, 0x00, 0x00, 0x33, 0x01, 0x00, 0x0E, 0x00, 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02, 0x00, 0xD2, 0x00, 0x34, 0x84, 0x00, 0x00, 0x00 -}; - -#define PUT_ULONG_BE(n,b,i) \ -{ \ - (b)[(i) ] = (uint8_t) ( (n) >> 24 ); \ - (b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \ - (b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \ - (b)[(i) + 3] = (uint8_t) ( (n) ); \ -} - -#define GET_ULONG_BE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} - -#define XS_SERIAL_RB_BUFSZ 128 - -char rx_buffer_cmp[256 + 1]; -char msg_pool[256]; -char rx_buffer[XS_SERIAL_RB_BUFSZ + 1] = {0}; -#define WIFI_RESET 73 - -#define LEN_PARA_BUF 128 -uint8_t server_addr_wifi[LEN_PARA_BUF] = "192.168.23.181"; //??????? -uint8_t server_port_wifi[LEN_PARA_BUF] = "9999"; //?????? - -uint8_t WIFI_ssid[LEN_PARA_BUF] = "xiaoshanos"; //WIFI? -uint8_t WIFI_pwd[LEN_PARA_BUF] = "12345678"; //WIFI???? - -#if 0 -/* ?????? */ -xs_device_t hfa21; -/* ??????? */ -struct xs_MessageQueue* rx3_mq; -/* ???????? */ - -void hfa21_sta_config(void) -{ - xs_kprintf("hfa21_sta_config start\r\n"); - uint8_t i,step; - uint8_t cmd[LEN_PARA_BUF]; - step = 1; - strcpy(cmd, "+++"); - //send - xs_DeviceWrite(hfa21, 0, cmd, 3); - xs_kprintf("cmd.%d=%s\r\n",step,cmd); - step = 2; - memset(cmd,0,sizeof(cmd)); - xs_MdelayKTask(100); - strcpy(cmd,"a"); - //send - xs_DeviceWrite(hfa21, 0, cmd, 1); - xs_kprintf("cmd.%d=%s\r\n",step,cmd); - step = 3; - memset(cmd,0,sizeof(cmd)); - xs_MdelayKTask(2500); - strcpy(cmd, "AT+FCLR\r\n"); - - //send - for(i=0; i 1000) - { - xs_kprintf("data length too long\n "); - return; - } - - xs_DeviceWrite(hfa21, 0, buf, len); - return ; -} - -void msg_send_once(void) -{ - uint8_t msg[1024] = { 0}; - uint32_t i = 0; - xs_kprintf("ap test, send msg to server : "); - memset(msg,0x37,1024); - xs_memcpy(msg,"arm:dev7,data:",14); - msg[1023] = '\n'; - xs_DeviceWrite(hfa21,0, msg, 1024); - // xs_MdelayKTask(1000); - return ; -} -MSH_CMD_EXPORT(msg_send_once, asd); - -void msg_send_nointerval(void) -{ - uint8_t msg[1024] = { 0}; - uint32_t i = 1; - uint32_t index = 0; - uint8_t seq[10] = { 0}; - uint8_t* prefix = "{board:stm32f407-st-discovery,arch:arm,device:7,seq:"; - xs_kprintf("ap test, send msg to server : "); - - for(;;) - { - index = 0; - memset(msg,0x37,1024); - memset(seq,0,10); - msg[1021] = '}'; - msg[1022] = '\n'; - msg[1023] = 0; - xs_memcpy(msg,prefix,strlen(prefix)); - index = index + strlen(prefix); - PUT_ULONG_BE(i, msg, index) - index = index + 4; - xs_memcpy(msg+index,",data:",6); - xs_DeviceWrite(hfa21,0, msg, 1024); - ++ i; - } - - return ; -} -MSH_CMD_EXPORT(msg_send_nointerval, asd); - -void msg_send_interval(void) -{ - uint8_t msg[1024] = { 0}; - uint32_t i = 1; - uint32_t index = 0; - uint8_t seq[4] = { 0}; - uint8_t* prefix = "{board:stm32f407-st-discovery,arch:arm,device:7,seq:"; - xs_kprintf("ap test, send msg to server : "); - - for(;;) - { - index = 0; - memset(msg,0x37,1024); - memset(seq,0,10); - msg[1021] = '}'; - msg[1022] = '\n'; - msg[1023] = 0; - xs_memcpy(msg,prefix,strlen(prefix)); - index = index + strlen(prefix); - PUT_ULONG_BE(i, msg, index) - index = index + 4; - xs_memcpy(msg+index,",data:",6); - xs_DeviceWrite(hfa21,0, msg, 1024); - xs_MdelayKTask(1000); - ++ i; - } - - return ; -} -MSH_CMD_EXPORT(msg_send_interval, asd); - -struct xs_Ringbuffer* ringbuffer; - -uint8_t stack[256] = {0}; -xs_uint16 data_index = 0 ; -uint8_t start = 0xaa; -uint8_t end = 0xbb; - -uint8_t lora_buffer[8][256] = {0}; - -static void read_ringbuffer_thread_entry(void* parameter) -{ - uint8_t ch = 0; - xs_uint16 index = 0 ; - uint8_t devicenumber; - - while(1) - { - if(1 == xs_GetRingBufferchar(ringbuffer,&ch)) - { - if(data_index < 256) - { - stack[data_index++] = ch; - - if(data_index > 2) - { - if(stack[data_index-1] == start && stack[data_index-2] == start) - { - data_index = 0; - stack[data_index++] == start; - stack[data_index++] == start; - } - else if(stack[data_index-1] == end && stack[data_index-2] == end) - { - //end - devicenumber = stack[3] - 0x30; - - if(devicenumber > 0 && devicenumber < 8) - { - memset(lora_buffer[devicenumber-1],0,256); - memcpy(lora_buffer[devicenumber-1], &stack[2],data_index - 4); - // xs_kprintf("lora data: %s\n",lora_buffer[devicenumber-1]); - } - - data_index = 0; - } - } - } - else - { - data_index = 0; - } - } - else - { - xs_MdelayKTask(20); - } - } -} -#endif - -#if 0 -/***********************************************************************/ -//欧姆龙PLC IP 192.168.250.3 port 9600 - -static xs_err_t oml_uart_input(xs_device_t dev, xs_size_t size) -{ - xs_KSemaphoreAbandon(Ch_Sem); - return XS_EOK; -} - -static void oml_plc_thread(void* parameter) -{ - xs_err_t ret; - uint32_t rx_length, total_rx_length = 0; - uint8_t i; - unsigned int length = 0; - - while(1) - { - ret = xs_KSemaphoreObtain(Ch_Sem, XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - rx_length = xs_DeviceRead(hfa21, 0, redis_data + 1 + sizeof(device_s14) + total_rx_length, 78); - xs_kprintf("dst data length %d total length %d\n", rx_length, total_rx_length); - - for(i = 0; i < rx_length; ++i) - { - xs_kprintf("0x%x ", redis_data[i + 1 + sizeof(device_s14) + total_rx_length]); - } - - xs_kprintf("\n"); - total_rx_length += rx_length; - - if((78 == total_rx_length) && (0xC0 == redis_data[1 + sizeof(device_s14)]) - && (0x00 == redis_data[1 + sizeof(device_s14) + 1]) - && (0x02 == redis_data[1 + sizeof(device_s14) + 2]) - && (0x00 == redis_data[1 + sizeof(device_s14) + 3])) - { - /******format redis data******/ - memcpy(redis_data, &data_head, 1); - memcpy(redis_data + 1, device_s14, sizeof(device_s14)); - memcpy(redis_data + 1 + sizeof(device_s14) + total_rx_length, data_end, sizeof(data_end)); - length = 1 + sizeof(device_s14) + total_rx_length + sizeof(data_end); - /******end******/ - xs_DeviceWrite(ec200t, 0, redis_data, length); - total_rx_length = 0; - memset(redis_data, 0, sizeof(redis_data)); - xs_KSemaphoreAbandon(Rx_Sem); - } - } - } -} - -int OML_UDP(void) -{ - struct SerialConfigure config3 = XS_SERIAL_CONFIG_DEFAULT; - hfa21 = xs_DeviceFind("uart3"); - - if(!hfa21) - { - xs_kprintf("find dev.hfa21 failed!\r\n"); - return XS_ERROR; - } - - Ch_Sem = xs_KCreateSemaphore("Ch_Sem",0,XS_LINKLIST_FLAG_FIFO); - - if(Ch_Sem == NULL) - { - xs_kprintf("create Ch_sem failed .\n"); - return XS_ERROR; - } - - Rx_Sem = xs_KCreateSemaphore("Rx_Sem",0,XS_LINKLIST_FLAG_FIFO); - - if(Rx_Sem == NULL) - { - xs_kprintf("create Rx_sem failed .\n"); - return XS_ERROR; - } - - //hfa21 - config3.BaudRate = BAUD_RATE_460800; - config3.DataBits = DATA_BITS_8; - config3.StopBits = STOP_BITS_1; - config3.bufsz = XS_SERIAL_RB_BUFSZ; - config3.parity = PARITY_NONE; - xs_DeviceControl(hfa21, XS_DEVICE_CTRL_CONFIG, &config3); - xs_DeviceOpen(hfa21,XS_DEVICE_FLAG_DMA_RX); - xs_DeviceSetRxIndicate(hfa21, oml_uart_input); - xs_kprintf("hfa21 init success!\r\n"); - TaskDescriptorPointer thread_oml_plc = xs_KTaskCreate("oml_hfa21", oml_plc_thread, NULL, 1024, 25); - - if(thread_oml_plc != NULL) - { - xs_StartupKTask(thread_oml_plc); - } - else - { - return XS_ERROR; - } - - xs_MdelayKTask(10000); - xs_kprintf("The UDP send_receive function is running......\n"); - - while(1) - { -// CH438UARTSend(6, UDP_read_data, sizeof(UDP_read_data)); //UDP_read_data - xs_DeviceWrite(hfa21, 0, UDP_read_data, sizeof(UDP_read_data)); - xs_kprintf("hfa21 write cmd\n"); - xs_KSemaphoreObtain(Rx_Sem, XS_WAITING_FOREVER); - xs_MdelayKTask(1000); - } - - return XS_EOK; -} -MSH_CMD_EXPORT(OML_UDP, oml); - -/*********************************************************************************************/ -// IP 192.168.250.50 port 102 tcp - -//工控机的测试代码,数据帧头FF,帧尾FE,数据帧长度不固?static struct xs_MessageQueue *ipc_mq; -xs_device_t ipc_hfa21; - -static xs_err_t ipc_uart_input(xs_device_t dev, xs_size_t size) -{ - struct rx_msg msg; - xs_err_t result; - msg.dev = dev; - msg.size = size; - result = xs_KMsgQueueSend(ipc_mq, &msg, sizeof(msg)); - - if(result == -XS_EFULL) - { - xs_kprintf("ipc_mq message queue full!\r\n"); - } - - xs_KSemaphoreAbandon(Ch_Sem); - return result; -} - -static void ipc_plc_thread(void* parameter) -{ - struct rx_msg msg; - xs_err_t ret; - uint32_t rx_length, total_rx_length = 0; - uint8_t i; - unsigned int length = 0; - - while(1) - { - ret = xs_KSemaphoreObtain(Ch_Sem, XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - xs_memset(&msg, 0, sizeof(msg)); - ret = xs_KMsgQueueRecv(ipc_mq, &msg, sizeof(msg), XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - rx_length = xs_DeviceRead(ipc_hfa21, 0, redis_data + 2 + sizeof(device_s15) + total_rx_length, msg.size); - xs_kprintf("dst data length %d total length %d\n", rx_length, total_rx_length); - - for(i = 0; i < rx_length; ++i) - { - xs_kprintf("0x%x ", redis_data[i + 2 + sizeof(device_s15) + total_rx_length]); - } - - xs_kprintf("\n"); - total_rx_length += rx_length; - - if((0x46 == redis_data[2 + sizeof(device_s15)]) && (0x46 == redis_data[2 + sizeof(device_s15) + 1]) && - (0x45 == redis_data[2 + sizeof(device_s15) + total_rx_length - 1]) && (0x46 == redis_data[2 + sizeof(device_s15) + total_rx_length - 2])) - { - /******format redis data******/ - redis_data[0] = data_head; - redis_data[1] = PLC_IPC_FLAG; - memcpy(redis_data + 2, device_s15, sizeof(device_s15)); - length = 2 + sizeof(device_s15) + total_rx_length; - /******end******/ - // xs_kprintf("redis data : \n"); - // for(i = 0; i < length; ++i) - // xs_kprintf("0x%x ", redis_data[i]); - // xs_kprintf("\n"); - xs_DeviceWrite(ec200t, 0, redis_data, length); - total_rx_length = 0; - memset(redis_data, 0, sizeof(redis_data)); - xs_KSemaphoreAbandon(Rx_Sem); - } - } - } - } -} - -int BRL_IPC(void) -{ - struct SerialConfigure config3 = XS_SERIAL_CONFIG_DEFAULT; - ipc_hfa21 = xs_DeviceFind("uart3"); - - if(!ipc_hfa21) - { - xs_kprintf("find ipc_hfa21 failed!\r\n"); - return XS_ERROR; - } - - Ch_Sem = xs_KCreateSemaphore("Ch_Sem",0,XS_LINKLIST_FLAG_FIFO); - - if(Ch_Sem == NULL) - { - xs_kprintf("BRL_IPC create sem failed .\n"); - return XS_ERROR; - } - - Rx_Sem = xs_KCreateSemaphore("Rx_Sem",0,XS_LINKLIST_FLAG_FIFO); - - if(Rx_Sem == NULL) - { - xs_kprintf("BRL_IPC create Rx_sem failed .\n"); - return XS_ERROR; - } - - ipc_mq = xs_KCreateMsgQueue("ipc_mq", - sizeof(struct rx_msg), - sizeof(msg_pool), - XS_LINKLIST_FLAG_FIFO); - - if(ipc_mq == NULL) - { - xs_kprintf("create ipc_mq mutex failed.\n"); - return -1; - } - - //ipc_hfa21 - config3.BaudRate = BAUD_RATE_460800; - config3.DataBits = DATA_BITS_8; - config3.StopBits = STOP_BITS_1; - config3.bufsz = XS_SERIAL_RB_BUFSZ; - config3.parity = PARITY_NONE; - xs_DeviceControl(ipc_hfa21, XS_DEVICE_CTRL_CONFIG, &config3); - xs_DeviceOpen(ipc_hfa21,XS_DEVICE_FLAG_DMA_RX); - xs_DeviceSetRxIndicate(ipc_hfa21, ipc_uart_input); - xs_kprintf("ipc_hfa21 init success!\r\n"); - TaskDescriptorPointer thread_ipc_plc = xs_KTaskCreate("ipc_hfa21", ipc_plc_thread, NULL, 1024, 25); - - if(thread_ipc_plc != NULL) - { - xs_StartupKTask(thread_ipc_plc); - } - else - { - return XS_ERROR; - } - - xs_kprintf("start to receive data...\n"); - - while(1) - { - xs_MdelayKTask(100); - xs_KSemaphoreObtain(Rx_Sem, XS_WAITING_FOREVER); - xs_kprintf("\n"); - } - - return XS_EOK; -} -MSH_CMD_EXPORT(BRL_IPC, IPC server); - -/*********************************************************************************************/ -// IP 192.168.250.150 port 9898 tcp - -//金凤工控机的测试代码,数据帧头FF,帧尾EF -xs_device_t jf_ipc_hfa21; -static sem_t jf_input_sem = NULL; -static sem_t jf_ec200t_sem = NULL; - -static xs_err_t jf_ipc_uart_input(xs_device_t dev, xs_size_t size) -{ - xs_KSemaphoreAbandon(jf_input_sem); - return XS_EOK; -} - -static void jf_ipc_plc_thread(void* parameter) -{ - xs_err_t ret; - uint32_t rx_length, total_rx_length = 0; - uint8_t i; - unsigned int length = 0; - - while(1) - { - ret = xs_KSemaphoreObtain(jf_input_sem, XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - rx_length = xs_DeviceRead(jf_ipc_hfa21, 0, redis_data + 2 + sizeof(device_s16) + total_rx_length, 512); - xs_kprintf("dst data length %d total length %d\n", rx_length, total_rx_length); - - for(i = 0; i < rx_length; ++i) - { - xs_kprintf("0x%x ", redis_data[i + 2 + sizeof(device_s16) + total_rx_length]); - } - - xs_kprintf("\n"); - total_rx_length += rx_length; - - if(((((unsigned int)redis_data[2 + sizeof(device_s16) + 2] & 0x000000FF) << 8) | ((unsigned int)redis_data[2 + sizeof(device_s16) + 3] & 0x000000FF) == total_rx_length) && - (0xFF == redis_data[2 + sizeof(device_s16)]) && (0x20 == redis_data[2 + sizeof(device_s16) + 1])) - { - /******format redis data******/ - redis_data[0] = data_head; - redis_data[1] = PLC_JF_IPC_FLAG; - memcpy(redis_data + 2, device_s16, sizeof(device_s16)); - length = 2 + sizeof(device_s16) + total_rx_length; - /******end******/ - xs_DeviceWrite(ec200t, 0, redis_data, length); - total_rx_length = 0; - memset(redis_data, 0, sizeof(redis_data)); - xs_KSemaphoreAbandon(jf_ec200t_sem); - } - } - } -} - -int JF_IPC(void) -{ - struct SerialConfigure config3 = XS_SERIAL_CONFIG_DEFAULT; - jf_ipc_hfa21 = xs_DeviceFind("uart3"); - - if(!jf_ipc_hfa21) - { - xs_kprintf("find jf_ipc_hfa21 failed!\r\n"); - return XS_ERROR; - } - - jf_ec200t_sem = xs_KCreateSemaphore("jf_ec200t_sem",0,XS_LINKLIST_FLAG_FIFO); - - if(jf_ec200t_sem == NULL) - { - xs_kprintf("JF_IPC create jf_ec200t_sem failed .\n"); - return XS_ERROR; - } - - jf_input_sem = xs_KCreateSemaphore("jf_input_sem",0,XS_LINKLIST_FLAG_FIFO); - - if(jf_input_sem == NULL) - { - xs_kprintf("JF_IPC create jf_input_sem failed .\n"); - return XS_ERROR; - } - - //jf_ipc_hfa21 - config3.BaudRate = BAUD_RATE_460800; - config3.DataBits = DATA_BITS_8; - config3.StopBits = STOP_BITS_1; - config3.bufsz = XS_SERIAL_RB_BUFSZ; - config3.parity = PARITY_NONE; - xs_DeviceControl(jf_ipc_hfa21, XS_DEVICE_CTRL_CONFIG, &config3); - xs_DeviceOpen(jf_ipc_hfa21,XS_DEVICE_FLAG_DMA_RX); - xs_DeviceSetRxIndicate(jf_ipc_hfa21, jf_ipc_uart_input); - xs_kprintf("jf_ipc_hfa21 init success!\r\n"); - TaskDescriptorPointer thread_jf_ipc_plc = xs_KTaskCreate("jf_ipc_hfa21", jf_ipc_plc_thread, NULL, 1024, 25); - - if(thread_jf_ipc_plc != NULL) - { - xs_StartupKTask(thread_jf_ipc_plc); - } - else - { - return XS_ERROR; - } - - xs_kprintf("start to receive data...\n"); - - while(1) - { - xs_MdelayKTask(100); - xs_KSemaphoreObtain(jf_ec200t_sem, XS_WAITING_FOREVER); - xs_kprintf("JF send data to server done\n"); - } - - return XS_EOK; -} -MSH_CMD_EXPORT(JF_IPC, JF IPC client); - -/*********************************************************************************************/ -//西门子PLC IP 192.168.250.9 port 102 - -static xs_device_t siemens_hfa21; -static sem_t siemens_input_sem = NULL; -static sem_t siemens_ec200t_sem = NULL; - -static xs_err_t siemens_uart_input(xs_device_t dev, xs_size_t size) -{ - xs_KSemaphoreAbandon(siemens_input_sem); - return XS_EOK; -} - -static void siemens_plc_thread(void* parameter) -{ - xs_err_t ret; - uint32_t rx_length, total_rx_length = 0; - uint8_t i; - static char shakehand_cnt = 0; - unsigned int length = 0; - - while(1) - { - ret = xs_KSemaphoreObtain(siemens_input_sem, XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - //rx_length = xs_DeviceRead(siemens_hfa21, 0, redis_data, 234); - // xs_kprintf("siemens dst data length %d\n", rx_length); - // for(i = 0; i < rx_length; ++i) - // xs_kprintf("0x%x ", redis_data[i]); - // xs_kprintf("\n"); - if(shakehand_cnt < 2) - { - //ignore first two siemens input sem - xs_DeviceRead(siemens_hfa21, 0, redis_data + 2 + sizeof(device_s17), 87); - shakehand_cnt++; - continue; - } - - rx_length = xs_DeviceRead(siemens_hfa21, 0, redis_data + 2 + sizeof(device_s17) + total_rx_length, 87); - xs_kprintf("siemens dst data length %d total length %d\n", rx_length, total_rx_length); - - for(i = 0; i < rx_length; ++i) - { - xs_kprintf("0x%x ", redis_data[i + 2 + sizeof(device_s17) + total_rx_length]); - } - - xs_kprintf("\n"); - total_rx_length += rx_length; - - if((87 == total_rx_length) && (0x03 == redis_data[2 + sizeof(device_s17)]) - && (0x00 == redis_data[2 + sizeof(device_s17) + 1]) - && (0x00 == redis_data[2 + sizeof(device_s17) + 2]) - && (0x57 == redis_data[2 + sizeof(device_s17) + 3])) - { - /******format redis data******/ - redis_data[0] = data_head; - redis_data[1] = PLC_SIEMENS_FLAG; - memcpy(redis_data + 2, device_s17, sizeof(device_s17)); - length = 2 + sizeof(device_s17) + total_rx_length; - /******end******/ - xs_DeviceWrite(ec200t, 0, redis_data, length); - total_rx_length = 0; - memset(redis_data, 0, sizeof(redis_data)); - xs_KSemaphoreAbandon(siemens_ec200t_sem); - } - } - } -} - -int SIEMENS_TCP(void) -{ - xs_err_t result; - struct SerialConfigure config3 = XS_SERIAL_CONFIG_DEFAULT; - siemens_hfa21 = xs_DeviceFind("uart3"); - - if(!siemens_hfa21) - { - xs_kprintf("find siemens_hfa21 failed!\r\n"); - return XS_ERROR; - } - - siemens_input_sem = xs_KCreateSemaphore("siemens_input_sem", 0, XS_LINKLIST_FLAG_FIFO); - - if(siemens_input_sem == NULL) - { - xs_kprintf("siemens_hfa21 create siemens_input_sem failed.\n"); - return XS_ERROR; - } - - siemens_ec200t_sem = xs_KCreateSemaphore("siemens_ec200t_sem", 0, XS_LINKLIST_FLAG_FIFO); - - if(siemens_ec200t_sem == NULL) - { - xs_kprintf("siemens_hfa21 create siemens_ec200t_sem failed.\n"); - return XS_ERROR; - } - - //siemens_hfa21 - config3.BaudRate = BAUD_RATE_460800; - config3.DataBits = DATA_BITS_8; - config3.StopBits = STOP_BITS_1; - config3.bufsz = XS_SERIAL_RB_BUFSZ; - config3.parity = PARITY_NONE; - xs_DeviceControl(siemens_hfa21, XS_DEVICE_CTRL_CONFIG, &config3); - xs_DeviceOpen(siemens_hfa21, XS_DEVICE_FLAG_DMA_RX); - xs_DeviceSetRxIndicate(siemens_hfa21, siemens_uart_input); - TaskDescriptorPointer thread_siemens_plc = xs_KTaskCreate("siemens_hfa21", siemens_plc_thread, NULL, 1024, 25); - - if(thread_siemens_plc != NULL) - { - xs_StartupKTask(thread_siemens_plc); - } - else - { - return XS_ERROR; - } - - //step1 : send handshake_first cmd, waiting for response sem - xs_kprintf("siemens_hfa21 start send handshake_first!\r\n"); - //CH438UARTSend(6, handshake_first, sizeof(handshake_first)); - xs_DeviceWrite(siemens_hfa21, 0, handshake_first, sizeof(handshake_first)); - xs_MdelayKTask(3000); - xs_DeviceWrite(siemens_hfa21, 0, handshake_first, sizeof(handshake_first)); - xs_MdelayKTask(500); - //step2 : send handshake_second cmd, waiting for response sem - xs_kprintf("siemens_hfa21 start send handshake_second!\r\n"); - //CH438UARTSend(6, handshake_second, sizeof(handshake_second)); - xs_DeviceWrite(siemens_hfa21, 0, handshake_second, sizeof(handshake_second)); - xs_kprintf("siemens_hfa21 init success!\r\n"); - xs_MdelayKTask(500); - - while(1) - { - //CH438UARTSend(6, siemens_read_data, sizeof(siemens_read_data)); //read_data - xs_DeviceWrite(siemens_hfa21, 0, siemens_read_data, sizeof(siemens_read_data)); - xs_kprintf("siemens hfa21 write cmd\n"); - xs_KSemaphoreObtain(siemens_ec200t_sem, XS_WAITING_FOREVER); - xs_MdelayKTask(1000); - } - - return XS_EOK; -} -MSH_CMD_EXPORT(SIEMENS_TCP, Siemens TCP PLC); - -/*********************************************************************************************/ -//西门?1200 PLC IP 192.168.250.107 port 102 - -static xs_device_t siemens_1200_hfa21; -static sem_t siemens_1200_input_sem = NULL; -static sem_t siemens_1200_ec200t_sem = NULL; - -static xs_err_t siemens_1200_uart_input(xs_device_t dev, xs_size_t size) -{ - xs_KSemaphoreAbandon(siemens_1200_input_sem); - return XS_EOK; -} - -static void siemens_1200_plc_thread(void* parameter) -{ - xs_err_t ret; - uint32_t rx_length, total_rx_length = 0; - uint8_t i; - static char shakehand_cnt = 0; - unsigned int length = 0; - - while(1) - { - ret = xs_KSemaphoreObtain(siemens_1200_input_sem, XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - if(shakehand_cnt < 2) - { - //ignore first two siemens input sem - xs_DeviceRead(siemens_1200_hfa21, 0, redis_data + 2 + sizeof(device_s18), 235); - shakehand_cnt++; - continue; - } - - rx_length = xs_DeviceRead(siemens_1200_hfa21, 0, redis_data + 2 + sizeof(device_s18) + total_rx_length, 235); - xs_kprintf("siemens 1200 dst data length %d total length %d\n", rx_length, total_rx_length); - - for(i = 0; i < rx_length; ++i) - { - xs_kprintf("0x%x ", redis_data[i + 2 + sizeof(device_s18) + total_rx_length]); - } - - xs_kprintf("\n"); - total_rx_length += rx_length; - - if((235 == total_rx_length) && (0x03 == redis_data[2 + sizeof(device_s18)]) - && (0x00 == redis_data[2 + sizeof(device_s18) + 1]) - && (0x00 == redis_data[2 + sizeof(device_s18) + 2]) - && (0xEB == redis_data[2 + sizeof(device_s18) + 3])) - { - /******format redis data******/ - redis_data[0] = data_head; - redis_data[1] = PLC_SIEMENS_1200_FLAG; - memcpy(redis_data + 2, device_s18, sizeof(device_s18)); - length = 2 + sizeof(device_s18) + total_rx_length; - /******end******/ - xs_DeviceWrite(ec200t, 0, redis_data, length); - total_rx_length = 0; - memset(redis_data, 0, sizeof(redis_data)); - xs_KSemaphoreAbandon(siemens_1200_ec200t_sem); - } - } - } -} - -int SIEMENS_1200(void) -{ - xs_err_t result; - struct SerialConfigure config3 = XS_SERIAL_CONFIG_DEFAULT; - siemens_1200_hfa21 = xs_DeviceFind("uart3"); - - if(!siemens_1200_hfa21) - { - xs_kprintf("find siemens_1200_hfa21 failed!\r\n"); - return XS_ERROR; - } - - siemens_1200_input_sem = xs_KCreateSemaphore("siemens_1200_input_sem", 0, XS_LINKLIST_FLAG_FIFO); - - if(siemens_1200_input_sem == NULL) - { - xs_kprintf("siemens_1200_hfa21 create siemens_1200_input_sem failed.\n"); - return XS_ERROR; - } - - siemens_1200_ec200t_sem = xs_KCreateSemaphore("siemens_1200_ec200t_sem", 0, XS_LINKLIST_FLAG_FIFO); - - if(siemens_1200_ec200t_sem == NULL) - { - xs_kprintf("siemens_1200_hfa21 create siemens_1200_ec200t_sem failed.\n"); - return XS_ERROR; - } - - //siemens_hfa21 - config3.BaudRate = BAUD_RATE_460800; - config3.DataBits = DATA_BITS_8; - config3.StopBits = STOP_BITS_1; - config3.bufsz = XS_SERIAL_RB_BUFSZ; - config3.parity = PARITY_NONE; - xs_DeviceControl(siemens_1200_hfa21, XS_DEVICE_CTRL_CONFIG, &config3); - xs_DeviceOpen(siemens_1200_hfa21, XS_DEVICE_FLAG_DMA_RX); - xs_DeviceSetRxIndicate(siemens_1200_hfa21, siemens_1200_uart_input); - TaskDescriptorPointer thread_siemens_1200_plc = xs_KTaskCreate("siemens_1200_hfa21", siemens_1200_plc_thread, NULL, 1024, 25); - - if(thread_siemens_1200_plc != NULL) - { - xs_StartupKTask(thread_siemens_1200_plc); - } - else - { - return XS_ERROR; - } - - //step1 : send handshake_1200_first cmd, waiting for response sem - xs_kprintf("siemens_1200_hfa21 start send handshake_1200_first!\r\n"); - //CH438UARTSend(6, handshake_1200_first, sizeof(handshake_1200_first)); - xs_DeviceWrite(siemens_1200_hfa21, 0, handshake_1200_first, sizeof(handshake_1200_first)); - xs_MdelayKTask(3000); - xs_DeviceWrite(siemens_1200_hfa21, 0, handshake_1200_first, sizeof(handshake_1200_first)); - xs_MdelayKTask(500); - //step2 : send handshake_1200_second cmd, waiting for response sem - xs_kprintf("siemens_1200_hfa21 start send handshake_1200_second!\r\n"); - //CH438UARTSend(6, handshake_1200_second, sizeof(handshake_1200_second)); - xs_DeviceWrite(siemens_1200_hfa21, 0, handshake_1200_second, sizeof(handshake_1200_second)); - xs_kprintf("siemens_1200_hfa21 init success!\r\n"); - xs_MdelayKTask(500); - - while(1) - { - //CH438UARTSend(6, siemens_1200_read_data, sizeof(siemens_1200_read_data)); //read_data - xs_DeviceWrite(siemens_1200_hfa21, 0, siemens_1200_read_data, sizeof(siemens_1200_read_data)); - xs_kprintf("siemens 1200 hfa21 write cmd\n"); - xs_KSemaphoreObtain(siemens_1200_ec200t_sem, XS_WAITING_FOREVER); - xs_MdelayKTask(1000); - } - - return XS_EOK; -} -MSH_CMD_EXPORT(SIEMENS_1200, Siemens 1200 PLC); -#endif - -/*********************************************************************************************/ -//贝加莱PLC IP 192.168.250.4 port 12000 -static unsigned char brl_redis_data[2048]; -static xs_device_t brl_plc_hfa21; -static sem_t brl_plc_input_sem = NULL; -static sem_t brl_plc_ec200t_sem = NULL; - -static xs_err_t brl_plc_uart_input(xs_device_t dev, xs_size_t size) -{ - xs_KSemaphoreAbandon(brl_plc_input_sem); - return XS_EOK; -} - -static void brl_plc_thread(void* parameter) -{ - xs_err_t ret; - uint32_t rx_length, total_rx_length = 0; - uint8_t i; - unsigned int length = 0; - - while(1) - { - ret = xs_KSemaphoreObtain(brl_plc_input_sem, XS_WAITING_FOREVER); - - if(XS_EOK == ret) - { - xs_kprintf("before total %d\n", total_rx_length); - rx_length = xs_DeviceRead(brl_plc_hfa21, 0, brl_redis_data + 2 + total_rx_length, 1640); - xs_kprintf("brl dst data length %d total length %d\n", rx_length, total_rx_length); - - for(i = 0; i < rx_length / 10; i ++) - { - xs_kprintf("0x%x ", brl_redis_data[i + 2 + total_rx_length]); - } - - xs_kprintf("\n"); - total_rx_length += rx_length; - - if(total_rx_length > 1640) - { - xs_kprintf("ERROR : rx_buffer is full total_rx_length %d\n", total_rx_length); - total_rx_length = 0; - memset(brl_redis_data, 0, sizeof(brl_redis_data)); - xs_KSemaphoreAbandon(brl_plc_ec200t_sem); - continue; - } - - if((1640 == total_rx_length) && (0x53 == brl_redis_data[2])) - { - /******format redis data******/ - brl_redis_data[0] = data_head; - brl_redis_data[1] = PLC_BRL_FLAG; - length = 2 + total_rx_length; - - /******end******/ - for(i = 0; i < 10; i ++) - { - xs_kprintf("0x%x ", brl_redis_data[i + 1478]); - } - - xs_kprintf("\n"); - xs_DeviceWrite(ec200t, 0, brl_redis_data, length); - total_rx_length = 0; - memset(brl_redis_data, 0, sizeof(brl_redis_data)); - xs_KSemaphoreAbandon(brl_plc_ec200t_sem); - } - } - } -} - -int BRL_PLC(void) -{ - xs_err_t result; - const unsigned char brl_reply_data[] = {0x1, 0x1, 0x1}; - struct SerialConfigure config3 = XS_SERIAL_CONFIG_DEFAULT; - brl_plc_hfa21 = xs_DeviceFind("uart3"); - - if(!brl_plc_hfa21) - { - xs_kprintf("find brl_plc_hfa21 failed!\r\n"); - return XS_ERROR; - } - - brl_plc_input_sem = xs_KCreateSemaphore("brl_plc_input_sem", 0, XS_LINKLIST_FLAG_FIFO); - - if(brl_plc_input_sem == NULL) - { - xs_kprintf("brl_plc_hfa21 create brl_plc_input_sem failed.\n"); - return XS_ERROR; - } - - brl_plc_ec200t_sem = xs_KCreateSemaphore("brl_plc_ec200t_sem", 0, XS_LINKLIST_FLAG_FIFO); - - if(brl_plc_ec200t_sem == NULL) - { - xs_kprintf("brl_plc_hfa21 create brl_plc_ec200t_sem failed.\n"); - return XS_ERROR; - } - - //brl_hfa21 - config3.BaudRate = BAUD_RATE_460800; - config3.DataBits = DATA_BITS_8; - config3.StopBits = STOP_BITS_1; - config3.bufsz = 2048; - config3.parity = PARITY_NONE; - xs_DeviceControl(brl_plc_hfa21, XS_DEVICE_CTRL_CONFIG, &config3); - xs_DeviceOpen(brl_plc_hfa21, XS_DEVICE_FLAG_DMA_RX); - xs_DeviceSetRxIndicate(brl_plc_hfa21, brl_plc_uart_input); - TaskDescriptorPointer thread_brl_plc = xs_KTaskCreate("brl_plc_hfa21", brl_plc_thread, NULL, 1024, 25); - - if(thread_brl_plc != NULL) - { - xs_StartupKTask(thread_brl_plc); - } - else - { - return XS_ERROR; - } - - xs_kprintf("brl_plc_hfa21 init success!\r\n"); - xs_MdelayKTask(500); - - while(1) - { - //CH438UARTSend(6, brl_reply_data, sizeof(brl_reply_data)); - xs_kprintf("brl_plc hfa21 write cmd\n"); //read_data - xs_DeviceWrite(brl_plc_hfa21, 0, brl_reply_data, sizeof(brl_reply_data)); - xs_KSemaphoreObtain(brl_plc_ec200t_sem, XS_WAITING_FOREVER); - } - - return XS_EOK; -} - -MSH_CMD_EXPORT(BRL_PLC, Brl PLC); - diff --git a/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.c b/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.c index a69d5dc9d..74eba30c9 100755 --- a/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.c +++ b/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.c @@ -24,56 +24,18 @@ #include "lwip/sockets.h" #include "control_file.h" -#define PLC_SOCK_CMD_NUM 10 +// max support plc socket test commands number +#define PLC_SOCK_CMD_NUM CTL_CMD_NUM +#define PLC_SOCK_TIMEOUT 50000 -// for saving PLC command +// for saving PLC command index int plc_cmd_index = 0; +// only for test +#define SUPPORT_PLC_SIEMENS + //siemens test -PlcBinCmdType TestPlcCmd[PLC_SOCK_CMD_NUM] = -{ -#ifdef SUPPORT_PLC_SIEMENS - // handshake1 repeat 1 - { - 0, 3000, 22, - { - 0x03, 0x00, 0x00, 0x16, 0x11, 0xE0, 0x00, 0x00, - 0x02, 0xC8, 0x00, 0xC1, 0x02, 0x02, 0x01, 0xC2, - 0x02, 0x02, 0x01, 0xC0, 0x01, 0x0A - } - }, - // handshake2 - { - 1, 500, 25, - { - 0x03, 0x00, 0x00, 0x19, 0x02, 0xF0, 0x80, 0x32, - 0x01, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x08, 0x00, - 0x00, 0xF0, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, - 0xF0 - } - }, - // read command - { - 2, 1000, 31, - { - 0x03, 0x00, 0x00, 0x1F, 0x02, 0xF0, 0x80, 0x32, - 0x01, 0x00, 0x00, 0x33, 0x01, 0x00, 0x0E, 0x00, - 0x00, 0x04, 0x01, 0x12, 0x0A, 0x10, 0x02, 0x00, - 0xD2, 0x00, 0x34, 0x84, 0x00, 0x00, 0x00 - } - } - // oml plc -#else// SUPPORT_PLC_OML - { - 0, 1000, 18, - { - 0x80, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x7E, - 0x00, 0x00, 0x01, 0x01, 0x82, 0x0F, 0xA0, 0x00, - 0x00, 0x20 - } - } -#endif -}; +PlcBinCmdType TestPlcCmd[PLC_SOCK_CMD_NUM] = {0}; //Test information //SIEMENS ip: 192.168.250.9 port: 102 @@ -82,14 +44,21 @@ PlcBinCmdType TestPlcCmd[PLC_SOCK_CMD_NUM] = //OML ip: 192.168.250.3 port: 9600 PlcSocketParamType plc_socket_demo_data = { +#ifdef SUPPORT_PLC_SIEMENS + .ip = {192, 168, 250, 6}, + .port = 102, + .device_type = PLC_DEV_TYPE_SIEMENS, + .socket_type = SOCK_STREAM, + .cmd_num = 3, +#else .ip = {192, 168, 250, 3}, .port = 9600, .device_type = PLC_DEV_TYPE_OML, - .socket_type = SOCK_STREAM, //SOCK_DGRAM, //udp - .step = 0, - .cmd_num = 3, - .buf_size = PLC_TEST_SIZE, - .buf = NULL, + .socket_type = SOCK_DGRAM, + .cmd_num = 1, +#endif + .recv_len = PLC_RECV_BUF_LEN, + .recv_buf = NULL, }; #define OML_HEADER_LEN 78 @@ -97,12 +66,20 @@ PlcSocketParamType plc_socket_demo_data = { /******************************************************************************/ +static void plc_print_array(char *title, int size, uint8_t *cmd) +{ + lw_notice("%s : %d - ", title, size); + for(int i = 0; i < size; i++) + { + lw_notice(" %#x", cmd[i]); + } + lw_notice("\n"); +} + static void *PlcSocketStart(void *arg) { int fd = -1; - int recv_len; - int step = 0; - char *recv_buf; + int timeout, recv_len; struct sockaddr_in sock_addr; socklen_t addr_len = sizeof(struct sockaddr_in); PlcSocketParamType *param = (PlcSocketParamType *)&plc_socket_demo_data; @@ -116,9 +93,11 @@ static void *PlcSocketStart(void *arg) param->device_type, param->socket_type); + param->recv_len = PLC_RECV_BUF_LEN; + //malloc memory - recv_buf = (char *)malloc(param->buf_size); - if (recv_buf == NULL) + param->recv_buf = (char *)malloc(param->recv_len); + if (param->recv_buf == NULL) { plc_error("No memory\n"); return NULL; @@ -128,7 +107,7 @@ static void *PlcSocketStart(void *arg) if (fd < 0) { plc_error("Socket error %d\n", param->socket_type); - free(recv_buf); + free(param->recv_buf); return NULL; } @@ -143,51 +122,42 @@ static void *PlcSocketStart(void *arg) { plc_error("Unable to connect\n"); closesocket(fd); - free(recv_buf); + free(param->recv_buf); return NULL; } lw_notice("client %s connected\n", inet_ntoa(sock_addr.sin_addr)); - while(step < param->cmd_num) + for(int i = 0; i < param->cmd_num; i ++) { - sendto(fd, TestPlcCmd[step].cmd, TestPlcCmd[step].cmd_len, 0, (struct sockaddr*)&sock_addr, addr_len); - lw_notice("Send Cmd: %d - ", TestPlcCmd[step].cmd_len); - for(int i = 0; i < TestPlcCmd[step].cmd_len; i++) - { - lw_notice(" %#x", TestPlcCmd[step].cmd[i]); - } - lw_notice("\n"); - MdelayKTask(TestPlcCmd[step].delay_ms); + PlcBinCmdType *cmd = &TestPlcCmd[i]; + sendto(fd, cmd->cmd, cmd->cmd_len, 0, (struct sockaddr*)&sock_addr, addr_len); + plc_print_array("Send cmd", cmd->cmd_len, cmd->cmd); - memset(recv_buf, 0, param->buf_size); - while(1) + MdelayKTask(cmd->delay_ms); + timeout = PLC_SOCK_TIMEOUT; + memset(param->recv_buf, 0, param->recv_len); + while(timeout --) { - recv_len = recvfrom(fd, recv_buf, param->buf_size, 0, (struct sockaddr *)&sock_addr, &addr_len); + recv_len = recvfrom(fd, param->recv_buf, param->recv_len, 0, (struct sockaddr *)&sock_addr, &addr_len); if(recv_len > 0) { if(param->device_type == PLC_DEV_TYPE_OML) { - if((recv_len == OML_HEADER_LEN) && (CHECK_OML_HEADER(recv_buf))) + if((recv_len == OML_HEADER_LEN) && (CHECK_OML_HEADER(param->recv_buf))) { lw_notice("This is Oml package!!!\n"); } } lw_notice("Receive from : %s\n", inet_ntoa(sock_addr.sin_addr)); - lw_notice("Receive data : %d -", recv_len); - for(int i = 0; i < recv_len; i++) - { - lw_notice(" %#x", recv_buf[i]); - } - lw_notice("\n"); + plc_print_array("Receive data", recv_len, param->recv_buf); break; } } - step ++; } closesocket(fd); - free(recv_buf); + free(param->recv_buf); return NULL; } @@ -221,29 +191,46 @@ void PlcShowUsage(void) plc_notice("tcp=[] 0: udp 1:tcp\n"); plc_notice("ip=[ip.ip.ip.ip]\n"); plc_notice("port=port\n"); + plc_notice("file: use %s\n", PLC_SOCK_FILE_NAME); plc_notice("------------------------------------\n"); } -void PlcGetParamFromFile(void) +#if defined(MOUNT_SDCARD) && defined(LIB_USING_CJSON) +void PlcGetParamFromFile(char *file_name) { PlcSocketParamType *param = &plc_socket_demo_data; - //for PLC socket parameter file - char file_buf[CTL_FILE_SIZE] = {0}; - FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME); - - if(fd == NULL) + char *file_buf = malloc(CTL_FILE_LEN); + if(file_buf == NULL) + { + plc_error("No enough buffer %d\n", CTL_FILE_LEN); return; + } + memset(file_buf, 0, CTL_FILE_LEN); - memset(file_buf, 0, CTL_FILE_SIZE); - - CtlFileRead(fd, CTL_FILE_SIZE, file_buf); - CtlFileClose(fd); + if(CtlFileReadWithFilename(file_name, CTL_FILE_LEN, file_buf) != EOK) + { + plc_error("Can't open file %s\n", file_name); + //try again default file + if(strcmp(file_name, PLC_SOCK_FILE_NAME) != 0) + { + if(CtlFileReadWithFilename(PLC_SOCK_FILE_NAME, CTL_FILE_LEN, file_buf) != EOK) + { + plc_error("Can't open file %s\n", file_name); + return; + } + } + else + { + return; + } + } CtlParseJsonData(file_buf); memcpy(param->ip, ctl_file_param.ip, 4); param->port = ctl_file_param.port; param->cmd_num = ctl_file_param.cmd_num; + param->socket_type = ctl_file_param.tcp ? SOCK_STREAM : SOCK_DGRAM; for(int i = 0; i < param->cmd_num; i++) { @@ -253,17 +240,18 @@ void PlcGetParamFromFile(void) plc_print("ip: %d.%d.%d.%d\n", param->ip[0], param->ip[1], param->ip[2], param->ip[3]); plc_print("port: %d", param->port); + plc_print("tcp: %d", param->socket_type); plc_print("cmd number: %d\n", param->cmd_num); for(int i = 0; i < param->cmd_num; i++) { - plc_print("cmd %d len %d: ", i, TestPlcCmd[i].cmd_len); - for(int j = 0; j < TestPlcCmd[i].cmd_len; j++) - plc_print("%x ", TestPlcCmd[i].cmd[j]); - plc_print("\n"); + plc_print_array("cmd", TestPlcCmd[i].cmd_len, TestPlcCmd[i].cmd); } + free(file_buf); } +#endif + void PlcCheckParam(int argc, char *argv[]) { int i; @@ -278,13 +266,19 @@ void PlcCheckParam(int argc, char *argv[]) plc_print("check %d %s\n", i, str); - if(strcmp(str, "file") == 0) +#if defined(MOUNT_SDCARD) && defined(LIB_USING_CJSON) + if(strncmp(str, "file", 4) == 0) { - plc_notice("get parameter file %s\n", PLC_SOCK_FILE_NAME); - PlcGetParamFromFile(); + char file_name[CTL_FILE_NAME_LEN] = {0}; + if(sscanf(str, "file=%s", file_name) == EOF) + { + strcpy(file_name, PLC_SOCK_FILE_NAME); + } + plc_notice("get %s parameter file %s\n", str, file_name); + PlcGetParamFromFile(file_name); return; } - +#endif if(sscanf(str, "ip=%d.%d.%d.%d", ¶m->ip[0], ¶m->ip[1], diff --git a/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.h b/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.h index 4801c63e5..6b4ec9b60 100755 --- a/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.h +++ b/APP_Framework/Framework/control/plc/interoperability/socket/plc_socket.h @@ -23,9 +23,9 @@ #define PLC_BIN_CMD_LEN 512 +// for plc socket test bin commands typedef struct { - uint8_t step; uint16_t delay_ms; uint8_t cmd_len; uint8_t cmd[PLC_BIN_CMD_LEN]; @@ -43,28 +43,23 @@ enum PlcDeviceType { PLC_DEV_TYPE_END, }; -#define PLC_IP_SIZE 16 -#define PLC_DEV_SIZE 32 -#define PLC_TEST_SIZE 65536 +#define PLC_IP_LEN 16 +#define PLC_DEV_NAME_LEN 32 +#define PLC_RECV_BUF_LEN CTL_FILE_LEN typedef struct PlcSocketParamStruct{ - char ip[PLC_IP_SIZE]; + char ip[PLC_IP_LEN]; uint32_t port; uint32_t device_type; //PlcDeviceType uint32_t socket_type; //UDP or TCP - char device[PLC_DEV_SIZE]; - uint32_t step; // communication step + char device[PLC_DEV_NAME_LEN]; uint32_t cmd_num; // command number - uint32_t buf_size; - uint8_t *buf; + uint32_t recv_len; // receive length + uint8_t *recv_buf; // receive buffer }PlcSocketParamType; //debug command -<<<<<<< HEAD -#define plc_print KPrintf -======= #define plc_print //KPrintf ->>>>>>> e5d124231c72798f7f77b842cc8c631b79043914 #define plc_error KPrintf #define plc_notice KPrintf diff --git a/APP_Framework/Framework/control/shared/control_file.c b/APP_Framework/Framework/control/shared/control_file.c index d906beca8..3b383c7f3 100755 --- a/APP_Framework/Framework/control/shared/control_file.c +++ b/APP_Framework/Framework/control/shared/control_file.c @@ -36,6 +36,7 @@ "{ \r\n"\ " \"ip\": \"192.168.250.6\", \r\n"\ " \"port\": 102, \r\n"\ +" \"tcp\": 1, \r\n"\ " \"cmd\": [3, 0, 0, 22, 17, 224, 0, 0, 2, 200, 0, 193, 2, 2, 1, 194, 2, 2, 1, 192, 1, 10], \r\n"\ " \"cmd1\": [3, 0, 0, 25, 2, 240, 128, 50, 1, 0, 0, 0, 13, 0, 8, 0, 0, 240, 0, 0, 1, 0, 1, 0, 240], \r\n" \ " \"cmd2\": [3, 0, 0, 31, 2, 240, 128, 50, 1, 0, 0, 51, 1, 0, 14, 0, 0, 4, 1, 18, 10, 16, 2, 0, 210, 0, 52, 132, 0, 0, 0]\r\n" \ @@ -48,12 +49,21 @@ FILE *CtlFileInit(char *file) { FILE *fd = NULL; +#ifdef MOUNT_SDCARD + // SD card mount flag 1: OK + if(sd_mount_flag == 0) + { + ctl_error("SD card mount failed\n"); + return NULL; + } + fd = fopen(file, "a+"); if(fd == NULL) { ctl_error("open file %s failed\n", file); } +#endif return fd; } @@ -76,6 +86,22 @@ void CtlFileWrite(FILE *fd, int size, char *buf) ctl_print("write size %d: %s\n", size, buf); } +int CtlFileReadWithFilename(char *file, int size, char *buf) +{ + FILE *fd; + fd = fopen(file, "r"); + if(fd == NULL) + { + ctl_error("open file %s failed\n", file); + return EEMPTY; + } + + fseek(fd, 0, SEEK_SET); + fread(buf, size, 1, fd); + ctl_print("read file %d: %.100s\n", size, buf); + return EOK; +} + void CtlCreateFileTest(void) { FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME); @@ -117,10 +143,9 @@ void CtlParseJsonData(char *buf) cJSON *file_dat = NULL; cJSON *ip_dat = NULL; cJSON *port_dat = NULL; + cJSON *tcp_dat = NULL; cJSON *cmd_dat = NULL; - int cmd_num = 0; - int cmd_index = 1; - char cmd_str[10] = {0}; + char cmd_title[10] = {"cmd"}; CtlPlcSockParamType *file_param = &ctl_file_param; file_dat = cJSON_Parse(buf); @@ -132,6 +157,7 @@ void CtlParseJsonData(char *buf) ip_dat = cJSON_GetObjectItem(file_dat, "ip"); port_dat = cJSON_GetObjectItem(file_dat, "port"); + tcp_dat = cJSON_GetObjectItem(file_dat, "tcp"); ctl_print(" ip : %s\n", ip_dat->valuestring); sscanf(ip_dat->valuestring, "%d.%d.%d.%d", &file_param->ip[0], @@ -141,14 +167,17 @@ void CtlParseJsonData(char *buf) ctl_print(" port: %s %d\n", ip_dat->string, port_dat->valueint); file_param->port = port_dat->valueint; + file_param->tcp = tcp_dat->valueint; + file_param->cmd_num = 0; - strcpy(cmd_str, "cmd"); - while(cmd_dat = cJSON_GetObjectItem(file_dat, cmd_str)) + for(int i = 0; i < CTL_CMD_NUM; i++) { - CtlParseJsonArray(cmd_dat, &file_param->cmd_len[cmd_index - 1], file_param->cmd[cmd_index - 1]); - snprintf(cmd_str, sizeof(cmd_str), "cmd%d", cmd_index++); + cmd_dat = cJSON_GetObjectItem(file_dat, cmd_title); + if(!cmd_dat) + break; + CtlParseJsonArray(cmd_dat, &file_param->cmd_len[i], file_param->cmd[i]); + snprintf(cmd_title, sizeof(cmd_title), "cmd%d", ++file_param->cmd_num); } - file_param->cmd_num = cmd_index - 1; cJSON_Delete(file_dat); } @@ -156,14 +185,25 @@ void CtlParseJsonData(char *buf) void CtlParseFileTest(void) { //for PLC socket parameter file - char file_buf[CTL_FILE_SIZE] = {0}; FILE *fd = CtlFileInit(PLC_SOCK_FILE_NAME); if(fd == NULL) + { + ctl_error("ctl get file %s failed\n", PLC_SOCK_FILE_NAME); return; - memset(file_buf, 0, CTL_FILE_SIZE); - CtlFileRead(fd, CTL_FILE_SIZE, file_buf); + } + + char *file_buf = malloc(CTL_FILE_LEN); + + if(file_buf == NULL) + { + ctl_error("ctl malloc failed\n"); + return; + } + memset(file_buf, 0, CTL_FILE_LEN); + CtlFileRead(fd, CTL_FILE_LEN, file_buf); CtlFileClose(fd); CtlParseJsonData(file_buf); + free(file_buf); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), diff --git a/APP_Framework/Framework/control/shared/control_file.h b/APP_Framework/Framework/control/shared/control_file.h index dcafee179..9f8cda11e 100755 --- a/APP_Framework/Framework/control/shared/control_file.h +++ b/APP_Framework/Framework/control/shared/control_file.h @@ -21,31 +21,36 @@ #ifndef __CONTROL_FILE_H_ #define __CONTROL_FILE_H_ -#define CTL_FILE_SIZE 1000 -#define CTL_CMD_NUM 10 // max command number -#define CTL_CMD_LEN 100 -#define CTL_IP_LEN 32 +#define CTL_FILE_LEN 1000 // control file size +#define CTL_CMD_NUM 10 // support command number +#define CTL_CMD_LEN 100 // control command length +#define CTL_IP_LEN 32 // IP address length +#define CTL_FILE_NAME_LEN 100 // file name length #define PLC_SOCK_FILE_NAME "/plc/socket_param.json" -#define ctl_print KPrintf +#define ctl_print //KPrintf #define ctl_error KPrintf +// for running plc socket typedef struct CtlPlcSockParamStruct { char ip[CTL_IP_LEN]; int port; + int tcp; // 1: TCP 0: UDP int cmd_num; //command number int cmd_len[CTL_CMD_NUM]; // command length char cmd[CTL_CMD_NUM][CTL_CMD_LEN]; }CtlPlcSockParamType; extern CtlPlcSockParamType ctl_file_param; +extern int sd_mount_flag; FILE *CtlFileInit(char *file); void CtlFileClose(FILE *fd); void CtlFileRead(FILE *fd, int size, char *buf); void CtlFileWrite(FILE *fd, int size, char *buf); +int CtlFileReadWithFilename(char *file_name, int size, char *buf); #ifdef LIB_USING_CJSON void CtlParseJsonData(char *buf); diff --git a/Ubiquitous/XiZi/board/ok1052-c/board.c b/Ubiquitous/XiZi/board/ok1052-c/board.c index 82a9a10ea..284857639 100644 --- a/Ubiquitous/XiZi/board/ok1052-c/board.c +++ b/Ubiquitous/XiZi/board/ok1052-c/board.c @@ -84,6 +84,10 @@ int MountSDCard(void) #include #endif +#ifdef BSP_USING_I2C +#include +#endif + #ifdef BSP_USING_SPI #include #endif @@ -688,6 +692,10 @@ void InitBoardHardware() Imrt1052HwAdcInit(); #endif +#ifdef BSP_USING_I2C + Imrt1052HwI2cInit(); +#endif + #ifdef BSP_USING_SPI Imrt1052HwSpiInit(); #endif diff --git a/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c.c b/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c.c index 4b9eec117..1b2b827ae 100755 --- a/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c.c +++ b/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c.c @@ -143,7 +143,7 @@ static int BoardI2cDevBend(void) } /*BOARD I2C INIT*/ -int Stm32HwI2cInit(void) +int Imrt1052HwI2cInit(void) { static int init_flag = 0; x_err_t ret = EOK; diff --git a/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c_eeprom.c b/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c_eeprom.c index 26754cd25..792a19273 100755 --- a/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c_eeprom.c +++ b/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/i2c/connect_i2c_eeprom.c @@ -65,7 +65,6 @@ void I2cEEpromTestWrite(void) int I2cEEpromTest(void) { - Stm32HwI2cInit(); BOARD_InitI2C1Pins(); I2cHardwareInit(); I2cEEpromTestWrite(); diff --git a/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/include/connect_i2c.h b/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/include/connect_i2c.h index 619e801cc..a06675be0 100755 --- a/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/include/connect_i2c.h +++ b/Ubiquitous/XiZi/board/ok1052-c/third_party_driver/include/connect_i2c.h @@ -37,7 +37,7 @@ typedef struct Stm32I2c #define i2c_print KPrintf -int Stm32HwI2cInit(void); +int Imrt1052HwI2cInit(void); #ifdef __cplusplus } diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/api/api_lib.c b/Ubiquitous/XiZi/resources/ethernet/LwIP/api/api_lib.c index 88d24dc6e..5d7f8863c 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/api/api_lib.c +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/api/api_lib.c @@ -245,7 +245,6 @@ netconn_delete(struct netconn *conn) err = ERR_OK; } else #endif /* LWIP_NETCONN_FULLDUPLEX */ -//tst by wly { err = netconn_prepare_delete(conn); } diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/api/sockets.c b/Ubiquitous/XiZi/resources/ethernet/LwIP/api/sockets.c index 1e8031672..2660f73e5 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/api/sockets.c +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/api/sockets.c @@ -497,18 +497,6 @@ get_socket(int fd) return sock; } -void pr_socket_buf(void) -{ - int i; - lw_debug("socket:\n"); - for (i = 0; i < NUM_SOCKETS; ++i) { -// if (!sockets[i].conn) - lw_debug("%d: conn %p rcv %d send %d wait %d\n", i, sockets[i].conn, sockets[i].rcvevent, sockets[i].sendevent, - sockets[i].select_waiting); - } -} - - /** * Allocate a new socket for a given netconn. * @@ -600,7 +588,6 @@ free_socket_free_elements(int is_tcp, struct netconn *conn, union lwip_sock_last } if (conn != NULL) { /* netconn_prepare_delete() has already been called, here we only free the conn */ - lw_debug("lw: [%s] tcp %d socket %d accept %d recv %d\n", __func__, is_tcp, conn->socket, conn->acceptmbox, conn->recvmbox); netconn_delete(conn); } } @@ -673,8 +660,6 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) newsock = alloc_socket(newconn, 1); if (newsock == -1) { - lw_debug("lw: [%s] recv %d\n", __func__, newconn->recvmbox); - pr_socket_buf(); netconn_delete(newconn); sock_set_errno(sock, ENFILE); done_socket(sock); @@ -711,8 +696,6 @@ lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen) err = netconn_peer(newconn, &naddr, &port); if (err != ERR_OK) { LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_peer failed, err=%d\n", s, err)); - lw_debug("lw: [%s] recv %x\n", __func__, newconn->recvmbox); - netconn_delete(newconn); free_socket(nsock, 1); sock_set_errno(sock, err_to_errno(err)); @@ -1756,8 +1739,6 @@ lwip_socket(int domain, int type, int protocol) i = alloc_socket(conn, 0); if (i == -1) { - lw_debug("lw: [%s] recv %d delete no socket\n", __func__, conn->recvmbox); - pr_socket_buf(); netconn_delete(conn); set_errno(ENFILE); return -1; @@ -1766,7 +1747,6 @@ lwip_socket(int domain, int type, int protocol) done_socket(&sockets[i - LWIP_SOCKET_OFFSET]); LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i)); set_errno(0); - lw_debug("lw: [%s] new socket %d %p\n", __func__, i, conn); return i; } diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/lwipopts.h b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/lwipopts.h index a8f4b5be8..cdff39538 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/lwipopts.h +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/arch/lwipopts.h @@ -250,10 +250,9 @@ typedef unsigned int nfds_t; #define MEMP_MEM_MALLOC 1 #define lw_print //KPrintf -#define lw_trace() //KPrintf("lw: [%s][%d] passed!\n", __func__, __LINE__) -#define lw_error() //KPrintf("lw: [%s][%d] failed!\n", __func__, __LINE__) -#define lw_debug KPrintf +#define lw_error KPrintf #define lw_notice KPrintf + #endif /* __LWIPOPTS_H__ */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/Ubiquitous/XiZi/resources/ethernet/LwIP/core/tcp_out.c b/Ubiquitous/XiZi/resources/ethernet/LwIP/core/tcp_out.c index 3fb89e245..724df1097 100644 --- a/Ubiquitous/XiZi/resources/ethernet/LwIP/core/tcp_out.c +++ b/Ubiquitous/XiZi/resources/ethernet/LwIP/core/tcp_out.c @@ -355,7 +355,7 @@ tcp_write_checks(struct tcp_pcb *pcb, u16_t len) * it can send them more efficiently by combining them together). * To prompt the system to send data now, call tcp_output() after * calling tcp_write(). - * + * * This function enqueues the data pointed to by the argument dataptr. The length of * the data is passed as the len parameter. The apiflags can be one or more of: * - TCP_WRITE_FLAG_COPY: indicates whether the new memory should be allocated @@ -1386,17 +1386,13 @@ tcp_output(struct tcp_pcb *pcb) /* In the case of fast retransmit, the packet should not go to the tail * of the unacked queue, but rather somewhere before it. We need to check for * this case. -STJ Jul 27, 2004 */ - lw_debug("check %s seg %p useg %p\n", __func__, seg, useg); if (TCP_SEQ_LT(lwip_ntohl(seg->tcphdr->seqno), lwip_ntohl(useg->tcphdr->seqno))) { /* add segment to before tail of unacked list, keeping the list sorted */ struct tcp_seg **cur_seg = &(pcb->unacked); - lw_debug("check %s ", __func__); while (*cur_seg && TCP_SEQ_LT(lwip_ntohl((*cur_seg)->tcphdr->seqno), lwip_ntohl(seg->tcphdr->seqno))) { - lw_debug("%p -> ", *cur_seg); cur_seg = &((*cur_seg)->next ); } - lw_debug("\n"); seg->next = (*cur_seg); (*cur_seg) = seg; } else { diff --git a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_config_demo.c b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_config_demo.c index a9e662c09..7e94dc272 100755 --- a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_config_demo.c +++ b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_config_demo.c @@ -18,40 +18,18 @@ * @date 2021.12.15 */ -#include "lwip/opt.h" - -#if LWIP_IPV4 && LWIP_RAW - -#include "ping.h" - -#include "lwip/timeouts.h" -#include "lwip/init.h" -#include "netif/ethernet.h" - #include "board.h" -#include "pin_mux.h" -#include "clock_config.h" - -#include -#include -#include "connect_ethernet.h" +#include "sys_arch.h" /******************************************************************************/ -static void *LwipSetIPTask(void *param) +static void LwipSetIPTask(void *param) { lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr); } void LwipSetIPTest(int argc, char *argv[]) { - int result = 0; - pthread_t th_id; - pthread_attr_t attr; - - attr.schedparam.sched_priority = LWIP_DEMO_TASK_PRIO; - attr.stacksize = LWIP_TASK_STACK_SIZE; - if(argc >= 4) { lw_print("lw: [%s] ip %s mask %s gw %s\n", __func__, argv[1], argv[2], argv[3]); @@ -65,12 +43,7 @@ void LwipSetIPTest(int argc, char *argv[]) sscanf(argv[1], "%d.%d.%d.%d", &lwip_ipaddr[0], &lwip_ipaddr[1], &lwip_ipaddr[2], &lwip_ipaddr[3]); } - result = pthread_create(&th_id, &attr, LwipSetIPTask, NULL); - if (0 == result) { - lw_print("lw: [%s] thread %d successfully!\n", __func__, th_id); - } else { - lw_print("lw: [%s] failed! error code is %d\n", __func__, result); - } + sys_thread_new("SET ip address", LwipSetIPTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), @@ -98,4 +71,3 @@ void LwipShowIPTask(int argc, char *argv[]) SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), showip, LwipShowIPTask, GetIp [IP] [Netmask] [Gateway]); -#endif diff --git a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_demo.h b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_demo.h index 99e273aa1..68a78b4d7 100755 --- a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_demo.h +++ b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_demo.h @@ -1,7 +1,37 @@ +/* +* Copyright (c) 2022 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file lwip_demo.h +* @brief lwip demo relative struct and definition +* @version 1.0 +* @author AIIT XUOS Lab +* @date 2022-03-21 +*/ + #ifndef __LWIP_DEMO_H__ #define __LWIP_DEMO_H__ +typedef struct LwipTcpSocketStruct +{ + char ip[6]; + uint16_t port; + char *buf; +}LwipTcpSocketParamType; + +#define LWIP_TEST_MSG_SIZE 128 + #define LWIP_TEST_STACK_SIZE 4096 #define LWIP_TEST_TASK_PRIO 20 #endif /* __LWIP_DEMO_H__ */ + diff --git a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_tcp_demo.c b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_tcp_demo.c index 387899abc..f57f80b4f 100755 --- a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_tcp_demo.c +++ b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_tcp_demo.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2020 AIIT XUOS Lab +* Copyright (c) 2022 AIIT XUOS Lab * XiUOS is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -11,45 +11,32 @@ */ /** -* @file tcp_echo_socket_demo.c -* @brief One UDP demo based on LwIP +* @file lwip_tcp_demo.c +* @brief TCP demo based on LwIP * @version 1.0 * @author AIIT XUOS Lab -* @date 2021-05-29 +* @date 2022-03-21 */ -#include -#include + #include "board.h" +#include "lwip_demo.h" #include "sys_arch.h" -#include -#include "lwip/sys.h" +#include "lwip/sockets.h" #include "tcpecho_raw.h" -#define MSG_SIZE 128 - -typedef struct LwipTcpSocketStruct -{ - char ip[6]; - uint16_t port; - char *buf; // test buffer -}LwipTcpSocketParamType; - -// this is for test in shell, in fact, shell restrict the length of input string, which is less then 128 -char tcp_send_msg[MSG_SIZE] = {0}; -char tcp_target[] = {192, 168, 250, 252}; -uint16_t tcp_port = LWIP_TARGET_PORT; +char tcp_demo_msg[LWIP_TEST_MSG_SIZE] = {0}; +char tcp_demo_ip[] = {192, 168, 250, 252}; +u16_t tcp_demo_port = LWIP_TARGET_PORT; /******************************************************************************/ static void LwipTcpSendTask(void *arg) { - lw_notice("LwipTcpSendTask start.\n"); - int fd = -1; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { - lw_notice("Socket error\n"); + lw_error("Socket error\n"); return; } @@ -63,7 +50,7 @@ static void LwipTcpSendTask(void *arg) if (connect(fd, (struct sockaddr *)&tcp_sock, sizeof(struct sockaddr))) { - lw_notice("Unable to connect\n"); + lw_error("Unable to connect\n"); closesocket(fd); return; } @@ -71,9 +58,9 @@ static void LwipTcpSendTask(void *arg) lw_notice("tcp connect success, start to send.\n"); lw_notice("\n\nTarget Port:%d\n\n", tcp_sock.sin_port); - sendto(fd, tcp_send_msg, strlen(tcp_send_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr)); + sendto(fd, tcp_demo_msg, strlen(tcp_demo_msg), 0, (struct sockaddr*)&tcp_sock, sizeof(struct sockaddr)); - lw_notice("Send tcp msg: %s ", tcp_send_msg); + lw_notice("Send tcp msg: %s ", tcp_demo_msg); closesocket(fd); return; @@ -82,31 +69,31 @@ static void LwipTcpSendTask(void *arg) void LwipTcpSendTest(int argc, char *argv[]) { LwipTcpSocketParamType param; - memset(tcp_send_msg, 0, MSG_SIZE); + memset(tcp_demo_msg, 0, LWIP_TEST_MSG_SIZE); if(argc >= 2) { - strncpy(tcp_send_msg, argv[1], strlen(argv[1])); + strncpy(tcp_demo_msg, argv[1], strlen(argv[1])); } else { - strncpy(tcp_send_msg, "hello world", strlen("hello world")); + strncpy(tcp_demo_msg, "hello world", strlen("hello world")); } - strcat(tcp_send_msg, "\r\n"); + strcat(tcp_demo_msg, "\r\n"); if(argc >= 3) { - if(sscanf(argv[2], "%d.%d.%d.%d:%d", &tcp_target[0], &tcp_target[1], &tcp_target[2], &tcp_target[3], &tcp_port) == EOK) + if(sscanf(argv[2], "%d.%d.%d.%d:%d", &tcp_demo_ip[0], &tcp_demo_ip[1], &tcp_demo_ip[2], &tcp_demo_ip[3], &tcp_demo_port) == EOK) { - sscanf(argv[2], "%d.%d.%d.%d", &tcp_target[0], &tcp_target[1], &tcp_target[2], &tcp_target[3]); + sscanf(argv[2], "%d.%d.%d.%d", &tcp_demo_ip[0], &tcp_demo_ip[1], &tcp_demo_ip[2], &tcp_demo_ip[3]); } } - lw_notice("get ipaddr %d.%d.%d.%d:%d\n", tcp_target[0], tcp_target[1], tcp_target[2], tcp_target[3], tcp_port); - lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_target); + lw_notice("get ipaddr %d.%d.%d.%d:%d\n", tcp_demo_ip[0], tcp_demo_ip[1], tcp_demo_ip[2], tcp_demo_ip[3], tcp_demo_port); + lwip_config_tcp(lwip_ipaddr, lwip_netmask, tcp_demo_ip); - memcpy(param.ip, tcp_target, 4); - param.port = tcp_port; - param.buf = malloc(MSG_SIZE); - memcpy(param.buf, tcp_send_msg, MSG_SIZE); + memcpy(param.ip, tcp_demo_ip, 4); + param.port = tcp_demo_port; + param.buf = malloc(LWIP_TEST_MSG_SIZE); + memcpy(param.buf, tcp_demo_msg, LWIP_TEST_MSG_SIZE); sys_thread_new("tcp send", LwipTcpSendTask, ¶m, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } diff --git a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_udp_demo.c b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_udp_demo.c index 97d06b119..bb3f48be1 100755 --- a/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_udp_demo.c +++ b/Ubiquitous/XiZi/resources/ethernet/cmd_lwip/lwip_udp_demo.c @@ -1,5 +1,5 @@ /* -* Copyright (c) 2021 AIIT XUOS Lab +* Copyright (c) 2022 AIIT XUOS Lab * XiUOS is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: @@ -15,25 +15,23 @@ * @brief One UDP demo based on LwIP * @version 1.0 * @author AIIT XUOS Lab -* @date 2021-05-29 +* @date 2022-03-21 */ -#include -#include #include "board.h" #include "sys_arch.h" #include "lwip/udp.h" -#include -#include "lwip/sys.h" +#include "lwip/sockets.h" + -#define UDP_TASK_STACK_SIZE 4096 -#define UDP_TASK_PRIO 15 #define PBUF_SIZE 27 static struct udp_pcb *udpecho_raw_pcb; + char udp_demo_ip[] = {192, 168, 250, 252}; -uint16_t udp_demo_port = LWIP_TARGET_PORT; +u16_t udp_demo_port = LWIP_TARGET_PORT; + char hello_str[] = {"hello world\r\n"}; -char udp_send_msg[] = "\n\nThis one is UDP pkg. Congratulations on you.\n\n"; +char udp_demo_msg[] = "\nThis one is UDP package!!!\n"; /******************************************************************************/ @@ -47,7 +45,7 @@ static void LwipUDPSendTask(void *arg) socket_fd = socket(AF_INET, SOCK_DGRAM, 0); if (socket_fd < 0) { - lw_print("Socket error\n"); + lw_error("Socket error\n"); return; } @@ -59,45 +57,43 @@ static void LwipUDPSendTask(void *arg) if (connect(socket_fd, (struct sockaddr *)&udp_sock, sizeof(struct sockaddr))) { - lw_print("Unable to connect\n"); + lw_error("Unable to connect\n"); closesocket(socket_fd); return; } - lw_print("UDP connect success, start to send.\n"); - lw_print("\n\nTarget Port:%d\n\n", udp_sock.sin_port); + lw_notice("UDP connect success, start to send.\n"); + lw_notice("\n\nTarget Port:%d\n\n", udp_sock.sin_port); - sendto(socket_fd, udp_send_msg, strlen(udp_send_msg), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr)); - lw_notice("Send UDP msg: %s ", udp_send_msg); + sendto(socket_fd, udp_demo_msg, strlen(udp_demo_msg), 0, (struct sockaddr*)&udp_sock, sizeof(struct sockaddr)); + lw_notice("Send UDP msg: %s ", udp_demo_msg); closesocket(socket_fd); return; } void *LwipUdpSendTest(int argc, char *argv[]) { - int result = 0; - sys_thread_t th_id; - - memset(udp_send_msg, 0, sizeof(udp_send_msg)); + memset(udp_demo_msg, 0, sizeof(udp_demo_msg)); if(argc == 1) { lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3]); - strncpy(udp_send_msg, hello_str, strlen(hello_str)); + strncpy(udp_demo_msg, hello_str, strlen(hello_str)); } else { - strncpy(udp_send_msg, argv[1], strlen(argv[1])); - strncat(udp_send_msg, "\r\n", 2); + strncpy(udp_demo_msg, argv[1], strlen(argv[1])); + strncat(udp_demo_msg, "\r\n", 2); if(argc == 3) { sscanf(argv[2], "%d.%d.%d.%d", &udp_demo_ip[0], &udp_demo_ip[1], &udp_demo_ip[2], &udp_demo_ip[3]); } } + lw_print("lw: [%s] gw %d.%d.%d.%d\n", __func__, udp_demo_ip[0], udp_demo_ip[1], udp_demo_ip[2], udp_demo_ip[3]); - lwip_config_net(lwip_ipaddr, lwip_netmask, lwip_gwaddr); - sys_thread_new("udp socket send", LwipUDPSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); + lwip_config_net(lwip_ipaddr, lwip_netmask, udp_demo_ip); + sys_thread_new("udp send", LwipUDPSendTask, NULL, LWIP_TASK_STACK_SIZE, LWIP_DEMO_TASK_PRIO); } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(3), @@ -116,6 +112,7 @@ static void LwipUdpRecvTask(void *arg, struct udp_pcb *upcb, struct pbuf *p, { return; } + udp_len = p->tot_len; lw_notice("Receive data :%dB\r\n", udp_len); @@ -158,5 +155,5 @@ void LwipUdpRecvTest(void) } SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(0), - UDPRecv, LwipUdpRecvTest, UDP server echo); + UDPRecv, LwipUdpRecvTest, UDP Receive echo); diff --git a/Ubiquitous/XiZi/resources/include/flash_spi.h b/Ubiquitous/XiZi/resources/include/flash_spi.h index 1f6aa4913..f41d40d25 100644 --- a/Ubiquitous/XiZi/resources/include/flash_spi.h +++ b/Ubiquitous/XiZi/resources/include/flash_spi.h @@ -21,9 +21,7 @@ #ifndef FLASH_SPI_H #define FLASH_SPI_H -#ifdef RESOURCES_SPI_SFUD #include -#endif #ifdef __cplusplus extern "C" { diff --git a/Ubiquitous/XiZi/tool/shell/letter-shell/shell_port.c b/Ubiquitous/XiZi/tool/shell/letter-shell/shell_port.c index 5d8d21a65..661cfb604 100644 --- a/Ubiquitous/XiZi/tool/shell/letter-shell/shell_port.c +++ b/Ubiquitous/XiZi/tool/shell/letter-shell/shell_port.c @@ -1,12 +1,12 @@ /** * @file shell_port.c * @author Letter (NevermindZZT@gmail.com) - * @brief + * @brief * @version 0.1 * @date 2019-02-22 - * + * * @copyright (c) 2019 Letter - * + * */ #include "xizi.h" @@ -26,11 +26,11 @@ char *shellBuffer; ShellFs shellFs; char *shellPathBuffer; -HardwareDevType console; +HardwareDevType console; /** - * @brief Shell write - * + * @brief Shell write + * * @param data write data */ void userShellWrite(char data) @@ -40,7 +40,7 @@ void userShellWrite(char data) /** * @brief shell read - * + * * @param data read data * @return char read status */ @@ -62,7 +62,7 @@ signed char userShellRead(char *data) #ifdef SHELL_ENABLE_FILESYSTEM /** * @brief list file - * + * * @param path path * @param buffer result buffer * @param maxLen the maximum buffer size @@ -86,14 +86,14 @@ size_t userShellListDir(char *path, char *buffer, size_t maxLen) #endif /** - * @brief Initialize the shell - * + * @brief Initialize the shell + * */ int userShellInit(void) { shellBuffer = x_malloc(512*sizeof(char)); - - + + #ifdef SHELL_ENABLE_FILESYSTEM shellPathBuffer = x_malloc(512*sizeof(char)); shellPathBuffer[0] = '/'; @@ -102,7 +102,7 @@ int userShellInit(void) shellFs.listdir = userShellListDir; shellFsInit(&shellFs, shellPathBuffer, 512); shellSetPath(&shell, shellPathBuffer); -#endif +#endif shell.write = userShellWrite; shell.read = userShellRead; @@ -114,13 +114,13 @@ int userShellInit(void) serial_dev_param->serial_set_mode = 0; serial_dev_param->serial_stream_mode = SIGN_OPER_STREAM; BusDevOpen(console); - - shellInit(&shell, shellBuffer, 4096); + + shellInit(&shell, shellBuffer, 512); int32 tid; tid = KTaskCreate("letter-shell", shellTask, &shell, SHELL_TASK_STACK_SIZE, SHELL_TASK_PRIORITY); - + StartupKTask(tid); return 0; }