diff options
| author | yangjie11 <[email protected]> | 2020-11-18 09:56:35 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-11-18 09:56:35 +0800 |
| commit | d4a682e8d97fa131e5a8d795f8c5451d55cdd5b0 (patch) | |
| tree | 4773b40cb012475c5daf74bfd55707e141477669 /examples | |
| parent | 2b98f4720c827ae1850df78299f6b273d568c8c7 (diff) | |
| parent | 2ef3c9d861f55253620996a94831a4be31ca6620 (diff) | |
Merge pull request #3 from RT-Thread/master
sync
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/file/listdir.c | 97 | ||||
| -rw-r--r-- | examples/file/readspeed.c | 39 | ||||
| -rw-r--r-- | examples/file/readwrite.c | 137 | ||||
| -rw-r--r-- | examples/file/seekdir.c | 19 | ||||
| -rw-r--r-- | examples/file/writespeed.c | 80 | ||||
| -rw-r--r-- | examples/nanopb/SConscript | 13 | ||||
| -rw-r--r-- | examples/nanopb/simple.c | 73 | ||||
| -rw-r--r-- | examples/nanopb/simple.options | 1 | ||||
| -rw-r--r-- | examples/nanopb/simple.pb.c | 18 | ||||
| -rw-r--r-- | examples/nanopb/simple.pb.h | 45 | ||||
| -rw-r--r-- | examples/nanopb/simple.proto | 10 | ||||
| -rw-r--r-- | examples/network/tcpclient.c | 10 | ||||
| -rw-r--r-- | examples/network/tcpsendpacket.c | 37 | ||||
| -rw-r--r-- | examples/network/tcpserver.c | 12 | ||||
| -rw-r--r-- | examples/network/udpclient.c | 14 | ||||
| -rw-r--r-- | examples/network/udpserver.c | 12 | ||||
| -rw-r--r-- | examples/pm/timer_app.c | 5 | ||||
| -rw-r--r-- | examples/pm/wakeup_app.c | 77 | ||||
| -rw-r--r-- | examples/test/hwtimer_test.c | 30 | ||||
| -rw-r--r-- | examples/test/net_test.c | 441 |
20 files changed, 633 insertions, 537 deletions
diff --git a/examples/file/listdir.c b/examples/file/listdir.c index cf91bc77d4..d8ea7af82a 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -1,56 +1,85 @@ /* - * File : listdir.c - * This file is part of RT-TestCase in RT-Thread RTOS - * COPYRIGHT (C) 2010, RT-Thread Development Team + * Copyright (c) 2006-2020, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2010-02-10 Bernard first version + * 2020-04-12 Jianjia Ma add msh cmd */ #include <rtthread.h> #include <dfs_posix.h> -static char fullpath[256]; void list_dir(const char* path) { - DIR *dir; + char * fullpath; + DIR *dir; - dir = opendir(path); - if (dir != RT_NULL) - { - struct dirent* dirent; - struct stat s; + dir = opendir(path); + if (dir != RT_NULL) + { + struct dirent* dirent; + struct stat s; - do - { - dirent = readdir(dir); - if (dirent == RT_NULL) break; - rt_memset(&s, 0, sizeof(struct stat)); + fullpath = rt_malloc(256); + if (fullpath == RT_NULL) + { + closedir(dir); + rt_kprintf("no memory\n"); + return; + } - /* build full path for each file */ - rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); + do + { + dirent = readdir(dir); + if (dirent == RT_NULL) break; + rt_memset(&s, 0, sizeof(struct stat)); - stat(fullpath, &s); - if ( s.st_mode & DFS_S_IFDIR ) - { - rt_kprintf("%s\t\t<DIR>\n", dirent->d_name); - } - else - { - rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size); - } - } while (dirent != RT_NULL); + /* build full path for each file */ + rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); - closedir(dir); - } - else rt_kprintf("open %s directory failed\n", path); + stat(fullpath, &s); + if ( s.st_mode & S_IFDIR ) + { + rt_kprintf("%s\t\t<DIR>\n", dirent->d_name); + } + else + { + rt_kprintf("%s\t\t%lu\n", dirent->d_name, s.st_size); + } + } while (dirent != RT_NULL); + + closedir(dir); + } + else + { + rt_kprintf("open %s directory failed\n", path); + } + + rt_free(fullpath); } #ifdef RT_USING_FINSH #include <finsh.h> FINSH_FUNCTION_EXPORT(list_dir, list directory); -#endif + +#ifdef FINSH_USING_MSH +static void cmd_list_dir(int argc, char *argv[]) +{ + char* filename; + + if(argc == 2) + { + filename = argv[1]; + } + else + { + rt_kprintf("Usage: list_dir [file_path]\n"); + return; + } + list_dir(filename); +} +FINSH_FUNCTION_EXPORT_ALIAS(cmd_list_dir, __cmd_list_dir, list directory); +#endif /* FINSH_USING_MSH */ +#endif /* RT_USING_FINSH */ diff --git a/examples/file/readspeed.c b/examples/file/readspeed.c index 52f15b627b..e5073856b4 100644 --- a/examples/file/readspeed.c +++ b/examples/file/readspeed.c @@ -1,15 +1,12 @@ /* - * File : readspeed.c - * This file is part of RT-TestCase in RT-Thread RTOS - * COPYRIGHT (C) 2010, RT-Thread Development Team + * Copyright (c) 2006-2020, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2010-02-10 Bernard first version + * 2020-04-12 Jianjia Ma add msh cmd */ #include <rtthread.h> @@ -34,7 +31,6 @@ void readspeed(const char* filename, int block_size) { rt_kprintf("no memory\n"); close(fd); - return; } @@ -61,4 +57,31 @@ void readspeed(const char* filename, int block_size) #ifdef RT_USING_FINSH #include <finsh.h> FINSH_FUNCTION_EXPORT(readspeed, perform file read test); -#endif + +#ifdef FINSH_USING_MSH +static void cmd_readspeed(int argc, char *argv[]) +{ + char* filename; + int block_size; + + if(argc == 3) + { + filename = argv[1]; + block_size = atoi(argv[2]); + } + else if(argc == 2) + { + filename = argv[1]; + block_size = 512; + } + else + { + rt_kprintf("Usage:\nreadspeed [file_path] [block_size]\n"); + rt_kprintf("readspeed [file_path] with default block size 512\n"); + return; + } + readspeed(filename, block_size); +} +FINSH_FUNCTION_EXPORT_ALIAS(cmd_readspeed, __cmd_readspeed, test file system read speed); +#endif /* FINSH_USING_MSH */ +#endif /* RT_USING_FINSH */ diff --git a/examples/file/readwrite.c b/examples/file/readwrite.c index 6f25249c61..5738323805 100644 --- a/examples/file/readwrite.c +++ b/examples/file/readwrite.c @@ -1,123 +1,168 @@ /* - * 代码清单:文件读写例子 + * Copyright (c) 2006-2020, RT-Thread Development Team * - * 这个例子演示了如何读写一个文件,特别是写的时候应该如何操作。 + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2010-02-10 Bernard first version + * 2020-04-12 Jianjia Ma add msh cmd */ #include <rtthread.h> -#include <dfs_posix.h> /* 当需要使用文件操作时,需要包含这个头文件 */ - -#define TEST_FN "/test.dat" +#include <dfs_posix.h> -/* 测试用的数据和缓冲 */ -static char test_data[120], buffer[120]; +#define TEST_DATA_LEN 120 -/* 文件读写测试 */ +/* file read write test */ void readwrite(const char* filename) { int fd; int index, length; + char* test_data; + char* buffer; + int block_size = TEST_DATA_LEN; - /* 只写 & 创建 打开 */ - fd = open(TEST_FN, O_WRONLY | O_CREAT | O_TRUNC, 0); + /* open with write only & create */ + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); if (fd < 0) { rt_kprintf("open file for write failed\n"); return; } - /* 准备写入数据 */ - for (index = 0; index < sizeof(test_data); index ++) + test_data = rt_malloc(block_size); + if (test_data == RT_NULL) + { + rt_kprintf("no memory\n"); + close(fd); + return; + } + + buffer = rt_malloc(block_size); + if (buffer == RT_NULL) + { + rt_kprintf("no memory\n"); + close(fd); + rt_free(test_data); + return; + } + + /* prepare some data */ + for (index = 0; index < block_size; index ++) { test_data[index] = index + 27; } - /* 写入数据 */ - length = write(fd, test_data, sizeof(test_data)); - if (length != sizeof(test_data)) + /* write to file */ + length = write(fd, test_data, block_size); + if (length != block_size) { rt_kprintf("write data failed\n"); close(fd); - return; + goto __exit; } - /* 关闭文件 */ + /* close file */ close(fd); - /* 只写并在末尾添加打开 */ - fd = open(TEST_FN, O_WRONLY | O_CREAT | O_APPEND, 0); + /* reopen the file with append to the end */ + fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0); if (fd < 0) { rt_kprintf("open file for append write failed\n"); - return; + goto __exit;; } - length = write(fd, test_data, sizeof(test_data)); - if (length != sizeof(test_data)) + length = write(fd, test_data, block_size); + if (length != block_size) { rt_kprintf("append write data failed\n"); close(fd); - return; + goto __exit; } - /* 关闭文件 */ + /* close the file */ close(fd); - /* 只读打开进行数据校验 */ - fd = open(TEST_FN, O_RDONLY, 0); + /* open the file for data validation. */ + fd = open(filename, O_RDONLY, 0); if (fd < 0) { rt_kprintf("check: open file for read failed\n"); - return; + goto __exit; } - /* 读取数据(应该为第一次写入的数据) */ - length = read(fd, buffer, sizeof(buffer)); - if (length != sizeof(buffer)) + /* read the data (should be the data written by the first time ) */ + length = read(fd, buffer, block_size); + if (length != block_size) { rt_kprintf("check: read file failed\n"); close(fd); - return; + goto __exit; } - /* 检查数据是否正确 */ - for (index = 0; index < sizeof(test_data); index ++) + /* validate */ + for (index = 0; index < block_size; index ++) { if (test_data[index] != buffer[index]) { rt_kprintf("check: check data failed at %d\n", index); close(fd); - return; + goto __exit; } } - /* 读取数据(应该为第二次写入的数据) */ - length = read(fd, buffer, sizeof(buffer)); - if (length != sizeof(buffer)) + /* read the data (should be the second time data) */ + length = read(fd, buffer, block_size); + if (length != block_size) { rt_kprintf("check: read file failed\n"); close(fd); - return; + goto __exit; } - /* 检查数据是否正确 */ - for (index = 0; index < sizeof(test_data); index ++) + /* validate */ + for (index = 0; index < block_size; index ++) { if (test_data[index] != buffer[index]) { rt_kprintf("check: check data failed at %d\n", index); close(fd); - return; + goto __exit; } } - /* 检查数据完毕,关闭文件 */ + /* close the file */ close(fd); - /* 打印结果 */ - rt_kprintf("read/write done.\n"); + /* print result */ + rt_kprintf("read/write test successful!\n"); + +__exit: + rt_free(test_data); + rt_free(buffer); } #ifdef RT_USING_FINSH #include <finsh.h> -/* 输出函数到finsh shell命令行中 */ +/* export to finsh */ FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test); -#endif + +#ifdef FINSH_USING_MSH +static void cmd_readwrite(int argc, char *argv[]) +{ + char* filename; + + if(argc == 2) + { + filename = argv[1]; + } + else + { + rt_kprintf("Usage: readwrite [file_path]\n"); + return; + } + readwrite(filename); +} +FINSH_FUNCTION_EXPORT_ALIAS(cmd_readwrite, __cmd_readwrite, perform file read and write test); +#endif /* FINSH_USING_MSH */ +#endif /* RT_USING_FINSH */ diff --git a/examples/file/seekdir.c b/examples/file/seekdir.c index 1cf8b5fd78..d6a33426b4 100644 --- a/examples/file/seekdir.c +++ b/examples/file/seekdir.c @@ -1,15 +1,12 @@ /* - * File : seekdir.c - * This file is part of RT-TestCase in RT-Thread RTOS - * COPYRIGHT (C) 2011, RT-Thread Development Team + * Copyright (c) 2006-2020, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2011-06-02 Bernard first version + * 2020-04-12 Jianjia Ma add msh cmd */ #include <dfs_posix.h> @@ -17,7 +14,6 @@ void seekdir_test(void) { DIR * dirp; long save3 = 0; - long cur; int i = 0; struct dirent *dp; @@ -27,14 +23,14 @@ void seekdir_test(void) { rt_kprintf("direntry: %s\n", dp->d_name); - /* ���������Ŀ¼���Ŀ¼ָ�� */ + /* save the pointer of the third directory */ if (i++ == 3) { save3 = telldir(dirp); } } - /* �ص��ղű���ĵ�����Ŀ¼���Ŀ¼ָ�� */ + /* get back to the third directory */ seekdir (dirp, save3); rt_kprintf("seek dientry to: %d\n", save3); for (dp = readdir(dirp); dp != RT_NULL; dp = readdir(dirp)) @@ -42,9 +38,12 @@ void seekdir_test(void) rt_kprintf("direntry: %s\n", dp->d_name); } - /* �ر�Ŀ¼ */ + /* close the directory */ closedir (dirp); } +#ifdef RT_USING_FINSH #include <finsh.h> FINSH_FUNCTION_EXPORT(seekdir_test, perform directory seek test); +MSH_CMD_EXPORT(seekdir_test, perform directory seek test); +#endif /* RT_USING_FINSH */ diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index ab6946888f..60dd900a68 100644 --- a/examples/file/writespeed.c +++ b/examples/file/writespeed.c @@ -1,15 +1,12 @@ /* - * File : writespeed.c - * This file is part of RT-TestCase in RT-Thread RTOS - * COPYRIGHT (C) 2010, RT-Thread Development Team + * Copyright (c) 2006-2020, RT-Thread Development Team * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rt-thread.org/license/LICENSE + * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2010-02-10 Bernard first version + * 2020-04-12 Jianjia Ma add msh cmd */ #include <rtthread.h> #include <dfs_posix.h> @@ -32,35 +29,34 @@ void writespeed(const char* filename, int total_length, int block_size) { rt_kprintf("no memory\n"); close(fd); - return; } - /* prepare write data */ - for (index = 0; index < block_size; index++) - { - buff_ptr[index] = index; - } - index = 0; + /* prepare write data */ + for (index = 0; index < block_size; index++) + { + buff_ptr[index] = index; + } + index = 0; - /* get the beginning tick */ + /* get the beginning tick */ tick = rt_tick_get(); - while (index < total_length / block_size) - { - length = write(fd, buff_ptr, block_size); - if (length != block_size) - { - rt_kprintf("write failed\n"); - break; - } + while (index < total_length / block_size) + { + length = write(fd, buff_ptr, block_size); + if (length != block_size) + { + rt_kprintf("write failed\n"); + break; + } - index ++; - } + index ++; + } tick = rt_tick_get() - tick; - /* close file and release memory */ + /* close file and release memory */ close(fd); - rt_free(buff_ptr); + rt_free(buff_ptr); /* calculate write speed */ rt_kprintf("File write speed: %d byte/s\n", total_length / tick * RT_TICK_PER_SECOND); @@ -69,4 +65,34 @@ void writespeed(const char* filename, int total_length, int block_size) #ifdef RT_USING_FINSH #include <finsh.h> FINSH_FUNCTION_EXPORT(writespeed, perform file write test); -#endif + +#ifdef FINSH_USING_MSH +static void cmd_writespeed(int argc, char *argv[]) +{ + char* filename; + int length; + int block_size; + + if(argc == 4) + { + filename = argv[1]; + length = atoi(argv[2]); + block_size = atoi(argv[3]); + } + else if(argc == 2) + { + filename = argv[1]; + block_size = 512; + length = 1024*1024; + } + else + { + rt_kprintf("Usage:\nwritespeed [file_path] [length] [block_size]\n"); + rt_kprintf("writespeed [file_path] with default length 1MB and block size 512\n"); + return; + } + writespeed(filename, length, block_size); +} +FINSH_FUNCTION_EXPORT_ALIAS(cmd_writespeed, __cmd_writespeed, test file system write speed); +#endif /* FINSH_USING_MSH */ +#endif /* RT_USING_FINSH */ diff --git a/examples/nanopb/SConscript b/examples/nanopb/SConscript deleted file mode 100644 index 7c8bce9f26..0000000000 --- a/examples/nanopb/SConscript +++ /dev/null @@ -1,13 +0,0 @@ -Import('RTT_ROOT') -Import('rtconfig') -from building import * - -src = Split(''' -simple.c -simple.pb.c -''') -CPPPATH = [RTT_ROOT + '/examples/nanopb'] - -group = DefineGroup('Nanopb_test', src, depend = ['RT_USING_NANOPB'], CPPPATH = CPPPATH) - -Return('group')
\ No newline at end of file diff --git a/examples/nanopb/simple.c b/examples/nanopb/simple.c deleted file mode 100644 index 2b793c014f..0000000000 --- a/examples/nanopb/simple.c +++ /dev/null @@ -1,73 +0,0 @@ -#include <rthw.h> -#include <stm32f10x.h> -#include <pb_encode.h> -#include <pb_decode.h> -#include "simple.pb.h" - -int nanopb_test() -{ - /* This is the buffer where we will store our message. */ - uint8_t buffer[128]; - size_t message_length; - bool status; - - /* Encode our message */ - { - /* Allocate space on the stack to store the message data. - * - * Nanopb generates simple struct definitions for all the messages. - * - check out the contents of simple.pb.h! */ - SimpleMessage message = SimpleMessage_init_zero; - - /* Create a stream that will write to our buffer. */ - pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); - - /* Fill in the lucky number */ - message.lucky_number = 13; - - /* Now we are ready to encode the message! */ - status = pb_encode(&stream, SimpleMessage_fields, &message); - message_length = stream.bytes_written; - - /* Then just check for any errors.. */ - if (!status) - { - rt_kprintf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); - return 1; - } - } - - /* Now we could transmit the message over network, store it in a file or - * wrap it to a pigeon's leg. - */ - - /* But because we are lazy, we will just decode it immediately. */ - - { - /* Allocate space for the decoded message. */ - SimpleMessage message; - - /* Create a stream that reads from the buffer. */ - pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); - - /* Now we are ready to decode the message. */ - status = pb_decode(&stream, SimpleMessage_fields, &message); - - /* Check for errors... */ - if (!status) - { - rt_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); - return 1; - } - - /* Print the data contained in the message. */ - rt_kprintf("Your lucky number was %d!\n", message.lucky_number); - } - - return 0; -} - -#ifdef RT_USING_FINSH -#include <finsh.h> -FINSH_FUNCTION_EXPORT(nanopb_test, nanopb encode/decode test.) -#endif diff --git a/examples/nanopb/simple.options b/examples/nanopb/simple.options deleted file mode 100644 index 90960d0d34..0000000000 --- a/examples/nanopb/simple.options +++ /dev/null @@ -1 +0,0 @@ -SimpleMessage.name max_size:16
\ No newline at end of file diff --git a/examples/nanopb/simple.pb.c b/examples/nanopb/simple.pb.c deleted file mode 100644 index eaf09e58da..0000000000 --- a/examples/nanopb/simple.pb.c +++ /dev/null @@ -1,18 +0,0 @@ -/* Automatically generated nanopb constant definitions */ -/* Generated by nanopb-0.3.1 at Tue Mar 10 01:16:15 2015. */ - -#include "simple.pb.h" - -#if PB_PROTO_HEADER_VERSION != 30 -#error Regenerate this file with the current version of nanopb generator. -#endif - - - -const pb_field_t SimpleMessage_fields[3] = { - PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, SimpleMessage, lucky_number, lucky_number, 0), - PB_FIELD( 2, BYTES , REQUIRED, STATIC , OTHER, SimpleMessage, name, lucky_number, 0), - PB_LAST_FIELD -}; - - diff --git a/examples/nanopb/simple.pb.h b/examples/nanopb/simple.pb.h deleted file mode 100644 index 47521dee69..0000000000 --- a/examples/nanopb/simple.pb.h +++ /dev/null @@ -1,45 +0,0 @@ -/* Automatically generated nanopb header */ -/* Generated by nanopb-0.3.1 at Tue Mar 10 01:16:15 2015. */ - -#ifndef PB_SIMPLE_PB_H_INCLUDED -#define PB_SIMPLE_PB_H_INCLUDED -#include <pb.h> - -#if PB_PROTO_HEADER_VERSION != 30 -#error Regenerate this file with the current version of nanopb generator. -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* Enum definitions */ -/* Struct definitions */ -typedef PB_BYTES_ARRAY_T(16) SimpleMessage_name_t; - -typedef struct _SimpleMessage { - int32_t lucky_number; - SimpleMessage_name_t name; -} SimpleMessage; - -/* Default values for struct fields */ - -/* Initializer values for message structs */ -#define SimpleMessage_init_default {0, {0, {0}}} -#define SimpleMessage_init_zero {0, {0, {0}}} - -/* Field tags (for use in manual encoding/decoding) */ -#define SimpleMessage_lucky_number_tag 1 -#define SimpleMessage_name_tag 2 - -/* Struct field encoding specification for nanopb */ -extern const pb_field_t SimpleMessage_fields[3]; - -/* Maximum encoded size of messages (where known) */ -#define SimpleMessage_size 29 - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif diff --git a/examples/nanopb/simple.proto b/examples/nanopb/simple.proto deleted file mode 100644 index 1905d0e50b..0000000000 --- a/examples/nanopb/simple.proto +++ /dev/null @@ -1,10 +0,0 @@ -// A very simple protocol definition, consisting of only -// one message. - -message SimpleMessage { - required int32 lucky_number = 1; - required bytes name = 2; -} - - - diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index d4c96f893a..cc67645868 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -12,14 +12,12 @@ #define DEBUG_TCP_CLIENT -#define DBG_ENABLE -#define DBG_SECTION_NAME "TCP" +#define DBG_TAG "TCP" #ifdef DEBUG_TCP_CLIENT -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> #define BUFSZ 1024 @@ -131,7 +129,7 @@ static void tcpclient(void *arg) ret = send(sock, send_data, rt_strlen(send_data), 0); if (ret < 0) { - /* 接收失败,关闭这个连接 */ + /* 发送失败,关闭这个连接 */ LOG_I("send error, close the socket."); goto __exit; } diff --git a/examples/network/tcpsendpacket.c b/examples/network/tcpsendpacket.c index 75134c19a2..47bb562506 100644 --- a/examples/network/tcpsendpacket.c +++ b/examples/network/tcpsendpacket.c @@ -1,7 +1,7 @@ #include <rtthread.h> -#include <netdb.h> /* Ϊ�˽�������������Ҫ����netdb.hͷ�ļ� */ -#include <sys/socket.h> /* ʹ��BSD socket����Ҫ����socket.hͷ�ļ� */ +#include <netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */ +#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */ void tcp_senddata(const char *url, int port, int length) { @@ -10,45 +10,52 @@ void tcp_senddata(const char *url, int port, int length) struct sockaddr_in server_addr; rt_uint8_t *buffer_ptr; - /* ͨ��������ڲ���url���host��ַ��������������������������� */ + /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = gethostbyname(url); - /* ����һ��socket��������SOCKET_STREAM��TCP���� */ + /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { - /* ����socketʧ�� */ + /* 创建socket失败 */ rt_kprintf("Socket error\n"); return; } - /* �����ڴ� */ + /* 申请内存 */ buffer_ptr = rt_malloc(length); - /* ���췢������ */ + if(RT_NULL == buffer_ptr) + { + /* 申请内存失败 */ + rt_kprintf("No memory\n"); + return; + } + + /* 构造发送数据 */ for (index = 0; index < length; index ++) buffer_ptr[index] = index & 0xff; timeout = 100; - /* ���÷��ͳ�ʱʱ��100ms */ + /* 设置发送超时时间100ms */ setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); - /* ��ʼ��Ԥ���ӵķ���˵�ַ */ + /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *)host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); - /* ���ӵ������ */ + /* 连接到服务端 */ err = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); rt_kprintf("TCP thread connect error code: %d\n", err); while (1) { - /* �������ݵ�sock���� */ + /* 发送数据到sock连接 */ result = send(sock, buffer_ptr, length, MSG_DONTWAIT); - if (result < 0) //���ݷ��ʹ����� + if (result < 0) //数据发送错误处理 { rt_kprintf("TCP thread send error: %d\n", result); closesocket(sock); - /* �ر����ӣ����´������� */ + /* 关闭连接,重新创建连接 */ rt_thread_delay(10); if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) @@ -59,7 +66,7 @@ void tcp_senddata(const char *url, int port, int length) } else if (result == 0) { - /* ��ӡsend��������ֵΪ0�ľ�����Ϣ */ + /* 打印send函数返回值为0的警告信息 */ rt_kprintf("\n Send warning,send function return 0.\r\n"); } } @@ -67,7 +74,7 @@ void tcp_senddata(const char *url, int port, int length) #ifdef RT_USING_FINSH #include <finsh.h> -/* ���tcpclient������finsh shell�� */ +/* 输出tcpclient函数到finsh shell中 */ FINSH_FUNCTION_EXPORT(tcp_senddata, send a packet through tcp connection); #endif diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c index 264b6f7c83..8c7382dcbb 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -12,14 +12,12 @@ #define DEBUG_TCP_SERVER -#define DBG_ENABLE -#define DBG_SECTION_NAME "TCP" +#define DBG_TAG "TCP" #ifdef DEBUG_TCP_SERVER -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> #define BUFSZ (1024) @@ -231,8 +229,8 @@ static void tcpserver_test(int argc, char** argv) { if (started) { - LOG_I("The tcpclient has started!"); - LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); + LOG_I("The tcpserver has started!"); + LOG_I("Please stop tcpserver firstly, by: tcpserver --stop"); return; } diff --git a/examples/network/udpclient.c b/examples/network/udpclient.c index 40542b9fa1..f8c42f3a12 100644 --- a/examples/network/udpclient.c +++ b/examples/network/udpclient.c @@ -5,14 +5,12 @@ #define DEBUG_UDP_CLIENT -#define DBG_ENABLE -#define DBG_SECTION_NAME "UDP" +#define DBG_TAG "UDP" #ifdef DEBUG_UDP_CLIENT -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> static int started = 0; @@ -91,7 +89,7 @@ static void usage(void) rt_kprintf(" -h Specify host address\n"); rt_kprintf(" -p Specify the host port number\n"); rt_kprintf(" --cnt Specify the send data count\n"); - rt_kprintf(" --stop Stop tcpclient program\n"); + rt_kprintf(" --stop Stop udpclient program\n"); rt_kprintf(" --help Print help information\n"); } @@ -119,8 +117,8 @@ static void udpclient_test(int argc, char** argv) { if (started) { - LOG_I("The tcpclient has started!"); - LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); + LOG_I("The udpclient has started!"); + LOG_I("Please stop udpclient firstly, by: udpclient --stop"); return; } diff --git a/examples/network/udpserver.c b/examples/network/udpserver.c index c40a1972b4..a249475e04 100644 --- a/examples/network/udpserver.c +++ b/examples/network/udpserver.c @@ -12,14 +12,12 @@ #define DEBUG_UDP_SERVER -#define DBG_ENABLE -#define DBG_SECTION_NAME "UDP" +#define DBG_TAG "UDP" #ifdef DEBUG_UDP_SERVER -#define DBG_LEVEL DBG_LOG +#define DBG_LVL DBG_LOG #else -#define DBG_LEVEL DBG_INFO /* DBG_ERROR */ +#define DBG_LVL DBG_INFO /* DBG_ERROR */ #endif -#define DBG_COLOR #include <rtdbg.h> #define BUFSZ 1024 @@ -165,8 +163,8 @@ static void udpserver_test(int argc, char** argv) { if (started) { - LOG_I("The tcpclient has started!"); - LOG_I("Please stop tcpclient firstly, by: tcpclient --stop"); + LOG_I("The udpserver has started!"); + LOG_I("Please stop udpserver firstly, by: udpserver --stop"); return; } diff --git a/examples/pm/timer_app.c b/examples/pm/timer_app.c index 73faf52d0a..b7ff675b39 100644 --- a/examples/pm/timer_app.c +++ b/examples/pm/timer_app.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2018-08-07 Tanek first implementation + * 2019-05-06 Zero-Free adapt to the new power management interface */ #include <board.h> @@ -39,7 +40,7 @@ static int timer_app_init(void) rt_timer_start(timer1); /* keep in timer mode */ - rt_pm_request(PM_SLEEP_MODE_TIMER); + rt_pm_request(PM_SLEEP_MODE_DEEP); return 0; } @@ -48,7 +49,7 @@ static int timer_app_init(void) return -1; } } -INIT_EXPORT_APP(timer_app_init); +INIT_APP_EXPORT(timer_app_init); #endif /* RT_USING_PM */ diff --git a/examples/pm/wakeup_app.c b/examples/pm/wakeup_app.c new file mode 100644 index 0000000000..0d4c42a997 --- /dev/null +++ b/examples/pm/wakeup_app.c @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-05-09 Zero-Free first implementation + */ + +#include <board.h> +#include <rtthread.h> +#include <rtdevice.h> + +#ifdef RT_USING_PM + +#define WAKEUP_EVENT_BUTTON (1 << 0) +#define PIN_LED_R GET_PIN(E, 7) +#define WAKEUP_PIN GET_PIN(C, 13) +#define WAKEUP_APP_THREAD_STACK_SIZE 1024 + +static rt_event_t wakeup_event; + +static void wakeup_callback(void *args) +{ + rt_event_send(wakeup_event, WAKEUP_EVENT_BUTTON); +} + +static void wakeup_init(void) +{ + rt_pin_mode(WAKEUP_PIN, PIN_MODE_INPUT_PULLUP); + rt_pin_attach_irq(WAKEUP_PIN, PIN_IRQ_MODE_FALLING, wakeup_callback, RT_NULL); + rt_pin_irq_enable(WAKEUP_PIN, 1); +} + +static void wakeup_app_entry(void *parameter) +{ + wakeup_init(); + rt_pm_request(PM_SLEEP_MODE_DEEP); + + while (1) + { + if (rt_event_recv(wakeup_event, + WAKEUP_EVENT_BUTTON, + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, RT_NULL) == RT_EOK) + { + rt_pm_request(PM_SLEEP_MODE_NONE); + + rt_pin_mode(PIN_LED_R, PIN_MODE_OUTPUT); + rt_pin_write(PIN_LED_R, 0); + rt_thread_delay(rt_tick_from_millisecond(500)); + rt_pin_write(PIN_LED_R, 1); + + rt_pm_release(PM_SLEEP_MODE_NONE); + } + } +} + +static int wakeup_app(void) +{ + rt_thread_t tid; + + wakeup_event = rt_event_create("wakup", RT_IPC_FLAG_FIFO); + RT_ASSERT(wakeup_event != RT_NULL); + + tid = rt_thread_create("wakeup_app", wakeup_app_entry, RT_NULL, + WAKEUP_APP_THREAD_STACK_SIZE, RT_MAIN_THREAD_PRIORITY, 20); + RT_ASSERT(tid != RT_NULL); + + rt_thread_startup(tid); + + return 0; +} +INIT_APP_EXPORT(wakeup_app); + +#endif diff --git a/examples/test/hwtimer_test.c b/examples/test/hwtimer_test.c index 8f69c4ef87..428373dde0 100644 --- a/examples/test/hwtimer_test.c +++ b/examples/test/hwtimer_test.c @@ -8,7 +8,7 @@ static rt_err_t timer_timeout_cb(rt_device_t dev, rt_size_t size) { - rt_kprintf("HT %d\n", rt_tick_get()); + rt_kprintf("enter hardware timer isr\n"); return 0; } @@ -35,7 +35,7 @@ int hwtimer(void) return -1; } - rt_device_set_rx_indicate(dev, timer_timeout_cb); + /* ʱ����� */ /* ����ʱ������(Ĭ��1Mhz��֧�ֵ���С����Ƶ��) */ err = rt_device_control(dev, HWTIMER_CTRL_FREQ_SET, &freq); if (err != RT_EOK) @@ -69,12 +69,34 @@ int hwtimer(void) rt_device_read(dev, 0, &val, sizeof(val)); rt_kprintf("Read: Sec = %d, Usec = %d\n", val.sec, val.usec); + /* ��ʱִ�лص����� -- ����ģʽ */ + /* ���ó�ʱ�ص����� */ + rt_device_set_rx_indicate(dev, timer_timeout_cb); + + /* ����ģʽ */ + mode = HWTIMER_MODE_PERIOD; + err = rt_device_control(dev, HWTIMER_CTRL_MODE_SET, &mode); + + /* ���ö�ʱ����ʱֵ��������ʱ�� */ + val.sec = t; + val.usec = 0; + rt_kprintf("SetTime: Sec %d, Usec %d\n", val.sec, val.usec); + if (rt_device_write(dev, 0, &val, sizeof(val)) != sizeof(val)) + { + rt_kprintf("SetTime Fail\n"); + goto EXIT; + } + + /* �ȴ��ص�����ִ�� */ + rt_thread_delay((t + 1)*RT_TICK_PER_SECOND); + EXIT: err = rt_device_close(dev); rt_kprintf("Close %s\n", TIMER); return err; } - -FINSH_FUNCTION_EXPORT(hwtimer, "Test hardware timer"); +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(hwtimer, "Test hardware timer"); #endif +#endif /* RT_USING_HWTIMER */ diff --git a/examples/test/net_test.c b/examples/test/net_test.c index 53bd2836fa..553305f4d0 100644 --- a/examples/test/net_test.c +++ b/examples/test/net_test.c @@ -14,41 +14,51 @@ rt_thread_t udpecho_tid = RT_NULL; void udpecho_entry(void *parameter) { - struct netconn *conn; - struct netbuf *buf; - struct ip_addr *addr; - unsigned short port; + struct netconn *conn; + struct netbuf *buf; + struct ip_addr *addr; + unsigned short port; - conn = netconn_new(NETCONN_UDP); - netconn_bind(conn, IP_ADDR_ANY, 7); + conn = netconn_new(NETCONN_UDP); + if(conn == NULL) + { + rt_kprintf("no memory error\n"); + return; + } + netconn_bind(conn, IP_ADDR_ANY, 7); - while(1) - { + while(1) + { /* received data to buffer */ #if LWIP_VERSION_MINOR==3U - buf = netconn_recv(conn); + buf = netconn_recv(conn); #else - netconn_recv(conn, &buf); + netconn_recv(conn, &buf); #endif - - addr = netbuf_fromaddr(buf); - port = netbuf_fromport(buf); + if(buf == NULL) + { + break; + } + addr = netbuf_fromaddr(buf); + port = netbuf_fromport(buf); - /* send the data to buffer */ - netconn_connect(conn, addr, port); + /* send the data to buffer */ + netconn_connect(conn, addr, port); - /* reset address, and send to client */ + /* reset address, and send to client */ #if LWIP_VERSION_MINOR==3U - buf->addr = RT_NULL; + buf->addr = RT_NULL; #else - buf->addr = *IP_ADDR_ANY; + buf->addr = *IP_ADDR_ANY; #endif - netconn_send(conn, buf); + netconn_send(conn, buf); - /* release buffer */ - netbuf_delete(buf); - } + /* release buffer */ + netbuf_delete(buf); + } + + netconn_delete(conn); } /* * UDP socket echo server @@ -58,57 +68,57 @@ void udpecho_entry(void *parameter) rt_thread_t udpecho_socket_tid = RT_NULL; void udpecho_socket_entry(void *parameter) { - int sock; - int bytes_read; - char *recv_data; - rt_uint32_t addr_len; - struct sockaddr_in server_addr, client_addr; - - /* allocate the data buffer */ - recv_data = rt_malloc(UDP_SOCKET_BUFFER_SIZE); - if (recv_data == RT_NULL) - { - /* no memory yet */ - rt_kprintf("no memory\n"); - goto _exit; - } - /* create a UDP socket */ - if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) - { - rt_kprintf("create socket error\n"); - goto _exit; - } + int sock; + int bytes_read; + char *recv_data; + rt_uint32_t addr_len; + struct sockaddr_in server_addr, client_addr; - /* initialize server address */ - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(UDP_SOCKET_ECHO_PORT); - server_addr.sin_addr.s_addr = INADDR_ANY; - rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); + /* allocate the data buffer */ + recv_data = rt_malloc(UDP_SOCKET_BUFFER_SIZE); + if (recv_data == RT_NULL) + { + /* no memory yet */ + rt_kprintf("no memory\n"); + return; + } + /* create a UDP socket */ + if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) + { + rt_kprintf("create socket error\n"); + goto _exit; + } - /* bind socket to server address */ - if (bind(sock,(struct sockaddr *)&server_addr, - sizeof(struct sockaddr)) == -1) - { - /* bind failed */ - rt_kprintf("bind error\n"); - goto _exit; - } + /* initialize server address */ + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(UDP_SOCKET_ECHO_PORT); + server_addr.sin_addr.s_addr = INADDR_ANY; + rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); - addr_len = sizeof(struct sockaddr); - while (1) - { - /* try to receive from UDP socket */ - bytes_read = recvfrom(sock, recv_data, UDP_SOCKET_BUFFER_SIZE, 0, - (struct sockaddr *)&client_addr, &addr_len); + /* bind socket to server address */ + if (bind(sock,(struct sockaddr *)&server_addr, + sizeof(struct sockaddr)) == -1) + { + /* bind failed */ + rt_kprintf("bind error\n"); + goto _exit; + } - /* send back */ - sendto(sock, recv_data, bytes_read, 0, - (struct sockaddr *)&client_addr, addr_len); - } + addr_len = sizeof(struct sockaddr); + while (1) + { + /* try to receive from UDP socket */ + bytes_read = recvfrom(sock, recv_data, UDP_SOCKET_BUFFER_SIZE, 0, + (struct sockaddr *)&client_addr, &addr_len); + + /* send back */ + sendto(sock, recv_data, bytes_read, 0, + (struct sockaddr *)&client_addr, addr_len); + } _exit: - rt_free(recv_data); - return; + rt_free(recv_data); + return; } /* @@ -118,52 +128,62 @@ _exit: rt_thread_t tcpecho_tid = RT_NULL; void tcpecho_entry(void *parameter) { - struct netconn *conn, *newconn; - err_t err; + struct netconn *conn, *newconn; + err_t err; - /* Create a new connection identifier. */ - conn = netconn_new(NETCONN_TCP); + /* Create a new connection identifier. */ + conn = netconn_new(NETCONN_TCP); + if(conn == NULL) + { + rt_kprintf("no memory error\n"); + return; + } - /* Bind connection to well known port number 7. */ - netconn_bind(conn, NULL, TCP_ECHO_PORT); + /* Bind connection to well known port number 7. */ + netconn_bind(conn, NULL, TCP_ECHO_PORT); - /* Tell connection to go into listening mode. */ - netconn_listen(conn); + /* Tell connection to go into listening mode. */ + netconn_listen(conn); - while(1) - { - /* Grab new connection. */ + while(1) + { + /* Grab new connection. */ #if LWIP_VERSION_MINOR==3U - newconn = netconn_accept(conn); - if(newconn != NULL) + newconn = netconn_accept(conn); + if(newconn != NULL) #else - err = netconn_accept(conn, &newconn); - if(err == ERR_OK) + err = netconn_accept(conn, &newconn); + if(err == ERR_OK) #endif - /* Process the new connection. */ - { - struct netbuf *buf; - void *data; - u16_t len; + /* Process the new connection. */ + { + struct netbuf *buf; + void *data; + u16_t len; #if LWIP_VERSION_MINOR==3U - while((buf = netconn_recv(newconn)) != NULL) + while((buf = netconn_recv(newconn)) != NULL) #else - while((err = netconn_recv(newconn, &buf)) == ERR_OK) + while((err = netconn_recv(newconn, &buf)) == ERR_OK) #endif - { - do - { - netbuf_data(buf, &data, &len); - err = netconn_write(newconn, data, len, NETCONN_COPY); - if(err != ERR_OK){} - } - while(netbuf_next(buf) >= 0); - netbuf_delete(buf); - } - /* Close connection and discard connection identifier. */ - netconn_delete(newconn); - } - } + { + do + { + netbuf_data(buf, &data, &len); + err = netconn_write(newconn, data, len, NETCONN_COPY); + if(err != ERR_OK) + { + break; + } + }while(netbuf_next(buf) >= 0); + + netbuf_delete(buf); + } + /* Close connection and discard connection identifier. */ + netconn_delete(newconn); + } + } + + netconn_delete(conn); } /* @@ -174,84 +194,83 @@ void tcpecho_entry(void *parameter) rt_thread_t tcpecho_socket_tid = RT_NULL; void tcpecho_socket_entry(void *parameter) { - char *recv_data; - rt_uint32_t sin_size; - int sock = -1, connected, bytes_received; - struct sockaddr_in server_addr, client_addr; + char *recv_data; + rt_uint32_t sin_size; + int sock = -1, connected, bytes_received; + struct sockaddr_in server_addr, client_addr; - recv_data = rt_malloc(TCP_SOCKET_BUFFER_SIZE); - if (recv_data == RT_NULL) - { - rt_kprintf("no memory\n"); - goto _exit; - } + recv_data = rt_malloc(TCP_SOCKET_BUFFER_SIZE); + if (recv_data == RT_NULL) + { + rt_kprintf("no memory\n"); + return; + } - /* create a TCP socket */ - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) - { - rt_kprintf("create socket error\n"); - goto _exit; - } + /* create a TCP socket */ + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) + { + rt_kprintf("create socket error\n"); + goto _exit; + } - /* initialize server address */ - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(TCP_SOCKET_ECHO_PORT); - server_addr.sin_addr.s_addr = INADDR_ANY; - rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero)); + /* initialize server address */ + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(TCP_SOCKET_ECHO_PORT); + server_addr.sin_addr.s_addr = INADDR_ANY; + rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero)); - /* bind to server address */ - if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) - { - rt_kprintf("bind address failed\n"); - goto _exit; - } + /* bind to server address */ + if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) + { + rt_kprintf("bind address failed\n"); + goto _exit; + } - /* listen */ - if (listen(sock, 5) == -1) - { - rt_kprintf("listen error\n"); - goto _exit; - } + /* listen */ + if (listen(sock, 5) == -1) + { + rt_kprintf("listen error\n"); + goto _exit; + } - sin_size = sizeof(struct sockaddr_in); - while(1) - { - /* accept client connected */ - connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); - if (connected > 0) - { - int timeout; + sin_size = sizeof(struct sockaddr_in); + while(1) + { + /* accept client connected */ + connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size); + if (connected > 0) + { + int timeout; - /* set timeout option */ - timeout = 5000; /* 5second */ - setsockopt(connected, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + /* set timeout option */ + timeout = 5000; /* 5second */ + setsockopt(connected, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); - /* handle this client */ - while (1) - { - /* receive data from this connection */ - bytes_received = recv(connected,recv_data, TCP_SOCKET_BUFFER_SIZE, 0); - if (bytes_received <= 0) - { - rt_kprintf("close client connection, errno: %d\n", - rt_get_errno()); - /* connection closed. */ - lwip_close(connected); - break; - } + /* handle this client */ + while (1) + { + /* receive data from this connection */ + bytes_received = recv(connected,recv_data, TCP_SOCKET_BUFFER_SIZE, 0); + if (bytes_received <= 0) + { + rt_kprintf("close client connection, errno: %d\n", rt_get_errno()); + /* connection closed. */ + lwip_close(connected); + break; + } - /* send data to client */ - send(connected, recv_data, bytes_received, 0); - } - } - } + /* send data to client */ + send(connected, recv_data, bytes_received, 0); + } + } + } _exit: - /* close socket */ - if (sock != -1) lwip_close(sock); - rt_free(recv_data); + /* close socket */ + if (sock != -1) lwip_close(sock); + rt_free(recv_data); - return ; + return; } /* @@ -261,40 +280,56 @@ _exit: /* network test utilities entry */ void net_test(void) { - /* start UDP echo server */ - if (udpecho_tid == RT_NULL) - { - udpecho_tid = rt_thread_create("uecho", - udpecho_entry, RT_NULL, - 512, RT_THREAD_PRIORITY_MAX/2, 5); - if (udpecho_tid != RT_NULL) - rt_thread_startup(udpecho_tid); - } - if (udpecho_socket_tid == RT_NULL) - { - udpecho_socket_tid = rt_thread_create("uecho_s", - udpecho_socket_entry, RT_NULL, - 512, RT_THREAD_PRIORITY_MAX/2 + 1, 5); - if (udpecho_socket_tid != RT_NULL) - rt_thread_startup(udpecho_socket_tid); - } + /* start UDP echo server */ + if (udpecho_tid == RT_NULL) + { + udpecho_tid = rt_thread_create("uecho", + udpecho_entry, + RT_NULL, + 512, + RT_THREAD_PRIORITY_MAX/2, 5); + if (udpecho_tid != RT_NULL) + { + rt_thread_startup(udpecho_tid); + } + } - if (tcpecho_tid == RT_NULL) - { - tcpecho_tid = rt_thread_create("techo", - tcpecho_entry, RT_NULL, - 512, RT_THREAD_PRIORITY_MAX/2 + 2, 5); - if (tcpecho_tid != RT_NULL) - rt_thread_startup(tcpecho_tid); - } - if (tcpecho_socket_tid == RT_NULL) - { - tcpecho_socket_tid = rt_thread_create("techo_s", - tcpecho_socket_entry, RT_NULL, - 512, RT_THREAD_PRIORITY_MAX/2 + 3, 5); - if (tcpecho_socket_tid != RT_NULL) - rt_thread_startup(tcpecho_socket_tid); - } + if (udpecho_socket_tid == RT_NULL) + { + udpecho_socket_tid = rt_thread_create("uecho_s", + udpecho_socket_entry, + RT_NULL, + 512, + RT_THREAD_PRIORITY_MAX/2 + 1, 5); + if (udpecho_socket_tid != RT_NULL) + { + rt_thread_startup(udpecho_socket_tid); + } + } + + if (tcpecho_tid == RT_NULL) + { + tcpecho_tid = rt_thread_create("techo", + tcpecho_entry, + RT_NULL, + 512, + RT_THREAD_PRIORITY_MAX/2 + 2, 5); + if (tcpecho_tid != RT_NULL) + { + rt_thread_startup(tcpecho_tid); + } + } + + if (tcpecho_socket_tid == RT_NULL) + { + tcpecho_socket_tid = rt_thread_create("techo_s", + tcpecho_socket_entry, + RT_NULL, + 512, + RT_THREAD_PRIORITY_MAX/2 + 3, 5); + if (tcpecho_socket_tid != RT_NULL) + { + rt_thread_startup(tcpecho_socket_tid); + } } FINSH_FUNCTION_EXPORT(net_test, network test); - |
