at.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. */
  24. #ifndef __AT_H__
  25. #define __AT_H__
  26. #include <rtthread.h>
  27. #define AT_SW_VERSION "0.2.2"
  28. #define DBG_ENABLE
  29. #define DBG_SECTION_NAME "AT"
  30. #ifdef AT_DEBUG
  31. #define DBG_LEVEL DBG_LOG
  32. #else
  33. #define DBG_LEVEL DBG_INFO
  34. #endif /* AT_DEBUG */
  35. #define DBG_COLOR
  36. #include <rtdbg.h>
  37. #define AT_CMD_NAME_LEN 16
  38. #define AT_END_MARK_LEN 4
  39. #ifndef AT_CMD_MAX_LEN
  40. #define AT_CMD_MAX_LEN 128
  41. #endif
  42. /* client receive idle timeout, client will wait this timeout when send data, unit: ms */
  43. #ifndef AT_CLIENT_RECV_IDEL
  44. #define AT_CLIENT_RECV_IDEL 50
  45. #endif
  46. /* the server AT commands new line sign */
  47. #if defined(AT_CMD_END_MARK_CRLF)
  48. #define AT_CMD_END_MARK "\r\n"
  49. #elif defined(AT_CMD_END_MARK_CR)
  50. #define AT_CMD_END_MARK "\r"
  51. #elif defined(AT_CMD_END_MARK_LF)
  52. #define AT_CMD_END_MARK "\n"
  53. #endif
  54. #ifndef AT_SERVER_RECV_BUFF_LEN
  55. #define AT_SERVER_RECV_BUFF_LEN 256
  56. #endif
  57. #ifndef AT_CLIENT_RECV_BUFF_LEN
  58. #define AT_CLIENT_RECV_BUFF_LEN 512
  59. #endif
  60. #ifndef AT_SERVER_DEVICE
  61. #define AT_SERVER_DEVICE "uart2"
  62. #endif
  63. #ifndef AT_CLIENT_DEVICE
  64. #define AT_CLIENT_DEVICE "uart2"
  65. #endif
  66. #define AT_CMD_EXPORT(_name_, _args_expr_, _test_, _query_, _setup_, _exec_) \
  67. RT_USED static const struct at_cmd __at_cmd_##_test_##_query_##_setup_##_exec_ SECTION("RtAtCmdTab") = \
  68. { \
  69. _name_, \
  70. _args_expr_, \
  71. _test_, \
  72. _query_, \
  73. _setup_, \
  74. _exec_, \
  75. };
  76. enum at_status
  77. {
  78. AT_STATUS_UNINITIALIZED = 0,
  79. AT_STATUS_INITIALIZED,
  80. AT_STATUS_BUSY,
  81. };
  82. typedef enum at_status at_status_t;
  83. #ifdef AT_USING_SERVER
  84. enum at_result
  85. {
  86. AT_RESULT_OK = 0, /* AT result is no error */
  87. AT_RESULT_FAILE = -1, /* AT result have a generic error */
  88. AT_RESULT_NULL = -2, /* AT result not need return */
  89. AT_RESULT_CMD_ERR = -3, /* AT command format error or No way to execute */
  90. AT_RESULT_CHECK_FAILE = -4, /* AT command expression format is error */
  91. AT_RESULT_PARSE_FAILE = -5, /* AT command arguments parse is error */
  92. };
  93. typedef enum at_result at_result_t;
  94. struct at_cmd
  95. {
  96. char name[AT_CMD_NAME_LEN];
  97. char *args_expr;
  98. at_result_t (*test)(void);
  99. at_result_t (*query)(void);
  100. at_result_t (*setup)(const char *args);
  101. at_result_t (*exec)(void);
  102. };
  103. typedef struct at_cmd *at_cmd_t;
  104. struct at_server
  105. {
  106. rt_device_t device;
  107. at_status_t status;
  108. char (*get_char)(void);
  109. rt_bool_t echo_mode;
  110. char recv_buffer[AT_SERVER_RECV_BUFF_LEN];
  111. rt_size_t cur_recv_len;
  112. rt_sem_t rx_notice;
  113. char end_mark[AT_END_MARK_LEN];
  114. rt_thread_t parser;
  115. void (*parser_entry)(struct at_server *server);
  116. };
  117. typedef struct at_server *at_server_t;
  118. #endif /* AT_USING_SERVER */
  119. #ifdef AT_USING_CLIENT
  120. enum at_resp_status
  121. {
  122. AT_RESP_OK = 0, /* AT response end is OK */
  123. AT_RESP_ERROR = -1, /* AT response end is ERROR */
  124. AT_RESP_TIMEOUT = -2, /* AT response is timeout */
  125. AT_RESP_BUFF_FULL= -3, /* AT response buffer is full */
  126. };
  127. typedef enum at_resp_status at_resp_status_t;
  128. struct at_response
  129. {
  130. /* response buffer */
  131. char *buf;
  132. /* the maximum response buffer size */
  133. rt_size_t buf_size;
  134. /* the number of setting response lines
  135. * == 0: the response data will auto return when received 'OK' or 'ERROR'
  136. * != 0: the response data will return when received setting lines number data */
  137. rt_size_t line_num;
  138. /* the count of received response lines */
  139. rt_size_t line_counts;
  140. /* the maximum response time */
  141. rt_int32_t timeout;
  142. };
  143. typedef struct at_response *at_response_t;
  144. /* URC(Unsolicited Result Code) object, such as: 'RING', 'READY' request by AT server */
  145. struct at_urc
  146. {
  147. const char *cmd_prefix;
  148. const char *cmd_suffix;
  149. void (*func)(const char *data, rt_size_t size);
  150. };
  151. typedef struct at_urc *at_urc_t;
  152. struct at_client
  153. {
  154. rt_device_t device;
  155. at_status_t status;
  156. char recv_buffer[AT_CLIENT_RECV_BUFF_LEN];
  157. rt_size_t cur_recv_len;
  158. rt_sem_t rx_notice;
  159. rt_mutex_t lock;
  160. at_response_t resp;
  161. rt_sem_t resp_notice;
  162. at_resp_status_t resp_status;
  163. const struct at_urc *urc_table;
  164. rt_size_t urc_table_size;
  165. rt_thread_t parser;
  166. };
  167. typedef struct at_client *at_client_t;
  168. #endif /* AT_USING_CLIENT */
  169. #ifdef AT_USING_SERVER
  170. /* AT server initialize and start */
  171. int at_server_init(void);
  172. /* AT server send command execute result to AT device */
  173. void at_server_printf(const char *format, ...);
  174. void at_server_printfln(const char *format, ...);
  175. void at_server_print_result(at_result_t result);
  176. /* AT server request arguments parse */
  177. int at_req_parse_args(const char *req_args, const char *req_expr, ...);
  178. #endif /* AT_USING_SERVER */
  179. #ifdef AT_USING_CLIENT
  180. /* AT client initialize and start */
  181. int at_client_init(void);
  182. /* AT client send commands to AT server and waiter response */
  183. int at_exec_cmd(at_response_t resp, const char *cmd_expr, ...);
  184. /* AT Client send or receive data */
  185. rt_size_t at_client_send(const char *buf, rt_size_t size);
  186. rt_size_t at_client_recv(char *buf, rt_size_t size);
  187. /* AT response structure create and delete */
  188. at_response_t at_create_resp(rt_size_t buf_size, rt_size_t line_num, rt_int32_t timeout);
  189. void at_delete_resp(at_response_t resp);
  190. 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);
  191. /* AT response line buffer get and parse response buffer arguments */
  192. const char *at_resp_get_line(at_response_t resp, rt_size_t resp_line);
  193. const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword);
  194. int at_resp_parse_line_args(at_response_t resp, rt_size_t resp_line, const char *resp_expr, ...);
  195. int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...);
  196. /* Set URC(Unsolicited Result Code) table */
  197. void at_set_urc_table(const struct at_urc * table, rt_size_t size);
  198. #endif /* AT_USING_CLIENT */
  199. /* ========================== User port function ============================ */
  200. #ifdef AT_USING_SERVER
  201. /* AT server device reset */
  202. void at_port_reset(void);
  203. /* AT server device factory reset */
  204. void at_port_factory_reset(void);
  205. #endif
  206. #ifdef AT_USING_CLIENT
  207. /* AT client port initialization */
  208. int at_client_port_init(void);
  209. #endif
  210. #endif /* __AT_H__ */