diff options
| author | guo <[email protected]> | 2021-09-28 10:36:54 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-09-28 10:36:54 +0800 |
| commit | 55a302cd092758a44b589d65470a69d9e8ae4ced (patch) | |
| tree | e60fc53994c426007e9eb4877e5bd123f124ef39 /components/utilities | |
| parent | 00727d6b6db92dc1cf98f028f26fd6168594b905 (diff) | |
| parent | 00d42ef73cff61ecda7ff006b077a73e1a4ff669 (diff) | |
Merge branch 'master' into usbh_construct
Diffstat (limited to 'components/utilities')
34 files changed, 4036 insertions, 1430 deletions
diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index 8b733ecf5c..71869bee81 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -11,8 +11,8 @@ config RT_USING_RYM config YMODEM_USING_FILE_TRANSFER bool "Enable file transfer feature" - select RT_USING_DFS - default n + depends on RT_USING_DFS + default y endif config RT_USING_ULOG @@ -205,4 +205,6 @@ config RT_USING_UTEST default 20 endif +source "$RTT_DIR/components/utilities/rt-link/Kconfig" + endmenu diff --git a/components/utilities/rt-link/Kconfig b/components/utilities/rt-link/Kconfig new file mode 100644 index 0000000000..d20c2f01d4 --- /dev/null +++ b/components/utilities/rt-link/Kconfig @@ -0,0 +1,25 @@ +# Kconfig file for rt_link +menuconfig RT_USING_RT_LINK + bool "RT-Link" + default n + +if RT_USING_RT_LINK + choice + prompt"use hw crc device or not" + default RT_LINK_USING_SF_CRC + + config RT_LINK_USING_SF_CRC + bool "use software crc table" + config RT_LINK_USING_HW_CRC + bool "use hardware crc device" + endchoice + + menu "rt link debug option" + config USING_RT_LINK_DEBUG + bool "Enable RT-Link debug" + default n + config USING_RT_LINK_HW_DEBUG + bool "Enable RT-Link hw debug" + default n + endmenu +endif diff --git a/components/utilities/rt-link/SConscript b/components/utilities/rt-link/SConscript new file mode 100644 index 0000000000..4c815c49b8 --- /dev/null +++ b/components/utilities/rt-link/SConscript @@ -0,0 +1,15 @@ +# RT-Thread building script for bridge + +import os +from building import * + +cwd = GetCurrentDir() +objs = [] +list = os.listdir(cwd) + +for d in list: + path = os.path.join(cwd, d) + if os.path.isfile(os.path.join(path, 'SConscript')): + objs = objs + SConscript(os.path.join(d, 'SConscript')) + +Return('objs') diff --git a/components/utilities/rt-link/inc/rtlink.h b/components/utilities/rt-link/inc/rtlink.h new file mode 100644 index 0000000000..941c35a359 --- /dev/null +++ b/components/utilities/rt-link/inc/rtlink.h @@ -0,0 +1,210 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-02 xiangxistu the first version + * 2021-03-19 Sherman Streamline the struct rt_link_session + */ + +#ifndef __RT_LINK_H__ +#define __RT_LINK_H__ + +#include <rtdef.h> + +#define RT_LINK_VER "0.2.0" + +#define RT_LINK_AUTO_INIT + +#define RT_LINK_FLAG_ACK 0x01 +#define RT_LINK_FLAG_CRC 0x02 + +#define RT_LINK_FRAME_HEAD 0x15 +#define RT_LINK_FRAME_HEAD_MASK 0x1F +/* The maximum number of split frames for a long package */ +#define RT_LINK_FRAMES_MAX 0x03 +/* The length in the rt_link_frame_head structure occupies 11 bits, +so the value range after 4-byte alignment is 0-2044.*/ +#define RT_LINK_MAX_FRAME_LENGTH 1024 + +#define RT_LINK_ACK_MAX 0x07 +#define RT_LINK_CRC_LENGTH 4 +#define RT_LINK_HEAD_LENGTH 4 +#define RT_LINK_EXTEND_LENGTH 4 + +#define RT_LINK_MAX_DATA_LENGTH (RT_LINK_MAX_FRAME_LENGTH - \ + RT_LINK_HEAD_LENGTH - \ + RT_LINK_EXTEND_LENGTH - \ + RT_LINK_CRC_LENGTH) +#define RT_LINK_RECEIVE_BUFFER_LENGTH (RT_LINK_MAX_FRAME_LENGTH * \ + RT_LINK_FRAMES_MAX + \ + RT_LINK_HEAD_LENGTH + \ + RT_LINK_EXTEND_LENGTH) + +typedef enum +{ + RT_LINK_SERVICE_RTLINK = 0, + RT_LINK_SERVICE_SOCKET = 1, + RT_LINK_SERVICE_WIFI = 2, + RT_LINK_SERVICE_MNGT = 3, + RT_LINK_SERVICE_MSHTOOLS = 4, + + /* Expandable to a maximum of 31 */ + RT_LINK_SERVICE_MAX +} rt_link_service_e; + +typedef enum +{ + RT_LINK_RESEND_FRAME = 0, + RT_LINK_CONFIRM_FRAME = 1, + + RT_LINK_HANDSHAKE_FRAME = 2, + RT_LINK_DETACH_FRAME = 3, /* service is not online */ + RT_LINK_SESSION_END = 4, /* The retring failed to end the session */ + + RT_LINK_LONG_DATA_FRAME = 5, + RT_LINK_SHORT_DATA_FRAME = 6, + + RT_LINK_RESERVE_FRAME = 7 +} rt_link_frame_attr_e; + +typedef enum +{ + /* receive event */ + RT_LINK_READ_CHECK_EVENT = 1 << 0, + RT_LINK_RECV_TIMEOUT_FRAME_EVENT = 1 << 1, + RT_LINK_RECV_TIMEOUT_LONG_EVENT = 1 << 2, + + /* send event */ + RT_LINK_SEND_READY_EVENT = 1 << 4, + RT_LINK_SEND_OK_EVENT = 1 << 5, + RT_LINK_SEND_FAILED_EVENT = 1 << 6, + RT_LINK_SEND_TIMEOUT_EVENT = 1 << 7 +} rt_link_notice_e; + +typedef enum +{ + RT_LINK_INIT = 0, + RT_LINK_DISCONN = 1, + RT_LINK_CONNECT = 2, +} rt_link_linkstate_e; + +typedef enum +{ + RT_LINK_EOK = 0, + RT_LINK_ERR = 1, + RT_LINK_ETIMEOUT = 2, + RT_LINK_EFULL = 3, + RT_LINK_EEMPTY = 4, + RT_LINK_ENOMEM = 5, + RT_LINK_EIO = 6, + RT_LINK_ESESSION = 7, + RT_LINK_ESERVICE = 8, + + RT_LINK_EMAX +} rt_link_err_e; + +struct rt_link_receive_buffer +{ + /* rt-link receive data buffer */ + rt_uint8_t data[RT_LINK_RECEIVE_BUFFER_LENGTH]; + rt_uint8_t *read_point; + rt_uint8_t *write_point; + rt_uint8_t *end_point; +}; + +struct rt_link_frame_head +{ + rt_uint8_t magicid : 5; + rt_uint8_t extend : 1; + rt_uint8_t crc : 1; + rt_uint8_t ack : 1; + + rt_uint8_t sequence; + rt_uint16_t service: 5; + rt_uint16_t length : 11; /* range 0~2047 */ +}; + +/* record frame information that opposite */ +struct rt_link_record +{ + rt_uint8_t rx_seq; /* record the opposite sequence */ + rt_uint8_t total; /* the number of long frame number */ + rt_uint8_t long_count; /* long packet recv counter */ + rt_uint8_t *dataspace; /* the space of long frame */ +}; + +struct rt_link_extend +{ + rt_uint16_t attribute; /* rt_link_frame_attr_e */ + rt_uint16_t parameter; +}; + +struct rt_link_frame +{ + struct rt_link_frame_head head; /* frame head */ + struct rt_link_extend extend; /* frame extend data */ + rt_uint8_t *real_data; /* the origin data */ + rt_uint32_t crc; /* CRC result */ + + rt_uint16_t data_len; /* the length of frame length */ + rt_uint16_t attribute; /* rt_link_frame_attr_e */ + + rt_uint8_t issent; + rt_uint8_t index; /* the index frame for long frame */ + rt_uint8_t total; /* the total frame for long frame */ + + rt_slist_t slist; /* the frame will hang on the send list on session */ +}; + +struct rt_link_service +{ + rt_int32_t timeout_tx; + void (*send_cb)(struct rt_link_service *service, void *buffer); + void (*recv_cb)(struct rt_link_service *service, void *data, rt_size_t size); + void *user_data; + + rt_uint8_t flag; /* Whether to use the CRC and ACK */ + rt_link_service_e service; + rt_link_linkstate_e state; /* channel link state */ + rt_link_err_e err; +}; + +struct rt_link_session +{ + struct rt_event event; + struct rt_link_service *service[RT_LINK_SERVICE_MAX]; + + rt_uint8_t tx_seq; /* Sequence number of the send data frame */ + rt_slist_t tx_data_slist; + rt_uint8_t sendbuffer[RT_LINK_MAX_FRAME_LENGTH]; + struct rt_event sendevent; + struct rt_timer sendtimer; + + struct rt_link_record rx_record; /* the memory of receive status */ + struct rt_timer recvtimer; /* receive a frame timer for rt link */ + struct rt_timer longframetimer; /* receive long frame timer for rt link */ + + struct rt_link_receive_buffer *rx_buffer; + rt_uint32_t (*calculate_crc)(rt_uint8_t using_buffer_ring, rt_uint8_t *data, rt_size_t size); + rt_link_linkstate_e state; /* Link status */ +}; + +#define SERV_ERR_GET(service) (service->err) + +/* rtlink init and deinit, default is automatic initialization*/ +int rt_link_init(void); +rt_err_t rt_link_deinit(void); + +rt_size_t rt_link_send(struct rt_link_service *service, const void *data, rt_size_t size); + +/* rtlink service attach and detach */ +rt_err_t rt_link_service_attach(struct rt_link_service *service); +rt_err_t rt_link_service_detach(struct rt_link_service *service); + +/* Private operator function */ +struct rt_link_session *rt_link_get_scb(void); + +#endif /* __RT_LINK_H__ */ diff --git a/components/utilities/rt-link/inc/rtlink_dev.h b/components/utilities/rt-link/inc/rtlink_dev.h new file mode 100644 index 0000000000..01fd04373c --- /dev/null +++ b/components/utilities/rt-link/inc/rtlink_dev.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-06-15 Sherman the first version + */ + +#include <rtthread.h> +#include <rtlink.h> + +#define RT_LINK_RX_NONBLOCKING 0x1000 +#define RT_LINK_RX_BLOCKING 0x2000 +#define RT_LINK_TX_NONBLOCKING 0x4000 +#define RT_LINK_TX_BLOCKING 0x8000 +#define RT_LINK_DEVICE_MASK 0xf000 + +struct rtlink_recv_list +{ + void *data; + rt_size_t size; + rt_size_t index; + struct rt_slist_node list; +}; + +struct rt_link_device +{ + struct rt_device parent; + struct rt_link_service service; + struct rt_slist_node recv_head; /* recv data list, struct rtlink_recv_list */ +}; + +/* + * rtlink device register + */ +rt_err_t rt_link_dev_register(struct rt_link_device *rtlink, + const char *name, + rt_uint32_t flag, + void *data); diff --git a/components/utilities/rt-link/inc/rtlink_hw.h b/components/utilities/rt-link/inc/rtlink_hw.h new file mode 100644 index 0000000000..d0a56fe98c --- /dev/null +++ b/components/utilities/rt-link/inc/rtlink_hw.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-02 xiangxistu the first version + * 2021-07-13 Sherman add reconnect API + * + */ +#ifndef __RT_LINK_HW_H__ +#define __RT_LINK_HW_H__ + +#include <rtdef.h> + +rt_size_t rt_link_hw_recv_len(struct rt_link_receive_buffer *buffer); +void rt_link_hw_copy(rt_uint8_t *dst, rt_uint8_t *src, rt_size_t count); +void rt_link_hw_buffer_point_shift(rt_uint8_t **pointer_address, rt_size_t length); + +rt_err_t rt_link_hw_init(void); +rt_err_t rt_link_hw_deinit(void); +rt_err_t rt_link_hw_reconnect(void); +rt_size_t rt_link_hw_send(void *data, rt_size_t length); + +#endif /* _RT_LINK_PORT_INTERNAL_H_ */ diff --git a/components/utilities/rt-link/inc/rtlink_port.h b/components/utilities/rt-link/inc/rtlink_port.h new file mode 100644 index 0000000000..b22f19072a --- /dev/null +++ b/components/utilities/rt-link/inc/rtlink_port.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-02 xiangxistu the first version + * 2021-05-15 Sherman function rename + * 2021-07-13 Sherman add reconnect API + */ +#ifndef __RT_LINK_PORT_H__ +#define __RT_LINK_PORT_H__ + +#include <rtdef.h> + +/* Functions that need to be implemented at the hardware */ +rt_err_t rt_link_port_init(void); +rt_err_t rt_link_port_deinit(void); +rt_err_t rt_link_port_reconnect(void); +rt_size_t rt_link_port_send(void *data, rt_size_t length); + +#ifdef RT_LINK_USING_HW_CRC + rt_err_t rt_link_hw_crc32_init(void); + rt_err_t rt_link_hw_crc32_deinit(void); + rt_err_t rt_link_hw_crc32_reset(void); + rt_uint32_t rt_link_hw_crc32(rt_uint8_t *data, rt_size_t u32_size) +#endif + +/* Called when the hardware receives data and the data is transferred to RTLink */ +rt_size_t rt_link_hw_write_cb(void *data, rt_size_t length); + +#endif /* __RT_LINK_PORT_H__ */ diff --git a/components/utilities/rt-link/inc/rtlink_utils.h b/components/utilities/rt-link/inc/rtlink_utils.h new file mode 100644 index 0000000000..564039c390 --- /dev/null +++ b/components/utilities/rt-link/inc/rtlink_utils.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-05-15 Sherman the first version + */ +#ifndef __RT_LINK_UTILITIES_H__ +#define __RT_LINK_UTILITIES_H__ + +#include <rtthread.h> + +/* Calculate the number of '1' */ +int rt_link_utils_num1(rt_uint32_t n); + +rt_err_t rt_link_sf_crc32_reset(void); +rt_uint32_t rt_link_sf_crc32(rt_uint8_t *data, rt_size_t len); + +#endif /* __RT_LINK_UTILITIES_H__ */ diff --git a/components/utilities/rt-link/src/SConscript b/components/utilities/rt-link/src/SConscript new file mode 100644 index 0000000000..8bbb969b2a --- /dev/null +++ b/components/utilities/rt-link/src/SConscript @@ -0,0 +1,13 @@ +Import('rtconfig') +from building import * + +cwd = GetCurrentDir() +src = Glob('*.c') +CPPPATH = [cwd + '/../inc'] + +group = DefineGroup('rt-link', src, depend = ['RT_USING_RT_LINK'], CPPPATH = CPPPATH) + +if os.path.isfile(os.path.join(cwd, 'hw', 'SConscript')): + group = group + SConscript(os.path.join('hw', 'SConscript')) + +Return('group') diff --git a/components/utilities/rt-link/src/rtlink.c b/components/utilities/rt-link/src/rtlink.c new file mode 100644 index 0000000000..010083aaa5 --- /dev/null +++ b/components/utilities/rt-link/src/rtlink.c @@ -0,0 +1,1320 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-02 xiangxistu the first version + * 2021-03-19 Sherman Optimize the transfer process + * 2021-04-20 Sherman Optimize memory footprint + * 2021-05-10 Sherman Add rtlink_status MSH command; + * Optimize transmission timer Settings; + * Fix known bugs + * 2021-08-06 Sherman Add NACK, NCRC, non-blocking transmit mode; + * Add service connection status; + */ + +#include <rtthread.h> +#include <rtdevice.h> +#include <board.h> +#include <rthw.h> + +#include <rtlink.h> +#include <rtlink_hw.h> +#include <rtlink_utils.h> + +#define DBG_ENABLE +#ifdef USING_RT_LINK_DEBUG + #define DBG_LVL DBG_LOG +#else + #define DBG_LVL DBG_INFO +#endif +#define DBG_TAG "rtlink" +#define DBG_COLOR +#include <rtdbg.h> + +#ifdef RT_LINK_USING_SPI + #define RT_LINK_LONG_FRAME_TIMEOUT 50 + #define RT_LINK_SENT_FRAME_TIMEOUT 100 +#else + #define RT_LINK_LONG_FRAME_TIMEOUT 100 + #define RT_LINK_SENT_FRAME_TIMEOUT 100 +#endif /* RT_LINK_USING_SPI */ + +#define RT_LINK_RECV_DATA_SEQUENCE 0 +#define RT_LINK_INIT_FRAME_SEQENCE 129 + +#define RT_LINK_THREAD_NAME "rtlink" +#define RT_LINK_THREAD_TICK 20 +#define RT_LINK_THREAD_PRIORITY 25 +#define RT_LINK_THREAD_STACK_SIZE 1024 /* 32 bytes aligned */ + +#define RT_LINK_FRAME_SENT 1 +#define RT_LINK_FRAME_NOSEND 0 + +typedef enum +{ + FIND_FRAME_HEAD = 0, + PARSE_FRAME_HEAD = 1, + PARSE_FRAME_EXTEND = 2, + PARSE_FRAME_SEQ = 3, + CHECK_FRAME_CRC = 4, + HEADLE_FRAME_DATA = 5, +} rt_link_frame_parse_t; + +/* rtlink SCB(Session control block) */ +static struct rt_link_session *rt_link_scb = RT_NULL; +struct rt_link_session *rt_link_get_scb(void) +{ + return rt_link_scb; +} + +static rt_int16_t rt_link_check_seq(rt_uint8_t new, rt_uint8_t used) +{ + rt_int16_t compare_seq = 0; + compare_seq = new - used; + if (compare_seq < 0) + { + compare_seq = compare_seq + 256; + } + return compare_seq; +} + +static int rt_link_frame_init(struct rt_link_frame *frame, rt_uint8_t config) +{ + if (frame == RT_NULL) + { + return -RT_ERROR; + } + + /* set frame control information */ + rt_memset(&frame->head, 0, sizeof(struct rt_link_frame_head)); + if (config & RT_LINK_FLAG_CRC) + { + frame->head.crc = 1; + } + if (config & RT_LINK_FLAG_ACK) + { + frame->head.ack = 1; + } + + frame->head.magicid = RT_LINK_FRAME_HEAD; + /* frame data information */ + rt_memset(&frame->extend, 0, sizeof(struct rt_link_extend)); + frame->crc = 0; + frame->real_data = RT_NULL; + frame->data_len = 0; + frame->index = 0; + frame->total = 0; + frame->attribute = RT_LINK_RESERVE_FRAME; + frame->issent = RT_LINK_FRAME_NOSEND; + + rt_slist_init(&frame->slist); + + return RT_EOK; +} + +/* remove the sending list node*/ +static void rt_link_frame_remove(struct rt_link_service *service) +{ + RT_ASSERT(service != RT_NULL); + struct rt_link_frame *find_frame = RT_NULL; + rt_uint8_t total = 0; + rt_slist_t *tem_list = rt_slist_first(&rt_link_scb->tx_data_slist); + if (tem_list != RT_NULL) + { + do + { + find_frame = rt_container_of(tem_list, struct rt_link_frame, slist); + tem_list = rt_slist_next(tem_list); + if (find_frame->head.service == service->service) + { + if (total == 0) + { + total = find_frame->total; + } + /* The data frame order is appended to the list, + with total counting starting at 1 and index counting from 0 */ + LOG_D("remove seq(%d) serv (%d)", find_frame->head.sequence, service->service); + rt_enter_critical(); + rt_slist_remove(&rt_link_scb->tx_data_slist, &find_frame->slist); + rt_exit_critical(); + total--; + rt_memset(find_frame, 0, sizeof(struct rt_link_frame)); + rt_free(find_frame); + } + } while ((total > 0) && (tem_list != RT_NULL)); + } +} + +/* Configure the extended field of the frame */ +static rt_err_t rt_link_frame_extend_config(struct rt_link_frame *frame, rt_link_frame_attr_e attribute, rt_uint16_t parameter) +{ + frame->head.extend = 1; + frame->extend.attribute = attribute; + frame->extend.parameter = parameter; + return RT_EOK; +} + +static int rt_link_command_frame_send(rt_uint8_t serv, rt_uint8_t sequence, rt_link_frame_attr_e attribute, rt_uint16_t parameter) +{ + struct rt_link_frame command_frame = {0}; + rt_uint8_t data[sizeof(command_frame.head) + sizeof(command_frame.extend)] = {0}; + rt_uint8_t data_len = 0; + + /* command frame don't need crc and ack ability */ + rt_link_frame_init(&command_frame, RT_NULL); + data_len += sizeof(command_frame.head); + + rt_link_frame_extend_config(&command_frame, attribute, parameter); + rt_memcpy(data + data_len, &command_frame.extend, sizeof(command_frame.extend)); + data_len += sizeof(command_frame.extend); + + command_frame.head.sequence = sequence; + command_frame.head.service = serv; + rt_memcpy(data, &command_frame.head, sizeof(command_frame.head)); + + rt_link_hw_send(data, data_len); + return RT_EOK; +} + +static void rt_link_service_send_finish(rt_link_err_e err) +{ + struct rt_link_frame *frame = RT_NULL; + rt_uint8_t service = RT_LINK_SERVICE_MAX; + void *buffer = RT_NULL; + rt_slist_t *tem_list = rt_slist_first(&rt_link_scb->tx_data_slist); + if (tem_list == RT_NULL) + { + return ; + } + frame = rt_container_of(tem_list, struct rt_link_frame, slist); + if (frame) + { + service = frame->head.service; + buffer = frame->real_data - (frame->index * RT_LINK_MAX_DATA_LENGTH); + rt_link_scb->service[service]->err = err; + rt_link_scb->sendtimer.parameter = 0; + rt_link_frame_remove(rt_link_scb->service[service]); + if (rt_link_scb->service[service]->timeout_tx != RT_WAITING_NO) + { + rt_event_send(&rt_link_scb->sendevent, (0x01 << service)); + } + else + { + rt_link_scb->service[service]->send_cb(rt_link_scb->service[service], buffer); + } + } +} + +static rt_size_t frame_send(struct rt_link_frame *frame) +{ + rt_size_t length = 0; + rt_uint8_t *data = RT_NULL; + + rt_memset(rt_link_scb->sendbuffer, 0, sizeof(rt_link_scb->sendbuffer)); + data = rt_link_scb->sendbuffer; + length = RT_LINK_HEAD_LENGTH; + if (frame->head.crc) + { + length += RT_LINK_CRC_LENGTH; + } + if (frame->head.extend) + { + length += RT_LINK_EXTEND_LENGTH; + } + + length += frame->data_len; + frame->head.length = frame->data_len; + rt_memcpy(data, &frame->head, RT_LINK_HEAD_LENGTH); + data = data + RT_LINK_HEAD_LENGTH; + if (frame->head.extend) + { + rt_memcpy(data, &frame->extend, RT_LINK_EXTEND_LENGTH); + data = data + RT_LINK_EXTEND_LENGTH; + } + if (frame->attribute == RT_LINK_SHORT_DATA_FRAME || frame->attribute == RT_LINK_LONG_DATA_FRAME) + { + rt_memcpy(data, frame->real_data, frame->data_len); + data = data + frame->data_len; + } + if (frame->head.crc) + { + frame->crc = rt_link_scb->calculate_crc(RT_FALSE, rt_link_scb->sendbuffer, length - RT_LINK_CRC_LENGTH); + rt_memcpy(data, &frame->crc, RT_LINK_CRC_LENGTH); + } + + LOG_D("frame send seq(%d) len(%d) attr:(%d), crc:(0x%08x).", frame->head.sequence, length, frame->attribute, frame->crc); + return rt_link_hw_send(rt_link_scb->sendbuffer, length); +} + +/* performs data transmission */ +static rt_err_t rt_link_frame_send(rt_slist_t *slist) +{ + rt_uint8_t is_finish = RT_FALSE; + struct rt_link_frame *frame = RT_NULL; + rt_uint8_t send_max = RT_LINK_ACK_MAX; + + /* if slist is tx_data_slist, we should send all data on the slist*/ + if (slist == &rt_link_scb->tx_data_slist) + { + slist = rt_slist_next(&rt_link_scb->tx_data_slist); + } + if (slist == RT_NULL) + { + LOG_W("send data list NULL"); + return -RT_ERROR; + } + + do + { + /* get frame for send */ + frame = rt_container_of(slist, struct rt_link_frame, slist); + slist = rt_slist_next(slist); + + if (frame_send(frame) == 0) + { + rt_link_scb->service[frame->head.service]->err = RT_LINK_EIO; + goto __err; + } + frame->issent = RT_LINK_FRAME_SENT; + if ((slist == RT_NULL) || (frame->index + 1 >= frame->total)) + { + send_max = 0; + is_finish = RT_TRUE; + } + else + { + send_max >>= 1; + } + }while (send_max); + + if ((is_finish) && (frame->head.ack == 0)) + { + /* NACK frame send finish, remove after sending */ + rt_link_service_send_finish(RT_LINK_EOK); + if (slist != RT_NULL) + { + LOG_D("Continue sending"); + rt_event_send(&rt_link_scb->event, RT_LINK_SEND_READY_EVENT); + } + } + else + { + rt_int32_t timeout = RT_LINK_SENT_FRAME_TIMEOUT; + rt_timer_control(&rt_link_scb->sendtimer, RT_TIMER_CTRL_SET_TIME, &timeout); + rt_timer_start(&rt_link_scb->sendtimer); + } + return RT_EOK; +__err: + return -RT_ERROR; +} + +static void _stop_recv_long(void) +{ + rt_timer_stop(&rt_link_scb->longframetimer); + if (rt_link_scb->rx_record.dataspace != RT_NULL) + { + rt_free(rt_link_scb->rx_record.dataspace); + rt_link_scb->rx_record.dataspace = RT_NULL; + } + rt_link_scb->rx_record.long_count = 0; + rt_link_scb->rx_record.total = 0; +} + +static rt_err_t rt_link_frame_stop_receive(struct rt_link_frame *frame) +{ + rt_memset(frame, 0, sizeof(struct rt_link_frame)); + if (rt_link_scb->rx_record.dataspace) + { + rt_free(rt_link_scb->rx_record.dataspace); + } + rt_link_scb->rx_record.dataspace = RT_NULL; + rt_link_scb->rx_record.long_count = 0; + rt_link_scb->rx_record.total = 0; + + rt_timer_stop(&rt_link_scb->recvtimer); + rt_link_scb->recvtimer.parameter = 0; + return RT_EOK; +} + +static rt_err_t rt_link_resend_handle(struct rt_link_frame *receive_frame) +{ + struct rt_link_frame *find_frame = RT_NULL; + rt_slist_t *tem_list = RT_NULL; + + tem_list = rt_slist_first(&rt_link_scb->tx_data_slist); + while (tem_list != RT_NULL) + { + find_frame = rt_container_of(tem_list, struct rt_link_frame, slist); + if (find_frame->head.sequence == receive_frame->head.sequence) + { + LOG_D("resend frame(%d)", find_frame->head.sequence); + frame_send(find_frame); + break; + } + tem_list = tem_list->next; + } + + if (tem_list == RT_NULL) + { + LOG_D("frame resent failed, can't find(%d).", receive_frame->head.sequence); + rt_link_command_frame_send(receive_frame->head.service, + receive_frame->head.sequence, + RT_LINK_SESSION_END, RT_NULL); + } + return RT_EOK; +} + +static rt_err_t rt_link_confirm_handle(struct rt_link_frame *receive_frame) +{ + static struct rt_link_frame *send_frame = RT_NULL; + rt_slist_t *tem_list = RT_NULL; + rt_uint16_t seq_offset = 0; + LOG_D("confirm seq(%d) frame", receive_frame->head.sequence); + + rt_timer_stop(&rt_link_scb->sendtimer); + + if (rt_link_scb->service[receive_frame->head.service] != RT_NULL) + { + rt_link_scb->service[receive_frame->head.service]->state = RT_LINK_CONNECT; + } + + if (rt_link_scb->state != RT_LINK_CONNECT) + { + /* The handshake success and resends the data frame */ + rt_link_scb->state = RT_LINK_CONNECT; + if (rt_slist_first(&rt_link_scb->tx_data_slist)) + { + rt_event_send(&rt_link_scb->event, RT_LINK_SEND_READY_EVENT); + } + return RT_EOK; + } + + /* Check to see if the frame is send for confirm */ + tem_list = rt_slist_first(&rt_link_scb->tx_data_slist); + if (tem_list == RT_NULL) + { + return -RT_ERROR; + } + send_frame = rt_container_of(tem_list, struct rt_link_frame, slist); + seq_offset = rt_link_check_seq(receive_frame->head.sequence, send_frame->head.sequence); + if (seq_offset <= send_frame->total) + { + rt_link_service_send_finish(RT_LINK_EOK); + rt_link_scb->state = RT_LINK_CONNECT; + + tem_list = rt_slist_first(&rt_link_scb->tx_data_slist); + if (tem_list != RT_NULL) + { + LOG_D("Continue sending"); + rt_event_send(&rt_link_scb->event, RT_LINK_SEND_READY_EVENT); + } + } + return RT_EOK; +} + +/* serv type rt_link_service_e */ +static void rt_link_recv_finish(rt_uint8_t serv, void *data, rt_size_t size) +{ + if (rt_link_scb->service[serv] == RT_NULL) + { + rt_link_command_frame_send(serv, 0, RT_LINK_DETACH_FRAME, RT_NULL); + return; + } + + if (rt_link_scb->service[serv]->recv_cb == RT_NULL) + { + rt_free(data); + LOG_W("service %d haven't been registered.", serv); + } + else + { + rt_link_scb->service[serv]->recv_cb(rt_link_scb->service[serv], data, size); + } +} + +static rt_err_t rt_link_short_handle(struct rt_link_frame *receive_frame) +{ + LOG_D("Seq(%d) short data", receive_frame->head.sequence); + rt_link_scb->rx_record.dataspace = rt_malloc(receive_frame->data_len); + if (rt_link_scb->rx_record.dataspace != RT_NULL) + { + if (receive_frame->head.ack) + { + rt_link_command_frame_send(receive_frame->head.service, + receive_frame->head.sequence, + RT_LINK_CONFIRM_FRAME, RT_NULL); + } + rt_link_scb->rx_record.rx_seq = receive_frame->head.sequence; + rt_link_hw_copy(rt_link_scb->rx_record.dataspace, receive_frame->real_data, receive_frame->data_len); + rt_link_recv_finish(receive_frame->head.service, rt_link_scb->rx_record.dataspace, receive_frame->data_len); + rt_link_scb->rx_record.dataspace = RT_NULL; + } + else + { + LOG_W("short data %dB alloc failed", receive_frame->data_len); + } + receive_frame->real_data = RT_NULL; + return 0; +} + +static void _long_handle_first(struct rt_link_frame *receive_frame) +{ + if (receive_frame->extend.parameter % RT_LINK_MAX_DATA_LENGTH == 0) + { + receive_frame->total = receive_frame->extend.parameter / RT_LINK_MAX_DATA_LENGTH; + } + else + { + receive_frame->total = receive_frame->extend.parameter / RT_LINK_MAX_DATA_LENGTH + 1; + } + + rt_link_scb->rx_record.total = receive_frame->total; + rt_link_scb->rx_record.dataspace = rt_malloc(receive_frame->extend.parameter); + if (rt_link_scb->rx_record.dataspace == RT_NULL) + { + LOG_W("long data (%dB) alloc failed.", receive_frame->extend.parameter); + } +} + +static void _long_handle_second(struct rt_link_frame *receive_frame) +{ + static rt_uint8_t ack_mask = RT_LINK_ACK_MAX; + rt_size_t offset = 0; /* offset, count from 0 */ + + receive_frame->index = rt_link_check_seq(receive_frame->head.sequence, rt_link_scb->rx_record.rx_seq) - 1; + LOG_D("seq(%d), rxseq(%d), index(%d), total(%d), count(0x%x)" + , receive_frame->head.sequence + , rt_link_scb->rx_record.rx_seq + , receive_frame->index + , receive_frame->total + , rt_link_scb->rx_record.long_count); + + if ((receive_frame->index > RT_LINK_FRAMES_MAX) || (rt_link_scb->rx_record.long_count & (0x01 << receive_frame->index))) + { + LOG_D("ERR:index %d, rx_seq %d", receive_frame->index, rt_link_scb->rx_record.rx_seq); + } + else if (rt_link_scb->rx_record.dataspace != RT_NULL) + { + rt_link_scb->rx_record.long_count |= (0x01 << receive_frame->index); + offset = RT_LINK_MAX_DATA_LENGTH * receive_frame->index; + rt_link_hw_copy(rt_link_scb->rx_record.dataspace + offset, receive_frame->real_data, receive_frame->data_len); + + if (receive_frame->head.ack) + { + if (rt_link_utils_num1(rt_link_scb->rx_record.long_count) == rt_link_scb->rx_record.total) + { + rt_link_command_frame_send(receive_frame->head.service, + (rt_link_scb->rx_record.rx_seq + rt_link_scb->rx_record.total), + RT_LINK_CONFIRM_FRAME, RT_NULL); + } + else if ((rt_link_scb->rx_record.long_count & ack_mask) == ack_mask) + { + rt_link_command_frame_send(receive_frame->head.service, + (rt_link_scb->rx_record.rx_seq + rt_link_utils_num1(ack_mask)), + RT_LINK_CONFIRM_FRAME, RT_NULL); + ack_mask |= ack_mask << rt_link_utils_num1(RT_LINK_ACK_MAX); + } + } + + /* receive a complete package */ + if (rt_link_utils_num1(rt_link_scb->rx_record.long_count) == rt_link_scb->rx_record.total) + { + rt_timer_stop(&rt_link_scb->longframetimer); + rt_link_recv_finish(receive_frame->head.service, rt_link_scb->rx_record.dataspace, receive_frame->extend.parameter); + + rt_enter_critical(); + /* empty rx_record */ + rt_link_scb->rx_record.rx_seq += rt_link_scb->rx_record.total; + rt_link_scb->rx_record.dataspace = RT_NULL; + rt_link_scb->rx_record.long_count = 0; + rt_link_scb->rx_record.total = 0; + ack_mask = RT_LINK_ACK_MAX; + rt_exit_critical(); + } + else if (rt_link_hw_recv_len(rt_link_scb->rx_buffer) < (receive_frame->data_len % RT_LINK_MAX_DATA_LENGTH)) + { + rt_int32_t timeout = RT_LINK_LONG_FRAME_TIMEOUT; + rt_timer_control(&rt_link_scb->longframetimer, RT_TIMER_CTRL_SET_TIME, &timeout); + rt_timer_start(&rt_link_scb->longframetimer); + } + } +} + +static rt_err_t rt_link_long_handle(struct rt_link_frame *receive_frame) +{ + if (rt_link_scb->rx_record.long_count == 0) + { + /* Receive this long package for the first time: + * calculates the total number of frames, + * requests space, and turns on the receive timer */ + _long_handle_first(receive_frame); + } + if (rt_link_scb->rx_record.total > 0) + { + /* Intermediate frame processing: + * serial number repeated check, + * receive completion check, reply to ACK */ + _long_handle_second(receive_frame); + } + receive_frame->real_data = RT_NULL; + return RT_EOK; +} + +static rt_err_t rt_link_handshake_handle(struct rt_link_frame *receive_frame) +{ + LOG_D("HANDSHAKE: seq(%d) param(%d)", receive_frame->head.sequence, receive_frame->extend.parameter); + rt_link_scb->state = RT_LINK_CONNECT; + /* sync requester tx seq, responder rx seq = requester tx seq */ + rt_link_scb->rx_record.rx_seq = receive_frame->head.sequence; + /* sync requester rx seq, responder tx seq = requester rx seq */ + rt_link_scb->tx_seq = receive_frame->extend.parameter; + + if (rt_link_scb->service[receive_frame->head.service] != RT_NULL) + { + rt_link_scb->service[receive_frame->head.service]->state = RT_LINK_CONNECT; + rt_link_command_frame_send(receive_frame->head.service, + receive_frame->head.sequence, + RT_LINK_CONFIRM_FRAME, RT_NULL); + } + else + { + rt_link_command_frame_send(receive_frame->head.service, + receive_frame->head.sequence, + RT_LINK_DETACH_FRAME, RT_NULL); + } + return RT_EOK; +} + +static rt_err_t rt_link_detach_handle(struct rt_link_frame *receive_frame) +{ + if (rt_link_scb->service[receive_frame->head.service] != RT_NULL) + { + rt_link_scb->service[receive_frame->head.service]->state = RT_LINK_DISCONN; + } + return RT_EOK; +} + +static rt_err_t rt_link_session_end_handle(struct rt_link_frame *receive_frame) +{ + rt_link_frame_stop_receive(receive_frame); + return RT_EOK; +} + +/* Discriminate frame type */ +static rt_err_t rt_link_parse_frame(struct rt_link_frame *receive_frame) +{ + switch (receive_frame->attribute) + { + case RT_LINK_RESEND_FRAME: + rt_link_resend_handle(receive_frame); + break; + case RT_LINK_CONFIRM_FRAME: + rt_link_confirm_handle(receive_frame); + break; + case RT_LINK_HANDSHAKE_FRAME: + rt_link_handshake_handle(receive_frame); + break; + case RT_LINK_SESSION_END: + rt_link_session_end_handle(receive_frame); + break; + case RT_LINK_DETACH_FRAME: + rt_link_detach_handle(receive_frame); + break; + + case RT_LINK_SHORT_DATA_FRAME: + rt_link_short_handle(receive_frame); + break; + case RT_LINK_LONG_DATA_FRAME: + rt_link_long_handle(receive_frame); + break; + + default: + return -RT_ERROR; + } + return RT_EOK; +} + +/* RT_LINK_READ_CHECK_EVENT handle */ +static void rt_link_frame_check(void) +{ + static struct rt_link_frame receive_frame = {0}; + static rt_link_frame_parse_t analysis_status = FIND_FRAME_HEAD; + static rt_uint8_t *data = RT_NULL; + static rt_uint16_t buff_len = RT_LINK_HEAD_LENGTH; + + struct rt_link_frame *send_frame = RT_NULL; + rt_tick_t timeout = 0; + rt_uint32_t temporary_crc = 0; + + rt_uint8_t offset = 0; + rt_size_t recv_len = rt_link_hw_recv_len(rt_link_scb->rx_buffer); + while (recv_len > 0) + { + switch (analysis_status) + { + case FIND_FRAME_HEAD: + { + /* if we can't find frame head, throw that data */ + if ((*rt_link_scb->rx_buffer->read_point & RT_LINK_FRAME_HEAD_MASK) == RT_LINK_FRAME_HEAD) + { + analysis_status = PARSE_FRAME_HEAD; + break; + } + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + break; + } + + case PARSE_FRAME_HEAD: + { + if (recv_len < buff_len) + { + LOG_D("HEAD: actual: %d, need: %d.", recv_len, buff_len); + return ; + } + /* Data is an offset address */ + data = rt_link_scb->rx_buffer->read_point; + rt_link_frame_init(&receive_frame, RT_NULL); + rt_link_hw_copy((rt_uint8_t *)&receive_frame.head, data, sizeof(struct rt_link_frame_head)); + rt_link_hw_buffer_point_shift(&data, sizeof(struct rt_link_frame_head)); + + LOG_D("HEAD: seq(%d) serv(%d) ack(%d) crc(%d) ext(%d) len(%d) attr(%d)(0x%x)" + , receive_frame.head.sequence + , receive_frame.head.service + , receive_frame.head.ack + , receive_frame.head.crc + , receive_frame.head.extend + , receive_frame.head.length + , receive_frame.extend.attribute + , receive_frame.extend.parameter); + + receive_frame.data_len = receive_frame.head.length; + if (receive_frame.head.service >= RT_LINK_SERVICE_MAX) + { + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + goto __find_head; + } + + if (receive_frame.head.extend) + { + buff_len += RT_LINK_EXTEND_LENGTH; + analysis_status = PARSE_FRAME_EXTEND; + } + else + { + receive_frame.attribute = RT_LINK_SHORT_DATA_FRAME; + analysis_status = PARSE_FRAME_SEQ; + } + } + + case PARSE_FRAME_EXTEND: + { + if (receive_frame.head.extend) + { + if (recv_len < buff_len) + { + LOG_D("EXTEND: actual: %d, need: %d.", recv_len, buff_len); + /* should set timer, control receive frame timeout, one shot */ + timeout = 50; + rt_timer_control(&rt_link_scb->recvtimer, RT_TIMER_CTRL_SET_TIME, &timeout); + rt_timer_start(&rt_link_scb->recvtimer); + return; + } + rt_timer_stop(&rt_link_scb->recvtimer); + rt_link_hw_copy((rt_uint8_t *)&receive_frame.extend, data, sizeof(struct rt_link_extend)); + rt_link_hw_buffer_point_shift(&data, sizeof(struct rt_link_extend)); + if (receive_frame.extend.attribute < RT_LINK_RESERVE_FRAME) + { + receive_frame.attribute = receive_frame.extend.attribute; + } + else + { + LOG_D("EXTEND: attr(%d) err", receive_frame.extend.attribute); + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + goto __find_head; + } + } + analysis_status = PARSE_FRAME_SEQ; + } + + case PARSE_FRAME_SEQ: + { + switch (receive_frame.attribute) + { + case RT_LINK_CONFIRM_FRAME: + case RT_LINK_RESEND_FRAME: + { + /* Check the send sequence */ + offset = rt_link_check_seq(receive_frame.head.sequence, rt_link_scb->tx_seq); + if (rt_slist_first(&rt_link_scb->tx_data_slist) != RT_NULL) + { + send_frame = rt_container_of(rt_link_scb->tx_data_slist.next, struct rt_link_frame, slist); + if (offset > send_frame->total) + { + /* exceptional frame, ignore it */ + LOG_D("seq (%d) failed, tx_seq (%d).offset=(%d) total= (%d)", receive_frame.head.sequence, rt_link_scb->tx_seq, offset, send_frame->total); + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + goto __find_head; + } + } + break; + } + case RT_LINK_LONG_DATA_FRAME: + case RT_LINK_SHORT_DATA_FRAME: + case RT_LINK_SESSION_END: + { + /* Check the receive sequence */ + offset = rt_link_check_seq(receive_frame.head.sequence, rt_link_scb->rx_record.rx_seq) - 1; + if (offset > RT_LINK_FRAMES_MAX) + { + /* exceptional frame, ignore it */ + LOG_D("seq (%d) failed, rx_seq (%d) offset=(%d) attr= (%d) status (%d)", receive_frame.head.sequence, rt_link_scb->rx_record.rx_seq, offset, receive_frame.attribute, rt_link_scb->state); + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + goto __find_head; + } + } + case RT_LINK_HANDSHAKE_FRAME: + case RT_LINK_DETACH_FRAME: + analysis_status = HEADLE_FRAME_DATA; + break; + + default: + LOG_D("quick filter error frame."); + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + goto __find_head; + } + buff_len += receive_frame.data_len; + if (receive_frame.head.crc) + { + buff_len += RT_LINK_CRC_LENGTH; + analysis_status = CHECK_FRAME_CRC; + } + else + { + analysis_status = HEADLE_FRAME_DATA; + } + /* fill real data point */ + receive_frame.real_data = data; + } + + case CHECK_FRAME_CRC: + { + if (receive_frame.head.crc) + { + if (recv_len < buff_len) + { + LOG_D("CRC: actual: %d, need: %d.", recv_len, buff_len); + /* should set timer, control receive frame timeout, one shot */ + timeout = 50; + rt_timer_control(&rt_link_scb->recvtimer, RT_TIMER_CTRL_SET_TIME, &timeout); + rt_timer_start(&rt_link_scb->recvtimer); + return; + } + + rt_timer_stop(&rt_link_scb->recvtimer); + rt_link_hw_buffer_point_shift(&data, receive_frame.data_len); + rt_link_hw_copy((rt_uint8_t *)&receive_frame.crc, data, RT_LINK_CRC_LENGTH); + temporary_crc = rt_link_scb->calculate_crc(RT_TRUE, rt_link_scb->rx_buffer->read_point, buff_len - RT_LINK_CRC_LENGTH); + if (receive_frame.crc != temporary_crc) + { + /* check failed. ready resent */ + LOG_D("CRC: calc:(0x%08x) ,recv:(0x%08x).", temporary_crc, receive_frame.crc); + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, 1); + goto __find_head; + } + } + analysis_status = HEADLE_FRAME_DATA; + } + + case HEADLE_FRAME_DATA: + { + if (recv_len < buff_len) + { + LOG_D("PARSE: actual: %d, need: %d.", recv_len, buff_len); + /* should set timer, control receive frame timeout, one shot */ + timeout = 50; + rt_timer_control(&rt_link_scb->recvtimer, RT_TIMER_CTRL_SET_TIME, &timeout); + rt_timer_start(&rt_link_scb->recvtimer); + return; + } + LOG_D("PARSE: buff_len (%d) r (0x%p) w (0x%p)" + , buff_len, rt_link_scb->rx_buffer->read_point + , rt_link_scb->rx_buffer->write_point); + rt_timer_stop(&rt_link_scb->recvtimer); + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, buff_len); + rt_link_parse_frame(&receive_frame); + data = RT_NULL; + buff_len = RT_LINK_HEAD_LENGTH; + analysis_status = FIND_FRAME_HEAD; + break; + } + + default: +__find_head: + LOG_D("to find head (%d)", analysis_status); + rt_link_frame_stop_receive(&receive_frame); + buff_len = RT_LINK_HEAD_LENGTH; + analysis_status = FIND_FRAME_HEAD; + break; + } + + recv_len = rt_link_hw_recv_len(rt_link_scb->rx_buffer); + } +} + +static void rt_link_send_ready(void) +{ + struct rt_link_frame *frame = RT_NULL; + rt_uint8_t seq = rt_link_scb->tx_seq; + if (rt_slist_next(&rt_link_scb->tx_data_slist)) + { + frame = rt_container_of(rt_slist_next(&rt_link_scb->tx_data_slist), struct rt_link_frame, slist); + } + + if (rt_link_scb->state != RT_LINK_CONNECT) + { + rt_link_scb->state = RT_LINK_DISCONN; + rt_link_command_frame_send(RT_LINK_SERVICE_RTLINK, seq, + RT_LINK_HANDSHAKE_FRAME, rt_link_scb->rx_record.rx_seq); + + rt_int32_t timeout = 50; + rt_timer_control(&rt_link_scb->sendtimer, RT_TIMER_CTRL_SET_TIME, &timeout); + rt_timer_start(&rt_link_scb->sendtimer); + } + else + { + /* Avoid sending the first data frame multiple times */ + if ((frame != RT_NULL) && (frame->issent == RT_LINK_FRAME_NOSEND)) + { + if (RT_EOK != rt_link_frame_send(&rt_link_scb->tx_data_slist)) + { + rt_link_scb->state = RT_LINK_DISCONN; + rt_link_service_send_finish(RT_LINK_EIO); + } + } + } +} + +static void rt_link_frame_recv_timeout(void) +{ + /* The receiving frame timeout and a new receive begins */ + rt_link_hw_buffer_point_shift(&rt_link_scb->rx_buffer->read_point, rt_link_hw_recv_len(rt_link_scb->rx_buffer)); +} + +static void rt_link_send_timeout(void) +{ + LOG_D("send count(%d)", (rt_uint32_t)rt_link_scb->sendtimer.parameter); + if ((rt_uint32_t)rt_link_scb->sendtimer.parameter >= 5) + { + rt_timer_stop(&rt_link_scb->sendtimer); + LOG_W("Send timeout, please check the link status!"); + rt_link_scb->sendtimer.parameter = 0x00; + rt_link_service_send_finish(RT_LINK_ETIMEOUT); + } + else + { + if (rt_slist_next(&rt_link_scb->tx_data_slist)) + { + struct rt_link_frame *frame = rt_container_of(rt_slist_next(&rt_link_scb->tx_data_slist), struct rt_link_frame, slist); + frame->issent = RT_LINK_FRAME_NOSEND; + rt_link_command_frame_send(RT_LINK_SERVICE_RTLINK, + frame->head.sequence, + RT_LINK_HANDSHAKE_FRAME, + rt_link_scb->rx_record.rx_seq); + } + } +} + +static void rt_link_long_recv_timeout(void) +{ + if ((rt_uint32_t)rt_link_scb->longframetimer.parameter >= 5) + { + LOG_W("long package receive timeout"); + rt_link_scb->longframetimer.parameter = 0x00; + _stop_recv_long(); + rt_timer_stop(&rt_link_scb->longframetimer); + } + else + { + rt_uint8_t total = rt_link_scb->rx_record.total; + for (; total > 0; total--) + { + if (((rt_link_scb->rx_record.long_count >> (total - 1)) & 0x01) == 0x00) + { + /* resend command */ + rt_link_command_frame_send(RT_LINK_SERVICE_RTLINK, + (rt_link_scb->rx_record.rx_seq + total), + RT_LINK_RESEND_FRAME, RT_NULL); + } + } + } +} + +void rt_link_thread(void *parameter) +{ + rt_uint32_t recved = 0; + while (1) + { + rt_event_recv(&rt_link_scb->event, + RT_LINK_READ_CHECK_EVENT | + RT_LINK_SEND_READY_EVENT | + RT_LINK_SEND_TIMEOUT_EVENT | + RT_LINK_RECV_TIMEOUT_FRAME_EVENT | + RT_LINK_RECV_TIMEOUT_LONG_EVENT, + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, + &recved); + + if (recved & RT_LINK_READ_CHECK_EVENT) + { + rt_link_frame_check(); + } + + if (recved & RT_LINK_SEND_READY_EVENT) + { + rt_link_send_ready(); + } + + if (recved & RT_LINK_SEND_TIMEOUT_EVENT) + { + rt_link_send_timeout(); + } + + if (recved & RT_LINK_RECV_TIMEOUT_FRAME_EVENT) + { + rt_link_frame_recv_timeout(); + } + + if (recved & RT_LINK_RECV_TIMEOUT_LONG_EVENT) + { + rt_link_long_recv_timeout(); + } + } +} + +static void rt_link_sendtimer_callback(void *parameter) +{ + rt_uint32_t count = (rt_uint32_t)rt_link_scb->sendtimer.parameter + 1; + rt_link_scb->sendtimer.parameter = (void *)count; + rt_event_send(&rt_link_scb->event, RT_LINK_SEND_TIMEOUT_EVENT); +} + +static void rt_link_recvtimer_callback(void *parameter) +{ + rt_event_send(&rt_link_scb->event, RT_LINK_RECV_TIMEOUT_FRAME_EVENT); +} + +static void rt_link_receive_long_frame_callback(void *parameter) +{ + rt_uint32_t count = (rt_uint32_t)rt_link_scb->longframetimer.parameter + 1; + rt_link_scb->longframetimer.parameter = (void *)count; + rt_event_send(&rt_link_scb->event, RT_LINK_RECV_TIMEOUT_LONG_EVENT); +} + +/** + * rtlink send data interface + * @param service Registered service channel, struct rt_link_service + * @param data send data + * @param size send data size + * @return The actual size of the data sent + * */ +rt_size_t rt_link_send(struct rt_link_service *service, const void *data, rt_size_t size) +{ + RT_ASSERT(service != RT_NULL); + + rt_uint32_t recved = 0; + rt_uint8_t total = 0; /* The total number of frames to send */ + rt_uint8_t index = 0; /* The index of the split packet */ + rt_size_t offset = 0; /* The offset of the send data */ + rt_size_t send_len = 0; + + struct rt_link_frame *send_frame = RT_NULL; + rt_link_frame_attr_e attribute = RT_LINK_SHORT_DATA_FRAME; + + if ((size == 0) || (data == RT_NULL)) + { + service->err = RT_LINK_ERR; + goto __exit; + } + + service->err = RT_LINK_EOK; + if (size % RT_LINK_MAX_DATA_LENGTH == 0) + { + total = size / RT_LINK_MAX_DATA_LENGTH; + } + else + { + total = size / RT_LINK_MAX_DATA_LENGTH + 1; + } + + if (total > RT_LINK_FRAMES_MAX) + { + service->err = RT_LINK_ENOMEM; + goto __exit; + } + else if (total > 1) + { + attribute = RT_LINK_LONG_DATA_FRAME; + } + + do + { + send_frame = rt_malloc(sizeof(struct rt_link_frame)); + if (send_frame == RT_NULL) + { + service->err = RT_LINK_ENOMEM; + goto __exit; + } + rt_link_frame_init(send_frame, service->flag); + send_frame->head.sequence = ++rt_link_scb->tx_seq; + send_frame->head.service = service->service; + send_frame->real_data = (rt_uint8_t *)data + offset; + send_frame->index = index; + send_frame->total = total; + + if (attribute == RT_LINK_LONG_DATA_FRAME) + { + send_frame->attribute = RT_LINK_LONG_DATA_FRAME; + if (offset + RT_LINK_MAX_DATA_LENGTH > size) + { + send_frame->data_len = size - offset; + } + else + { + send_frame->data_len = RT_LINK_MAX_DATA_LENGTH; + offset += RT_LINK_MAX_DATA_LENGTH; + } + + rt_link_frame_extend_config(send_frame, RT_LINK_LONG_DATA_FRAME, size); + } + else + { + send_frame->attribute = RT_LINK_SHORT_DATA_FRAME; + send_frame->data_len = size; + } + + /* append the frame on the tail of list */ + LOG_D("append send slist, seq(%d), len(%d)", send_frame->head.sequence, send_frame->data_len); + rt_slist_append(&rt_link_scb->tx_data_slist, &send_frame->slist); + + index++; + send_len += send_frame->data_len; + }while(total > index); + + /* Notify the core thread to send packet */ + rt_event_send(&rt_link_scb->event, RT_LINK_SEND_READY_EVENT); + + if (service->timeout_tx != RT_WAITING_NO) + { + /* Wait for the packet to send the result */ + rt_err_t ret = rt_event_recv(&rt_link_scb->sendevent, (0x01 << service->service), + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + service->timeout_tx, &recved); + if (ret == -RT_ETIMEOUT) + { + service->err = RT_LINK_ETIMEOUT; + send_len = 0; + } + } + +__exit: + return send_len; +} + +void rtlink_status(void) +{ + rt_kprintf("rtlink(v%s) status:\n", RT_LINK_VER); + if (rt_link_scb != RT_NULL) + { + rt_kprintf("\tlink status=%d\n", rt_link_scb->state); + + rt_kprintf("\trx seq=%d\n", rt_link_scb->rx_record.rx_seq); + rt_kprintf("\ttx seq=%d\n", rt_link_scb->tx_seq); + rt_kprintf("\trecv len=%d\n", rt_link_hw_recv_len(rt_link_scb->rx_buffer)); + + rt_tick_t state = 0; + rt_timer_control(&rt_link_scb->longframetimer, RT_TIMER_CTRL_GET_STATE, &state); + rt_kprintf("\tlong timer state=%d\n", state); + rt_timer_control(&rt_link_scb->sendtimer, RT_TIMER_CTRL_GET_STATE, &state); + rt_kprintf("\tsend timer state=%d\n", state); + + rt_kprintf("\tevent set=0x%08x\n", rt_link_scb->event.set); + if (rt_link_scb->tx_data_slist.next) + { + rt_slist_t *data = RT_NULL; + rt_slist_for_each(data, &rt_link_scb->tx_data_slist) + { + rt_kprintf("\tsend data list: serv %u\t", ((struct rt_link_frame_head *)data)->service); + rt_kprintf(" seq %u\t", ((struct rt_link_frame_head *)data)->sequence); + rt_kprintf(" len %u\n", ((struct rt_link_frame_head *)data)->length); + } + } + else + { + rt_kprintf("\tsend data list: NULL\n"); + } + + rt_uint8_t serv = RT_LINK_SERVICE_MAX - 1; + while (serv--) + { + rt_kprintf("\tservices [%d](0x%p)\n", serv, rt_link_scb->service[serv]); + } + } + else + { + rt_kprintf("status NULL, please check the initialization status!\n"); + } +} +MSH_CMD_EXPORT(rtlink_status, Display RTLINK status); + +/** + * rtlink deinit the interface + * */ +rt_err_t rt_link_deinit(void) +{ + rt_enter_critical(); + rt_link_hw_deinit(); + if (rt_link_scb) + { + rt_timer_detach(&rt_link_scb->longframetimer); + rt_timer_detach(&rt_link_scb->sendtimer); + rt_timer_detach(&rt_link_scb->recvtimer); + rt_event_detach(&rt_link_scb->event); + rt_free(rt_link_scb); + rt_link_scb = RT_NULL; + } + rt_thread_t thread = rt_thread_find(RT_LINK_THREAD_NAME); + if (thread) + { + rt_thread_delete(thread); + } + rt_exit_critical(); + return RT_EOK; +} +MSH_CMD_EXPORT(rt_link_deinit, rt link deinit); + +/** + * rtlink initializes the interface, usually automatically. + * @return int Function Execution Result + * */ +int rt_link_init(void) +{ + rt_err_t result = RT_EOK; + rt_thread_t thread = RT_NULL; + + if (rt_link_scb != RT_NULL) + { + goto __exit; + } + + rt_link_scb = rt_malloc(sizeof(struct rt_link_session)); + if (rt_link_scb == RT_NULL) + { + result = -RT_ENOMEM; + goto __exit; + } + + rt_memset(rt_link_scb, 0, sizeof(struct rt_link_session)); + rt_event_init(&rt_link_scb->event, "rtlink", RT_IPC_FLAG_FIFO); + rt_event_control(&rt_link_scb->event, RT_IPC_CMD_RESET, RT_NULL); + + rt_event_init(&rt_link_scb->sendevent, "send_rtlink", RT_IPC_FLAG_FIFO); + rt_event_control(&rt_link_scb->sendevent, RT_IPC_CMD_RESET, RT_NULL); + + rt_timer_init(&rt_link_scb->sendtimer, "tx_time", rt_link_sendtimer_callback, + RT_NULL, 0, RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_PERIODIC); + rt_timer_init(&rt_link_scb->recvtimer, "rx_time", rt_link_recvtimer_callback, + RT_NULL, 0, RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT); + rt_timer_init(&rt_link_scb->longframetimer, "rxl_time", rt_link_receive_long_frame_callback, + RT_NULL, 0, RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_PERIODIC); + + rt_link_scb->rx_record.rx_seq = 255; + + rt_slist_init(&rt_link_scb->tx_data_slist); + rt_link_scb->tx_seq = RT_LINK_INIT_FRAME_SEQENCE; + + /* create rtlink core work thread */ + thread = rt_thread_create(RT_LINK_THREAD_NAME, + rt_link_thread, + RT_NULL, + RT_LINK_THREAD_STACK_SIZE, + RT_LINK_THREAD_PRIORITY, + RT_LINK_THREAD_TICK); + if (thread == RT_NULL) + { + result = -RT_ENOMEM; + goto __exit; + } + rt_thread_startup(thread); + + rt_link_scb->state = RT_LINK_INIT; +__exit: + if (result != RT_EOK) + { + LOG_E("rtlink init failed."); + rt_link_deinit(); + } + else + { + LOG_I("rtlink init success(VER:%s).", RT_LINK_VER); + } + return result; +} +#ifdef RT_LINK_AUTO_INIT + INIT_ENV_EXPORT(rt_link_init); +#endif +MSH_CMD_EXPORT(rt_link_init, rt link init); + +/** + * rtlink service attach + * @param service Registered service channel, struct rt_link_service + * @return Function Execution Result + * */ +rt_err_t rt_link_service_attach(struct rt_link_service *serv) +{ + RT_ASSERT(serv != RT_NULL); + if (serv->service >= RT_LINK_SERVICE_MAX) + { + LOG_W("Invalid parameter."); + return -RT_EINVAL; + } + rt_uint8_t seq = rt_link_scb->tx_seq; + rt_link_hw_init(); + rt_link_scb->service[serv->service] = serv; + serv->state = RT_LINK_INIT; + LOG_I("rt link attach service[%02d].", serv->service); + + if (rt_slist_next(&rt_link_scb->tx_data_slist)) + { + struct rt_link_frame *frame = rt_container_of(rt_slist_next(&rt_link_scb->tx_data_slist), struct rt_link_frame, slist); + seq = frame->head.sequence; + } + rt_link_command_frame_send(serv->service, seq, RT_LINK_HANDSHAKE_FRAME, rt_link_scb->rx_record.rx_seq); + return RT_EOK; +} + +/** + * rtlink service detach + * @param service Registered service channel, struct rt_link_service + * @return rt_err_t Function Execution Result + * */ +rt_err_t rt_link_service_detach(struct rt_link_service *serv) +{ + RT_ASSERT(serv != RT_NULL); + if (serv->service >= RT_LINK_SERVICE_MAX) + { + LOG_W("Invalid parameter."); + return -RT_EINVAL; + } + rt_link_command_frame_send(serv->service, + rt_link_scb->tx_seq, + RT_LINK_DETACH_FRAME, + rt_link_scb->rx_record.rx_seq); + + serv->state = RT_LINK_DISCONN; + rt_link_scb->service[serv->service] = RT_NULL; + LOG_I("rt link detach service[%02d].", serv->service); + return RT_EOK; +} diff --git a/components/utilities/rt-link/src/rtlink_dev.c b/components/utilities/rt-link/src/rtlink_dev.c new file mode 100644 index 0000000000..efebf37565 --- /dev/null +++ b/components/utilities/rt-link/src/rtlink_dev.c @@ -0,0 +1,396 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-06-15 Sherman the first version + */ + +#define DBG_TAG "RTLINK_DEV" +#define DBG_LVL DBG_LOG +#include <rtdbg.h> + +#include <rthw.h> +#include <rtthread.h> +#include <rtdevice.h> +#include <rtlink_dev.h> + +#define RTLINK_SERV(dev) (((struct rt_link_device*)dev)->service) + +#ifdef RT_USING_POSIX +#include <dfs_posix.h> +#include <poll.h> + +int rtlink_fops_open(struct dfs_fd *fd) +{ + rt_uint16_t flags = 0; + rt_device_t device; + + switch (fd->flags & O_ACCMODE) + { + case O_RDONLY: + LOG_D("fops open: O_RDONLY!"); + flags = RT_DEVICE_FLAG_RDONLY; + break; + case O_WRONLY: + LOG_D("fops open: O_WRONLY!"); + flags = RT_DEVICE_FLAG_WRONLY; + break; + case O_RDWR: + LOG_D("fops open: O_RDWR!"); + flags = RT_DEVICE_FLAG_RDWR; + break; + default: + LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE); + break; + } + + device = (rt_device_t)fd->data; + if (fd->flags & O_NONBLOCK) + { + rt_device_control(device, RT_LINK_TX_NONBLOCKING | RT_LINK_RX_NONBLOCKING, RT_NULL); + } + + return rt_device_open(device, flags); +} + +int rtlink_fops_close(struct dfs_fd *fd) +{ + rt_device_t device; + device = (rt_device_t)fd->data; + + rt_device_set_rx_indicate(device, RT_NULL); + return rt_device_close(device); +} + +int rtlink_fops_ioctl(struct dfs_fd *fd, int cmd, void *args) +{ + rt_device_t device; + device = (rt_device_t)fd->data; + + if (cmd == O_NONBLOCK) + { + return rt_device_control(device, RT_LINK_TX_NONBLOCKING | RT_LINK_RX_NONBLOCKING, RT_NULL); + } + else + { + return rt_device_control(device, cmd, args); + } +} + +int rtlink_fops_read(struct dfs_fd *fd, void *buf, size_t count) +{ + int size = 0; + rt_device_t device; + device = (rt_device_t)fd->data; + + size = rt_device_read(device, -1, buf, count); + if (size <= 0) + { + size = -EAGAIN; + } + return size; +} + +int rtlink_fops_write(struct dfs_fd *fd, const void *buf, size_t count) +{ + int size = 0; + rt_device_t device; + device = (rt_device_t)fd->data; + + size = rt_device_write(device, -1, buf, count); + if (size <= 0) + { + size = -EAGAIN; + } + return size; +} + +int rtlink_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req) +{ + int mask = 0; + int flags = 0; + rt_device_t device; + struct rt_link_device *rtlink_dev; + + device = (rt_device_t)fd->data; + RT_ASSERT(device != RT_NULL); + + rtlink_dev = (struct rt_link_device *)device; + + flags = fd->flags & O_ACCMODE; + if (flags == O_RDONLY || flags == O_RDWR) + { + rt_base_t level; + rt_poll_add(&(device->wait_queue), req); + + level = rt_hw_interrupt_disable(); + if (RT_NULL != rt_slist_first(&rtlink_dev->recv_head)) + mask |= POLLIN; + rt_hw_interrupt_enable(level); + } + mask |= POLLOUT; + + return mask; +} + +const static struct dfs_file_ops _rtlink_fops = +{ + rtlink_fops_open, + rtlink_fops_close, + rtlink_fops_ioctl, + rtlink_fops_read, + rtlink_fops_write, + RT_NULL, /* flush */ + RT_NULL, /* lseek */ + RT_NULL, /* getdents */ + rtlink_fops_poll, +}; +#endif /* RT_USING_POSIX */ + +/* The event type for the service channel number, + * which is used to wake up the service thread in blocking receive mode */ +struct rt_event recv_event; + +static rt_err_t rt_link_event_send(struct rt_link_service *serv) +{ + RT_ASSERT(serv != RT_NULL); + RT_ASSERT(serv->service < RT_LINK_SERVICE_MAX); + rt_uint32_t set = 0x01 << serv->service; + return rt_event_send(&recv_event, set); +} + +static rt_err_t rt_link_event_recv(struct rt_link_service *service) +{ + RT_ASSERT(service != RT_NULL); + RT_ASSERT(service->service < RT_LINK_SERVICE_MAX); + + rt_uint32_t set = 0x01 << service->service; + rt_uint32_t recved = 0; + rt_err_t ret = rt_event_recv(&recv_event, + set, + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, + &recved); + if (recved & set) + { + return ret; + } + return RT_ERROR; +} + +static void send_cb(struct rt_link_service *service, void *buffer) +{ + RT_ASSERT(service != RT_NULL); + RT_ASSERT(buffer != RT_NULL); + struct rt_link_device *rtlink = (struct rt_link_device *)service->user_data; + + if (rtlink && rtlink->parent.tx_complete) + { + rtlink->parent.tx_complete(&rtlink->parent, buffer); + } +} + +static void recv_cb(struct rt_link_service *service, void *data, rt_size_t size) +{ + RT_ASSERT(service != RT_NULL); + struct rt_link_device *rtlink = (struct rt_link_device *)service->user_data; + + if (rtlink) + { + struct rtlink_recv_list *node = rt_malloc(sizeof(struct rtlink_recv_list)); + node->data = data; + node->size = size; + node->index = 0; + rt_slist_append(&rtlink->recv_head, &node->list); + rt_link_event_send(service); + + if (rtlink->parent.rx_indicate) + { + rtlink->parent.rx_indicate(&rtlink->parent, size); + } + } + else + { + rt_free(data); + } +} + +rt_err_t rt_link_dev_init(rt_device_t dev) +{ + RT_ASSERT(dev != RT_NULL); + + dev->rx_indicate = RT_NULL; + dev->tx_complete = RT_NULL; + + struct rt_link_device *rtlink = (struct rt_link_device *)dev; + rtlink->service.service = RT_LINK_SERVICE_MAX; + rtlink->service.recv_cb = RT_NULL; + rtlink->service.send_cb = RT_NULL; + rtlink->service.timeout_tx = RT_WAITING_NO; + rtlink->service.user_data = (void *)dev; + + rt_slist_init(&rtlink->recv_head); + return RT_EOK; +} + +rt_err_t rt_link_dev_open(rt_device_t dev, rt_uint16_t oflag) +{ + RT_ASSERT(dev != RT_NULL); + struct rt_link_device *rtlink = (struct rt_link_device *)dev; + + rtlink->service.recv_cb = recv_cb; + rtlink->service.send_cb = send_cb; + + dev->open_flag = oflag & RT_DEVICE_OFLAG_MASK; + if (dev->open_flag == RT_DEVICE_OFLAG_RDONLY) + { + rtlink->service.send_cb = RT_NULL; + } + else if (dev->open_flag == RT_DEVICE_OFLAG_WRONLY) + { + rtlink->service.recv_cb = RT_NULL; + } + return rt_link_service_attach(&rtlink->service); +} + +rt_err_t rt_link_dev_close(rt_device_t dev) +{ + RT_ASSERT(dev != RT_NULL); + struct rt_link_device *rtlink = (struct rt_link_device *)dev; + return rt_link_service_detach(&rtlink->service); +} + +rt_size_t rt_link_dev_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) +{ + RT_ASSERT(dev != RT_NULL); + RT_ASSERT(buffer != RT_NULL); + RT_ASSERT(size != 0); + + struct rt_link_device *rtlink = (struct rt_link_device *)dev; + struct rtlink_recv_list *node; + rt_size_t read_len = 0; + rt_size_t unread_len = 0; + + if (dev->rx_indicate == RT_NULL) + { + /* RT_LINK_RX_BLOCKING, wait service receive data event */ + rt_link_event_recv(&rtlink->service); + } + + if (rt_slist_first(&rtlink->recv_head) != RT_NULL) + { + node = rt_container_of(rt_slist_next(&rtlink->recv_head), struct rtlink_recv_list, list); + unread_len = (node->size) - (node->index); + read_len = (size > unread_len) ? unread_len : size; + rt_memcpy(buffer, (rt_uint8_t *)node->data + node->index, read_len); + node->index += read_len; + + if (node->index >= node->size) + { + rt_slist_remove(&rtlink->recv_head, &node->list); + node->index = 0; + rt_free(node->data); + rt_free(node); + } + if (rt_slist_first(&rtlink->recv_head) != RT_NULL) + { + rt_link_event_send(&rtlink->service); + } + } + return read_len; +} + +rt_size_t rt_link_dev_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) +{ + RT_ASSERT(dev != RT_NULL); + RT_ASSERT(buffer != RT_NULL); + RT_ASSERT(size != 0); + + return rt_link_send(&RTLINK_SERV(dev), buffer, size); +} + +rt_err_t rt_link_dev_control(rt_device_t dev, int cmd, void *args) +{ + RT_ASSERT(dev != RT_NULL); + + if (cmd & RT_DEVICE_CTRL_CONFIG) + { + if (args == RT_NULL) + return RT_EINVAL; + RTLINK_SERV(dev).service = ((struct rt_link_service *)args)->service; + RTLINK_SERV(dev).timeout_tx = ((struct rt_link_service *)args)->timeout_tx; + RTLINK_SERV(dev).flag = ((struct rt_link_service *)args)->flag; + } + + if (cmd & RT_LINK_RX_BLOCKING) + { + dev->rx_indicate = RT_NULL; + } + if (cmd & RT_LINK_TX_BLOCKING) + { + RTLINK_SERV(dev).timeout_tx = RT_WAITING_FOREVER; + dev->tx_complete = RT_NULL; + } + if (cmd & RT_LINK_TX_NONBLOCKING) + { + RTLINK_SERV(dev).timeout_tx = RT_WAITING_NO; + } + + return RT_EOK; +} + +#ifdef RT_USING_DEVICE_OPS +const static struct rt_device_ops rtlink_ops = +{ + rt_link_dev_init, + rt_link_dev_open, + rt_link_dev_close, + rt_link_dev_read, + rt_link_dev_write, + rt_link_dev_control +}; +#endif /* RT_USING_DEVICE_OPS */ + +/* + * rtlink device register + */ +rt_err_t rt_link_dev_register(struct rt_link_device *rtlink, + const char *name, + rt_uint32_t flag, + void *data) +{ + rt_err_t ret; + struct rt_device *device; + RT_ASSERT(rtlink != RT_NULL); + + device = (struct rt_device *)rtlink; + device->type = RT_Device_Class_Char; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + +#ifdef RT_USING_DEVICE_OPS + device->ops = &rtlink_ops; +#else + device->init = rt_link_dev_init; + device->open = rt_link_dev_open; + device->close = rt_link_dev_close; + device->read = rt_link_dev_read; + device->write = rt_link_dev_write; + device->control = rt_link_dev_control; +#endif + device->user_data = data; + + /* register a character device */ + ret = rt_device_register(device, name, flag); + +#if defined(RT_USING_POSIX) + /* set fops */ + device->fops = &_rtlink_fops; +#endif + + rt_event_init(&recv_event, "rtlink_dev", RT_IPC_FLAG_FIFO); + return ret; +} diff --git a/components/utilities/rt-link/src/rtlink_hw.c b/components/utilities/rt-link/src/rtlink_hw.c new file mode 100644 index 0000000000..696ab5b78a --- /dev/null +++ b/components/utilities/rt-link/src/rtlink_hw.c @@ -0,0 +1,296 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-02-02 xiangxistu the first version + * 2021-05-08 Sherman Optimize the operation function on the rt_link_receive_buffer + */ + +#include <rtthread.h> + +#include <rtlink.h> +#include <rtlink_hw.h> +#include <rtlink_port.h> +#include <rtlink_utils.h> + +#define DBG_TAG "rtlink_hw" +#ifdef USING_RT_LINK_HW_DEBUG + #define DBG_LVL DBG_LOG +#else + #define DBG_LVL DBG_INFO +#endif +#define DBG_COLOR +#include <rtdbg.h> + +static struct rt_link_receive_buffer *rx_buffer = RT_NULL; + +struct rt_link_receive_buffer *rt_link_hw_buffer_init(void *parameter) +{ + rx_buffer = rt_malloc(sizeof(struct rt_link_receive_buffer)); + if (rx_buffer != RT_NULL) + { + rt_memset(rx_buffer, 0, sizeof(struct rt_link_receive_buffer)); + rx_buffer->read_point = rx_buffer->data; + rx_buffer->write_point = rx_buffer->data; + rx_buffer->end_point = rx_buffer->data + RT_LINK_RECEIVE_BUFFER_LENGTH; /* Point to memory that has no access rights */ + } + else + { + LOG_E("receive buffer alloc failed, init failed."); + } + + return rx_buffer; +} + +static rt_size_t rt_link_hw_buffer_write(void *data, rt_size_t count) +{ + int surplus = 0; + if (rx_buffer == RT_NULL) + { + return 0; + } + /* (data)----(r)----(w)----(end) */ + if (rx_buffer->write_point >= rx_buffer->read_point) + { + rt_size_t w2end = rx_buffer->end_point - rx_buffer->write_point; + surplus = RT_LINK_RECEIVE_BUFFER_LENGTH - (rx_buffer->write_point - rx_buffer->read_point); + count = count > surplus ? surplus : count; + if (count >= w2end) + { + rt_memcpy(rx_buffer->write_point, data, w2end); + rx_buffer->write_point = rx_buffer->data; + + rt_memcpy(rx_buffer->write_point, (rt_uint8_t *)data + w2end, (count - w2end)); + rx_buffer->write_point += (count - w2end); + } + else + { + rt_memcpy(rx_buffer->write_point, data, count); + rx_buffer->write_point += count; + } + } + else /* (data)----(w)----(r)----(end) */ + { + surplus = rx_buffer->read_point - rx_buffer->write_point; + count = count > surplus ? surplus : count; + rt_memcpy(rx_buffer->write_point, data, count); + rx_buffer->write_point += count; + } + return count; +} + +/* increases buffer pointer by one and circle around if necessary */ +void rt_link_hw_buffer_point_shift(rt_uint8_t **pointer_address, rt_size_t length) +{ + rt_uint8_t *pointer = *pointer_address + length; + + if (rx_buffer->write_point >= rx_buffer->read_point) + { + if (pointer >= rx_buffer->write_point) + { + *pointer_address = rx_buffer->write_point; + } + else + { + *pointer_address = pointer; + } + } + else + { + if (pointer >= rx_buffer->end_point) + { + *pointer_address = rx_buffer->data; + pointer = pointer - rx_buffer->end_point + rx_buffer->data; + + if (pointer >= rx_buffer->write_point) + { + *pointer_address = rx_buffer->write_point; + } + else + { + *pointer_address = pointer; + } + } + else + { + *pointer_address = pointer; + } + } +} + +/* copy data from receive buffer */ +void rt_link_hw_copy(rt_uint8_t *dst, rt_uint8_t *src, rt_size_t count) +{ + rt_uint8_t *pointer = RT_NULL; + + pointer = src + count; + if (pointer >= rx_buffer->end_point) + { + rt_size_t offset = 0; + offset = rx_buffer->end_point - src; + rt_memcpy(dst, src, offset); + rt_memcpy(dst + offset, rx_buffer->data, pointer - rx_buffer->end_point); + } + else + { + rt_memcpy(dst, src, count); + } +} + +/* Length of data received */ +rt_size_t rt_link_hw_recv_len(struct rt_link_receive_buffer *buffer) +{ + if (buffer == RT_NULL) + { + return 0; + } + if (buffer->write_point >= buffer->read_point) + { + return (buffer->write_point - buffer->read_point); + } + else + { + return (RT_LINK_RECEIVE_BUFFER_LENGTH - (buffer->read_point - buffer->write_point)); + } +} + +rt_err_t rt_link_reset_crc32(void) +{ +#ifdef RT_LINK_USING_HW_CRC + return rt_link_hw_crc32_reset(); +#else + return rt_link_sf_crc32_reset(); +#endif +} + +rt_uint32_t rt_link_crc32(rt_uint8_t *data, rt_size_t u32_size) +{ +#ifdef RT_LINK_USING_HW_CRC + return rt_link_hw_crc32(data, u32_size); +#else + return rt_link_sf_crc32(data, u32_size); +#endif +} + +rt_uint32_t rt_link_get_crc(rt_uint8_t using_buffer_ring, rt_uint8_t *data, rt_size_t size) +{ + rt_uint32_t crc32 = 0x0; + rt_size_t surplus = 0; + + if (data == RT_NULL) + { + LOG_D("warning, the parameter error: %d, data: 0x%08d.", size, data); + return 0; + } + + rt_link_reset_crc32(); + if (using_buffer_ring == 1) + { + /* modify the missing character */ + surplus = rx_buffer->end_point - data; + if (surplus >= size) + { + crc32 = rt_link_crc32(data, size); + } + else + { + rt_link_crc32(data, surplus); + crc32 = rt_link_crc32(rx_buffer->data, size - surplus); + } + } + else + { + crc32 = rt_link_crc32(data, size); + } + return crc32; +} + +rt_size_t rt_link_hw_send(void *data, rt_size_t length) +{ + rt_size_t send_len = 0; + send_len = rt_link_port_send(data, length); + if (send_len <= 0) + { + rt_link_port_reconnect(); + send_len = rt_link_port_send(data, length); + } + return send_len; +} + +rt_size_t rt_link_hw_write_cb(void *data, rt_size_t length) +{ + /* write real data into rtlink receive buffer */ + rt_size_t len = rt_link_hw_buffer_write(data, length); + struct rt_link_session *scb = rt_link_get_scb(); + if (scb) + { + rt_event_send(&scb->event, RT_LINK_READ_CHECK_EVENT); + } + return len; +} + +rt_err_t rt_link_hw_init(void) +{ + struct rt_link_session *scb = rt_link_get_scb(); + if ((rx_buffer != RT_NULL) || (scb == RT_NULL)) + { + return -RT_ERROR; + } + + /* alloc receive buffer to store data */ + if (rt_link_hw_buffer_init(RT_NULL) == RT_NULL) + { + return -RT_ENOMEM; + } + scb->rx_buffer = rx_buffer; + scb->calculate_crc = rt_link_get_crc; + + if (RT_EOK != rt_link_port_init()) + { + return -RT_ERROR; + } + +#ifdef LINK_LAYER_USING_HW_CRC + /* crc hardware device for mcu and node */ + if (RT_EOK != rt_link_hw_crc32_init()) + { + return -RT_ERROR; + } +#endif + + LOG_I("link layer hardware environment init successful."); + return RT_EOK; +} + +rt_err_t rt_link_hw_deinit(void) +{ + if (rx_buffer) + { + rt_free(rx_buffer); + rx_buffer = RT_NULL; + } + struct rt_link_session *scb = rt_link_get_scb(); + if (scb) + { + scb->rx_buffer = rx_buffer; + scb->calculate_crc = RT_NULL; + } + if (RT_EOK != rt_link_port_deinit()) + { + return -RT_ERROR; + } + +#ifdef LINK_LAYER_USING_HW_CRC + /* crc hardware device for mcu and node */ + if (RT_EOK != rt_link_hw_crc32_deinit()) + { + return -RT_ERROR; + } +#endif + + LOG_I("rtlink hardware deinit successful."); + return RT_EOK; +} diff --git a/components/utilities/rt-link/src/rtlink_utils.c b/components/utilities/rt-link/src/rtlink_utils.c new file mode 100644 index 0000000000..c5cfbb9422 --- /dev/null +++ b/components/utilities/rt-link/src/rtlink_utils.c @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2021-05-15 Sherman the first version + */ + +#include <rtlink_utils.h> + +/* Calculate the number of '1' */ +int rt_link_utils_num1(rt_uint32_t n) +{ + int ret = 0; + while (n) + { + n &= n - 1; + ret++; + } + return ret; +} + +#ifdef RT_LINK_USING_SF_CRC + +static rt_uint32_t crc = 0xffffffff; +const rt_uint32_t crc_table[256] = +{ + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, + 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, + 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, + 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, + 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, + 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, + 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, + 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, + 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, + 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, + 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, + 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, + 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, + 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, + 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, + 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, + 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, + 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, + 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, + 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, + 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, + 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, + 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, + 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, + 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, + 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, + 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, + 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, + 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, + 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, + 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, + 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, + 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, + 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D +}; + +rt_err_t rt_link_sf_crc32_reset(void) +{ + crc = 0xffffffff; + return RT_EOK; +} + +rt_uint32_t rt_link_sf_crc32(rt_uint8_t *data, rt_size_t len) +{ + rt_uint32_t x, y; + x = 0; + y = 0; + rt_size_t i; + + for (i = 0; i < len; i++) + { + y = (crc ^ data[i]) & 0xff; + x = crc_table[y]; + crc = (crc >> 8) ^ x; + } + return (crc ^ 0xffffffff); +} +#endif /* RT_LINK_USING_SF_CRC */ diff --git a/components/utilities/ulog/backend/console_be.c b/components/utilities/ulog/backend/console_be.c index 95dd547426..b84cd95d09 100644 --- a/components/utilities/ulog/backend/console_be.c +++ b/components/utilities/ulog/backend/console_be.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/components/utilities/ulog/syslog/syslog.c b/components/utilities/ulog/syslog/syslog.c index 4c290bed52..a081db6eb6 100644 --- a/components/utilities/ulog/syslog/syslog.c +++ b/components/utilities/ulog/syslog/syslog.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/components/utilities/ulog/syslog/syslog.h b/components/utilities/ulog/syslog/syslog.h index 43eec7125f..8e4d5ba15f 100644 --- a/components/utilities/ulog/syslog/syslog.h +++ b/components/utilities/ulog/syslog/syslog.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 7b4cb80c3a..0e227e39f3 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2019, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -77,6 +77,7 @@ struct rt_ulog { rt_bool_t init_ok; + rt_bool_t output_lock_enabled; struct rt_semaphore output_locker; /* all backends */ rt_slist_t backend_list; @@ -186,6 +187,9 @@ size_t ulog_ultoa(char *s, unsigned long int n) static void output_unlock(void) { + if (!ulog.output_lock_enabled) + return; + /* is in thread context */ if (rt_interrupt_get_nest() == 0) { @@ -201,6 +205,9 @@ static void output_unlock(void) static void output_lock(void) { + if (!ulog.output_lock_enabled) + return; + /* is in thread context */ if (rt_interrupt_get_nest() == 0) { @@ -214,6 +221,11 @@ static void output_lock(void) } } +void ulog_output_lock_enabled(rt_bool_t enabled) +{ + ulog.output_lock_enabled = enabled; +} + static char *get_log_buf(void) { /* is in thread context */ @@ -260,19 +272,36 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta /* add time info */ { #ifdef ULOG_TIME_USING_TIMESTAMP - static time_t now; + static struct timeval now; static struct tm *tm, tm_tmp; + static rt_bool_t check_usec_support = RT_FALSE, usec_is_support = RT_FALSE; - 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 */ + if (gettimeofday(&now, NULL) >= 0) + { + time_t t = now.tv_sec; + tm = localtime_r(&t, &tm_tmp); + /* show the time format MM-DD HH:MM:SS */ + 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); + /* check the microseconds support when kernel is startup */ + if (!check_usec_support && rt_thread_self() != RT_NULL) + { + long old_usec = now.tv_usec; + /* delay some time for wait microseconds changed */ + rt_thread_delay(2); + gettimeofday(&now, NULL); + check_usec_support = RT_TRUE; + /* the microseconds is not equal between two gettimeofday calls */ + if (now.tv_usec != old_usec) + usec_is_support = RT_TRUE; + } + if (usec_is_support) + { + /* show the millisecond */ + log_len += rt_strlen(log_buf + log_len); + rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, ".%03d", now.tv_usec / 1000); + } + } #else static rt_size_t tick_len = 0; @@ -318,9 +347,14 @@ RT_WEAK rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta /* 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); + rt_size_t name_len = 0; + const char *thread_name = "N/A"; + if (rt_thread_self()) + { + thread_name = rt_thread_self()->name; + } + name_len = rt_strnlen(thread_name, RT_NAME_MAX); + rt_strncpy(log_buf + log_len, thread_name, name_len); log_len += name_len; } else @@ -667,6 +701,9 @@ void ulog_hexdump(const char *tag, rt_size_t width, rt_uint8_t *buf, rt_size_t s rt_size_t i, j; rt_size_t log_len = 0, name_len = rt_strlen(tag); +#ifdef ULOG_OUTPUT_TIME + rt_size_t time_head_len = 0; +#endif char *log_buf = NULL, dump_string[8]; int fmt_result; @@ -703,6 +740,35 @@ void ulog_hexdump(const char *tag, rt_size_t width, rt_uint8_t *buf, rt_size_t s /* package header */ if (i == 0) { +#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 + 2 + tick_len] = ' '; + log_buf[log_len + 3 + tick_len] = '\0'; +#endif /* ULOG_TIME_USING_TIMESTAMP */ + time_head_len = rt_strlen(log_buf + log_len); + log_len += time_head_len; +#endif /* ULOG_OUTPUT_TIME */ 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, ": "); @@ -710,6 +776,9 @@ void ulog_hexdump(const char *tag, rt_size_t width, rt_uint8_t *buf, rt_size_t s else { log_len = 6 + name_len + 2; +#ifdef ULOG_OUTPUT_TIME + log_len += time_head_len; +#endif rt_memset(log_buf, ' ', log_len); } fmt_result = rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE, "%04X-%04X: ", i, i + width - 1); @@ -1006,7 +1075,7 @@ const char *ulog_global_filter_kw_get(void) return ulog.filter.keyword; } -#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) +#ifdef RT_USING_FINSH #include <finsh.h> static void _print_lvl_info(void) @@ -1181,7 +1250,7 @@ static void ulog_filter(uint8_t argc, char **argv) } } MSH_CMD_EXPORT(ulog_filter, Show ulog filter settings); -#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */ +#endif defined(RT_USING_FINSH) #endif /* ULOG_USING_FILTER */ rt_err_t ulog_backend_register(ulog_backend_t backend, const char *name, rt_bool_t support_color) @@ -1335,6 +1404,7 @@ int ulog_init(void) return 0; rt_sem_init(&ulog.output_locker, "ulog lock", 1, RT_IPC_FLAG_FIFO); + ulog.output_lock_enabled = RT_TRUE; rt_slist_init(&ulog.backend_list); #ifdef ULOG_USING_FILTER diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index 670644bb30..1e139222aa 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -25,6 +25,7 @@ extern "C" { */ int ulog_init(void); int ulog_async_init(void); +void ulog_output_lock_enabled(rt_bool_t enabled); void ulog_deinit(void); /* diff --git a/components/utilities/ulog/ulog_def.h b/components/utilities/ulog/ulog_def.h index 771e76a601..d51c6f0d84 100644 --- a/components/utilities/ulog/ulog_def.h +++ b/components/utilities/ulog/ulog_def.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -102,8 +102,8 @@ extern "C" { #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) */ - +#endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */ + /* assert for developer. */ #ifdef ULOG_ASSERT_ENABLE #define ULOG_ASSERT(EXPR) \ @@ -142,7 +142,7 @@ extern "C" { #define log_d LOG_D #define log_v LOG_D #define log_raw LOG_RAW -#define log_hex LOG_HEX +#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 diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 6500ae2fbe..7103efb89c 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -46,12 +46,17 @@ 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 rt_uint8_t *tc_fail_list; static struct utest local_utest = {UTEST_PASSED, 0, 0}; #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */ #pragma section="UtestTcTab" #endif +#define TC_FAIL_LIST_SIZE (RT_ALIGN(tc_num, 8) / 8) +#define TC_FAIL_LIST_MARK_FAILED(index) (tc_fail_list[index / 8] |= (1UL << (index % 8))) +#define TC_FAIL_LIST_IS_FAILED(index) (tc_fail_list[index / 8] & (1UL << (index % 8))) + void utest_log_lv_set(rt_uint8_t lv) { if (lv == UTEST_LOG_ALL || lv == UTEST_LOG_ASSERT) @@ -80,6 +85,14 @@ int utest_init(void) LOG_I("utest is initialize success."); LOG_I("total utest testcase num: (%d)", tc_num); + if (tc_num > 0) + { + tc_fail_list = rt_malloc(TC_FAIL_LIST_SIZE); + if(!tc_fail_list) + { + LOG_E("no memory, tc_fail_list init failed!"); + } + } return tc_num; } INIT_COMPONENT_EXPORT(utest_init); @@ -147,6 +160,8 @@ static void utest_run(const char *utest_name) rt_size_t i; rt_uint32_t index; rt_bool_t is_find; + rt_uint32_t tc_fail_num = 0; + rt_uint32_t tc_run_num = 0; rt_thread_mdelay(1000); @@ -154,6 +169,14 @@ static void utest_run(const char *utest_name) { i = 0; is_find = RT_FALSE; + + tc_fail_num = 0; + tc_run_num = 0; + if (tc_fail_list) + { + memset(tc_fail_list, 0, TC_FAIL_LIST_SIZE); + } + LOG_I("[==========] [ utest ] loop %d/%d", index + 1, tc_loop); LOG_I("[==========] [ utest ] started"); while(i < tc_num) @@ -192,6 +215,8 @@ static void utest_run(const char *utest_name) } else { + TC_FAIL_LIST_MARK_FAILED(i); + tc_fail_num ++; LOG_E("[ FAILED ] [ result ] testcase (%s)", tc_table[i].name); } } @@ -212,6 +237,7 @@ static void utest_run(const char *utest_name) __tc_continue: LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name); + tc_run_num ++; i++; } @@ -223,6 +249,20 @@ static void utest_run(const char *utest_name) } LOG_I("[==========] [ utest ] finished"); + LOG_I("[==========] [ utest ] %d tests from %d testcase ran.", tc_run_num, tc_num); + LOG_I("[ PASSED ] [ result ] %d tests.", tc_run_num - tc_fail_num); + + if(tc_fail_list && (tc_fail_num > 0)) + { + LOG_E("[ FAILED ] [ result ] %d tests, listed below:", tc_fail_num); + for(i = 0; i < tc_num; i ++) + { + if (TC_FAIL_LIST_IS_FAILED(i)) + { + LOG_E("[ FAILED ] [ result ] %s", tc_table[i].name); + } + } + } } } diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index e2d72a931d..33d50efddb 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -22,13 +22,13 @@ extern "C" { /** * utest_error - * + * * @brief Test result. - * + * * @member UTEST_PASSED Test success. * @member UTEST_FAILED Test failed. * @member UTEST_PASSED Test skipped. - * + * */ enum utest_error { @@ -40,13 +40,13 @@ 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 { @@ -58,16 +58,16 @@ 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; @@ -80,61 +80,61 @@ 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 \ @@ -149,14 +149,14 @@ utest_t utest_handle_get(void); /** * 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); \ diff --git a/components/utilities/utest/utest_assert.h b/components/utilities/utest/utest_assert.h index d83279272e..ba76fc39ee 100644 --- a/components/utilities/utest/utest_assert.h +++ b/components/utilities/utest/utest_assert.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * @@ -30,10 +30,10 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa /** * 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. @@ -46,12 +46,12 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa * @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_null(value) __utest_assert((const char *)(value) == RT_NULL, "(" #value ") is not null") +#define uassert_not_null(value) __utest_assert((const char *)(value) != RT_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 ")") @@ -62,7 +62,7 @@ void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equa #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_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 diff --git a/components/utilities/utest/utest_log.h b/components/utilities/utest/utest_log.h index 0403268351..6aa54386a8 100644 --- a/components/utilities/utest/utest_log.h +++ b/components/utilities/utest/utest_log.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006-2018, RT-Thread Development Team + * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/components/utilities/ymodem/SConscript b/components/utilities/ymodem/SConscript index 5be32247d6..d75539fe2c 100644 --- a/components/utilities/ymodem/SConscript +++ b/components/utilities/ymodem/SConscript @@ -7,7 +7,7 @@ ymodem.c CPPPATH = [cwd] -if GetDepend('RT_USING_DFS') and GetDepend('YMODEM_USING_FILE_TRANSFER'): +if GetDepend('YMODEM_USING_FILE_TRANSFER'): src += ['ry_sy.c'] group = DefineGroup('Utilities', src, depend = ['RT_USING_RYM'], CPPPATH = CPPPATH) diff --git a/components/utilities/ymodem/ry_sy.c b/components/utilities/ymodem/ry_sy.c index a87f6949db..caad51d6cd 100644 --- a/components/utilities/ymodem/ry_sy.c +++ b/components/utilities/ymodem/ry_sy.c @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2019-12-09 Steven Liu the first version + * 2021-04-14 Meco Man Check the file path's legitimacy of 'sy' command */ #include <rtthread.h> @@ -219,6 +220,12 @@ static rt_err_t sy(uint8_t argc, char **argv) const char *file_path; rt_device_t dev; + if (argc < 2) + { + rt_kprintf("invalid file path.\n"); + return -RT_ERROR; + } + if (argc > 2) dev = rt_device_find(argv[2]); else diff --git a/components/utilities/ymodem/ymodem.c b/components/utilities/ymodem/ymodem.c index 07055db615..a74237689d 100644 --- a/components/utilities/ymodem/ymodem.c +++ b/components/utilities/ymodem/ymodem.c @@ -1,5 +1,5 @@ /* - * COPYRIGHT (C) 2012, Real-Thread Information Technology Ltd + * COPYRIGHT (C) 2011-2021, Real-Thread Information Technology Ltd * All rights reserved * * SPDX-License-Identifier: Apache-2.0 @@ -13,7 +13,7 @@ #include <rthw.h> #include "ymodem.h" -#ifdef YMODEM_USING_CRC_TABLE +#ifdef YMODEM_USING_CRC_TABLE static const rt_uint16_t ccitt_table[256] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, diff --git a/components/utilities/ymodem/ymodem.h b/components/utilities/ymodem/ymodem.h index 8419290e13..7006cf268d 100644 --- a/components/utilities/ymodem/ymodem.h +++ b/components/utilities/ymodem/ymodem.h @@ -1,5 +1,5 @@ /* - * COPYRIGHT (C) 2012, Real-Thread Information Technology Ltd + * COPYRIGHT (C) 2011-2021, Real-Thread Information Technology Ltd * All rights reserved * * SPDX-License-Identifier: Apache-2.0 diff --git a/components/utilities/zmodem/crc.h b/components/utilities/zmodem/crc.h index fd11c1960a..c5251a7aa1 100644 --- a/components/utilities/zmodem/crc.h +++ b/components/utilities/zmodem/crc.h @@ -39,14 +39,14 @@ static unsigned short crctab[256] = { }; /* - * updcrc macro derived from article Copyright (C) 1986 Stephen Satchell. + * updcrc macro derived from article Copyright (C) 1986 Stephen Satchell. * NOTE: First srgument must be in range 0 to 255. * Second argument is referenced twice. - * - * Programmers may incorporate any or all code into their programs, - * giving proper credit within the source. Publication of the - * source routines is permitted so long as proper credit is given - * to Stephen Satchell, Satchell Evaluations and Chuck Forsberg, + * + * Programmers may incorporate any or all code into their programs, + * giving proper credit within the source. Publication of the + * source routines is permitted so long as proper credit is given + * to Stephen Satchell, Satchell Evaluations and Chuck Forsberg, * Omen Technology. */ diff --git a/components/utilities/zmodem/rz.c b/components/utilities/zmodem/rz.c index 1324b5994b..1845fd24c2 100644 --- a/components/utilities/zmodem/rz.c +++ b/components/utilities/zmodem/rz.c @@ -1,11 +1,11 @@ /* * File : rz.c - * the implemention of receiving files from the remote computers + * the implemention of receiving files from the remote computers * through the zmodem protocol. * Change Logs: * Date Author Notes - * 2011-03-29 itspy - * 2011-12-12 aozima fixed syntax error. + * 2011-03-29 itspy + * 2011-12-12 aozima fixed syntax error. */ #include <rtthread.h> @@ -35,293 +35,293 @@ void zr_start(char *path) { struct zfile *zf; rt_uint8_t n; - char ch,*p,*q; - rt_err_t res = -RT_ERROR; + char ch,*p,*q; + rt_err_t res = -RT_ERROR; - zf = rt_malloc(sizeof(struct zfile)); - if (zf == RT_NULL) - { - rt_kprintf("zf: out of memory\r\n"); - return; - } - memset(zf, 0, sizeof(struct zfile)); + zf = rt_malloc(sizeof(struct zfile)); + if (zf == RT_NULL) + { + rt_kprintf("zf: out of memory\r\n"); + return; + } + memset(zf, 0, sizeof(struct zfile)); zf->fname = path; - zf->fd = -1; - res = zrec_files(zf); - p = zf->fname; - for (;;) - { - q = strstr(p,"/"); - if (q == RT_NULL) break; - p = q+1; - } + zf->fd = -1; + res = zrec_files(zf); + p = zf->fname; + for (;;) + { + q = strstr(p,"/"); + if (q == RT_NULL) break; + p = q+1; + } if (res == RT_EOK) - { + { rt_kprintf("\b\b\bfile: %s \r\n",p); - rt_kprintf("size: %ld bytes\r\n",zf->bytes_received); - rt_kprintf("receive completed.\r\n"); - close(zf->fd); - rt_free(zf->fname); + rt_kprintf("size: %ld bytes\r\n",zf->bytes_received); + rt_kprintf("receive completed.\r\n"); + close(zf->fd); + rt_free(zf->fname); } else { rt_kprintf("\b\b\bfile: %s \r\n",p); - rt_kprintf("size: 0 bytes\r\n"); - rt_kprintf("receive failed.\r\n"); - if (zf->fd >= 0) - { - close(zf->fd); - unlink(zf->fname); /* remove this file */ - rt_free(zf->fname); - } + rt_kprintf("size: 0 bytes\r\n"); + rt_kprintf("receive failed.\r\n"); + if (zf->fd >= 0) + { + close(zf->fd); + unlink(zf->fname); /* remove this file */ + rt_free(zf->fname); + } + } + rt_free(zf); + /* waiting,clear console buffer */ + rt_thread_delay(RT_TICK_PER_SECOND/2); + while(1) + { + n=rt_device_read(shell->device, 0, &ch, 1); + if (n == 0) break; } - rt_free(zf); - /* waiting,clear console buffer */ - rt_thread_delay(RT_TICK_PER_SECOND/2); - while(1) - { - n=rt_device_read(shell->device, 0, &ch, 1); - if (n == 0) break; - } - return ; + return ; } /* receiver init, wait for ack */ static rt_err_t zrec_init(rt_uint8_t *rxbuf, struct zfile *zf) { rt_uint8_t err_cnt = 0; - rt_err_t res = -RT_ERROR; + rt_err_t res = -RT_ERROR; - for (;;) - { - zput_pos(0L); - tx_header[ZF0] = ZF0_CMD; - tx_header[ZF1] = ZF1_CMD; - tx_header[ZF2] = ZF2_CMD; - zsend_hex_header(ZRINIT, tx_header); + for (;;) + { + zput_pos(0L); + tx_header[ZF0] = ZF0_CMD; + tx_header[ZF1] = ZF1_CMD; + tx_header[ZF2] = ZF2_CMD; + zsend_hex_header(ZRINIT, tx_header); again: res = zget_header(rx_header); - switch(res) - { - case ZFILE: - ZF0_CMD = rx_header[ZF0]; - ZF1_CMD = rx_header[ZF1]; - ZF2_CMD = rx_header[ZF2]; - ZF3_CMD = rx_header[ZF3]; - res = zget_data(rxbuf, RX_BUFFER_SIZE); - if (res == GOTCRCW) - { - if ((res =zget_file_info((char*)rxbuf,zf))!= RT_EOK) - { - zsend_hex_header(ZSKIP, tx_header); - return (res); - } - return RT_EOK;; - } - zsend_hex_header(ZNAK, tx_header); - goto again; - case ZSINIT: - if (zget_data((rt_uint8_t*)Attn, ZATTNLEN) == GOTCRCW) /* send zack */ - { - zsend_hex_header(ZACK, tx_header); - goto again; - } - zsend_hex_header(ZNAK, tx_header); /* send znak */ - goto again; - case ZRQINIT: - continue; - case ZEOF: - continue; - case ZCOMPL: - goto again; - case ZFIN: /* end file session */ - zrec_ack_bibi(); - return res; - default: - if (++err_cnt >1000) return -RT_ERROR; - continue; - } - } + switch(res) + { + case ZFILE: + ZF0_CMD = rx_header[ZF0]; + ZF1_CMD = rx_header[ZF1]; + ZF2_CMD = rx_header[ZF2]; + ZF3_CMD = rx_header[ZF3]; + res = zget_data(rxbuf, RX_BUFFER_SIZE); + if (res == GOTCRCW) + { + if ((res =zget_file_info((char*)rxbuf,zf))!= RT_EOK) + { + zsend_hex_header(ZSKIP, tx_header); + return (res); + } + return RT_EOK;; + } + zsend_hex_header(ZNAK, tx_header); + goto again; + case ZSINIT: + if (zget_data((rt_uint8_t*)Attn, ZATTNLEN) == GOTCRCW) /* send zack */ + { + zsend_hex_header(ZACK, tx_header); + goto again; + } + zsend_hex_header(ZNAK, tx_header); /* send znak */ + goto again; + case ZRQINIT: + continue; + case ZEOF: + continue; + case ZCOMPL: + goto again; + case ZFIN: /* end file session */ + zrec_ack_bibi(); + return res; + default: + if (++err_cnt >1000) return -RT_ERROR; + continue; + } + } } /* receive files */ static rt_err_t zrec_files(struct zfile *zf) { - rt_uint8_t *rxbuf; - rt_err_t res = -RT_ERROR; + rt_uint8_t *rxbuf; + rt_err_t res = -RT_ERROR; - zinit_parameter(); - rxbuf = rt_malloc(RX_BUFFER_SIZE*sizeof(rt_uint8_t)); - if (rxbuf == RT_NULL) - { - rt_kprintf("rxbuf: out of memory\r\n"); - return -RT_ERROR; - } - rt_kprintf("\r\nrz: ready...\r\n"); /* here ready to receive things */ - if ((res = zrec_init(rxbuf,zf))!= RT_EOK) - { - rt_kprintf("\b\b\breceive init failed\r\n"); - rt_free(rxbuf); - return -RT_ERROR; - } - res = zrec_file(rxbuf,zf); - if (res == ZFIN) - { - rt_free(rxbuf); - return RT_EOK; /* if finish session */ - } - else if (res == ZCAN) - { + zinit_parameter(); + rxbuf = rt_malloc(RX_BUFFER_SIZE*sizeof(rt_uint8_t)); + if (rxbuf == RT_NULL) + { + rt_kprintf("rxbuf: out of memory\r\n"); + return -RT_ERROR; + } + rt_kprintf("\r\nrz: ready...\r\n"); /* here ready to receive things */ + if ((res = zrec_init(rxbuf,zf))!= RT_EOK) + { + rt_kprintf("\b\b\breceive init failed\r\n"); + rt_free(rxbuf); + return -RT_ERROR; + } + res = zrec_file(rxbuf,zf); + if (res == ZFIN) + { rt_free(rxbuf); - return ZCAN; /* cancel by sender */ - } - else - { - zsend_can(); - rt_free(rxbuf); - return res; - } + return RT_EOK; /* if finish session */ + } + else if (res == ZCAN) + { + rt_free(rxbuf); + return ZCAN; /* cancel by sender */ + } + else + { + zsend_can(); + rt_free(rxbuf); + return res; + } } /* receive file */ static rt_err_t zrec_file(rt_uint8_t *rxbuf, struct zfile *zf) { - rt_err_t res = - RT_ERROR; - rt_uint16_t err_cnt = 0; + rt_err_t res = - RT_ERROR; + rt_uint16_t err_cnt = 0; - do - { - zput_pos(zf->bytes_received); - zsend_hex_header(ZRPOS, tx_header); + do + { + zput_pos(zf->bytes_received); + zsend_hex_header(ZRPOS, tx_header); again: res = zget_header(rx_header); - switch (res) - { - case ZDATA: - zget_pos(Rxpos); - if (Rxpos != zf->bytes_received) - { - zsend_break(Attn); - continue; - } - err_cnt = 0; - res = zrec_file_data(rxbuf,zf); - if (res == -RT_ERROR) - { - zsend_break(Attn); - continue; - } - else if (res == GOTCAN) return res; - else goto again; - case ZRPOS: - zget_pos(Rxpos); - continue; - case ZEOF: - err_cnt = 0; - zget_pos(Rxpos); - if (Rxpos != zf->bytes_received || Rxpos != zf->bytes_total) - { - continue; - } - return (zrec_init(rxbuf,zf)); /* resend ZRINIT packet,ready to receive next file */ + switch (res) + { + case ZDATA: + zget_pos(Rxpos); + if (Rxpos != zf->bytes_received) + { + zsend_break(Attn); + continue; + } + err_cnt = 0; + res = zrec_file_data(rxbuf,zf); + if (res == -RT_ERROR) + { + zsend_break(Attn); + continue; + } + else if (res == GOTCAN) return res; + else goto again; + case ZRPOS: + zget_pos(Rxpos); + continue; + case ZEOF: + err_cnt = 0; + zget_pos(Rxpos); + if (Rxpos != zf->bytes_received || Rxpos != zf->bytes_total) + { + continue; + } + return (zrec_init(rxbuf,zf)); /* resend ZRINIT packet,ready to receive next file */ case ZFIN: - zrec_ack_bibi(); - return ZCOMPL; - case ZCAN: + zrec_ack_bibi(); + return ZCOMPL; + case ZCAN: #ifdef ZDEBUG rt_kprintf("error code: sender cancelled \r\n"); #endif - zf->bytes_received = 0L; /* throw the received data */ - return res; - case ZSKIP: - return res; - case -RT_ERROR: - zsend_break(Attn); - continue; - case ZNAK: - case TIMEOUT: - default: - continue; - } - } while(++err_cnt < 100); + zf->bytes_received = 0L; /* throw the received data */ + return res; + case ZSKIP: + return res; + case -RT_ERROR: + zsend_break(Attn); + continue; + case ZNAK: + case TIMEOUT: + default: + continue; + } + } while(++err_cnt < 100); - return res; + return res; } /* proccess file infomation */ static rt_err_t zget_file_info(char *name, struct zfile *zf) { - char *p; - char *full_path,*ptr; - rt_uint16_t i,len; - rt_err_t res = -RT_ERROR; - struct statfs buf; - struct stat finfo; + char *p; + char *full_path,*ptr; + rt_uint16_t i,len; + rt_err_t res = -RT_ERROR; + struct statfs buf; + struct stat finfo; - if (zf->fname == RT_NULL) /* extract file path */ + if (zf->fname == RT_NULL) /* extract file path */ { - len = strlen(name)+2; - } - else - len = strlen(zf->fname)+strlen(name)+2; + len = strlen(name)+2; + } + else + len = strlen(zf->fname)+strlen(name)+2; full_path = rt_malloc(len); - if (full_path == RT_NULL) - { - zsend_can(); - rt_kprintf("\b\b\bfull_path: out of memory\n"); - rt_free(full_path); - return -RT_ERROR; - } - memset(full_path,0,len); + if (full_path == RT_NULL) + { + zsend_can(); + rt_kprintf("\b\b\bfull_path: out of memory\n"); + rt_free(full_path); + return -RT_ERROR; + } + memset(full_path,0,len); for (i=0,ptr=zf->fname;i<len-strlen(name)-2;i++) - full_path[i] = *ptr++; + full_path[i] = *ptr++; full_path[len-strlen(name)-2] = '/'; - /* check if is a directory */ - if ((zf->fd=open(full_path, DFS_O_DIRECTORY,0)) < 0) - { - zsend_can(); - rt_kprintf("\b\b\bcan not open file:%s\r\n",zf->fname+1); - close(zf->fd); - zf->fd = -1; - rt_free(full_path); - return res; - } - fstat(zf->fd, &finfo); - if ((finfo.st_mode&S_IFDIR) != S_IFDIR) - { - close(zf->fd); - zf->fd = -1; - return res; - } - close(zf->fd); - /* get fullpath && file attributes */ + /* check if is a directory */ + if ((zf->fd=open(full_path, DFS_O_DIRECTORY,0)) < 0) + { + zsend_can(); + rt_kprintf("\b\b\bcan not open file:%s\r\n",zf->fname+1); + close(zf->fd); + zf->fd = -1; + rt_free(full_path); + return res; + } + fstat(zf->fd, &finfo); + if ((finfo.st_mode&S_IFDIR) != S_IFDIR) + { + close(zf->fd); + zf->fd = -1; + return res; + } + close(zf->fd); + /* get fullpath && file attributes */ strcat(full_path,name); zf->fname = full_path; - p = strlen(name)+name+1; - sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode); + p = strlen(name)+name+1; + sscanf((const char *)p, "%ld%lo%o", &zf->bytes_total,&zf->ctime,&zf->mode); #if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR) - dfs_statfs(working_directory,&buf); - if (zf->bytes_total > (buf.f_blocks * buf.f_bfree)) - { - zsend_can(); - rt_kprintf("\b\b\bnot enough disk space\r\n"); - zf->fd = -1; - rt_free(full_path); - return -RT_ERROR; - } + dfs_statfs(working_directory,&buf); + if (zf->bytes_total > (buf.f_blocks * buf.f_bfree)) + { + zsend_can(); + rt_kprintf("\b\b\bnot enough disk space\r\n"); + zf->fd = -1; + rt_free(full_path); + return -RT_ERROR; + } #else buf = buf; #endif - zf->bytes_received = 0L; - if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0) /* create or replace exist file */ - { - zsend_can(); - rt_kprintf("\b\b\bcan not create file:%s \r\n",zf->fname); - return -RT_ERROR; - } + zf->bytes_received = 0L; + if ((zf->fd = open(zf->fname,DFS_O_CREAT|DFS_O_WRONLY,0)) < 0) /* create or replace exist file */ + { + zsend_can(); + rt_kprintf("\b\b\bcan not create file:%s \r\n",zf->fname); + return -RT_ERROR; + } - return RT_EOK; + return RT_EOK; } /* receive file data,continously, no ack */ @@ -330,72 +330,72 @@ static rt_err_t zrec_file_data(rt_uint8_t *buf, struct zfile *zf) rt_err_t res = -RT_ERROR; more_data: - res = zget_data(buf,RX_BUFFER_SIZE); - switch(res) - { - case GOTCRCW: /* zack received */ - zwrite_file(buf,Rxcount,zf); - zf->bytes_received += Rxcount; - zput_pos(zf->bytes_received); - zsend_line(XON); - zsend_hex_header(ZACK, tx_header); - return RT_EOK; - case GOTCRCQ: - zwrite_file(buf,Rxcount,zf); - zf->bytes_received += Rxcount; - zput_pos(zf->bytes_received); - zsend_hex_header(ZACK, tx_header); - goto more_data; - case GOTCRCG: - zwrite_file(buf,Rxcount,zf); - zf->bytes_received += Rxcount; - goto more_data; - case GOTCRCE: - zwrite_file(buf,Rxcount,zf); - zf->bytes_received += Rxcount; - return RT_EOK; - case GOTCAN: + res = zget_data(buf,RX_BUFFER_SIZE); + switch(res) + { + case GOTCRCW: /* zack received */ + zwrite_file(buf,Rxcount,zf); + zf->bytes_received += Rxcount; + zput_pos(zf->bytes_received); + zsend_line(XON); + zsend_hex_header(ZACK, tx_header); + return RT_EOK; + case GOTCRCQ: + zwrite_file(buf,Rxcount,zf); + zf->bytes_received += Rxcount; + zput_pos(zf->bytes_received); + zsend_hex_header(ZACK, tx_header); + goto more_data; + case GOTCRCG: + zwrite_file(buf,Rxcount,zf); + zf->bytes_received += Rxcount; + goto more_data; + case GOTCRCE: + zwrite_file(buf,Rxcount,zf); + zf->bytes_received += Rxcount; + return RT_EOK; + case GOTCAN: #ifdef ZDEBUG - rt_kprintf("error code : ZCAN \r\n"); + rt_kprintf("error code : ZCAN \r\n"); #endif - return res; - case TIMEOUT: - return res; + return res; + case TIMEOUT: + return res; case -RT_ERROR: - zsend_break(Attn); - return res; - default: - return res; - } + zsend_break(Attn); + return res; + default: + return res; + } } /* write file */ static rt_err_t zwrite_file(rt_uint8_t *buf,rt_uint16_t size, struct zfile *zf) { - return (write(zf->fd,buf,size)); + return (write(zf->fd,buf,size)); } /* ack bibi */ static void zrec_ack_bibi(void) { - rt_uint8_t i; + rt_uint8_t i; - zput_pos(0L); - for (i=0;i<3;i++) - { - zsend_hex_header(ZFIN, tx_header); - switch (zread_line(100)) - { - case 'O': - zread_line(1); - return; - case RCDO: - return; - case TIMEOUT: - default: - break; - } - } + zput_pos(0L); + for (i=0;i<3;i++) + { + zsend_hex_header(ZFIN, tx_header); + switch (zread_line(100)) + { + case 'O': + zread_line(1); + return; + case RCDO: + return; + case TIMEOUT: + default: + break; + } + } } /* end of rz.c */ diff --git a/components/utilities/zmodem/sz.c b/components/utilities/zmodem/sz.c index 0fde4165f6..0b0b5515f0 100644 --- a/components/utilities/zmodem/sz.c +++ b/components/utilities/zmodem/sz.c @@ -1,10 +1,10 @@ /* * File : sz.c - * the implemention of sending files to the remote computers + * the implemention of sending files to the remote computers * through the zmodem protocol. * Change Logs: * Date Author Notes - * 2011-03-29 itspy + * 2011-03-29 itspy */ #include <rtthread.h> @@ -17,10 +17,10 @@ #include "zdef.h" -static rt_uint8_t TX_BUFFER[TX_BUFFER_SIZE]; /* sender buffer */ -static rt_uint8_t file_cnt = 0; /* count of number of files opened */ -static rt_uint8_t Rxflags = 0; /* rx parameter flags */ -static rt_uint8_t ZF2_OP; /* file transfer option */ +static rt_uint8_t TX_BUFFER[TX_BUFFER_SIZE]; /* sender buffer */ +static rt_uint8_t file_cnt = 0; /* count of number of files opened */ +static rt_uint8_t Rxflags = 0; /* rx parameter flags */ +static rt_uint8_t ZF2_OP; /* file transfer option */ void zs_start(char *path); static void zsend_init(void); @@ -38,233 +38,233 @@ static void zsay_bibi(void); void zs_start(char *path) { struct zfile *zf; - rt_err_t res = RT_ERROR; + rt_err_t res = RT_ERROR; char *p,*q; - zf = rt_malloc(sizeof(struct zfile)); - if (zf == RT_NULL) - { - rt_kprintf("zf: out of memory\r\n"); - return; - } - rt_kprintf("\r\nsz: ready...\r\n"); /* here ready to send things */ - memset(zf, 0, sizeof(struct zfile)); + zf = rt_malloc(sizeof(struct zfile)); + if (zf == RT_NULL) + { + rt_kprintf("zf: out of memory\r\n"); + return; + } + rt_kprintf("\r\nsz: ready...\r\n"); /* here ready to send things */ + memset(zf, 0, sizeof(struct zfile)); zf->fname = path; - zf->fd = -1; - res = zsend_files(zf); - p = zf->fname; - for (;;) - { - q = strstr(p,"/"); - if (q == RT_NULL) break; - p = q+1; - } + zf->fd = -1; + res = zsend_files(zf); + p = zf->fname; + for (;;) + { + q = strstr(p,"/"); + if (q == RT_NULL) break; + p = q+1; + } if (res == RT_EOK) { rt_kprintf("\r\nfile: %s \r\nsize: %ld bytes\r\nsend completed.\r\n", - p,zf->bytes_received); + p,zf->bytes_received); } else { rt_kprintf("\r\nfile: %s \r\nsize: 0 bytes\r\nsend failed.\r\n",p); } - rt_free(zf); + rt_free(zf); - return; + return; } /* init the parameters */ static void zsend_init(void) { - rt_err_t res = -RT_ERROR; + rt_err_t res = -RT_ERROR; - zinit_parameter(); - for(;;) /* wait ZPAD */ - { - res = zread_line(800); - if (res == ZPAD) break; - } - for (;;) - { - res = zget_header(rx_header); - if (res == ZRINIT) break; - } - if ((rx_header[ZF1] & ZRQNVH)) - { - zput_pos(0x80L); /* Show we can var header */ - zsend_hex_header(ZRQINIT, tx_header); - } - Rxflags = rx_header[ZF0] & 0377; - if (Rxflags & CANFC32) Txfcs32 = 1; /* used 32bits CRC check */ + zinit_parameter(); + for(;;) /* wait ZPAD */ + { + res = zread_line(800); + if (res == ZPAD) break; + } + for (;;) + { + res = zget_header(rx_header); + if (res == ZRINIT) break; + } + if ((rx_header[ZF1] & ZRQNVH)) + { + zput_pos(0x80L); /* Show we can var header */ + zsend_hex_header(ZRQINIT, tx_header); + } + Rxflags = rx_header[ZF0] & 0377; + if (Rxflags & CANFC32) Txfcs32 = 1; /* used 32bits CRC check */ - if (ZF2_OP == ZTRLE && (Rxflags & CANRLE)) /* for RLE packet */ - Txfcs32 = 2; - else - ZF2_OP = 0; + if (ZF2_OP == ZTRLE && (Rxflags & CANRLE)) /* for RLE packet */ + Txfcs32 = 2; + else + ZF2_OP = 0; /* send SINIT cmd */ - return; + return; } /* send files */ static rt_err_t zsend_files(struct zfile *zf) { - char *p,*q; - char *str = "/"; - struct stat finfo; - rt_err_t res = -RT_ERROR; + char *p,*q; + char *str = "/"; + struct stat finfo; + rt_err_t res = -RT_ERROR; - if (zf->fname == RT_NULL) - { - rt_kprintf("\r\nerror: no file to be send.\r\n"); - return res; - } - if ((zf->fd=open(zf->fname, DFS_O_RDONLY,0)) <0) - { - rt_kprintf("\r\ncan not open file:%s\r\n",zf->fname+1); - return res; - } + if (zf->fname == RT_NULL) + { + rt_kprintf("\r\nerror: no file to be send.\r\n"); + return res; + } + if ((zf->fd=open(zf->fname, DFS_O_RDONLY,0)) <0) + { + rt_kprintf("\r\ncan not open file:%s\r\n",zf->fname+1); + return res; + } - zf->file_end = 0; - ++file_cnt; - /* extract file name */ - p = zf->fname; - for (;;) - { - q = strstr(p,str); - if (q == RT_NULL) break; - p = q+1; - } - q = (char*)TX_BUFFER; - for (;;) - { - *q++ = *p++; - if (*p == 0) break; - } - *q++ = 0; - p=q; - while (q < (char*)(TX_BUFFER + 1024)) - *q++ = 0; - /* get file attributes */ - fstat(zf->fd,&finfo); - Left_sizes += finfo.st_size; - rt_sprintf(p, "%lu %lo %o 3 %d %ld", (long)finfo.st_size, finfo.st_mtime, - finfo.st_mode, file_cnt, Left_sizes); - Left_sizes -= finfo.st_size; - TX_BUFFER[127] = (finfo.st_size + 127) >>7; - TX_BUFFER[126] = (finfo.st_size + 127) >>15; + zf->file_end = 0; + ++file_cnt; + /* extract file name */ + p = zf->fname; + for (;;) + { + q = strstr(p,str); + if (q == RT_NULL) break; + p = q+1; + } + q = (char*)TX_BUFFER; + for (;;) + { + *q++ = *p++; + if (*p == 0) break; + } + *q++ = 0; + p=q; + while (q < (char*)(TX_BUFFER + 1024)) + *q++ = 0; + /* get file attributes */ + fstat(zf->fd,&finfo); + Left_sizes += finfo.st_size; + rt_sprintf(p, "%lu %lo %o 3 %d %ld", (long)finfo.st_size, finfo.st_mtime, + finfo.st_mode, file_cnt, Left_sizes); + Left_sizes -= finfo.st_size; + TX_BUFFER[127] = (finfo.st_size + 127) >>7; + TX_BUFFER[126] = (finfo.st_size + 127) >>15; - zsend_init(); - /* start sending files */ - res = zsend_file(zf,TX_BUFFER, (p-(char*)TX_BUFFER)+strlen(p)+1); - zsay_bibi(); - close(zf->fd); - - return res; + zsend_init(); + /* start sending files */ + res = zsend_file(zf,TX_BUFFER, (p-(char*)TX_BUFFER)+strlen(p)+1); + zsay_bibi(); + close(zf->fd); + + return res; } /* send file name and related info */ static rt_err_t zsend_file(struct zfile *zf, rt_uint8_t *buf, rt_uint16_t len) { - rt_uint8_t cnt; - rt_err_t res = -RT_ERROR; + rt_uint8_t cnt; + rt_err_t res = -RT_ERROR; - for (cnt=0;cnt<5;cnt++) - { - tx_header[ZF0] = ZF0_CMD; /* file conversion option */ - tx_header[ZF1] = ZF1_CMD; /* file management option */ - tx_header[ZF2] = (ZF3_CMD|ZF2_OP); /* file transfer option */ - tx_header[ZF3] = ZF3_CMD; - zsend_bin_header(ZFILE, tx_header); - zsend_bin_data(buf, len, ZCRCW); + for (cnt=0;cnt<5;cnt++) + { + tx_header[ZF0] = ZF0_CMD; /* file conversion option */ + tx_header[ZF1] = ZF1_CMD; /* file management option */ + tx_header[ZF2] = (ZF3_CMD|ZF2_OP); /* file transfer option */ + tx_header[ZF3] = ZF3_CMD; + zsend_bin_header(ZFILE, tx_header); + zsend_bin_data(buf, len, ZCRCW); loop: - res = zget_header(rx_header); - switch (res) - { - case ZRINIT: - while ((res = zread_line(50)) > 0) - { - if (res == ZPAD) - { - goto loop; - } - } - break; - case ZCAN: - case TIMEOUT: - case ZABORT: - case ZFIN: + res = zget_header(rx_header); + switch (res) + { + case ZRINIT: + while ((res = zread_line(50)) > 0) + { + if (res == ZPAD) + { + goto loop; + } + } + break; + case ZCAN: + case TIMEOUT: + case ZABORT: + case ZFIN: + break; + case -RT_ERROR: + case ZNAK: + break; + case ZCRC: /* no CRC request */ + goto loop; + case ZFERR: + case ZSKIP: break; - case -RT_ERROR: - case ZNAK: - break; - case ZCRC: /* no CRC request */ - goto loop; - case ZFERR: - case ZSKIP: - break; - case ZRPOS: /* here we want */ - zget_pos(Rxpos); - Txpos = Rxpos; - return(zsend_file_data(zf)); - default: - break; - } - } + case ZRPOS: /* here we want */ + zget_pos(Rxpos); + Txpos = Rxpos; + return(zsend_file_data(zf)); + default: + break; + } + } - return res; + return res; } /* send the file data */ static rt_err_t zsend_file_data(struct zfile *zf) { - rt_int16_t cnt; - rt_uint8_t cmd; - rt_err_t res = -RT_ERROR; - /* send ZDATA packet, start to send data */ + rt_int16_t cnt; + rt_uint8_t cmd; + rt_err_t res = -RT_ERROR; + /* send ZDATA packet, start to send data */ start_send: - zput_pos(Txpos); - zsend_bin_header(ZDATA, tx_header); - do - { - cnt = zfill_buffer(zf,TX_BUFFER,RX_BUFFER_SIZE); - if (cnt < RX_BUFFER_SIZE ) - cmd = ZCRCE; - else - cmd = ZCRCG; - zsend_bin_data(TX_BUFFER, cnt, cmd); - zf->bytes_received= Txpos += cnt; - if (cmd == ZCRCW) - goto get_syn1; - } while (cnt == RX_BUFFER_SIZE); - for (;;) /* get ack and check if send finish */ - { - zput_pos(Txpos); - zsend_bin_header(ZEOF, tx_header); + zput_pos(Txpos); + zsend_bin_header(ZDATA, tx_header); + do + { + cnt = zfill_buffer(zf,TX_BUFFER,RX_BUFFER_SIZE); + if (cnt < RX_BUFFER_SIZE ) + cmd = ZCRCE; + else + cmd = ZCRCG; + zsend_bin_data(TX_BUFFER, cnt, cmd); + zf->bytes_received= Txpos += cnt; + if (cmd == ZCRCW) + goto get_syn1; + } while (cnt == RX_BUFFER_SIZE); + for (;;) /* get ack and check if send finish */ + { + zput_pos(Txpos); + zsend_bin_header(ZEOF, tx_header); get_syn1: res = zget_sync(); - switch (res) - { - case ZACK: - goto get_syn1; - case ZNAK: - continue; - case ZRPOS: /* resend here */ - lseek(zf->fd,Txpos,0); - goto start_send; - case ZRINIT: /* send finish,then begin to send next file */ - return RT_EOK; - case ZSKIP: - case -RT_ERROR: - return res; - default: + switch (res) + { + case ZACK: + goto get_syn1; + case ZNAK: + continue; + case ZRPOS: /* resend here */ + lseek(zf->fd,Txpos,0); + goto start_send; + case ZRINIT: /* send finish,then begin to send next file */ + return RT_EOK; + case ZSKIP: + case -RT_ERROR: return res; - } - } + default: + return res; + } + } } /* fill file data to buffer*/ static rt_uint16_t zfill_buffer(struct zfile *zf, rt_uint8_t *buf, rt_uint16_t size) { - return (read(zf->fd,buf,size)); + return (read(zf->fd,buf,size)); } /* wait sync(ack) from the receiver */ @@ -272,50 +272,50 @@ static rt_err_t zget_sync(void) { rt_err_t res = -RT_ERROR; - for (;;) - { - res = zget_header(rx_header); - switch (res) - { - case ZCAN: - case ZABORT: - case ZFIN: - case TIMEOUT: - return -RT_ERROR; - case ZRPOS: /* get pos, need to resend */ - zget_pos(Rxpos); - Txpos = Rxpos; - return res; - case ZACK: - return res; - case ZRINIT: /* get ZRINIT indicate that the prev file send completed */ - return res; - case ZSKIP: - return res; - case -RT_ERROR: - default: - zsend_bin_header(ZNAK, tx_header); - continue; - } - } + for (;;) + { + res = zget_header(rx_header); + switch (res) + { + case ZCAN: + case ZABORT: + case ZFIN: + case TIMEOUT: + return -RT_ERROR; + case ZRPOS: /* get pos, need to resend */ + zget_pos(Rxpos); + Txpos = Rxpos; + return res; + case ZACK: + return res; + case ZRINIT: /* get ZRINIT indicate that the prev file send completed */ + return res; + case ZSKIP: + return res; + case -RT_ERROR: + default: + zsend_bin_header(ZNAK, tx_header); + continue; + } + } } /* say "bibi" to the receiver */ static void zsay_bibi(void) { - for (;;) - { - zput_pos(0L); /* reninit position of next file*/ - zsend_hex_header(ZFIN, tx_header); /* send finished session cmd */ - switch (zget_header(rx_header)) - { - case ZFIN: - zsend_line('O'); - zsend_line('O'); - case ZCAN: - case TIMEOUT: - return; - } - } + for (;;) + { + zput_pos(0L); /* reninit position of next file*/ + zsend_hex_header(ZFIN, tx_header); /* send finished session cmd */ + switch (zget_header(rx_header)) + { + case ZFIN: + zsend_line('O'); + zsend_line('O'); + case ZCAN: + case TIMEOUT: + return; + } + } } /* end of sz.c */ diff --git a/components/utilities/zmodem/zcore.c b/components/utilities/zmodem/zcore.c index 02ffbba830..75d9647348 100644 --- a/components/utilities/zmodem/zcore.c +++ b/components/utilities/zmodem/zcore.c @@ -3,7 +3,7 @@ * the core functions of implementing zmodem protocol * Change Logs: * Date Author Notes - * 2011-03-29 itspy + * 2011-03-29 itspy */ #include <rtthread.h> @@ -16,22 +16,22 @@ #include <stdio.h> #include "zdef.h" -char ZF0_CMD; /* file conversion request */ -char ZF1_CMD; /* file management request */ -char ZF2_CMD; /* file transport request */ -char ZF3_CMD; -rt_uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */ -rt_uint16_t Rxcount; /* received count*/ -char header_type; /* header type */ -rt_uint8_t rx_header[4]; /* received header */ -rt_uint8_t tx_header[4]; /* transmitted header */ -rt_uint32_t Rxpos; /* received file position */ -rt_uint32_t Txpos; /* transmitted file position */ -rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */ -rt_uint8_t TxCRC; /* controls 32 bit CRC being sent */ -rt_uint8_t RxCRC; /* indicates/controls 32 bit CRC being received */ +char ZF0_CMD; /* file conversion request */ +char ZF1_CMD; /* file management request */ +char ZF2_CMD; /* file transport request */ +char ZF3_CMD; +rt_uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */ +rt_uint16_t Rxcount; /* received count*/ +char header_type; /* header type */ +rt_uint8_t rx_header[4]; /* received header */ +rt_uint8_t tx_header[4]; /* transmitted header */ +rt_uint32_t Rxpos; /* received file position */ +rt_uint32_t Txpos; /* transmitted file position */ +rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */ +rt_uint8_t TxCRC; /* controls 32 bit CRC being sent */ +rt_uint8_t RxCRC; /* indicates/controls 32 bit CRC being received */ /* 0 == CRC16, 1 == CRC32, 2 == CRC32 + RLE */ -char Attn[ZATTNLEN+1]; /* attention string rx sends to tx on err */ +char Attn[ZATTNLEN+1]; /* attention string rx sends to tx on err */ void zinit_parameter(void); void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr); @@ -52,670 +52,670 @@ rt_int16_t zread_byte(void); rt_int16_t zxor_read(void); void zput_pos(rt_uint32_t pos); void zget_pos(rt_uint32_t pos); - - + + void zinit_parameter(void) { rt_uint8_t i; - ZF0_CMD = CANFC32|CANFDX|CANOVIO; /* not chose CANFC32,CANRLE,although it have been supported */ - ZF1_CMD = 0; /* fix header length,not support CANVHDR */ - ZF2_CMD = 0; - ZF3_CMD = 0; - Rxframeind =0; - header_type = 0; - Rxcount = 0; - for (i=0;i<4;i++) rx_header[i] = tx_header[i] = 0; - Rxpos = Txpos = 0; - RxCRC = 0; - Txfcs32 = 0; + ZF0_CMD = CANFC32|CANFDX|CANOVIO; /* not chose CANFC32,CANRLE,although it have been supported */ + ZF1_CMD = 0; /* fix header length,not support CANVHDR */ + ZF2_CMD = 0; + ZF3_CMD = 0; + Rxframeind =0; + header_type = 0; + Rxcount = 0; + for (i=0;i<4;i++) rx_header[i] = tx_header[i] = 0; + Rxpos = Txpos = 0; + RxCRC = 0; + Txfcs32 = 0; - return ; + return ; } /* send binary header */ void zsend_bin_header(rt_uint8_t type, rt_uint8_t *hdr) { - rt_uint8_t i; - rt_uint32_t crc; + rt_uint8_t i; + rt_uint32_t crc; - zsend_byte(ZPAD); - zsend_byte(ZDLE); - TxCRC = Txfcs32; - if (TxCRC == 0) - { - zsend_byte(ZBIN); - zsend_zdle_char(type); - /* add 16bits crc */ - crc = 0L; - crc = updcrc16(type, 0); - for (i=0;i<4;i++) - { - zsend_zdle_char(*hdr); - crc = updcrc16((0377 & *hdr++),crc); - } - crc = updcrc16(0,updcrc16(0,crc)); - zsend_zdle_char(((int)(crc>>8))); - zsend_zdle_char(crc); - } - else if(TxCRC == 1) - { + zsend_byte(ZPAD); + zsend_byte(ZDLE); + TxCRC = Txfcs32; + if (TxCRC == 0) + { + zsend_byte(ZBIN); + zsend_zdle_char(type); + /* add 16bits crc */ + crc = 0L; + crc = updcrc16(type, 0); + for (i=0;i<4;i++) + { + zsend_zdle_char(*hdr); + crc = updcrc16((0377 & *hdr++),crc); + } + crc = updcrc16(0,updcrc16(0,crc)); + zsend_zdle_char(((int)(crc>>8))); + zsend_zdle_char(crc); + } + else if(TxCRC == 1) + { zsend_byte(ZBIN32); - zsend_zdle_char(type); - /* add 32bits crc */ - crc = 0xffffffffL; - crc = updcrc32(type, crc); + zsend_zdle_char(type); + /* add 32bits crc */ + crc = 0xffffffffL; + crc = updcrc32(type, crc); for (i=0;i<4;i++) - { - zsend_zdle_char(*hdr); - crc = updcrc32((0377 & *hdr++), crc); - } - crc = ~crc; - for (i=0; i<4;i++) - { - zsend_zdle_char(crc); - crc >>= 8; - } - } - else if (TxCRC == 2) - { - zsend_byte(ZBINR32); - zsend_zdle_char(type); - /* add 32bits crc */ - crc = 0xffffffffL; - crc = updcrc32(type, crc); + { + zsend_zdle_char(*hdr); + crc = updcrc32((0377 & *hdr++), crc); + } + crc = ~crc; + for (i=0; i<4;i++) + { + zsend_zdle_char(crc); + crc >>= 8; + } + } + else if (TxCRC == 2) + { + zsend_byte(ZBINR32); + zsend_zdle_char(type); + /* add 32bits crc */ + crc = 0xffffffffL; + crc = updcrc32(type, crc); for (i=0;i<4;i++) - { - zsend_zdle_char(*hdr); - crc = updcrc32((0377 & *hdr++), crc); - } - crc = ~crc; - for (i=0; i<4;i++) - { - zsend_zdle_char(crc); - crc >>= 8; - } - } + { + zsend_zdle_char(*hdr); + crc = updcrc32((0377 & *hdr++), crc); + } + crc = ~crc; + for (i=0; i<4;i++) + { + zsend_zdle_char(crc); + crc >>= 8; + } + } - return; + return; } /* send hex header */ void zsend_hex_header(rt_uint8_t type, rt_uint8_t *hdr) { - rt_uint8_t i; - rt_uint16_t crc; + rt_uint8_t i; + rt_uint16_t crc; - zsend_line(ZPAD); zsend_line(ZPAD); zsend_line(ZDLE); - zsend_line(ZHEX); - zsend_ascii(type); - crc = updcrc16(type, 0); - for (i=0; i<4; i++) - { - zsend_ascii(*hdr); - crc = updcrc16((0377 & *hdr++), crc); - } - crc = updcrc16(0,updcrc16(0,crc)); - zsend_ascii(crc>>8); - zsend_ascii(crc); - /* send display control cmd */ - zsend_line(015); zsend_line(0212); - if (type != ZFIN && type != ZACK) - zsend_line(021); - TxCRC = 0; /* clear tx crc type */ + zsend_line(ZPAD); zsend_line(ZPAD); zsend_line(ZDLE); + zsend_line(ZHEX); + zsend_ascii(type); + crc = updcrc16(type, 0); + for (i=0; i<4; i++) + { + zsend_ascii(*hdr); + crc = updcrc16((0377 & *hdr++), crc); + } + crc = updcrc16(0,updcrc16(0,crc)); + zsend_ascii(crc>>8); + zsend_ascii(crc); + /* send display control cmd */ + zsend_line(015); zsend_line(0212); + if (type != ZFIN && type != ZACK) + zsend_line(021); + TxCRC = 0; /* clear tx crc type */ - return; + return; } /* send binary data,with frameend */ void zsend_bin_data(rt_uint8_t *buf, rt_int16_t len, rt_uint8_t frameend) { rt_int16_t i,c,tmp; - rt_uint32_t crc; + rt_uint32_t crc; if (TxCRC == 0) /* send binary data with 16bits crc check */ - { - crc = 0x0L; - for (i=0;i<len;i++) - { - zsend_zdle_char(*buf); - crc = updcrc16((0377 & *buf++), crc); - } - zsend_byte(ZDLE); zsend_byte(frameend); - crc = updcrc16(frameend, crc); - crc = updcrc16(0,updcrc16(0,crc)); - zsend_zdle_char(crc>>8); - zsend_zdle_char(crc); - } - else if (TxCRC == 1) /* send binary data with 32 bits crc check */ - { - crc = 0xffffffffL; - for (i=0;i<len;i++) - { - c = *buf++ & 0377; - zsend_zdle_char(c); - crc = updcrc32(c, crc); - } - zsend_byte(ZDLE); zsend_byte(frameend); - crc = updcrc32(frameend, crc); - crc = ~crc; - for (i=0;i<4;i++) - { - zsend_zdle_char((int)crc); crc >>= 8; - } - } - else if (TxCRC == 2) /* send binary data with 32bits crc check,RLE encode */ - { - crc = 0xffffffffL; + { + crc = 0x0L; + for (i=0;i<len;i++) + { + zsend_zdle_char(*buf); + crc = updcrc16((0377 & *buf++), crc); + } + zsend_byte(ZDLE); zsend_byte(frameend); + crc = updcrc16(frameend, crc); + crc = updcrc16(0,updcrc16(0,crc)); + zsend_zdle_char(crc>>8); + zsend_zdle_char(crc); + } + else if (TxCRC == 1) /* send binary data with 32 bits crc check */ + { + crc = 0xffffffffL; + for (i=0;i<len;i++) + { + c = *buf++ & 0377; + zsend_zdle_char(c); + crc = updcrc32(c, crc); + } + zsend_byte(ZDLE); zsend_byte(frameend); + crc = updcrc32(frameend, crc); + crc = ~crc; + for (i=0;i<4;i++) + { + zsend_zdle_char((int)crc); crc >>= 8; + } + } + else if (TxCRC == 2) /* send binary data with 32bits crc check,RLE encode */ + { + crc = 0xffffffffL; tmp = *buf++ & 0377; - for (i = 0; --len >= 0; ++buf) - { - if ((c = *buf & 0377) == tmp && i < 126 && len>0) - { - ++i; continue; - } - if (i==0) - { - zsend_zdle_char(tmp); - crc = updcrc32(tmp, crc); - if (tmp == ZRESC) - { - zsend_zdle_char(0100); crc = updcrc32(0100, crc); - } - tmp = c; - } - else if (i == 1) - { - if (tmp != ZRESC) - { - zsend_zdle_char(tmp); zsend_zdle_char(tmp); - crc = updcrc32(tmp, crc); - crc = updcrc32(tmp, crc); - i = 0; tmp = c; - } + for (i = 0; --len >= 0; ++buf) + { + if ((c = *buf & 0377) == tmp && i < 126 && len>0) + { + ++i; continue; + } + if (i==0) + { + zsend_zdle_char(tmp); + crc = updcrc32(tmp, crc); + if (tmp == ZRESC) + { + zsend_zdle_char(0100); crc = updcrc32(0100, crc); + } + tmp = c; + } + else if (i == 1) + { + if (tmp != ZRESC) + { + zsend_zdle_char(tmp); zsend_zdle_char(tmp); + crc = updcrc32(tmp, crc); + crc = updcrc32(tmp, crc); + i = 0; tmp = c; + } - } - else - { - zsend_zdle_char(ZRESC); crc = updcrc32(ZRESC, crc); - if (tmp == 040 && i < 34) - { - i += 036; - zsend_zdle_char(i); - crc = updcrc32(i, crc); - } - else - { - i += 0101; - zsend_zdle_char(i); crc = updcrc32(i, crc); - zsend_zdle_char(tmp); crc = updcrc32(tmp, crc); - } - i = 0; tmp = c; - } - } - zsend_byte(ZDLE); zsend_byte(frameend); - crc = updcrc32(frameend, crc); - crc = ~crc; - for (i=0;i<4;i++) - { - zsend_zdle_char(crc); - crc >>= 8; - } - } - if (frameend == ZCRCW) - zsend_byte(XON); + } + else + { + zsend_zdle_char(ZRESC); crc = updcrc32(ZRESC, crc); + if (tmp == 040 && i < 34) + { + i += 036; + zsend_zdle_char(i); + crc = updcrc32(i, crc); + } + else + { + i += 0101; + zsend_zdle_char(i); crc = updcrc32(i, crc); + zsend_zdle_char(tmp); crc = updcrc32(tmp, crc); + } + i = 0; tmp = c; + } + } + zsend_byte(ZDLE); zsend_byte(frameend); + crc = updcrc32(frameend, crc); + crc = ~crc; + for (i=0;i<4;i++) + { + zsend_zdle_char(crc); + crc >>= 8; + } + } + if (frameend == ZCRCW) + zsend_byte(XON); - return; + return; } /* receive data,with 16bits CRC check */ static rt_int16_t zrec_data16(rt_uint8_t *buf, rt_uint16_t len) { - rt_int16_t c,crc_cnt; - rt_uint16_t crc; - rt_err_t res = -RT_ERROR; - rt_uint8_t *p,flag = 0; + rt_int16_t c,crc_cnt; + rt_uint16_t crc; + rt_err_t res = -RT_ERROR; + rt_uint8_t *p,flag = 0; - p = buf; - crc_cnt = 0; crc = 0L; - Rxcount = 0; - while(buf <= p+len) - { - if ((res = zread_byte()) & ~0377) - { - if (res == GOTCRCE || res == GOTCRCG || - res == GOTCRCQ || res == GOTCRCW) - { - c = res; - c = res; - crc = updcrc16(res&0377, crc); - flag = 1; - continue; - } - else if (res == GOTCAN) return ZCAN; - else if (res == TIMEOUT) return TIMEOUT; - else return res; + p = buf; + crc_cnt = 0; crc = 0L; + Rxcount = 0; + while(buf <= p+len) + { + if ((res = zread_byte()) & ~0377) + { + if (res == GOTCRCE || res == GOTCRCG || + res == GOTCRCQ || res == GOTCRCW) + { + c = res; + c = res; + crc = updcrc16(res&0377, crc); + flag = 1; + continue; + } + else if (res == GOTCAN) return ZCAN; + else if (res == TIMEOUT) return TIMEOUT; + else return res; - } - else - { - if (flag) - { - crc = updcrc16(res, crc); - crc_cnt++; - if (crc_cnt < 2) continue; - if ((crc & 0xffff)) - { + } + else + { + if (flag) + { + crc = updcrc16(res, crc); + crc_cnt++; + if (crc_cnt < 2) continue; + if ((crc & 0xffff)) + { #ifdef ZDEBUG - rt_kprintf("error code: CRC16 error \r\n"); + rt_kprintf("error code: CRC16 error \r\n"); #endif - return -RT_ERROR; - } + return -RT_ERROR; + } return c; - } - else - { - *buf++ = res; - Rxcount++; - crc = updcrc16(res, crc); - } - } - } + } + else + { + *buf++ = res; + Rxcount++; + crc = updcrc16(res, crc); + } + } + } - return -RT_ERROR; + return -RT_ERROR; } /* receive data,with 32bits CRC check */ static rt_int16_t zrec_data32(rt_uint8_t *buf, rt_int16_t len) { - rt_int16_t c,crc_cnt; - rt_uint32_t crc; - rt_err_t res = -RT_ERROR; - rt_uint8_t *p,flag = 0; + rt_int16_t c,crc_cnt; + rt_uint32_t crc; + rt_err_t res = -RT_ERROR; + rt_uint8_t *p,flag = 0; - crc_cnt = 0; crc = 0xffffffffL; - Rxcount = 0; - while (buf <= p+len) - { - if ((res = zread_byte()) & ~0377) - { - if (res == GOTCRCE || res == GOTCRCG || - res == GOTCRCQ || res == GOTCRCW) - { - c = res; - crc = updcrc32(res&0377, crc); - flag = 1; - continue; - } - else if (res == GOTCAN) return ZCAN; - else if (res == TIMEOUT) return TIMEOUT; - else return res; + crc_cnt = 0; crc = 0xffffffffL; + Rxcount = 0; + while (buf <= p+len) + { + if ((res = zread_byte()) & ~0377) + { + if (res == GOTCRCE || res == GOTCRCG || + res == GOTCRCQ || res == GOTCRCW) + { + c = res; + crc = updcrc32(res&0377, crc); + flag = 1; + continue; + } + else if (res == GOTCAN) return ZCAN; + else if (res == TIMEOUT) return TIMEOUT; + else return res; - } - else - { - if (flag) - { - crc = updcrc32(res, crc); - crc_cnt++; - if (crc_cnt < 4) continue; - if ((crc & 0xDEBB20E3)) - { + } + else + { + if (flag) + { + crc = updcrc32(res, crc); + crc_cnt++; + if (crc_cnt < 4) continue; + if ((crc & 0xDEBB20E3)) + { #ifdef ZDEBUG - rt_kprintf("error code: CRC32 error \r\n"); + rt_kprintf("error code: CRC32 error \r\n"); #endif - return -RT_ERROR; - } + return -RT_ERROR; + } return c; - } - else - { - *buf++ = res; - Rxcount++; - crc = updcrc32(res, crc); - } - } - } + } + else + { + *buf++ = res; + Rxcount++; + crc = updcrc32(res, crc); + } + } + } - return -RT_ERROR; + return -RT_ERROR; } /* receive data,with RLE encoded,32bits CRC check */ static rt_int16_t zrec_data32r(rt_uint8_t *buf, rt_int16_t len) { - rt_int16_t c,crc_cnt; - rt_uint32_t crc; - rt_err_t res = -RT_ERROR; - rt_uint8_t *p,flag = 0; + rt_int16_t c,crc_cnt; + rt_uint32_t crc; + rt_err_t res = -RT_ERROR; + rt_uint8_t *p,flag = 0; - crc_cnt = 0; crc = 0xffffffffL; - Rxcount = 0; - p = buf; - while (buf <= p+len) - { - if ((res = zread_byte()) & ~0377) - { - if (res == GOTCRCE || res == GOTCRCG || - res == GOTCRCQ || res == GOTCRCW) - { - c = res; - crc = updcrc32(res&0377, crc); - flag = 1; - continue; - } - else if (res == GOTCAN) return ZCAN; - else if (res == TIMEOUT) return TIMEOUT; - else return res; + crc_cnt = 0; crc = 0xffffffffL; + Rxcount = 0; + p = buf; + while (buf <= p+len) + { + if ((res = zread_byte()) & ~0377) + { + if (res == GOTCRCE || res == GOTCRCG || + res == GOTCRCQ || res == GOTCRCW) + { + c = res; + crc = updcrc32(res&0377, crc); + flag = 1; + continue; + } + else if (res == GOTCAN) return ZCAN; + else if (res == TIMEOUT) return TIMEOUT; + else return res; - } - else - { - if (flag) - { - crc = updcrc32(res, crc); - crc_cnt++; - if (crc_cnt < 4) continue; - if ((crc & 0xDEBB20E3)) - { + } + else + { + if (flag) + { + crc = updcrc32(res, crc); + crc_cnt++; + if (crc_cnt < 4) continue; + if ((crc & 0xDEBB20E3)) + { #ifdef ZDEBUG - rt_kprintf("error code: CRC32 error \r\n"); + rt_kprintf("error code: CRC32 error \r\n"); #endif - return -RT_ERROR; - } + return -RT_ERROR; + } return c; - } - else - { - crc = updcrc32(res, crc); - switch (c) - { - case 0: - if (res == ZRESC) - { - c = -1; continue; - } - *buf++ = res; - Rxcount++; - continue; - case -1: - if (res >= 040 && res < 0100) - { - c = res - 035; res = 040; - goto spaces; - } - if (res == 0100) - { - c = 0; - *buf++ = ZRESC; - Rxcount++; - continue; - } - c = res; continue; - default: - c -= 0100; - if (c < 1) - goto end; + } + else + { + crc = updcrc32(res, crc); + switch (c) + { + case 0: + if (res == ZRESC) + { + c = -1; continue; + } + *buf++ = res; + Rxcount++; + continue; + case -1: + if (res >= 040 && res < 0100) + { + c = res - 035; res = 040; + goto spaces; + } + if (res == 0100) + { + c = 0; + *buf++ = ZRESC; + Rxcount++; + continue; + } + c = res; continue; + default: + c -= 0100; + if (c < 1) + goto end; spaces: - if ((buf + c) > p+len) - goto end; - while ( --res >= 0) - { - *buf++ = res; - Rxcount++; - } - c = 0; continue; - } - } - } // if -else + if ((buf + c) > p+len) + goto end; + while ( --res >= 0) + { + *buf++ = res; + Rxcount++; + } + c = 0; continue; + } + } + } // if -else - } + } end: - return -RT_ERROR; + return -RT_ERROR; } rt_int16_t zget_data(rt_uint8_t *buf, rt_uint16_t len) { - rt_int16_t res = -RT_ERROR; + rt_int16_t res = -RT_ERROR; - if (RxCRC == 0) - { - res = zrec_data16(buf,len); - } - else if (RxCRC == 1) - { - res = zrec_data32(buf, len); - } - else if (RxCRC == 2) - { - res = zrec_data32r(buf, len); - } + if (RxCRC == 0) + { + res = zrec_data16(buf,len); + } + else if (RxCRC == 1) + { + res = zrec_data32(buf, len); + } + else if (RxCRC == 2) + { + res = zrec_data32r(buf, len); + } - return res; + return res; } /* get type and cmd of header, fix lenght */ rt_int16_t zget_header(rt_uint8_t *hdr) { - rt_int16_t c,prev_char; - rt_uint32_t bit; - rt_uint16_t get_can,step_out; + rt_int16_t c,prev_char; + rt_uint32_t bit; + rt_uint16_t get_can,step_out; - bit = get_device_baud(); /* get console baud rate */ - Rxframeind = header_type = 0; + bit = get_device_baud(); /* get console baud rate */ + Rxframeind = header_type = 0; step_out = 0; - prev_char = 0xff; - for (;;) - { - c = zread_line(100); - switch(c) - { - case 021: - case 0221: - if (prev_char == CAN) break; - if (prev_char == ZCRCW) goto start_again; - break; - case RCDO: - goto end; - case TIMEOUT: - if (prev_char == CAN) break; - if (prev_char == ZCRCW) - { - c = -RT_ERROR; goto end; - } - goto end; - case ZCRCW: - if (prev_char == CAN) goto start_again; - break; - case CAN: + prev_char = 0xff; + for (;;) + { + c = zread_line(100); + switch(c) + { + case 021: + case 0221: + if (prev_char == CAN) break; + if (prev_char == ZCRCW) goto start_again; + break; + case RCDO: + goto end; + case TIMEOUT: + if (prev_char == CAN) break; + if (prev_char == ZCRCW) + { + c = -RT_ERROR; goto end; + } + goto end; + case ZCRCW: + if (prev_char == CAN) goto start_again; + break; + case CAN: get_can: - if (++get_can > 5) - { - c = ZCAN; goto end; + if (++get_can > 5) + { + c = ZCAN; goto end; } - break; - case ZPAD: - if (prev_char == CAN) break; - if (prev_char == ZCRCW) goto start_again; - step_out = 1; - break; - default: - if (prev_char == CAN) break; - if (prev_char == ZCRCW) goto start_again; + break; + case ZPAD: + if (prev_char == CAN) break; + if (prev_char == ZCRCW) goto start_again; + step_out = 1; + break; + default: + if (prev_char == CAN) break; + if (prev_char == ZCRCW) goto start_again; start_again: - if (--bit == 0) - { - c = GCOUNT; goto end; - } - get_can = 0; - break; - } - prev_char = c; - if (step_out) break; /* exit loop */ - } - step_out = get_can = 0; - for (;;) - { - c = zxor_read(); - switch(c) - { - case ZPAD: - break; - case RCDO: - case TIMEOUT: - goto end; - case ZDLE: - step_out = 1; - break; - default: - goto start_again; - } - if (step_out) break; - } - - Rxframeind = c = zxor_read(); - switch (c) - { - case ZBIN32: - RxCRC = 1; c = zget_bin_fcs(hdr); break; - case ZBINR32: - RxCRC = 2; c = zget_bin_fcs(hdr); break; - case ZBIN: - RxCRC = 0; c = zget_bin_header(hdr); break; - case ZHEX: - RxCRC = 0; c = zget_hex_header(hdr); break; - case CAN: - goto get_can; - case RCDO: - case TIMEOUT: - goto end; - default: - goto start_again; - } + if (--bit == 0) + { + c = GCOUNT; goto end; + } + get_can = 0; + break; + } + prev_char = c; + if (step_out) break; /* exit loop */ + } + step_out = get_can = 0; + for (;;) + { + c = zxor_read(); + switch(c) + { + case ZPAD: + break; + case RCDO: + case TIMEOUT: + goto end; + case ZDLE: + step_out = 1; + break; + default: + goto start_again; + } + if (step_out) break; + } + + Rxframeind = c = zxor_read(); + switch (c) + { + case ZBIN32: + RxCRC = 1; c = zget_bin_fcs(hdr); break; + case ZBINR32: + RxCRC = 2; c = zget_bin_fcs(hdr); break; + case ZBIN: + RxCRC = 0; c = zget_bin_header(hdr); break; + case ZHEX: + RxCRC = 0; c = zget_hex_header(hdr); break; + case CAN: + goto get_can; + case RCDO: + case TIMEOUT: + goto end; + default: + goto start_again; + } end: - return c; + return c; } /* receive a binary header */ static rt_int16_t zget_bin_header(rt_uint8_t *hdr) { - rt_int16_t res, i; - rt_uint16_t crc; + rt_int16_t res, i; + rt_uint16_t crc; - if ((res = zread_byte()) & ~0377) - return res; - header_type = res; - crc = updcrc16(res, 0); + if ((res = zread_byte()) & ~0377) + return res; + header_type = res; + crc = updcrc16(res, 0); - for (i=0;i<4;i++) - { - if ((res = zread_byte()) & ~0377) - return res; - crc = updcrc16(res, crc); - *hdr++ = res; - } - if ((res = zread_byte()) & ~0377) - return res; - crc = updcrc16(res, crc); - if ((res = zread_byte()) & ~0377) - return res; - crc = updcrc16(res, crc); - if (crc & 0xFFFF) - { - rt_kprintf("CRC error\n"); - return -RT_ERROR; - } + for (i=0;i<4;i++) + { + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc16(res, crc); + *hdr++ = res; + } + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc16(res, crc); + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc16(res, crc); + if (crc & 0xFFFF) + { + rt_kprintf("CRC error\n"); + return -RT_ERROR; + } - return header_type; + return header_type; } /* receive a binary header,with 32bits FCS */ static rt_int16_t zget_bin_fcs(rt_uint8_t *hdr) { - rt_int16_t res, i; - rt_uint32_t crc; + rt_int16_t res, i; + rt_uint32_t crc; - if ((res = zread_byte()) & ~0377) - return res; - header_type = res; - crc = 0xFFFFFFFFL; - crc = updcrc32(res, crc); + if ((res = zread_byte()) & ~0377) + return res; + header_type = res; + crc = 0xFFFFFFFFL; + crc = updcrc32(res, crc); - for (i=0;i<4;i++) /* 4headers */ - { - if ((res = zread_byte()) & ~0377) - return res; - crc = updcrc32(res, crc); - *hdr++ = res; + for (i=0;i<4;i++) /* 4headers */ + { + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc32(res, crc); + *hdr++ = res; - } - for (i=0;i<4;i++) /* 4bytes crc */ - { - if ((res = zread_byte()) & ~0377) - return res; - crc = updcrc32(res, crc); + } + for (i=0;i<4;i++) /* 4bytes crc */ + { + if ((res = zread_byte()) & ~0377) + return res; + crc = updcrc32(res, crc); - } - if (crc != 0xDEBB20E3) - { + } + if (crc != 0xDEBB20E3) + { #ifdef ZDEBUG - rt_kprintf("CRC error\n"); + rt_kprintf("CRC error\n"); #endif - return -RT_ERROR; - } + return -RT_ERROR; + } - return header_type; + return header_type; } /* receive a hex style header (type and position) */ rt_int16_t zget_hex_header(rt_uint8_t *hdr) { - rt_int16_t res,i; - rt_uint16_t crc; + rt_int16_t res,i; + rt_uint16_t crc; - if ((res = zget_hex()) < 0) - return res; - header_type = res; - crc = updcrc16(res, 0); + if ((res = zget_hex()) < 0) + return res; + header_type = res; + crc = updcrc16(res, 0); - for (i=0;i<4;i++) - { - if ((res = zget_hex()) < 0) - return res; - crc = updcrc16(res, crc); - *hdr++ = res; - } - if ((res = zget_hex()) < 0) - return res; - crc = updcrc16(res, crc); - if ((res = zget_hex()) < 0) - return res; - crc = updcrc16(res, crc); - if (crc & 0xFFFF) - { + for (i=0;i<4;i++) + { + if ((res = zget_hex()) < 0) + return res; + crc = updcrc16(res, crc); + *hdr++ = res; + } + if ((res = zget_hex()) < 0) + return res; + crc = updcrc16(res, crc); + if ((res = zget_hex()) < 0) + return res; + crc = updcrc16(res, crc); + if (crc & 0xFFFF) + { #ifdef ZDEBUG - rt_kprintf("error code : CRC error\r\n"); + rt_kprintf("error code : CRC error\r\n"); #endif - return -RT_ERROR; - } - res = zread_line(100); - if (res < 0) - return res; - res = zread_line(100); - if (res < 0) - return res; + return -RT_ERROR; + } + res = zread_line(100); + if (res < 0) + return res; + res = zread_line(100); + if (res < 0) + return res; - return header_type; + return header_type; } /* convert to ascii */ static void zsend_ascii(rt_uint8_t c) { - const char hex[] = "0123456789abcdef"; + const char hex[] = "0123456789abcdef"; - zsend_line(hex[(c&0xF0)>>4]); - zsend_line(hex[(c)&0xF]); + zsend_line(hex[(c&0xF0)>>4]); + zsend_line(hex[(c)&0xF]); - return; + return; } /* @@ -723,54 +723,54 @@ static void zsend_ascii(rt_uint8_t c) */ void zsend_zdle_char(rt_uint16_t ch) { - rt_uint16_t res; + rt_uint16_t res; - res = ch & 0377; - switch (res) - { - case 0377: - zsend_byte(res); - break; - case ZDLE: - zsend_byte(ZDLE); - res ^= 0100; - zsend_byte(res); - break; - case 021: - case 023: - case 0221: - case 0223: - zsend_byte(ZDLE); - res ^= 0100; - zsend_byte(res); - break; - default: - zsend_byte(res); - } + res = ch & 0377; + switch (res) + { + case 0377: + zsend_byte(res); + break; + case ZDLE: + zsend_byte(ZDLE); + res ^= 0100; + zsend_byte(res); + break; + case 021: + case 023: + case 0221: + case 0223: + zsend_byte(ZDLE); + res ^= 0100; + zsend_byte(res); + break; + default: + zsend_byte(res); + } } /* decode two lower case hex digits into an 8 bit byte value */ static rt_int16_t zget_hex(void) { - rt_int16_t res,n; + rt_int16_t res,n; - if ((res = zxor_read()) < 0) - return res; - n = res - '0'; - if (n > 9) - n -= ('a' - ':'); - if (n & ~0x0f) - return -RT_ERROR; - if ((res = zxor_read()) < 0) - return res; - res -= '0'; - if (res > 9) - res -= ('a' - ':'); - if (res & ~0x0f) - return -RT_ERROR; - res += (n<<4); + if ((res = zxor_read()) < 0) + return res; + n = res - '0'; + if (n > 9) + n -= ('a' - ':'); + if (n & ~0x0f) + return -RT_ERROR; + if ((res = zxor_read()) < 0) + return res; + res -= '0'; + if (res > 9) + res -= ('a' - ':'); + if (res & ~0x0f) + return -RT_ERROR; + res += (n<<4); - return res; + return res; } @@ -780,58 +780,58 @@ static rt_int16_t zget_hex(void) */ rt_int16_t zread_byte(void) { - register int res; + register int res; again: - /* Quick check for non control characters */ - if ((res = zread_line(100)) & 0140) - return res; - switch (res) - { - case ZDLE: - break; - case 023: - case 0223: - case 021: - case 0221: - goto again; - default: - return res; - } + /* Quick check for non control characters */ + if ((res = zread_line(100)) & 0140) + return res; + switch (res) + { + case ZDLE: + break; + case 023: + case 0223: + case 021: + case 0221: + goto again; + default: + return res; + } again2: - if ((res = zread_line(100)) < 0) - return res; - if (res == CAN && (res = zread_line(100)) < 0) - return res; - if (res == CAN && (res = zread_line(100)) < 0) - return res; - if (res == CAN && (res = zread_line(100)) < 0) - return res; - switch (res) - { - case CAN: - return GOTCAN; - case ZCRCE: - case ZCRCG: - case ZCRCQ: - case ZCRCW: - return (res | GOTOR); - case ZRUB0: - return 0177; - case ZRUB1: - return 0377; - case 023: - case 0223: - case 021: - case 0221: - goto again2; - default: - if ((res & 0140) == 0100) - return (res ^ 0100); - break; - } + if ((res = zread_line(100)) < 0) + return res; + if (res == CAN && (res = zread_line(100)) < 0) + return res; + if (res == CAN && (res = zread_line(100)) < 0) + return res; + if (res == CAN && (res = zread_line(100)) < 0) + return res; + switch (res) + { + case CAN: + return GOTCAN; + case ZCRCE: + case ZCRCG: + case ZCRCQ: + case ZCRCW: + return (res | GOTOR); + case ZRUB0: + return 0177; + case ZRUB1: + return 0377; + case 023: + case 0223: + case 021: + case 0221: + goto again2; + default: + if ((res & 0140) == 0100) + return (res ^ 0100); + break; + } - return -RT_ERROR; + return -RT_ERROR; } /* @@ -840,46 +840,46 @@ again2: */ rt_int16_t zxor_read(void) { - rt_int16_t res; + rt_int16_t res; - for (;;) - { - if ((res = zread_line(100)) < 0) - return res; - switch (res &= 0177) { - case XON: - case XOFF: - continue; - case '\r': - case '\n': - case ZDLE: - default: - return res; - } - } + for (;;) + { + if ((res = zread_line(100)) < 0) + return res; + switch (res &= 0177) { + case XON: + case XOFF: + continue; + case '\r': + case '\n': + case ZDLE: + default: + return res; + } + } } /* put file posistion into the header*/ void zput_pos(rt_uint32_t pos) { - tx_header[ZP0] = pos; - tx_header[ZP1] = pos>>8; - tx_header[ZP2] = pos>>16; - tx_header[ZP3] = pos>>24; + tx_header[ZP0] = pos; + tx_header[ZP1] = pos>>8; + tx_header[ZP2] = pos>>16; + tx_header[ZP3] = pos>>24; - return; + return; } /* Recover a long integer from a header */ void zget_pos(rt_uint32_t pos) { - Rxpos = (rx_header[ZP3] & 0377); - Rxpos = (Rxpos << 8) | (rx_header[ZP2] & 0377); - Rxpos = (Rxpos << 8) | (rx_header[ZP1] & 0377); - Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377); + Rxpos = (rx_header[ZP3] & 0377); + Rxpos = (Rxpos << 8) | (rx_header[ZP2] & 0377); + Rxpos = (Rxpos << 8) | (rx_header[ZP1] & 0377); + Rxpos = (Rxpos << 8) | (rx_header[ZP0] & 0377); - return; + return; } /* end of zcore.c */ diff --git a/components/utilities/zmodem/zdef.h b/components/utilities/zmodem/zdef.h index 3568bdfd96..cfb9784f30 100644 --- a/components/utilities/zmodem/zdef.h +++ b/components/utilities/zmodem/zdef.h @@ -3,133 +3,133 @@ #include <rtthread.h> #include "crc.h" -#define ZPAD '*' /* 052 padding character begins frames */ -#define ZDLE 030 /* ctrl-X ZMODEM escape - `ala BISYNC DLE */ -#define ZDLEE (ZDLE^0100) /* escaped ZDLE as transmitted */ -#define ZBIN 'A' /* binary frame indicator (CRC-16) */ -#define ZHEX 'B' /* hex frame indicator */ -#define ZBIN32 'C' /* binary frame with 32 bit FCS */ -#define ZBINR32 'D' /* RLE packed Binary frame with 32 bit FCS */ -#define ZVBIN 'a' /* binary frame indicator (CRC-16) */ -#define ZVHEX 'b' /* hex frame indicator */ -#define ZVBIN32 'c' /* binary frame with 32 bit FCS */ -#define ZVBINR32 'd' /* RLE packed Binary frame with 32 bit FCS */ -#define ZRESC 0176 /* RLE flag/escape character */ +#define ZPAD '*' /* 052 padding character begins frames */ +#define ZDLE 030 /* ctrl-X ZMODEM escape - `ala BISYNC DLE */ +#define ZDLEE (ZDLE^0100) /* escaped ZDLE as transmitted */ +#define ZBIN 'A' /* binary frame indicator (CRC-16) */ +#define ZHEX 'B' /* hex frame indicator */ +#define ZBIN32 'C' /* binary frame with 32 bit FCS */ +#define ZBINR32 'D' /* RLE packed Binary frame with 32 bit FCS */ +#define ZVBIN 'a' /* binary frame indicator (CRC-16) */ +#define ZVHEX 'b' /* hex frame indicator */ +#define ZVBIN32 'c' /* binary frame with 32 bit FCS */ +#define ZVBINR32 'd' /* RLE packed Binary frame with 32 bit FCS */ +#define ZRESC 0176 /* RLE flag/escape character */ /* Frame types */ -#define ZRQINIT 0 /* request receive init */ +#define ZRQINIT 0 /* request receive init */ #define ZRINIT 1 /* receive init */ -#define ZSINIT 2 /* send init sequence (optional) */ -#define ZACK 3 /* ACK to above */ -#define ZFILE 4 /* file name from sender */ -#define ZSKIP 5 /* ro sender: skip this file */ -#define ZNAK 6 /* last packet was garbled */ -#define ZABORT 7 /* abort batch transfers */ -#define ZFIN 8 /* finish session */ -#define ZRPOS 9 /* resume data trans at this position */ +#define ZSINIT 2 /* send init sequence (optional) */ +#define ZACK 3 /* ACK to above */ +#define ZFILE 4 /* file name from sender */ +#define ZSKIP 5 /* ro sender: skip this file */ +#define ZNAK 6 /* last packet was garbled */ +#define ZABORT 7 /* abort batch transfers */ +#define ZFIN 8 /* finish session */ +#define ZRPOS 9 /* resume data trans at this position */ #define ZDATA 10 /* data packet(s) follow */ #define ZEOF 11 /* end of file */ #define ZFERR 12 /* fatal Read or Write error Detected */ #define ZCRC 13 /* request for file CRC and response */ #define ZCHALLENGE 14 /* receiver's Challenge */ -#define ZCOMPL 15 /* request is complete */ -#define ZCAN 16 /* other end canned session with CAN*5 */ -#define ZFREECNT 17 /* request for free bytes on filesystem */ -#define ZCOMMAND 18 /* command from sending program */ +#define ZCOMPL 15 /* request is complete */ +#define ZCAN 16 /* other end canned session with CAN*5 */ +#define ZFREECNT 17 /* request for free bytes on filesystem */ +#define ZCOMMAND 18 /* command from sending program */ /* ZDLE sequfences */ -#define ZCRCE 'h' /* CRC next, frame ends, header packet follows */ -#define ZCRCG 'i' /* CRC next, frame continues nonstop */ -#define ZCRCQ 'j' /* CRC next, frame continues, ZACK expected */ -#define ZCRCW 'k' /* CRC next, ZACK expected, end of frame */ -#define ZRUB0 'l' /* translate to rubout 0177 */ -#define ZRUB1 'm' /* translate to rubout 0377 */ +#define ZCRCE 'h' /* CRC next, frame ends, header packet follows */ +#define ZCRCG 'i' /* CRC next, frame continues nonstop */ +#define ZCRCQ 'j' /* CRC next, frame continues, ZACK expected */ +#define ZCRCW 'k' /* CRC next, ZACK expected, end of frame */ +#define ZRUB0 'l' /* translate to rubout 0177 */ +#define ZRUB1 'm' /* translate to rubout 0377 */ /* zdlread return values (internal) */ /* -1 is general error, -2 is timeout */ #define GOTOR 0400 -#define GOTCRCE (ZCRCE|GOTOR) /* ZDLE-ZCRCE received */ -#define GOTCRCG (ZCRCG|GOTOR) /* ZDLE-ZCRCG received */ -#define GOTCRCQ (ZCRCQ|GOTOR) /* ZDLE-ZCRCQ received */ -#define GOTCRCW (ZCRCW|GOTOR) /* ZDLE-ZCRCW received */ -#define GOTCAN (GOTOR|030) /* CAN*5 seen */ +#define GOTCRCE (ZCRCE|GOTOR) /* ZDLE-ZCRCE received */ +#define GOTCRCG (ZCRCG|GOTOR) /* ZDLE-ZCRCG received */ +#define GOTCRCQ (ZCRCQ|GOTOR) /* ZDLE-ZCRCQ received */ +#define GOTCRCW (ZCRCW|GOTOR) /* ZDLE-ZCRCW received */ +#define GOTCAN (GOTOR|030) /* CAN*5 seen */ /* Byte positions within header array */ -#define ZF0 3 /* first flags byte */ -#define ZF1 2 -#define ZF2 1 -#define ZF3 0 -#define ZP0 0 /* low order 8 bits of position */ -#define ZP1 1 -#define ZP2 2 -#define ZP3 3 /* high order 8 bits of file position */ +#define ZF0 3 /* first flags byte */ +#define ZF1 2 +#define ZF2 1 +#define ZF3 0 +#define ZP0 0 /* low order 8 bits of position */ +#define ZP1 1 +#define ZP2 2 +#define ZP3 3 /* high order 8 bits of file position */ /* parameters for ZRINIT header */ -#define ZRPXWN 8 /* 9th byte in header contains window size/256 */ -#define ZRPXQQ 9 /* 10th to 14th bytes contain quote mask */ +#define ZRPXWN 8 /* 9th byte in header contains window size/256 */ +#define ZRPXQQ 9 /* 10th to 14th bytes contain quote mask */ /* bit Masks for ZRINIT flags byte ZF0 */ -#define CANFDX 0x01 /* rx can send and receive true FDX */ -#define CANOVIO 0x02 /* rx can receive data during disk I/O */ -#define CANBRK 0x04 /* rx can send a break signal */ -#define CANRLE 0x10 /* receiver can decode RLE */ -#define CANLZW 0x20 /* receiver can uncompress */ -#define CANFC32 0x28 /* receiver can use 32 bit Frame Check */ -#define ESCCTL 0x64 /* receiver expects ctl chars to be escaped */ -#define ESC8 0xc8 /* receiver expects 8th bit to be escaped */ +#define CANFDX 0x01 /* rx can send and receive true FDX */ +#define CANOVIO 0x02 /* rx can receive data during disk I/O */ +#define CANBRK 0x04 /* rx can send a break signal */ +#define CANRLE 0x10 /* receiver can decode RLE */ +#define CANLZW 0x20 /* receiver can uncompress */ +#define CANFC32 0x28 /* receiver can use 32 bit Frame Check */ +#define ESCCTL 0x64 /* receiver expects ctl chars to be escaped */ +#define ESC8 0xc8 /* receiver expects 8th bit to be escaped */ /* bit Masks for ZRINIT flags byte ZF1 */ -#define CANVHDR 01 /* variable headers OK */ -#define ZRRQWN 8 /* receiver specified window size in ZRPXWN */ -#define ZRRQQQ 16 /* additional control chars to quote in ZRPXQQ */ -#define ZRQNVH (ZRRQWN|ZRRQQQ) /* variable len hdr reqd to access info */ +#define CANVHDR 01 /* variable headers OK */ +#define ZRRQWN 8 /* receiver specified window size in ZRPXWN */ +#define ZRRQQQ 16 /* additional control chars to quote in ZRPXQQ */ +#define ZRQNVH (ZRRQWN|ZRRQQQ) /* variable len hdr reqd to access info */ /* Parameters for ZSINIT frame */ -#define ZATTNLEN 32 /* max length of attention string */ -#define ALTCOFF ZF1 /* offset to alternate canit string, 0 if not used */ +#define ZATTNLEN 32 /* max length of attention string */ +#define ALTCOFF ZF1 /* offset to alternate canit string, 0 if not used */ /* Parameters for ZFILE frame */ /* Conversion options one of these in ZF0 */ -#define ZCBIN 1 /* binary transfer - inhibit conversion */ -#define ZCNL 2 /* convert NL to local end of line convention */ -#define ZCRESUM 3 /* resume interrupted file transfer */ +#define ZCBIN 1 /* binary transfer - inhibit conversion */ +#define ZCNL 2 /* convert NL to local end of line convention */ +#define ZCRESUM 3 /* resume interrupted file transfer */ /* management include options, one of these ored in ZF1 */ #define ZMSKNOLOC 0200 /* skip file if not present at rx */ /* management options, one of these ored in ZF1 */ -#define ZMMASK 037 /* mask for the choices below */ -#define ZMNEWL 1 /* transfer if source newer or longer */ -#define ZMCRC 2 /* transfer if different file CRC or length */ -#define ZMAPND 3 /* append contents to existing file (if any) */ -#define ZMCLOB 4 /* replace existing file */ -#define ZMNEW 5 /* transfer if source newer */ +#define ZMMASK 037 /* mask for the choices below */ +#define ZMNEWL 1 /* transfer if source newer or longer */ +#define ZMCRC 2 /* transfer if different file CRC or length */ +#define ZMAPND 3 /* append contents to existing file (if any) */ +#define ZMCLOB 4 /* replace existing file */ +#define ZMNEW 5 /* transfer if source newer */ /* number 5 is alive ... */ -#define ZMDIFF 6 /* transfer if dates or lengths different */ -#define ZMPROT 7 /* protect destination file */ -#define ZMCHNG 8 /* change filename if destination exists */ +#define ZMDIFF 6 /* transfer if dates or lengths different */ +#define ZMPROT 7 /* protect destination file */ +#define ZMCHNG 8 /* change filename if destination exists */ /* transport options, one of these in ZF2 */ -#define ZTLZW 1 /* lempel-Ziv compression */ -#define ZTRLE 3 /* run Length encoding */ +#define ZTLZW 1 /* lempel-Ziv compression */ +#define ZTRLE 3 /* run Length encoding */ /* extended options for ZF3, bit encoded */ -#define ZXSPARS 64 /* encoding for sparse file operations */ +#define ZXSPARS 64 /* encoding for sparse file operations */ #define ZCANVHDR 01 /* variable headers OK */ /* receiver window size override */ -#define ZRWOVR 4 /* byte position for receive window override/256 */ +#define ZRWOVR 4 /* byte position for receive window override/256 */ /* parameters for ZCOMMAND frame ZF0 (otherwise 0) */ -#define ZCACK1 1 /* acknowledge, then do command */ -extern char Attn[ZATTNLEN+1]; /* Attention string rx sends to tx on err */ +#define ZCACK1 1 /* acknowledge, then do command */ +extern char Attn[ZATTNLEN+1]; /* Attention string rx sends to tx on err */ /* globals used by ZMODEM functions */ extern rt_uint8_t Rxframeind; /* ZBIN ZBIN32, or ZHEX type of frame */ -extern char header_type; /* type of header received */ +extern char header_type; /* type of header received */ extern rt_uint8_t rx_header[4]; /* received header */ extern rt_uint8_t tx_header[4]; /* transmitted header */ -extern rt_uint8_t Txfcs32; /* TRUE means send binary frames with 32 bit FCS */ -extern rt_uint16_t Rxcount; /* count of data bytes received */ +extern rt_uint8_t Txfcs32; /* TRUE means send binary frames with 32 bit FCS */ +extern rt_uint16_t Rxcount; /* count of data bytes received */ extern rt_uint16_t Rxtimeout; /* tenths of seconds to wait for something */ -extern rt_uint32_t Rxpos; /* received file position */ -extern rt_uint32_t Txpos; /* transmitted file position */ -extern rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */ +extern rt_uint32_t Rxpos; /* received file position */ +extern rt_uint32_t Txpos; /* transmitted file position */ +extern rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit FCS */ /* ward Christensen / CP/M parameters - Don't change these! */ #define ENQ 005 @@ -141,34 +141,34 @@ extern rt_uint8_t Txfcs32; /* TURE means send binary frames with 32 bit #define ETX 3 #define SYN 026 #define ESC 033 -#define WANTG 0107 /* send G not NAK to get nonstop batch xmsn */ +#define WANTG 0107 /* send G not NAK to get nonstop batch xmsn */ #define EOT 4 #define ACK 6 #define NAK 025 #define CPMEOF 032 -#define WANTCRC 0103 /* send C not NAK to get crc not checksum */ +#define WANTCRC 0103 /* send C not NAK to get crc not checksum */ #define TIMEOUT (-2) #define RCDO (-3) #define GCOUNT (-4) #define ERRORMAX 5 #define RETRYMAX 5 #define WCEOT (-10) - - + + #define BITRATE 115200 #define TX_BUFFER_SIZE 1024 -#define RX_BUFFER_SIZE 1024 /* sender or receiver's max buffer size */ -extern char ZF0_CMD; /* local ZMODEM file conversion request */ -extern char ZF1_CMD; /* local ZMODEM file management request */ -extern char ZF2_CMD; /* local ZMODEM file management request */ -extern char ZF3_CMD; /* local ZMODEM file management request */ +#define RX_BUFFER_SIZE 1024 /* sender or receiver's max buffer size */ +extern char ZF0_CMD; /* local ZMODEM file conversion request */ +extern char ZF1_CMD; /* local ZMODEM file management request */ +extern char ZF2_CMD; /* local ZMODEM file management request */ +extern char ZF3_CMD; /* local ZMODEM file management request */ extern rt_uint32_t Baudrate ; extern rt_uint32_t Left_bytes; extern rt_uint32_t Left_sizes; - + struct zmodemf { @@ -179,18 +179,18 @@ extern struct zmodemf zmodem; struct zfile { - char *fname; - rt_int32_t fd; - rt_uint32_t ctime; + char *fname; + rt_int32_t fd; + rt_uint32_t ctime; rt_uint32_t mode; - rt_uint32_t bytes_total; - rt_uint32_t bytes_sent; - rt_uint32_t bytes_received; - rt_uint32_t file_end; - + rt_uint32_t bytes_total; + rt_uint32_t bytes_sent; + rt_uint32_t bytes_received; + rt_uint32_t file_end; + }; extern struct finsh_shell* shell; - + #define ZDEBUG 0 /* sz.c */ extern void zs_start(char *path); @@ -213,5 +213,5 @@ extern void zsend_line(rt_uint16_t c); extern rt_int16_t zread_line(rt_uint16_t timeout); extern void zsend_break(char *cmd); extern void zsend_can(void); - + #endif /* __ZDEF_H__ */ diff --git a/components/utilities/zmodem/zdevice.c b/components/utilities/zmodem/zdevice.c index 1562cc5f5c..3a144e79de 100644 --- a/components/utilities/zmodem/zdevice.c +++ b/components/utilities/zmodem/zdevice.c @@ -3,7 +3,7 @@ * the implemention of zmodem protocol. * Change Logs: * Date Author Notes - * 2011-03-29 itspy + * 2011-03-29 itspy */ #include <rtthread.h> @@ -15,11 +15,11 @@ #include <dfs_posix.h> #include "zdef.h" - -rt_uint32_t Line_left = 0; /* left number of data in the read line buffer*/ -rt_uint32_t Left_sizes = 0; /* left file sizes */ -rt_uint32_t Baudrate = BITRATE; /* console baudrate */ - + +rt_uint32_t Line_left = 0; /* left number of data in the read line buffer*/ +rt_uint32_t Left_sizes = 0; /* left file sizes */ +rt_uint32_t Baudrate = BITRATE; /* console baudrate */ + rt_uint32_t get_device_baud(void) @@ -34,7 +34,7 @@ rt_uint32_t get_sys_time(void) void zsend_byte(rt_uint16_t ch) { - rt_device_write(zmodem.device, 0, &ch,1); + rt_device_write(zmodem.device, 0, &ch,1); return; } @@ -43,38 +43,38 @@ void zsend_line(rt_uint16_t c) { rt_uint16_t ch; - ch = (c & 0377); - rt_device_write(zmodem.device, 0, &ch, 1); + ch = (c & 0377); + rt_device_write(zmodem.device, 0, &ch, 1); return; } rt_int16_t zread_line(rt_uint16_t timeout) { - char *str; - static char buf[10]; + char *str; + static char buf[10]; - if (Line_left > 0) - { - Line_left -= 1; - return (*str++ & 0377); - } - Line_left = 0; - timeout/=5; - while (1) - { - Line_left = rt_device_read(shell->device, 0, buf, 1); - if (Line_left) - { - Line_left = Line_left; - str = buf; - break; - } - } - if (Line_left < 1) return TIMEOUT; - Line_left -=1; + if (Line_left > 0) + { + Line_left -= 1; + return (*str++ & 0377); + } + Line_left = 0; + timeout/=5; + while (1) + { + Line_left = rt_device_read(shell->device, 0, buf, 1); + if (Line_left) + { + Line_left = Line_left; + str = buf; + break; + } + } + if (Line_left < 1) return TIMEOUT; + Line_left -=1; - return (*str++ & 0377); + return (*str++ & 0377); } /* @@ -83,32 +83,32 @@ rt_int16_t zread_line(rt_uint16_t timeout) */ void zsend_break(char *cmd) { - - while (*cmd++) - { - switch (*cmd) - { - case '\336': - continue; - case '\335': - rt_thread_delay(RT_TICK_PER_SECOND); - continue; - default: - zsend_line(*cmd); - break; - } - } + + while (*cmd++) + { + switch (*cmd) + { + case '\336': + continue; + case '\335': + rt_thread_delay(RT_TICK_PER_SECOND); + continue; + default: + zsend_line(*cmd); + break; + } + } } /* send cancel string to get the other end to shut up */ void zsend_can(void) { - static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0}; + static char cmd[] = {24,24,24,24,24,24,24,24,24,24,0}; - zsend_break(cmd); - rt_kprintf("\x0d"); - Line_left=0; /* clear Line_left */ + zsend_break(cmd); + rt_kprintf("\x0d"); + Line_left=0; /* clear Line_left */ - return; + return; } /* end of zdevice.c */ diff --git a/components/utilities/zmodem/zstart.c b/components/utilities/zmodem/zstart.c index e5a36ca7ab..5c44ee86a7 100644 --- a/components/utilities/zmodem/zstart.c +++ b/components/utilities/zmodem/zstart.c @@ -3,7 +3,7 @@ * the implemention of zmodem protocol. * Change Logs: * Date Author Notes - * 2011-03-29 itspy + * 2011-03-29 itspy */ #include <rtthread.h> @@ -27,43 +27,43 @@ rt_err_t zmodem_rx_ind(rt_device_t dev, rt_size_t size) void finsh_rz(void *parameter) { - char *path; + char *path; rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); - rt_uint8_t flag; + rt_uint8_t flag; - flag = RT_DEVICE_FLAG_STREAM; + flag = RT_DEVICE_FLAG_STREAM; zmodem.device->flag &=(~flag); rt_sem_init(&(zmodem.zsem), "zsem", 0, 0); - path = rt_thread_self()->parameter; - /* save old rx_indicate */ + path = rt_thread_self()->parameter; + /* save old rx_indicate */ rx_indicate = zmodem.device->rx_indicate; /* set new rx_indicate */ rt_device_set_rx_indicate(zmodem.device, RT_NULL); - /* start receive remote files */ + /* start receive remote files */ zr_start(path); - zmodem.device->flag |=flag; - /* recovery old rx_indicate */ + zmodem.device->flag |=flag; + /* recovery old rx_indicate */ rt_device_set_rx_indicate(zmodem.device, rx_indicate); /* finsh>> */ rt_kprintf(FINSH_PROMPT); } void finsh_sz(void *parameter) { - char *path; + char *path; rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size); - rt_uint8_t flag; + rt_uint8_t flag; - flag = RT_DEVICE_FLAG_STREAM; + flag = RT_DEVICE_FLAG_STREAM; zmodem.device->flag &=(~flag); rt_sem_init(&(zmodem.zsem), "zsem", 0, 0); - path = rt_thread_self()->parameter; - /* save old rx_indicate */ + path = rt_thread_self()->parameter; + /* save old rx_indicate */ rx_indicate = zmodem.device->rx_indicate; - /* set new rx_indicate */ + /* set new rx_indicate */ rt_device_set_rx_indicate(zmodem.device, zmodem_rx_ind); - zs_start(path); - zmodem.device->flag |=flag; - /* recovery old rx_indicate */ + zs_start(path); + zmodem.device->flag |=flag; + /* recovery old rx_indicate */ rt_device_set_rx_indicate(zmodem.device, rx_indicate); /* finsh>> */ rt_kprintf(FINSH_PROMPT); |
