summaryrefslogtreecommitdiff
path: root/components/utilities
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 /components/utilities
parentf763f8e7284b0492c5a65ad0abf94ee1963d7dcc (diff)
parentbaa3145bfe61b3330b6a160523cc5470fa5db3a7 (diff)
Merge pull request #1 from RT-Thread/master
sync rtt
Diffstat (limited to 'components/utilities')
-rw-r--r--components/utilities/Kconfig223
-rw-r--r--components/utilities/logtrace/SConscript17
-rw-r--r--components/utilities/logtrace/log_file.c151
-rw-r--r--components/utilities/logtrace/log_trace.c445
-rw-r--r--components/utilities/logtrace/log_trace.h187
-rw-r--r--components/utilities/logtrace/memlog.c47
-rw-r--r--components/utilities/ulog/SConscript16
-rw-r--r--components/utilities/ulog/backend/console_be.c57
-rw-r--r--components/utilities/ulog/syslog/syslog.c265
-rw-r--r--components/utilities/ulog/syslog/syslog.h100
-rw-r--r--components/utilities/ulog/ulog.c1317
-rw-r--r--components/utilities/ulog/ulog.h100
-rw-r--r--components/utilities/ulog/ulog_def.h216
-rw-r--r--components/utilities/utest/SConscript8
-rw-r--r--components/utilities/utest/utest.c379
-rw-r--r--components/utilities/utest/utest.h168
-rw-r--r--components/utilities/utest/utest_assert.h72
-rw-r--r--components/utilities/utest/utest_log.h34
-rw-r--r--components/utilities/ymodem/SConscript9
-rw-r--r--components/utilities/ymodem/ry_sy.c238
-rw-r--r--components/utilities/ymodem/ymodem.c447
-rw-r--r--components/utilities/ymodem/ymodem.h57
22 files changed, 3608 insertions, 945 deletions
diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig
index 09ab2cfe8e..8b733ecf5c 100644
--- a/components/utilities/Kconfig
+++ b/components/utilities/Kconfig
@@ -1,49 +1,208 @@
menu "Utilities"
-config RT_USING_LOGTRACE
- bool "Enable log trace"
+config RT_USING_RYM
+ bool "Enable Ymodem"
default n
-if RT_USING_LOGTRACE
- config LOG_TRACE_MAX_SESSION
- int "Maximal number of session"
- default 16
+ if RT_USING_RYM
+ config YMODEM_USING_CRC_TABLE
+ bool "Enable CRC Table in Ymodem"
+ default n
- choice
- prompt "The default level of log"
- default LOG_TRACE_USING_LEVEL_INFO
+ config YMODEM_USING_FILE_TRANSFER
+ bool "Enable file transfer feature"
+ select RT_USING_DFS
+ default n
+ endif
- config LOG_TRACE_USING_LEVEL_NOTRACE
- bool "No trace"
+config RT_USING_ULOG
+ bool "Enable ulog"
+ default n
- config LOG_TRACE_USING_LEVEL_ERROR
- bool "Only error log"
+ if RT_USING_ULOG
+ if !ULOG_USING_SYSLOG
+ choice
+ prompt "The static output log level."
+ default ULOG_OUTPUT_LVL_D
+ help
+ When the log level is less than this option and it will stop output.
+ These log will not compile into ROM when using LOG_X api.
+ NOTE: It's not available on syslog mode.
+ config ULOG_OUTPUT_LVL_A
+ bool "Assert"
+ config ULOG_OUTPUT_LVL_E
+ bool "Error"
+ config ULOG_OUTPUT_LVL_W
+ bool "Warning"
+ config ULOG_OUTPUT_LVL_I
+ bool "Information"
+ config ULOG_OUTPUT_LVL_D
+ bool "Debug"
+ endchoice
+ endif
- config LOG_TRACE_USING_LEVEL_WARNING
- bool "Warning log"
+ if ULOG_USING_SYSLOG
+ choice
+ prompt "The static output log level."
+ default ULOG_OUTPUT_LVL_DEBUG
+ help
+ When the log level is less than this option and it will stop output.
+ These log will not compile into ROM when using LOG_X api.
+ NOTE: It's not available on syslog mode.
+ config ULOG_OUTPUT_LVL_EMERG
+ bool "EMERG"
+ config ULOG_OUTPUT_LVL_ALERT
+ bool "ALERT"
+ config ULOG_OUTPUT_LVL_CRIT
+ bool "CRIT"
+ config ULOG_OUTPUT_LVL_ERROR
+ bool "ERR"
+ config ULOG_OUTPUT_LVL_WARNING
+ bool "WARNING"
+ config ULOG_OUTPUT_LVL_NOTICE
+ bool "NOTICE"
+ config ULOG_OUTPUT_LVL_INFO
+ bool "INFO"
+ config ULOG_OUTPUT_LVL_DEBUG
+ bool "DEBUG"
+ endchoice
+ endif
- config LOG_TRACE_USING_LEVEL_INFO
- bool "Information log"
+ config ULOG_OUTPUT_LVL
+ int
+ default 0 if ULOG_OUTPUT_LVL_A
+ default 0 if ULOG_OUTPUT_LVL_EMERG
+ default 1 if ULOG_OUTPUT_LVL_ALERT
+ default 2 if ULOG_OUTPUT_LVL_CRIT
+ default 3 if ULOG_OUTPUT_LVL_E
+ default 3 if ULOG_OUTPUT_LVL_ERROR
+ default 4 if ULOG_OUTPUT_LVL_W
+ default 4 if ULOG_OUTPUT_LVL_WARNING
+ default 5 if ULOG_OUTPUT_LVL_NOTICE
+ default 6 if ULOG_OUTPUT_LVL_I
+ default 6 if ULOG_OUTPUT_LVL_INFO
+ default 7 if ULOG_OUTPUT_LVL_D
+ default 7 if ULOG_OUTPUT_LVL_DEBUG
+ default 7
- config LOG_TRACE_USING_LEVEL_VERBOSE
- bool "Verbose log"
+ config ULOG_USING_ISR_LOG
+ bool "Enable ISR log."
+ default n
+ help
+ The log output API can using in ISR (Interrupt Service Routines) also.
- config LOG_TRACE_USING_LEVEL_DEBUG
- bool "All debug log"
- endchoice
+ config ULOG_ASSERT_ENABLE
+ bool "Enable assert check."
+ default y
- config LOG_TRACE_USING_MEMLOG
- bool "Enable memory log for logtrace"
- default n
- help
- Enable memory log for logtrace, then the logs in log_trace
- will be printed out in idle thread hook function.
+ config ULOG_LINE_BUF_SIZE
+ int "The log's max width."
+ default 128
+ help
+ The buffer size for every line log.
- Please make sure the idle hook is not used.
-endif
+ config ULOG_USING_ASYNC_OUTPUT
+ bool "Enable async output mode."
+ default n
+ help
+ When enable asynchronous output mode. The log output is not immediately and the log will stored to buffer.
+ The another thread (Such as idle) will read the buffer and output the log. So it will using more RAM.
-config RT_USING_RYM
- bool "Enable Ymodem"
+ if ULOG_USING_ASYNC_OUTPUT
+ config ULOG_ASYNC_OUTPUT_BUF_SIZE
+ int "The async output buffer size."
+ default 2048
+
+ config ULOG_ASYNC_OUTPUT_BY_THREAD
+ bool "Enable async output by thread."
+ default y
+ help
+ This thread will output the asynchronous logs. The logs can output by other user thread when this option is disable.
+
+ if ULOG_ASYNC_OUTPUT_BY_THREAD
+
+ config ULOG_ASYNC_OUTPUT_THREAD_STACK
+ int "The async output thread stack size."
+ default 1024
+
+ config ULOG_ASYNC_OUTPUT_THREAD_PRIORITY
+ int "The async output thread stack priority."
+ range 0 RT_THREAD_PRIORITY_MAX
+ default 30
+
+ endif
+ endif
+
+ menu "log format"
+ config ULOG_OUTPUT_FLOAT
+ bool "Enable float number support. It will using more thread stack."
+ select RT_USING_LIBC
+ default n
+ help
+ The default formater is using rt_vsnprint and it not supported float number.
+ When enable this option then it will enable libc. The formater will change to vsnprint on libc.
+
+ if !ULOG_USING_SYSLOG
+ config ULOG_USING_COLOR
+ bool "Enable color log."
+ default y
+ help
+ The log will has different color by level.
+ endif
+
+ config ULOG_OUTPUT_TIME
+ bool "Enable time information."
+ default y
+
+ config ULOG_TIME_USING_TIMESTAMP
+ bool "Enable timestamp format for time."
+ default n
+ depends on ULOG_OUTPUT_TIME
+
+ config ULOG_OUTPUT_LEVEL
+ bool "Enable level information."
+ default y
+
+ config ULOG_OUTPUT_TAG
+ bool "Enable tag information."
+ default y
+
+ config ULOG_OUTPUT_THREAD_NAME
+ bool "Enable thread information."
+ default n
+ endmenu
+
+ config ULOG_BACKEND_USING_CONSOLE
+ bool "Enable console backend."
+ default y
+ help
+ The low level output using rt_kprintf().
+
+ config ULOG_USING_FILTER
+ bool "Enable runtime log filter."
+ default n
+ help
+ It will enable the log filter.
+ Such as level filter, log tag filter, log kw filter and tag's level filter.
+
+ config ULOG_USING_SYSLOG
+ bool "Enable syslog format log and API."
+ select ULOG_OUTPUT_TIME
+ select ULOG_USING_FILTER
+ default n
+ endif
+
+config RT_USING_UTEST
+ bool "Enable utest (RT-Thread test framework)"
default n
+ if RT_USING_UTEST
+ config UTEST_THR_STACK_SIZE
+ int "The utest thread stack size"
+ default 4096
+ config UTEST_THR_PRIORITY
+ int "The utest thread priority"
+ default 20
+ endif
+
endmenu
diff --git a/components/utilities/logtrace/SConscript b/components/utilities/logtrace/SConscript
deleted file mode 100644
index afc60792b9..0000000000
--- a/components/utilities/logtrace/SConscript
+++ /dev/null
@@ -1,17 +0,0 @@
-from building import *
-
-cwd = GetCurrentDir()
-src = Split('''
-log_trace.c
-''')
-CPPPATH = [cwd]
-
-if GetDepend('LOG_TRACE_USING_MEMLOG'):
- src += ['memlog.c']
-
-if GetDepend('RT_USING_DFS'):
- src += ['log_file.c']
-
-group = DefineGroup('Utilities', src, depend = ['RT_USING_LOGTRACE'], CPPPATH = CPPPATH)
-
-Return('group')
diff --git a/components/utilities/logtrace/log_file.c b/components/utilities/logtrace/log_file.c
deleted file mode 100644
index 766e55c3a6..0000000000
--- a/components/utilities/logtrace/log_file.c
+++ /dev/null
@@ -1,151 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * Bernard the first version
- * 2013-06-26 Grissiom refactor
- */
-#include <rtthread.h>
-#include <log_trace.h>
-
-#ifdef RT_USING_DFS
-
-#include <dfs_posix.h>
-
-struct file_device
-{
- struct rt_device parent;
-
- int fd;
- char *filename;
-};
-
-/* file device for log trace */
-static struct file_device _file_device;
-
-/* common device interface */
-static rt_err_t fdevice_open(rt_device_t dev, rt_uint16_t oflag)
-{
- int fd;
- struct file_device *fdev = (struct file_device *)dev;
-
- if (fdev->fd >= 0)
- return -RT_EBUSY;
-
- /* test and open */
- fd = open(fdev->filename, O_RDONLY, 0);
- if (fd >= 0)
- {
- close(fd);
- fd = open(fdev->filename, O_WRONLY | O_APPEND, 0);
- }
- else
- {
- /* file not exists */
- fd = open(fdev->filename, O_WRONLY | O_CREAT, 0);
- }
- fdev->fd = fd;
-
- return RT_EOK;
-}
-
-static rt_err_t fdevice_close(rt_device_t dev)
-{
- rt_err_t result;
- struct file_device *fdev = (struct file_device *)dev;
-
- if (fdev->fd < 0)
- return -RT_EBUSY;
-
- result = close(fdev->fd);
- if (result == 0)
- {
- fdev->fd = -1;
- }
-
- return result;
-}
-
-static rt_size_t fdevice_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
-{
- struct file_device *fdev = (struct file_device *)dev;
-
- if (fdev->fd < 0)
- return 0;
-
- return write(fdev->fd, buffer, size);
-}
-
-static rt_err_t fdevice_control(rt_device_t dev, int cmd, void *arg)
-{
- struct file_device *fdev = (struct file_device *)dev;
-
- if (fdev->fd < 0)
- return 0;
-
- switch (cmd)
- {
- case LOG_TRACE_CTRL_FLUSH:
- if (fsync(fdev->fd) != 0)
- return RT_ERROR;
- break;
- default:
- break;
- }
- return RT_EOK;
-}
-
-#ifdef RT_USING_DEVICE_OPS
-const static struct rt_device_ops log_trace_ops =
-{
- RT_NULL,
- fdevice_open,
- fdevice_close,
- RT_NULL,
- fdevice_write,
- fdevice_control
-};
-#endif
-
-void log_trace_file_init(const char *filename)
-{
- rt_device_t device;
-
- device = rt_device_find("logfile");
- if (device == RT_NULL)
- {
- rt_memset(&_file_device, 0x00, sizeof(_file_device));
-
- _file_device.parent.type = RT_Device_Class_Char;
-
-#ifdef RT_USING_DEVICE_OPS
- _file_device.parent.ops = &log_trace_ops;
-#else
- _file_device.parent.init = RT_NULL;
- _file_device.parent.open = fdevice_open;
- _file_device.parent.close = fdevice_close;
- _file_device.parent.write = fdevice_write;
- _file_device.parent.control = fdevice_control;
-#endif
-
- rt_device_register(&_file_device.parent, "logfile", O_RDWR);
- }
-
- _file_device.filename = rt_strdup(filename);
- _file_device.fd = -1;
-}
-
-void log_trace_set_file(const char *filename)
-{
- log_trace_file_init(filename);
- log_trace_set_device("logfile");
-}
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_file, log_file, set output filename of log trace);
-#endif
-
-#endif /* RT_USING_DFS */
diff --git a/components/utilities/logtrace/log_trace.c b/components/utilities/logtrace/log_trace.c
deleted file mode 100644
index 2d63149320..0000000000
--- a/components/utilities/logtrace/log_trace.c
+++ /dev/null
@@ -1,445 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * Bernard the first version
- * 2013-06-26 Grissiom refactor
- */
-
-#include <rtthread.h>
-#include <rthw.h>
-#include <stdio.h>
-#include "log_trace.h"
-
-#ifdef RT_USING_FINSH
-#include <finsh.h>
-#else
-#define FINSH_FUNCTION_EXPORT(...)
-#define FINSH_FUNCTION_EXPORT_ALIAS(...)
-#endif
-
-/* log pseudo device */
-static struct rt_device _log_device;
-
-static rt_device_t _traceout_device = RT_NULL;
-
-/* define a default lg session. The name is empty. */
-static struct log_trace_session _def_session = {{"\0"}, LOG_TRACE_LEVEL_DEFAULT};
-static const struct log_trace_session *_the_sessions[LOG_TRACE_MAX_SESSION] = {&_def_session};
-/* there is a default session at least */
-static rt_uint16_t _the_sess_nr = 1;
-
-rt_inline int _idname_len(log_trace_idnum_t id)
-{
- /* little endian */
- if ((id & 0x000000FF) == 0)
- return 0;
- if ((id & 0x0000FF00) == 0)
- return 1;
- if ((id & 0x00FF0000) == 0)
- return 2;
- if ((id & 0xFF000000) == 0)
- return 3;
-#ifndef LOG_TRACE_USE_LONGNAME
- return 4;
-#else
- {
- rt_uint32_t id2 = id >> 32;
- if ((id2 & 0x000000FF) == 0)
- return 4;
- if ((id2 & 0x0000FF00) == 0)
- return 5;
- if ((id2 & 0x00FF0000) == 0)
- return 6;
- if ((id2 & 0xFF000000) == 0)
- return 7;
- return 8;
- }
-#endif
-}
-
-/* lookup the session according to name.
- *
- * @param len is the length of the name
- * @return the pointer to the named session. RT_NULL when there is no such a
- * session.
- */
-static struct log_trace_session* _lg_lookup_session(log_trace_idnum_t num)
-{
- static const struct log_trace_session *_cache = &_def_session;
- rt_uint16_t first, last;
-
- if (_cache->id.num == num)
- return (struct log_trace_session *)_cache;
-
- first = 0;
- last = _the_sess_nr;
- do {
- unsigned int i = (first + last)/2;
-
- RT_ASSERT(_the_sessions[i]);
-
- if (_the_sessions[i]->id.num == num)
- {
- /* there is no need to protect the _cache because write a pointer
- * is atomic. So we cannot get a invalid pointer. The worst thing
- * could happen is there is an interrupt in the read/modify/write
- * process and we wrote the old one to _cache. But it doesn't harm
- * a lot because it will be flushed in the next time. */
- _cache = _the_sessions[i];
- return (struct log_trace_session *)_the_sessions[i];
- }
- else if (_the_sessions[i]->id.num > num)
- {
- last = i;
- }
- else // _the_sessions[i]->id.num < num
- {
- first = i;
- }
- } while (first != last-1);
-
- return RT_NULL;
-}
-
-rt_err_t log_trace_register_session(const struct log_trace_session *session)
-{
- unsigned int lvl, i;
-
- if (_the_sess_nr == LOG_TRACE_MAX_SESSION)
- return -RT_EFULL;
-
- if (session == RT_NULL)
- return RT_EOK;
-
- lvl = rt_hw_interrupt_disable();
- /* inserting the sessions in ascending order.
- *
- * this might take relatively long time. But since the register should only
- * happen when initialize the whole system, this should not be a matter. */
- for (i = 0; i < _the_sess_nr; i++)
- {
- if (_the_sessions[i]->id.num > session->id.num)
- {
- rt_memmove(_the_sessions+i+1, _the_sessions+i,
- (_the_sess_nr-i)*sizeof(&_the_sessions[0]));
- _the_sessions[i] = session;
- break;
- }
- else if (_the_sessions[i]->id.num == session->id.num)
- {
- rt_kprintf("registering session 0x%p twice\n", session);
- rt_hw_interrupt_enable(lvl);
- return -RT_ERROR;
- }
- }
- if (i == _the_sess_nr)
- _the_sessions[i] = session;
- _the_sess_nr++;
- rt_hw_interrupt_enable(lvl);
-
- return RT_EOK;
-}
-
-struct log_trace_session* log_trace_session_find(const char *name)
-{
- union log_trace_id *idp;
-
- RT_ASSERT(name);
- idp = (union log_trace_id*)name;
- return _lg_lookup_session(idp->num);
-}
-
-void log_trace_set_level(rt_uint8_t level)
-{
- _def_session.lvl = level;
-}
-FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_level, log_level, set the filter level of log trace);
-
-void log_trace_session_set_level(struct log_trace_session *sess, rt_uint8_t level)
-{
- RT_ASSERT(sess);
- sess->lvl = level;
-}
-
-/* parse the level info in fmt
- *
- * @param flen the length of the format.
- * @param lvlp the pointer to level. It will store the level in the memory the
- * lvlp points to. The default value is LOG_TRACE_LEVEL_DEFAULT.
- * @return the number of char it scaned.
- */
-static rt_size_t _lg_parse_lvl(const char *fmt, rt_size_t flen, int *lvlp)
-{
- RT_ASSERT(fmt);
- RT_ASSERT(lvlp);
-
- /* setup default value */
- *lvlp = LOG_TRACE_LEVEL_DEFAULT;
-
- if (flen < 3)
- {
- return 0;
- }
-
- if (fmt[0] == '<' && fmt[2] == '>')
- {
- *lvlp = fmt[1] - '0';
- return 3;
- }
- return 0;
-}
-
-/* parse the header in fmt
- *
- * @param flen the length of the format.
- * @param sessp the pointer of pointer to the session. It will store the
- * session pointer in the memory the sessp points to. When failed to
- * find the session, it will be setted to the default session.
- * @return the number of char it scaned, i.e., the length of header.
- */
-static rt_size_t _lg_parse_session(
- const char *fmt, rt_size_t flen, struct log_trace_session **sessp)
-{
- unsigned int i;
- struct log_trace_session *tmpsess;
- union log_trace_id id;
-
- RT_ASSERT(fmt);
- RT_ASSERT(sessp);
-
- /* setup default value */
- *sessp = &_def_session;
-
- /* no name space left */
- if (flen < sizeof(id) + 2)
- return 0;
-
- if (fmt[0] != '[')
- return 0;
-
- id.num = 0;
- /* skip '[' and convert the string to id number. */
- for (i = 1; fmt[i] != ']'; i++)
- {
- if (i - 1 == sizeof(id))
- return 0;
- id.name[i-1] = fmt[i];
- }
- tmpsess = _lg_lookup_session(id.num);
- if (tmpsess != RT_NULL)
- {
- *sessp = tmpsess;
- /* only count the header length when we found the session. So
- * the wrong [name] will be printed out. */
- return i + 1;
- }
-
- return 0;
-}
-
-void __logtrace_vfmtout(const struct log_trace_session *session,
- const char *fmt,
- va_list argptr)
-{
- /* 1 for ']' */
- static char _trace_buf[1+LOG_TRACE_BUFSZ];
- char *ptr;
- rt_size_t length;
-
- RT_ASSERT(session);
- RT_ASSERT(fmt);
-
- /* it's default session */
- if (session->id.name[0] == '\0')
- {
- rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x]", rt_tick_get());
- if (_traceout_device != RT_NULL)
- {
- rt_device_write(_traceout_device, -1, _trace_buf, 10);
- }
-
- ptr = &_trace_buf[0];
- }
- else
- {
- rt_snprintf(_trace_buf, sizeof(_trace_buf), "[%08x][", rt_tick_get());
- if (_traceout_device != RT_NULL)
- {
- rt_device_write(_traceout_device, -1, _trace_buf, 11);
- rt_device_write(_traceout_device, -1,
- session->id.name, _idname_len(session->id.num));
- }
-
- _trace_buf[0] = ']';
- ptr = &_trace_buf[1];
- }
-
- length = rt_vsnprintf(ptr, LOG_TRACE_BUFSZ, fmt, argptr);
-
- if (length >= LOG_TRACE_BUFSZ)
- length = LOG_TRACE_BUFSZ - 1;
-
- if (_traceout_device != RT_NULL)
- {
- rt_device_write(_traceout_device, -1, _trace_buf, length + 1);
- }
-}
-
-void log_trace(const char *fmt, ...)
-{
- va_list args;
- int level;
- struct log_trace_session *session;
-
- RT_ASSERT(fmt);
-
- fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
- fmt += _lg_parse_session(fmt, strlen(fmt), &session);
-
- /* filter by level */
- if (level > session->lvl)
- return;
-
- va_start(args, fmt);
- __logtrace_vfmtout(session, fmt, args);
- va_end(args);
-}
-FINSH_FUNCTION_EXPORT(log_trace, log trace);
-
-void log_session(const struct log_trace_session *session, const char *fmt, ...)
-{
- va_list args;
- int level;
-
- RT_ASSERT(session);
- RT_ASSERT(fmt);
-
- fmt += _lg_parse_lvl(fmt, strlen(fmt), &level);
- if (level > session->lvl)
- return;
-
- va_start(args, fmt);
- __logtrace_vfmtout(session, fmt, args);
- va_end(args);
-}
-
-void log_trace_flush(void)
-{
- rt_device_control(_traceout_device, LOG_TRACE_CTRL_FLUSH, RT_NULL);
-}
-FINSH_FUNCTION_EXPORT_ALIAS(log_trace_flush, log_flush, flush log on the buffer);
-
-/* RT-Thread common device interface */
-static rt_size_t _log_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
-{
- char c;
- int level;
- rt_size_t head_len;
- const char *ptr = buffer;
- struct log_trace_session *session;
-
- head_len = _lg_parse_lvl(ptr, size, &level);
- head_len += _lg_parse_session(ptr+head_len, size-head_len, &session);
-
- /* filter by level */
- if (level > session->lvl)
- return size;
-
- if (_traceout_device != RT_NULL)
- {
- c = '[';
- rt_device_write(_traceout_device, -1, &c, 1);
- rt_device_write(_traceout_device, -1, session->id.name, _idname_len(session->id.num));
- c = ']';
- rt_device_write(_traceout_device, -1, &c, 1);
- rt_device_write(_traceout_device, -1, ((char*)buffer)+head_len, size - head_len);
- }
-
- return size;
-}
-
-static rt_err_t _log_control(rt_device_t dev, int cmd, void *arg)
-{
- if (_traceout_device == RT_NULL) return -RT_ERROR;
-
- return rt_device_control(_traceout_device, cmd, arg);
-}
-
-#ifdef RT_USING_DEVICE_OPS
-const static struct rt_device_ops log_device_ops =
-{
- RT_NULL,
- RT_NULL,
- RT_NULL,
- RT_NULL,
- _log_write,
- _log_control
-};
-#endif
-
-int log_trace_init(void)
-{
- rt_memset(&_log_device, 0x00, sizeof(_log_device));
-
- _log_device.type = RT_Device_Class_Char;
-#ifdef RT_USING_DEVICE_OPS
- _log_device.ops = &log_device_ops;
-#else
- _log_device.init = RT_NULL;
- _log_device.open = RT_NULL;
- _log_device.close = RT_NULL;
- _log_device.read = RT_NULL;
- _log_device.write = _log_write;
- _log_device.control = _log_control;
-#endif
-
- /* no indication and complete callback */
- _log_device.rx_indicate = RT_NULL;
- _log_device.tx_complete = RT_NULL;
-
- rt_device_register(&_log_device, "log", RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR);
-
- /* set console as default device */
- _traceout_device = rt_console_get_device();
-
- return 0;
-}
-INIT_DEVICE_EXPORT(log_trace_init);
-
-rt_device_t log_trace_get_device(void)
-{
- return _traceout_device;
-}
-
-rt_err_t log_trace_set_device(const char *device_name)
-{
- struct rt_device *output_device;
-
- /* find out output device */
- output_device = rt_device_find(device_name);
- if (output_device != RT_NULL)
- {
- rt_err_t result;
-
- /* open device */
- result = rt_device_open(output_device, RT_DEVICE_FLAG_STREAM | RT_DEVICE_OFLAG_RDWR);
- if (result != RT_EOK)
- {
- rt_kprintf("Open trace device failed.\n");
- return -RT_ERROR;
- }
- }
-
- /* set trace out device */
- if (_traceout_device != RT_NULL)
- rt_device_close(_traceout_device);
- _traceout_device = output_device;
-
- return RT_EOK;
-}
-FINSH_FUNCTION_EXPORT_ALIAS(log_trace_set_device, log_device, set device of log trace);
-
diff --git a/components/utilities/logtrace/log_trace.h b/components/utilities/logtrace/log_trace.h
deleted file mode 100644
index 0c8a122515..0000000000
--- a/components/utilities/logtrace/log_trace.h
+++ /dev/null
@@ -1,187 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * Bernard the first version
- * 2013-06-26 Grissiom refactor
- */
-
-#ifndef __LOG_TRACE_H__
-#define __LOG_TRACE_H__
-
-#include <rtthread.h>
-
-#define LOG_TRACE_LEVEL_MASK 0x0f
-#define LOG_TRACE_LEVEL_NOTRACE 0x00
-#define LOG_TRACE_LEVEL_ERROR 0x01
-#define LOG_TRACE_LEVEL_WARNING 0x03
-#define LOG_TRACE_LEVEL_INFO 0x05
-#define LOG_TRACE_LEVEL_VERBOSE 0x07
-#define LOG_TRACE_LEVEL_DEBUG 0x09
-#define LOG_TRACE_LEVEL_ALL 0x0f
-
-#if defined(LOG_TRACE_USING_LEVEL_NOTRACE)
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_NOTRACE
-#elif defined(LOG_TRACE_USING_LEVEL_ERROR)
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_ERROR
-#elif defined(LOG_TRACE_USING_LEVEL_WARNING)
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_WARNING
-#elif defined(LOG_TRACE_USING_LEVEL_INFO)
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO
-#elif defined(LOG_TRACE_USING_LEVEL_VERBOSE)
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_VERBOSE
-#elif defined(LOG_TRACE_USING_LEVEL_DEBUG)
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_DEBUG
-#else
-#define LOG_TRACE_LEVEL_DEFAULT LOG_TRACE_LEVEL_INFO
-#endif
-
-#define LOG_TRACE_ERROR "<1>"
-#define LOG_TRACE_WARNING "<3>"
-#define LOG_TRACE_INFO "<5>"
-#define LOG_TRACE_VERBOSE "<7>"
-#define LOG_TRACE_DEBUG "<9>"
-
-#define LOG_TRACE_OPT_NOTS 0x10 /* no timestamp */
-#define LOG_TRACE_OPT_LN 0x20 /* terminate the current line */
-
-#define LOG_TRACE_CTRL_FLUSH 0x10
-#define LOG_TRACE_CTRL_RESET 0x11
-#define LOG_TRACE_CTRL_DUMP 0x12
-
-//#define LOG_TRACE_USE_LONGNAME
-
-#ifndef LOG_TRACE_BUFSZ
-#define LOG_TRACE_BUFSZ RT_CONSOLEBUF_SIZE
-#endif
-
-/** maximum number of sessions that can be registered to the log_trace system
- */
-#ifndef LOG_TRACE_MAX_SESSION
-#define LOG_TRACE_MAX_SESSION 16
-#endif
-
-#ifdef LOG_TRACE_USE_LONGNAME
-typedef rt_uint64_t log_trace_idnum_t;
-#else
-typedef rt_uint32_t log_trace_idnum_t;
-#endif
-
-/* use a integer to represent a string to avoid strcmp. Even 4 chars
- * should be enough for most of the cases.
- * NOTE: take care when converting the name string to id number, you
- * can trapped in endianness.
- */
-union log_trace_id {
- char name[sizeof(log_trace_idnum_t)];
- log_trace_idnum_t num;
-};
-
-struct log_trace_session
-{
- union log_trace_id id;
- rt_uint8_t lvl;
-};
-
-/** initialize the log_trace system */
-int log_trace_init(void);
-
-/** register a session.
- *
- * @return RT_EOK on success. -RT_EFULL if there is no space for registration.
- */
-rt_err_t log_trace_register_session(const struct log_trace_session *session);
-
-/** find a session with name
- *
- * The name is not enclosed by parenthesis([]).
- *
- * @return RT_NULL if there is no such a session.
- */
-struct log_trace_session* log_trace_session_find(const char *name);
-
-/** set the log level of the default session. */
-void log_trace_set_level(rt_uint8_t level);
-
-/** set the log level of the session */
-void log_trace_session_set_level(
- struct log_trace_session *session, rt_uint8_t level);
-
-/** log according to the format
- *
- * the format of log_trace is: "<level>[name]log messages". It will output
- * "[systick][name]log messages" When there is no session named name, the
- * default session will be used.
- *
- * We have keep the level tag before the name tag because we don't print put
- * the level tag to the output and it's easier to implement if we place the
- * level tag in the very beginning.
- */
-void log_trace(const char *fmt, ...);
-
-/** log into session.
- *
- * the format of log_trace is: "<level>log messages". It will output
- * "[systick][name]log messages". The name is the name of the session. It is
- * faster than bare log_trace.
- */
-void log_session(const struct log_trace_session *session, const char *fmt, ...);
-
-extern void __logtrace_vfmtout(const struct log_trace_session *session,
- const char *fmt,
- va_list argptr);
-
-rt_inline void __logtrace_fmtout(const struct log_trace_session *session,
- const char *fmt, ...)
-{
- va_list args;
-
- va_start(args, fmt);
- __logtrace_vfmtout(session, fmt, args);
- va_end(args);
-}
-
-/**
- * log with numeric level
- *
- * The prototype of this function is:
- *
- * void log_session_lvl(struct log_trace_session *session,
- * int lvl,
- * const char *fmt, ...);
- *
- * If the @session is const and @level is greater than @session->lvl, the whole
- * function will be optimized out. This is suitable for performance critical
- * places where in most cases, the log is turned off by level.
- */
-#define log_session_lvl(session, level, fmt, ...) \
- do { \
- if ((level) > (session)->lvl) \
- { \
- break; \
- } \
- __logtrace_fmtout(session, fmt, ##__VA_ARGS__); \
- } while (0)
-
-/* here comes the global part. All sessions share the some output backend. */
-
-/** get the backend device */
-rt_device_t log_trace_get_device(void);
-
-/** set the backend device */
-rt_err_t log_trace_set_device(const char *device_name);
-
-void log_trace_flush(void);
-
-#ifdef RT_USING_DFS
-/** set the backend to file */
-void log_trace_set_file(const char *filename);
-
-void log_trace_file_init(const char *filename);
-#endif /* RT_USING_DFS */
-
-#endif
-
diff --git a/components/utilities/logtrace/memlog.c b/components/utilities/logtrace/memlog.c
deleted file mode 100644
index ea440c4958..0000000000
--- a/components/utilities/logtrace/memlog.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2006-2018, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2013-06-26 Grissiom the first version
- */
-
-#include <rtthread.h>
-#include <rtdevice.h>
-#include <log_trace.h>
-
-#define PIPE_SZ 2048
-#define PIPE_NAME "memlog"
-
-static rt_pipe_t *_log_pipe = NULL;
-static rt_uint8_t outbuf[1024];
-void memlog_flush(void)
-{
- rt_size_t readsz;
- rt_device_t console;
-
- console = rt_console_get_device();
- if (!console) return;
-
- readsz = rt_device_read((rt_device_t)&(_log_pipe->parent), 0, outbuf, sizeof(outbuf));
- if (readsz)
- rt_device_write(console, 0, outbuf, readsz);
-}
-
-int memlog_init(void)
-{
- _log_pipe = rt_pipe_create(PIPE_NAME, PIPE_SZ);
- if (_log_pipe == RT_NULL)
- {
- rt_kprintf("init pipe device failed.\n");
- return -1;
- }
-
- log_trace_set_device(PIPE_NAME);
- rt_thread_idle_sethook(memlog_flush);
-
- return 0;
-}
-INIT_APP_EXPORT(memlog_init);
diff --git a/components/utilities/ulog/SConscript b/components/utilities/ulog/SConscript
new file mode 100644
index 0000000000..03ea1b5528
--- /dev/null
+++ b/components/utilities/ulog/SConscript
@@ -0,0 +1,16 @@
+from building import *
+
+cwd = GetCurrentDir()
+src = Glob('*.c')
+path = [cwd]
+
+if GetDepend('ULOG_BACKEND_USING_CONSOLE'):
+ src += ['backend/console_be.c']
+
+if GetDepend('ULOG_USING_SYSLOG'):
+ path += [cwd + '/syslog']
+ src += Glob('syslog/*.c')
+
+group = DefineGroup('Utilities', src, depend = ['RT_USING_ULOG'], CPPPATH = path)
+
+Return('group')
diff --git a/components/utilities/ulog/backend/console_be.c b/components/utilities/ulog/backend/console_be.c
new file mode 100644
index 0000000000..95dd547426
--- /dev/null
+++ b/components/utilities/ulog/backend/console_be.c
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-09-04 armink the first version
+ */
+
+#include <rthw.h>
+#include <ulog.h>
+
+#ifdef ULOG_BACKEND_USING_CONSOLE
+
+#if defined(ULOG_ASYNC_OUTPUT_BY_THREAD) && ULOG_ASYNC_OUTPUT_THREAD_STACK < 384
+#error "The thread stack size must more than 384 when using async output by thread (ULOG_ASYNC_OUTPUT_BY_THREAD)"
+#endif
+
+static struct ulog_backend console;
+
+void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw,
+ const char *log, size_t len)
+{
+#ifdef RT_USING_DEVICE
+ rt_device_t dev = rt_console_get_device();
+
+ if (dev == RT_NULL)
+ {
+ rt_hw_console_output(log);
+ }
+ else
+ {
+ rt_uint16_t old_flag = dev->open_flag;
+
+ dev->open_flag |= RT_DEVICE_FLAG_STREAM;
+ rt_device_write(dev, 0, log, len);
+ dev->open_flag = old_flag;
+ }
+#else
+ rt_hw_console_output(log);
+#endif
+
+}
+
+int ulog_console_backend_init(void)
+{
+ ulog_init();
+ console.output = ulog_console_backend_output;
+
+ ulog_backend_register(&console, "console", RT_TRUE);
+
+ return 0;
+}
+INIT_PREV_EXPORT(ulog_console_backend_init);
+
+#endif /* ULOG_BACKEND_USING_CONSOLE */
diff --git a/components/utilities/ulog/syslog/syslog.c b/components/utilities/ulog/syslog/syslog.c
new file mode 100644
index 0000000000..7586148fbf
--- /dev/null
+++ b/components/utilities/ulog/syslog/syslog.c
@@ -0,0 +1,265 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-09-07 armink the first version
+ */
+
+#include <stdarg.h>
+#include <ulog.h>
+#include <rthw.h>
+#include "syslog.h"
+
+#ifdef ULOG_OUTPUT_FLOAT
+#include <stdio.h>
+#endif
+
+/*
+ * reference:
+ * http://pubs.opengroup.org/onlinepubs/7908799/xsh/syslog.h.html
+ * https://www.gnu.org/software/libc/manual/html_node/Submitting-Syslog-Messages.html
+ * http://man7.org/linux/man-pages/man3/syslog.3.html
+ */
+
+#ifdef ULOG_USING_SYSLOG
+
+#include <sys/time.h>
+
+#ifndef ULOG_SYSLOG_IDENT_MAX_LEN
+#define ULOG_SYSLOG_IDENT_MAX_LEN ULOG_FILTER_TAG_MAX_LEN
+#endif
+
+static char local_ident[ULOG_SYSLOG_IDENT_MAX_LEN + 1];
+static int local_facility = LOG_USER;
+static int local_option = LOG_USER;
+static rt_bool_t is_open = RT_FALSE;
+
+/**
+ * open connection to syslog
+ *
+ * @param ident is an arbitrary identification string which future syslog invocations will prefix to each message.
+ * @param option is not using on ulog.
+ * @param facility is the default facility code for this connection.
+ */
+void openlog(const char *ident, int option, int facility)
+{
+ rt_base_t level;
+
+ ulog_init();
+
+ level = rt_hw_interrupt_disable();
+
+ rt_memset(local_ident, 0, sizeof(local_ident));
+ if (ident)
+ {
+ rt_strncpy(local_ident, ident, ULOG_SYSLOG_IDENT_MAX_LEN);
+ }
+ else
+ {
+ rt_strncpy(local_ident, "rtt", ULOG_SYSLOG_IDENT_MAX_LEN);
+ }
+
+ local_option = option;
+
+ if (facility)
+ {
+ local_facility = facility;
+ }
+ else
+ {
+ /* default facility is LOG_USER */
+ local_facility = LOG_USER;
+ }
+ /* output all level log */
+ setlogmask(LOG_UPTO(LOG_DEBUG));
+
+ is_open = RT_TRUE;
+
+ rt_hw_interrupt_enable(level);
+
+}
+
+/**
+ * This is functionally identical to syslog.
+ *
+ * @param priority log priority, can be generated by the macro LOG_MAKEPRI
+ * @param format log format
+ * @param args log arguments
+ */
+void vsyslog(int priority, const char *format, va_list args)
+{
+ if (LOG_FAC(priority) == 0)
+ {
+ /* using local facility */
+ priority |= local_facility;
+ }
+
+ ulog_voutput(priority, local_ident, RT_TRUE, format, args);
+}
+
+/**
+ * generates a log message
+ *
+ * @param priority log priority, can be generated by the macro LOG_MAKEPRI
+ * @param format log format, like printf()
+ */
+void syslog(int priority, const char *format, ...)
+{
+ va_list args;
+
+ if (!is_open)
+ {
+ openlog(0, 0, 0);
+ }
+ /* args point to the first variable parameter */
+ va_start(args, format);
+
+ vsyslog(priority, format, args);
+
+ va_end(args);
+}
+
+/**
+ * close the syslog
+ */
+void closelog(void)
+{
+ ulog_deinit();
+
+ is_open = RT_FALSE;
+}
+
+/**
+ * set log priority mask
+ *
+ * @param mask The log priority mask which generate by macro LOG_MASK and LOG_UPTO.
+ *
+ * @return This function returns the previous log priority mask.
+ */
+int setlogmask(int mask)
+{
+ static int old_mask = 0;
+ int return_mask = old_mask;
+
+ ulog_tag_lvl_filter_set(local_ident, mask);
+
+ old_mask = mask;
+
+ return return_mask;
+}
+
+static const char *get_month_str(uint8_t month)
+{
+ switch(month)
+ {
+ case 1: return "Jan";
+ case 2: return "Feb";
+ case 3: return "Mar";
+ case 4: return "Apr";
+ case 5: return "May";
+ case 6: return "June";
+ case 7: return "July";
+ case 8: return "Aug";
+ case 9: return "Sept";
+ case 10: return "Oct";
+ case 11: return "Nov";
+ case 12: return "Dec";
+ default: return "Unknown";
+ }
+}
+
+RT_WEAK rt_size_t syslog_formater(char *log_buf, int level, const char *tag, rt_bool_t newline, const char *format, va_list args)
+{
+ extern size_t ulog_strcpy(size_t cur_len, char *dst, const char *src);
+
+ rt_size_t log_len = 0, newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
+ int fmt_result;
+
+ RT_ASSERT(log_buf);
+ RT_ASSERT(LOG_PRI(level) <= LOG_DEBUG);
+ RT_ASSERT(tag);
+ RT_ASSERT(format);
+
+ /* add time and priority (level) info */
+ {
+ time_t now = time(RT_NULL);
+ struct tm *tm, tm_tmp;
+
+ tm = gmtime_r(&now, &tm_tmp);
+
+#ifdef ULOG_OUTPUT_LEVEL
+ rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "<%d>%s%3d %02d:%02d:%02d", level,
+ get_month_str(tm->tm_mon + 1), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, rt_tick_get() % 1000);
+#else
+ rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%s%3d %02d:%02d:%02d",
+ get_month_str(tm->tm_mon + 1), tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, rt_tick_get() % 1000);
+#endif /* ULOG_OUTPUT_LEVEL */
+
+ log_len += rt_strlen(log_buf + log_len);
+ }
+
+#ifdef ULOG_OUTPUT_TAG
+ /* add identification (tag) info */
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+ log_len += ulog_strcpy(log_len, log_buf + log_len, tag);
+ }
+#endif /* ULOG_OUTPUT_TAG */
+
+#ifdef ULOG_OUTPUT_THREAD_NAME
+ /* add thread info */
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+ /* is not in interrupt context */
+ if (rt_interrupt_get_nest() == 0)
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, rt_thread_self()->name);
+ }
+ else
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, "ISR");
+ }
+ }
+#endif /* ULOG_OUTPUT_THREAD_NAME */
+
+ log_len += ulog_strcpy(log_len, log_buf + log_len, ": ");
+
+#ifdef ULOG_OUTPUT_FLOAT
+ fmt_result = vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
+#else
+ fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
+#endif /* ULOG_OUTPUT_FLOAT */
+
+ /* calculate log length */
+ if ((log_len + fmt_result <= ULOG_LINE_BUF_SIZE) && (fmt_result > -1))
+ {
+ log_len += fmt_result;
+ }
+ else
+ {
+ /* using max length */
+ log_len = ULOG_LINE_BUF_SIZE;
+ }
+
+ /* overflow check and reserve some space for newline sign */
+ if (log_len + newline_len > ULOG_LINE_BUF_SIZE)
+ {
+ /* using max length */
+ log_len = ULOG_LINE_BUF_SIZE;
+ /* reserve some space for newline sign */
+ log_len -= newline_len;
+ }
+
+ /* package newline sign */
+ if (newline)
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
+ }
+
+ return log_len;
+}
+
+#endif /* ULOG_USING_SYSLOG */
diff --git a/components/utilities/ulog/syslog/syslog.h b/components/utilities/ulog/syslog/syslog.h
new file mode 100644
index 0000000000..43eec7125f
--- /dev/null
+++ b/components/utilities/ulog/syslog/syslog.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-09-07 armink the first version
+ */
+
+#ifndef _SYSLOG_H_
+#define _SYSLOG_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * priorities/facilities are encoded into a single 32-bit quantity, where the
+ * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
+ * (0-big number). Both the priorities and the facilities map roughly
+ * one-to-one to strings in the syslogd(8) source code. This mapping is
+ * included in this file.
+ *
+ * priorities (these are ordered)
+ */
+#define LOG_EMERG 0 /* system is unusable */
+#define LOG_ALERT 1 /* action must be taken immediately */
+#define LOG_CRIT 2 /* critical conditions */
+#define LOG_ERR 3 /* error conditions */
+#define LOG_WARNING 4 /* warning conditions */
+#define LOG_NOTICE 5 /* normal but significant condition */
+#define LOG_INFO 6 /* informational */
+#define LOG_DEBUG 7 /* debug-level messages */
+
+#define LOG_PRIMASK 0x07
+
+#define LOG_PRI(p) ((p) & LOG_PRIMASK)
+#define LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
+
+/* facility codes */
+#define LOG_KERN (0<<3) /* kernel messages */
+#define LOG_USER (1<<3) /* random user-level messages */
+#define LOG_MAIL (2<<3) /* mail system */
+#define LOG_DAEMON (3<<3) /* system daemons */
+#define LOG_AUTH (4<<3) /* security/authorization messages */
+#define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */
+#define LOG_LPR (6<<3) /* line printer subsystem */
+#define LOG_NEWS (7<<3) /* network news subsystem */
+#define LOG_UUCP (8<<3) /* UUCP subsystem */
+#define LOG_CRON (9<<3) /* clock daemon */
+#define LOG_AUTHPRIV (10<<3) /* security/authorization messages (private) */
+
+/* other codes through 15 reserved for system use */
+#define LOG_LOCAL0 (16<<3) /* reserved for local use */
+#define LOG_LOCAL1 (17<<3) /* reserved for local use */
+#define LOG_LOCAL2 (18<<3) /* reserved for local use */
+#define LOG_LOCAL3 (19<<3) /* reserved for local use */
+#define LOG_LOCAL4 (20<<3) /* reserved for local use */
+#define LOG_LOCAL5 (21<<3) /* reserved for local use */
+#define LOG_LOCAL6 (22<<3) /* reserved for local use */
+#define LOG_LOCAL7 (23<<3) /* reserved for local use */
+
+#define LOG_NFACILITIES 24 /* current number of facilities */
+#define LOG_FACMASK 0x03f8 /* mask to extract facility part */
+/* facility of pri */
+#define LOG_FAC(p) (((p) & LOG_FACMASK) >> 3)
+
+/*
+ * arguments to setlogmask.
+ */
+#define LOG_MASK(pri) (1 << (pri)) /* mask for one priority */
+#define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1) /* all priorities through pri */
+
+/*
+ * Option flags for openlog.
+ *
+ * LOG_ODELAY no longer does anything.
+ * LOG_NDELAY is the inverse of what it used to be.
+ */
+#define LOG_PID 0x01 /* log the pid with each message */
+#define LOG_CONS 0x02 /* log on the console if errors in sending */
+#define LOG_ODELAY 0x04 /* delay open until first syslog() (default) */
+#define LOG_NDELAY 0x08 /* don't delay open */
+#define LOG_NOWAIT 0x10 /* don't wait for console forks: DEPRECATED */
+#define LOG_PERROR 0x20 /* log to stderr as well */
+
+#include <stdarg.h>
+
+void closelog(void);
+void openlog(const char *ident, int option, int facility);
+int setlogmask(int mask);
+void syslog(int priority, const char *format, ...);
+void vsyslog(int priority, const char *format, va_list args);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _SYSLOG_H_ */
diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c
new file mode 100644
index 0000000000..7610089298
--- /dev/null
+++ b/components/utilities/ulog/ulog.c
@@ -0,0 +1,1317 @@
+/*
+ * Copyright (c) 2006-2019, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-08-25 armink the first version
+ */
+
+#include <stdarg.h>
+#include "ulog.h"
+#include "rthw.h"
+
+#ifdef ULOG_USING_SYSLOG
+#include <syslog.h>
+#endif
+
+#ifdef ULOG_OUTPUT_FLOAT
+#include <stdio.h>
+#endif
+
+#ifdef ULOG_TIME_USING_TIMESTAMP
+#include <sys/time.h>
+#endif
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+#include <rtdevice.h>
+#endif
+
+#ifdef RT_USING_ULOG
+
+/* the number which is max stored line logs */
+#ifndef ULOG_ASYNC_OUTPUT_STORE_LINES
+#define ULOG_ASYNC_OUTPUT_STORE_LINES (ULOG_ASYNC_OUTPUT_BUF_SIZE * 3 / 2 / ULOG_LINE_BUF_SIZE)
+#endif
+
+#ifdef ULOG_USING_COLOR
+/**
+ * CSI(Control Sequence Introducer/Initiator) sign
+ * more information on https://en.wikipedia.org/wiki/ANSI_escape_code
+ */
+#define CSI_START "\033["
+#define CSI_END "\033[0m"
+/* output log front color */
+#define F_BLACK "30m"
+#define F_RED "31m"
+#define F_GREEN "32m"
+#define F_YELLOW "33m"
+#define F_BLUE "34m"
+#define F_MAGENTA "35m"
+#define F_CYAN "36m"
+#define F_WHITE "37m"
+
+/* output log default color definition */
+#ifndef ULOG_COLOR_DEBUG
+#define ULOG_COLOR_DEBUG NULL
+#endif
+#ifndef ULOG_COLOR_INFO
+#define ULOG_COLOR_INFO (F_GREEN)
+#endif
+#ifndef ULOG_COLOR_WARN
+#define ULOG_COLOR_WARN (F_YELLOW)
+#endif
+#ifndef ULOG_COLOR_ERROR
+#define ULOG_COLOR_ERROR (F_RED)
+#endif
+#ifndef ULOG_COLOR_ASSERT
+#define ULOG_COLOR_ASSERT (F_MAGENTA)
+#endif
+#endif /* ULOG_USING_COLOR */
+
+#if ULOG_LINE_BUF_SIZE < 80
+#error "the log line buffer size must more than 80"
+#endif
+
+struct rt_ulog
+{
+ rt_bool_t init_ok;
+ struct rt_mutex output_locker;
+ /* all backends */
+ rt_slist_t backend_list;
+ /* the thread log's line buffer */
+ char log_buf_th[ULOG_LINE_BUF_SIZE + 1];
+
+#ifdef ULOG_USING_ISR_LOG
+ /* the ISR log's line buffer */
+ rt_base_t output_locker_isr_lvl;
+ char log_buf_isr[ULOG_LINE_BUF_SIZE + 1];
+#endif /* ULOG_USING_ISR_LOG */
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+ rt_rbb_t async_rbb;
+ rt_thread_t async_th;
+ struct rt_semaphore async_notice;
+#endif
+
+#ifdef ULOG_USING_FILTER
+ struct
+ {
+ /* all tag's level filter */
+ rt_slist_t tag_lvl_list;
+ /* global filter level, tag and keyword */
+ rt_uint32_t level;
+ char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
+ char keyword[ULOG_FILTER_KW_MAX_LEN + 1];
+ } filter;
+#endif /* ULOG_USING_FILTER */
+};
+
+/* level output info */
+static const char * const level_output_info[] =
+{
+ "A/",
+ NULL,
+ NULL,
+ "E/",
+ "W/",
+ NULL,
+ "I/",
+ "D/",
+};
+
+#ifdef ULOG_USING_COLOR
+/* color output info */
+static const char * const color_output_info[] =
+{
+ ULOG_COLOR_ASSERT,
+ NULL,
+ NULL,
+ ULOG_COLOR_ERROR,
+ ULOG_COLOR_WARN,
+ NULL,
+ ULOG_COLOR_INFO,
+ ULOG_COLOR_DEBUG,
+};
+#endif /* ULOG_USING_COLOR */
+
+/* ulog local object */
+static struct rt_ulog ulog = { 0 };
+
+size_t ulog_strcpy(size_t cur_len, char *dst, const char *src)
+{
+ const char *src_old = src;
+
+ RT_ASSERT(dst);
+ RT_ASSERT(src);
+
+ while (*src != 0)
+ {
+ /* make sure destination has enough space */
+ if (cur_len++ < ULOG_LINE_BUF_SIZE)
+ {
+ *dst++ = *src++;
+ }
+ else
+ {
+ break;
+ }
+ }
+ return src - src_old;
+}
+
+size_t ulog_ultoa(char *s, unsigned long int n)
+{
+ size_t i = 0, j = 0, len = 0;
+ char swap;
+
+ do
+ {
+ s[len++] = n % 10 + '0';
+ } while (n /= 10);
+ s[len] = '\0';
+ /* reverse string */
+ for (i = 0, j = len - 1; i < j; ++i, --j)
+ {
+ swap = s[i];
+ s[i] = s[j];
+ s[j] = swap;
+ }
+ return len;
+}
+
+static void output_unlock(void)
+{
+ /* is in thread context */
+ if (rt_interrupt_get_nest() == 0)
+ {
+ rt_mutex_release(&ulog.output_locker);
+ }
+ else
+ {
+#ifdef ULOG_USING_ISR_LOG
+ rt_hw_interrupt_enable(ulog.output_locker_isr_lvl);
+#endif
+ }
+}
+
+static void output_lock(void)
+{
+ /* is in thread context */
+ if (rt_interrupt_get_nest() == 0)
+ {
+ rt_mutex_take(&ulog.output_locker, RT_WAITING_FOREVER);
+ }
+ else
+ {
+#ifdef ULOG_USING_ISR_LOG
+ ulog.output_locker_isr_lvl = rt_hw_interrupt_disable();
+#endif
+ }
+}
+
+static char *get_log_buf(void)
+{
+ /* is in thread context */
+ if (rt_interrupt_get_nest() == 0)
+ {
+ return ulog.log_buf_th;
+ }
+ else
+ {
+#ifdef ULOG_USING_ISR_LOG
+ return ulog.log_buf_isr;
+#else
+ rt_kprintf("Error: Current mode not supported run in ISR. Please enable ULOG_USING_ISR_LOG.\n");
+ return NULL;
+#endif
+ }
+}
+
+RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, rt_bool_t newline,
+ const char *format, va_list args)
+{
+ /* the caller has locker, so it can use static variable for reduce stack usage */
+ static rt_size_t log_len, newline_len;
+ static int fmt_result;
+
+ RT_ASSERT(log_buf);
+ RT_ASSERT(level <= LOG_LVL_DBG);
+ RT_ASSERT(tag);
+ RT_ASSERT(format);
+
+ log_len = 0;
+ newline_len = rt_strlen(ULOG_NEWLINE_SIGN);
+
+#ifdef ULOG_USING_COLOR
+ /* add CSI start sign and color info */
+ if (color_output_info[level])
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, CSI_START);
+ log_len += ulog_strcpy(log_len, log_buf + log_len, color_output_info[level]);
+ }
+#endif /* ULOG_USING_COLOR */
+
+#ifdef ULOG_OUTPUT_TIME
+ /* add time info */
+ {
+#ifdef ULOG_TIME_USING_TIMESTAMP
+ static time_t now;
+ static struct tm *tm, tm_tmp;
+
+ now = time(NULL);
+ tm = gmtime_r(&now, &tm_tmp);
+
+#ifdef RT_USING_SOFT_RTC
+ rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%02d-%02d %02d:%02d:%02d.%03d", tm->tm_mon + 1,
+ tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, rt_tick_get() % 1000);
+#else
+ rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%02d-%02d %02d:%02d:%02d", tm->tm_mon + 1,
+ tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+#endif /* RT_USING_SOFT_RTC */
+
+#else
+ static rt_size_t tick_len = 0;
+
+ log_buf[log_len] = '[';
+ tick_len = ulog_ultoa(log_buf + log_len + 1, rt_tick_get());
+ log_buf[log_len + 1 + tick_len] = ']';
+ log_buf[log_len + 1 + tick_len + 1] = '\0';
+#endif /* ULOG_TIME_USING_TIMESTAMP */
+
+ log_len += rt_strlen(log_buf + log_len);
+ }
+#endif /* ULOG_OUTPUT_TIME */
+
+#ifdef ULOG_OUTPUT_LEVEL
+
+#ifdef ULOG_OUTPUT_TIME
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+#endif
+
+ /* add level info */
+ log_len += ulog_strcpy(log_len, log_buf + log_len, level_output_info[level]);
+#endif /* ULOG_OUTPUT_LEVEL */
+
+#ifdef ULOG_OUTPUT_TAG
+
+#if !defined(ULOG_OUTPUT_LEVEL) && defined(ULOG_OUTPUT_TIME)
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+#endif
+
+ /* add tag info */
+ log_len += ulog_strcpy(log_len, log_buf + log_len, tag);
+#endif /* ULOG_OUTPUT_TAG */
+
+#ifdef ULOG_OUTPUT_THREAD_NAME
+ /* add thread info */
+ {
+
+#if defined(ULOG_OUTPUT_TIME) || defined(ULOG_OUTPUT_LEVEL) || defined(ULOG_OUTPUT_TAG)
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+#endif
+
+ /* is not in interrupt context */
+ if (rt_interrupt_get_nest() == 0)
+ {
+ rt_size_t name_len = rt_strnlen(rt_thread_self()->name, RT_NAME_MAX);
+
+ rt_strncpy(log_buf + log_len, rt_thread_self()->name, name_len);
+ log_len += name_len;
+ }
+ else
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, "ISR");
+ }
+ }
+#endif /* ULOG_OUTPUT_THREAD_NAME */
+
+ log_len += ulog_strcpy(log_len, log_buf + log_len, ": ");
+
+#ifdef ULOG_OUTPUT_FLOAT
+ fmt_result = vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
+#else
+ fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args);
+#endif /* ULOG_OUTPUT_FLOAT */
+
+ /* calculate log length */
+ if ((log_len + fmt_result <= ULOG_LINE_BUF_SIZE) && (fmt_result > -1))
+ {
+ log_len += fmt_result;
+ }
+ else
+ {
+ /* using max length */
+ log_len = ULOG_LINE_BUF_SIZE;
+ }
+
+ /* overflow check and reserve some space for CSI end sign and newline sign */
+#ifdef ULOG_USING_COLOR
+ if (log_len + (sizeof(CSI_END) - 1) + newline_len > ULOG_LINE_BUF_SIZE)
+ {
+ /* using max length */
+ log_len = ULOG_LINE_BUF_SIZE;
+ /* reserve some space for CSI end sign */
+ log_len -= (sizeof(CSI_END) - 1);
+#else
+ if (log_len + newline_len > ULOG_LINE_BUF_SIZE)
+ {
+ /* using max length */
+ log_len = ULOG_LINE_BUF_SIZE;
+#endif /* ULOG_USING_COLOR */
+ /* reserve some space for newline sign */
+ log_len -= newline_len;
+ }
+
+ /* package newline sign */
+ if (newline)
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
+ }
+
+#ifdef ULOG_USING_COLOR
+ /* add CSI end sign */
+ if (color_output_info[level])
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, CSI_END);
+ }
+#endif /* ULOG_USING_COLOR */
+
+ return log_len;
+}
+
+void ulog_output_to_all_backend(rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, rt_size_t size)
+{
+ rt_slist_t *node;
+ ulog_backend_t backend;
+
+ if (!ulog.init_ok)
+ return;
+
+ /* output for all backends */
+ for (node = rt_slist_first(&ulog.backend_list); node; node = rt_slist_next(node))
+ {
+ backend = rt_slist_entry(node, struct ulog_backend, list);
+#if !defined(ULOG_USING_COLOR) || defined(ULOG_USING_SYSLOG)
+ backend->output(backend, level, tag, is_raw, log, size);
+#else
+ if (backend->support_color || is_raw)
+ {
+ backend->output(backend, level, tag, is_raw, log, size);
+ }
+ else
+ {
+ /* recalculate the log start address and log size when backend not supported color */
+ rt_size_t color_info_len = rt_strlen(color_output_info[level]), output_size = size;
+ if (color_info_len)
+ {
+ rt_size_t color_hdr_len = rt_strlen(CSI_START) + color_info_len;
+
+ log += color_hdr_len;
+ output_size -= (color_hdr_len + (sizeof(CSI_END) - 1));
+ }
+ backend->output(backend, level, tag, is_raw, log, output_size);
+ }
+#endif /* !defined(ULOG_USING_COLOR) || defined(ULOG_USING_SYSLOG) */
+ }
+}
+
+static void do_output(rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log_buf, rt_size_t log_len)
+{
+#ifdef ULOG_USING_ASYNC_OUTPUT
+ rt_rbb_blk_t log_blk;
+ ulog_frame_t log_frame;
+
+ /* allocate log frame */
+ log_blk = rt_rbb_blk_alloc(ulog.async_rbb, RT_ALIGN(sizeof(struct ulog_frame) + log_len, RT_ALIGN_SIZE));
+ if (log_blk)
+ {
+ /* package the log frame */
+ log_frame = (ulog_frame_t) log_blk->buf;
+ log_frame->magic = ULOG_FRAME_MAGIC;
+ log_frame->is_raw = is_raw;
+ log_frame->level = level;
+ log_frame->log_len = log_len;
+ log_frame->tag = tag;
+ log_frame->log = (const char *)log_blk->buf + sizeof(struct ulog_frame);
+ /* copy log data */
+ rt_memcpy(log_blk->buf + sizeof(struct ulog_frame), log_buf, log_len);
+ /* put the block */
+ rt_rbb_blk_put(log_blk);
+ /* send a notice */
+ rt_sem_release(&ulog.async_notice);
+ }
+ else
+ {
+ static rt_bool_t already_output = RT_FALSE;
+ if (already_output == RT_FALSE)
+ {
+ rt_kprintf("Warning: There is no enough buffer for saving async log,"
+ " please increase the ULOG_ASYNC_OUTPUT_BUF_SIZE option.\n");
+ already_output = RT_TRUE;
+ }
+ }
+#else
+ /* is in thread context */
+ if (rt_interrupt_get_nest() == 0)
+ {
+ /* output to all backends */
+ ulog_output_to_all_backend(level, tag, is_raw, log_buf, log_len);
+ }
+ else
+ {
+#ifdef ULOG_BACKEND_USING_CONSOLE
+ /* We can't ensure that all backends support ISR context output.
+ * So only using rt_kprintf when context is ISR */
+ extern void ulog_console_backend_output(struct ulog_backend *backend, rt_uint32_t level, const char *tag,
+ rt_bool_t is_raw, const char *log, size_t len);
+ ulog_console_backend_output(NULL, level, tag, is_raw, log_buf, log_len);
+#endif /* ULOG_BACKEND_USING_CONSOLE */
+ }
+#endif /* ULOG_USING_ASYNC_OUTPUT */
+}
+
+/**
+ * output the log by variable argument list
+ *
+ * @param level level
+ * @param tag tag
+ * @param newline has_newline
+ * @param format output format
+ * @param args variable argument list
+ */
+void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args)
+{
+ char *log_buf = NULL;
+ rt_size_t log_len = 0;
+
+#ifndef ULOG_USING_SYSLOG
+ RT_ASSERT(level <= LOG_LVL_DBG);
+#else
+ RT_ASSERT(LOG_PRI(level) <= LOG_DEBUG);
+#endif /* ULOG_USING_SYSLOG */
+
+ RT_ASSERT(tag);
+ RT_ASSERT(format);
+
+ if (!ulog.init_ok)
+ {
+ return;
+ }
+
+#ifdef ULOG_USING_FILTER
+ /* level filter */
+#ifndef ULOG_USING_SYSLOG
+ if (level > ulog.filter.level || level > ulog_tag_lvl_filter_get(tag))
+ {
+ return;
+ }
+#else
+ if (((LOG_MASK(LOG_PRI(level)) & ulog.filter.level) == 0)
+ || ((LOG_MASK(LOG_PRI(level)) & ulog_tag_lvl_filter_get(tag)) == 0))
+ {
+ return;
+ }
+#endif /* ULOG_USING_SYSLOG */
+ else if (!rt_strstr(tag, ulog.filter.tag))
+ {
+ /* tag filter */
+ return;
+ }
+#endif /* ULOG_USING_FILTER */
+
+ /* get log buffer */
+ log_buf = get_log_buf();
+
+ /* lock output */
+ output_lock();
+
+#ifndef ULOG_USING_SYSLOG
+ log_len = ulog_formater(log_buf, level, tag, newline, format, args);
+#else
+ extern rt_size_t syslog_formater(char *log_buf, rt_uint8_t level, const char *tag, rt_bool_t newline, const char *format, va_list args);
+ log_len = syslog_formater(log_buf, level, tag, newline, format, args);
+#endif /* ULOG_USING_SYSLOG */
+
+#ifdef ULOG_USING_FILTER
+ /* keyword filter */
+ if (ulog.filter.keyword[0] != '\0')
+ {
+ /* add string end sign */
+ log_buf[log_len] = '\0';
+ /* find the keyword */
+ if (!rt_strstr(log_buf, ulog.filter.keyword))
+ {
+ /* unlock output */
+ output_unlock();
+ return;
+ }
+ }
+#endif /* ULOG_USING_FILTER */
+ /* do log output */
+ do_output(level, tag, RT_FALSE, log_buf, log_len);
+
+ /* unlock output */
+ output_unlock();
+}
+
+/**
+ * output the log
+ *
+ * @param level level
+ * @param tag tag
+ * @param newline has newline
+ * @param format output format
+ * @param ... args
+ */
+void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...)
+{
+ va_list args;
+
+ /* args point to the first variable parameter */
+ va_start(args, format);
+
+ ulog_voutput(level, tag, newline, format, args);
+
+ va_end(args);
+}
+
+/**
+ * output RAW string format log
+ *
+ * @param format output format
+ * @param ... args
+ */
+void ulog_raw(const char *format, ...)
+{
+ rt_size_t log_len = 0;
+ char *log_buf = NULL;
+ va_list args;
+ int fmt_result;
+
+ RT_ASSERT(ulog.init_ok);
+
+ /* get log buffer */
+ log_buf = get_log_buf();
+
+ /* lock output */
+ output_lock();
+ /* args point to the first variable parameter */
+ va_start(args, format);
+
+#ifdef ULOG_OUTPUT_FLOAT
+ fmt_result = vsnprintf(log_buf, ULOG_LINE_BUF_SIZE, format, args);
+#else
+ fmt_result = rt_vsnprintf(log_buf, ULOG_LINE_BUF_SIZE, format, args);
+#endif /* ULOG_OUTPUT_FLOAT */
+
+ va_end(args);
+
+ /* calculate log length */
+ if ((fmt_result > -1) && (fmt_result <= ULOG_LINE_BUF_SIZE))
+ {
+ log_len = fmt_result;
+ }
+ else
+ {
+ log_len = ULOG_LINE_BUF_SIZE;
+ }
+
+ /* do log output */
+ do_output(LOG_LVL_DBG, NULL, RT_TRUE, log_buf, log_len);
+
+ /* unlock output */
+ output_unlock();
+}
+
+/**
+ * dump the hex format data to log
+ *
+ * @param tag name for hex object, it will show on log header
+ * @param width hex number for every line, such as: 16, 32
+ * @param buf hex buffer
+ * @param size buffer size
+ */
+void ulog_hexdump(const char *tag, rt_size_t width, rt_uint8_t *buf, rt_size_t size)
+{
+#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
+
+ rt_size_t i, j;
+ rt_size_t log_len = 0, name_len = rt_strlen(tag);
+ char *log_buf = NULL, dump_string[8];
+ int fmt_result;
+
+ RT_ASSERT(ulog.init_ok);
+
+#ifdef ULOG_USING_FILTER
+ /* level filter */
+#ifndef ULOG_USING_SYSLOG
+ if (LOG_LVL_DBG > ulog.filter.level || LOG_LVL_DBG > ulog_tag_lvl_filter_get(tag))
+ {
+ return;
+ }
+#else
+ if ((LOG_MASK(LOG_DEBUG) & ulog.filter.level) == 0)
+ {
+ return;
+ }
+#endif /* ULOG_USING_SYSLOG */
+ else if (!rt_strstr(tag, ulog.filter.tag))
+ {
+ /* tag filter */
+ return;
+ }
+#endif /* ULOG_USING_FILTER */
+
+ /* get log buffer */
+ log_buf = get_log_buf();
+
+ /* lock output */
+ output_lock();
+
+ for (i = 0, log_len = 0; i < size; i += width)
+ {
+ /* package header */
+ if (i == 0)
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, "D/HEX ");
+ log_len += ulog_strcpy(log_len, log_buf + log_len, tag);
+ log_len += ulog_strcpy(log_len, log_buf + log_len, ": ");
+ }
+ else
+ {
+ log_len = 6 + name_len + 2;
+ rt_memset(log_buf, ' ', log_len);
+ }
+ fmt_result = rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE, "%04X-%04X: ", i, i + width - 1);
+ /* calculate log length */
+ if ((fmt_result > -1) && (fmt_result <= ULOG_LINE_BUF_SIZE))
+ {
+ log_len += fmt_result;
+ }
+ else
+ {
+ log_len = ULOG_LINE_BUF_SIZE;
+ }
+ /* dump hex */
+ for (j = 0; j < width; j++)
+ {
+ if (i + j < size)
+ {
+ rt_snprintf(dump_string, sizeof(dump_string), "%02X ", buf[i + j]);
+ }
+ else
+ {
+ rt_strncpy(dump_string, " ", sizeof(dump_string));
+ }
+ log_len += ulog_strcpy(log_len, log_buf + log_len, dump_string);
+ if ((j + 1) % 8 == 0)
+ {
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+ }
+ }
+ log_len += ulog_strcpy(log_len, log_buf + log_len, " ");
+ /* dump char for hex */
+ for (j = 0; j < width; j++)
+ {
+ if (i + j < size)
+ {
+ rt_snprintf(dump_string, sizeof(dump_string), "%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
+ log_len += ulog_strcpy(log_len, log_buf + log_len, dump_string);
+ }
+ }
+ /* overflow check and reserve some space for newline sign */
+ if (log_len + rt_strlen(ULOG_NEWLINE_SIGN) > ULOG_LINE_BUF_SIZE)
+ {
+ log_len = ULOG_LINE_BUF_SIZE - rt_strlen(ULOG_NEWLINE_SIGN);
+ }
+ /* package newline sign */
+ log_len += ulog_strcpy(log_len, log_buf + log_len, ULOG_NEWLINE_SIGN);
+ /*add string end sign*/
+ log_buf[log_len] = '\0';
+ /* do log output */
+ do_output(LOG_LVL_DBG, NULL, RT_TRUE, log_buf, log_len);
+ }
+ /* unlock output */
+ output_unlock();
+}
+
+#ifdef ULOG_USING_FILTER
+/**
+ * Set the filter's level by different tag.
+ * The log on this tag which level is less than it will stop output.
+ *
+ * example:
+ * // the example tag log enter silent mode
+ * ulog_set_filter_lvl("example", LOG_FILTER_LVL_SILENT);
+ * // the example tag log which level is less than INFO level will stop output
+ * ulog_set_filter_lvl("example", LOG_LVL_INFO);
+ * // remove example tag's level filter, all level log will resume output
+ * ulog_set_filter_lvl("example", LOG_FILTER_LVL_ALL);
+ *
+ * @param tag log tag
+ * @param level The filter level. When the level is LOG_FILTER_LVL_SILENT, the log enter silent mode.
+ * When the level is LOG_FILTER_LVL_ALL, it will remove this tag's level filer.
+ * Then all level log will resume output.
+ *
+ * @return 0 : success
+ * -5 : no memory
+ * -10: level is out of range
+ */
+int ulog_tag_lvl_filter_set(const char *tag, rt_uint32_t level)
+{
+ rt_slist_t *node;
+ ulog_tag_lvl_filter_t tag_lvl = NULL;
+ int result = RT_EOK;
+
+ if (level > LOG_FILTER_LVL_ALL)
+ return -RT_EINVAL;
+
+ if (!ulog.init_ok)
+ return result;
+
+ /* lock output */
+ output_lock();
+ /* find the tag in list */
+ for (node = rt_slist_first(ulog_tag_lvl_list_get()); node; node = rt_slist_next(node))
+ {
+ tag_lvl = rt_slist_entry(node, struct ulog_tag_lvl_filter, list);
+ if (!rt_strncmp(tag_lvl->tag, tag, ULOG_FILTER_TAG_MAX_LEN))
+ {
+ break;
+ }
+ else
+ {
+ tag_lvl = NULL;
+ }
+ }
+ /* find OK */
+ if (tag_lvl)
+ {
+ if (level == LOG_FILTER_LVL_ALL)
+ {
+ /* remove current tag's level filter when input level is the lowest level */
+ rt_slist_remove(ulog_tag_lvl_list_get(), &tag_lvl->list);
+ rt_free(tag_lvl);
+ }
+ else
+ {
+ /* update level */
+ tag_lvl->level = level;
+ }
+ }
+ else
+ {
+ /* only add the new tag's level filer when level is not LOG_FILTER_LVL_ALL */
+ if (level != LOG_FILTER_LVL_ALL)
+ {
+ /* new a tag's level filter */
+ tag_lvl = (ulog_tag_lvl_filter_t)rt_malloc(sizeof(struct ulog_tag_lvl_filter));
+ if (tag_lvl)
+ {
+ rt_memset(tag_lvl->tag, 0 , sizeof(tag_lvl->tag));
+ rt_strncpy(tag_lvl->tag, tag, ULOG_FILTER_TAG_MAX_LEN);
+ tag_lvl->level = level;
+ rt_slist_append(ulog_tag_lvl_list_get(), &tag_lvl->list);
+ }
+ else
+ {
+ result = -RT_ENOMEM;
+ }
+ }
+ }
+ /* unlock output */
+ output_unlock();
+
+ return result;
+}
+
+/**
+ * get the level on tag's level filer
+ *
+ * @param tag log tag
+ *
+ * @return It will return the lowest level when tag was not found.
+ * Other level will return when tag was found.
+ */
+rt_uint32_t ulog_tag_lvl_filter_get(const char *tag)
+{
+ rt_slist_t *node;
+ ulog_tag_lvl_filter_t tag_lvl = NULL;
+ rt_uint32_t level = LOG_FILTER_LVL_ALL;
+
+ if (!ulog.init_ok)
+ return level;
+
+ /* lock output */
+ output_lock();
+ /* find the tag in list */
+ for (node = rt_slist_first(ulog_tag_lvl_list_get()); node; node = rt_slist_next(node))
+ {
+ tag_lvl = rt_slist_entry(node, struct ulog_tag_lvl_filter, list);
+ if (!rt_strncmp(tag_lvl->tag, tag, ULOG_FILTER_TAG_MAX_LEN))
+ {
+ level = tag_lvl->level;
+ break;
+ }
+ }
+ /* unlock output */
+ output_unlock();
+
+ return level;
+}
+
+/**
+ * get the tag's level list on filter
+ *
+ * @return tag's level list
+ */
+rt_slist_t *ulog_tag_lvl_list_get(void)
+{
+ return &ulog.filter.tag_lvl_list;
+}
+
+/**
+ * set log global filter level
+ *
+ * @param level log level: LOG_LVL_ASSERT, LOG_LVL_ERROR, LOG_LVL_WARNING, LOG_LVL_INFO, LOG_LVL_DBG
+ * LOG_FILTER_LVL_SILENT: disable all log output, except assert level
+ * LOG_FILTER_LVL_ALL: enable all log output
+ */
+void ulog_global_filter_lvl_set(rt_uint32_t level)
+{
+ RT_ASSERT(level <= LOG_FILTER_LVL_ALL);
+
+ ulog.filter.level = level;
+}
+
+/**
+ * get log global filter level
+ *
+ * @return log level: LOG_LVL_ASSERT, LOG_LVL_ERROR, LOG_LVL_WARNING, LOG_LVL_INFO, LOG_LVL_DBG
+ * LOG_FILTER_LVL_SILENT: disable all log output, except assert level
+ * LOG_FILTER_LVL_ALL: enable all log output
+ */
+rt_uint32_t ulog_global_filter_lvl_get(void)
+{
+ return ulog.filter.level;
+}
+
+/**
+ * set log global filter tag
+ *
+ * @param tag tag
+ */
+void ulog_global_filter_tag_set(const char *tag)
+{
+ RT_ASSERT(tag);
+
+ rt_strncpy(ulog.filter.tag, tag, ULOG_FILTER_TAG_MAX_LEN);
+}
+
+/**
+ * get log global filter tag
+ *
+ * @return tag
+ */
+const char *ulog_global_filter_tag_get(void)
+{
+ return ulog.filter.tag;
+}
+
+/**
+ * set log global filter keyword
+ *
+ * @param keyword keyword
+ */
+void ulog_global_filter_kw_set(const char *keyword)
+{
+ RT_ASSERT(keyword);
+
+ rt_strncpy(ulog.filter.keyword, keyword, ULOG_FILTER_KW_MAX_LEN);
+}
+
+/**
+ * get log global filter keyword
+ *
+ * @return keyword
+ */
+const char *ulog_global_filter_kw_get(void)
+{
+ return ulog.filter.keyword;
+}
+
+#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
+#include <finsh.h>
+
+static void ulog_tag_lvl(uint8_t argc, char **argv)
+{
+ if (argc > 2)
+ {
+ if ((atoi(argv[2]) <= LOG_FILTER_LVL_ALL) && (atoi(argv[2]) >= 0))
+ {
+ ulog_tag_lvl_filter_set(argv[1], atoi(argv[2]));
+ }
+ else
+ {
+ rt_kprintf("Please input correct level (0-%d).\n", LOG_FILTER_LVL_ALL);
+ }
+ }
+ else
+ {
+ rt_kprintf("Please input: ulog_tag_lvl <tag> <level>.\n");
+#ifndef ULOG_USING_SYSLOG
+ rt_kprintf("Assert : 0\n");
+ rt_kprintf("Error : 3\n");
+ rt_kprintf("Warning : 4\n");
+ rt_kprintf("Info : 6\n");
+ rt_kprintf("Debug : 7\n");
+#else
+ rt_kprintf("EMERG : 1 (1 << 0)\n");
+ rt_kprintf("ALERT : 2 (1 << 1)\n");
+ rt_kprintf("CRIT : 4 (1 << 2)\n");
+ rt_kprintf("ERR : 8 (1 << 3)\n");
+ rt_kprintf("WARNING : 16 (1 << 4)\n");
+ rt_kprintf("NOTICE : 32 (1 << 5)\n");
+ rt_kprintf("INFO : 64 (1 << 6)\n");
+ rt_kprintf("DEBUG : 128 (1 << 7)\n");
+#endif /* ULOG_USING_SYSLOG */
+ }
+}
+MSH_CMD_EXPORT(ulog_tag_lvl, Set ulog filter level by different tag.);
+
+static void ulog_lvl(uint8_t argc, char **argv)
+{
+ if (argc > 1)
+ {
+ if ((atoi(argv[1]) <= LOG_FILTER_LVL_ALL) && (atoi(argv[1]) >= 0))
+ {
+ ulog_global_filter_lvl_set(atoi(argv[1]));
+ }
+ else
+ {
+ rt_kprintf("Please input correct level (0-%d).\n", LOG_FILTER_LVL_ALL);
+ }
+ }
+ else
+ {
+ rt_kprintf("Please input: ulog_lvl <level>.\n");
+#ifndef ULOG_USING_SYSLOG
+ rt_kprintf("Assert : 0\n");
+ rt_kprintf("Error : 3\n");
+ rt_kprintf("Warning : 4\n");
+ rt_kprintf("Info : 6\n");
+ rt_kprintf("Debug : 7\n");
+#else
+ rt_kprintf("EMERG : 1 (1 << 0)\n");
+ rt_kprintf("ALERT : 2 (1 << 1)\n");
+ rt_kprintf("CRIT : 4 (1 << 2)\n");
+ rt_kprintf("ERR : 8 (1 << 3)\n");
+ rt_kprintf("WARNING : 16 (1 << 4)\n");
+ rt_kprintf("NOTICE : 32 (1 << 5)\n");
+ rt_kprintf("INFO : 64 (1 << 6)\n");
+ rt_kprintf("DEBUG : 128 (1 << 7)\n");
+#endif /* ULOG_USING_SYSLOG */
+ }
+}
+MSH_CMD_EXPORT(ulog_lvl, Set ulog global filter level.);
+
+static void ulog_tag(uint8_t argc, char **argv)
+{
+ if (argc > 1)
+ {
+ if (rt_strlen(argv[1]) <= ULOG_FILTER_TAG_MAX_LEN)
+ {
+ ulog_global_filter_tag_set(argv[1]);
+ }
+ else
+ {
+ rt_kprintf("The tag length is too long. Max is %d.\n", ULOG_FILTER_TAG_MAX_LEN);
+ }
+ }
+ else
+ {
+ ulog_global_filter_tag_set("");
+ }
+}
+MSH_CMD_EXPORT(ulog_tag, Set ulog global filter tag);
+
+static void ulog_kw(uint8_t argc, char **argv)
+{
+ if (argc > 1)
+ {
+ if (rt_strlen(argv[1]) <= ULOG_FILTER_KW_MAX_LEN)
+ {
+ ulog_global_filter_kw_set(argv[1]);
+ }
+ else
+ {
+ rt_kprintf("The keyword length is too long. Max is %d.\n", ULOG_FILTER_KW_MAX_LEN);
+ }
+ }
+ else
+ {
+ ulog_global_filter_kw_set("");
+ }
+}
+MSH_CMD_EXPORT(ulog_kw, Set ulog global filter keyword);
+
+static void ulog_filter(uint8_t argc, char **argv)
+{
+#ifndef ULOG_USING_SYSLOG
+ const char *lvl_name[] = { "Assert ", "Error ", "Error ", "Error ", "Warning", "Info ", "Info ", "Debug " };
+#endif
+ const char *tag = ulog_global_filter_tag_get(), *kw = ulog_global_filter_kw_get();
+ rt_slist_t *node;
+ ulog_tag_lvl_filter_t tag_lvl = NULL;
+
+ rt_kprintf("--------------------------------------\n");
+ rt_kprintf("ulog global filter:\n");
+
+#ifndef ULOG_USING_SYSLOG
+ rt_kprintf("level : %s\n", lvl_name[ulog_global_filter_lvl_get()]);
+#else
+ rt_kprintf("level : %d\n", ulog_global_filter_lvl_get());
+#endif
+
+ rt_kprintf("tag : %s\n", rt_strlen(tag) == 0 ? "NULL" : tag);
+ rt_kprintf("keyword : %s\n", rt_strlen(kw) == 0 ? "NULL" : kw);
+
+ rt_kprintf("--------------------------------------\n");
+ rt_kprintf("ulog tag's level filter:\n");
+ if (rt_slist_isempty(ulog_tag_lvl_list_get()))
+ {
+ rt_kprintf("settings not found\n");
+ }
+ else
+ {
+ /* lock output */
+ output_lock();
+ /* show the tag level list */
+ for (node = rt_slist_first(ulog_tag_lvl_list_get()); node; node = rt_slist_next(node))
+ {
+ tag_lvl = rt_slist_entry(node, struct ulog_tag_lvl_filter, list);
+ rt_kprintf("%-*.s: ", ULOG_FILTER_TAG_MAX_LEN, tag_lvl->tag);
+
+#ifndef ULOG_USING_SYSLOG
+ rt_kprintf("%s\n", lvl_name[tag_lvl->level]);
+#else
+ rt_kprintf("%d\n", tag_lvl->level);
+#endif
+
+ }
+ /* unlock output */
+ output_unlock();
+ }
+}
+MSH_CMD_EXPORT(ulog_filter, Show ulog filter settings);
+#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
+#endif /* ULOG_USING_FILTER */
+
+rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color)
+{
+ rt_base_t level;
+
+ RT_ASSERT(backend);
+ RT_ASSERT(name);
+ RT_ASSERT(ulog.init_ok);
+ RT_ASSERT(backend->output);
+
+ if (backend->init)
+ {
+ backend->init(backend);
+ }
+
+ backend->support_color = support_color;
+ rt_memcpy(backend->name, name, RT_NAME_MAX);
+
+ level = rt_hw_interrupt_disable();
+ rt_slist_append(&ulog.backend_list, &backend->list);
+ rt_hw_interrupt_enable(level);
+
+ return RT_EOK;
+}
+
+rt_err_t ulog_backend_unregister(ulog_backend_t backend)
+{
+ rt_base_t level;
+
+ RT_ASSERT(backend);
+ RT_ASSERT(ulog.init_ok);
+
+ if (backend->deinit)
+ {
+ backend->deinit(backend);
+ }
+
+ level = rt_hw_interrupt_disable();
+ rt_slist_remove(&ulog.backend_list, &backend->list);
+ rt_hw_interrupt_enable(level);
+
+ return RT_EOK;
+}
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+/**
+ * asynchronous output logs to all backends
+ *
+ * @note you must call this function when ULOG_ASYNC_OUTPUT_BY_THREAD is disable
+ */
+void ulog_async_output(void)
+{
+ rt_rbb_blk_t log_blk;
+ ulog_frame_t log_frame;
+
+ while ((log_blk = rt_rbb_blk_get(ulog.async_rbb)) != NULL)
+ {
+ log_frame = (ulog_frame_t) log_blk->buf;
+ if (log_frame->magic == ULOG_FRAME_MAGIC)
+ {
+ /* output to all backends */
+ ulog_output_to_all_backend(log_frame->level, log_frame->tag, log_frame->is_raw, log_frame->log,
+ log_frame->log_len);
+ }
+ rt_rbb_blk_free(ulog.async_rbb, log_blk);
+ }
+}
+
+/**
+ * waiting for get asynchronous output log
+ *
+ * @param time the waiting time
+ */
+void ulog_async_waiting_log(rt_int32_t time)
+{
+ rt_sem_control(&ulog.async_notice, RT_IPC_CMD_RESET, RT_NULL);
+ rt_sem_take(&ulog.async_notice, time);
+}
+
+static void async_output_thread_entry(void *param)
+{
+ ulog_async_output();
+
+ while (1)
+ {
+ ulog_async_waiting_log(RT_WAITING_FOREVER);
+ ulog_async_output();
+ }
+}
+#endif /* ULOG_USING_ASYNC_OUTPUT */
+
+/**
+ * flush all backends's log
+ */
+void ulog_flush(void)
+{
+ rt_slist_t *node;
+ ulog_backend_t backend;
+
+ if (!ulog.init_ok)
+ return;
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+ ulog_async_output();
+#endif
+
+ /* flush all backends */
+ for (node = rt_slist_first(&ulog.backend_list); node; node = rt_slist_next(node))
+ {
+ backend = rt_slist_entry(node, struct ulog_backend, list);
+ if (backend->flush)
+ {
+ backend->flush(backend);
+ }
+ }
+}
+
+int ulog_init(void)
+{
+ if (ulog.init_ok)
+ return 0;
+
+ rt_mutex_init(&ulog.output_locker, "ulog lock", RT_IPC_FLAG_FIFO);
+ rt_slist_init(&ulog.backend_list);
+
+#ifdef ULOG_USING_FILTER
+ rt_slist_init(ulog_tag_lvl_list_get());
+#endif
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+ RT_ASSERT(ULOG_ASYNC_OUTPUT_STORE_LINES >= 2);
+ /* async output ring block buffer */
+ ulog.async_rbb = rt_rbb_create(RT_ALIGN(ULOG_ASYNC_OUTPUT_BUF_SIZE, RT_ALIGN_SIZE), ULOG_ASYNC_OUTPUT_STORE_LINES);
+ if (ulog.async_rbb == NULL)
+ {
+ rt_kprintf("Error: ulog init failed! No memory for async rbb.\n");
+ rt_mutex_detach(&ulog.output_locker);
+ return -RT_ENOMEM;
+ }
+ /* async output thread */
+ ulog.async_th = rt_thread_create("ulog_async", async_output_thread_entry, &ulog, ULOG_ASYNC_OUTPUT_THREAD_STACK,
+ ULOG_ASYNC_OUTPUT_THREAD_PRIORITY, 20);
+ if (ulog.async_th == NULL)
+ {
+ rt_kprintf("Error: ulog init failed! No memory for async output thread.\n");
+ rt_mutex_detach(&ulog.output_locker);
+ rt_rbb_destroy(ulog.async_rbb);
+ return -RT_ENOMEM;
+ }
+
+ rt_sem_init(&ulog.async_notice, "ulog", 0, RT_IPC_FLAG_FIFO);
+ /* async output thread startup */
+ rt_thread_startup(ulog.async_th);
+
+#endif /* ULOG_USING_ASYNC_OUTPUT */
+
+#ifdef ULOG_USING_FILTER
+ ulog_global_filter_lvl_set(LOG_FILTER_LVL_ALL);
+#endif
+
+ ulog.init_ok = RT_TRUE;
+
+ return 0;
+}
+INIT_PREV_EXPORT(ulog_init);
+
+void ulog_deinit(void)
+{
+ rt_slist_t *node;
+ ulog_backend_t backend;
+
+ if (!ulog.init_ok)
+ return;
+
+ /* deinit all backends */
+ for (node = rt_slist_first(&ulog.backend_list); node; node = rt_slist_next(node))
+ {
+ backend = rt_slist_entry(node, struct ulog_backend, list);
+ if (backend->deinit)
+ {
+ backend->deinit(backend);
+ }
+ }
+
+#ifdef ULOG_USING_FILTER
+ /* deinit tag's level filter */
+ {
+ ulog_tag_lvl_filter_t tag_lvl;
+ for (node = rt_slist_first(ulog_tag_lvl_list_get()); node; node = rt_slist_next(node))
+ {
+ tag_lvl = rt_slist_entry(node, struct ulog_tag_lvl_filter, list);
+ rt_free(tag_lvl);
+ }
+ }
+#endif /* ULOG_USING_FILTER */
+
+ rt_mutex_detach(&ulog.output_locker);
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+ rt_rbb_destroy(ulog.async_rbb);
+ rt_thread_delete(ulog.async_th);
+#endif
+
+ ulog.init_ok = RT_FALSE;
+}
+
+#endif /* RT_USING_ULOG */
diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h
new file mode 100644
index 0000000000..c78d72eda5
--- /dev/null
+++ b/components/utilities/ulog/ulog.h
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-08-25 armink the first version
+ */
+
+#ifndef _ULOG_H_
+#define _ULOG_H_
+
+#include <rtthread.h>
+#include "ulog_def.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ULOG_VERSION_STR "0.1.1"
+
+/*
+ * ulog init and deint
+ */
+int ulog_init(void);
+void ulog_deinit(void);
+
+/*
+ * output different level log by LOG_X API
+ *
+ * NOTE: The `LOG_TAG` and `LOG_LVL` must be defined before including the <ulog.h> when you want to use LOG_X API.
+ *
+ * #define LOG_TAG "example"
+ * #define LOG_LVL LOG_LVL_DBG
+ * #include <ulog.h>
+ *
+ * Then you can using LOG_X API to output log
+ *
+ * LOG_D("this is a debug log!");
+ * LOG_E("this is a error log!");
+ */
+#define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__)
+#define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__)
+#define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__)
+#define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__)
+#define LOG_RAW(...) ulog_raw(__VA_ARGS__)
+#define LOG_HEX(name, width, buf, size) ulog_hex(name, width, buf, size)
+
+/*
+ * backend register and unregister
+ */
+rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color);
+rt_err_t ulog_backend_unregister(ulog_backend_t backend);
+
+#ifdef ULOG_USING_FILTER
+/*
+ * log filter setting
+ */
+int ulog_tag_lvl_filter_set(const char *tag, rt_uint32_t level);
+rt_uint32_t ulog_tag_lvl_filter_get(const char *tag);
+rt_slist_t *ulog_tag_lvl_list_get(void);
+void ulog_global_filter_lvl_set(rt_uint32_t level);
+rt_uint32_t ulog_global_filter_lvl_get(void);
+void ulog_global_filter_tag_set(const char *tag);
+const char *ulog_global_filter_tag_get(void);
+void ulog_global_filter_kw_set(const char *keyword);
+const char *ulog_global_filter_kw_get(void);
+#endif /* ULOG_USING_FILTER */
+
+/*
+ * flush all backends's log
+ */
+void ulog_flush(void);
+
+#ifdef ULOG_USING_ASYNC_OUTPUT
+/*
+ * asynchronous output API
+ */
+void ulog_async_output(void);
+void ulog_async_waiting_log(rt_int32_t time);
+#endif
+
+/*
+ * dump the hex format data to log
+ */
+void ulog_hexdump(const char *tag, rt_size_t width, rt_uint8_t *buf, rt_size_t size);
+
+/*
+ * Another log output API. This API is more difficult to use than LOG_X API.
+ */
+void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, va_list args);
+void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...);
+void ulog_raw(const char *format, ...);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ULOG_H_ */
diff --git a/components/utilities/ulog/ulog_def.h b/components/utilities/ulog/ulog_def.h
new file mode 100644
index 0000000000..de5e82fcc5
--- /dev/null
+++ b/components/utilities/ulog/ulog_def.h
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-08-25 armink the first version
+ */
+
+#ifndef _ULOG_DEF_H_
+#define _ULOG_DEF_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* logger level, the number is compatible for syslog */
+#define LOG_LVL_ASSERT 0
+#define LOG_LVL_ERROR 3
+#define LOG_LVL_WARNING 4
+#define LOG_LVL_INFO 6
+#define LOG_LVL_DBG 7
+
+/* the output silent level and all level for filter setting */
+#ifndef ULOG_USING_SYSLOG
+#define LOG_FILTER_LVL_SILENT 0
+#define LOG_FILTER_LVL_ALL 7
+#else
+#define LOG_FILTER_LVL_SILENT 1
+#define LOG_FILTER_LVL_ALL 255
+#endif /* ULOG_USING_SYSLOG */
+
+/* compatible for rtdbg */
+#undef LOG_D
+#undef LOG_I
+#undef LOG_W
+#undef LOG_E
+#undef LOG_RAW
+#undef DBG_ERROR
+#undef DBG_WARNING
+#undef DBG_INFO
+#undef DBG_LOG
+#undef dbg_log
+#define DBG_ERROR LOG_LVL_ERROR
+#define DBG_WARNING LOG_LVL_WARNING
+#define DBG_INFO LOG_LVL_INFO
+#define DBG_LOG LOG_LVL_DBG
+#define dbg_log(level, ...) \
+ if ((level) <= LOG_LVL) \
+ { \
+ ulog_output(level, LOG_TAG, RT_FALSE, __VA_ARGS__);\
+ }
+
+#if !defined(LOG_TAG)
+ /* compatible for rtdbg */
+ #if defined(DBG_TAG)
+ #define LOG_TAG DBG_TAG
+ #elif defined(DBG_SECTION_NAME)
+ #define LOG_TAG DBG_SECTION_NAME
+ #else
+ #define LOG_TAG "NO_TAG"
+ #endif
+#endif /* !defined(LOG_TAG) */
+
+#if !defined(LOG_LVL)
+ /* compatible for rtdbg */
+ #if defined(DBG_LVL)
+ #define LOG_LVL DBG_LVL
+ #elif defined(DBG_LEVEL)
+ #define LOG_LVL DBG_LEVEL
+ #else
+ #define LOG_LVL LOG_LVL_DBG
+ #endif
+#endif /* !defined(LOG_LVL) */
+
+#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
+ #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__)
+#else
+ #define ulog_d(TAG, ...)
+#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
+
+#if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO)
+ #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__)
+#else
+ #define ulog_i(TAG, ...)
+#endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */
+
+#if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING)
+ #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__)
+#else
+ #define ulog_w(TAG, ...)
+#endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */
+
+#if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR)
+ #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__)
+#else
+ #define ulog_e(TAG, ...)
+#endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */
+
+#if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG)
+ #define ulog_hex(TAG, width, buf, size) ulog_hexdump(TAG, width, buf, size)
+#else
+ #define ulog_hex(TAG, width, buf, size)
+#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */
+
+/* assert for developer. */
+#ifdef ULOG_ASSERT_ENABLE
+ #define ULOG_ASSERT(EXPR) \
+ if (!(EXPR)) \
+ { \
+ ulog_output(LOG_LVL_ASSERT, LOG_TAG, RT_TRUE, "(%s) has assert failed at %s:%ld.", #EXPR, __FUNCTION__, __LINE__); \
+ ulog_flush(); \
+ while (1); \
+ }
+#else
+ #define ULOG_ASSERT(EXPR)
+#endif
+
+/* ASSERT API definition */
+#if !defined(ASSERT)
+ #define ASSERT ULOG_ASSERT
+#endif
+
+/* compatible for elog */
+#undef assert
+#undef log_e
+#undef log_w
+#undef log_i
+#undef log_d
+#undef log_v
+#undef ELOG_LVL_ASSERT
+#undef ELOG_LVL_ERROR
+#undef ELOG_LVL_WARN
+#undef ELOG_LVL_INFO
+#undef ELOG_LVL_DEBUG
+#undef ELOG_LVL_VERBOSE
+#define assert ASSERT
+#define log_e LOG_E
+#define log_w LOG_W
+#define log_i LOG_I
+#define log_d LOG_D
+#define log_v LOG_D
+#define log_raw LOG_RAW
+#define log_hex LOG_HEX
+#define ELOG_LVL_ASSERT LOG_LVL_ASSERT
+#define ELOG_LVL_ERROR LOG_LVL_ERROR
+#define ELOG_LVL_WARN LOG_LVL_WARNING
+#define ELOG_LVL_INFO LOG_LVL_INFO
+#define ELOG_LVL_DEBUG LOG_LVL_DBG
+#define ELOG_LVL_VERBOSE LOG_LVL_DBG
+
+/* setting static output log level */
+#ifndef ULOG_OUTPUT_LVL
+#define ULOG_OUTPUT_LVL LOG_LVL_DBG
+#endif
+
+/* buffer size for every line's log */
+#ifndef ULOG_LINE_BUF_SIZE
+#define ULOG_LINE_BUF_SIZE 128
+#endif
+
+/* output filter's tag max length */
+#ifndef ULOG_FILTER_TAG_MAX_LEN
+#define ULOG_FILTER_TAG_MAX_LEN 23
+#endif
+
+/* output filter's keyword max length */
+#ifndef ULOG_FILTER_KW_MAX_LEN
+#define ULOG_FILTER_KW_MAX_LEN 15
+#endif
+
+#ifndef ULOG_NEWLINE_SIGN
+#define ULOG_NEWLINE_SIGN "\r\n"
+#endif
+
+#define ULOG_FRAME_MAGIC 0x10
+
+/* tag's level filter */
+struct ulog_tag_lvl_filter
+{
+ char tag[ULOG_FILTER_TAG_MAX_LEN + 1];
+ rt_uint32_t level;
+ rt_slist_t list;
+};
+typedef struct ulog_tag_lvl_filter *ulog_tag_lvl_filter_t;
+
+struct ulog_frame
+{
+ /* magic word is 0x10 ('lo') */
+ rt_uint32_t magic:8;
+ rt_uint32_t is_raw:1;
+ rt_uint32_t log_len:23;
+ rt_uint32_t level;
+ const char *log;
+ const char *tag;
+};
+typedef struct ulog_frame *ulog_frame_t;
+
+struct ulog_backend
+{
+ char name[RT_NAME_MAX];
+ rt_bool_t support_color;
+ void (*init) (struct ulog_backend *backend);
+ void (*output)(struct ulog_backend *backend, rt_uint32_t level, const char *tag, rt_bool_t is_raw, const char *log, size_t len);
+ void (*flush) (struct ulog_backend *backend);
+ void (*deinit)(struct ulog_backend *backend);
+ rt_slist_t list;
+};
+typedef struct ulog_backend *ulog_backend_t;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ULOG_DEF_H_ */
diff --git a/components/utilities/utest/SConscript b/components/utilities/utest/SConscript
new file mode 100644
index 0000000000..dc77bc2591
--- /dev/null
+++ b/components/utilities/utest/SConscript
@@ -0,0 +1,8 @@
+from building import *
+
+cwd = GetCurrentDir()
+src = Glob('*.c')
+CPPPATH = [cwd]
+group = DefineGroup('utest', src, depend = ['RT_USING_UTEST'], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c
new file mode 100644
index 0000000000..6500ae2fbe
--- /dev/null
+++ b/components/utilities/utest/utest.c
@@ -0,0 +1,379 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-11-19 MurphyZhao the first version
+ */
+
+#include <rtthread.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "utest.h"
+#include <utest_log.h>
+
+#undef DBG_TAG
+#undef DBG_LVL
+
+#define DBG_TAG "utest"
+#ifdef UTEST_DEBUG
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_INFO
+#endif
+#include <rtdbg.h>
+
+#if RT_CONSOLEBUF_SIZE < 256
+#error "RT_CONSOLEBUF_SIZE is less than 256!"
+#endif
+
+#ifdef UTEST_THR_STACK_SIZE
+#define UTEST_THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
+#else
+#define UTEST_THREAD_STACK_SIZE (4096)
+#endif
+
+#ifdef UTEST_THR_PRIORITY
+#define UTEST_THREAD_PRIORITY UTEST_THR_PRIORITY
+#else
+#define UTEST_THREAD_PRIORITY FINSH_THREAD_PRIORITY
+#endif
+
+static rt_uint8_t utest_log_lv = UTEST_LOG_ALL;
+static utest_tc_export_t tc_table = RT_NULL;
+static rt_size_t tc_num;
+static rt_uint32_t tc_loop;
+static struct utest local_utest = {UTEST_PASSED, 0, 0};
+
+#if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
+#pragma section="UtestTcTab"
+#endif
+
+void utest_log_lv_set(rt_uint8_t lv)
+{
+ if (lv == UTEST_LOG_ALL || lv == UTEST_LOG_ASSERT)
+ {
+ utest_log_lv = lv;
+ }
+}
+
+int utest_init(void)
+{
+ /* initialize the utest commands table.*/
+#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM C Compiler */
+ extern const int UtestTcTab$$Base;
+ extern const int UtestTcTab$$Limit;
+ tc_table = (utest_tc_export_t)&UtestTcTab$$Base;
+ tc_num = (utest_tc_export_t)&UtestTcTab$$Limit - tc_table;
+#elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
+ tc_table = (utest_tc_export_t)__section_begin("UtestTcTab");
+ tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table;
+#elif defined (__GNUC__) /* for GCC Compiler */
+ extern const int __rt_utest_tc_tab_start;
+ extern const int __rt_utest_tc_tab_end;
+ tc_table = (utest_tc_export_t)&__rt_utest_tc_tab_start;
+ tc_num = (utest_tc_export_t) &__rt_utest_tc_tab_end - tc_table;
+#endif
+
+ LOG_I("utest is initialize success.");
+ LOG_I("total utest testcase num: (%d)", tc_num);
+ return tc_num;
+}
+INIT_COMPONENT_EXPORT(utest_init);
+
+static void utest_tc_list(void)
+{
+ rt_size_t i = 0;
+
+ LOG_I("Commands list : ");
+
+ for (i = 0; i < tc_num; i++)
+ {
+ LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout);
+ }
+}
+MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_list, output all utest testcase);
+
+static const char *file_basename(const char *file)
+{
+ char *end_ptr = RT_NULL;
+ char *rst = RT_NULL;
+
+ if (!((end_ptr = strrchr(file, '\\')) != RT_NULL || \
+ (end_ptr = strrchr(file, '/')) != RT_NULL) || \
+ (rt_strlen(file) < 2))
+ {
+ rst = (char *)file;
+ }
+ else
+ {
+ rst = (char *)(end_ptr + 1);
+ }
+ return (const char *)rst;
+}
+
+static int utest_help(void)
+{
+ rt_kprintf("\n");
+ rt_kprintf("Command: utest_run\n");
+ rt_kprintf(" info: Execute test cases.\n");
+ rt_kprintf(" format: utest_run [-thread or -help] [testcase name] [loop num]\n");
+ rt_kprintf(" usage:\n");
+ rt_kprintf(" 1. utest_run\n");
+ rt_kprintf(" Do not specify a test case name. Run all test cases.\n");
+ rt_kprintf(" 2. utest_run -thread\n");
+ rt_kprintf(" Do not specify a test case name. Run all test cases in threaded mode.\n");
+ rt_kprintf(" 3. utest_run testcaseA\n");
+ rt_kprintf(" Run 'testcaseA'.\n");
+ rt_kprintf(" 4. utest_run testcaseA 10\n");
+ rt_kprintf(" Run 'testcaseA' ten times.\n");
+ rt_kprintf(" 5. utest_run -thread testcaseA\n");
+ rt_kprintf(" Run 'testcaseA' in threaded mode.\n");
+ rt_kprintf(" 6. utest_run -thread testcaseA 10\n");
+ rt_kprintf(" Run 'testcaseA' ten times in threaded mode.\n");
+ rt_kprintf(" 7. utest_run test*\n");
+ rt_kprintf(" support '*' wildcard. Run all test cases starting with 'test'.\n");
+ rt_kprintf(" 8. utest_run -help\n");
+ rt_kprintf(" Show utest help information\n");
+ rt_kprintf("\n");
+ return 0;
+}
+
+static void utest_run(const char *utest_name)
+{
+ rt_size_t i;
+ rt_uint32_t index;
+ rt_bool_t is_find;
+
+ rt_thread_mdelay(1000);
+
+ for (index = 0; index < tc_loop; index ++)
+ {
+ i = 0;
+ is_find = RT_FALSE;
+ LOG_I("[==========] [ utest ] loop %d/%d", index + 1, tc_loop);
+ LOG_I("[==========] [ utest ] started");
+ while(i < tc_num)
+ {
+ if (utest_name)
+ {
+ int len = strlen(utest_name);
+ if (utest_name[len - 1] == '*')
+ {
+ len -= 1;
+ }
+ if (rt_memcmp(tc_table[i].name, utest_name, len) != 0)
+ {
+ i++;
+ continue;
+ }
+ }
+ is_find = RT_TRUE;
+
+ LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
+ if (tc_table[i].init != RT_NULL)
+ {
+ if (tc_table[i].init() != RT_EOK)
+ {
+ LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
+ goto __tc_continue;
+ }
+ }
+
+ if (tc_table[i].tc != RT_NULL)
+ {
+ tc_table[i].tc();
+ if (local_utest.failed_num == 0)
+ {
+ LOG_I("[ PASSED ] [ result ] testcase (%s)", tc_table[i].name);
+ }
+ else
+ {
+ LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
+ }
+ }
+ else
+ {
+ LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
+ }
+
+ if (tc_table[i].cleanup != RT_NULL)
+ {
+ if (tc_table[i].cleanup() != RT_EOK)
+ {
+ LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name);
+ goto __tc_continue;
+ }
+ }
+
+ __tc_continue:
+ LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name);
+
+ i++;
+ }
+
+ if (i == tc_num && is_find == RT_FALSE && utest_name != RT_NULL)
+ {
+ LOG_I("[==========] [ utest ] Not find (%s)", utest_name);
+ LOG_I("[==========] [ utest ] finished");
+ break;
+ }
+
+ LOG_I("[==========] [ utest ] finished");
+ }
+}
+
+static void utest_testcase_run(int argc, char** argv)
+{
+ void *thr_param = RT_NULL;
+
+ static char utest_name[UTEST_NAME_MAX_LEN];
+ rt_memset(utest_name, 0x0, sizeof(utest_name));
+
+ tc_loop = 1;
+
+ if (argc == 1)
+ {
+ utest_run(RT_NULL);
+ return;
+ }
+ else if (argc == 2 || argc == 3 || argc == 4)
+ {
+ if (rt_strcmp(argv[1], "-thread") == 0)
+ {
+ rt_thread_t tid = RT_NULL;
+ if (argc == 3 || argc == 4)
+ {
+ rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
+ thr_param = (void*)utest_name;
+
+ if (argc == 4) tc_loop = atoi(argv[3]);
+ }
+ tid = rt_thread_create("utest",
+ (void (*)(void *))utest_run, thr_param,
+ UTEST_THREAD_STACK_SIZE, UTEST_THREAD_PRIORITY, 10);
+ if (tid != NULL)
+ {
+ rt_thread_startup(tid);
+ }
+ }
+ else if (rt_strcmp(argv[1], "-help") == 0)
+ {
+ utest_help();
+ }
+ else
+ {
+ rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
+ if (argc == 3) tc_loop = atoi(argv[2]);
+ utest_run(utest_name);
+ }
+ }
+ else
+ {
+ LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__);
+ utest_help();
+ }
+}
+MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num]);
+
+utest_t utest_handle_get(void)
+{
+ return (utest_t)&local_utest;
+}
+
+void utest_unit_run(test_unit_func func, const char *unit_func_name)
+{
+ // LOG_I("[==========] utest unit name: (%s)", unit_func_name);
+ local_utest.error = UTEST_PASSED;
+ local_utest.passed_num = 0;
+ local_utest.failed_num = 0;
+
+ if (func != RT_NULL)
+ {
+ func();
+ }
+}
+
+void utest_assert(int value, const char *file, int line, const char *func, const char *msg)
+{
+ if (!(value))
+ {
+ local_utest.error = UTEST_FAILED;
+ local_utest.failed_num ++;
+ LOG_E("[ ASSERT ] [ unit ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
+ }
+ else
+ {
+ if (utest_log_lv == UTEST_LOG_ALL)
+ {
+ LOG_D("[ OK ] [ unit ] (%s:%d) is passed", func, line);
+ }
+ local_utest.error = UTEST_PASSED;
+ local_utest.passed_num ++;
+ }
+}
+
+void utest_assert_string(const char *a, const char *b, rt_bool_t equal, const char *file, int line, const char *func, const char *msg)
+{
+ if (a == RT_NULL || b == RT_NULL)
+ {
+ utest_assert(0, file, line, func, msg);
+ }
+
+ if (equal)
+ {
+ if (rt_strcmp(a, b) == 0)
+ {
+ utest_assert(1, file, line, func, msg);
+ }
+ else
+ {
+ utest_assert(0, file, line, func, msg);
+ }
+ }
+ else
+ {
+ if (rt_strcmp(a, b) == 0)
+ {
+ utest_assert(0, file, line, func, msg);
+ }
+ else
+ {
+ utest_assert(1, file, line, func, msg);
+ }
+ }
+}
+
+void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equal, const char *file, int line, const char *func, const char *msg)
+{
+ if (a == RT_NULL || b == RT_NULL)
+ {
+ utest_assert(0, file, line, func, msg);
+ }
+
+ if (equal)
+ {
+ if (rt_memcmp(a, b, sz) == 0)
+ {
+ utest_assert(1, file, line, func, msg);
+ }
+ else
+ {
+ utest_assert(0, file, line, func, msg);
+ }
+ }
+ else
+ {
+ if (rt_memcmp(a, b, sz) == 0)
+ {
+ utest_assert(0, file, line, func, msg);
+ }
+ else
+ {
+ utest_assert(1, file, line, func, msg);
+ }
+ }
+}
diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h
new file mode 100644
index 0000000000..2c91bfe0e1
--- /dev/null
+++ b/components/utilities/utest/utest.h
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-11-19 MurphyZhao the first version
+ */
+
+#ifndef __UTEST_H__
+#define __UTEST_H__
+
+#include <rtthread.h>
+#include "utest_log.h"
+#include "utest_assert.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * utest_error
+ *
+ * @brief Test result.
+ *
+ * @member UTEST_PASSED Test success.
+ * @member UTEST_FAILED Test failed.
+ * @member UTEST_PASSED Test skipped.
+ *
+*/
+enum utest_error
+{
+ UTEST_PASSED = 0,
+ UTEST_FAILED = 1,
+ UTEST_SKIPPED = 2
+};
+typedef enum utest_error utest_err_e;
+
+/**
+ * utest
+ *
+ * @brief utest data structure.
+ *
+ * @member error Error number from enum `utest_error`.
+ * @member passed_num Total number of tests passed.
+ * @member failed_num Total number of tests failed.
+ *
+*/
+struct utest
+{
+ utest_err_e error;
+ uint32_t passed_num;
+ uint32_t failed_num;
+};
+typedef struct utest *utest_t;
+
+/**
+ * utest_tc_export
+ *
+ * @brief utest testcase data structure.
+ * Will export the data to `UtestTcTab` section in flash.
+ *
+ * @member name Testcase name.
+ * @member run_timeout Testcase maximum test time (Time unit: seconds).
+ * @member init Necessary initialization before executing the test case function.
+ * @member tc Total number of tests failed.
+ * @member cleanup Total number of tests failed.
+ *
+*/
+struct utest_tc_export {
+ const char *name;
+ uint32_t run_timeout;
+ rt_err_t (*init)(void);
+ void (*tc)(void);
+ rt_err_t (*cleanup)(void);
+};
+typedef struct utest_tc_export *utest_tc_export_t;
+
+/**
+ * test_unit_func
+ *
+ * @brief Unit test handler function pointer.
+ *
+*/
+typedef void (*test_unit_func)(void);
+
+/**
+ * utest_unit_run
+ *
+ * @brief Unit test function executor.
+ * No need for the user to call this function directly
+ *
+ * @param func Unit test function.
+ * @param unit_func_name Unit test function name.
+ *
+ * @return void
+ *
+*/
+void utest_unit_run(test_unit_func func, const char *unit_func_name);
+
+/**
+ * utest_handle_get
+ *
+ * @brief Get the utest data structure handle.
+ * No need for the user to call this function directly
+ *
+ * @param void
+ *
+ * @return utest_t type. (struct utest *)
+ *
+*/
+utest_t utest_handle_get(void);
+
+/**
+ * UTEST_NAME_MAX_LEN
+ *
+ * @brief Testcase name maximum length.
+ *
+*/
+#define UTEST_NAME_MAX_LEN (128u)
+
+/**
+ * UTEST_TC_EXPORT
+ *
+ * @brief Export testcase function to `UtestTcTab` section in flash.
+ * Used in application layer.
+ *
+ * @param testcase The testcase function.
+ * @param name The testcase name.
+ * @param init The initialization function of the test case.
+ * @param cleanup The cleanup function of the test case.
+ * @param timeout Testcase maximum test time (Time unit: seconds).
+ *
+ * @return None
+ *
+*/
+#define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \
+ RT_USED static const struct utest_tc_export _utest_testcase \
+ SECTION("UtestTcTab") = \
+ { \
+ name, \
+ timeout, \
+ init, \
+ testcase, \
+ cleanup \
+ }
+
+/**
+ * UTEST_UNIT_RUN
+ *
+ * @brief Unit test function executor.
+ * Used in `testcase` function in application.
+ *
+ * @param test_unit_func Unit test function
+ *
+ * @return None
+ *
+*/
+#define UTEST_UNIT_RUN(test_unit_func) \
+ utest_unit_run(test_unit_func, #test_unit_func); \
+ if(utest_handle_get()->failed_num != 0) return;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __UTEST_H__ */
diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h
new file mode 100644
index 0000000000..d83279272e
--- /dev/null
+++ b/components/utilities/utest/utest_assert.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-11-19 MurphyZhao the first version
+ */
+
+#ifndef __UTEST_ASSERT_H__
+#define __UTEST_ASSERT_H__
+
+#include "utest.h"
+#include <rtthread.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* No need for the user to use this function directly */
+void utest_assert(int value, const char *file, int line, const char *func, const char *msg);
+
+/* No need for the user to use this function directly */
+void utest_assert_string(const char *a, const char *b, rt_bool_t equal, const char *file, int line, const char *func, const char *msg);
+void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equal, const char *file, int line, const char *func, const char *msg);
+
+/* No need for the user to use this macro directly */
+#define __utest_assert(value, msg) utest_assert(value, __FILE__, __LINE__, __func__, msg)
+
+/**
+ * uassert_x macros
+ *
+ * @brief Get the utest data structure handle.
+ * No need for the user to call this function directly.
+ *
+ * @macro uassert_true if @value is true, not assert, means passing.
+ * @macro uassert_false if @value is false, not assert, means passing.
+ * @macro uassert_null if @value is null, not assert, means passing.
+ * @macro uassert_not_null if @value is not null, not assert, means passing.
+ * @macro uassert_int_equal if @a equal to @b, not assert, means passing. Integer type test.
+ * @macro uassert_int_not_equal if @a not equal to @b, not assert, means passing. Integer type test.
+ * @macro uassert_str_equal if @a equal to @b, not assert, means passing. String type test.
+ * @macro uassert_str_not_equal if @a not equal to @b, not assert, means passing. String type test.
+ * @macro uassert_buf_equal if @a equal to @b, not assert, means passing. buf type test.
+ * @macro uassert_buf_not_equal if @a not equal to @b, not assert, means passing. buf type test.
+ * @macro uassert_in_range if @value is in range of min and max, not assert, means passing.
+ * @macro uassert_not_in_range if @value is not in range of min and max, not assert, means passing.
+ *
+*/
+#define uassert_true(value) __utest_assert(value, "(" #value ") is false")
+#define uassert_false(value) __utest_assert(!(value), "(" #value ") is true")
+#define uassert_null(value) __utest_assert((const char *)(value) == NULL, "(" #value ") is not null")
+#define uassert_not_null(value) __utest_assert((const char *)(value) != NULL, "(" #value ") is null")
+
+#define uassert_int_equal(a, b) __utest_assert((a) == (b), "(" #a ") not equal to (" #b ")")
+#define uassert_int_not_equal(a, b) __utest_assert((a) != (b), "(" #a ") equal to (" #b ")")
+
+#define uassert_str_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_TRUE, __FILE__, __LINE__, __func__, "string not equal")
+#define uassert_str_not_equal(a, b) utest_assert_string((const char*)(a), (const char*)(b), RT_FALSE, __FILE__, __LINE__, __func__, "string equal")
+
+#define uassert_buf_equal(a, b, sz) utest_assert_buf((const char*)(a), (const char*)(b), (sz), RT_TRUE, __FILE__, __LINE__, __func__, "buf not equal")
+#define uassert_buf_not_equal(a, b, sz) utest_assert_buf((const char*)(a), (const char*)(b), (sz), RT_FALSE, __FILE__, __LINE__, __func__, "buf equal")
+
+#define uassert_in_range(value, min, max) __utest_assert(((value >= min) && (value <= max)), "(" #value ") not in range("#min","#max")")
+#define uassert_not_in_range(value, min, max) __utest_assert(!((value >= min) && (value <= max)), "(" #value ") in range("#min","#max")")
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __UTEST_ASSERT_H__ */
diff --git a/components/utilities/utest/utest_log.h b/components/utilities/utest/utest_log.h
new file mode 100644
index 0000000000..0403268351
--- /dev/null
+++ b/components/utilities/utest/utest_log.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2018-11-19 MurphyZhao the first version
+ */
+
+#ifndef __UTEST_LOG_H__
+#define __UTEST_LOG_H__
+
+#include <rtthread.h>
+
+#define UTEST_DEBUG
+
+#undef DBG_TAG
+#undef DBG_LVL
+
+#define DBG_TAG "testcase"
+#ifdef UTEST_DEBUG
+#define DBG_LVL DBG_LOG
+#else
+#define DBG_LVL DBG_INFO
+#endif
+#include <rtdbg.h>
+
+#define UTEST_LOG_ALL (1u)
+#define UTEST_LOG_ASSERT (2u)
+
+void utest_log_lv_set(rt_uint8_t lv);
+
+#endif /* __UTEST_LOG_H__ */
diff --git a/components/utilities/ymodem/SConscript b/components/utilities/ymodem/SConscript
index 3fc3b17249..5be32247d6 100644
--- a/components/utilities/ymodem/SConscript
+++ b/components/utilities/ymodem/SConscript
@@ -1,8 +1,15 @@
from building import *
cwd = GetCurrentDir()
-src = Glob('*.c')
+src = Split('''
+ymodem.c
+''')
+
CPPPATH = [cwd]
+
+if GetDepend('RT_USING_DFS') and GetDepend('YMODEM_USING_FILE_TRANSFER'):
+ src += ['ry_sy.c']
+
group = DefineGroup('Utilities', src, depend = ['RT_USING_RYM'], CPPPATH = CPPPATH)
Return('group')
diff --git a/components/utilities/ymodem/ry_sy.c b/components/utilities/ymodem/ry_sy.c
new file mode 100644
index 0000000000..a87f6949db
--- /dev/null
+++ b/components/utilities/ymodem/ry_sy.c
@@ -0,0 +1,238 @@
+/*
+ * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2019-12-09 Steven Liu the first version
+ */
+
+#include <rtthread.h>
+#include <ymodem.h>
+#include <dfs_posix.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct custom_ctx
+{
+ struct rym_ctx parent;
+ int fd;
+ int flen;
+ char fpath[DFS_PATH_MAX];
+};
+
+static enum rym_code _rym_recv_begin(
+ struct rym_ctx *ctx,
+ rt_uint8_t *buf,
+ rt_size_t len)
+{
+ struct custom_ctx *cctx = (struct custom_ctx *)ctx;
+
+ cctx->fpath[0] = '/';
+ rt_strncpy(&(cctx->fpath[1]), (const char *)buf, len - 1);
+ cctx->fd = open(cctx->fpath, O_CREAT | O_WRONLY | O_TRUNC, 0);
+ if (cctx->fd < 0)
+ {
+ rt_err_t err = rt_get_errno();
+ rt_kprintf("error creating file: %d\n", err);
+ return RYM_CODE_CAN;
+ }
+ cctx->flen = atoi(1 + (const char *)buf + rt_strnlen((const char *)buf, len - 1));
+ if (cctx->flen == 0)
+ cctx->flen = -1;
+
+ return RYM_CODE_ACK;
+}
+
+static enum rym_code _rym_recv_data(
+ struct rym_ctx *ctx,
+ rt_uint8_t *buf,
+ rt_size_t len)
+{
+ struct custom_ctx *cctx = (struct custom_ctx *)ctx;
+
+ RT_ASSERT(cctx->fd >= 0);
+ if (cctx->flen == -1)
+ {
+ write(cctx->fd, buf, len);
+ }
+ else
+ {
+ int wlen = len > cctx->flen ? cctx->flen : len;
+ write(cctx->fd, buf, wlen);
+ cctx->flen -= wlen;
+ }
+
+ return RYM_CODE_ACK;
+}
+
+static enum rym_code _rym_recv_end(
+ struct rym_ctx *ctx,
+ rt_uint8_t *buf,
+ rt_size_t len)
+{
+ struct custom_ctx *cctx = (struct custom_ctx *)ctx;
+
+ RT_ASSERT(cctx->fd >= 0);
+ close(cctx->fd);
+ cctx->fd = -1;
+
+ return RYM_CODE_ACK;
+}
+
+static enum rym_code _rym_send_begin(
+ struct rym_ctx *ctx,
+ rt_uint8_t *buf,
+ rt_size_t len)
+{
+ struct custom_ctx *cctx = (struct custom_ctx *)ctx;
+ struct stat file_buf;
+ char insert_0 = '\0';
+ rt_err_t err;
+
+ cctx->fd = open(cctx->fpath, O_RDONLY);
+ if (cctx->fd < 0)
+ {
+ err = rt_get_errno();
+ rt_kprintf("error open file: %d\n", err);
+ return RYM_ERR_FILE;
+ }
+ rt_memset(buf, 0, len);
+ err = stat(cctx->fpath, &file_buf);
+ if (err != RT_EOK)
+ {
+ rt_kprintf("error open file.\n");
+ return RYM_ERR_FILE;
+ }
+ sprintf((char *)buf, "%s%c%ld", (char *) & (cctx->fpath[1]), insert_0, file_buf.st_size);
+
+ return RYM_CODE_SOH;
+}
+
+static enum rym_code _rym_send_data(
+ struct rym_ctx *ctx,
+ rt_uint8_t *buf,
+ rt_size_t len)
+{
+ struct custom_ctx *cctx = (struct custom_ctx *)ctx;
+ rt_size_t read_size;
+ int retry_read;
+
+ read_size = 0;
+ for (retry_read = 0; retry_read < 10; retry_read++)
+ {
+ read_size += read(cctx->fd, buf + read_size, len - read_size);
+ if (read_size == len)
+ break;
+ }
+
+ if (read_size < len)
+ {
+ rt_memset(buf + read_size, 0x1A, len - read_size);
+ /* stage = RYM_STAGE_FINISHING */
+ ctx->stage = 4;
+ }
+
+ return RYM_CODE_SOH;
+}
+
+static enum rym_code _rym_send_end(
+ struct rym_ctx *ctx,
+ rt_uint8_t *buf,
+ rt_size_t len)
+{
+ rt_memset(buf, 0, len);
+
+ return RYM_CODE_SOH;
+}
+
+static rt_err_t rym_download_file(rt_device_t idev)
+{
+ rt_err_t res;
+ struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
+
+ if (!ctx)
+ {
+ rt_kprintf("rt_malloc failed\n");
+ return RT_ENOMEM;
+ }
+ ctx->fd = -1;
+ RT_ASSERT(idev);
+ res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
+ _rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000);
+ rt_free(ctx);
+
+ return res;
+}
+
+static rt_err_t rym_upload_file(rt_device_t idev, const char *file_path)
+{
+ rt_err_t res = 0;
+
+ struct custom_ctx *ctx = rt_calloc(1, sizeof(*ctx));
+ if (!ctx)
+ {
+ rt_kprintf("rt_malloc failed\n");
+ return RT_ENOMEM;
+ }
+ ctx->fd = -1;
+ rt_strncpy(ctx->fpath, file_path, DFS_PATH_MAX);
+ RT_ASSERT(idev);
+ res = rym_send_on_device(&ctx->parent, idev,
+ RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
+ _rym_send_begin, _rym_send_data, _rym_send_end, 1000);
+ rt_free(ctx);
+
+ return res;
+}
+
+#ifdef RT_USING_FINSH
+#include <finsh.h>
+
+static rt_err_t ry(uint8_t argc, char **argv)
+{
+ rt_err_t res;
+ rt_device_t dev;
+
+ if (argc > 1)
+ dev = rt_device_find(argv[1]);
+ else
+ dev = rt_console_get_device();
+ if (!dev)
+ {
+ rt_kprintf("could not find device.\n");
+ return -RT_ERROR;
+ }
+ res = rym_download_file(dev);
+
+ return res;
+}
+MSH_CMD_EXPORT(ry, YMODEM Receive e.g: ry [uart0] default by console.);
+
+static rt_err_t sy(uint8_t argc, char **argv)
+{
+ rt_err_t res;
+ /* temporarily support 1 file*/
+ const char *file_path;
+ rt_device_t dev;
+
+ if (argc > 2)
+ dev = rt_device_find(argv[2]);
+ else
+ dev = rt_console_get_device();
+ if (!dev)
+ {
+ rt_kprintf("could not find device.\n");
+ return -RT_ERROR;
+ }
+ file_path = argv[1];
+ res = rym_upload_file(dev, file_path);
+
+ return res;
+}
+MSH_CMD_EXPORT(sy, YMODEM Send e.g: sy file_path [uart0] default by console.);
+
+#endif /* RT_USING_FINSH */
diff --git a/components/utilities/ymodem/ymodem.c b/components/utilities/ymodem/ymodem.c
index a1f21cf6c6..07055db615 100644
--- a/components/utilities/ymodem/ymodem.c
+++ b/components/utilities/ymodem/ymodem.c
@@ -1,16 +1,21 @@
/*
- * File : ymodem.c
- * COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd
+ * COPYRIGHT (C) 2012, Real-Thread Information Technology Ltd
+ * All rights reserved
+ *
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2013-04-14 Grissiom initial implementation
+ * 2019-12-09 Steven Liu add YMODEM send protocol
*/
#include <rthw.h>
#include "ymodem.h"
-static const rt_uint16_t ccitt_table[256] = {
+#ifdef YMODEM_USING_CRC_TABLE
+static const rt_uint16_t ccitt_table[256] =
+{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
@@ -44,7 +49,7 @@ static const rt_uint16_t ccitt_table[256] = {
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
-rt_uint16_t CRC16(unsigned char *q, int len)
+static rt_uint16_t CRC16(unsigned char *q, int len)
{
rt_uint16_t crc = 0;
@@ -52,6 +57,30 @@ rt_uint16_t CRC16(unsigned char *q, int len)
crc = (crc << 8) ^ ccitt_table[((crc >> 8) ^ *q++) & 0xff];
return crc;
}
+#else
+static rt_uint16_t CRC16(unsigned char *q, int len)
+{
+ rt_uint16_t crc;
+ char i;
+
+ crc = 0;
+ while (--len >= 0)
+ {
+ crc = crc ^ (int) * q++ << 8;
+ i = 8;
+ do
+ {
+ if (crc & 0x8000)
+ crc = crc << 1 ^ 0x1021;
+ else
+ crc = crc << 1;
+ }
+ while (--i);
+ }
+
+ return (crc);
+}
+#endif
// we could only use global varible because we could not use
// rt_device_t->user_data(it is used by the serial driver)...
@@ -67,15 +96,16 @@ static rt_err_t _rym_rx_ind(rt_device_t dev, rt_size_t size)
#define _RYM_STX_PKG_SZ (1+2+1024+2)
static enum rym_code _rym_read_code(
- struct rym_ctx *ctx,
- rt_tick_t timeout)
+ struct rym_ctx *ctx,
+ rt_tick_t timeout)
{
/* Fast path */
if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1)
return (enum rym_code)(*ctx->buf);
/* Slow path */
- do {
+ do
+ {
rt_size_t rsz;
/* No data yet, wait for one */
@@ -86,13 +116,14 @@ static enum rym_code _rym_read_code(
rsz = rt_device_read(ctx->dev, 0, ctx->buf, 1);
if (rsz == 1)
return (enum rym_code)(*ctx->buf);
- } while (1);
+ }
+ while (1);
}
/* the caller should at least alloc _RYM_STX_PKG_SZ buffer */
static rt_size_t _rym_read_data(
- struct rym_ctx *ctx,
- rt_size_t len)
+ struct rym_ctx *ctx,
+ rt_size_t len)
{
/* we should already have had the code */
rt_uint8_t *buf = ctx->buf + 1;
@@ -101,28 +132,67 @@ static rt_size_t _rym_read_data(
do
{
readlen += rt_device_read(ctx->dev,
- 0, buf+readlen, len-readlen);
+ 0, buf + readlen, len - readlen);
if (readlen >= len)
return readlen;
- } while (rt_sem_take(&ctx->sem, RYM_WAIT_CHR_TICK) == RT_EOK);
+ }
+ while (rt_sem_take(&ctx->sem, RYM_WAIT_CHR_TICK) == RT_EOK);
return readlen;
}
+static rt_err_t _rym_send_packet(
+ struct rym_ctx *ctx,
+ enum rym_code code,
+ rt_uint8_t index)
+{
+ rt_uint16_t send_crc;
+ rt_uint8_t index_inv = ~index;
+ rt_size_t writelen = 0;
+
+ send_crc = CRC16(ctx->buf + 3, _RYM_SOH_PKG_SZ - 5);
+ ctx->buf[0] = code;
+ ctx->buf[1] = index;
+ ctx->buf[2] = index_inv;
+ ctx->buf[131] = (rt_uint8_t)(send_crc >> 8);
+ ctx->buf[132] = (rt_uint8_t)send_crc & 0xff;
+
+ do
+ {
+ writelen += rt_device_write(ctx->dev, 0, ctx->buf + writelen,
+ _RYM_SOH_PKG_SZ - writelen);
+ }
+ while (writelen < _RYM_SOH_PKG_SZ);
+
+ return RT_EOK;
+}
+
static rt_size_t _rym_putchar(struct rym_ctx *ctx, rt_uint8_t code)
{
rt_device_write(ctx->dev, 0, &code, sizeof(code));
return 1;
}
+static rt_size_t _rym_getchar(struct rym_ctx *ctx)
+{
+ rt_uint8_t getc_ack;
+
+ while (rt_device_read(ctx->dev, 0, &getc_ack, 1) != 1)
+ {
+ rt_sem_take(&ctx->sem, RT_WAITING_FOREVER);
+ }
+ return getc_ack;
+}
+
static rt_err_t _rym_do_handshake(
- struct rym_ctx *ctx,
- int tm_sec)
+ struct rym_ctx *ctx,
+ int tm_sec)
{
enum rym_code code;
rt_size_t i;
rt_uint16_t recv_crc, cal_crc;
rt_size_t data_sz;
+ rt_tick_t tick;
ctx->stage = RYM_STAGE_ESTABLISHING;
/* send C every second, so the sender could know we are waiting for it. */
@@ -130,13 +200,13 @@ static rt_err_t _rym_do_handshake(
{
_rym_putchar(ctx, RYM_CODE_C);
code = _rym_read_code(ctx,
- RYM_CHD_INTV_TICK);
+ RYM_CHD_INTV_TICK);
if (code == RYM_CODE_SOH)
{
data_sz = _RYM_SOH_PKG_SZ;
break;
}
- else if(code == RYM_CODE_STX)
+ else if (code == RYM_CODE_STX)
{
data_sz = _RYM_STX_PKG_SZ;
break;
@@ -147,32 +217,96 @@ static rt_err_t _rym_do_handshake(
return -RYM_ERR_TMO;
}
- i = _rym_read_data(ctx, data_sz-1);
- if (i != (data_sz-1))
+ /* receive all data */
+ i = 0;
+ /* automatic exit after receiving specified length data, timeout: 100ms */
+ tick = rt_tick_get();
+ while (rt_tick_get() <= (tick + rt_tick_from_millisecond(100)) && i < (data_sz - 1))
+ {
+ i += _rym_read_data(ctx, data_sz - 1);
+ rt_thread_mdelay(5);
+ }
+
+ if (i != (data_sz - 1))
return -RYM_ERR_DSZ;
/* sanity check */
if (ctx->buf[1] != 0 || ctx->buf[2] != 0xFF)
return -RYM_ERR_SEQ;
- recv_crc = (rt_uint16_t)(*(ctx->buf+data_sz-2) << 8) | *(ctx->buf+data_sz-1);
- cal_crc = CRC16(ctx->buf+3, data_sz-5);
+ recv_crc = (rt_uint16_t)(*(ctx->buf + data_sz - 2) << 8) | *(ctx->buf + data_sz - 1);
+ cal_crc = CRC16(ctx->buf + 3, data_sz - 5);
if (recv_crc != cal_crc)
return -RYM_ERR_CRC;
/* congratulations, check passed. */
- if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf+3, data_sz-5) != RYM_CODE_ACK)
+ if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_ACK)
return -RYM_ERR_CAN;
return RT_EOK;
}
+static rt_err_t _rym_do_send_handshake(
+ struct rym_ctx *ctx,
+ int tm_sec)
+{
+ enum rym_code code;
+ rt_size_t i;
+ rt_size_t data_sz;
+ rt_uint8_t index = 0;
+ rt_uint8_t getc_ack;
+
+ ctx->stage = RYM_STAGE_ESTABLISHING;
+ data_sz = _RYM_SOH_PKG_SZ;
+
+ /* receive C every second */
+ for (i = 0; i < tm_sec; i++)
+ {
+ code = _rym_read_code(ctx,
+ RYM_CHD_INTV_TICK);
+ if (code == RYM_CODE_C)
+ {
+ break;
+ }
+ }
+ if (i == tm_sec)
+ {
+ return -RYM_ERR_TMO;
+ }
+
+ /* congratulations, check passed. */
+ if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH)
+ return -RYM_ERR_CODE;
+
+ code = RYM_CODE_SOH;
+ _rym_send_packet(ctx, code, index);
+
+ rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind);
+ getc_ack = _rym_getchar(ctx);
+
+ if (getc_ack != RYM_CODE_ACK)
+ {
+ return -RYM_ERR_ACK;
+ }
+
+ getc_ack = _rym_getchar(ctx);
+
+ if (getc_ack != RYM_CODE_C)
+ {
+ return -RYM_ERR_ACK;
+ }
+
+ ctx->stage = RYM_STAGE_ESTABLISHED;
+
+ return RT_EOK;
+}
+
static rt_err_t _rym_trans_data(
- struct rym_ctx *ctx,
- rt_size_t data_sz,
- enum rym_code *code)
+ struct rym_ctx *ctx,
+ rt_size_t data_sz,
+ enum rym_code *code)
{
- const rt_size_t tsz = 2+data_sz+2;
+ const rt_size_t tsz = 2 + data_sz + 2;
rt_uint16_t recv_crc;
/* seq + data + crc */
@@ -198,13 +332,13 @@ static rt_err_t _rym_trans_data(
ctx->stage = RYM_STAGE_TRANSMITTING;
/* sanity check */
- recv_crc = (rt_uint16_t)(*(ctx->buf+tsz-1) << 8) | *(ctx->buf+tsz);
- if (recv_crc != CRC16(ctx->buf+3, data_sz))
+ recv_crc = (rt_uint16_t)(*(ctx->buf + tsz - 1) << 8) | *(ctx->buf + tsz);
+ if (recv_crc != CRC16(ctx->buf + 3, data_sz))
return -RYM_ERR_CRC;
/* congratulations, check passed. */
if (ctx->on_data)
- *code = ctx->on_data(ctx, ctx->buf+3, data_sz);
+ *code = ctx->on_data(ctx, ctx->buf + 3, data_sz);
else
*code = RYM_CODE_ACK;
return RT_EOK;
@@ -223,7 +357,7 @@ static rt_err_t _rym_do_trans(struct rym_ctx *ctx)
rt_size_t data_sz, i;
code = _rym_read_code(ctx,
- RYM_WAIT_PKG_TICK);
+ RYM_WAIT_PKG_TICK);
switch (code)
{
case RYM_CODE_SOH:
@@ -245,7 +379,8 @@ static rt_err_t _rym_do_trans(struct rym_ctx *ctx)
{
case RYM_CODE_CAN:
/* the spec require multiple CAN */
- for (i = 0; i < RYM_END_SESSION_SEND_CAN_NUM; i++) {
+ for (i = 0; i < RYM_END_SESSION_SEND_CAN_NUM; i++)
+ {
_rym_putchar(ctx, RYM_CODE_CAN);
}
return -RYM_ERR_CAN;
@@ -259,17 +394,53 @@ static rt_err_t _rym_do_trans(struct rym_ctx *ctx)
}
}
+static rt_err_t _rym_do_send_trans(struct rym_ctx *ctx)
+{
+ ctx->stage = RYM_STAGE_TRANSMITTING;
+ enum rym_code code;
+ rt_size_t data_sz;
+ rt_uint32_t index = 1;
+ rt_uint8_t getc_ack;
+
+ data_sz = _RYM_SOH_PKG_SZ;
+
+ while (1)
+ {
+ if (ctx->on_data && ctx->on_data(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH)
+ return -RYM_ERR_CODE;
+
+ code = RYM_CODE_SOH;
+
+ _rym_send_packet(ctx, code, index);
+ index++;
+ rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind);
+
+ getc_ack = _rym_getchar(ctx);
+
+ if (getc_ack != RYM_CODE_ACK)
+ {
+ return -RYM_ERR_ACK;
+ }
+
+ if (ctx->stage == RYM_STAGE_FINISHING)
+ break;
+ }
+
+ return RT_EOK;
+}
+
static rt_err_t _rym_do_fin(struct rym_ctx *ctx)
{
enum rym_code code;
rt_uint16_t recv_crc;
rt_size_t i;
+ rt_size_t data_sz;
ctx->stage = RYM_STAGE_FINISHING;
/* we already got one EOT in the caller. invoke the callback if there is
* one. */
if (ctx->on_end)
- ctx->on_end(ctx, ctx->buf+3, 128);
+ ctx->on_end(ctx, ctx->buf + 3, 128);
_rym_putchar(ctx, RYM_CODE_NAK);
code = _rym_read_code(ctx, RYM_WAIT_PKG_TICK);
@@ -280,24 +451,38 @@ static rt_err_t _rym_do_fin(struct rym_ctx *ctx)
_rym_putchar(ctx, RYM_CODE_C);
code = _rym_read_code(ctx, RYM_WAIT_PKG_TICK);
- if (code != RYM_CODE_SOH)
+ if (code == RYM_CODE_SOH)
+ {
+ data_sz = _RYM_SOH_PKG_SZ;
+ }
+ else if (code == RYM_CODE_STX)
+ {
+ data_sz = _RYM_STX_PKG_SZ;
+ }
+ else
return -RYM_ERR_CODE;
- i = _rym_read_data(ctx, _RYM_SOH_PKG_SZ-1);
- if (i != (_RYM_SOH_PKG_SZ-1))
+ i = _rym_read_data(ctx, _RYM_SOH_PKG_SZ - 1);
+ if (i != (_RYM_SOH_PKG_SZ - 1))
return -RYM_ERR_DSZ;
/* sanity check
- *
- * TODO: multiple files transmission
*/
if (ctx->buf[1] != 0 || ctx->buf[2] != 0xFF)
return -RYM_ERR_SEQ;
- recv_crc = (rt_uint16_t)(*(ctx->buf+_RYM_SOH_PKG_SZ-2) << 8) | *(ctx->buf+_RYM_SOH_PKG_SZ-1);
- if (recv_crc != CRC16(ctx->buf+3, _RYM_SOH_PKG_SZ-5))
+ recv_crc = (rt_uint16_t)(*(ctx->buf + _RYM_SOH_PKG_SZ - 2) << 8) | *(ctx->buf + _RYM_SOH_PKG_SZ - 1);
+ if (recv_crc != CRC16(ctx->buf + 3, _RYM_SOH_PKG_SZ - 5))
return -RYM_ERR_CRC;
+ /*next file transmission*/
+ if (ctx->buf[3] != 0)
+ {
+ if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_ACK)
+ return -RYM_ERR_CAN;
+ return RT_EOK;
+ }
+
/* congratulations, check passed. */
ctx->stage = RYM_STAGE_FINISHED;
@@ -307,9 +492,54 @@ static rt_err_t _rym_do_fin(struct rym_ctx *ctx)
return RT_EOK;
}
+static rt_err_t _rym_do_send_fin(struct rym_ctx *ctx)
+{
+ enum rym_code code;
+ rt_size_t data_sz;
+ rt_uint8_t index = 0;
+ rt_uint8_t getc_ack;
+
+ data_sz = _RYM_SOH_PKG_SZ;
+ rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind);
+
+ _rym_putchar(ctx, RYM_CODE_EOT);
+ getc_ack = _rym_getchar(ctx);
+
+ if (getc_ack != RYM_CODE_NAK)
+ {
+ return -RYM_ERR_ACK;
+ }
+
+ _rym_putchar(ctx, RYM_CODE_EOT);
+ getc_ack = _rym_getchar(ctx);
+
+ if (getc_ack != RYM_CODE_ACK)
+ {
+ return -RYM_ERR_ACK;
+ }
+
+ getc_ack = _rym_getchar(ctx);
+
+ if (getc_ack != RYM_CODE_C)
+ {
+ return -RYM_ERR_ACK;
+ }
+
+ if (ctx->on_end && ctx->on_end(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH)
+ return -RYM_ERR_CODE;
+
+ code = RYM_CODE_SOH;
+
+ _rym_send_packet(ctx, code, index);
+
+ ctx->stage = RYM_STAGE_FINISHED;
+
+ return RT_EOK;
+}
+
static rt_err_t _rym_do_recv(
- struct rym_ctx *ctx,
- int handshake_timeout)
+ struct rym_ctx *ctx,
+ int handshake_timeout)
{
rt_err_t err;
@@ -321,23 +551,78 @@ static rt_err_t _rym_do_recv(
err = _rym_do_handshake(ctx, handshake_timeout);
if (err != RT_EOK)
+ {
+ rt_free(ctx->buf);
+ return err;
+ }
+ while (1)
+ {
+ err = _rym_do_trans(ctx);
+ if (err != RT_EOK)
+ {
+ rt_free(ctx->buf);
+ return err;
+ }
+
+ err = _rym_do_fin(ctx);
+ if (err != RT_EOK)
+ {
+ rt_free(ctx->buf);
+ return err;
+ }
+
+ if (ctx->stage == RYM_STAGE_FINISHED)
+ break;
+ }
+ rt_free(ctx->buf);
+ return err;
+}
+
+static rt_err_t _rym_do_send(
+ struct rym_ctx *ctx,
+ int handshake_timeout)
+{
+ rt_err_t err;
+
+ ctx->stage = RYM_STAGE_NONE;
+
+ ctx->buf = rt_malloc(_RYM_STX_PKG_SZ);
+ if (ctx->buf == RT_NULL)
+ return -RT_ENOMEM;
+
+ err = _rym_do_send_handshake(ctx, handshake_timeout);
+ if (err != RT_EOK)
+ {
+ rt_free(ctx->buf);
+ return err;
+ }
+
+ err = _rym_do_send_trans(ctx);
+ if (err != RT_EOK)
+ {
+ rt_free(ctx->buf);
return err;
+ }
- err = _rym_do_trans(ctx);
+ err = _rym_do_send_fin(ctx);
if (err != RT_EOK)
+ {
+ rt_free(ctx->buf);
return err;
+ }
- return _rym_do_fin(ctx);
+ rt_free(ctx->buf);
+ return err;
}
rt_err_t rym_recv_on_device(
- struct rym_ctx *ctx,
- rt_device_t dev,
- rt_uint16_t oflag,
- rym_callback on_begin,
- rym_callback on_data,
- rym_callback on_end,
- int handshake_timeout)
+ struct rym_ctx *ctx,
+ rt_device_t dev,
+ rt_uint16_t oflag,
+ rym_callback on_begin,
+ rym_callback on_data,
+ rym_callback on_end,
+ int handshake_timeout)
{
rt_err_t res;
rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size);
@@ -359,9 +644,9 @@ rt_err_t rym_recv_on_device(
int_lvl = rt_hw_interrupt_disable();
rt_device_set_rx_indicate(dev, _rym_rx_ind);
- odev_flag = dev->flag;
+ odev_flag = dev->open_flag;
/* make sure the device don't change the content. */
- dev->flag &= ~RT_DEVICE_FLAG_STREAM;
+ dev->open_flag &= ~RT_DEVICE_FLAG_STREAM;
rt_hw_interrupt_enable(int_lvl);
res = rt_device_open(dev, oflag);
@@ -378,12 +663,68 @@ __exit:
int_lvl = rt_hw_interrupt_disable();
rt_sem_detach(&ctx->sem);
- dev->flag = odev_flag;
+ dev->open_flag = odev_flag;
rt_device_set_rx_indicate(dev, odev_rx_ind);
rt_hw_interrupt_enable(int_lvl);
- rt_free(ctx->buf);
_rym_the_ctx = RT_NULL;
return res;
}
+
+rt_err_t rym_send_on_device(
+ struct rym_ctx *ctx,
+ rt_device_t dev,
+ rt_uint16_t oflag,
+ rym_callback on_begin,
+ rym_callback on_data,
+ rym_callback on_end,
+ int handshake_timeout)
+{
+ rt_err_t res = 0;
+ rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size);
+ rt_uint16_t odev_flag;
+ int int_lvl;
+
+ RT_ASSERT(_rym_the_ctx == 0);
+ _rym_the_ctx = ctx;
+
+ ctx->on_begin = on_begin;
+ ctx->on_data = on_data;
+ ctx->on_end = on_end;
+ ctx->dev = dev;
+ rt_sem_init(&ctx->sem, "rymsem", 0, RT_IPC_FLAG_FIFO);
+
+ odev_rx_ind = dev->rx_indicate;
+ /* no data should be received before the device has been fully setted up.
+ */
+ int_lvl = rt_hw_interrupt_disable();
+ rt_device_set_rx_indicate(dev, _rym_rx_ind);
+
+ odev_flag = dev->open_flag;
+ /* make sure the device don't change the content. */
+ dev->open_flag &= ~RT_DEVICE_FLAG_STREAM;
+ rt_hw_interrupt_enable(int_lvl);
+
+ res = rt_device_open(dev, oflag);
+ if (res != RT_EOK)
+ goto __exit;
+
+ res = _rym_do_send(ctx, handshake_timeout);
+
+ rt_device_close(dev);
+
+__exit:
+
+ int_lvl = rt_hw_interrupt_disable();
+ rt_sem_detach(&ctx->sem);
+
+ dev->open_flag = odev_flag;
+ rt_device_set_rx_indicate(dev, odev_rx_ind);
+ rt_hw_interrupt_enable(int_lvl);
+
+ _rym_the_ctx = RT_NULL;
+
+ return res;
+}
+
diff --git a/components/utilities/ymodem/ymodem.h b/components/utilities/ymodem/ymodem.h
index 6de5acc8fc..8419290e13 100644
--- a/components/utilities/ymodem/ymodem.h
+++ b/components/utilities/ymodem/ymodem.h
@@ -1,19 +1,25 @@
-#ifndef __YMODEM_H__
-#define __YMODEM_H__
/*
- * File : ymodem.h
- * COPYRIGHT (C) 2012, Shanghai Real-Thread Technology Co., Ltd
+ * COPYRIGHT (C) 2012, Real-Thread Information Technology Ltd
+ * All rights reserved
+ *
+ * SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2013-04-14 Grissiom initial implementation
+ * 2019-12-09 Steven Liu add YMODEM send protocol
*/
+#ifndef __YMODEM_H__
+#define __YMODEM_H__
+
#include "rtthread.h"
+#include <string.h>
/* The word "RYM" is stand for "Real-YModem". */
-enum rym_code {
+enum rym_code
+{
RYM_CODE_NONE = 0x00,
RYM_CODE_SOH = 0x01,
RYM_CODE_STX = 0x02,
@@ -41,6 +47,10 @@ enum rym_code {
#define RYM_ERR_DSZ 0x74
/* the transmission is aborted by user */
#define RYM_ERR_CAN 0x75
+/* wrong answer, wrong ACK or C */
+#define RYM_ERR_ACK 0x76
+/* transmit file invalid */
+#define RYM_ERR_FILE 0x77
/* how many ticks wait for chars between packet. */
#ifndef RYM_WAIT_CHR_TICK
@@ -60,7 +70,8 @@ enum rym_code {
#define RYM_END_SESSION_SEND_CAN_NUM 0x07
#endif
-enum rym_stage {
+enum rym_stage
+{
RYM_STAGE_NONE,
/* set when C is send */
RYM_STAGE_ESTABLISHING,
@@ -77,16 +88,15 @@ enum rym_stage {
};
struct rym_ctx;
-/* when receiving files, the buf will be the data received from ymodem protocol
+/* When receiving files, the buf will be the data received from ymodem protocol
* and the len is the data size.
*
- * TODO:
* When sending files, the len is the buf size in RYM. The callback need to
* fill the buf with data to send. Returning RYM_CODE_EOT will terminate the
* transfer and the buf will be discarded. Any other return values will cause
* the transfer continue.
*/
-typedef enum rym_code (*rym_callback)(struct rym_ctx *ctx, rt_uint8_t *buf, rt_size_t len);
+typedef enum rym_code(*rym_callback)(struct rym_ctx *ctx, rt_uint8_t *buf, rt_size_t len);
/* Currently RYM only support one transfer session(ctx) for simplicity.
*
@@ -109,7 +119,7 @@ struct rym_ctx
rt_device_t dev;
};
-/** recv a file on device dev with ymodem session ctx.
+/* recv a file on device dev with ymodem session ctx.
*
* If an error happens, you can get where it is failed from ctx->stage.
*
@@ -135,7 +145,30 @@ struct rym_ctx
* second.
*/
rt_err_t rym_recv_on_device(struct rym_ctx *ctx, rt_device_t dev, rt_uint16_t oflag,
- rym_callback on_begin, rym_callback on_data, rym_callback on_end,
- int handshake_timeout);
+ rym_callback on_begin, rym_callback on_data, rym_callback on_end,
+ int handshake_timeout);
+
+/* send a file on device dev with ymodem session ctx.
+ *
+ * If an error happens, you can get where it is failed from ctx->stage.
+ *
+ * @param on_begin The callback will be invoked when the first packet is sent.
+ * This packet often contain file names and the size of the file. It is the
+ * on_begin's responsibility to parse the basic information of the file. The
+ * on_begin can not be NULL.
+ *
+ * @param on_data The callback will be invoked when the data packets is sent.
+ * The callback should read file system and prepare the data packets. The
+ * on_data can not be NULL.
+ *
+ * @param on_end The callback will be invoked when one transmission is
+ * finished. The data should be 128 bytes of NULL. The on_end can not be NULL.
+ *
+ * @param handshake_timeout the timeout when hand shaking. The unit is in
+ * second.
+ */
+rt_err_t rym_send_on_device(struct rym_ctx *ctx, rt_device_t dev, rt_uint16_t oflag,
+ rym_callback on_begin, rym_callback on_data, rym_callback on_end,
+ int handshake_timeout);
#endif