at.h 8.5 KB

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