quec_sio_adapter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #ifndef _QUEC_SIO_ADAPTER_H__
  2. #define _QUEC_SIO_ADAPTER_H__
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stddef.h>
  6. #include "osi_api.h"
  7. #include "osi_event_hub.h"
  8. #include "osi_generic_list.h"
  9. #include "osi_pipe.h"
  10. #include "quec_proj_config.h"
  11. #include "drv_uart.h"
  12. #include "drv_serial.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /**
  17. * @brief forward declaration of uart driver
  18. */
  19. struct drvUart;
  20. /**
  21. * @brief opaque data structure of AT engine
  22. */
  23. typedef struct atEngine atEngine_t;
  24. /**
  25. * @brief data structure of AT device
  26. */
  27. typedef struct atDevice atDevice_t;
  28. typedef enum atDeviceFormat
  29. {
  30. AT_DEVICE_FORMAT_AUTO_DETECT, ///< auto detect
  31. AT_DEVICE_FORMAT_8N2, ///< 8 Data; 2 Stop
  32. AT_DEVICE_FORMAT_811, ///< 8 Data; 1 Parity; 1 Stop
  33. AT_DEVICE_FORMAT_8N1, ///< 8 Data; 1 Stop
  34. AT_DEVICE_FORMAT_7N2, ///< 7 Data; 2 Stop
  35. AT_DEVICE_FORMAT_711, ///< 7 Data; 1 Parity; 1 Stop
  36. AT_DEVICE_FORMAT_7N1 ///< 7 Data; 1 Stop
  37. } atDeviceFormat_t;
  38. /**
  39. * @ brief AT device character framing parity
  40. *
  41. * Refer to V.250 +ICF. The enum value *matches* format parameter in +ICF
  42. * command.
  43. */
  44. typedef enum atDeviceParity
  45. {
  46. AT_DEVICE_PARITY_ODD, ///< Odd
  47. AT_DEVICE_PARITY_EVEN, ///< Even
  48. AT_DEVICE_PARITY_MARK, ///< Mark
  49. AT_DEVICE_PARITY_SPACE ///< Space
  50. } atDeviceParity_t;
  51. /**
  52. * @ brief AT device receive flow control
  53. *
  54. * Refer to V.250 +IFC. The enum value *matches* format parameter in +IFC
  55. * command. Not supported options are not listed.
  56. */
  57. typedef enum atDeviceRXFC
  58. {
  59. AT_DEVICE_RXFC_NONE, ///< None
  60. AT_DEVICE_RXFC_HW = 2 ///< Circuit 133 (Ready for Receiving)
  61. } atDeviceRXFC_t;
  62. /**
  63. * @ brief AT device transfer flow control
  64. *
  65. * Refer to V.250 +IFC. The enum value *matches* format parameter in +IFC
  66. * command. Not supported options are not listed.
  67. */
  68. typedef enum atDeviceTXFC
  69. {
  70. AT_DEVICE_TXFC_NONE, ///< None
  71. AT_DEVICE_TXFC_HW = 2 ///< Circuit 106 (Clear to Send/Ready for Sending)
  72. } atDeviceTXFC_t;
  73. /**
  74. * @brief AT device attribute
  75. * (TODO!! maybe need to distinguish device type,
  76. * below all for UART like device)
  77. * @baud_max Maximum value of baudrate
  78. * @baud_min Minimum value of baudrate
  79. * @format_support Boolean type array mark whether this device support
  80. * the format refer to \p atDeviceFormat or not.
  81. * @parity_support Boolean type array mark whether this device support
  82. * the parity refer to \p atDeviceParity or not.
  83. */
  84. typedef struct
  85. {
  86. uint32_t baud_max;
  87. uint32_t baud_min;
  88. bool format_support[7];
  89. bool parity_support[4];
  90. } atDeviceAttr_t;
  91. /**
  92. * @brief AT device data structure
  93. */
  94. struct atDevice
  95. {
  96. /** delete function */
  97. void (*destroy)(atDevice_t *th);
  98. /** open function */
  99. bool (*open)(atDevice_t *th);
  100. /** close function */
  101. void (*close)(atDevice_t *th);
  102. /** write function */
  103. int (*write)(atDevice_t *th, const void *data, size_t size);
  104. /** read function */
  105. int (*read)(atDevice_t *th, void *data, size_t size);
  106. /** get read available bytes function */
  107. int (*read_avail)(atDevice_t *th);
  108. /** get write available space function */
  109. int (*write_avail)(atDevice_t *th);
  110. /** set baudrate function */
  111. void (*set_baud)(atDevice_t *th, size_t baud);
  112. /** set format function */
  113. void (*set_format)(atDevice_t *th, atDeviceFormat_t format, atDeviceParity_t parity);
  114. /** set flow control */
  115. bool (*set_flow_ctrl)(atDevice_t *th, atDeviceRXFC_t rxfc, atDeviceTXFC_t txfc);
  116. /** set auto sleep timeout */
  117. void (*set_auto_sleep)(atDevice_t *th, int timeout);
  118. /** check if the device is ready*/
  119. bool isReady;
  120. /** get device attributes */
  121. const atDeviceAttr_t *(*get_attribute)(atDevice_t *th);
  122. /** the dispatch */
  123. void *recv;
  124. };
  125. /**
  126. * @brief CMUX configuration
  127. */
  128. typedef struct
  129. {
  130. uint8_t transparency; ///< 0: basic, 1: advanced
  131. uint8_t subset; ///< 0: UIH, 1: UI, 2: I
  132. uint8_t port_speed; ///< transmission rate
  133. int max_frame_size; ///< maximum frame size
  134. uint8_t ack_timer; ///< acknowledgement timer in units of ten milliseconds
  135. uint8_t max_retrans_count; ///< maximum number of re-transmissions
  136. uint8_t resp_timer; ///< response timer for the multiplexer control channel in units of ten milliseconds
  137. uint8_t wakeup_resp_timer; ///< wake up response timer in seconds
  138. uint8_t window_size; ///< window size, for Advanced option with Error-Recovery Mode
  139. } atCmuxConfig_t;
  140. /**
  141. * @brief enum type of command mode engine prompt finish mode
  142. *
  143. * In prompt mode, BACKSPACE will be chcked. When BACKSPACE is input
  144. * in the middle, the previous character will be removed from the
  145. * buffer.
  146. */
  147. typedef enum atCmdPromptEndMode
  148. {
  149. AT_PROMPT_END_CTRL_Z, ///< ended with CTRL-Z
  150. AT_PROMPT_END_ESC, ///< ended with ESCAPE
  151. AT_PROMPT_END_OVERFLOW ///< provided buffer overflow
  152. } atCmdPromptEndMode_t;
  153. /**
  154. * @brief UART AT device configuration
  155. */
  156. typedef struct
  157. {
  158. uint32_t name; ///< uart name, such as DRV_NAME_UART1
  159. size_t baud; ///< baud rate
  160. atDeviceFormat_t format; ///< character framing format
  161. atDeviceParity_t parity; ///< character framing parity
  162. bool rts_enable; ///< hw flow control, rts enable
  163. bool cts_enable; ///< hw flow control, cts enable
  164. } atDeviceUartConfig_t;
  165. /**
  166. * @brief virtual AT device configuration
  167. */
  168. typedef struct
  169. {
  170. uint32_t name; ///< device name, only used in trace
  171. osiPipe_t *rx_pipe; ///< AT RX pipe
  172. osiPipe_t *tx_pipe; ///< AT TX pipe
  173. } atDeviceVirtConfig_t;
  174. #ifdef CONFIG_QUEC_PROJECT_FEATURE_UART
  175. typedef struct
  176. {
  177. atDevice_t ops; // API
  178. drvUart_t *drv;
  179. atDeviceUartConfig_t config; // UART configuration
  180. osiWork_t *work;
  181. uint32_t pending_event;
  182. atDeviceAttr_t attr;
  183. } atDeviceUart_t;
  184. typedef struct
  185. {
  186. atDevice_t ops; // API
  187. uint32_t name;
  188. drvSerial_t *serial;
  189. } atDeviceUsrl_t;
  190. #endif
  191. atDevice_t *atDeviceUartCreate(atDeviceUartConfig_t *cfg);
  192. /**
  193. * @brief create UART AT device
  194. *
  195. * Comparing to \p atDeviceUartCreate, the parameter is an already created
  196. * UART device.
  197. *
  198. * @param drv UART AT device driver, must be valid
  199. * @return
  200. * - UART AT device pointer
  201. * - NULL if out of memory, or invalid parmameter
  202. */
  203. atDevice_t *atDeviceUartCreateWithDevice(struct drvUart *drv);
  204. /**
  205. * @brief create serial AT device
  206. *
  207. * This used to create USB CDC/ACM AT device.
  208. *
  209. * @param name serial device name, such as DRV_NAME_USRL_COM0
  210. * @return
  211. * - serial AT device pointer
  212. * - NULL if out of memory
  213. */
  214. atDevice_t *atDeviceUserialCreate(uint32_t name, uint rx_size, uint tx_size);
  215. /**
  216. * @brief create diag at device
  217. *
  218. * This used to create diag AT device
  219. *
  220. * @return
  221. * - NULL fail
  222. * - other the at device
  223. */
  224. atDevice_t *atDeviceDiagCreate();
  225. /**
  226. * @brief create virtual AT device
  227. *
  228. * @param cfg virtual AT device configuration, must be valid
  229. * @return
  230. * - AT device pointer
  231. * - NULL if out of memory
  232. */
  233. atDevice_t *atDeviceVirtCreate(const atDeviceVirtConfig_t *cfg);
  234. /**
  235. * @brief delete the AT device
  236. *
  237. * When \p th is NULL, nothing will be done.
  238. *
  239. * @param th AT device to be deleted
  240. */
  241. void atDeviceDelete(atDevice_t *th);
  242. /**
  243. * @brief open the AT device for read and write
  244. *
  245. * @param th AT device, must be valid
  246. * @return
  247. * - true on success
  248. */
  249. bool atDeviceOpen(atDevice_t *th);
  250. /**
  251. * @brief close the AT device
  252. *
  253. * @param th AT device, must be valid
  254. */
  255. void atDeviceClose(atDevice_t *th);
  256. /**
  257. * @brief write data to AT device
  258. *
  259. * AT device will try to write all data. When the output buffer is
  260. * full, it will wait.
  261. *
  262. * Usually, AT device will define a *reasonable* timeout. At timeout,
  263. * the written size may be less than specified size.
  264. *
  265. * When \p size is 0, nothing will be done.
  266. *
  267. * @param th AT device, must be valid
  268. * @param data data pointer to be written, must be valid if \p size
  269. * is not zero
  270. * @param size data size
  271. * @return
  272. * - written byte count
  273. * - -1 if parameter is invalid, or device error
  274. */
  275. int atDeviceWrite(atDevice_t *th, const void *data, size_t size);
  276. /**
  277. * @brief read data from AT device
  278. *
  279. * It will just read from the device receive buffer. When the buffer
  280. * is empty, return 0. Even the receive buffer is not empty, the return
  281. * size may be less than the specified size.
  282. *
  283. * When \p size is 0, nothing will be done.
  284. *
  285. * @param th AT device, must be valid
  286. * @param data mempry pointer for read, must be valid if \p size
  287. * is not zero
  288. * @param size memory size
  289. * @return
  290. * - read byte count
  291. * - -1 if parameter is invalid, or device error
  292. */
  293. int atDeviceRead(atDevice_t *th, void *data, size_t size);
  294. /**
  295. * @brief AT device read available bytes
  296. *
  297. * @param th AT device, must be valid
  298. * @return
  299. * - (-1) Parameter error
  300. * - OTHERS (>=0) Available size in byte
  301. */
  302. int atDeviceReadAvail(atDevice_t *th);
  303. /**
  304. * @brief AT device write available space
  305. *
  306. * @param th AT device, must be valid
  307. * @return
  308. * - (-1) Parameter error
  309. * - OTHERS (>=0) Available size in byte
  310. */
  311. int atDeviceWriteAvail(atDevice_t *th);
  312. /**
  313. * @brief set AT device baudrate
  314. *
  315. * @param th AT device, must be valid
  316. * @param baud baud rate
  317. */
  318. void atDeviceSetBaudrate(atDevice_t *th, size_t baud);
  319. /**
  320. * @brief set AT device charactor format
  321. *
  322. * @param th AT device, must be valid
  323. * @param format refer to \p atDeviceFormat_t
  324. * @param parity refer to \p atDeviceParity_t
  325. */
  326. void atDeviceSetFormat(atDevice_t *th, atDeviceFormat_t format, atDeviceParity_t parity);
  327. /**
  328. * @brief set AT device flow control
  329. *
  330. * @param th AT device, must be valid
  331. * @param rxfc flow control of RX
  332. * @param txfc flow control of TX
  333. * @return
  334. * - true on success
  335. * - false on failed
  336. */
  337. bool atDeviceSetFlowCtrl(atDevice_t *th, atDeviceRXFC_t rxfc, atDeviceTXFC_t txfc);
  338. /**
  339. * @brief set AT device auto sleep
  340. *
  341. * Refer to \p drvUartSetAutoSleep
  342. *
  343. * @param th AT device, must be valid
  344. * @param timeout auto sleep wait time after transfer done. It can be
  345. * 0 but not recommended. Negative value to disable
  346. * auto sleep feature.
  347. */
  348. void atDeviceSetAutoSleep(atDevice_t *th, int timeout);
  349. /**
  350. * @brief get AT device attribute
  351. *
  352. * @param th AT device, must be valid
  353. * @return the attribute of this device
  354. */
  355. const atDeviceAttr_t *atDeviceAttribute(atDevice_t *th);
  356. /*========================================================================
  357. * 以下API为适配展锐底层接口, 函数未实现,请勿直接调用
  358. *========================================================================*/
  359. #define atCmdDeceaseRef(th)
  360. #define atCmdIncreaseRef(th)
  361. #define atCmdClearRemains(th)
  362. #ifdef __cplusplus
  363. }
  364. #endif
  365. #endif