diff options
| author | cliff-cmc <[email protected]> | 2021-02-26 11:09:27 +0800 |
|---|---|---|
| committer | Cliff Chen <[email protected]> | 2021-02-26 11:45:19 +0800 |
| commit | eea0c3d74a40f73235da45c6f77e8b411ac94cf2 (patch) | |
| tree | a6906f1aed7118ab53dbe18f2ee4727a754583af /examples | |
| parent | 1a9ecf27f31277750d92c921b62503f75275e122 (diff) | |
| parent | fae0fa8eea4c0e797940bf1eddc8ee6c1711734a (diff) | |
Add memtrace command for memheap.c
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/file/listdir.c | 73 | ||||
| -rw-r--r-- | examples/file/writespeed.c | 38 | ||||
| -rwxr-xr-x[-rw-r--r--] | examples/kernel/SConscript | 3 | ||||
| -rwxr-xr-x | examples/kernel/mutex_priority.c | 425 | ||||
| -rw-r--r-- | examples/libc/SConscript | 2 | ||||
| -rw-r--r-- | examples/network/tcpclient.c | 2 | ||||
| -rw-r--r-- | examples/network/tcpserver.c | 4 | ||||
| -rw-r--r-- | examples/network/udpclient.c | 6 | ||||
| -rw-r--r-- | examples/network/udpserver.c | 4 | ||||
| -rw-r--r-- | examples/test/net_test.c | 441 |
10 files changed, 730 insertions, 268 deletions
diff --git a/examples/file/listdir.c b/examples/file/listdir.c index 98419db873..d8ea7af82a 100644 --- a/examples/file/listdir.c +++ b/examples/file/listdir.c @@ -14,49 +14,50 @@ void list_dir(const char* path) { char * fullpath; - DIR *dir; + 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; - fullpath = rt_malloc(256); - if (fullpath == RT_NULL) - { - rt_kprintf("no memory\n"); - return; - } + fullpath = rt_malloc(256); + if (fullpath == RT_NULL) + { + closedir(dir); + rt_kprintf("no memory\n"); + return; + } - do - { - dirent = readdir(dir); - if (dirent == RT_NULL) break; - rt_memset(&s, 0, sizeof(struct stat)); + do + { + dirent = readdir(dir); + if (dirent == RT_NULL) break; + rt_memset(&s, 0, sizeof(struct stat)); - /* build full path for each file */ - rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); + /* build full path for each file */ + rt_sprintf(fullpath, "%s/%s", path, dirent->d_name); - 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); + 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); - } + closedir(dir); + } + else + { + rt_kprintf("open %s directory failed\n", path); + } - rt_free(fullpath); + rt_free(fullpath); } #ifdef RT_USING_FINSH diff --git a/examples/file/writespeed.c b/examples/file/writespeed.c index 806a9674bc..60dd900a68 100644 --- a/examples/file/writespeed.c +++ b/examples/file/writespeed.c @@ -32,31 +32,31 @@ void writespeed(const char* filename, int total_length, int block_size) 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); diff --git a/examples/kernel/SConscript b/examples/kernel/SConscript index 4cffe1c38c..24d28cdd43 100644..100755 --- a/examples/kernel/SConscript +++ b/examples/kernel/SConscript @@ -19,6 +19,7 @@ semaphore_priority.c semaphore_buffer_worker.c semaphore_producer_consumer.c mutex_simple.c +mutex_priority.c event_simple.c mbox_simple.c mbox_send_wait.c @@ -34,7 +35,7 @@ memp_simple.c tc_sample.c """) -group = DefineGroup('examples', src, +group = DefineGroup('Examples', src, depend = ['RT_USING_TC'], CPPPATH=[GetCurrentDir()]) diff --git a/examples/kernel/mutex_priority.c b/examples/kernel/mutex_priority.c new file mode 100755 index 0000000000..f647fc4039 --- /dev/null +++ b/examples/kernel/mutex_priority.c @@ -0,0 +1,425 @@ +/* + * 创建7个进程,tid20、tid21、tid22、tid23、tid24、tid25、tid26、tid27、tid28、tid29 + * 各任务优先级分别为20、21、22、23、24、25、26、27、28、29, + * 其中tid26最先获得锁,tid22不需要使用到互斥锁,其他所有进程均需要使用互斥锁, + * 通过各个进程的打印信息,观察各个进程获取到互斥锁后,优先级变化情况 + */ +#include <rtthread.h> +#include "tc_comm.h" + +static rt_thread_t tid20 = RT_NULL; +static rt_thread_t tid21 = RT_NULL; +static rt_thread_t tid22 = RT_NULL; +static rt_thread_t tid23 = RT_NULL; +static rt_thread_t tid24 = RT_NULL; +static rt_thread_t tid25 = RT_NULL; +static rt_thread_t tid26 = RT_NULL; +static rt_thread_t tid27 = RT_NULL; +static rt_thread_t tid28 = RT_NULL; +static rt_thread_t tid29 = RT_NULL; + +static rt_mutex_t mutex = RT_NULL; + +static void thread20_entry(void* parameter) +{ + rt_err_t result; + rt_tick_t tick; + + rt_kprintf("[%d] thread20 run!\n", rt_tick_get()); + rt_thread_delay(30); + rt_kprintf("[%d] thread20 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread20 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread20, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid20->init_priority, tid20->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread20 exit!\n", rt_tick_get()); +} + +static void thread21_entry(void* parameter) +{ + rt_err_t result; + rt_tick_t tick; + rt_kprintf("[%d] thread21 run!\n", rt_tick_get()); + rt_thread_delay(25); + rt_kprintf("[%d] thread21 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread21 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread21, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid21->init_priority, tid21->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread21 exit!\n", rt_tick_get()); + +} + +static void thread22_entry(void* parameter) +{ + rt_tick_t tick; + + rt_kprintf("[%d] thread22 run!\n", rt_tick_get()); + rt_thread_delay(20); + rt_kprintf("[%d] thread22 wake!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 100) + { + rt_kprintf("[%d] thread22 running..., init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid22->init_priority, tid22->current_priority); + rt_thread_delay(2); + } + rt_kprintf("[%d] thread22 exit!\n", rt_tick_get()); +} + +static void thread23_entry(void* parameter) +{ + rt_err_t result; + rt_tick_t tick; + rt_kprintf("[%d] thread23 run!\n", rt_tick_get()); + rt_thread_delay(15); + rt_kprintf("[%d] thread23 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread23 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread23, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid23->init_priority, tid23->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread23 exit!\n", rt_tick_get()); +} + +static void thread24_entry(void* parameter) +{ + rt_err_t result; + rt_tick_t tick; + + rt_kprintf("[%d] thread24 run!\n", rt_tick_get()); + rt_thread_delay(10); + rt_kprintf("[%d] thread24 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread24 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread24, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid24->init_priority, tid24->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread24 exit!\n", rt_tick_get()); +} + +static void thread25_entry(void* parameter) +{ + rt_err_t result; + rt_tick_t tick; + rt_kprintf("[%d] thread25 run!\n", rt_tick_get()); + rt_thread_delay(5); + rt_kprintf("[%d] thread25 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread25 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread25, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid25->init_priority, tid25->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread25 exit!\n", rt_tick_get()); +} + +static void thread26_entry(void* parameter) +{ + rt_tick_t tick; + rt_err_t result; + + rt_kprintf("[%d] thread26 run!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread26 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 50) + { + rt_kprintf("[%d] thread26, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid26->init_priority, tid26->current_priority); + rt_thread_delay(1); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread26 exit!\n", rt_tick_get()); +} + +static void thread27_entry(void* parameter) +{ + rt_tick_t tick; + rt_err_t result; + + rt_kprintf("[%d] thread27 run!\n", rt_tick_get()); + rt_thread_delay(35); + rt_kprintf("[%d] thread27 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread27 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread27, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid27->init_priority, tid27->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread27 exit!\n", rt_tick_get()); +} + +static void thread28_entry(void* parameter) +{ + rt_tick_t tick; + rt_err_t result; + + rt_kprintf("[%d] thread28 run!\n", rt_tick_get()); + rt_thread_delay(40); + rt_kprintf("[%d] thread28 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread28 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread28, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid28->init_priority, tid28->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread28 exit!\n", rt_tick_get()); +} + +static void thread29_entry(void* parameter) +{ + rt_tick_t tick; + rt_err_t result; + + rt_kprintf("[%d] thread29 run!\n", rt_tick_get()); + rt_thread_delay(45); + rt_kprintf("[%d] thread29 wake!\n", rt_tick_get()); + + result = rt_mutex_take(mutex, RT_WAITING_FOREVER); + if (result != RT_EOK) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + } + rt_kprintf("[%d] thread29 take!\n", rt_tick_get()); + + tick = rt_tick_get(); + while (rt_tick_get() - tick < 5) + { + rt_kprintf("[%d] thread29, init_priority=%d, current_priority=%d\n", + rt_tick_get(), tid29->init_priority, tid29->current_priority); + } + rt_mutex_release(mutex); + rt_kprintf("[%d] thread29 exit!\n", rt_tick_get()); +} + +static int mutex_simple_init() +{ + /* 创建互斥锁 */ + mutex = rt_mutex_create("mutex", RT_IPC_FLAG_FIFO); + if (mutex == RT_NULL) + { + tc_stat(TC_STAT_END | TC_STAT_FAILED); + return 0; + } + + tid29 = rt_thread_create("t29", + thread29_entry, RT_NULL, + THREAD_STACK_SIZE, 29, THREAD_TIMESLICE); + if (tid29 != RT_NULL) + rt_thread_startup(tid29); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid28 = rt_thread_create("t28", + thread28_entry, RT_NULL, + THREAD_STACK_SIZE, 28, THREAD_TIMESLICE); + if (tid28 != RT_NULL) + rt_thread_startup(tid28); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid27 = rt_thread_create("t27", + thread27_entry, RT_NULL, + THREAD_STACK_SIZE, 27, THREAD_TIMESLICE); + if (tid27 != RT_NULL) + rt_thread_startup(tid27); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid26 = rt_thread_create("t26", + thread26_entry, RT_NULL, + THREAD_STACK_SIZE, 26, THREAD_TIMESLICE); + if (tid26 != RT_NULL) + rt_thread_startup(tid26); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid25 = rt_thread_create("t25", + thread25_entry, RT_NULL, + THREAD_STACK_SIZE, 25, THREAD_TIMESLICE); + if (tid25 != RT_NULL) + rt_thread_startup(tid25); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid24 = rt_thread_create("t24", + thread24_entry, RT_NULL, + THREAD_STACK_SIZE, 24, THREAD_TIMESLICE); + if (tid24 != RT_NULL) + rt_thread_startup(tid24); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid23 = rt_thread_create("t23", + thread23_entry, RT_NULL, + THREAD_STACK_SIZE, 23, THREAD_TIMESLICE); + if (tid23 != RT_NULL) + rt_thread_startup(tid23); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid22 = rt_thread_create("t22", + thread22_entry, RT_NULL, + THREAD_STACK_SIZE, 22, THREAD_TIMESLICE); + if (tid22 != RT_NULL) + rt_thread_startup(tid22); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid21 = rt_thread_create("t21", + thread21_entry, RT_NULL, + THREAD_STACK_SIZE, 21, THREAD_TIMESLICE); + if (tid21 != RT_NULL) + rt_thread_startup(tid21); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + tid20 = rt_thread_create("t20", + thread20_entry, RT_NULL, + THREAD_STACK_SIZE, 20, THREAD_TIMESLICE); + if (tid20 != RT_NULL) + rt_thread_startup(tid20); + else + tc_stat(TC_STAT_END | TC_STAT_FAILED); + + return 0; +} + +#ifdef RT_USING_TC +static void _tc_cleanup() +{ + /* 调度器上锁,上锁后,将不再切换到其他线程,仅响应中断 */ + rt_enter_critical(); + + /* 删除线程 */ + if (tid20 != RT_NULL && tid20->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid20); + if (tid21 != RT_NULL && tid21->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid21); + if (tid22 != RT_NULL && tid22->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid22); + if (tid23 != RT_NULL && tid23->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid23); + if (tid24 != RT_NULL && tid24->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid24); + if (tid25 != RT_NULL && tid25->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid25); + if (tid26 != RT_NULL && tid26->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid26); + if (tid27 != RT_NULL && tid27->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid27); + if (tid28 != RT_NULL && tid28->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid28); + if (tid29 != RT_NULL && tid29->stat != RT_THREAD_CLOSE) + rt_thread_delete(tid29); + + if (mutex != RT_NULL) + { + rt_mutex_delete(mutex); + } + rt_kprintf("test_done!\n"); + + /* 调度器解锁 */ + rt_exit_critical(); + + /* 设置TestCase状态 */ + tc_done(TC_STAT_PASSED); +} + +int _tc_mutex_priority() +{ + /* 设置TestCase清理回调函数 */ + tc_cleanup(_tc_cleanup); + mutex_simple_init(); + + /* 返回TestCase运行的最长时间 */ + return 100; +} +/* 输出函数命令到finsh shell中 */ +FINSH_FUNCTION_EXPORT(_tc_mutex_priority, a priority rollover example of mutex); +#else +/* 用户应用入口 */ +int rt_application_init() +{ + mutex_simple_init(); + + return 0; +} +#endif diff --git a/examples/libc/SConscript b/examples/libc/SConscript index 7801049c07..01a74356e7 100644 --- a/examples/libc/SConscript +++ b/examples/libc/SConscript @@ -1,6 +1,6 @@ from building import * src = Glob('*.c') -group = DefineGroup('libc_test', src, depend = ['RT_USING_NEWLIB', 'RT_USING_PTHREADS']) +group = DefineGroup('UTest', src, depend = ['RT_USING_NEWLIB', 'RT_USING_PTHREADS']) Return('group') diff --git a/examples/network/tcpclient.c b/examples/network/tcpclient.c index 2059d9f17f..cc67645868 100644 --- a/examples/network/tcpclient.c +++ b/examples/network/tcpclient.c @@ -129,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/tcpserver.c b/examples/network/tcpserver.c index c019ec132f..8c7382dcbb 100644 --- a/examples/network/tcpserver.c +++ b/examples/network/tcpserver.c @@ -229,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 cd870cd758..f8c42f3a12 100644 --- a/examples/network/udpclient.c +++ b/examples/network/udpclient.c @@ -89,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"); } @@ -117,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 ed63363189..a249475e04 100644 --- a/examples/network/udpserver.c +++ b/examples/network/udpserver.c @@ -163,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/test/net_test.c b/examples/test/net_test.c index 53bd2836fa..ec5d50197d 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),0, 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); - |
