diff --git a/APP_Framework/Applications/connection_app/mqtt_demo/Makefile b/APP_Framework/Applications/connection_app/mqtt_demo/Makefile index e5d888bb3..6f459e9f4 100644 --- a/APP_Framework/Applications/connection_app/mqtt_demo/Makefile +++ b/APP_Framework/Applications/connection_app/mqtt_demo/Makefile @@ -1,5 +1,6 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y) -SRC_FILES := lwip_mqtt_demo.c +SRC_FILES += lwip_mqtt_demo.c +SRC_FILES += online_mqtt.c include $(KERNEL_ROOT)/compiler.mk endif diff --git a/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.c b/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.c index 3592016e0..7209054bb 100644 --- a/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.c +++ b/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.c @@ -1,6 +1,6 @@ #include #include "lwip_mqtt_demo.h" - +#include #ifdef ADD_XIZI_FETURES #include #include @@ -27,9 +27,9 @@ #define LWIP_MQTT_DEMO_TASK_STACK_SIZE 4096 #define LWIP_MQTT_DEMO_TASK_PRIO 20 -static char mqtt_demo_ipaddr[] = {192, 168, 131, 77}; +static char mqtt_demo_ipaddr[] = {192, 168, 130, 77}; static char mqtt_demo_netmask[] = {255, 255, 254, 0}; -static char mqtt_demo_gwaddr[] = {192, 168, 131, 1}; +static char mqtt_demo_gwaddr[] = {192, 168, 130, 1}; static pthread_t mqtt_client_task; static pthread_t mqtt_server_task; @@ -76,7 +76,7 @@ uint8_t MQTT_Connect(void) len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data); //发送消息 transport_sendPacketBuffer(buf, len); - + /* 等待连接响应 */ if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) { @@ -149,7 +149,7 @@ int32_t MQTTSubscribe(int32_t sock,char *topic,enum QoS pos) MQTTString topicString = MQTTString_initializer; int32_t len; int32_t req_qos,qosbk; - + fd_set readfd; struct timeval tv; tv.tv_sec = 2; @@ -342,6 +342,7 @@ void mqtt_pktype_ctl(uint8_t packtype,uint8_t *buf,uint32_t buflen) static void *MqttSocketRecvTask(void *arg) { +MQTT_START: lw_print("Recv begin**********\n"); int fd = -1, clientfd; int recv_len; @@ -371,7 +372,7 @@ static void *MqttSocketRecvTask(void *arg) return NULL; } - lw_print("MQTT connect %s:%d success, begin to verify hostname and password.\n", mqtt_ip_str, mqtt_socket_port); + lw_print("MQTT connect %s:%d success, begin to verify username and password.\n", mqtt_ip_str, mqtt_socket_port); if(MQTT_Connect() != Connect_OK) { @@ -379,7 +380,8 @@ static void *MqttSocketRecvTask(void *arg) shutdown(fd, SHUT_WR); recv(fd, NULL, (size_t)0, 0); close(fd); - return NULL; + PrivTaskDelay(1000); + goto MQTT_START; } lw_print("MQTT subscribe begin.\n"); diff --git a/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.h b/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.h index f1a68e95a..a9870fb66 100644 --- a/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.h +++ b/APP_Framework/Applications/connection_app/mqtt_demo/lwip_mqtt_demo.h @@ -1,8 +1,8 @@ #include -#define MSG_MAX_LEN 500 +#define MSG_MAX_LEN 1024 #define MSG_TOPIC_LEN 50 -#define KEEPLIVE_TIME 50 +#define KEEPLIVE_TIME 650 #define MQTT_VERSION 4 #ifdef LWIP_DNS @@ -15,11 +15,11 @@ //#define HOST_IP "129.204.201.235" #define HOST_PORT 1883 //由于是TCP连接,端口必须是1883 -#define CLIENT_ID "mqttx_24d1dccf" //随机的id -#define USER_NAME "test" //用户名 -#define PASSWORD "hc123456" //秘钥 +#define CLIENT_ID "iw3rn3pa11K.test|securemode=2,signmethod=hmacsha256,timestamp=1689296035604|" //随机的id +#define USER_NAME "test&iw3rn3pa11K" //用户名 +#define PASSWORD "7b948d22fe46f0f63d1a403376d26e7cb298abc227d29e44311d7040307a71f8" //秘钥 -#define TOPIC "temp" //订阅的主题 +#define TOPIC "/iw3rn3pa11K/test/user/Test" //订阅的主题 #define TEST_MESSAGE "test_message" //发送测试消息 diff --git a/APP_Framework/Applications/connection_app/mqtt_demo/online_mqtt.c b/APP_Framework/Applications/connection_app/mqtt_demo/online_mqtt.c new file mode 100644 index 000000000..64c7f7d1e --- /dev/null +++ b/APP_Framework/Applications/connection_app/mqtt_demo/online_mqtt.c @@ -0,0 +1,328 @@ +#include +#include +#include + +/* lwip includes. */ +#include "lwip/apps/mqtt.h" +#include "lwip/ip4_addr.h" + + + + +static mqtt_client_t *s__mqtt_client_instance = NULL; //mqtt连接句柄,这里一定要设置全局变量,防止 lwip 底层重复申请空间 + +//MQTT 数据结构体 +struct mqtt_recv_buffer +{ + char recv_buffer[1024]; //储存接收的buffer + uint16_t recv_len; //记录已接收多少个字节的数据,MQTT的数据分包来的 + uint16_t recv_total; //MQTT接收数据的回调函数会有个总的大小 +}; + +//结构体初始化 +struct mqtt_recv_buffer s__mqtt_recv_buffer_g = { + .recv_len = 0, + .recv_total = 0, +}; + + + + + +/*! +* @brief MQTT 处理失败调用的函数 +* +* @param [in1] : MQTT 连接句柄 +* @param [in2] : MQTT 连接参数指针 +* +* @retval: None +*/ +void mqtt_error_process_callback(mqtt_client_t * client, void *arg) +{ + +} + + +/*! +* @brief mqtt 接收数据处理函数接口,需要在应用层进行处理 +* 执行条件:mqtt连接成功 +* +* @param [in1] : 用户提供的回调参数指针 +* @param [in2] : 接收的数据指针 +* @param [in3] : 接收数据长度 +* @retval: 处理的结果 +*/ +int mqtt_rec_data_process(void* arg, char *rec_buf, uint64_t buf_len) +{ + lw_print("recv_buffer = %s\n", rec_buf); + return 0; +} + +/*! +* @brief MQTT 订阅的回调函数 +* 执行条件:MQTT 连接成功 +* +* @param [in] : 用户提供的回调参数指针 +* @param [in] : MQTT 订阅结果 +* @retval: None +*/ +static void bsp_mqtt_request_cb(void *arg, err_t err) +{ + if ( arg == NULL ) + { + lw_print("bsp_mqtt_request_cb: input error@@\n"); + return; + } + + mqtt_client_t *client = (mqtt_client_t *)arg; + + if ( err != ERR_OK ) + { + lw_print("bsp_mqtt_request_cb: FAIL sub, sub again, err = %s\n", lwip_strerr(err)); + + //错误处理 + mqtt_error_process_callback(client, arg); + } + else + { + lw_print("bsp_mqtt_request_cb: sub SUCCESS!\n"); + } +} + + +/*! +* @brief mqtt 订阅 +* 执行条件:连接成功 +* +* @param [in1] : mqtt 连接句柄 +* @param [in2] : mqtt 发送 topic 指针 +* @param [in5] : qos +* @retval: 订阅状态 +*/ +static err_t bsp_mqtt_subscribe(mqtt_client_t* mqtt_client, char * sub_topic, uint8_t qos) +{ + lw_print("bsp_mqtt_subscribe: Enter\n"); + + if( ( mqtt_client == NULL) || ( sub_topic == NULL) || ( qos > 2 ) ) + { + lw_print("bsp_mqtt_subscribe: input error@@\n"); + return ERR_VAL; + } + + if ( mqtt_client_is_connected(mqtt_client) != true ) + { + lw_print("bsp_mqtt_subscribe: mqtt is not connected, return ERR_CLSD.\n"); + return ERR_CLSD; + } + + err_t err; + err = mqtt_subscribe(mqtt_client, sub_topic, qos, bsp_mqtt_request_cb, (void *)mqtt_client); // subscribe and call back. + + if (err != ERR_OK) + { + lw_print("bsp_mqtt_subscribe: mqtt_subscribe Fail, return:%s \n", lwip_strerr(err)); + } + else + { + lw_print("bsp_mqtt_subscribe: mqtt_subscribe SUCCESS, reason: %s\n", lwip_strerr(err)); + } + + return err; +} + +/*! +* @brief MQTT 连接成功的处理函数,需要的话在应用层定义 +* +* @param [in1] : MQTT 连接句柄 +* @param [in2] : MQTT 连接参数指针 +* +* @retval: None +*/ + +void mqtt_conn_suc_proc(mqtt_client_t *client, void *arg) +{ + char test_sub_topic[] = "/public/TEST/AidenHinGwenWong_sub"; + bsp_mqtt_subscribe(client,test_sub_topic,0); +} + + +/*! +* @brief MQTT 接收到数据的回调函数 +* 执行条件:MQTT 连接成功 +* +* @param [in1] : 用户提供的回调参数指针 +* @param [in2] : MQTT 收到的分包数据指针 +* @param [in3] : MQTT 分包数据长度 +* @param [in4] : MQTT 数据包的标志位 +* @retval: None +*/ +static void bsp_mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags) +{ + if( (data == NULL) || (len == 0) ) + { + lw_print("mqtt_client_incoming_data_cb: condition error @entry\n"); + return; + } + + if(s__mqtt_recv_buffer_g.recv_len + len < sizeof(s__mqtt_recv_buffer_g.recv_buffer)) + { + // + snprintf(&s__mqtt_recv_buffer_g.recv_buffer[s__mqtt_recv_buffer_g.recv_len], len, "%s", data); + s__mqtt_recv_buffer_g.recv_len += len; + } + + if ( (flags & MQTT_DATA_FLAG_LAST) == MQTT_DATA_FLAG_LAST ) + { + //处理数据 + mqtt_rec_data_process(arg , s__mqtt_recv_buffer_g.recv_buffer, s__mqtt_recv_buffer_g.recv_len); + + //已接收字节计数归0 + s__mqtt_recv_buffer_g.recv_len = 0; + + //清空接收buffer + memset(s__mqtt_recv_buffer_g.recv_buffer, 0, sizeof(s__mqtt_recv_buffer_g.recv_buffer)); + } + + + lw_print("mqtt_client_incoming_data_cb:reveiving incomming data.\n"); +} + +/*! +* @brief MQTT 接收到数据的回调函数 +* 执行条件:MQTT 连接成功 +* +* @param [in] : 用户提供的回调参数指针 +* @param [in] : MQTT 收到数据的topic +* @param [in] : MQTT 收到数据的总长度 +* @retval: None +*/ +static void bsp_mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len) +{ + if( (topic == NULL) || (tot_len == 0) ) + { + lw_print("bsp_mqtt_incoming_publish_cb: condition error @entry\n"); + return; + } + + lw_print("bsp_mqtt_incoming_publish_cb: topic = %s.\n",topic); + lw_print("bsp_mqtt_incoming_publish_cb: tot_len = %d.\n",tot_len); + s__mqtt_recv_buffer_g.recv_total = tot_len; //需要接收的总字节 + s__mqtt_recv_buffer_g.recv_len = 0; //已接收字节计数归0 + + //清空接收buffer + memset(s__mqtt_recv_buffer_g.recv_buffer, 0, sizeof(s__mqtt_recv_buffer_g.recv_buffer)); +} + + +/*! +* @brief MQTT 连接状态的回调函数 +* +* @param [in] : MQTT 连接句柄 +* @param [in] : 用户提供的回调参数指针 +* @param [in] : MQTT 连接状态 +* @retval: None +*/ +static void bsp_mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status) +{ + if( client == NULL ) + { + lw_print("bsp_mqtt_connection_cb: condition error @entry\n"); + return; + } + lw_print("status:%d",status); + if ( status == MQTT_CONNECT_ACCEPTED ) //Successfully connected + { + lw_print("bsp_mqtt_connection_cb: Successfully connected\n"); + + // 注册接收数据的回调函数 + mqtt_set_inpub_callback(client, bsp_mqtt_incoming_publish_cb, bsp_mqtt_incoming_data_cb, arg); + + + + //成功处理函数 + mqtt_conn_suc_proc(client, arg); + } + else + { + lw_print("bsp_mqtt_connection_cb: Fail connected, status = %s\n", lwip_strerr(status) ); + //错误处理 + mqtt_error_process_callback(client, arg); + } +} + +static err_t bsp_mqtt_connect(void) +{ + lw_print("bsp_mqtt_connect: Enter!\n"); + err_t ret; + + struct mqtt_connect_client_info_t mqtt_connect_info = { + "Hc_MQTT_Test", /* 这里需要修改,以免在同一个服务器两个相同ID会发生冲突 */ + NULL, /* MQTT 服务器用户名 */ + NULL, /* MQTT 服务器密码 */ + 60, /* 与 MQTT 服务器保持连接时间,时间超过未发送数据会断开 */ + "/public/TEST/AidenHinGwenWong_pub",/* MQTT遗嘱的消息发送topic */ + "Offline_pls_check", /* MQTT遗嘱的消息,断开服务器的时候会发送 */ + 0, /* MQTT遗嘱的消息 Qos */ + 0 /* MQTT遗嘱的消息 Retain */ + }; + + ip_addr_t server_ip; + ip4_addr_set_u32(&server_ip, ipaddr_addr("120.76.100.197")); //MQTT服务器IP + + uint16_t server_port = 18830; //注意这里是 MQTT 的 TCP 连接方式的端口号!!!! + + if (s__mqtt_client_instance == NULL) + { + // 句柄==NULL 才申请空间,否则无需重复申请 + s__mqtt_client_instance = mqtt_client_new(); + } + + if (s__mqtt_client_instance == NULL) + { + //防止申请失败 + lw_print("bsp_mqtt_connect: s__mqtt_client_instance malloc fail @@!!!\n"); + return ERR_MEM; + } + + //进行连接,注意:如果需要带入 arg ,arg必须是全局变量,局部变量指针会被回收,大坑!!!!! + ret = mqtt_client_connect(s__mqtt_client_instance, &server_ip, server_port, bsp_mqtt_connection_cb, NULL, &mqtt_connect_info); + + /****************** + 小提示:连接错误不需要做任何操作,mqtt_client_connect 中注册的回调函数里面做判断并进行对应的操作 + *****************/ + + lw_print("bsp_mqtt_connect: connect to mqtt %s\n", lwip_strerr(ret)); + + return ret; +} + + +/* =========================================== + 初始化接口函数 +============================================== */ + +/*! +* @brief 封装 MQTT 初始化接口 +* 执行条件:无 +* +* @retval: 无 +*/ +void MQTTOnline(void) +{ + lw_print("Mqtt init..."); + + // 连接服务器 + bsp_mqtt_connect(); + + // 发送消息到服务器 + // char message_test[] = "Hello mqtt server"; + // for(int i = 0; i < 10; i++) + // { + // bsp_mqtt_publish(s__mqtt_client_instance,"/public/TEST/AidenHinGwenWong_pub",message_test,sizeof(message_test),1,0); + // PrivTaskDelay(1000); + // } + +} + + +PRIV_SHELL_CMD_FUNCTION(MQTTOnline, a tcp send sample, PRIV_SHELL_CMD_MAIN_ATTR); diff --git a/APP_Framework/Applications/connection_app/mqtt_demo/online_mqtt.h b/APP_Framework/Applications/connection_app/mqtt_demo/online_mqtt.h new file mode 100644 index 000000000..abac5506f --- /dev/null +++ b/APP_Framework/Applications/connection_app/mqtt_demo/online_mqtt.h @@ -0,0 +1,29 @@ + +#ifndef __BSP_MQTT_H +#define __BSP_MQTT_H + +/*----------------------------------------------------------- + * Includes files + *----------------------------------------------------------*/ + +/*----------------------------------------------------------- + * Exported constants + *----------------------------------------------------------*/ + + +/*----------------------------------------------------------- + * Exported macro + *----------------------------------------------------------*/ + +/*----------------------------------------------------------- + * Exported function + *----------------------------------------------------------*/ +/*! +* @brief 封装 MQTT 初始化接口 +* 执行条件:无 +* +* @retval: 无 +*/ +void bsp_mqtt_init(void); + +#endif /* __BSP_WOLFSSL_H */ diff --git a/APP_Framework/lib/cJSON/cJSON_Process.c b/APP_Framework/lib/cJSON/cJSON_Process.c index 6e2aa9c1e..12490a10b 100644 --- a/APP_Framework/lib/cJSON/cJSON_Process.c +++ b/APP_Framework/lib/cJSON/cJSON_Process.c @@ -60,7 +60,7 @@ uint8_t cJSON_Update(const cJSON * const object,const char * const string,void * void Proscess(void* data) { - lw_print("��ʼ����JSON����"); + cJSON *root,*json_name,*json_temp_num,*json_hum_num; root = cJSON_Parse((char*)data); //解析成json形式 diff --git a/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/Makefile b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/Makefile index 1ea20caac..b5bc3166b 100644 --- a/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/Makefile @@ -1,3 +1,3 @@ -SRC_FILES := socket.c connect_w5500.c w5500.c wizchip_conf.c spi_interface.c wiz_ping.c +SRC_FILES := socket.c connect_w5500.c w5500.c wizchip_conf.c spi_interface.c wiz_ping.c connect_w5500_test.c wiz_iperf.c include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500.c b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500.c index d06dfaa2d..0b245b11d 100644 --- a/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500.c +++ b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500.c @@ -23,47 +23,53 @@ extern void spi_select_cs(void); extern void spi_deselete_cs(void); // global configurations for w5500 tcp connection -const uint32_t g_wiznet_buf_size = 2048; +uint32_t get_gbuf_size() { + static const uint32_t g_wiznet_buf_size = 2048; + return g_wiznet_buf_size; +} -static wiz_NetInfo g_wiz_netinfo = {.mac = {0x00, 0x08, 0xdc, 0x11, 0x11, 0x11}, - .ip = {192, 168, 131, 42}, - .sn = {255, 255, 254, 0}, - .gw = {192, 168, 130, 1}, - .dns = {0, 0, 0, 0}, - .dhcp = NETINFO_STATIC}; +wiz_NetInfo *get_gnetinfo() { + static wiz_NetInfo g_wiz_netinfo = {.mac = {0x00, 0x08, 0xdc, 0x11, 0x11, 0x11}, + .ip = {192, 168, 130, 77}, + .sn = {255, 255, 254, 0}, + .gw = {192, 168, 130, 1}, + .dns = {0, 0, 0, 0}, + .dhcp = NETINFO_STATIC}; + return &g_wiz_netinfo; +} int network_init() { wiz_NetInfo check_wiz_netinfo; check_wiz_netinfo.dhcp = NETINFO_STATIC; - ctlnetwork(CN_SET_NETINFO, (void *)&g_wiz_netinfo); + ctlnetwork(CN_SET_NETINFO, (void *)get_gnetinfo()); ctlnetwork(CN_GET_NETINFO, (void *)&check_wiz_netinfo); - if (memcmp(&g_wiz_netinfo, &check_wiz_netinfo, sizeof(wiz_NetInfo)) != 0) { + if (memcmp(get_gnetinfo(), &check_wiz_netinfo, sizeof(wiz_NetInfo)) != 0) { KPrintf( "mac: %d; ip: %d; gw: %d; sn: %d; dns: %d; dhcp: %d;\n", - memcmp(&g_wiz_netinfo.mac, &check_wiz_netinfo.mac, sizeof(uint8_t) * 6), - memcmp(&g_wiz_netinfo.ip, &check_wiz_netinfo.ip, sizeof(uint8_t) * 4), - memcmp(&g_wiz_netinfo.sn, &check_wiz_netinfo.sn, sizeof(uint8_t) * 4), - memcmp(&g_wiz_netinfo.gw, &check_wiz_netinfo.gw, sizeof(uint8_t) * 4), - memcmp(&g_wiz_netinfo.dns, &check_wiz_netinfo.dns, sizeof(uint8_t) * 4), - memcmp(&g_wiz_netinfo.dhcp, &check_wiz_netinfo.dhcp, sizeof(uint8_t))); + memcmp(&get_gnetinfo()->mac, &check_wiz_netinfo.mac, sizeof(uint8_t) * 6), + memcmp(&get_gnetinfo()->ip, &check_wiz_netinfo.ip, sizeof(uint8_t) * 4), + memcmp(&get_gnetinfo()->sn, &check_wiz_netinfo.sn, sizeof(uint8_t) * 4), + memcmp(&get_gnetinfo()->gw, &check_wiz_netinfo.gw, sizeof(uint8_t) * 4), + memcmp(&get_gnetinfo()->dns, &check_wiz_netinfo.dns, sizeof(uint8_t) * 4), + memcmp(&get_gnetinfo()->dhcp, &check_wiz_netinfo.dhcp, sizeof(uint8_t))); KPrintf("WIZCHIP set network information fail.\n"); return ERROR; } uint8_t tmpstr[6]; ctlwizchip(CW_GET_ID, (void *)tmpstr); KPrintf("=== %s NET CONF ===\r\n", (char *)tmpstr); - KPrintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", g_wiz_netinfo.mac[0], - g_wiz_netinfo.mac[1], g_wiz_netinfo.mac[2], g_wiz_netinfo.mac[3], - g_wiz_netinfo.mac[4], g_wiz_netinfo.mac[5]); - KPrintf("SIP: %d.%d.%d.%d\r\n", g_wiz_netinfo.ip[0], g_wiz_netinfo.ip[1], - g_wiz_netinfo.ip[2], g_wiz_netinfo.ip[3]); - KPrintf("GAR: %d.%d.%d.%d\r\n", g_wiz_netinfo.gw[0], g_wiz_netinfo.gw[1], - g_wiz_netinfo.gw[2], g_wiz_netinfo.gw[3]); - KPrintf("SUB: %d.%d.%d.%d\r\n", g_wiz_netinfo.sn[0], g_wiz_netinfo.sn[1], - g_wiz_netinfo.sn[2], g_wiz_netinfo.sn[3]); - KPrintf("DNS: %d.%d.%d.%d\r\n", g_wiz_netinfo.dns[0], g_wiz_netinfo.dns[1], - g_wiz_netinfo.dns[2], g_wiz_netinfo.dns[3]); + KPrintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", get_gnetinfo()->mac[0], + get_gnetinfo()->mac[1], get_gnetinfo()->mac[2], get_gnetinfo()->mac[3], + get_gnetinfo()->mac[4], get_gnetinfo()->mac[5]); + KPrintf("SIP: %d.%d.%d.%d\r\n", get_gnetinfo()->ip[0], get_gnetinfo()->ip[1], + get_gnetinfo()->ip[2], get_gnetinfo()->ip[3]); + KPrintf("GAR: %d.%d.%d.%d\r\n", get_gnetinfo()->gw[0], get_gnetinfo()->gw[1], + get_gnetinfo()->gw[2], get_gnetinfo()->gw[3]); + KPrintf("SUB: %d.%d.%d.%d\r\n", get_gnetinfo()->sn[0], get_gnetinfo()->sn[1], + get_gnetinfo()->sn[2], get_gnetinfo()->sn[3]); + KPrintf("DNS: %d.%d.%d.%d\r\n", get_gnetinfo()->dns[0], get_gnetinfo()->dns[1], + get_gnetinfo()->dns[2], get_gnetinfo()->dns[3]); KPrintf("======================\r\n"); return EOK; @@ -255,312 +261,4 @@ int HwWiznetInit(void) { network_init(); return EOK; -} - -/****************** basic functions ********************/ - -enum TCP_OPTION { - SEND_DATA = 0, - RECV_DATA, -}; - -uint32_t wiz_client_op(uint8_t sn, uint8_t *buf, uint32_t buf_size, - uint8_t dst_ip[4], uint16_t dst_port, - enum TCP_OPTION opt) { - // assert(buf_size <= g_wiznet_buf_size); - int32_t ret; - switch (getSn_SR(sn)) { - case SOCK_CLOSE_WAIT: - wiz_sock_disconnect(sn); - break; - case SOCK_CLOSED: - wiz_socket(sn, Sn_MR_TCP, 5000, 0x00); - break; - case SOCK_INIT: - KPrintf("[SOCKET CLIENT] sock init.\n"); - wiz_sock_connect(sn, dst_ip, dst_port); - break; - case SOCK_ESTABLISHED: - if (getSn_IR(sn) & Sn_IR_CON) { - printf("[SOCKET CLIENT] %d:Connected\r\n", sn); - setSn_IR(sn, Sn_IR_CON); - } - if (opt == SEND_DATA) { - uint32_t sent_size = 0; - ret = wiz_sock_send(sn, buf, buf_size); - if (ret < 0) { - wiz_sock_close(sn); - return ret; - } - } else if (opt == RECV_DATA) { - uint32_t size = 0; - if ((size = getSn_RX_RSR(sn)) > 0) { - if (size > buf_size) size = buf_size; - ret = wiz_sock_recv(sn, buf, size); - if (ret <= 0) return ret; - } - } - break; - default: - break; - } -} - -void wiz_client_op_test(int argc, char *argv[]) { - /* argv[1]: ip ip addr - * argv[2]: port port number - * argv[3]: msg send msg - * argv[4]: count test times,if no this parameter,default 10 times - */ - if (argc < 4) - { - KPrintf("wiz_client_op_test error\n"); - return; - } - uint8_t client_sock = 2; - uint32_t tmp_ip[4]; - uint8_t ip[4]; - uint64_t pCount = 10; - uint8_t buf[g_wiznet_buf_size]; - uint16_t port; - - sscanf(argv[1], "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1], &tmp_ip[2], &tmp_ip[3]); - ip[0] = (uint8_t)tmp_ip[0]; - ip[1] = (uint8_t)tmp_ip[1]; - ip[2] = (uint8_t)tmp_ip[2]; - ip[3] = (uint8_t)tmp_ip[3]; - - port = atoi(argv[2]); - KPrintf("wiz client to wiz_server, send to %d.%d.%d.%d %d\n", // tip info - ip[0], ip[1], ip[2], ip[3], port); - - if (argc >= 5){ - pCount = atoi(argv[4]); - } - for(uint64_t i = 0; i < pCount; i++) - { - wiz_client_op(client_sock, argv[3], strlen(argv[3]), ip, port, SEND_DATA); - MdelayKTask(10); - // waiting for a responding. - wiz_client_op(client_sock, buf, g_wiznet_buf_size, ip, port, RECV_DATA); - KPrintf("received msg: %s\n", buf); - memset(buf, 0, g_wiznet_buf_size); - } -} - -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), - wiz_client_op, wiz_client_op_test, - wiz_sock_recv or wiz_sock_send data as tcp client); - -int32_t wiz_server_op(uint8_t sn, uint8_t *buf, uint32_t buf_size, - uint16_t port, enum TCP_OPTION opt) { - int32_t ret = 0; - uint16_t size = 0, sentsize = 0; - switch (getSn_SR(sn)) { - case SOCK_ESTABLISHED: - if (getSn_IR(sn) & Sn_IR_CON) { - printf("%d:Connected\r\n", sn); - setSn_IR(sn, Sn_IR_CON); - } - if (opt == SEND_DATA) { - uint32_t sent_size = 0; - ret = wiz_sock_send(sn, buf, buf_size); - if (ret < 0) { - wiz_sock_close(sn); - return ret; - } - } else if (opt == RECV_DATA) { - uint32_t size = 0; - if ((size = getSn_RX_RSR(sn)) > 0) { - if (size > buf_size) size = buf_size; - ret = wiz_sock_recv(sn, buf, size); - return ret; - } - } - break; - case SOCK_CLOSE_WAIT: - printf("%d:CloseWait\r\n", sn); - if ((ret = wiz_sock_disconnect(sn)) != SOCK_OK) return ret; - printf("%d:Closed\r\n", sn); - break; - case SOCK_INIT: - printf("%d:Listen, port [%d]\r\n", sn, port); - if ((ret = wiz_sock_listen(sn)) != SOCK_OK) return ret; - break; - case SOCK_CLOSED: - printf("%d:LBTStart\r\n", sn); - if ((ret = wiz_socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret; - printf("%d:Opened\r\n", sn); - break; - default: - break; - } - return 0; -} - -void wiz_server(void *param) { - uint16_t port = *(uint16_t *)param; - KPrintf("wiz server, listen port: %d\n", port); - uint8_t buf[g_wiznet_buf_size]; - memset(buf, 0, g_wiznet_buf_size); - int ret = 0; - uint32_t size = 0; - - while (1) { - ret = wiz_server_op(0, buf, g_wiznet_buf_size, port, RECV_DATA); - while(buf[size] != 0){ - size ++; - } - if (ret > 0) { - KPrintf("received %d bytes: %s\n", size, buf); - - wiz_server_op(0, buf, g_wiznet_buf_size, port, SEND_DATA); - memset(buf, 0, g_wiznet_buf_size); - } - size = 0; - } -} - -void wiz_server_test(uint16_t port) { - /* argv[1]: port - */ - int32 wiz_server_id = - KTaskCreate("wiz_server", wiz_server, (void *)&port, 4096, 25); - x_err_t flag = StartupKTask(wiz_server_id); - if (flag != EOK) { - KPrintf("StartupKTask wiz_server_id failed .\n"); - } -} - -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC) | - SHELL_CMD_PARAM_NUM(1), - wiz_server_op, wiz_server_test, - wiz_sock_recv or wiz_sock_send data as tcp server); - -void loopback_udps(int argc, char *argv[]) -{ - /* argv[1]: remote_ip ip address of remote udp - * argv[2]: remote_port the port number of the remote udp - * argv[2]: local_port the port number of the local udp - */ - uint32_t tmp_ip[4]; - uint8_t remote_ip[4]; - uint16_t remote_port, local_port; - uint8_t buffer[g_wiznet_buf_size]; - uint16_t len = 0; - - if (argc < 4) - { - KPrintf("loopback_udps test error\n"); - return; - } - - sscanf(argv[1], "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1], &tmp_ip[2], &tmp_ip[3]); - for(int i = 0;i < 4; i++) - { - remote_ip[i] = (uint8_t)tmp_ip[i]; - } - - remote_port = atoi(argv[2]); - local_port = atoi(argv[3]); - while(1) - { - switch (getSn_SR(0)) - { - case SOCK_UDP: - if(getSn_IR(0) & Sn_IR_RECV) - { - setSn_IR(0, Sn_IR_RECV); - } - if((len = getSn_RX_RSR(0))>0) - { - memset(buffer,0,len+1); - wiz_sock_recvfrom(0, buffer, len, remote_ip, (uint16_t *)&remote_port); - printf("received msg: %s\n", buffer); - wiz_sock_sendto(0, buffer, len, remote_ip, remote_port); - } - break; - - case SOCK_CLOSED: - printf("LBUStart\r\n"); - wiz_socket(0, Sn_MR_UDP, local_port, 0); - break; - } - } -} - -/* wiz_udp remote_ip remote_port local_port */ -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), - wiz_udp, loopback_udps, w5500 upd test); - -void ifconfig() { - wiz_NetInfo wiz_netinfo; - ctlnetwork(CN_GET_NETINFO, (void *)&wiz_netinfo); - uint8_t tmpstr[6]; - ctlwizchip(CW_GET_ID, (void *)tmpstr); - KPrintf("=== %s NET CONF ===\r\n", (char *)tmpstr); - KPrintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", wiz_netinfo.mac[0], - wiz_netinfo.mac[1], wiz_netinfo.mac[2], wiz_netinfo.mac[3], - wiz_netinfo.mac[4], wiz_netinfo.mac[5]); - KPrintf("SIP: %d.%d.%d.%d\r\n", wiz_netinfo.ip[0], wiz_netinfo.ip[1], - wiz_netinfo.ip[2], wiz_netinfo.ip[3]); - KPrintf("GAR: %d.%d.%d.%d\r\n", wiz_netinfo.gw[0], wiz_netinfo.gw[1], - wiz_netinfo.gw[2], wiz_netinfo.gw[3]); - KPrintf("SUB: %d.%d.%d.%d\r\n", wiz_netinfo.sn[0], wiz_netinfo.sn[1], - wiz_netinfo.sn[2], wiz_netinfo.sn[3]); - KPrintf("DNS: %d.%d.%d.%d\r\n", wiz_netinfo.dns[0], wiz_netinfo.dns[1], - wiz_netinfo.dns[2], wiz_netinfo.dns[3]); - KPrintf("======================\r\n"); -} -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), - ifconfig, ifconfig, printf w5500 configurations); - -void char_arr_assign(uint8_t *dst, uint32_t *src, int len) { - for (int i = 0; i < len; ++i) { - dst[i] = (uint8_t)(src[i]); - } -} - -char *network_param_name[] = {"ip", "sn", "gw"}; - -void config_w5500_network(int argc, char *argv[]) { - if (argc < 2) { - KPrintf("[W5500] Network config require params.\n"); - return; - } - - wiz_NetInfo wiz_netinfo; - memcpy(&wiz_netinfo, &g_wiz_netinfo, sizeof(wiz_NetInfo)); - - int cur_arg_idx = 1; - - while (argv[cur_arg_idx] != NULL) { - if (argv[cur_arg_idx + 1] == NULL) { - KPrintf("[W5500] Network config %s requires value.\n", argv[cur_arg_idx]); - return; - } - uint32_t tmp_arr[4]; - sscanf(argv[cur_arg_idx + 1], "%d.%d.%d.%d", &tmp_arr[0], &tmp_arr[1], - &tmp_arr[2], &tmp_arr[3]); - if (memcmp(argv[cur_arg_idx], network_param_name[0], 2 * sizeof(char)) == - 0) { - char_arr_assign(wiz_netinfo.ip, tmp_arr, 4); - } else if (memcmp(argv[cur_arg_idx], network_param_name[1], - 2 * sizeof(char)) == 0) { - char_arr_assign(wiz_netinfo.sn, tmp_arr, 4); - } else if (memcmp(argv[cur_arg_idx], network_param_name[2], - 2 * sizeof(char)) == 0) { - char_arr_assign(wiz_netinfo.gw, tmp_arr, 4); - } else { - KPrintf("[W5500] Invalid network param.\n"); - } - cur_arg_idx += 2; - } - - ctlnetwork(CN_SET_NETINFO, (void *)&wiz_netinfo); - KPrintf("[W5500] Network config success.\n", argv[cur_arg_idx]); - ifconfig(); -} -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), - config_w5500_network, config_w5500_network, - set w5500 configurations); \ No newline at end of file +} \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500_test.c b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500_test.c new file mode 100644 index 000000000..c85c33090 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/connect_w5500_test.c @@ -0,0 +1,314 @@ +#include +#include +#include + +#include "socket.h" + +extern uint32_t get_gbuf_size(); +extern wiz_NetInfo *get_gnetinfo(); + +enum TCP_OPTION { + SEND_DATA = 0, + RECV_DATA, +}; + +uint32_t wiz_client_op(uint8_t sn, uint8_t *buf, uint32_t buf_size, + uint8_t dst_ip[4], uint16_t dst_port, + enum TCP_OPTION opt) { + // assert(buf_size <= get_gbuf_size()); + int32_t ret; + switch (getSn_SR(sn)) { + case SOCK_CLOSE_WAIT: + wiz_sock_disconnect(sn); + break; + case SOCK_CLOSED: + wiz_socket(sn, Sn_MR_TCP, 5000, 0x00); + break; + case SOCK_INIT: + KPrintf("[SOCKET CLIENT] sock init.\n"); + wiz_sock_connect(sn, dst_ip, dst_port); + break; + case SOCK_ESTABLISHED: + if (getSn_IR(sn) & Sn_IR_CON) { + printf("[SOCKET CLIENT] %d:Connected\r\n", sn); + setSn_IR(sn, Sn_IR_CON); + } + if (opt == SEND_DATA) { + uint32_t sent_size = 0; + ret = wiz_sock_send(sn, buf, buf_size); + if (ret < 0) { + wiz_sock_close(sn); + return ret; + } + } else if (opt == RECV_DATA) { + uint32_t size = 0; + if ((size = getSn_RX_RSR(sn)) > 0) { + if (size > buf_size) size = buf_size; + ret = wiz_sock_recv(sn, buf, size); + if (ret <= 0) return ret; + } + } + break; + default: + break; + } +} + +void wiz_client_op_test(int argc, char *argv[]) { + /* argv[1]: ip ip addr + * argv[2]: port port number + * argv[3]: msg send msg + * argv[4]: count test times,if no this parameter,default 10 times + */ + if (argc < 4) + { + KPrintf("wiz_client_op_test error\n"); + return; + } + uint8_t client_sock = 2; + uint32_t tmp_ip[4]; + uint8_t ip[4]; + uint64_t pCount = 10; + uint8_t buf[get_gbuf_size()]; + uint16_t port; + + sscanf(argv[1], "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1], &tmp_ip[2], &tmp_ip[3]); + ip[0] = (uint8_t)tmp_ip[0]; + ip[1] = (uint8_t)tmp_ip[1]; + ip[2] = (uint8_t)tmp_ip[2]; + ip[3] = (uint8_t)tmp_ip[3]; + + port = atoi(argv[2]); + KPrintf("wiz client to wiz_server, send to %d.%d.%d.%d %d\n", // tip info + ip[0], ip[1], ip[2], ip[3], port); + + if (argc >= 5){ + pCount = atoi(argv[4]); + } + for(uint64_t i = 0; i < pCount; i++) + { + wiz_client_op(client_sock, argv[3], strlen(argv[3]), ip, port, SEND_DATA); + MdelayKTask(10); + // waiting for a responding. + wiz_client_op(client_sock, buf, get_gbuf_size(), ip, port, RECV_DATA); + KPrintf("received msg: %s\n", buf); + memset(buf, 0, get_gbuf_size()); + } +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), + wiz_client_op, wiz_client_op_test, + wiz_sock_recv or wiz_sock_send data as tcp client); + +int32_t wiz_server_op(uint8_t sn, uint8_t *buf, uint32_t buf_size, + uint16_t port, enum TCP_OPTION opt) { + int32_t ret = 0; + uint16_t size = 0, sentsize = 0; + switch (getSn_SR(sn)) { + case SOCK_ESTABLISHED: + if (getSn_IR(sn) & Sn_IR_CON) { + printf("%d:Connected\r\n", sn); + setSn_IR(sn, Sn_IR_CON); + } + if (opt == SEND_DATA) { + uint32_t sent_size = 0; + ret = wiz_sock_send(sn, buf, buf_size); + if (ret < 0) { + wiz_sock_close(sn); + return ret; + } + } else if (opt == RECV_DATA) { + uint32_t size = 0; + if ((size = getSn_RX_RSR(sn)) > 0) { + if (size > buf_size) size = buf_size; + ret = wiz_sock_recv(sn, buf, size); + return ret; + } + } + break; + case SOCK_CLOSE_WAIT: + printf("%d:CloseWait\r\n", sn); + if ((ret = wiz_sock_disconnect(sn)) != SOCK_OK) return ret; + printf("%d:Closed\r\n", sn); + break; + case SOCK_INIT: + printf("%d:Listen, port [%d]\r\n", sn, port); + if ((ret = wiz_sock_listen(sn)) != SOCK_OK) return ret; + break; + case SOCK_CLOSED: + printf("%d:LBTStart\r\n", sn); + if ((ret = wiz_socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret; + printf("%d:Opened\r\n", sn); + break; + default: + break; + } + return 0; +} + +void wiz_server(void *param) { + uint16_t port = *(uint16_t *)param; + KPrintf("wiz server, listen port: %d\n", port); + uint8_t buf[get_gbuf_size()]; + memset(buf, 0, get_gbuf_size()); + int ret = 0; + uint32_t size = 0; + + while (1) { + ret = wiz_server_op(0, buf, get_gbuf_size(), port, RECV_DATA); + while(buf[size] != 0){ + size ++; + } + if (ret > 0) { + KPrintf("received %d bytes: %s\n", size, buf); + + wiz_server_op(0, buf, get_gbuf_size(), port, SEND_DATA); + memset(buf, 0, get_gbuf_size()); + } + size = 0; + } +} + +void wiz_server_test(uint16_t port) { + /* argv[1]: port + */ + int32 wiz_server_id = + KTaskCreate("wiz_server", wiz_server, (void *)&port, 4096, 25); + x_err_t flag = StartupKTask(wiz_server_id); + if (flag != EOK) { + KPrintf("StartupKTask wiz_server_id failed .\n"); + } +} + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC) | + SHELL_CMD_PARAM_NUM(1), + wiz_server_op, wiz_server_test, + wiz_sock_recv or wiz_sock_send data as tcp server); + +void loopback_udps(int argc, char *argv[]) +{ + /* argv[1]: remote_ip ip address of remote udp + * argv[2]: remote_port the port number of the remote udp + * argv[2]: local_port the port number of the local udp + */ + uint32_t tmp_ip[4]; + uint8_t remote_ip[4]; + uint16_t remote_port, local_port; + uint8_t buffer[get_gbuf_size()]; + uint16_t len = 0; + + if (argc < 4) + { + KPrintf("loopback_udps test error\n"); + return; + } + + sscanf(argv[1], "%d.%d.%d.%d", &tmp_ip[0], &tmp_ip[1], &tmp_ip[2], &tmp_ip[3]); + for(int i = 0;i < 4; i++) + { + remote_ip[i] = (uint8_t)tmp_ip[i]; + } + + remote_port = atoi(argv[2]); + local_port = atoi(argv[3]); + while(1) + { + switch (getSn_SR(0)) + { + case SOCK_UDP: + if(getSn_IR(0) & Sn_IR_RECV) + { + setSn_IR(0, Sn_IR_RECV); + } + if((len = getSn_RX_RSR(0))>0) + { + memset(buffer, 0, len + 1); + wiz_sock_recvfrom(0, buffer, len, remote_ip, (uint16_t *)&remote_port); + printf("received msg: %s\n", buffer); + wiz_sock_sendto(0, buffer, len, remote_ip, remote_port); + } + break; + + case SOCK_CLOSED: + printf("LBUStart\r\n"); + wiz_socket(0, Sn_MR_UDP, local_port, 0); + break; + } + } +} + +/* wiz_udp remote_ip remote_port local_port */ +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), + wiz_udp, loopback_udps, w5500 upd test); + +void ifconfig() { + wiz_NetInfo wiz_netinfo; + ctlnetwork(CN_GET_NETINFO, (void *)&wiz_netinfo); + uint8_t tmpstr[6]; + ctlwizchip(CW_GET_ID, (void *)tmpstr); + KPrintf("=== %s NET CONF ===\r\n", (char *)tmpstr); + KPrintf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", wiz_netinfo.mac[0], + wiz_netinfo.mac[1], wiz_netinfo.mac[2], wiz_netinfo.mac[3], + wiz_netinfo.mac[4], wiz_netinfo.mac[5]); + KPrintf("SIP: %d.%d.%d.%d\r\n", wiz_netinfo.ip[0], wiz_netinfo.ip[1], + wiz_netinfo.ip[2], wiz_netinfo.ip[3]); + KPrintf("GAR: %d.%d.%d.%d\r\n", wiz_netinfo.gw[0], wiz_netinfo.gw[1], + wiz_netinfo.gw[2], wiz_netinfo.gw[3]); + KPrintf("SUB: %d.%d.%d.%d\r\n", wiz_netinfo.sn[0], wiz_netinfo.sn[1], + wiz_netinfo.sn[2], wiz_netinfo.sn[3]); + KPrintf("DNS: %d.%d.%d.%d\r\n", wiz_netinfo.dns[0], wiz_netinfo.dns[1], + wiz_netinfo.dns[2], wiz_netinfo.dns[3]); + KPrintf("======================\r\n"); +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), + ifconfig, ifconfig, printf w5500 configurations); + +void char_arr_assign(uint8_t *dst, uint32_t *src, int len) { + for (int i = 0; i < len; ++i) { + dst[i] = (uint8_t)(src[i]); + } +} + +char *network_param_name[] = {"ip", "sn", "gw"}; + +void config_w5500_network(int argc, char *argv[]) { + if (argc < 2) { + KPrintf("[W5500] Network config require params.\n"); + return; + } + + wiz_NetInfo wiz_netinfo; + memcpy(&wiz_netinfo, get_gnetinfo(), sizeof(wiz_NetInfo)); + + int cur_arg_idx = 1; + + while (argv[cur_arg_idx] != NULL) { + if (argv[cur_arg_idx + 1] == NULL) { + KPrintf("[W5500] Network config %s requires value.\n", argv[cur_arg_idx]); + return; + } + uint32_t tmp_arr[4]; + sscanf(argv[cur_arg_idx + 1], "%d.%d.%d.%d", &tmp_arr[0], &tmp_arr[1], + &tmp_arr[2], &tmp_arr[3]); + if (memcmp(argv[cur_arg_idx], network_param_name[0], 2 * sizeof(char)) == + 0) { + char_arr_assign(wiz_netinfo.ip, tmp_arr, 4); + } else if (memcmp(argv[cur_arg_idx], network_param_name[1], + 2 * sizeof(char)) == 0) { + char_arr_assign(wiz_netinfo.sn, tmp_arr, 4); + } else if (memcmp(argv[cur_arg_idx], network_param_name[2], + 2 * sizeof(char)) == 0) { + char_arr_assign(wiz_netinfo.gw, tmp_arr, 4); + } else { + KPrintf("[W5500] Invalid network param.\n"); + } + cur_arg_idx += 2; + } + + ctlnetwork(CN_SET_NETINFO, (void *)&wiz_netinfo); + KPrintf("[W5500] Network config success.\n", argv[cur_arg_idx]); + ifconfig(); +} +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), + config_w5500_network, config_w5500_network, + set w5500 configurations); \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/wiz_iperf.c b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/wiz_iperf.c new file mode 100644 index 000000000..09c154347 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/edu-riscv64/third_party_driver/ethernet/wiz_iperf.c @@ -0,0 +1,462 @@ +#include "socket.h" +#include "w5500.h" +#include "connect_w5500.h" + +#define IPERF_PORT 5001 +#define IPERF_BUFSZ (4 * 1024) + +#define IPERF_MODE_STOP 0 +#define IPERF_MODE_SERVER 1 +#define IPERF_MODE_CLIENT 2 + +typedef struct{ + int mode; + uint8 host[4]; + uint16 port; +} IPERF_PARAM; +static IPERF_PARAM param = {IPERF_MODE_STOP, {0, 0, 0, 0}, IPERF_PORT}; + +static void iperf_udp_client(void *thread_param) +{ + int sock; + uint8 *buffer; + uint16 local_port = 4840; + uint32 packet_count = 0; + uint32 tick; + int send_size; + + send_size = IPERF_BUFSZ > 1470 ? 1470 : IPERF_BUFSZ; + + sock = 0; // w5500支持8个socket独立工作,todo socket端口管理 + // setSn_TXBUF_SIZE(sock, 16); + // setSn_RXBUF_SIZE(sock, 16); + + + buffer = malloc(IPERF_BUFSZ); + if (buffer == NULL){ + printf("[%s:%d] malloc failed\n", __FILE__, __LINE__); + return; + } + for(int i = 0; i < IPERF_BUFSZ; i++) + buffer[i] = i % 10; + + KPrintf("iperf udp mode run...\n"); + while (param.mode != IPERF_MODE_STOP){ + switch(getSn_SR(sock)){ + case SOCK_CLOSED: + wiz_socket(sock, Sn_MR_UDP, local_port, 0x00); + break; + case SOCK_UDP: + packet_count++; + tick = CurrentTicksGain(); + buffer[0] = (uint8)(packet_count >> 24); + buffer[1] = (uint8)(packet_count >> 16); + buffer[2] = (uint8)(packet_count >> 8); + buffer[3] = (uint8)(packet_count); + + buffer[4] = (uint8)((tick / TICK_PER_SECOND) >> 24); + buffer[5] = (uint8)((tick / TICK_PER_SECOND) >> 16); + buffer[6] = (uint8)((tick / TICK_PER_SECOND) >> 8); + buffer[7] = (uint8)((tick / TICK_PER_SECOND)); + + buffer[8] = (uint8)(((tick % TICK_PER_SECOND) * 1000) >> 24); + buffer[9] = (uint8)(((tick % TICK_PER_SECOND) * 1000) >> 16); + buffer[10] = (uint8)(((tick % TICK_PER_SECOND) * 1000) >> 8); + buffer[11] = (uint8)(((tick % TICK_PER_SECOND) * 1000)); + wiz_sock_sendto(sock, buffer, send_size, param.host, param.port); + break; + } + } + if(getSn_SR(sock) != SOCK_CLOSED) wiz_sock_close(sock); + free(buffer); + KPrintf("iperf udp mode exit...\n"); +} + +static void iperf_udp_server(void *thread_param) +{ + int sock, sender_len, r_size; + uint8 *buffer, client_addr[4]; + uint16 client_port; + uint32 pcount = 0, last_pcount = 0; + uint32 lost, total; + uint64 recvlen; + x_ticks_t tick1, tick2; + struct timeval timeout; + + buffer = malloc(IPERF_BUFSZ); + if (buffer == NULL){ + return; + } + + sock = 0; //todo + // setSn_RXBUF_SIZE(sock, 16); + // setSn_TXBUF_SIZE(sock, 16); + + KPrintf("iperf udp server run...\n"); + while (param.mode != IPERF_MODE_STOP){ + tick1 = CurrentTicksGain(); + tick2 = tick1; + lost = 0; + total = 0; + recvlen = 0; + while ((tick2 - tick1) < (TICK_PER_SECOND * 5)){ + switch(getSn_SR(sock)){ + case SOCK_UDP: + if ((r_size = getSn_RX_RSR(sock)) > 0){ + if (r_size > IPERF_BUFSZ) r_size = IPERF_BUFSZ; + memset(buffer, 0, IPERF_BUFSZ); + wiz_sock_recvfrom(sock, buffer, r_size, client_addr, &client_port); + recvlen += r_size; + last_pcount = buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3]; + if(last_pcount > pcount){ + total += last_pcount - pcount; + lost += last_pcount - pcount - 1; + pcount = last_pcount; + } + else if(last_pcount < 10){ + pcount = last_pcount; + total = 1; + lost = 0; + } + } + tick2 = CurrentTicksGain(); + break; + case SOCK_CLOSED: + wiz_socket(sock, Sn_MR_UDP, param.port, 0x00); + break; + } + } + if (recvlen > 0){ + long data; + int integer, decimal; + KTaskDescriptorType tid; + + tid = GetKTaskDescriptor(); + data = recvlen * TICK_PER_SECOND / 125 / (tick2 - tick1); + integer = data/1000; + decimal = data%1000; + KPrintf("%s: %d.%03d0 Mbps! recv:%d lost:%d total:%d\n", tid->task_base_info.name, integer, decimal, total - lost, lost, total); + } + } + free(buffer); + if(getSn_SR(sock) != SOCK_CLOSED) wiz_sock_close(sock); +} + +static void iperf_client(void *thread_param) +{ + int i; + int sock = 0; + int ret; + uint8_t *send_buf, connected = 0; + uint64 sentlen; + x_ticks_t tick1, tick2; + + // setSn_RXBUF_SIZE(sock, 16); + // setSn_TXBUF_SIZE(sock, 16); + + send_buf = (uint8_t *) malloc(IPERF_BUFSZ); + if (!send_buf) return ; + + for (i = 0; i < IPERF_BUFSZ; i ++) + send_buf[i] = i & 0xff; + + while (param.mode != IPERF_MODE_STOP) + { + while((getSn_SR(sock) != SOCK_ESTABLISHED || !connected) && param.mode != IPERF_MODE_STOP){ + switch (getSn_SR(sock)) { + case SOCK_ESTABLISHED: + if (getSn_IR(sock) & Sn_IR_CON) { + KPrintf("Connected\n", sock); + setSn_IR(sock, Sn_IR_CON); + } + connected = 1; + break; + case SOCK_CLOSE_WAIT: + wiz_sock_disconnect(sock); + break; + case SOCK_INIT: + KPrintf("Socket %d:try to connect to [%d.%d.%d.%d:%d]...", + sock, param.host[0], param.host[1], param.host[2], param.host[3], param.port); + ret = wiz_sock_connect(sock, param.host, param.port); + if (ret != SOCK_OK){ + printf("failed, wait 1s to try again\n"); + MdelayKTask(1000); + } + break; + case SOCK_CLOSED: + if(connected) KPrintf("Socket %d:closed\n", sock); + wiz_socket(sock, Sn_MR_TCP, param.port, 0x00); + connected = 0; + break; + default: + break; + } + } + + sentlen = 0; + + tick1 = CurrentTicksGain(); + while (param.mode != IPERF_MODE_STOP){ + tick2 = CurrentTicksGain(); + if (tick2 - tick1 >= TICK_PER_SECOND * 5){ + long data; + int integer, decimal; + KTaskDescriptorType tid; + + tid = GetKTaskDescriptor(); + data = sentlen * TICK_PER_SECOND / 125 / (tick2 - tick1); + integer = data/1000; + decimal = data%1000; + KPrintf("%s: %d.%03d0 Mbps!\n", tid->task_base_info.name, integer, decimal); + tick1 = tick2; + sentlen = 0; + } + + ret = wiz_sock_send(sock, send_buf, IPERF_BUFSZ); + if (ret > 0){ + sentlen += ret; + } + + if (ret < 0) break; + } + + if(getSn_SR(sock) != SOCK_CLOSED)wiz_sock_close(sock); + + KPrintf("Disconnected, iperf client exit!"); + } + free(send_buf); +} + +static void iperf_server(void *thread_param) +{ + uint8_t *recv_data; + x_ticks_t tick1, tick2; + int sock = -1, connected = 0, bytes_received; + uint64 recvlen; + + sock = 0; //todo + // setSn_RXBUF_SIZE(sock, 16); + // setSn_TXBUF_SIZE(sock, 16); + + recv_data = (uint8_t *)malloc(IPERF_BUFSZ); + if (recv_data == NULL){ + KPrintf("No memory!\n"); + goto __exit; + } + + while (param.mode != IPERF_MODE_STOP){ + while((getSn_SR(sock) != SOCK_ESTABLISHED || !connected)){ + switch (getSn_SR(sock)) { + case SOCK_ESTABLISHED: + if (getSn_IR(sock) & Sn_IR_CON) { + KPrintf("Socket %d:Connected\n", sock); + setSn_IR(sock, Sn_IR_CON); + } + recvlen = 0; + tick1 = CurrentTicksGain(); + connected = 1; + break; + case SOCK_CLOSE_WAIT: + wiz_sock_disconnect(sock); + break; + case SOCK_INIT: + KPrintf("Socket %d:Listen, port [%d]\n", sock, param.port); + wiz_sock_listen(sock); + break; + case SOCK_CLOSED: + if(connected) KPrintf("Socket %d:closed\n", sock); + wiz_socket(sock, Sn_MR_TCP, param.port, 0x00); + connected = 0; + break; + default: + break; + } + if(param.mode == IPERF_MODE_STOP) goto __exit; + } + + if ((bytes_received = getSn_RX_RSR(sock)) > 0) { + if (bytes_received > IPERF_BUFSZ) bytes_received = IPERF_BUFSZ; + memset(recv_data, 0, IPERF_BUFSZ); + wiz_sock_recv(sock, recv_data, bytes_received); + recvlen += bytes_received; + } + + tick2 = CurrentTicksGain(); + if (tick2 - tick1 >= TICK_PER_SECOND * 5){ + long data; + int integer, decimal; + KTaskDescriptorType tid; + + tid = GetKTaskDescriptor(); + data = recvlen * TICK_PER_SECOND / 125 / (tick2 - tick1); + integer = data/1000; + decimal = data%1000; + KPrintf("%s: %d.%03d0 Mbps!\n", tid->task_base_info.name, integer, decimal); + tick1 = tick2; + recvlen = 0; + } + } + +__exit: + if(getSn_SR(sock) != SOCK_CLOSED)wiz_sock_close(sock); + if (recv_data) free(recv_data); +} + +void iperf_usage(void) +{ + KPrintf("Usage: iperf [-s|-c host] [options] [multi-threaded]\n"); + KPrintf(" iperf [-h|--stop]\n"); + KPrintf("\n"); + KPrintf("Client/Server:\n"); + KPrintf(" -p # server port to listen on/connect to\n"); + KPrintf("\n"); + KPrintf("Server specific:\n"); + KPrintf(" -s run in server mode\n"); + KPrintf("\n"); + KPrintf("Client specific:\n"); + KPrintf(" -c run in client mode, connecting to \n"); + KPrintf("\n"); + KPrintf("Miscellaneous:\n"); + KPrintf(" -h print this message and quit\n"); + KPrintf(" --stop stop iperf program\n"); + KPrintf(" -u testing UDP protocol\n"); + KPrintf(" -m