at.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * File : at.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-03-30 chenyong first version
  23. * 2018-08-17 chenyong multiple client support
  24. */
  25. #ifndef __AT_H__
  26. #define __AT_H__
  27. #include <rtthread.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #define AT_SW_VERSION "1.0.1"
  32. #define AT_SW_VERSION_NUM 0x10000
  33. #define DBG_ENABLE
  34. #define DBG_SECTION_NAME "AT"
  35. #ifdef AT_DEBUG
  36. #define DBG_LEVEL DBG_LOG
  37. #else
  38. #define DBG_LEVEL DBG_INFO
  39. #endif /* AT_DEBUG */
  40. #define DBG_COLOR
  41. #include <rtdbg.h>
  42. #define AT_CMD_NAME_LEN 16
  43. #define AT_END_MARK_LEN 4
  44. #ifndef AT_CMD_MAX_LEN
  45. #define AT_CMD_MAX_LEN 128
  46. #endif
  47. /* client receive idle timeout, client will wait this timeout when send data, unit: ms */
  48. #ifndef AT_CLIENT_RECV_IDEL
  49. #define AT_CLIENT_RECV_IDEL 50
  50. #endif
  51. /* the server AT commands new line sign */
  52. #if defined(AT_CMD_END_MARK_CRLF)
  53. #define AT_CMD_END_MARK "\r\n"
  54. #elif defined(AT_CMD_END_MARK_CR)
  55. #define AT_CMD_END_MARK "\r"
  56. #elif defined(AT_CMD_END_MARK_LF)
  57. #define AT_CMD_END_MARK "\n"
  58. #endif
  59. #ifndef AT_SERVER_RECV_BUFF_LEN
  60. #define AT_SERVER_RECV_BUFF_LEN 256
  61. #endif
  62. #ifndef AT_SERVER_DEVICE
  63. #define AT_SERVER_DEVICE "uart2"
  64. #endif
  65. /* the maximum number of supported AT clients */
  66. #ifndef AT_CLIENT_NUM_MAX
  67. #define AT_CLIENT_NUM_MAX 1
  68. #endif
  69. #define AT_CMD_EXPORT(_name_, _args_expr_, _test_, _query_, _setup_, _exec_) \
  70. RT_USED static const struct at_cmd __at_cmd_##_test_##_query_##_setup_##_exec_ SECTION("RtAtCmdTab") = \
  71. { \
  72. _name_, \
  73. _args_expr_, \
  74. _test_, \
  75. _query_, \
  76. _setup_, \
  77. _exec_, \
  78. };
  79. enum at_status
  80. {
  81. AT_STATUS_UNINITIALIZED = 0,
  82. AT_STATUS_INITIALIZED,
  83. AT_STATUS_BUSY,
  84. };
  85. typedef enum at_status at_status_t;
  86. #ifdef AT_USING_SERVER
  87. enum at_result
  88. {
  89. AT_RESULT_OK = 0, /* AT result is no error */
  90. AT_RESULT_FAILE = -1, /* AT result have a generic error */
  91. AT_RESULT_NULL = -2, /* AT result not need return */
  92. AT_RESULT_CMD_ERR = -3, /* AT command format error or No way to execute */
  93. AT_RESULT_CHECK_FAILE = -4, /* AT command expression format is error */
  94. AT_RESULT_PARSE_FAILE = -5, /* AT command arguments parse is error */
  95. };
  96. typedef enum at_result at_result_t;
  97. struct at_cmd
  98. {
  99. char name[AT_CMD_NAME_LEN];
  100. char *args_expr;
  101. at_result_t (*test)(void);
  102. at_result_t (*query)(void);
  103. at_result_t (*setup)(const char *args);
  104. at_result_t (*exec)(void);
  105. };
  106. typedef struct at_cmd *at_cmd_t;
  107. struct at_server
  108. {
  109. rt_device_t device;
  110. at_status_t status;
  111. char (*get_char)(void);
  112. rt_bool_t echo_mode;
  113. char recv_buffer[AT_SERVER_RECV_BUFF_LEN];
  114. rt_size_t cur_recv_len;
  115. rt_sem_t rx_notice;
  116. char end_mark[AT_END_MARK_LEN];
  117. rt_thread_t parser;
  118. void (*parser_entry)(struct at_server *server);
  119. };
  120. typedef struct at_server *at_server_t;
  121. #endif /* AT_USING_SERVER */
  122. #ifdef AT_USING_CLIENT
  123. enum at_resp_status
  124. {
  125. AT_RESP_OK = 0, /* AT response end is OK */
  126. AT_RESP_ERROR = -1, /* AT response end is ERROR */
  127. AT_RESP_TIMEOUT = -2, /* AT response is timeout */
  128. AT_RESP_BUFF_FULL= -3, /* AT response buffer is full */
  129. };
  130. typedef enum at_resp_status at_resp_status_t;
  131. struct at_response
  132. {
  133. /* response buffer */
  134. char *buf;
  135. /* the maximum response buffer size */
  136. rt_size_t buf_size;
  137. /* the number of setting response lines
  138. * == 0: the response data will auto return when received 'OK' or 'ERROR'
  139. * != 0: the response data will return when received setting lines number data */
  140. rt_size_t line_num;
  141. /* the count of received response lines */
  142. rt_size_t line_counts;
  143. /* the maximum response time */
  144. rt_int32_t timeout;
  145. };
  146. typedef struct at_response *at_response_t;
  147. /* URC(Unsolicited Result Code) object, such as: 'RING', 'READY' request by AT server */
  148. struct at_urc
  149. {
  150. const char *cmd_prefix;
  151. const char *cmd_suffix;
  152. void (*func)(const char *data, rt_size_t size);
  153. };
  154. typedef struct at_urc *at_urc_t;
  155. struct at_client
  156. {
  157. rt_device_t device;
  158. at_status_t status;
  159. char end_sign;
  160. char *recv_buffer;
  161. rt_size_t recv_bufsz;
  162. rt_size_t cur_recv_len;
  163. rt_sem_t rx_notice;
  164. rt_mutex_t lock;
  165. at_response_t resp;
  166. rt_sem_t resp_notice;
  167. at_resp_status_t resp_status;
  168. const struct at_urc *urc_table;
  169. rt_size_t urc_table_size;
  170. rt_thread_t parser;
  171. };
  172. typedef struct at_client *at_client_t;
  173. #endif /* AT_USING_CLIENT */
  174. #ifdef AT_USING_SERVER
  175. /* AT server initialize and start */
  176. int at_server_init(void);
  177. /* AT server send command execute result to AT device */
  178. void at_server_printf(const char *format, ...);
  179. void at_server_printfln(const char *format, ...);
  180. void at_server_print_result(at_result_t result);
  181. /* AT server request arguments parse */
  182. int at_req_parse_args(const char *req_args, const char *req_expr, ...);
  183. #endif /* AT_USING_SERVER */
  184. #ifdef AT_USING_CLIENT
  185. /* AT client initialize and start*/
  186. int at_client_init(const char *dev_name, rt_size_t recv_bufsz);
  187. /* ========================== multiple AT client function ============================ */
  188. /* get AT client object */
  189. at_client_t at_client_get(const char *dev_name);
  190. at_client_t at_client_get_first(void);
  191. /* AT client wait for connection to external devices. */
  192. int at_client_obj_wait_connect(at_client_t client, rt_uint32_t timeout);
  193. /* AT client send or receive data */
  194. rt_size_t at_client_obj_send(at_client_t client, const char *buf, rt_size_t size);
  195. rt_size_t at_client_obj_recv(at_client_t client, char *buf, rt_size_t size);
  196. /* set AT client a line end sign */
  197. void at_obj_set_end_sign(at_client_t client, char ch);
  198. /* Set URC(Unsolicited Result Code) table */
  199. void at_obj_set_urc_table(at_client_t client, const struct at_urc * table, rt_size_t size);
  200. /* AT client send commands to AT server and waiter response */
  201. int at_obj_exec_cmd(at_client_t client, at_response_t resp, const char *cmd_expr, ...);
  202. /* AT response object create and delete */
  203. at_response_t at_create_resp(rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout);
  204. void at_delete_resp(at_response_t resp);
  205. 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);
  206. /* AT response line buffer get and parse response buffer arguments */
  207. const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line);
  208. const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword);
  209. int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...);
  210. int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...);
  211. /* ========================== single AT client function ============================ */
  212. /**
  213. * NOTE: These functions can be used directly when there is only one AT client.
  214. * If there are multiple AT Client in the program, these functions can operate on the first initialized AT client.
  215. */
  216. #define at_exec_cmd(resp, ...) at_obj_exec_cmd(at_client_get_first(), resp, __VA_ARGS__)
  217. #define at_client_wait_connect(timeout) at_client_obj_wait_connect(at_client_get_first(), timeout)
  218. #define at_client_send(buf, size) at_client_obj_send(at_client_get_first(), buf, size)
  219. #define at_client_recv(buf, size) at_client_obj_recv(at_client_get_first(), buf, size)
  220. #define at_set_end_sign(ch) at_obj_set_end_sign(at_client_get_first(), ch)
  221. #define at_set_urc_table(urc_table, table_sz) at_obj_set_urc_table(at_client_get_first(), urc_table, table_sz)
  222. #endif /* AT_USING_CLIENT */
  223. /* ========================== User port function ============================ */
  224. #ifdef AT_USING_SERVER
  225. /* AT server device reset */
  226. void at_port_reset(void);
  227. /* AT server device factory reset */
  228. void at_port_factory_reset(void);
  229. #endif
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* __AT_H__ */