summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorxph <[email protected]>2020-04-09 16:35:28 +0800
committerGitHub <[email protected]>2020-04-09 16:35:28 +0800
commitca7f01d79b3e0a997c771cec298e4ebd65b75809 (patch)
tree3d2c48aae7203b14038ba7b98b500b5343ed7f2a /examples
parentf763f8e7284b0492c5a65ad0abf94ee1963d7dcc (diff)
parentbaa3145bfe61b3330b6a160523cc5470fa5db3a7 (diff)
Merge pull request #1 from RT-Thread/master
sync rtt
Diffstat (limited to 'examples')
-rw-r--r--examples/nanopb/SConscript13
-rw-r--r--examples/nanopb/simple.c73
-rw-r--r--examples/nanopb/simple.options1
-rw-r--r--examples/nanopb/simple.pb.c18
-rw-r--r--examples/nanopb/simple.pb.h45
-rw-r--r--examples/nanopb/simple.proto10
-rw-r--r--examples/network/tcpclient.c209
-rw-r--r--examples/network/tcpserver.c252
-rw-r--r--examples/network/udpclient.c133
-rw-r--r--examples/network/udpserver.c169
-rw-r--r--examples/pm/timer_app.c55
-rw-r--r--examples/pm/wakeup_app.c77
-rw-r--r--examples/test/hwtimer_test.c30
-rw-r--r--examples/ulog/ulog_example.c93
14 files changed, 850 insertions, 328 deletions
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 20d14baebb..2059d9f17f 100644
--- a/examples/network/tcpclient.c
+++ b/examples/network/tcpclient.c
@@ -1,27 +1,58 @@
#include <rtthread.h>
+#include <string.h>
+#if !defined(SAL_USING_POSIX)
+#error "Please enable SAL_USING_POSIX!"
+#else
+#include <sys/time.h>
+#include <sys/select.h>
+#endif
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
#include "netdb.h"
+#define DEBUG_TCP_CLIENT
+
+#define DBG_TAG "TCP"
+#ifdef DEBUG_TCP_CLIENT
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_INFO /* DBG_ERROR */
+#endif
+#include <rtdbg.h>
+
#define BUFSZ 1024
+static int started = 0;
+static int is_running = 0;
+static char url[256];
+static int port = 8080;
static const char send_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */
-void tcpclient(const char *url, int port)
+
+static void tcpclient(void *arg)
{
int ret;
char *recv_data;
- struct hostent *host;
- int sock, bytes_received;
+ int bytes_received;
+ int sock = -1;
+ struct hostent *host = RT_NULL;
struct sockaddr_in server_addr;
+ struct timeval timeout;
+ fd_set readset;
+
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host = gethostbyname(url);
+ if (host == RT_NULL)
+ {
+ LOG_E("Get host by name failed!");
+ return;
+ }
/* 分配用于存放接收数据的缓冲 */
recv_data = rt_malloc(BUFSZ);
if (recv_data == RT_NULL)
{
- rt_kprintf("No memory\n");
+ LOG_E("No memory");
return;
}
@@ -29,11 +60,8 @@ void tcpclient(const char *url, int port)
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
/* 创建socket失败 */
- rt_kprintf("Socket error\n");
-
- /* 释放接收缓冲 */
- rt_free(recv_data);
- return;
+ LOG_E("Create socket error");
+ goto __exit;
}
/* 初始化预连接的服务端地址 */
@@ -46,78 +74,159 @@ void tcpclient(const char *url, int port)
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
/* 连接失败 */
- rt_kprintf("Connect fail!\n");
- closesocket(sock);
-
- /*释放接收缓冲 */
- rt_free(recv_data);
- return;
+ LOG_E("Connect fail!");
+ goto __exit;
}
- while (1)
+ started = 1;
+ is_running = 1;
+
+ timeout.tv_sec = 3;
+ timeout.tv_usec = 0;
+
+ while (is_running)
{
+ FD_ZERO(&readset);
+ FD_SET(sock, &readset);
+
+ /* Wait for read */
+ if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
+ continue;
+
/* 从sock连接中接收最大BUFSZ - 1字节数据 */
bytes_received = recv(sock, recv_data, BUFSZ - 1, 0);
if (bytes_received < 0)
{
/* 接收失败,关闭这个连接 */
- closesocket(sock);
- rt_kprintf("\nreceived error,close the socket.\r\n");
-
- /* 释放接收缓冲 */
- rt_free(recv_data);
- break;
+ LOG_E("Received error, close the socket.");
+ goto __exit;
}
else if (bytes_received == 0)
{
/* 打印recv函数返回值为0的警告信息 */
- rt_kprintf("\nReceived warning,recv function return 0.\r\n");
-
+ LOG_W("Received warning, recv function return 0.");
continue;
}
-
- /* 有接收到数据,把末端清零 */
- recv_data[bytes_received] = '\0';
-
- if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
- {
- /* 如果是首字母是q或Q,关闭这个连接 */
- closesocket(sock);
- rt_kprintf("\n got a 'q' or 'Q',close the socket.\r\n");
-
- /* 释放接收缓冲 */
- rt_free(recv_data);
- break;
- }
else
{
- /* 在控制终端显示收到的数据 */
- rt_kprintf("\nReceived data = %s ", recv_data);
+ /* 有接收到数据,把末端清零 */
+ recv_data[bytes_received] = '\0';
+
+ if (rt_strcmp(recv_data, "q") == 0 || rt_strcmp(recv_data, "Q") == 0)
+ {
+ /* 如果是首字母是q或Q,关闭这个连接 */
+ LOG_I("Got a 'q' or 'Q', close the socket.");
+ goto __exit;
+ }
+ else
+ {
+ /* 在控制终端显示收到的数据 */
+ LOG_D("Received data = %s", recv_data);
+ }
}
/* 发送数据到sock连接 */
- ret = send(sock, send_data, strlen(send_data), 0);
+ ret = send(sock, send_data, rt_strlen(send_data), 0);
if (ret < 0)
{
/* 接收失败,关闭这个连接 */
- closesocket(sock);
- rt_kprintf("\nsend error,close the socket.\r\n");
-
- rt_free(recv_data);
- break;
+ LOG_I("send error, close the socket.");
+ goto __exit;
}
else if (ret == 0)
{
/* 打印send函数返回值为0的警告信息 */
- rt_kprintf("\n Send warning,send function return 0.\r\n");
+ LOG_W("Send warning, send function return 0.");
+ }
+ }
+
+__exit:
+ if (recv_data)
+ {
+ rt_free(recv_data);
+ recv_data = RT_NULL;
+ }
+ if (sock >= 0)
+ {
+ closesocket(sock);
+ sock = -1;
+ }
+ started = 0;
+ is_running = 0;
+ return;
+}
+
+static void usage(void)
+{
+ rt_kprintf("Usage: tcpclient -h <host> -p <port>\n");
+ rt_kprintf(" tcpclient --stop\n");
+ rt_kprintf(" tcpclient --help\n");
+ rt_kprintf("\n");
+ rt_kprintf("Miscellaneous:\n");
+ rt_kprintf(" -h Specify host address\n");
+ rt_kprintf(" -p Specify the host port number\n");
+ rt_kprintf(" --stop Stop tcpclient program\n");
+ rt_kprintf(" --help Print help information\n");
+}
+
+static void tcpclient_test(int argc, char** argv)
+{
+ rt_thread_t tid;
+
+ if (argc == 1 || argc > 5)
+ {
+ LOG_I("Please check the command you entered!\n");
+ goto __usage;
+ }
+ else
+ {
+ if (rt_strcmp(argv[1], "--help") == 0)
+ {
+ goto __usage;
+ }
+ else if (rt_strcmp(argv[1], "--stop") == 0)
+ {
+ is_running = 0;
+ return;
+ }
+ else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0)
+ {
+ if (started)
+ {
+ LOG_I("The tcpclient has started!");
+ LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
+ return;
+ }
+
+ if (rt_strlen(argv[2]) > sizeof(url))
+ {
+ LOG_E("The input url is too long, max %d bytes!", sizeof(url));
+ return;
+ }
+ rt_memset(url, 0x0, sizeof(url));
+ rt_strncpy(url, argv[2], rt_strlen(argv[2]));
+ port = atoi(argv[4]);
+ }
+ else
+ {
+ goto __usage;
}
}
+ tid = rt_thread_create("tcp_client",
+ tcpclient, RT_NULL,
+ 2048, RT_THREAD_PRIORITY_MAX/3, 20);
+ if (tid != RT_NULL)
+ {
+ rt_thread_startup(tid);
+ }
return;
+
+__usage:
+ usage();
}
#ifdef RT_USING_FINSH
-#include <finsh.h>
-/* 输出tcpclient函数到finsh shell中 */
-FINSH_FUNCTION_EXPORT(tcpclient, startup tcp client);
+MSH_CMD_EXPORT_ALIAS(tcpclient_test, tcpclient,
+ Start a tcp client. Help: tcpclient --help);
#endif
diff --git a/examples/network/tcpserver.c b/examples/network/tcpserver.c
index 97292449a0..c019ec132f 100644
--- a/examples/network/tcpserver.c
+++ b/examples/network/tcpserver.c
@@ -1,151 +1,261 @@
#include <rtthread.h>
+#include <string.h>
+#if !defined(SAL_USING_POSIX)
+#error "Please enable SAL_USING_POSIX!"
+#else
+#include <sys/time.h>
+#include <sys/select.h>
+#endif
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
#include "netdb.h"
+#define DEBUG_TCP_SERVER
+
+#define DBG_TAG "TCP"
+#ifdef DEBUG_TCP_SERVER
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_INFO /* DBG_ERROR */
+#endif
+#include <rtdbg.h>
+
#define BUFSZ (1024)
+static int started = 0;
+static int is_running = 0;
+static int port = 5000;
static const char send_data[] = "This is TCP Server from RT-Thread."; /* 发送用到的数据 */
-static void tcpserv(void *parameter)
+
+static void tcpserv(void *arg)
{
+ int ret;
char *recv_data; /* 用于接收的指针,后面会做一次动态分配以请求可用内存 */
- socklen_t sin_size;
int sock, connected, bytes_received;
struct sockaddr_in server_addr, client_addr;
- rt_bool_t stop = RT_FALSE; /* 停止标志 */
- int ret;
+
+ struct timeval timeout;
+ fd_set readset, readset_c;
+ socklen_t sin_size = sizeof(struct sockaddr_in);
recv_data = rt_malloc(BUFSZ + 1); /* 分配接收用的数据缓冲 */
if (recv_data == RT_NULL)
{
- rt_kprintf("No memory\n");
+ LOG_E("No memory");
return;
}
/* 一个socket在使用前,需要预先创建出来,指定SOCK_STREAM为TCP的socket */
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+ if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
- /* 创建失败的错误处理 */
- rt_kprintf("Socket error\n");
-
- /* 释放已分配的接收缓冲 */
- rt_free(recv_data);
- return;
+ LOG_E("Create socket error");
+ goto __exit;
}
/* 初始化服务端地址 */
server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(5000); /* 服务端工作的端口 */
+ server_addr.sin_port = htons(port); /* 服务端工作的端口 */
server_addr.sin_addr.s_addr = INADDR_ANY;
- rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
+ rt_memset(&(server_addr.sin_zero), 0x0, sizeof(server_addr.sin_zero));
/* 绑定socket到服务端地址 */
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
- /* 绑定失败 */
- rt_kprintf("Unable to bind\n");
-
- /* 释放已分配的接收缓冲 */
- rt_free(recv_data);
- return;
+ LOG_E("Unable to bind");
+ goto __exit;
}
/* 在socket上进行监听 */
- if (listen(sock, 5) == -1)
+ if (listen(sock, 10) == -1)
{
- rt_kprintf("Listen error\n");
-
- /* release recv buffer */
- rt_free(recv_data);
- return;
+ LOG_E("Listen error");
+ goto __exit;
}
- rt_kprintf("\nTCPServer Waiting for client on port 5000...\n");
- while (stop != RT_TRUE)
+ LOG_I("\nTCPServer Waiting for client on port %d...\n", port);
+
+ started = 1;
+ is_running = 1;
+
+ timeout.tv_sec = 3;
+ timeout.tv_usec = 0;
+
+ while (is_running)
{
- sin_size = sizeof(struct sockaddr_in);
+ FD_ZERO(&readset);
+ FD_SET(sock, &readset);
+
+ LOG_I("Waiting for a new connection...");
+
+ /* Wait for read or write */
+ if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
+ continue;
/* 接受一个客户端连接socket的请求,这个函数调用是阻塞式的 */
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
/* 返回的是连接成功的socket */
if (connected < 0)
{
- rt_kprintf("accept connection failed! errno = %d\n", errno);
+ LOG_E("accept connection failed! errno = %d", errno);
continue;
}
/* 接受返回的client_addr指向了客户端的地址信息 */
- rt_kprintf("I got a connection from (%s , %d)\n",
+ LOG_I("I got a connection from (%s , %d)\n",
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
/* 客户端连接的处理 */
- while (1)
+ while (is_running)
{
- /* 发送数据到connected socket */
- ret = send(connected, send_data, strlen(send_data), 0);
- if (ret < 0)
- {
- /* 发送失败,关闭这个连接 */
- closesocket(connected);
- rt_kprintf("\nsend error,close the socket.\r\n");
- break;
- }
- else if (ret == 0)
- {
- /* 打印send函数返回值为0的警告信息 */
- rt_kprintf("\n Send warning,send function return 0.\r\n");
- }
+ FD_ZERO(&readset_c);
+ FD_SET(connected, &readset_c);
+
+ /* Wait for read or write */
+ if (select(connected + 1, &readset_c, RT_NULL, RT_NULL, &timeout) == 0)
+ continue;
/* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
bytes_received = recv(connected, recv_data, BUFSZ, 0);
if (bytes_received < 0)
{
- /* 接收失败,关闭这个connected socket */
+ LOG_E("Received error, close the connect.");
closesocket(connected);
+ connected = -1;
break;
}
else if (bytes_received == 0)
{
/* 打印recv函数返回值为0的警告信息 */
- rt_kprintf("\nReceived warning,recv function return 0.\r\n");
- closesocket(connected);
- break;
+ LOG_W("Received warning, recv function return 0.");
+ continue;
}
-
- /* 有接收到数据,把末端清零 */
- recv_data[bytes_received] = '\0';
- if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
+ else
{
- /* 如果是首字母是q或Q,关闭这个连接 */
- closesocket(connected);
- break;
+ /* 有接收到数据,把末端清零 */
+ recv_data[bytes_received] = '\0';
+ if (strcmp(recv_data, "q") == 0 || strcmp(recv_data, "Q") == 0)
+ {
+ /* 如果是首字母是q或Q,关闭这个连接 */
+ LOG_I("Got a 'q' or 'Q', close the connect.");
+ closesocket(connected);
+ connected = -1;
+ break;
+ }
+ else if (strcmp(recv_data, "exit") == 0)
+ {
+ /* 如果接收的是exit,则关闭整个服务端 */
+ closesocket(connected);
+ connected = -1;
+ goto __exit;
+ }
+ else
+ {
+ /* 在控制终端显示收到的数据 */
+ LOG_D("Received data = %s", recv_data);
+ }
}
- else if (strcmp(recv_data, "exit") == 0)
+
+ /* 发送数据到connected socket */
+ ret = send(connected, send_data, rt_strlen(send_data), 0);
+ if (ret < 0)
{
- /* 如果接收的是exit,则关闭整个服务端 */
+ LOG_E("send error, close the connect.");
closesocket(connected);
- stop = RT_TRUE;
+ connected = -1;
break;
}
- else
+ else if (ret == 0)
{
- /* 在控制终端显示收到的数据 */
- rt_kprintf("RECEIVED DATA = %s \n", recv_data);
+ /* 打印send函数返回值为0的警告信息 */
+ LOG_W("Send warning, send function return 0.");
}
}
}
- /* 退出服务 */
- closesocket(sock);
+__exit:
+ if (recv_data)
+ {
+ rt_free(recv_data);
+ recv_data = RT_NULL;
+ }
+ if (connected >= 0)
+ {
+ closesocket(connected);
+ connected = -1;
+ }
+ if (sock >= 0)
+ {
+ closesocket(sock);
+ sock = -1;
+ }
+ started = 0;
+ is_running = 0;
+ return;
+}
+
+static void usage(void)
+{
+ rt_kprintf("Usage: tcpserver -p <port>\n");
+ rt_kprintf(" tcpserver --stop\n");
+ rt_kprintf(" tcpserver --help\n");
+ rt_kprintf("\n");
+ rt_kprintf("Miscellaneous:\n");
+ rt_kprintf(" -p Specify the host port number\n");
+ rt_kprintf(" --stop Stop tcpserver program\n");
+ rt_kprintf(" --help Print help information\n");
+}
+
+static void tcpserver_test(int argc, char** argv)
+{
+ rt_thread_t tid;
+
+ if (argc == 1 || argc > 3)
+ {
+ LOG_I("Please check the command you entered!\n");
+ goto __usage;
+ }
+ else
+ {
+ if (rt_strcmp(argv[1], "--help") == 0)
+ {
+ goto __usage;
+ }
+ else if (rt_strcmp(argv[1], "--stop") == 0)
+ {
+ is_running = 0;
+ return;
+ }
+ else if (rt_strcmp(argv[1], "-p") == 0)
+ {
+ if (started)
+ {
+ LOG_I("The tcpclient has started!");
+ LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
+ return;
+ }
+
+ port = atoi(argv[2]);
+ }
+ else
+ {
+ goto __usage;
+ }
+ }
- /* 释放接收缓冲 */
- rt_free(recv_data);
+ tid = rt_thread_create("tcp_serv",
+ tcpserv, RT_NULL,
+ 2048, RT_THREAD_PRIORITY_MAX/3, 20);
+ if (tid != RT_NULL)
+ {
+ rt_thread_startup(tid);
+ }
+ return;
- return ;
+__usage:
+ usage();
}
#ifdef RT_USING_FINSH
-#include <finsh.h>
-/* 输出tcpserv函数到finsh shell中 */
-FINSH_FUNCTION_EXPORT(tcpserv, startup tcp server);
+MSH_CMD_EXPORT_ALIAS(tcpserver_test, tcpserver,
+ Start a tcp server. Help: tcpserver --help);
#endif
diff --git a/examples/network/udpclient.c b/examples/network/udpclient.c
index 5060c9a087..cd870cd758 100644
--- a/examples/network/udpclient.c
+++ b/examples/network/udpclient.c
@@ -1,11 +1,26 @@
#include <rtthread.h>
-//#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
-//#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
+
#include <sys/socket.h> /* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h"
+#define DEBUG_UDP_CLIENT
+
+#define DBG_TAG "UDP"
+#ifdef DEBUG_UDP_CLIENT
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_INFO /* DBG_ERROR */
+#endif
+#include <rtdbg.h>
+
+static int started = 0;
+static int is_running = 0;
+static char url[256];
+static int port = 8080;
+static int count = 10;
const char send_data[] = "This is UDP Client from RT-Thread.\n"; /* 发送用到的数据 */
-void udpclient(const char *url, int port, int count)
+
+static void udpclient(void *arg)
{
int sock;
struct hostent *host;
@@ -13,11 +28,16 @@ void udpclient(const char *url, int port, int count)
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host = (struct hostent *) gethostbyname(url);
+ if (host == RT_NULL)
+ {
+ LOG_E("Get host by name failed!");
+ return;
+ }
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
- if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
- rt_kprintf("Socket error\n");
+ LOG_E("Create socket error");
return;
}
@@ -27,26 +47,115 @@ void udpclient(const char *url, int port, int count)
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
+ started = 1;
+ is_running = 1;
+
/* 总计发送count次数据 */
- while (count)
+ while (count && is_running)
{
/* 发送数据到服务远端 */
- sendto(sock, send_data, strlen(send_data), 0,
+ sendto(sock, send_data, rt_strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
/* 线程休眠一段时间 */
- rt_thread_delay(50);
+ rt_thread_mdelay(1000);
/* 计数值减一 */
count --;
}
+ if (count == 0)
+ {
+ LOG_I("UDP client send data finished!");
+ }
+
/* 关闭这个socket */
- closesocket(sock);
+ if (sock >= 0)
+ {
+ closesocket(sock);
+ sock = -1;
+ }
+ started = 0;
+ is_running = 0;
+}
+
+static void usage(void)
+{
+ rt_kprintf("Usage: udpclient -h <host> -p <port> [--cnt] [count]\n");
+ rt_kprintf(" udpclient --stop\n");
+ rt_kprintf(" udpclient --help\n");
+ rt_kprintf("\n");
+ rt_kprintf("Miscellaneous:\n");
+ 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(" --help Print help information\n");
+}
+
+static void udpclient_test(int argc, char** argv)
+{
+ rt_thread_t tid;
+
+ if (argc == 1 || argc > 7)
+ {
+ LOG_I("Please check the command you entered!\n");
+ goto __usage;
+ }
+ else
+ {
+ if (rt_strcmp(argv[1], "--help") == 0)
+ {
+ goto __usage;
+ }
+ else if (rt_strcmp(argv[1], "--stop") == 0)
+ {
+ is_running = 0;
+ return;
+ }
+ else if (rt_strcmp(argv[1], "-h") == 0 && rt_strcmp(argv[3], "-p") == 0)
+ {
+ if (started)
+ {
+ LOG_I("The tcpclient has started!");
+ LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
+ return;
+ }
+
+ if (argc == 7 && rt_strcmp(argv[6], "--cnt") == 0)
+ {
+ count = atoi(argv[7]);
+ }
+
+ if (rt_strlen(argv[2]) > sizeof(url))
+ {
+ LOG_E("The input url is too long, max %d bytes!", sizeof(url));
+ return;
+ }
+ rt_memset(url, 0x0, sizeof(url));
+ rt_strncpy(url, argv[2], rt_strlen(argv[2]));
+ port = atoi(argv[4]);
+ }
+ else
+ {
+ goto __usage;
+ }
+ }
+
+ tid = rt_thread_create("udp_client",
+ udpclient, RT_NULL,
+ 2048, RT_THREAD_PRIORITY_MAX/3, 20);
+ if (tid != RT_NULL)
+ {
+ rt_thread_startup(tid);
+ }
+ return;
+
+__usage:
+ usage();
}
#ifdef RT_USING_FINSH
-#include <finsh.h>
-/* 输出udpclient函数到finsh shell中 */
-FINSH_FUNCTION_EXPORT(udpclient, startup udp client);
+MSH_CMD_EXPORT_ALIAS(udpclient_test, udpclient,
+ Start a udp client. Help: udpclient --help);
#endif
diff --git a/examples/network/udpserver.c b/examples/network/udpserver.c
index a684e9cd0c..ed63363189 100644
--- a/examples/network/udpserver.c
+++ b/examples/network/udpserver.c
@@ -1,10 +1,31 @@
#include <rtthread.h>
+#include <string.h>
+#if !defined(SAL_USING_POSIX)
+#error "Please enable SAL_USING_POSIX!"
+#else
+#include <sys/time.h>
+#include <sys/select.h>
+#endif
#include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文件 */
#include "netdb.h"
+#define DEBUG_UDP_SERVER
+
+#define DBG_TAG "UDP"
+#ifdef DEBUG_UDP_SERVER
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_INFO /* DBG_ERROR */
+#endif
+#include <rtdbg.h>
+
#define BUFSZ 1024
+static int started = 0;
+static int is_running = 0;
+static int port = 5000;
+
static void udpserv(void *paramemter)
{
int sock;
@@ -13,28 +34,27 @@ static void udpserv(void *paramemter)
socklen_t addr_len;
struct sockaddr_in server_addr, client_addr;
+ struct timeval timeout;
+ fd_set readset;
+
/* 分配接收用的数据缓冲 */
recv_data = rt_malloc(BUFSZ);
if (recv_data == RT_NULL)
{
- /* 分配内存失败,返回 */
- rt_kprintf("No memory\n");
+ LOG_E("No memory");
return;
}
/* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
- rt_kprintf("Socket error\n");
-
- /* 释放接收用的数据缓冲 */
- rt_free(recv_data);
- return;
+ LOG_E("Create socket error");
+ goto __exit;
}
/* 初始化服务端地址 */
server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(5000);
+ server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
@@ -42,47 +62,134 @@ static void udpserv(void *paramemter)
if (bind(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
- /* 绑定地址失败 */
- rt_kprintf("Bind error\n");
-
- /* 释放接收用的数据缓冲 */
- rt_free(recv_data);
- return;
+ LOG_E("Unable to bind");
+ goto __exit;
}
addr_len = sizeof(struct sockaddr);
- rt_kprintf("UDPServer Waiting for client on port 5000...\n");
+ LOG_I("UDPServer Waiting for client on port %d...", port);
- while (1)
+ started = 1;
+ is_running = 1;
+
+ timeout.tv_sec = 3;
+ timeout.tv_usec = 0;
+
+ while (is_running)
{
+ FD_ZERO(&readset);
+ FD_SET(sock, &readset);
+
+ /* Wait for read or write */
+ if (select(sock + 1, &readset, RT_NULL, RT_NULL, &timeout) == 0)
+ continue;
+
/* 从sock中收取最大BUFSZ - 1字节数据 */
bytes_read = recvfrom(sock, recv_data, BUFSZ - 1, 0,
(struct sockaddr *)&client_addr, &addr_len);
- /* UDP不同于TCP,它基本不会出现收取的数据失败的情况,除非设置了超时等待 */
+ if (bytes_read < 0)
+ {
+ LOG_E("Received error, close the connect.");
+ goto __exit;
+ }
+ else if (bytes_read == 0)
+ {
+ LOG_W("Received warning, recv function return 0.");
+ continue;
+ }
+ else
+ {
+ recv_data[bytes_read] = '\0'; /* 把末端清零 */
- recv_data[bytes_read] = '\0'; /* 把末端清零 */
+ /* 输出接收的数据 */
+ LOG_D("Received data = %s", recv_data);
- /* 输出接收的数据 */
- rt_kprintf("\n(%s , %d) said : ", inet_ntoa(client_addr.sin_addr),
- ntohs(client_addr.sin_port));
- rt_kprintf("%s", recv_data);
+ /* 如果接收数据是exit,退出 */
+ if (strcmp(recv_data, "exit") == 0)
+ {
+ goto __exit;
+ }
+ }
+ }
- /* 如果接收数据是exit,退出 */
- if (strcmp(recv_data, "exit") == 0)
+__exit:
+ if (recv_data)
+ {
+ rt_free(recv_data);
+ recv_data = RT_NULL;
+ }
+ if (sock >= 0)
+ {
+ closesocket(sock);
+ sock = -1;
+ }
+ started = 0;
+ is_running = 0;
+}
+
+static void usage(void)
+{
+ rt_kprintf("Usage: udpserver -p <port>\n");
+ rt_kprintf(" udpserver --stop\n");
+ rt_kprintf(" udpserver --help\n");
+ rt_kprintf("\n");
+ rt_kprintf("Miscellaneous:\n");
+ rt_kprintf(" -p Specify the host port number\n");
+ rt_kprintf(" --stop Stop udpserver program\n");
+ rt_kprintf(" --help Print help information\n");
+}
+
+static void udpserver_test(int argc, char** argv)
+{
+ rt_thread_t tid;
+
+ if (argc == 1 || argc > 3)
+ {
+ LOG_I("Please check the command you entered!\n");
+ goto __usage;
+ }
+ else
+ {
+ if (rt_strcmp(argv[1], "--help") == 0)
+ {
+ goto __usage;
+ }
+ else if (rt_strcmp(argv[1], "--stop") == 0)
+ {
+ is_running = 0;
+ return;
+ }
+ else if (rt_strcmp(argv[1], "-p") == 0)
{
- closesocket(sock);
+ if (started)
+ {
+ LOG_I("The tcpclient has started!");
+ LOG_I("Please stop tcpclient firstly, by: tcpclient --stop");
+ return;
+ }
- /* 释放接收用的数据缓冲 */
- rt_free(recv_data);
- break;
+ port = atoi(argv[2]);
+ }
+ else
+ {
+ goto __usage;
}
}
+ tid = rt_thread_create("udp_serv",
+ udpserv, RT_NULL,
+ 2048, RT_THREAD_PRIORITY_MAX/3, 20);
+ if (tid != RT_NULL)
+ {
+ rt_thread_startup(tid);
+ }
return;
+
+__usage:
+ usage();
}
#ifdef RT_USING_FINSH
-#include <finsh.h>
-/* 输出udpserv函数到finsh shell中 */
-FINSH_FUNCTION_EXPORT(udpserv, startup udp server);
+MSH_CMD_EXPORT_ALIAS(udpserver_test, udpserver,
+ Start a udp server. Help: udpserver --help);
#endif
diff --git a/examples/pm/timer_app.c b/examples/pm/timer_app.c
new file mode 100644
index 0000000000..b7ff675b39
--- /dev/null
+++ b/examples/pm/timer_app.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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>
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#ifndef RT_USING_TIMER_SOFT
+ #error "Please enable soft timer feature!"
+#endif
+
+#define TIMER_APP_DEFAULT_TICK (RT_TICK_PER_SECOND * 2)
+
+#ifdef RT_USING_PM
+
+static rt_timer_t timer1;
+
+static void _timeout_entry(void *parameter)
+{
+ rt_kprintf("current tick: %ld\n", rt_tick_get());
+}
+
+static int timer_app_init(void)
+{
+ timer1 = rt_timer_create("timer_app",
+ _timeout_entry,
+ RT_NULL,
+ TIMER_APP_DEFAULT_TICK,
+ RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
+ if (timer1 != RT_NULL)
+ {
+ rt_timer_start(timer1);
+
+ /* keep in timer mode */
+ rt_pm_request(PM_SLEEP_MODE_DEEP);
+
+ return 0;
+ }
+ else
+ {
+ return -1;
+ }
+}
+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/ulog/ulog_example.c b/examples/ulog/ulog_example.c
new file mode 100644
index 0000000000..88572f5152
--- /dev/null
+++ b/examples/ulog/ulog_example.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-08-02 armink the first version
+ */
+
+#include <stdlib.h>
+#include <rtthread.h>
+
+#ifndef ULOG_USING_SYSLOG
+#define LOG_TAG "example"
+#define LOG_LVL LOG_LVL_DBG
+#include <ulog.h>
+#else
+#include <syslog.h>
+#endif /* ULOG_USING_SYSLOG */
+
+void ulog_example(void)
+{
+ int count = 0;
+
+#ifdef ULOG_USING_SYSLOG
+ openlog("example1", 0, 0);
+#endif
+
+ while (count++ < 50)
+ {
+#ifndef ULOG_USING_SYSLOG
+ /* output different level log by LOG_X API */
+ LOG_D("LOG_D(%d): RT-Thread is an open source IoT operating system from China.", count);
+ LOG_I("LOG_I(%d): RT-Thread is an open source IoT operating system from China.", count);
+ LOG_W("LOG_W(%d): RT-Thread is an open source IoT operating system from China.", count);
+ LOG_E("LOG_E(%d): RT-Thread is an open source IoT operating system from China.", count);
+ ulog_d("test", "ulog_d(%d): RT-Thread is an open source IoT operating system from China.", count);
+ ulog_i("test", "ulog_i(%d): RT-Thread is an open source IoT operating system from China.", count);
+ ulog_w("test", "ulog_w(%d): RT-Thread is an open source IoT operating system from China.", count);
+ ulog_e("test", "ulog_e(%d): RT-Thread is an open source IoT operating system from China.", count);
+
+#ifdef ULOG_USING_FILTER
+ if (count == 20)
+ {
+ /* Set the global filer level is INFO. All of DEBUG log will stop output */
+ ulog_global_filter_lvl_set(LOG_LVL_INFO);
+ /* Set the test tag's level filter's level is ERROR. The DEBUG, INFO, WARNING log will stop output. */
+ ulog_tag_lvl_filter_set("test", LOG_LVL_ERROR);
+ }
+ else if (count == 30)
+ {
+ /* Set the example tag's level filter's level is LOG_FILTER_LVL_SILENT, the log enter silent mode. */
+ ulog_tag_lvl_filter_set("example", LOG_FILTER_LVL_SILENT);
+ /* Set the test tag's level filter's level is WARNING. The DEBUG, INFO log will stop output. */
+ ulog_tag_lvl_filter_set("test", LOG_LVL_WARNING);
+ }
+ else if (count == 40)
+ {
+ /* Set the test tag's level filter's level is LOG_FILTER_LVL_ALL. All level log will resume output. */
+ ulog_tag_lvl_filter_set("test", LOG_FILTER_LVL_ALL);
+ /* Set the global filer level is LOG_FILTER_LVL_ALL. All level log will resume output */
+ ulog_global_filter_lvl_set(LOG_FILTER_LVL_ALL);
+ }
+#endif /* ULOG_USING_FILTER */
+
+#else
+ /* output different priority log by syslog API */
+ syslog(LOG_INFO, "syslog(%d) LOG_INFO: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_DEBUG, "syslog(%d) LOG_DEBUG: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_WARNING, "syslog(%d) LOG_WARNING: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_ERR, "syslog(%d) LOG_ERR: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_INFO | LOG_MAIL, "syslog(%d) LOG_INFO | LOG_MAIL: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_DEBUG | LOG_DAEMON, "syslog(%d) LOG_DEBUG | LOG_DAEMON: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_WARNING | LOG_AUTH, "syslog(%d) LOG_WARNING | LOG_AUTH: RT-Thread is an open source IoT operating system from China.", count);
+ syslog(LOG_ERR | LOG_SYSLOG, "syslog(%d) LOG_ERR | LOG_SYSLOG: RT-Thread is an open source IoT operating system from China.", count);
+
+ if (count == 20)
+ {
+ /* Set log priority mask. Only output ERR and WARNING log. */
+ setlogmask(LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING));
+ }
+ else if (count == 40)
+ {
+ /* Set log priority mask. The log which level is less than ERROR will stop output. */
+ setlogmask(LOG_UPTO(LOG_ERR));
+ }
+#endif /* ULOG_USING_SYSLOG */
+
+ rt_thread_delay(rt_tick_from_millisecond(rand() % 1000));
+ }
+}
+MSH_CMD_EXPORT(ulog_example, run ulog example)