From e7c891ba14f69d19a81c023ddd550ceb222e7b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AD=94=E7=BD=97=E6=8A=80=E6=9C=AF?= Date: Tue, 5 Jan 2021 22:31:48 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E8=A7=A3=E5=86=B3=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=B8=8D=E5=8C=B9=E9=85=8D=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=E3=80=82?= =?UTF-8?q?=202=E3=80=81=E6=9B=B4=E6=96=B0=E4=BD=BF=E7=94=A8=E6=8F=8F?= =?UTF-8?q?=E8=BF=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 63 +++++++++++++--------- at.c | 73 +++++++++++++------------ at.h | 32 +++++------ at_chat.c | 155 +++++++++++++++++++++++++++--------------------------- at_chat.h | 77 +++++++++++++-------------- at_util.h | 36 ++++++++----- list.h | 18 +++++-- 7 files changed, 245 insertions(+), 209 deletions(-) diff --git a/README.md b/README.md index 7f59d58..1aefd4e 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,13 @@ 一种AT命令通信解析模块,支持裸机(at_chat)和OS版本(at)。适用于modem、WIFI模块、蓝牙通信。 #### 软件架构 -软件架构说明 -at_chat.c at_chat.h用于无OS版本,使用链式队列及异步回调方式处理AT命令收发,支持URC处理。 -at_core.c at_core.h用于OS版本 + +- at_chat.c at_chat.h list.h + +用于无OS版本,使用链式队列及异步回调方式处理AT命令收发,支持URC处理。 +- at.c at.h at_util.h + +用于OS版本, 使用前需要根据at_util.h规定的操作系统相关的接口进行移植,如提供信号量操作、任务延时等操作。 #### 使用说明 ##### at_chat 模块(无OS) @@ -14,22 +18,28 @@ at_core.c at_core.h用于OS版本 ``` -static at_core_t at; //定义AT管理器 +static at_obj_t at; //定义AT控制器 -const at_core_conf_t conf = { //AT管理器配置参数 - +const at_adapter_t adap = { //AT适配器接口 + //适配GPRS模块的串口读写接口 + .write = uart_write, + .read = uart_read + ... }; + +``` + + +3. 初始化AT控制器 + +``` + +at_obj_init(&at, &adap); + ``` -3. 初始化AT管理器 - -``` -at_core_init(&at, &conf); -``` - - -4. 将AT管理器放入任务中轮询 +4. 将AT控制器放入任务中轮询 ``` void main(void) @@ -49,6 +59,9 @@ void main(void) ``` +/** + * @brief AT执行回调处理程序 + */ static void read_csq_callback(at_response_t *r) { /*...*/ @@ -61,15 +74,15 @@ at_send_singlline(&at, read_csq_callback, "AT+CSQ"); ``` -static at_obj_t at; //定义AT管理器 +static at_obj_t at; //定义AT控制器 -char urc_buf[128]; //URC主动上报缓冲区 +static char urc_buf[128]; //URC主动上报缓冲区 utc_item_t utc_tbl[] = { //定义URC表 "+CSQ: ", csq_updated_handler } -const at_conf_t conf = { //AT管理器配置参数 +const at_adapter_t adap = { //AT适配器接口 .urc_buf = urc_buf, .urc_bufsize = sizeof(urc_buf), .utc_tbl = utc_tbl, @@ -79,16 +92,17 @@ const at_conf_t conf = { //AT管理器配置参数 .write = uart_write, .read = uart_read }; -``` - -3. 初始化AT管理器并创建AT线程 ``` + +3. 初始化AT控制器并创建AT线程 + +``` + void at_thread(void) { - at_obj_create(&at, &conf); - while (1) { - /*do something ...*/ + at_obj_create(&at, &adap); + while (1) { at_thread(&at); } } @@ -102,6 +116,7 @@ void at_thread(void) <= +CSQ: 24, 0 <= OK + ``` /* * @brief 获取csq值 @@ -115,7 +130,7 @@ bool read_csq_value(at_obj_t *at, int *rssi, int *error_rate) // if (at_do_cmd(at, &r, "AT+CSQ") != AT_RET_OK) return false; - //解析响应响应 + //提取出响应数据 return (sscanf(recv, "%*[^+]+CSQ: %d,%d", rssi, error_rate) == 2); } diff --git a/at.c b/at.c index 941d2f9..cb08659 100644 --- a/at.c +++ b/at.c @@ -26,7 +26,7 @@ static LIST_HEAD(atlist); /* static void put_string(at_obj_t *at, const char *s) { while (*s != '\0') - at->cfg.write(s++, 1); + at->adap.write(s++, 1); } @@ -37,7 +37,7 @@ static void put_line(at_obj_t *at, const char *s) { put_string(at, s); put_string(at, "\r\n"); - at->cfg.debug("->\r\n%s\r\n", s); + at->adap.debug("->\r\n%s\r\n", s); } //ӡ @@ -67,8 +67,8 @@ static at_return wait_resp(at_obj_t *at, at_respond_t *r) at->resp_timer = at_get_ms(); recvbuf_clr(at); //սջ at->wait = 1; - at_sem_wait(&at->completed, r->timeout); - at->cfg.debug("<-\r\n%s\r\n", r->recvbuf); + at_sem_wait(at->completed, r->timeout); + at->adap.debug("<-\r\n%s\r\n", r->recvbuf); at->resp = NULL; at->wait = 0; return at->ret; @@ -87,7 +87,7 @@ at_return wait_resp_sync(struct at_obj *at, const char *resp, at_return ret = AT_RET_TIMEOUT; unsigned int timer = at_get_ms(); while (at_get_ms() - timer < timeout) { - len = at->cfg.read(buf, sizeof(buf) - cnt); + len = at->adap.read(buf, sizeof(buf) - cnt); cnt += len; buf[cnt] = '\0'; if (strstr(buf, resp)) { @@ -99,7 +99,7 @@ at_return wait_resp_sync(struct at_obj *at, const char *resp, } at_delay(10); } - at->cfg.debug("%s", buf); + at->adap.debug("%s\r\n", buf); return ret; } @@ -107,24 +107,23 @@ at_return wait_resp_sync(struct at_obj *at, const char *resp, /* * @brief ATں */ -void at_obj_create(at_obj_t *at, const at_conf_t cfg) +void at_obj_create(at_obj_t *at, const at_adapter_t *adap) { at_work_env_t *e; - at->cfg = cfg; + at->adap = *adap; at->rcv_cnt = 0; - at_sem_init(&at->cmd_lock, 1); - at_sem_init(&at->completed, 0); + at->cmd_lock = at_sem_new(1); + at->completed = at_sem_new(0); e = &at->env; e->at = at; e->printf = at_print; e->recvclr = recvbuf_clr; - e->read = cfg.read; - e->write = cfg.write; + e->read = adap->read; + e->write = adap->write; e->wait_resp = wait_resp_sync; list_add_tail(&at->node, &atlist); - } /* @@ -149,7 +148,7 @@ at_return at_do_cmd(at_obj_t *at, at_respond_t *r, const char *cmd) if (r == NULL) { r = &default_resp; //ĬӦ } - if (!at_sem_wait(&at->cmd_lock, r->timeout)) { + if (!at_sem_wait(at->cmd_lock, r->timeout)) { return AT_RET_TIMEOUT; } while (at->urc_cnt) { @@ -157,7 +156,7 @@ at_return at_do_cmd(at_obj_t *at, at_respond_t *r, const char *cmd) } put_line(at, cmd); ret = wait_resp(at, r); - at_sem_post(&at->cmd_lock); + at_sem_post(at->cmd_lock); return ret; } @@ -166,27 +165,31 @@ at_return at_do_cmd(at_obj_t *at, at_respond_t *r, const char *cmd) * @param[in] urc * @return none */ -int at_do_work(at_obj_t *at, at_work work, void *params) +at_return at_do_work(at_obj_t *at, at_work work, void *params) { - int ret; - if (!at_sem_wait(&at->cmd_lock, 150 * 1000)) { - return AT_RET_TIMEOUT; - } + at_return ret; + if (!at_sem_wait(at->cmd_lock, 150 * 1000)) { + return AT_RET_TIMEOUT; + } + while (at->urc_cnt) { + at_delay(1); + } at->env.params = params; at->dowork = true; ret = work(&at->env); at->dowork = false; - at_sem_post(&at->cmd_lock); + at_sem_post(at->cmd_lock); return ret; } /* * @brief ָӦ - * @param[in] recvbuf - ջ - * @param[out] lines - Ӧ + * @param[in] recvbuf - ջ + * @param[out] lines - Ӧ + * @param[in] separator- ָ(, \n) * @return */ -int at_split_respond_lines(char *recvbuf, char *lines[], int count) +int at_split_respond_lines(char *recvbuf, char *lines[], int count, char separator) { char *s = recvbuf; size_t i = 0; @@ -212,20 +215,20 @@ int at_split_respond_lines(char *recvbuf, char *lines[], int count) static void urc_handler_entry(at_obj_t *at, char *urcline, unsigned int size) { int i, n; - utc_item_t *tbl = at->cfg.utc_tbl; + utc_item_t *tbl = at->adap.utc_tbl; - for (i = 0; i < at->cfg.urc_tbl_count; i++){ + for (i = 0; i < at->adap.urc_tbl_count; i++){ n = strlen(tbl->prefix); if (n > 0 && strncmp(urcline, tbl->prefix, n) == 0) { tbl->handler(urcline, size); - at->cfg.debug("<=\r\n%s\r\n", urcline); + at->adap.debug("<=\r\n%s\r\n", urcline); return; } tbl++; } if (size >= 2 && !at->wait) //Զ - at->cfg.debug("%s\r\n", urcline); + at->adap.debug("%s\r\n", urcline); } /* @@ -238,13 +241,13 @@ static void urc_recv_process(at_obj_t *at, const char *buf, unsigned int size) char *urc_buf; unsigned short urc_size; unsigned char c; - urc_buf = (char *)at->cfg.urc_buf; - urc_size = at->cfg.urc_bufsize; + urc_buf = (char *)at->adap.urc_buf; + urc_size = at->adap.urc_bufsize; if (at->urc_cnt > 0 && size == 0) { if (AT_IS_TIMEOUT(at->urc_timer, 100)) { //100msʱ urc_buf[at->urc_cnt] = '\0'; at->urc_cnt = 0; - at->cfg.debug("urc recv timeout=>%s\r\n", urc_buf); + at->adap.debug("urc recv timeout=>%s\r\n", urc_buf); } } else { at->urc_timer = at_get_ms(); @@ -284,7 +287,7 @@ static void resp_recv_process(at_obj_t *at, const char *buf, unsigned int size) if (at->rcv_cnt + size >= rcv_size) { // at->rcv_cnt = 0; - at->cfg.debug("Receive overflow:%s", rcv_buf); + at->adap.debug("Receive overflow:%s", rcv_buf); } /*յݷrcv_buf ---------------------------------------------*/ memcpy(rcv_buf + at->rcv_cnt, buf, size); @@ -305,7 +308,7 @@ static void resp_recv_process(at_obj_t *at, const char *buf, unsigned int size) else return; - at_sem_post(&at->completed); + at_sem_post(at->completed); } /* @@ -350,8 +353,8 @@ void at_thread(void) list_for_each_safe(list, n, &atlist) { at = list_entry(list, at_obj_t, node); if (!at->dowork) { -#warning "ȡŻ(readline) ..." - len = at->cfg.read(buf, sizeof(buf)); +//#warning "ȡŻ(readline) ..." + len = at->adap.read(buf, sizeof(buf)); urc_recv_process(at, (char *)buf, len); if (len > 0) { resp_recv_process(at, buf, len); diff --git a/at.h b/at.h index 9742f89..85bfa10 100644 --- a/at.h +++ b/at.h @@ -9,7 +9,7 @@ * Date Author Notes * 2020-01-02 Morro Initial version. ******************************************************************************/ - + #ifndef _AT_H_ #define _AT_H_ @@ -17,7 +17,7 @@ #include "list.h" #include -#define MAX_AT_CMD_LEN 64 +#define MAX_AT_CMD_LEN 128 struct at_obj; /*AT*/ @@ -27,17 +27,17 @@ typedef struct { void (*handler)(char *recvbuf, int size); }utc_item_t; -/*AT -------------------------------------------------------------------*/ +/*ATӿ ---------------------------------------------------------------*/ typedef struct { /*ݶдӿ -----------------------------------------------------------*/ unsigned int (*read)(void *buf, unsigned int len); unsigned int (*write)(const void *buf, unsigned int len); - void (*debug)(const char *fmt, ...); - utc_item_t *utc_tbl; /*utc */ - char *urc_buf; /*urcջ*/ - unsigned short urc_tbl_count; - unsigned short urc_bufsize; /*urcС*/ -}at_conf_t; + void (*debug)(const char *fmt, ...); + utc_item_t *utc_tbl; /* utc */ + char *urc_buf; /* urcջ*/ + unsigned short urc_tbl_count; /* urc*/ + unsigned short urc_bufsize; /* urcС*/ +}at_adapter_t; /*ATӦ ---------------------------------------------------------------*/ typedef enum { @@ -52,11 +52,11 @@ typedef struct { const char *matcher; /*ƥ䴮*/ char *recvbuf; /*ջ*/ unsigned short bufsize; /*ճ*/ - unsigned int timeout; /*ʱʱ */ + unsigned int timeout; /*ʱʱ */ }at_respond_t; /*ATҵ ---------------------------------------------------------------------*/ -typedef struct at_work_env{ +typedef struct at_work_env { struct at_obj *at; void *params; unsigned int (*write)(const void *buf, unsigned int len); @@ -70,7 +70,7 @@ typedef struct at_work_env{ /*AT ---------------------------------------------------------------------*/ typedef struct at_obj { struct list_head node; - at_conf_t cfg; + at_adapter_t adap; at_work_env_t env; at_sem_t cmd_lock; /**/ at_sem_t completed; /**/ @@ -85,9 +85,9 @@ typedef struct at_obj { unsigned char dowork : 1; }at_obj_t; -typedef int (*at_work)(at_work_env_t *); +typedef at_return (*at_work)(at_work_env_t *); -void at_obj_create(at_obj_t *at, const at_conf_t cfg); /*ATʼ*/ +void at_obj_create(at_obj_t *at, const at_adapter_t *adap); /*ATʼ*/ void at_obj_destroy(at_obj_t *at); @@ -99,9 +99,9 @@ void at_resume(at_obj_t *at); /* at_return at_do_cmd(at_obj_t *at, at_respond_t *r, const char *cmd); -int at_split_respond_lines(char *recvbuf, char *lines[], int count); +int at_split_respond_lines(char *recvbuf, char *lines[], int count, char separator); -int at_do_work(at_obj_t *at, at_work work, void *params); /*ִATҵ*/ +at_return at_do_work(at_obj_t *at, at_work work, void *params);/*ִATҵ*/ void at_thread(void); /*AT߳*/ diff --git a/at_chat.c b/at_chat.c index 4bc2d88..d1d90ed 100644 --- a/at_chat.c +++ b/at_chat.c @@ -18,34 +18,34 @@ //ʱж #define AT_IS_TIMEOUT(start, time) (at_get_ms() - (start) > (time)) -/*ATCOMM work type -----------------------------------------------------------*/ -#define AT_TYPE_WORK 0 /* --------------*/ -#define AT_TYPE_CMD 1 /*׼ ----------*/ -#define AT_TYPE_MULTILINE 3 /* ----------*/ -#define AT_TYPE_SINGLLINE 4 /* ----------*/ +/**ATҵ(ʵ4͵״̬ѯ) -----------------------------------*/ +#define AT_TYPE_WORK 0 /* ͨҵ ----------*/ +#define AT_TYPE_CMD 1 /* ׼ ----------*/ +#define AT_TYPE_MULTILINE 3 /* ----------*/ +#define AT_TYPE_SINGLLINE 4 /* ----------*/ typedef int (*base_work)(at_obj_t *at, ...); static void at_send_line(at_obj_t *at, const char *fmt, va_list args); -static const inline at_core_conf_t *__get_adapter(at_obj_t *at) +static const inline at_adapter_t *__get_adapter(at_obj_t *at) { - return &at->cfg; + return &at->adap; } static bool is_timeout(at_obj_t *at, unsigned int ms) { return AT_IS_TIMEOUT(at->resp_timer, ms); } -/* +/** * @brief */ static void send_data(at_obj_t *at, const void *buf, unsigned int len) { - at->cfg.write(buf, len); + at->adap.write(buf, len); } -/* +/** * @brief ʽӡ */ static void print(at_obj_t *at, const char *cmd, ...) @@ -55,49 +55,52 @@ static void print(at_obj_t *at, const char *cmd, ...) at_send_line(at, cmd, args); va_end(args); } -/* +/** * @brief ȡǰݽճ */ static unsigned int get_recv_count(at_obj_t *at) { - return at->rcv_cnt; + return at->recv_cnt; } -/* +/** * @brief ȡݻ */ static char *get_recv_buf(at_obj_t *at) { - return (char *)at->cfg.rcv_buf; + return (char *)at->adap.recv_buf; } -/* +/** * @brief ݻ */ static void recv_buf_clear(at_obj_t *at) { - at->rcv_cnt = 0; + at->recv_cnt = 0; } -/*ǰִ*/ +/**ǰִ*/ static char *search_string(at_obj_t *at, const char *str) { return strstr(get_recv_buf(at), str); } -/*ǰִ*/ +/**ǰִ*/ static bool at_isabort(at_obj_t *at) { return at->cursor ? at->cursor->abort : 1; } -/* +/** * @brief ATִлص */ static void do_at_callbatk(at_obj_t *a, at_item_t *i, at_callbatk_t cb, at_return ret) { at_response_t r; + if ((ret == AT_RET_ERROR || ret == AT_RET_TIMEOUT) && a->adap.error != NULL) + a->adap.error(); + if (cb) { r.param = i->param; r.recvbuf = get_recv_buf(a); @@ -107,16 +110,16 @@ static void do_at_callbatk(at_obj_t *a, at_item_t *i, at_callbatk_t cb, at_retur } } -/* - * @brief AT +/** + * @brief ATʼ * @param[in] cfg - ATӦ */ -void at_core_init(at_obj_t *at, const at_core_conf_t cfg) +void at_core_init(at_obj_t *at, const at_adapter_t *adap) { at_env_t *e; - at->cfg = cfg; + at->adap = *adap; e = &at->env; - at->rcv_cnt = 0; + at->recv_cnt = 0; e->is_timeout = is_timeout; e->printf = print; @@ -126,7 +129,8 @@ void at_core_init(at_obj_t *at, const at_core_conf_t cfg) e->find = search_string; e->abort = at_isabort; } -/*ҵ*/ + +/**ҵ*/ static bool add_work(at_obj_t *at, void *params, void *info, int type) { at_item_t *i; @@ -142,8 +146,8 @@ static bool add_work(at_obj_t *at, void *params, void *info, int type) return i != 0; } -/* - * @brief ִ +/** + * @brief ִҵ */ static int do_work_handler(at_obj_t *at) { @@ -160,7 +164,7 @@ static int do_cmd_handler(at_obj_t *a) { at_item_t *i = a->cursor; at_env_t *e = &a->env; - const at_respond_t *c = (at_respond_t *)i->info; + const at_cmd_t *c = (at_cmd_t *)i->info; switch(e->state) { case 0: /*״̬ ------------------------------------------------------*/ c->sender(e); @@ -168,7 +172,7 @@ static int do_cmd_handler(at_obj_t *a) e->reset_timer(a); e->recvclr(a); break; - case 1: /*״̬ ------------------------------------------------------*/ + case 1: /*״̬ -----------------------------------0-------------------*/ if (search_string(a, c->matcher)) { do_at_callbatk(a, i, c->cb, AT_RET_OK); return true; @@ -177,19 +181,19 @@ static int do_cmd_handler(at_obj_t *a) do_at_callbatk(a, i, c->cb, AT_RET_ERROR); return true; } - e->state = 2; /*֮ʱһʱ*/ - e->reset_timer(a); /*öʱ*/ + e->state = 2; /* ֮ʱһʱ*/ + e->reset_timer(a); /* öʱ*/ } else if (e->is_timeout(a, c->timeout)) { if (++e->i >= c->retry) { do_at_callbatk(a, i, c->cb, AT_RET_TIMEOUT); return true; } - e->state = 0; /*һ״̬*/ + e->state = 0; /**һ״̬*/ } break; case 2: if (e->is_timeout(a, 500)) - e->state = 0; /*سʼ״̬*/ + e->state = 0; /**سʼ״̬*/ break; default: e->state = 0; @@ -198,7 +202,7 @@ static int do_cmd_handler(at_obj_t *a) } /******************************************************************************* - * @brief + * @brief  * @param[in] a - AT * @return 0 - ֹ,0 - ******************************************************************************/ @@ -216,7 +220,7 @@ static int send_signlline_handler(at_obj_t *a) e->reset_timer(a); e->recvclr(a); break; - case 1: /*״̬ ------------------------------------------------------*/ + case 1: /*״̬ -------------------------------------------------------*/ if (search_string(a, "OK")) { do_at_callbatk(a, i, cb, AT_RET_OK); return true; @@ -225,19 +229,19 @@ static int send_signlline_handler(at_obj_t *a) do_at_callbatk(a, i, cb, AT_RET_ERROR); return true; } - e->state = 2; /*֮ʱһʱ*/ - e->reset_timer(a); /*öʱ*/ + e->state = 2; /**֮ʱһʱ*/ + e->reset_timer(a); /**öʱ*/ } else if (e->is_timeout(a, 3000 + e->i * 2000)) { if (++e->i >= 3) { do_at_callbatk(a, i, cb, AT_RET_TIMEOUT); return true; } - e->state = 0; /*һ״̬*/ + e->state = 0; /**һ״̬*/ } break; case 2: if (e->is_timeout(a, 500)) - e->state = 0; /*سʼ״̬*/ + e->state = 0; /**سʼ״̬*/ break; default: e->state = 0; @@ -257,12 +261,12 @@ static int send_multiline_handler(at_obj_t *a) at_callbatk_t cb = (at_callbatk_t)i->info; switch(e->state) { case 0: - if (cmds[e->i] == NULL) { /*ִ*/ + if (cmds[e->i] == NULL) { /**ִ*/ do_at_callbatk(a, i, cb, AT_RET_OK); return true; } e->printf(a, "%s\r\n", cmds[e->i]); - e->recvclr(a); /**/ + e->recvclr(a); /***/ e->reset_timer(a); e->state++; break; @@ -276,8 +280,8 @@ static int send_multiline_handler(at_obj_t *a) do_at_callbatk(a, i, cb, AT_RET_ERROR); return true; } - e->state = 2; /*֮ʱһʱ*/ - e->reset_timer(a); /*öʱ*/ + e->state = 2; /**֮ʱһʱ*/ + e->reset_timer(a); /**öʱ*/ } else if (e->is_timeout(a, 3000)) { do_at_callbatk(a, i, cb, AT_RET_TIMEOUT); return true; @@ -289,7 +293,7 @@ static int send_multiline_handler(at_obj_t *a) return 0; } -/* +/** * @brief * @param[in] fmt - ʽ * @param[in] args - б @@ -298,14 +302,14 @@ static void at_send_line(at_obj_t *at, const char *fmt, va_list args) { char buf[MAX_AT_CMD_LEN]; int len; - const at_core_conf_t *adt = __get_adapter(at); + const at_adapter_t *adt = __get_adapter(at); len = vsnprintf(buf, sizeof(buf), fmt, args); recv_buf_clear(at); //սջ send_data(at, buf, len); send_data(at, "\r\n", 2); } -/* +/** * @brief urc * @param[in] urc * @return none @@ -313,15 +317,15 @@ static void at_send_line(at_obj_t *at, const char *fmt, va_list args) static void urc_handler_entry(at_obj_t *at, char *urc, unsigned int size) { int i, n; - utc_item_t *tbl = at->cfg.utc_tbl; - for (i = 0; i < at->cfg.urc_tbl_count; i++){ + utc_item_t *tbl = at->adap.utc_tbl; + for (i = 0; i < at->adap.urc_tbl_count; i++){ n = strlen(tbl->prefix); if (strncmp(urc, tbl->prefix, n) == 0) tbl[i].handler(urc, size); } } -/* +/** * @brief urc մ * @param[in] buf - ݻ * @return none @@ -330,12 +334,12 @@ static void urc_recv_process(at_obj_t *at, char *buf, unsigned int size) { char *urc_buf; unsigned short urc_size; - urc_buf = (char *)at->cfg.urc_buf; - urc_size = at->cfg.urc_bufsize; + urc_buf = (char *)at->adap.urc_buf; + urc_size = at->adap.urc_bufsize; if (size == 0 && at->urc_cnt > 0) { if (AT_IS_TIMEOUT(at->urc_timer, 2000)){ urc_handler_entry(at, urc_buf, at->urc_cnt); - at->rcv_cnt = 0; + at->recv_cnt = 0; } } else { at->urc_timer = at_get_ms(); @@ -352,29 +356,27 @@ static void urc_recv_process(at_obj_t *at, char *buf, unsigned int size) } } -/* +/** * @brief ָӦմ - * @param[in] buf - * @return none */ static void resp_recv_process(at_obj_t *at, const char *buf, unsigned int size) { - char *rcv_buf; - unsigned short rcv_size; + char *recv_buf; + unsigned short recv_size; - rcv_buf = (char *)at->cfg.rcv_buf; - rcv_size = at->cfg.rcv_bufsize; + recv_buf = (char *)at->adap.recv_buf; + recv_size = at->adap.recv_bufsize; - if (at->rcv_cnt + size >= rcv_size) // - at->rcv_cnt = 0; + if (at->recv_cnt + size >= recv_size) // + at->recv_cnt = 0; - memcpy(rcv_buf + rcv_cnt, buf, size); - at->rcv_cnt += size; - rcv_buf[at->rcv_cnt] = '\0'; - + memcpy(recv_buf + at->recv_cnt, buf, size); + at->recv_cnt += size; + recv_buf[at->recv_cnt] = '\0'; } -/* +/** * @brief ִATҵ * @param[in] a - AT * @param[in] work - ATҵ @@ -385,17 +387,17 @@ bool at_do_work(at_obj_t *at, int (*work)(at_env_t *e), void *params) return add_work(at, params, (void *)work, AT_TYPE_WORK); } -/* +/** * @brief ִATָ * @param[in] a - AT * @param[in] cmd - cmd */ -bool at_do_cmd(at_obj_t *at, void *params, const at_respond_t *cmd) +bool at_do_cmd(at_obj_t *at, void *params, const at_cmd_t *cmd) { return add_work(at, params, (void *)cmd, AT_TYPE_CMD); } -/* +/** * @brief ͵AT * @param[in] at - AT * @param[in] cb - ִлص @@ -407,7 +409,7 @@ bool at_send_singlline(at_obj_t *at, at_callbatk_t cb, const char *singlline) return add_work(at, (void *)singlline, (void *)cb, AT_TYPE_SINGLLINE); } -/* +/** * @brief ͶAT * @param[in] at - AT * @param[in] cb - ִлص @@ -419,7 +421,7 @@ bool at_send_multiline(at_obj_t *at, at_callbatk_t cb, const char **multiline) return add_work(at, multiline, (void *)cb, AT_TYPE_MULTILINE); } -/* +/** * @brief ǿֹATҵ */ @@ -428,7 +430,7 @@ void at_item_abort(at_item_t *i) i->abort = 1; } -/* +/** * @brief ATæж * @return true - ATִָ */ @@ -437,9 +439,9 @@ bool at_core_busy(at_obj_t *at) return !list_empty(&at->ls_ready); } -/******************************************************************************* +/** * @brief ATҵ - ******************************************************************************/ + */ static void at_work_manager(at_obj_t *at) { register at_item_t *cursor = at->cursor; @@ -462,14 +464,13 @@ static void at_work_manager(at_obj_t *at) e->reset_timer(at); at->cursor = list_first_entry(&at->ls_ready, at_item_t, node); } - /*ִ,뵽й ------------------------------------*/ + /**ִ,뵽й ------------------------------------*/ if (work_handler_table[cursor->type](at) || cursor->abort) { list_move_tail(&at->cursor->node, &at->ls_idle); at->cursor = NULL; } - } -/* +/** * @brief ATѯ */ void at_poll_task(at_obj_t *at) @@ -481,5 +482,3 @@ void at_poll_task(at_obj_t *at) resp_recv_process(at, rbuf, read_size); at_work_manager(at); } - - diff --git a/at_chat.h b/at_chat.h index 2cdfdb7..7c688e9 100644 --- a/at_chat.h +++ b/at_chat.h @@ -27,58 +27,56 @@ typedef struct { void (*handler)(char *recvbuf, int size); }utc_item_t; +/*ATӿ ---------------------------------------------------------------*/ typedef struct { - unsigned int (*write)(const void *buf, unsigned int len); /*ͽӿ*/ - unsigned int (*read)(void *buf, unsigned int len); /*սӿ*/ - /*Events -----------------------------------------------------------------*/ - void (*before_at)(void); /*ʼִAT*/ - void (*after_at)(void); - void (*error)(void); - utc_item_t *utc_tbl; /*utc */ - unsigned char *urc_buf; /*urcջ*/ - unsigned char *rcv_buf; - unsigned short urc_tbl_count; - unsigned short urc_bufsize; /*urcС*/ - unsigned short rcv_bufsize; /*ջ*/ -}at_obj_conf_t; + unsigned int (*write)(const void *buf, unsigned int len); /* ͽӿ*/ + unsigned int (*read)(void *buf, unsigned int len); /* սӿ*/ + void (*error)(void); /* ATִ쳣¼*/ + utc_item_t *utc_tbl; /* urc */ + unsigned char *urc_buf; /* urcջ*/ + unsigned char *recv_buf; /* ݻ*/ + unsigned short urc_tbl_count; /* urc*/ + unsigned short urc_bufsize; /* urcС*/ + unsigned short recv_bufsize; /* ջС*/ +}at_adapter_t; /*ATҵл*/ typedef struct { - int i,j,state; + int i,j,state; void *params; - void (*reset_timer)(struct at_obj *at); + void (*reset_timer)(struct at_obj *at); bool (*is_timeout)(struct at_obj *at, unsigned int ms); /*ʱж*/ void (*printf)(struct at_obj *at, const char *fmt, ...); char * (*find)(struct at_obj *at, const char *expect); - char * (*recvbuf)(struct at_obj *at); /*ָջ*/ - unsigned int(*recvlen)(struct at_obj *at); /*ܳ*/ - void (*recvclr)(struct at_obj *at); /*սջ*/ - bool (*abort)(struct at_obj *at); /*ִֹ*/ + char * (*recvbuf)(struct at_obj *at); /* ָջ*/ + unsigned int(*recvlen)(struct at_obj *at); /* ܳ*/ + void (*recvclr)(struct at_obj *at); /* սջ*/ + bool (*abort)(struct at_obj *at); /* ִֹ*/ }at_env_t; /*ATӦ*/ typedef enum { - AT_RET_OK = 0, /*ִгɹ*/ - AT_RET_ERROR, /*ִд*/ - AT_RET_TIMEOUT, /*Ӧʱ*/ - AT_RET_ABORT, /*ǿֹ*/ + AT_RET_OK = 0, /* ִгɹ*/ + AT_RET_ERROR, /* ִд*/ + AT_RET_TIMEOUT, /* Ӧʱ*/ + AT_RET_ABORT, /* ǿֹ*/ }at_return; /*ATӦ */ typedef struct { - void *param; - char *recvbuf; - unsigned short recvcnt; - at_return ret; + void *param; + char *recvbuf; /* ջ*/ + unsigned short recvcnt; /* ݳ*/ + at_return ret; /* ATִн*/ }at_response_t; -typedef void (*at_callbatk_t)(at_response_t *r); - -/*AT״̬ */ +typedef void (*at_callbatk_t)(at_response_t *r); /* AT ִлص*/ + +/*AT״̬ (ǰ汾δ) ------------------------------------------------------*/ typedef enum { - AT_STATE_IDLE, /*״̬*/ - AT_STATE_WAIT, /*ȴִ*/ - AT_STATE_EXEC, /*ִ*/ + AT_STATE_IDLE, /* ״̬*/ + AT_STATE_WAIT, /* ȴִ*/ + AT_STATE_EXEC, /* ִ*/ }at_work_state; /*ATҵ*/ @@ -93,7 +91,7 @@ typedef struct { /*AT ------------------------------------------------------------------*/ typedef struct at_obj{ - at_obj_conf_t cfg; + at_adapter_t adap; at_env_t env; at_item_t tbl[10]; at_item_t *cursor; @@ -102,7 +100,7 @@ typedef struct at_obj{ unsigned int urc_timer; at_return ret; //urcռ, Ӧռ - unsigned short urc_cnt, rcv_cnt; + unsigned short urc_cnt, recv_cnt; unsigned char suspend: 1; }at_obj_t; @@ -114,15 +112,14 @@ typedef struct { unsigned short timeout; /*ʱʱ */ }at_cmd_t; -void at_obj_init(at_obj_t *at, const at_obj_conf_t cfg); +void at_obj_init(at_obj_t *at, const at_adapter_t *); -/*͵AT*/ bool at_send_singlline(at_obj_t *at, at_callbatk_t cb, const char *singlline); -/*ͶAT*/ + bool at_send_multiline(at_obj_t *at, at_callbatk_t cb, const char **multiline); -/*ִAT*/ + bool at_do_cmd(at_obj_t *at, void *params, const at_cmd_t *cmd); -/*ԶATҵ*/ + bool at_do_work(at_obj_t *at, int (*work)(at_env_t *e), void *params); void at_item_abort(at_item_t *it); /*ֹǰҵ*/ diff --git a/at_util.h b/at_util.h index bd2b6bd..ef6f4fd 100644 --- a/at_util.h +++ b/at_util.h @@ -13,18 +13,19 @@ #ifndef _ATUTIL_H_ #define _ATUTIL_H_ -#include "os.h" #include #include +#include -typedef struct os_semaphore at_sem_t; /*ź*/ +typedef void *at_sem_t; /*ź*/ /* * @brief ȡǰϵͳ */ static inline unsigned int at_get_ms(void) { - return ril_get_ms(); + /*Add here...*/ + return 0; } /* * @brief ʱж @@ -41,32 +42,41 @@ static inline bool at_istimeout(unsigned int start_time, unsigned int timeout) */ static inline void at_delay(uint32_t ms) { - os_delay(ms); + } /* - * @brief ʼź + * @brief ź * @retval none */ -static inline void at_sem_init(at_sem_t *s, int value) +static inline at_sem_t at_sem_new(int value) { - os_sem_init(s, value); + return NULL; } /* - * @brief ȡź + * @brief ȴź * @retval none */ -static inline bool at_sem_wait(at_sem_t *s, uint32_t timeout) +static inline bool at_sem_wait(at_sem_t s, uint32_t timeout) { - return os_sem_wait(s, timeout); + return false; } /* - * @brief ͷź + * @brief ͷź * @retval none */ -static inline void at_sem_post(at_sem_t *s) +static inline void at_sem_post(at_sem_t s) { - os_sem_post(s); + +} + +/* + * @brief ٵź + * @retval none + */ +static inline void at_sem_free(at_sem_t s) +{ + } #endif \ No newline at end of file diff --git a/list.h b/list.h index 7e3f143..1511b51 100644 --- a/list.h +++ b/list.h @@ -1,9 +1,14 @@ - -#ifndef _LINUX_LIST_H_ -#define _LINUX_LIST_H_ +#ifndef _LINUX_LIST_H +#define _LINUX_LIST_H #include +#define typeof (struct list_head) + + +#undef offsetof +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) + /** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. @@ -11,9 +16,16 @@ * @member: the name of the member within the struct. * */ +#if defined(__ICCARM__) || defined(__CC_ARM) #define container_of(ptr, type, member) ( \ (type *)( (char *)(ptr) - offsetof(type,member) )) +#else +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) +#endif +/* copy from , */ /* * used to verify that nobody uses non-initialized list entries. */