at.h 8.8 KB

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