| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112 |
- /*
- * Copyright (c) 2006-2025 RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2018-03-30 chenyong first version
- * 2018-04-12 chenyong add client implement
- * 2018-08-17 chenyong multiple client support
- * 2021-03-17 Meco Man fix a buf of leaking memory
- * 2021-07-14 Sszl fix a buf of leaking memory
- * 2025-01-02 dongly support SERIAL_V2
- * 2025-04-18 RyanCw support New SERIAL_V2
- * 2025-08-11 RyanCw add client deInit
- */
- #include <at.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LOG_TAG "at.clnt"
- #include <at_log.h>
- #ifdef AT_USING_CLIENT
- #define AT_RESP_END_OK "OK"
- #define AT_RESP_END_ERROR "ERROR"
- #define AT_RESP_END_FAIL "FAIL"
- #define AT_END_CR_LF "\r\n"
- #define AT_END_CR "\r"
- #define AT_END_LF "\n"
- #define AT_END_RAW ""
- #define at_client_rx_notice_event (1 << 0)
- #define at_client_resp_notice_event (1 << 1)
- #define at_client_deInit_event (1 << 2)
- #define at_client_deInit_over_event (1 << 3)
- static rt_slist_t g_at_client_list = RT_SLIST_OBJECT_INIT(g_at_client_list);
- extern rt_size_t at_utils_send(rt_device_t dev,
- rt_off_t pos,
- const void *buffer,
- rt_size_t size);
- extern rt_size_t at_vprintfln(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args);
- extern rt_size_t at_vprintf(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args);
- extern rt_size_t at_vprintfcr(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args);
- extern rt_size_t at_vprintflf(rt_device_t device, char *send_buf, rt_size_t buf_size, const char *format, va_list args);
- extern void at_print_raw_cmd(const char *type, const char *cmd, rt_size_t size);
- /**
- * Create response object.
- *
- * @param buf_size the maximum response buffer size
- * @param line_num the number of setting response lines
- * = 0: the response data will auto return when received 'OK' or 'ERROR'
- * != 0: the response data will return when received setting lines number data
- * @param timeout the maximum response time
- *
- * @return != RT_NULL: response object
- * = RT_NULL: no memory
- */
- at_response_t at_create_resp(rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout)
- {
- at_response_t resp = RT_NULL;
- resp = (at_response_t)rt_calloc(1, sizeof(struct at_response));
- if (resp == RT_NULL)
- {
- LOG_E("AT create response object failed! No memory for response object!");
- return RT_NULL;
- }
- resp->buf = (char *)rt_calloc(1, buf_size);
- if (resp->buf == RT_NULL)
- {
- LOG_E("AT create response object failed! No memory for response buffer!");
- rt_free(resp);
- return RT_NULL;
- }
- resp->buf_size = buf_size;
- resp->line_num = line_num;
- resp->line_counts = 0;
- resp->timeout = timeout;
- return resp;
- }
- /**
- * Delete and free response object.
- *
- * @param resp response object
- */
- void at_delete_resp(at_response_t resp)
- {
- if (resp && resp->buf)
- {
- rt_free(resp->buf);
- }
- if (resp)
- {
- rt_free(resp);
- resp = RT_NULL;
- }
- }
- /**
- * Set response object information
- *
- * @param resp response object
- * @param buf_size the maximum response buffer size
- * @param line_num the number of setting response lines
- * = 0: the response data will auto return when received 'OK' or 'ERROR'
- * != 0: the response data will return when received setting lines number data
- * @param timeout the maximum response time
- *
- * @return != RT_NULL: response object
- * = RT_NULL: no memory
- */
- at_response_t at_resp_set_info(at_response_t resp, rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout)
- {
- char *p_temp;
- RT_ASSERT(resp);
- if (resp->buf_size != buf_size)
- {
- resp->buf_size = buf_size;
- p_temp = (char *)rt_realloc(resp->buf, buf_size);
- if (p_temp == RT_NULL)
- {
- LOG_D("No memory for realloc response buffer size(%d).", buf_size);
- return RT_NULL;
- }
- else
- {
- resp->buf = p_temp;
- }
- }
- resp->line_num = line_num;
- resp->timeout = timeout;
- return resp;
- }
- /**
- * Get one line AT response buffer by line number.
- *
- * @param resp response object
- * @param resp_line line number, start from '1'
- *
- * @return != RT_NULL: response line buffer
- * = RT_NULL: input response line error
- */
- const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line)
- {
- char *resp_buf = resp->buf;
- rt_size_t line_num = 1;
- RT_ASSERT(resp);
- if (resp_line > resp->line_counts || resp_line <= 0)
- {
- LOG_E("AT response get line failed! Input response line(%d) error!", resp_line);
- return RT_NULL;
- }
- for (line_num = 1; line_num <= resp->line_counts; line_num++)
- {
- if (resp_line == line_num)
- {
- return resp_buf;
- }
- resp_buf += strlen(resp_buf) + 1;
- }
- return RT_NULL;
- }
- /**
- * Get one line AT response buffer by keyword
- *
- * @param resp response object
- * @param keyword query keyword
- *
- * @return != RT_NULL: response line buffer
- * = RT_NULL: no matching data
- */
- const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword)
- {
- char *resp_buf = resp->buf;
- rt_size_t line_num = 1;
- RT_ASSERT(resp);
- RT_ASSERT(keyword);
- for (line_num = 1; line_num <= resp->line_counts; line_num++)
- {
- if (strstr(resp_buf, keyword))
- {
- return resp_buf;
- }
- resp_buf += strlen(resp_buf) + 1;
- }
- return RT_NULL;
- }
- /**
- * Get and parse AT response buffer arguments by line number.
- *
- * @param resp response object
- * @param resp_line line number, start from '1'
- * @param resp_expr response buffer expression
- *
- * @return -1 : input response line number error or get line buffer error
- * 0 : parsed without match
- * >0 : the number of arguments successfully parsed
- */
- int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...)
- {
- va_list args;
- int resp_args_num = 0;
- const char *resp_line_buf = RT_NULL;
- RT_ASSERT(resp);
- RT_ASSERT(resp_expr);
- resp_line_buf = at_resp_get_line(resp, resp_line);
- if (resp_line_buf == RT_NULL)
- {
- return -1;
- }
- va_start(args, resp_expr);
- resp_args_num = vsscanf(resp_line_buf, resp_expr, args);
- va_end(args);
- return resp_args_num;
- }
- /**
- * Get and parse AT response buffer arguments by keyword.
- *
- * @param resp response object
- * @param keyword query keyword
- * @param resp_expr response buffer expression
- *
- * @return -1 : input keyword error or get line buffer error
- * 0 : parsed without match
- * >0 : the number of arguments successfully parsed
- */
- int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...)
- {
- va_list args;
- int resp_args_num = 0;
- const char *resp_line_buf = RT_NULL;
- RT_ASSERT(resp);
- RT_ASSERT(resp_expr);
- resp_line_buf = at_resp_get_line_by_kw(resp, keyword);
- if (resp_line_buf == RT_NULL)
- {
- return -1;
- }
- va_start(args, resp_expr);
- resp_args_num = vsscanf(resp_line_buf, resp_expr, args);
- va_end(args);
- return resp_args_num;
- }
- /**
- * Send commands to AT server and wait response.
- *
- * @param client current AT client object
- * @param resp AT response object, using RT_NULL when you don't care response
- * @param cmd_expr AT commands expression
- *
- * @return 0 : success
- * -1 : response status error
- * -2 : wait timeout
- * -7 : enter AT CLI mode
- */
- int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...)
- {
- va_list args;
- rt_err_t result = RT_EOK;
- RT_ASSERT(cmd_expr);
- if (client == RT_NULL)
- {
- LOG_E("input AT Client object is NULL, please create or get AT Client object!");
- return -RT_ERROR;
- }
- /* check AT CLI mode */
- if (client->status == AT_STATUS_CLI && resp)
- {
- return -RT_EBUSY;
- }
- rt_mutex_take(&client->lock, RT_WAITING_FOREVER);
- client->resp_status = AT_RESP_OK;
- if (resp != RT_NULL)
- {
- resp->buf_len = 0;
- resp->line_counts = 0;
- }
- client->resp = resp;
- rt_event_recv(&client->event, at_client_resp_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 0, NULL);
- va_start(args, cmd_expr);
- client->last_cmd_len = at_vprintfln(client->device, client->send_buf, client->send_bufsz, cmd_expr, args);
- if (client->last_cmd_len > 2)
- {
- client->last_cmd_len -= 2; /* "\r\n" */
- }
- va_end(args);
- if (resp != RT_NULL)
- {
- if (rt_event_recv(&client->event, at_client_resp_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, resp->timeout, NULL) != RT_EOK)
- {
- LOG_W("execute command (%.*s) timeout (%d ticks)!", client->last_cmd_len, client->send_buf, resp->timeout);
- client->resp_status = AT_RESP_TIMEOUT;
- result = -RT_ETIMEOUT;
- }
- else if (client->resp_status != AT_RESP_OK)
- {
- LOG_E("execute command (%.*s) failed!", client->last_cmd_len, client->send_buf);
- result = -RT_ERROR;
- }
- }
- client->resp = RT_NULL;
- rt_mutex_release(&client->lock);
- return result;
- }
- /**
- * Send commands through custom formatting to AT server and wait response.
- *
- * @param client current AT client object
- * @param resp AT response object, using RT_NULL when you don't care response
- * @param format formatting macro, it can be one of these values: AT_END_CR_LF, AT_END_RAW, AT_END_CR, AT_END_LF.
- * Behavior of AT_END_CR_LF is same as at_obj_exec_cmd, and it will add \r\n symnbol behind message.
- * AT_END_RAW means frame work won't modify anything of message. AT_END_CR will add \r for Carriage
- * Return. AT_END_LF means add \\n for Line Feed.
- * @param cmd_expr AT commands expression
- *
- * @return 0 : success
- * -1 : response status error
- * -2 : wait timeout
- * -7 : enter AT CLI mode
- */
- int at_obj_exec_cmd_format(at_client_t client, at_response_t resp, const char *format, const char *cmd_expr, ...)
- {
- va_list args;
- rt_err_t result = RT_EOK;
- RT_ASSERT(cmd_expr);
- if (client == RT_NULL)
- {
- LOG_E("input AT Client object is NULL, please create or get AT Client object!");
- return -RT_ERROR;
- }
- /* check AT CLI mode */
- if (client->status == AT_STATUS_CLI && resp)
- {
- return -RT_EBUSY;
- }
- rt_mutex_take(&client->lock, RT_WAITING_FOREVER);
- client->resp_status = AT_RESP_OK;
- if (resp != RT_NULL)
- {
- resp->buf_len = 0;
- resp->line_counts = 0;
- }
- client->resp = resp;
- rt_event_recv(&client->event, at_client_resp_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 0, NULL);
- va_start(args, cmd_expr);
- if (strcmp(format, AT_END_CR_LF) == 0)
- {
- client->last_cmd_len = at_vprintfln(client->device, client->send_buf, client->send_bufsz, cmd_expr, args);
- }
- else if (strcmp(format, AT_END_RAW) == 0)
- {
- client->last_cmd_len = at_vprintf(client->device, client->send_buf, client->send_bufsz, cmd_expr, args);
- }
- else if (strcmp(format, AT_END_CR) == 0)
- {
- client->last_cmd_len = at_vprintfcr(client->device, client->send_buf, client->send_bufsz, cmd_expr, args);
- }
- else if (strcmp(format, AT_END_LF) == 0)
- {
- client->last_cmd_len = at_vprintflf(client->device, client->send_buf, client->send_bufsz, cmd_expr, args);
- }
- va_end(args);
- if (resp != RT_NULL)
- {
- if (rt_event_recv(&client->event, at_client_resp_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, resp->timeout, NULL) != RT_EOK)
- {
- LOG_W("execute command (%.*s) timeout (%d ticks)!", client->last_cmd_len, client->send_buf, resp->timeout);
- client->resp_status = AT_RESP_TIMEOUT;
- result = -RT_ETIMEOUT;
- }
- else if (client->resp_status != AT_RESP_OK)
- {
- LOG_E("execute command (%.*s) failed!", client->last_cmd_len, client->send_buf);
- result = -RT_ERROR;
- }
- }
- client->resp = RT_NULL;
- rt_mutex_release(&client->lock);
- return result;
- }
- /**
- * Waiting for connection to external devices.
- *
- * @param client current AT client object
- * @param timeout millisecond for timeout
- *
- * @return 0 : success
- * -2 : timeout
- * -5 : no memory
- */
- int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout)
- {
- rt_err_t result = RT_EOK;
- at_response_t resp = RT_NULL;
- rt_tick_t start_time = 0;
- if (client == RT_NULL)
- {
- LOG_E("input AT client object is NULL, please create or get AT Client object!");
- return -RT_ERROR;
- }
- resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
- if (resp == RT_NULL)
- {
- LOG_E("no memory for AT client(%s) response object.", client->device->parent.name);
- return -RT_ENOMEM;
- }
- start_time = rt_tick_get();
- while (1)
- {
- /* Check whether it is timeout */
- if (rt_tick_get() - start_time > rt_tick_from_millisecond(timeout))
- {
- LOG_E("wait AT client(%s) connect timeout(%d tick).", client->device->parent.name, timeout);
- result = -RT_ETIMEOUT;
- break;
- }
- if (at_obj_exec_cmd(client, resp, "AT") == RT_EOK)
- {
- break;
- }
- }
- at_delete_resp(resp);
- return result;
- }
- /**
- * Send data to AT server, send data don't have end sign(eg: \r\n).
- *
- * @param client current AT client object
- * @param buf send data buffer
- * @param size send fixed data size
- *
- * @return >0: send data size
- * =0: send failed
- */
- rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size)
- {
- rt_size_t len;
- RT_ASSERT(buf);
- if (client == RT_NULL)
- {
- LOG_E("input AT Client object is NULL, please create or get AT Client object!");
- return 0;
- }
- #ifdef AT_PRINT_RAW_CMD
- at_print_raw_cmd("sendline", buf, size);
- #endif
- rt_mutex_take(&client->lock, RT_WAITING_FOREVER);
- len = at_utils_send(client->device, 0, buf, size);
- rt_mutex_release(&client->lock);
- return len;
- }
- /**
- * AT client receive fixed-length data.
- *
- * @param client current AT client object
- * @param buf receive data buffer
- * @param size receive fixed data size
- * @param timeout receive data timeout (ms)
- *
- * @note this function can only be used in execution function of URC data
- *
- * @return >0: receive data size
- * =0: receive failed
- */
- rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size, rt_int32_t timeout)
- {
- rt_size_t read_idx = 0;
- RT_ASSERT(buf);
- if (client == RT_NULL)
- {
- LOG_E("input AT Client object is NULL, please create or get AT Client object!");
- return 0;
- }
- #ifndef RT_USING_SERIAL_V2
- while (size)
- {
- rt_size_t read_len;
- rt_event_recv(&client->event, at_client_rx_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 0, NULL);
- read_len = rt_device_read(client->device, 0, buf + read_idx, size);
- if (read_len > 0)
- {
- read_idx += read_len;
- size -= read_len;
- }
- else
- {
- if (rt_event_recv(&client->event, at_client_rx_notice_event, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, rt_tick_from_millisecond(timeout), NULL) != RT_EOK)
- break;
- }
- }
- #else
- rt_int32_t rx_timeout = rt_tick_from_millisecond(timeout);
- rt_device_control(client->device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timeout);
- read_idx = rt_device_read(client->device, 0, buf, size);
- rx_timeout = RT_WAITING_NO;
- rt_device_control(client->device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timeout);
- #endif
- #ifdef AT_PRINT_RAW_CMD
- at_print_raw_cmd("urc_recv", buf, read_idx);
- #endif
- return read_idx;
- }
- /**
- * AT client set end sign.
- *
- * @param client current AT client object
- * @param ch the end sign, can not be used when it is '\0'
- */
- void at_obj_set_end_sign(at_client_t client, char ch)
- {
- if (client == RT_NULL)
- {
- LOG_E("input AT Client object is NULL, please create or get AT Client object!");
- return;
- }
- client->end_sign = ch;
- }
- /**
- * set URC(Unsolicited Result Code) table
- *
- * @param client current AT client object
- * @param table URC table
- * @param size table size
- */
- int at_obj_set_urc_table(at_client_t client, const struct at_urc *urc_table, rt_size_t table_sz)
- {
- rt_size_t idx;
- if (client == RT_NULL)
- {
- LOG_E("input AT Client object is NULL, please create or get AT Client object!");
- return -RT_ERROR;
- }
- for (idx = 0; idx < table_sz; idx++)
- {
- RT_ASSERT(urc_table[idx].cmd_prefix);
- RT_ASSERT(urc_table[idx].cmd_suffix);
- }
- if (client->urc_table_size == 0)
- {
- client->urc_table = (struct at_urc_table *)rt_calloc(1, sizeof(struct at_urc_table));
- if (client->urc_table == RT_NULL)
- {
- return -RT_ENOMEM;
- }
- client->urc_table[0].urc = urc_table;
- client->urc_table[0].urc_size = table_sz;
- client->urc_table_size++;
- }
- else
- {
- struct at_urc_table *new_urc_table = RT_NULL;
- /* realloc urc table space */
- new_urc_table = (struct at_urc_table *)rt_realloc(client->urc_table, client->urc_table_size * sizeof(struct at_urc_table) + sizeof(struct at_urc_table));
- if (new_urc_table == RT_NULL)
- {
- return -RT_ENOMEM;
- }
- client->urc_table = new_urc_table;
- client->urc_table[client->urc_table_size].urc = urc_table;
- client->urc_table[client->urc_table_size].urc_size = table_sz;
- client->urc_table_size++;
- }
- return RT_EOK;
- }
- /**
- * get AT client object by AT device name.
- *
- * @dev_name AT client device name
- *
- * @return AT client object
- */
- at_client_t at_client_get(const char *dev_name)
- {
- RT_ASSERT(dev_name);
- rt_slist_t *node;
- at_client_t client;
- rt_base_t level = rt_hw_interrupt_disable();
- rt_slist_for_each(node, &g_at_client_list)
- {
- client = rt_slist_entry(node, struct at_client, list);
- if (rt_strcmp(client->device->parent.name, dev_name) == 0)
- {
- rt_hw_interrupt_enable(level);
- return client;
- }
- }
- rt_hw_interrupt_enable(level);
- return RT_NULL;
- }
- /**
- * get first AT client object in the table.
- *
- * @return AT client object
- */
- at_client_t at_client_get_first(void)
- {
- at_client_t client = RT_NULL;
- rt_base_t level = rt_hw_interrupt_disable();
- if (!rt_slist_isempty(&g_at_client_list))
- {
- client = rt_slist_first_entry(&g_at_client_list, struct at_client, list);
- }
- rt_hw_interrupt_enable(level);
- return client;
- }
- static const struct at_urc *get_urc_obj(at_client_t client)
- {
- rt_size_t i, j, prefix_len, suffix_len;
- rt_size_t bufsz;
- char *buffer = RT_NULL;
- const struct at_urc *urc = RT_NULL;
- struct at_urc_table *urc_table = RT_NULL;
- if (client->urc_table == RT_NULL)
- {
- return RT_NULL;
- }
- buffer = client->recv_line_buf;
- bufsz = client->recv_line_len;
- for (i = 0; i < client->urc_table_size; i++)
- {
- for (j = 0; j < client->urc_table[i].urc_size; j++)
- {
- urc_table = client->urc_table + i;
- urc = urc_table->urc + j;
- prefix_len = rt_strlen(urc->cmd_prefix);
- suffix_len = rt_strlen(urc->cmd_suffix);
- if (bufsz < prefix_len + suffix_len)
- {
- continue;
- }
- if ((prefix_len ? !rt_strncmp(buffer, urc->cmd_prefix, prefix_len) : 1)
- && (suffix_len ? !rt_strncmp(buffer + bufsz - suffix_len, urc->cmd_suffix, suffix_len) : 1))
- {
- return urc;
- }
- }
- }
- return RT_NULL;
- }
- static rt_err_t at_client_getchar(at_client_t client, char *ch)
- {
- rt_err_t result = RT_EOK;
- rt_ssize_t recvLen = 0;
- /* Temporarily retain the distinction */
- #ifndef RT_USING_SERIAL_V2
- recvLen = rt_device_read(client->device, 0, ch, 1);
- if (recvLen <= 0)
- {
- result = -RT_ERROR;
- }
- #else
- recvLen = rt_device_read(client->device, 0, ch, 1);
- if (recvLen != 1)
- {
- result = -RT_ERROR;
- }
- #endif
- return result;
- }
- static int at_recv_readline(at_client_t client)
- {
- char ch = 0, last_ch = 0;
- rt_bool_t is_full = RT_FALSE;
- rt_uint32_t event;
- rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
- client->recv_line_len = 0;
- while (1)
- {
- event = 0;
- rt_event_recv(&client->event, at_client_rx_notice_event | at_client_deInit_event,
- RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &event);
- if (event & at_client_deInit_event)
- {
- rt_event_send(&client->event, at_client_deInit_over_event);
- rt_thread_delete(rt_thread_self());
- }
- if (event & at_client_rx_notice_event)
- {
- while (RT_EOK == at_client_getchar(client, &ch))
- {
- if (client->recv_line_len < client->recv_bufsz)
- {
- client->recv_line_buf[client->recv_line_len++] = ch;
- }
- else
- {
- is_full = RT_TRUE;
- }
- /* is newline or URC data */
- client->urc = get_urc_obj(client);
- if (client->urc != RT_NULL || (ch == '\n' && last_ch == '\r')
- || (client->end_sign != 0 && ch == client->end_sign))
- {
- if (is_full)
- {
- LOG_E("read line failed. The line data length is out of buffer size(%d)!", client->recv_bufsz);
- rt_memset(client->recv_line_buf, 0x00, client->recv_bufsz);
- client->recv_line_len = 0;
- return -RT_EFULL;
- }
- /* Since the buffer state is uncertain, we proactively clear it; the overhead is negligible. */
- rt_event_send(&client->event, at_client_rx_notice_event);
- goto __next;
- }
- last_ch = ch;
- }
- }
- }
- __next:
- #ifdef AT_PRINT_RAW_CMD
- at_print_raw_cmd("recvline", client->recv_line_buf, client->recv_line_len);
- #endif
- return client->recv_line_len;
- }
- static void client_parser(at_client_t client)
- {
- while (1)
- {
- if (at_recv_readline(client) > 0)
- {
- if (client->urc != RT_NULL)
- {
- /* current receive is request, try to execute related operations */
- if (client->urc->func != RT_NULL)
- {
- client->urc->func(client, client->recv_line_buf, client->recv_line_len);
- }
- client->urc = RT_NULL;
- }
- else if (client->resp != RT_NULL)
- {
- at_response_t resp = client->resp;
- char end_ch = client->recv_line_buf[client->recv_line_len - 1];
- /* current receive is response */
- client->recv_line_buf[client->recv_line_len - 1] = '\0';
- if (resp->buf_len + client->recv_line_len < resp->buf_size)
- {
- /* copy response lines, separated by '\0' */
- rt_memcpy(resp->buf + resp->buf_len, client->recv_line_buf, client->recv_line_len);
- /* update the current response information */
- resp->buf_len += client->recv_line_len;
- resp->line_counts++;
- }
- else
- {
- client->resp_status = AT_RESP_BUFF_FULL;
- LOG_E("Read response buffer failed. The Response buffer size is out of buffer size(%d)!", resp->buf_size);
- }
- /* check response result */
- if ((client->end_sign != 0) && (end_ch == client->end_sign) && (resp->line_num == 0))
- {
- /* get the end sign, return response state END_OK.*/
- client->resp_status = AT_RESP_OK;
- }
- else if (rt_memcmp(client->recv_line_buf, AT_RESP_END_OK, rt_strlen(AT_RESP_END_OK)) == 0
- && resp->line_num == 0)
- {
- /* get the end data by response result, return response state END_OK. */
- client->resp_status = AT_RESP_OK;
- }
- else if (rt_strstr(client->recv_line_buf, AT_RESP_END_ERROR)
- || (rt_memcmp(client->recv_line_buf, AT_RESP_END_FAIL, rt_strlen(AT_RESP_END_FAIL)) == 0))
- {
- client->resp_status = AT_RESP_ERROR;
- }
- else if (resp->line_counts == resp->line_num && resp->line_num)
- {
- /* get the end data by response line, return response state END_OK.*/
- client->resp_status = AT_RESP_OK;
- }
- else
- {
- continue;
- }
- client->resp = RT_NULL;
- rt_event_send(&client->event, at_client_resp_notice_event);
- }
- else
- {
- LOG_D("unrecognized line: %.*s", client->recv_line_len, client->recv_line_buf);
- }
- }
- }
- }
- static rt_err_t at_client_rx_ind(rt_device_t dev, rt_size_t size)
- {
- rt_slist_t *node;
- at_client_t client;
- if (size <= 0)
- {
- return RT_EOK;
- }
- rt_base_t level = rt_hw_interrupt_disable();
- rt_slist_for_each(node, &g_at_client_list)
- {
- client = rt_slist_entry(node, struct at_client, list);
- if (client->device == dev)
- {
- rt_event_send(&client->event, at_client_rx_notice_event);
- break;
- }
- }
- rt_hw_interrupt_enable(level);
- return RT_EOK;
- }
- /* initialize the client object parameters */
- static int at_client_para_init(at_client_t client)
- {
- #define AT_CLIENT_LOCK_NAME "at_c"
- #define AT_CLIENT_EVENT_NAME "at_ce"
- #define AT_CLIENT_THREAD_NAME "at_clnt"
- int result = RT_EOK;
- char name[RT_NAME_MAX];
- rt_base_t level = rt_hw_interrupt_disable();
- unsigned int at_client_num = rt_slist_len(&g_at_client_list);
- rt_hw_interrupt_enable(level);
- rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_THREAD_NAME, at_client_num);
- client->parser = rt_thread_create(name,
- (void (*)(void *parameter))client_parser,
- client,
- 1024 + 512,
- RT_THREAD_PRIORITY_MAX / 3 - 1,
- 5);
- if (client->parser == RT_NULL)
- {
- result = -RT_ENOMEM;
- goto __exit;
- }
- rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_LOCK_NAME, at_client_num);
- rt_mutex_init(&client->lock, name, RT_IPC_FLAG_PRIO);
- rt_snprintf(name, RT_NAME_MAX, "%s%d", AT_CLIENT_EVENT_NAME, at_client_num);
- rt_event_init(&client->event, name, RT_IPC_FLAG_FIFO);
- __exit:
- return result;
- }
- /**
- * AT client initialize.
- *
- * @param dev_name AT client device name
- * @param recv_bufsz the maximum number of receive buffer length
- * @param send_bufsz the maximum number of send command length
- *
- * @return 0 : initialize success
- * -1 : initialize failed
- * -5 : no memory
- */
- int at_client_init(const char *dev_name, rt_size_t recv_bufsz, rt_size_t send_bufsz)
- {
- int result = RT_EOK;
- rt_err_t open_result = RT_EOK;
- at_client_t client = RT_NULL;
- RT_ASSERT(dev_name);
- RT_ASSERT(recv_bufsz > 0);
- RT_ASSERT(send_bufsz > 0);
- if (at_client_get(dev_name) != RT_NULL)
- {
- return result;
- }
- client = rt_malloc(sizeof(struct at_client) + recv_bufsz + send_bufsz);
- if (client == RT_NULL)
- {
- result = -RT_ENOMEM;
- goto __exit;
- }
- rt_memset(client, 0, sizeof(struct at_client) + recv_bufsz + send_bufsz);
- client->status = AT_STATUS_UNINITIALIZED;
- client->recv_bufsz = recv_bufsz;
- client->recv_line_buf = ((char *)client) + sizeof(struct at_client);
- client->send_bufsz = send_bufsz;
- client->send_buf = ((char *)client) + sizeof(struct at_client) + client->recv_bufsz;
- result = at_client_para_init(client);
- if (result != RT_EOK)
- {
- goto __exit;
- }
- /* find and open command device */
- client->device = rt_device_find(dev_name);
- if (client->device)
- {
- RT_ASSERT(client->device->type == RT_Device_Class_Char);
- #ifndef RT_USING_SERIAL_V2
- /* using DMA mode first */
- open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_DMA_RX);
- /* using interrupt mode when DMA mode not supported */
- if (open_result == -RT_EIO)
- {
- open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
- }
- RT_ASSERT(open_result == RT_EOK);
- #else
- open_result = rt_device_open(client->device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
- RT_ASSERT(open_result == RT_EOK);
- rt_int32_t rx_timeout = RT_WAITING_NO;
- rt_device_control(client->device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timeout);
- #endif
- }
- else
- {
- LOG_E("AT client initialize failed! Not find the device(%s).", dev_name);
- result = -RT_ERROR;
- }
- __exit:
- if (result == RT_EOK)
- {
- rt_slist_init(&client->list);
- rt_base_t level = rt_hw_interrupt_disable();
- rt_slist_append(&g_at_client_list, &client->list);
- rt_hw_interrupt_enable(level);
- rt_device_set_rx_indicate(client->device, at_client_rx_ind);
- client->status = AT_STATUS_INITIALIZED;
- rt_thread_startup(client->parser);
- LOG_I("AT client(V%s) on device %s initialize success.", AT_SW_VERSION, dev_name);
- }
- else
- {
- if (RT_NULL != client->parser)
- {
- rt_thread_delete(client->parser);
- }
- if (RT_NULL != client)
- {
- rt_free(client);
- }
- LOG_E("AT client(V%s) on device %s initialize failed(%d).", AT_SW_VERSION, dev_name, result);
- }
- return result;
- }
- int at_client_deInit(const char *dev_name)
- {
- int result = RT_EOK;
- at_client_t client = RT_NULL;
- RT_ASSERT(dev_name);
- client = at_client_get(dev_name);
- if (client == RT_NULL)
- {
- return RT_EOK;
- }
- rt_base_t level = rt_hw_interrupt_disable();
- rt_slist_remove(&g_at_client_list, &client->list);
- rt_hw_interrupt_enable(level);
- rt_event_send(&client->event, at_client_deInit_event);
- rt_event_recv(&client->event, at_client_deInit_over_event, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, NULL);
- rt_event_detach(&client->event);
- rt_mutex_detach(&client->lock);
- result = rt_device_close(client->device);
- rt_free(client);
- return result;
- }
- #endif /* AT_USING_CLIENT */
|