at_client.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Tencent is pleased to support the open source community by making IoT Hub
  3. available.
  4. * Copyright (C) 2018-2020 THL A29 Limited, a Tencent company. All rights
  5. reserved.
  6. * Licensed under the MIT License (the "License"); you may not use this file
  7. except in
  8. * compliance with the License. You may obtain a copy of the License at
  9. * http://opensource.org/licenses/MIT
  10. * Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is
  12. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND,
  14. * either express or implied. See the License for the specific language
  15. governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. #ifndef __AT_CLIENT_H__
  20. #define __AT_CLIENT_H__
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include "config.h"
  25. #include "stddef.h"
  26. #include "utils_ringbuff.h"
  27. #define AT_FRAME_VERSION "1.0.0"
  28. #define AT_CMD_NAME_LEN 16
  29. #define AT_END_MARK_LEN 4
  30. #define CLINET_BUFF_LEN (1024)
  31. #define RING_BUFF_LEN CLINET_BUFF_LEN // uart ring buffer len
  32. #define GET_CHAR_TIMEOUT_MS (5000)
  33. #define CMD_RESPONSE_INTERVAL_MS (100)
  34. typedef void (*ParserFunc)(void *userContex);
  35. typedef enum {
  36. AT_STATUS_UNINITIALIZED = 0,
  37. AT_STATUS_INITIALIZED = 0x55,
  38. AT_STATUS_BUSY = 0xaa,
  39. } at_status;
  40. enum at_resp_status {
  41. AT_RESP_OK = 0, /* AT response end is OK */
  42. AT_RESP_ERROR = -1, /* AT response end is ERROR */
  43. AT_RESP_TIMEOUT = -2, /* AT response is timeout */
  44. AT_RESP_BUFF_FULL = -3, /* AT response buffer is full */
  45. };
  46. typedef enum at_resp_status at_resp_status_t;
  47. typedef struct _at_response_ {
  48. /* response buffer */
  49. char *buf;
  50. /* the maximum response buffer size */
  51. int buf_size;
  52. /* the number of setting response lines
  53. * == 0: the response data will auto return when received 'OK' or 'ERROR'
  54. * != 0: the response data will return when received setting lines number data
  55. */
  56. int line_num;
  57. /* the count of received response lines */
  58. int line_counts;
  59. /* the maximum response time */
  60. uint32_t timeout;
  61. } at_response;
  62. typedef at_response *at_response_t;
  63. /* URC(Unsolicited Result Code) object, such as: 'RING', 'READY' request by AT
  64. * server */
  65. typedef struct _at_urc_ {
  66. const char *cmd_prefix;
  67. const char *cmd_suffix;
  68. void (*func)(const char *data, size_t size);
  69. } at_urc;
  70. typedef at_urc *at_urc_t;
  71. typedef struct _at_client_ {
  72. at_status status;
  73. char end_sign;
  74. ring_buff_t pRingBuff;
  75. char * recv_buffer;
  76. uint32_t recv_bufsz;
  77. uint32_t cur_recv_len;
  78. void * lock; // pre cmd take the lock wait for resp , another cmd need wait for
  79. // unlock
  80. at_response_t resp;
  81. at_resp_status_t resp_status;
  82. const at_urc *urc_table;
  83. uint16_t urc_table_size;
  84. #ifdef AT_OS_USED
  85. void * resp_sem; // resp received, send sem to notic ack wait
  86. ParserFunc parser; // RX parser
  87. #else
  88. // bool resp_notice;
  89. #endif
  90. } at_client;
  91. typedef at_client *at_client_t;
  92. /* AT client initialize and start*/
  93. int at_client_init(at_client_t *pClient);
  94. /* AT client deinitial*/
  95. int at_client_deinit(at_client_t pClient);
  96. /* get AT client handle*/
  97. at_client_t at_client_get(void);
  98. /*AT connect detect*/
  99. int at_client_wait_connect(uint32_t timeout);
  100. /*wrapper for os and nonos delay*/
  101. void at_delayms(uint32_t delayms);
  102. void at_setFlag(uint32_t flag);
  103. void at_clearFlag(uint32_t flag);
  104. uint32_t at_getFlag(void);
  105. bool at_waitFlag(uint32_t flag, uint32_t timeout);
  106. /* ========================== multiple AT client function
  107. * ============================ */
  108. /* set AT client a line end sign */
  109. void at_set_end_sign(char ch);
  110. /* Set URC(Unsolicited Result Code) table */
  111. void at_set_urc_table(at_client_t client, const at_urc_t table, uint32_t size);
  112. /* AT client send or receive data */
  113. int at_client_send(at_client_t client, const char *buf, int size, uint32_t timeout);
  114. int at_client_obj_recv(char *buf, int size, int timeout);
  115. /* AT client send commands to AT server and waiter response */
  116. int at_obj_exec_cmd(at_response_t resp, const char *cmd_expr, ...);
  117. #define at_exec_cmd(resp, ...) at_obj_exec_cmd(resp, __VA_ARGS__)
  118. #define at_client_recv(buf, size, timeout) at_client_obj_recv(buf, size, timeout)
  119. /* AT response object create and delete */
  120. at_response_t at_create_resp(uint32_t buf_size, uint32_t line_num, uint32_t timeout);
  121. void at_delete_resp(at_response_t resp);
  122. /* AT response line buffer get and parse response buffer arguments */
  123. const char *at_resp_get_line(at_response_t resp, uint32_t resp_line);
  124. const char *at_resp_get_line_by_kw(at_response_t resp, const char *keyword);
  125. int at_resp_parse_line_args(at_response_t resp, uint32_t resp_line, const char *resp_expr, ...);
  126. int at_resp_parse_line_args_by_kw(at_response_t resp, const char *keyword, const char *resp_expr, ...);
  127. /* ========================== single AT client function
  128. * ============================ */
  129. void at_client_yeild(at_urc *expect_urc, uint32_t timeout);
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif /* __AT_H__ */