at_command.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _AT_COMMAND_H_
  13. #define _AT_COMMAND_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include <sys/queue.h>
  18. #include "atr_config.h"
  19. #include "at_param.h"
  20. #include "osi_compiler.h"
  21. #include "quec_proj_config.h"
  22. #include "quec_atc_common.h"
  23. #include "quec_at_param.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. struct atCommand;
  28. struct atCmdEngine;
  29. struct osiEvent;
  30. #define AT_CMD_RETURN(r) OSI_DO_WHILE0(r; return;)
  31. #define AT_CFW_UTI_INVALID 0xff
  32. /**
  33. * \brief AT command type
  34. *
  35. * set: AT+CMD=1
  36. * exe: AT+CMD
  37. * read: AT+CMD?
  38. * test: AT+CMD=?
  39. */
  40. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  41. typedef enum
  42. {
  43. AT_CMD_SET,
  44. AT_CMD_TEST,
  45. AT_CMD_READ,
  46. AT_CMD_EXE
  47. } atCmdType_t;
  48. #endif
  49. /**
  50. * \brief AT command constrain
  51. *
  52. * Some AT commands are valid only when certain conditions are meet.
  53. * Each required condition is defined as a constrain.
  54. *
  55. * For each AT command, zero or more contrains can be declared. Only
  56. * when all contrains are meet, \p cmd->desc->handler will be called.
  57. * The constrain check is performed inside \p atCommandCheckConstrains.
  58. */
  59. enum
  60. {
  61. AT_CONSTRAINT_NONE = 0, ///< no constrain
  62. AT_CON_CALIB_MODE = (1 << 0), ///< only in calibration mode, not in normal mode
  63. AT_CON_NOT_CALIB_MODE = (1 << 1), ///< only in normal mode, not in calibration mode
  64. AT_CON_NOT_CLAC = (1 << 2), ///< ignore in CLAC
  65. };
  66. /**
  67. * \brief AT command description
  68. */
  69. typedef struct atCmdDesc
  70. {
  71. const char *name; ///< AT command name, such as "+IPR"
  72. void (*handler)(struct atCommand *); ///< At command handler
  73. uint32_t constrains; ///< AT command constrains
  74. } atCmdDesc_t;
  75. /**
  76. * \brief function prototype to delete async context
  77. */
  78. typedef void (*atCommandAsyncCtxDestroy_t)(struct atCommand *cmd);
  79. typedef SLIST_ENTRY(atCommand) atCommandIter_t;
  80. typedef SLIST_HEAD(atCommandHead, atCommand) atCommandHead_t;
  81. /**
  82. * \brief AT command context data struct
  83. *
  84. * AT engine will parse a whole command line, and put all commands in the
  85. * command line into a list. So, all commands are located on a list.
  86. *
  87. * Each AT command is bind to an AT command engine, which receives the
  88. * AT command.
  89. *
  90. * AT command can be executed asynchronously. For example, an event will be
  91. * sent to another thread, and then return. After a response event is
  92. * received, the command will be finished. \p async_ctx is a placeholder
  93. * for asynchronous context. \p async_ctx_destroy will be called to delete
  94. * \p async_ctx by AT command engine. It is not needed to delete \p async_ctx
  95. * by command handler explicitly.
  96. */
  97. #ifdef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  98. #include "quec_at_engine.h"
  99. typedef struct ql_at_cmd atCommand_t;
  100. #define atCommandAsyncCtxFree quec_at_async_param_free
  101. #else
  102. typedef struct atCommand
  103. {
  104. atCommandIter_t iter;
  105. const atCmdDesc_t *desc;
  106. struct atCmdEngine *engine;
  107. atCmdType_t type;
  108. uint8_t param_count;
  109. atCmdParam_t *params[CONFIG_ATR_CMD_PARAM_MAX];
  110. uint16_t uti;
  111. void *async_ctx;
  112. atCommandAsyncCtxDestroy_t async_ctx_destroy;
  113. } atCommand_t;
  114. /**
  115. * \brief AT command description count
  116. *
  117. * AT command decriptions are stored in a global array. This will return
  118. * the count.
  119. *
  120. * \return AT command description count
  121. */
  122. size_t atCommandDescCount(void);
  123. /**
  124. * \brief AT command description by index
  125. *
  126. * The index is the index for internal global AT command description
  127. * array.
  128. *
  129. * \param n AT command decription index
  130. * \return
  131. * - AT command description
  132. * - NULL is \p n is invalid
  133. */
  134. const atCmdDesc_t *atCommandDescByIndex(size_t n);
  135. /**
  136. * \brief search AT command description by name
  137. *
  138. * The search is case insensitive.
  139. *
  140. * The terminating \0 is not needed in \p name, however \p length parameter
  141. * is required. It is designed to permit sub-string parameter without string
  142. * copy.
  143. *
  144. * \param name AT command name
  145. * \param length AT command name length
  146. * \return
  147. * - AT command description
  148. * - NULL is \p n is invalid
  149. */
  150. const atCmdDesc_t *atCommandSearchDesc(const char *name, size_t length);
  151. /**
  152. * \brief check AT command constrains
  153. *
  154. * It is called by AT command engine. Only when it returns true,
  155. * \p cmd->desc->handler will be called. So, when it returns false, final
  156. * response should be called inside.
  157. *
  158. * \param cmd AT command, must be valid
  159. * \return
  160. * - true if the command is permitted
  161. * - false if the command is not permitted, and final response will be
  162. * called inside.
  163. */
  164. bool atCommandCheckConstrains(atCommand_t *cmd);
  165. /**
  166. * \brief delete an AT command
  167. *
  168. * It should be called inside AT command engine only.
  169. *
  170. * When \p cmd is NULL, nothing will be done.
  171. *
  172. * \param cmd AT command, must be valid
  173. */
  174. void atCommandDestroy(atCommand_t *cmd);
  175. /**
  176. * \brief delete all AT commands in the command line
  177. *
  178. * It should be called inside AT command engine only.
  179. *
  180. * \p cmd_list itself must be a valid pointer for list head.
  181. *
  182. * \param cmd_list AT command list
  183. */
  184. void atCommandDestroyAll(atCommandHead_t *cmd_list);
  185. /**
  186. * \brief default dtor of AT command async context
  187. *
  188. * When \p cmd->asynx_ctx is a plain memory pointer, this can be set to
  189. * \p cmd->async_ctx_destroy.
  190. *
  191. * \param cmd AT command, must be valid
  192. */
  193. void atCommandAsyncCtxFree(atCommand_t *cmd);
  194. #endif
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif