add support for multi sal devices concurrently

This commit is contained in:
mculover666
2020-08-13 17:51:05 +08:00
parent 772d714a20
commit 122fa9fd03
12 changed files with 588 additions and 356 deletions

View File

@@ -25,10 +25,10 @@
#define AT_AGENT_ECHO_FAIL "FAIL"
#define AT_AGENT_ECHO_ERROR "ERROR"
#define AT_DATA_CHANNEL_NUM 6
#define AT_DATA_CHANNEL_FIFO_BUFFER_SIZE (2048 + 1024)
#define AT_DATA_CHANNEL_NUM 2
#define AT_DATA_CHANNEL_FIFO_BUFFER_SIZE (2048)
#define AT_UART_RX_FIFO_BUFFER_SIZE (2048 + 1024)
#define AT_UART_RX_FIFO_BUFFER_SIZE (2048)
#define AT_RECV_CACHE_SIZE 2048
#define AT_CMD_BUFFER_SIZE 512
@@ -128,14 +128,13 @@ typedef struct at_agent_st {
uint8_t *uart_rx_fifo_buffer;
} at_agent_t;
#define AT_AGENT ((at_agent_t *)(&at_agent))
/**
* @brief Write data to a channel.
* Write data to a channel with certain id.
*
* @attention None
*
* @param[in] at_agent at agent object
* @param[in] channel_id id of the channel.
* @param[in] buffer data buffer to write.
* @param[in] buffer_len length of the buffer.
@@ -144,7 +143,7 @@ typedef struct at_agent_st {
* @retval -1 write failed(error).
* @retval none -1 the number of bytes written.
*/
__API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_len);
__API__ int tos_at_channel_write(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len);
/**
* @brief Read data from a channel.
@@ -161,7 +160,7 @@ __API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_
* @retval -1 read failed(error).
* @retval none -1 the number of bytes read.
*/
__API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout);
__API__ int tos_at_channel_read_timed(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout);
/**
* @brief Read data from a channel.
@@ -177,7 +176,7 @@ __API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t bu
* @retval -1 read failed(error).
* @retval none -1 the number of bytes read.
*/
__API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_len);
__API__ int tos_at_channel_read(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len);
/**
* @brief Allocate a channel.
@@ -193,7 +192,7 @@ __API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_l
* @retval -1 allocate failed(error).
* @retval none -1 the id of the channel.
*/
__API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *port);
__API__ int tos_at_channel_alloc_id(at_agent_t *at_agent, int channel_id, const char *ip, const char *port);
/**
* @brief Allocate a channel.
@@ -208,7 +207,7 @@ __API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *
* @retval -1 allocate failed(error).
* @retval none -1 the id of the channel.
*/
__API__ int tos_at_channel_alloc(const char *ip, const char *port);
__API__ int tos_at_channel_alloc(at_agent_t *at_agent, const char *ip, const char *port);
/**
* @brief Free a channel.
@@ -222,7 +221,7 @@ __API__ int tos_at_channel_alloc(const char *ip, const char *port);
* @retval -1 free failed(error).
* @retval 0 free successfully.
*/
__API__ int tos_at_channel_free(int channel_id);
__API__ int tos_at_channel_free(at_agent_t *at_agent, int channel_id);
/**
* @brief Set channel broken.
@@ -235,7 +234,7 @@ __API__ int tos_at_channel_free(int channel_id);
* @retval -1 set failed(error).
* @retval 0 set successfully.
*/
__API__ int tos_at_channel_set_broken(int channel_id);
__API__ int tos_at_channel_set_broken(at_agent_t *at_agent, int channel_id);
/**
* @brief Judge whether channel is working.
@@ -246,7 +245,7 @@ __API__ int tos_at_channel_set_broken(int channel_id);
*
* @return at channel status(type of at_channel_status_t)
*/
__API__ int tos_at_channel_is_working(int channel_id);
__API__ int tos_at_channel_is_working(at_agent_t *at_agent, int channel_id);
/**
* @brief Initialize the at framework.
@@ -261,7 +260,7 @@ __API__ int tos_at_channel_is_working(int channel_id);
* @retval -1 initialize failed(error).
* @retval 0 initialize successfully.
*/
__API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size);
__API__ int tos_at_init(at_agent_t *at_agent, k_stack_t *stk, hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size);
/**
* @brief De-initialize the at framework.
@@ -271,7 +270,7 @@ __API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size
* @return
None
*/
__API__ void tos_at_deinit(void);
__API__ void tos_at_deinit(at_agent_t *at_agent);
/**
* @brief Create a echo struct.
@@ -318,7 +317,7 @@ __API__ int tos_at_echo_fuzzy_matching_create(at_echo_t *echo, char *buffer, siz
* @retval -1 execute failed(error).
* @retval 0 execute successfully.
*/
__API__ int tos_at_cmd_exec(at_echo_t *echo, uint32_t timeout, const char *cmd, ...);
__API__ int tos_at_cmd_exec(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...);
/**
* @brief Execute an at command.
@@ -334,7 +333,7 @@ __API__ int tos_at_cmd_exec(at_echo_t *echo, uint32_t timeout, const char *cmd,
* @retval -1 execute failed(error).
* @retval 0 execute successfully.
*/
__API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char *cmd, ...);
__API__ int tos_at_cmd_exec_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...);
/**
* @brief Send raw data througth uart.
@@ -350,7 +349,7 @@ __API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char
* @retval -1 execute failed(error).
* @retval 0 execute successfully.
*/
__API__ int tos_at_raw_data_send(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size);
__API__ int tos_at_raw_data_send(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size);
/**
* @brief Send raw data througth uart.
@@ -367,7 +366,7 @@ __API__ int tos_at_raw_data_send(at_echo_t *echo, uint32_t timeout, const uint8_
* @retval -1 execute failed(error).
* @retval 0 execute successfully.
*/
__API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size);
__API__ int tos_at_raw_data_send_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size);
/**
* @brief Write byte to the at uart.
@@ -379,7 +378,7 @@ __API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const
*
* @return None
*/
__API__ void tos_at_uart_input_byte(uint8_t data);
__API__ void tos_at_uart_input_byte(at_agent_t *at_agent, uint8_t data);
/**
* @brief A global lock provided by at framework.
@@ -393,7 +392,7 @@ __API__ void tos_at_uart_input_byte(uint8_t data);
* @retval -1 pend failed(error).
* @retval 0 pend successfully.
*/
__API__ int tos_at_global_lock_pend(void);
__API__ int tos_at_global_lock_pend(at_agent_t *at_agent);
/**
* @brief A global lock provided by at framework.
@@ -407,7 +406,7 @@ __API__ int tos_at_global_lock_pend(void);
* @retval -1 post failed(error).
* @retval 0 post successfully.
*/
__API__ int tos_at_global_lock_post(void);
__API__ int tos_at_global_lock_post(at_agent_t *at_agent);
/**
* @brief Read data from the uart.
@@ -420,7 +419,7 @@ __API__ int tos_at_global_lock_post(void);
*
* @return length of the data read from the uart.
*/
__API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len);
__API__ int tos_at_uart_read(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len);
/**
* @brief Read data from the uart.
@@ -433,7 +432,7 @@ __API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len);
*
* @return length of the data read from the uart.
*/
__API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len);
__API__ int tos_at_uart_readline(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len);
/**
* @brief Read data from the uart.
@@ -446,7 +445,7 @@ __API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len);
*
* @return length of the data read from the uart.
*/
__API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len);
__API__ int tos_at_uart_drain(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len);
/**
* @brief Get the remote ip of a channel.

View File

@@ -17,55 +17,51 @@
#include "tos_at.h"
__STATIC__ at_agent_t at_agent;
__STATIC__ k_stack_t at_parser_task_stack[AT_PARSER_TASK_STACK_SIZE];
__API__ int tos_at_global_lock_pend(void)
__API__ int tos_at_global_lock_pend(at_agent_t *at_agent)
{
if (tos_mutex_pend(&AT_AGENT->global_lock) != K_ERR_NONE) {
if (tos_mutex_pend(&at_agent->global_lock) != K_ERR_NONE) {
return -1;
}
return 0;
}
__API__ int tos_at_global_lock_post(void)
__API__ int tos_at_global_lock_post(at_agent_t *at_agent)
{
if (tos_mutex_post(&AT_AGENT->global_lock) != K_ERR_NONE) {
if (tos_mutex_post(&at_agent->global_lock) != K_ERR_NONE) {
return -1;
}
return 0;
}
__STATIC__ int at_uart_getchar(uint8_t *data, k_tick_t timeout)
__STATIC__ int at_uart_getchar(at_agent_t *at_agent, uint8_t *data, k_tick_t timeout)
{
k_err_t err;
tos_stopwatch_delay(1);
if (tos_sem_pend(&AT_AGENT->uart_rx_sem, timeout) != K_ERR_NONE) {
if (tos_sem_pend(&at_agent->uart_rx_sem, timeout) != K_ERR_NONE) {
return -1;
}
if (tos_mutex_pend(&AT_AGENT->uart_rx_lock) != K_ERR_NONE) {
if (tos_mutex_pend(&at_agent->uart_rx_lock) != K_ERR_NONE) {
return -1;
}
err = tos_chr_fifo_pop(&AT_AGENT->uart_rx_fifo, data);
err = tos_chr_fifo_pop(&at_agent->uart_rx_fifo, data);
tos_mutex_post(&AT_AGENT->uart_rx_lock);
tos_mutex_post(&at_agent->uart_rx_lock);
return err == K_ERR_NONE ? 0 : -1;
}
__STATIC__ at_event_t *at_event_do_get(char *buffer, size_t buffer_len)
__STATIC__ at_event_t *at_event_do_get(at_agent_t *at_agent, char *buffer, size_t buffer_len)
{
int i = 0;
at_event_t *event_table = K_NULL, *event = K_NULL;
size_t event_table_size = 0, event_len;
event_table = AT_AGENT->event_table;
event_table_size = AT_AGENT->event_table_size;
event_table = at_agent->event_table;
event_table_size = at_agent->event_table_size;
for (i = 0; i < event_table_size; ++i) {
event = &event_table[i];
@@ -83,27 +79,27 @@ __STATIC__ at_event_t *at_event_do_get(char *buffer, size_t buffer_len)
return K_NULL;
}
__STATIC__ at_event_t *at_get_event(void)
__STATIC__ at_event_t *at_get_event(at_agent_t *at_agent)
{
char *buffer;
size_t buffer_len;
at_cache_t *at_cache = K_NULL;
at_cache = &AT_AGENT->recv_cache;
at_cache = &at_agent->recv_cache;
buffer = (char *)at_cache->buffer;
buffer_len = at_cache->recv_len;
return at_event_do_get(buffer, buffer_len);
return at_event_do_get(at_agent, buffer, buffer_len);
}
__API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len)
__API__ int tos_at_uart_read(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len)
{
uint8_t data;
size_t read_len = 0;
while (K_TRUE) {
if (at_uart_getchar(&data, TOS_TIME_FOREVER) != 0) {
if (at_uart_getchar(at_agent, &data, TOS_TIME_FOREVER) != 0) {
return read_len;
}
@@ -115,13 +111,13 @@ __API__ int tos_at_uart_read(uint8_t *buffer, size_t buffer_len)
}
}
__API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len)
__API__ int tos_at_uart_readline(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len)
{
uint8_t data;
size_t read_len = 0;
while (K_TRUE) {
if (at_uart_getchar(&data, TOS_TIME_FOREVER) != 0) {
if (at_uart_getchar(at_agent, &data, TOS_TIME_FOREVER) != 0) {
return read_len;
}
@@ -135,13 +131,13 @@ __API__ int tos_at_uart_readline(uint8_t *buffer, size_t buffer_len)
}
}
__API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len)
__API__ int tos_at_uart_drain(at_agent_t *at_agent, uint8_t *buffer, size_t buffer_len)
{
uint8_t data;
size_t read_len = 0;
while (K_TRUE) {
if (at_uart_getchar(&data, TOS_TIME_NOWAIT) != 0) {
if (at_uart_getchar(at_agent, &data, TOS_TIME_NOWAIT) != 0) {
return read_len;
}
@@ -153,20 +149,20 @@ __API__ int tos_at_uart_drain(uint8_t *buffer, size_t buffer_len)
}
}
__STATIC__ int at_is_echo_expect(void)
__STATIC__ int at_is_echo_expect(at_agent_t *at_agent)
{
char *recv_buffer, *expect;
size_t recv_buffer_len, expect_len;
at_echo_t *at_echo = K_NULL;
at_cache_t *at_cache = K_NULL;
at_echo = AT_AGENT->echo;
at_echo = at_agent->echo;
if (!at_echo || !at_echo->echo_expect) {
return 0;
}
at_cache = &AT_AGENT->recv_cache;
at_cache = &at_agent->recv_cache;
recv_buffer = (char *)at_cache->buffer;
recv_buffer_len = at_cache->recv_len;
@@ -192,19 +188,19 @@ __STATIC__ int at_is_echo_expect(void)
return 0;
}
__STATIC__ at_parse_status_t at_uart_line_parse(void)
__STATIC__ at_parse_status_t at_uart_line_parse(at_agent_t *at_agent)
{
size_t curr_len = 0;
uint8_t data, last_data = 0;
at_cache_t *recv_cache = K_NULL;
recv_cache = &AT_AGENT->recv_cache;
recv_cache = &at_agent->recv_cache;
recv_cache->recv_len = 0;
memset(recv_cache->buffer, 0, recv_cache->buffer_size);
while (K_TRUE) {
if (at_uart_getchar(&data, TOS_TIME_FOREVER) != 0) {
if (at_uart_getchar(at_agent, &data, TOS_TIME_FOREVER) != 0) {
continue;
}
@@ -220,11 +216,11 @@ __STATIC__ at_parse_status_t at_uart_line_parse(void)
return AT_PARSE_STATUS_OVERFLOW;
}
if (at_get_event() != K_NULL) {
if (at_get_event(at_agent) != K_NULL) {
return AT_PARSE_STATUS_EVENT;
}
if (at_is_echo_expect()) {
if (at_is_echo_expect(at_agent)) {
return AT_PARSE_STATUS_EXPECT;
}
@@ -247,12 +243,12 @@ __STATIC__ at_parse_status_t at_uart_line_parse(void)
}
}
__STATIC__ void at_echo_status_set(at_echo_t *echo)
__STATIC__ void at_echo_status_set(at_agent_t *at_agent, at_echo_t *echo)
{
char *buffer;
at_cache_t *at_cache;
at_cache = &AT_AGENT->recv_cache;
at_cache = &at_agent->recv_cache;
buffer = (char *)at_cache->buffer;
@@ -292,10 +288,12 @@ __STATIC__ void at_parser(void *arg)
at_cache_t *recv_cache = K_NULL;
at_parse_status_t at_parse_status;
recv_cache = &AT_AGENT->recv_cache;
at_agent_t *at_agent = (at_agent_t *)arg;
recv_cache = &at_agent->recv_cache;
while (K_TRUE) {
at_parse_status = at_uart_line_parse();
at_parse_status = at_uart_line_parse(at_agent);
if (at_parse_status == AT_PARSE_STATUS_OVERFLOW) {
tos_kprintln("AT parse overflow!");
@@ -303,14 +301,14 @@ __STATIC__ void at_parser(void *arg)
}
if (at_parse_status == AT_PARSE_STATUS_EVENT) {
at_event = at_get_event();
at_event = at_get_event(at_agent);
if (at_event && at_event->event_callback) {
at_event->event_callback();
}
continue;
}
at_echo = AT_AGENT->echo;
at_echo = at_agent->echo;
if (!at_echo) {
continue;
}
@@ -322,7 +320,7 @@ __STATIC__ void at_parser(void *arg)
}
} else if (at_parse_status == AT_PARSE_STATUS_NEWLINE &&
at_echo->status == AT_ECHO_STATUS_NONE) {
at_echo_status_set(at_echo);
at_echo_status_set(at_agent, at_echo);
}
if (at_echo->buffer) {
@@ -333,13 +331,13 @@ __STATIC__ void at_parser(void *arg)
}
}
__STATIC__ int at_uart_send(const uint8_t *buf, size_t size, uint32_t timeout)
__STATIC__ int at_uart_send(at_agent_t *at_agent, const uint8_t *buf, size_t size, uint32_t timeout)
{
int ret;
tos_mutex_pend(&AT_AGENT->uart_tx_lock);
ret = tos_hal_uart_write(&AT_AGENT->uart, buf, size, timeout);
tos_mutex_post(&AT_AGENT->uart_tx_lock);
tos_mutex_pend(&at_agent->uart_tx_lock);
ret = tos_hal_uart_write(&at_agent->uart, buf, size, timeout);
tos_mutex_post(&at_agent->uart_tx_lock);
return ret;
}
@@ -393,30 +391,30 @@ __STATIC_INLINE__ void at_echo_flush(at_echo_t *echo)
echo->__w_idx = 0;
}
__STATIC_INLINE__ void at_echo_attach(at_echo_t *echo)
__STATIC_INLINE__ void at_echo_attach(at_agent_t *at_agent, at_echo_t *echo)
{
at_echo_flush(echo);
AT_AGENT->echo = echo;
at_agent->echo = echo;
}
__API__ int tos_at_raw_data_send(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size)
__API__ int tos_at_raw_data_send(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size)
{
int ret = 0;
if (echo) {
at_echo_attach(echo);
at_echo_attach(at_agent, echo);
}
ret = at_uart_send(buf, size, 0xFFFF);
ret = at_uart_send(at_agent, buf, size, 0xFFFF);
tos_task_delay(tos_millisec2tick(timeout));
AT_AGENT->echo = K_NULL;
at_agent->echo = K_NULL;
return ret;
}
__API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size)
__API__ int tos_at_raw_data_send_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const uint8_t *buf, size_t size)
{
int ret = 0;
@@ -428,9 +426,9 @@ __API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const
return -1;
}
echo->__is_expecting = K_TRUE;
at_echo_attach(echo);
at_echo_attach(at_agent, echo);
ret = at_uart_send(buf, size, 0xFFFF);
ret = at_uart_send(at_agent, buf, size, 0xFFFF);
if (tos_sem_pend(&echo->__expect_notify, tos_millisec2tick(timeout)) != K_ERR_NONE) {
ret = -1;
@@ -438,56 +436,56 @@ __API__ int tos_at_raw_data_send_until(at_echo_t *echo, uint32_t timeout, const
tos_sem_destroy(&echo->__expect_notify);
AT_AGENT->echo = K_NULL;
at_agent->echo = K_NULL;
return ret;
}
__STATIC__ int at_cmd_do_exec(const char *format, va_list args)
__STATIC__ int at_cmd_do_exec(at_agent_t *at_agent, const char *format, va_list args)
{
size_t cmd_len = 0;
if (tos_mutex_pend(&AT_AGENT->cmd_buf_lock) != K_ERR_NONE) {
if (tos_mutex_pend(&at_agent->cmd_buf_lock) != K_ERR_NONE) {
return -1;
}
cmd_len = vsnprintf(AT_AGENT->cmd_buf, AT_CMD_BUFFER_SIZE, format, args);
cmd_len = vsnprintf(at_agent->cmd_buf, AT_CMD_BUFFER_SIZE, format, args);
printf("AT CMD:\n%s\n", AT_AGENT->cmd_buf);
printf("AT CMD:\n%s\n", at_agent->cmd_buf);
at_uart_send((uint8_t *)AT_AGENT->cmd_buf, cmd_len, 0xFFFF);
at_uart_send(at_agent, (uint8_t *)at_agent->cmd_buf, cmd_len, 0xFFFF);
tos_mutex_post(&AT_AGENT->cmd_buf_lock);
tos_mutex_post(&at_agent->cmd_buf_lock);
return 0;
}
__API__ int tos_at_cmd_exec(at_echo_t *echo, uint32_t timeout, const char *cmd, ...)
__API__ int tos_at_cmd_exec(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...)
{
int ret = 0;
va_list args;
if (echo) {
at_echo_attach(echo);
at_echo_attach(at_agent, echo);
}
va_start(args, cmd);
ret = at_cmd_do_exec(cmd, args);
ret = at_cmd_do_exec(at_agent, cmd, args);
va_end(args);
if (ret != 0) {
AT_AGENT->echo = K_NULL;
at_agent->echo = K_NULL;
return -1;
}
tos_task_delay(tos_millisec2tick(timeout));
AT_AGENT->echo = K_NULL;
at_agent->echo = K_NULL;
return 0;
}
__API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char *cmd, ...)
__API__ int tos_at_cmd_exec_until(at_agent_t *at_agent, at_echo_t *echo, uint32_t timeout, const char *cmd, ...)
{
int ret = 0;
va_list args;
@@ -500,14 +498,14 @@ __API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char
return -1;
}
echo->__is_expecting = K_TRUE;
at_echo_attach(echo);
at_echo_attach(at_agent, echo);
va_start(args, cmd);
ret = at_cmd_do_exec(cmd, args);
ret = at_cmd_do_exec(at_agent, cmd, args);
va_end(args);
if (ret != 0) {
AT_AGENT->echo = K_NULL;
at_agent->echo = K_NULL;
return -1;
}
@@ -517,42 +515,42 @@ __API__ int tos_at_cmd_exec_until(at_echo_t *echo, uint32_t timeout, const char
tos_sem_destroy(&echo->__expect_notify);
AT_AGENT->echo = K_NULL;
at_agent->echo = K_NULL;
return ret;
}
__STATIC__ int at_recv_cache_init(void)
__STATIC__ int at_recv_cache_init(at_agent_t *at_agent)
{
uint8_t *buffer = K_NULL;
buffer = tos_mmheap_alloc(AT_RECV_CACHE_SIZE);
if (!buffer) {
AT_AGENT->recv_cache.buffer = K_NULL;
at_agent->recv_cache.buffer = K_NULL;
return - 1;
}
AT_AGENT->recv_cache.buffer = buffer;
AT_AGENT->recv_cache.buffer_size = AT_RECV_CACHE_SIZE;
AT_AGENT->recv_cache.recv_len = 0;
at_agent->recv_cache.buffer = buffer;
at_agent->recv_cache.buffer_size = AT_RECV_CACHE_SIZE;
at_agent->recv_cache.recv_len = 0;
return 0;
}
__STATIC__ void at_recv_cache_deinit(void)
__STATIC__ void at_recv_cache_deinit(at_agent_t *at_agent)
{
uint8_t *buffer = K_NULL;
buffer = AT_AGENT->recv_cache.buffer;
buffer = at_agent->recv_cache.buffer;
if (buffer) {
tos_mmheap_free(buffer);
}
AT_AGENT->recv_cache.buffer = K_NULL;
AT_AGENT->recv_cache.buffer_size = 0;
AT_AGENT->recv_cache.recv_len = 0;
at_agent->recv_cache.buffer = K_NULL;
at_agent->recv_cache.buffer_size = 0;
at_agent->recv_cache.recv_len = 0;
}
__STATIC__ at_data_channel_t *at_channel_get(int channel_id, int is_alloc)
__STATIC__ at_data_channel_t *at_channel_get(at_agent_t *at_agent, int channel_id, int is_alloc)
{
/*
if is_alloc is K_TRUE, means we are allocating a channel with certain id,
@@ -566,7 +564,7 @@ __STATIC__ at_data_channel_t *at_channel_get(int channel_id, int is_alloc)
return K_NULL;
}
data_channel = &AT_AGENT->data_channel[channel_id];
data_channel = &at_agent->data_channel[channel_id];
if (is_alloc && data_channel->is_free) {
return data_channel;
@@ -579,13 +577,13 @@ __STATIC__ at_data_channel_t *at_channel_get(int channel_id, int is_alloc)
return K_NULL;
}
__API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_len)
__API__ int tos_at_channel_read(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len)
{
int read_len;
size_t total_read_len = 0;
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel || data_channel->status == AT_CHANNEL_STATUS_BROKEN) {
return -1;
}
@@ -608,23 +606,23 @@ __API__ int tos_at_channel_read(int channel_id, uint8_t *buffer, size_t buffer_l
}
}
__API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout)
__API__ int tos_at_channel_read_timed(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len, uint32_t timeout)
{
int read_len = 0;
size_t total_read_len = 0;
k_tick_t tick, remain_tick;
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel || data_channel->status == AT_CHANNEL_STATUS_BROKEN) {
return -1;
}
tick = tos_millisec2tick(timeout);
tos_stopwatch_countdown(&AT_AGENT->timer, tick);
while (!tos_stopwatch_is_expired(&AT_AGENT->timer)) {
remain_tick = tos_stopwatch_remain(&AT_AGENT->timer);
tos_stopwatch_countdown(&at_agent->timer, tick);
while (!tos_stopwatch_is_expired(&at_agent->timer)) {
remain_tick = tos_stopwatch_remain(&at_agent->timer);
if (remain_tick == (k_tick_t)0u) {
return total_read_len;
}
@@ -648,12 +646,12 @@ __API__ int tos_at_channel_read_timed(int channel_id, uint8_t *buffer, size_t bu
return total_read_len;
}
__API__ int tos_at_channel_write(int channel_id, uint8_t *buffer, size_t buffer_len)
__API__ int tos_at_channel_write(at_agent_t *at_agent, int channel_id, uint8_t *buffer, size_t buffer_len)
{
int ret;
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel) {
return -1;
}
@@ -697,11 +695,11 @@ errout:
return -1;
}
__API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *port)
__API__ int tos_at_channel_alloc_id(at_agent_t *at_agent, int channel_id, const char *ip, const char *port)
{
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_TRUE);
data_channel = at_channel_get(at_agent, channel_id, K_TRUE);
if (!data_channel) {
return -1;
}
@@ -713,13 +711,13 @@ __API__ int tos_at_channel_alloc_id(int channel_id, const char *ip, const char *
return channel_id;
}
__API__ int tos_at_channel_alloc(const char *ip, const char *port)
__API__ int tos_at_channel_alloc(at_agent_t *at_agent, const char *ip, const char *port)
{
int id = 0;
at_data_channel_t *data_channel = K_NULL;
for (id = 0; id < AT_DATA_CHANNEL_NUM; ++id) {
data_channel = &AT_AGENT->data_channel[id];
data_channel = &at_agent->data_channel[id];
if (data_channel->is_free) {
break;
}
@@ -736,11 +734,11 @@ __API__ int tos_at_channel_alloc(const char *ip, const char *port)
return id;
}
__API__ int tos_at_channel_free(int channel_id)
__API__ int tos_at_channel_free(at_agent_t *at_agent, int channel_id)
{
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel) {
return -1;
}
@@ -758,11 +756,11 @@ __API__ int tos_at_channel_free(int channel_id)
return 0;
}
__API__ int tos_at_channel_set_broken(int channel_id)
__API__ int tos_at_channel_set_broken(at_agent_t *at_agent, int channel_id)
{
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel) {
return -1;
}
@@ -771,39 +769,39 @@ __API__ int tos_at_channel_set_broken(int channel_id)
return 0;
}
__API__ int tos_at_channel_is_working(int channel_id)
__API__ int tos_at_channel_is_working(at_agent_t *at_agent, int channel_id)
{
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
return data_channel && data_channel->status == AT_CHANNEL_STATUS_WORKING;
}
__STATIC__ void at_channel_init(void)
__STATIC__ void at_channel_init(at_agent_t *at_agent)
{
int i = 0;
for (i = 0; i < AT_DATA_CHANNEL_NUM; ++i) {
memset(&AT_AGENT->data_channel[i], 0, sizeof(at_data_channel_t));
AT_AGENT->data_channel[i].is_free = K_TRUE;
AT_AGENT->data_channel[i].status = AT_CHANNEL_STATUS_HANGING;
memset(&at_agent->data_channel[i], 0, sizeof(at_data_channel_t));
at_agent->data_channel[i].is_free = K_TRUE;
at_agent->data_channel[i].status = AT_CHANNEL_STATUS_HANGING;
}
}
__STATIC__ void at_channel_deinit(void)
__STATIC__ void at_channel_deinit(at_agent_t *at_agent)
{
int i = 0;
for (i = 0; i < AT_DATA_CHANNEL_NUM; ++i) {
tos_at_channel_free(i);
tos_at_channel_free(at_agent, i);
}
}
__API__ const char *tos_at_channel_ip_get(int channel_id)
__API__ const char *tos_at_channel_ip_get(at_agent_t *at_agent, int channel_id)
{
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel) {
return K_NULL;
}
@@ -811,11 +809,11 @@ __API__ const char *tos_at_channel_ip_get(int channel_id)
return data_channel[channel_id].remote_ip;
}
__API__ const char *tos_at_channel_port_get(int channel_id)
__API__ const char *tos_at_channel_port_get(at_agent_t *at_agent, int channel_id)
{
at_data_channel_t *data_channel = K_NULL;
data_channel = at_channel_get(channel_id, K_FALSE);
data_channel = at_channel_get(at_agent, channel_id, K_FALSE);
if (!data_channel) {
return K_NULL;
}
@@ -823,142 +821,142 @@ __API__ const char *tos_at_channel_port_get(int channel_id)
return data_channel[channel_id].remote_port;
}
__STATIC__ void at_event_table_set(at_event_t *event_table, size_t event_table_size)
__STATIC__ void at_event_table_set(at_agent_t *at_agent, at_event_t *event_table, size_t event_table_size)
{
AT_AGENT->event_table = event_table;
AT_AGENT->event_table_size = event_table_size;
at_agent->event_table = event_table;
at_agent->event_table_size = event_table_size;
}
__API__ int tos_at_init(hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size)
__API__ int tos_at_init(at_agent_t *at_agent, k_stack_t *stk, hal_uart_port_t uart_port, at_event_t *event_table, size_t event_table_size)
{
void *buffer = K_NULL;
memset(AT_AGENT, 0, sizeof(at_agent_t));
memset(at_agent, 0, sizeof(at_agent_t));
at_event_table_set(event_table, event_table_size);
at_event_table_set(at_agent, event_table, event_table_size);
at_channel_init();
at_channel_init(at_agent);
tos_stopwatch_create(&AT_AGENT->timer);
tos_stopwatch_create(&at_agent->timer);
buffer = tos_mmheap_alloc(AT_UART_RX_FIFO_BUFFER_SIZE);
if (!buffer) {
return -1;
}
AT_AGENT->uart_rx_fifo_buffer = (uint8_t *)buffer;
tos_chr_fifo_create(&AT_AGENT->uart_rx_fifo, buffer, AT_UART_RX_FIFO_BUFFER_SIZE);
at_agent->uart_rx_fifo_buffer = (uint8_t *)buffer;
tos_chr_fifo_create(&at_agent->uart_rx_fifo, buffer, AT_UART_RX_FIFO_BUFFER_SIZE);
buffer = tos_mmheap_alloc(AT_CMD_BUFFER_SIZE);
if (!buffer) {
goto errout0;
}
AT_AGENT->cmd_buf = (char *)buffer;
at_agent->cmd_buf = (char *)buffer;
if (tos_mutex_create(&AT_AGENT->cmd_buf_lock) != K_ERR_NONE) {
if (tos_mutex_create(&at_agent->cmd_buf_lock) != K_ERR_NONE) {
goto errout1;
}
if (at_recv_cache_init() != 0) {
if (at_recv_cache_init(at_agent) != 0) {
goto errout2;
}
if (tos_sem_create(&AT_AGENT->uart_rx_sem, (k_sem_cnt_t)0u) != K_ERR_NONE) {
if (tos_sem_create(&at_agent->uart_rx_sem, (k_sem_cnt_t)0u) != K_ERR_NONE) {
goto errout3;
}
if (tos_mutex_create(&AT_AGENT->uart_rx_lock) != K_ERR_NONE) {
if (tos_mutex_create(&at_agent->uart_rx_lock) != K_ERR_NONE) {
goto errout4;
}
if (tos_mutex_create(&AT_AGENT->uart_tx_lock) != K_ERR_NONE) {
if (tos_mutex_create(&at_agent->uart_tx_lock) != K_ERR_NONE) {
goto errout5;
}
if (tos_task_create(&AT_AGENT->parser, "at_parser", at_parser,
K_NULL, AT_PARSER_TASK_PRIO, at_parser_task_stack,
if (tos_task_create(&at_agent->parser, "at_parser", at_parser,
at_agent, AT_PARSER_TASK_PRIO, stk,
AT_PARSER_TASK_STACK_SIZE, 0) != K_ERR_NONE) {
goto errout6;
}
if (tos_hal_uart_init(&AT_AGENT->uart, uart_port) != 0) {
if (tos_hal_uart_init(&at_agent->uart, uart_port) != 0) {
goto errout7;
}
if (tos_mutex_create(&AT_AGENT->global_lock) != K_ERR_NONE) {
if (tos_mutex_create(&at_agent->global_lock) != K_ERR_NONE) {
goto errout8;
}
return 0;
errout8:
tos_hal_uart_deinit(&AT_AGENT->uart);
tos_hal_uart_deinit(&at_agent->uart);
errout7:
tos_task_destroy(&AT_AGENT->parser);
tos_task_destroy(&at_agent->parser);
errout6:
tos_mutex_destroy(&AT_AGENT->uart_tx_lock);
tos_mutex_destroy(&at_agent->uart_tx_lock);
errout5:
tos_mutex_destroy(&AT_AGENT->uart_rx_lock);
tos_mutex_destroy(&at_agent->uart_rx_lock);
errout4:
tos_sem_destroy(&AT_AGENT->uart_rx_sem);
tos_sem_destroy(&at_agent->uart_rx_sem);
errout3:
at_recv_cache_deinit();
at_recv_cache_deinit(at_agent);
errout2:
tos_mutex_destroy(&AT_AGENT->cmd_buf_lock);
tos_mutex_destroy(&at_agent->cmd_buf_lock);
errout1:
tos_mmheap_free(AT_AGENT->cmd_buf);
AT_AGENT->cmd_buf = K_NULL;
tos_mmheap_free(at_agent->cmd_buf);
at_agent->cmd_buf = K_NULL;
errout0:
tos_mmheap_free(AT_AGENT->uart_rx_fifo_buffer);
AT_AGENT->uart_rx_fifo_buffer = K_NULL;
tos_chr_fifo_destroy(&AT_AGENT->uart_rx_fifo);
tos_mmheap_free(at_agent->uart_rx_fifo_buffer);
at_agent->uart_rx_fifo_buffer = K_NULL;
tos_chr_fifo_destroy(&at_agent->uart_rx_fifo);
return -1;
}
__API__ void tos_at_deinit(void)
__API__ void tos_at_deinit(at_agent_t *at_agent)
{
tos_mutex_destroy(&AT_AGENT->global_lock);
tos_mutex_destroy(&at_agent->global_lock);
tos_hal_uart_deinit(&AT_AGENT->uart);
tos_hal_uart_deinit(&at_agent->uart);
tos_task_destroy(&AT_AGENT->parser);
tos_task_destroy(&at_agent->parser);
tos_mutex_destroy(&AT_AGENT->uart_tx_lock);
tos_mutex_destroy(&at_agent->uart_tx_lock);
tos_sem_destroy(&AT_AGENT->uart_rx_sem);
tos_sem_destroy(&at_agent->uart_rx_sem);
at_recv_cache_deinit();
at_recv_cache_deinit(at_agent);
tos_mutex_destroy(&AT_AGENT->cmd_buf_lock);
tos_mutex_destroy(&at_agent->cmd_buf_lock);
tos_mmheap_free(AT_AGENT->cmd_buf);
AT_AGENT->cmd_buf = K_NULL;
tos_mmheap_free(at_agent->cmd_buf);
at_agent->cmd_buf = K_NULL;
tos_mmheap_free(AT_AGENT->uart_rx_fifo_buffer);
AT_AGENT->uart_rx_fifo_buffer = K_NULL;
tos_mmheap_free(at_agent->uart_rx_fifo_buffer);
at_agent->uart_rx_fifo_buffer = K_NULL;
tos_chr_fifo_destroy(&AT_AGENT->uart_rx_fifo);
tos_chr_fifo_destroy(&at_agent->uart_rx_fifo);
tos_stopwatch_destroy(&AT_AGENT->timer);
tos_stopwatch_destroy(&at_agent->timer);
at_channel_deinit();
at_channel_deinit(at_agent);
}
/* To completely decouple the uart intterupt and at agent, we need a more powerful
hal(driver framework), that would be a huge work, we place it in future plans. */
__API__ void tos_at_uart_input_byte(uint8_t data)
__API__ void tos_at_uart_input_byte(at_agent_t *at_agent, uint8_t data)
{
if (tos_chr_fifo_push(&AT_AGENT->uart_rx_fifo, data) == K_ERR_NONE) {
tos_sem_post(&AT_AGENT->uart_rx_sem);
if (tos_chr_fifo_push(&at_agent->uart_rx_fifo, data) == K_ERR_NONE) {
tos_sem_post(&at_agent->uart_rx_sem);
}
}