From 90c59e1d7a614eb8fdab73ee35fa1c7423bec380 Mon Sep 17 00:00:00 2001 From: "roger.luo" Date: Sun, 16 Jun 2024 00:36:02 +0800 Subject: [PATCH] Resolve the issue of not clearing the receive cache before sending multi line commands. --- src/at_chat.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/at_chat.c b/src/at_chat.c index dc4ff0c..32970d0 100644 --- a/src/at_chat.c +++ b/src/at_chat.c @@ -202,7 +202,7 @@ static void send_cmdline(at_info_t *at, const char *cmd) len = strlen(cmd); __get_adapter(at)->write(cmd, len); __get_adapter(at)->write("\r\n", 2); - AT_DEBUG(at,"->\r\n%s\r\n", cmd); + AT_DEBUG(at,"->\r\n%s", cmd); } /** @@ -228,6 +228,7 @@ static char *get_recvbuf(at_env_t *env) static void recvbuf_clear(at_env_t *env) { + obj_map(env->obj)->recvbuf[0] = '\0'; obj_map(env->obj)->recv_cnt = 0; } @@ -300,12 +301,12 @@ static void at_finish(struct at_env *env, at_resp_code code) static void do_at_callback(at_info_t *ai, work_item_t *wi, at_resp_code code) { at_response_t r; - AT_DEBUG(ai, "<-\r\n%s\r\n", ai->recvbuf); + AT_DEBUG(ai, "<-\r\n%s", ai->recvbuf); //Exception notification if ((code == AT_RESP_ERROR || code == AT_RESP_TIMEOUT) && __get_adapter(ai)->error != NULL) { __get_adapter(ai)->error(&r); ai->err_occur = 1; - AT_DEBUG(ai, "AT Respose :%s\r\n", code == AT_RESP_TIMEOUT ? "timeout" : "error"); + AT_DEBUG(ai, "AT Respose :%s", code == AT_RESP_TIMEOUT ? "timeout" : "error"); } else { ai->err_occur = 0; } @@ -581,11 +582,11 @@ static int send_multiline_handler(at_info_t *ai) env->i++; env->j = 0; env->params = (void *)true; /*Mark execution status*/ - AT_DEBUG(ai, "<-\r\n%s\r\n", ai->recvbuf); + AT_DEBUG(ai, "<-\r\n%s", ai->recvbuf); } else if (find_substr(env, AT_DEF_RESP_ERR)) { - AT_DEBUG(ai, "<-\r\n%s\r\n", ai->recvbuf); + AT_DEBUG(ai, "<-\r\n%s", ai->recvbuf); env->j++; - AT_DEBUG(ai, "CMD:'%s' failed to executed, retry:%d\r\n", cmds[env->i], env->j); + AT_DEBUG(ai, "CMD:'%s' failed to executed, retry:%d", cmds[env->i], env->j); if (env->j >= attr->retry) { env->state = 0; env->j = 0; @@ -643,6 +644,31 @@ void at_obj_set_urc(at_obj_t *at, const urc_item_t *tbl, int count) ai->urc_tbl = tbl; ai->urc_tbl_size = count; } +/** + * @brief Get the urc recv buffer count. + */ +int at_obj_get_urcbuf_count(at_obj_t *at) +{ + return obj_map(at)->urc_cnt; +} +/** + * @brief Temporarily disables the URC matching program + * @param enable Enable/disable the URC matching handler + * @param timeout Disabled timeout(ms). + */ +void at_obj_urc_set_enable(at_obj_t *at, int enable, unsigned short timeout) +{ + at_info_t *ai = obj_map(at); + ai->urc_enable = enable ? 1 : 0; + if (!enable) { + ai->urc_timer = at_get_ms(); + ai->urc_disable_time = timeout; + //AT_DEBUG(ai, "Disable the URC, time:%d ms\r\n", timeout); + } else { + //AT_DEBUG(ai, "Enable the URC\r\n"); + } +} + /** * @brief Find a URC handler based on URC receive buffer information. */ @@ -663,6 +689,7 @@ static void urc_reset(at_info_t *ai) ai->urc_cnt = 0; ai->urc_item = NULL; ai->urc_match = 0; + ai->urc_timer = at_get_ms(); } /** @@ -682,6 +709,7 @@ static void urc_handler_entry(at_info_t *ai, urc_recv_status status, char *urc, /* Send URC event notification. */ remain = ai->urc_item ? ai->urc_item->handler(&ctx) : 0; if (remain == 0 && (ai->urc_item || ai->cursor == NULL)) { + urc_reset(ai); } else { AT_DEBUG(ai,"URC receives %d bytes remaining.\r\n", remain); @@ -696,9 +724,9 @@ static void urc_timeout_process(at_info_t *ai) if (ai->urc_cnt > 0 && AT_IS_TIMEOUT(ai->urc_timer, AT_URC_TIMEOUT)) { if (ai->urc_cnt > 2 && ai->urc_item != NULL) { ai->urcbuf[ai->urc_cnt] = '\0'; - AT_DEBUG(ai,"urc recv timeout=>%s\r\n", ai->urcbuf); - urc_handler_entry(ai, URC_RECV_TIMEOUT, ai->urcbuf, ai->urc_cnt); - } + AT_DEBUG(ai,"urc recv timeout=>%s\r\n", ai->urcbuf); + urc_handler_entry(ai, URC_RECV_TIMEOUT, ai->urcbuf, ai->urc_cnt); + } urc_reset(ai); } } @@ -724,6 +752,7 @@ static void urc_recv_process(at_info_t *ai, char *buf, unsigned int size) ai->urc_enable = 1; AT_DEBUG(ai, "Enable the URC match handler\r\n"); } + ai->urc_timer = at_get_ms(); urc_buf = ai->urcbuf; while (size--) { ch = *buf++; @@ -764,7 +793,7 @@ static void urc_recv_process(at_info_t *ai, char *buf, unsigned int size) static void resp_recv_process(at_info_t *ai, const char *buf, unsigned int size) { if (size == 0) - return; + return; if (ai->recv_cnt + size >= ai->recv_bufsize) //Receive overflow, clear directly. ai->recv_cnt = 0; @@ -860,7 +889,7 @@ at_obj_t *at_obj_create(const at_adapter_t *adap) e = &ai->env; ai->recv_cnt = 0; ai->urc_enable = 1; - ai->enable = 1; + ai->enable = 1; //Initialization of public work environment. e->is_timeout = at_is_timeout; e->println = println;