at_engine.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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_ENGINE_H_
  13. #define _AT_ENGINE_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #include "osi_api.h"
  18. #include "osi_event_hub.h"
  19. #include "osi_generic_list.h"
  20. #include "osi_pipe.h"
  21. #include "atr_config.h"
  22. #include "at_cfg.h"
  23. #include "quec_proj_config.h"
  24. #include "drv_uart.h"
  25. #include "drv_serial.h"
  26. #include "quec_sio_adapter.h"
  27. #include "quec_dispatch.h"
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. //21220122 kevin.wang move it to quec_sio_adapter.h
  32. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  33. /**
  34. * @brief forward declaration of uart driver
  35. */
  36. struct drvUart;
  37. /**
  38. * @brief opaque data structure of AT engine
  39. */
  40. typedef struct atEngine atEngine_t;
  41. /**
  42. * @brief data structure of AT device
  43. */
  44. typedef struct atDevice atDevice_t;
  45. #endif
  46. /**
  47. * @brief opaque data structure of AT dispatch
  48. */
  49. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  50. typedef struct atDispatch atDispatch_t;
  51. #else
  52. typedef struct ql_dispatch atDispatch_t;
  53. #define atCmdEngine ql_dispatch
  54. #define atDataEngine ql_dispatch
  55. #endif
  56. /**
  57. * @brief opaque data structure of AT command mode engine
  58. */
  59. typedef struct atCmdEngine atCmdEngine_t;
  60. /**
  61. * @brief opaque data structure of AT data mode engine
  62. */
  63. typedef struct atDataEngine atDataEngine_t;
  64. /**
  65. * @brief opaque data structure of AT mux mode engine
  66. */
  67. typedef struct atCmuxEngine atCmuxEngine_t;
  68. /**
  69. * @brief data structure of AT command
  70. */
  71. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  72. typedef struct atCommand atCommand_t;
  73. #else
  74. #include "quec_at_param.h"
  75. typedef struct ql_at_cmd atCommand_t;
  76. #endif
  77. //21220122 kevin.wang move it to quec_sio_adapter.h
  78. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  79. /**
  80. * @ brief AT device character framing format
  81. *
  82. * Refer to V.250 +ICF. The enum value *matches* format parameter in +ICF
  83. * command.
  84. */
  85. typedef enum atDeviceFormat
  86. {
  87. AT_DEVICE_FORMAT_AUTO_DETECT, ///< auto detect
  88. AT_DEVICE_FORMAT_8N2, ///< 8 Data; 2 Stop
  89. AT_DEVICE_FORMAT_811, ///< 8 Data; 1 Parity; 1 Stop
  90. AT_DEVICE_FORMAT_8N1, ///< 8 Data; 1 Stop
  91. AT_DEVICE_FORMAT_7N2, ///< 7 Data; 2 Stop
  92. AT_DEVICE_FORMAT_711, ///< 7 Data; 1 Parity; 1 Stop
  93. AT_DEVICE_FORMAT_7N1 ///< 7 Data; 1 Stop
  94. } atDeviceFormat_t;
  95. /**
  96. * @ brief AT device character framing parity
  97. *
  98. * Refer to V.250 +ICF. The enum value *matches* format parameter in +ICF
  99. * command.
  100. */
  101. typedef enum atDeviceParity
  102. {
  103. AT_DEVICE_PARITY_ODD, ///< Odd
  104. AT_DEVICE_PARITY_EVEN, ///< Even
  105. AT_DEVICE_PARITY_MARK, ///< Mark
  106. AT_DEVICE_PARITY_SPACE ///< Space
  107. } atDeviceParity_t;
  108. /**
  109. * @ brief AT device receive flow control
  110. *
  111. * Refer to V.250 +IFC. The enum value *matches* format parameter in +IFC
  112. * command. Not supported options are not listed.
  113. */
  114. typedef enum atDeviceRXFC
  115. {
  116. AT_DEVICE_RXFC_NONE, ///< None
  117. AT_DEVICE_RXFC_HW = 2 ///< Circuit 133 (Ready for Receiving)
  118. } atDeviceRXFC_t;
  119. /**
  120. * @ brief AT device transfer flow control
  121. *
  122. * Refer to V.250 +IFC. The enum value *matches* format parameter in +IFC
  123. * command. Not supported options are not listed.
  124. */
  125. typedef enum atDeviceTXFC
  126. {
  127. AT_DEVICE_TXFC_NONE, ///< None
  128. AT_DEVICE_TXFC_HW = 2 ///< Circuit 106 (Clear to Send/Ready for Sending)
  129. } atDeviceTXFC_t;
  130. /**
  131. * @brief AT device attribute
  132. * (TODO!! maybe need to distinguish device type,
  133. * below all for UART like device)
  134. * @baud_max Maximum value of baudrate
  135. * @baud_min Minimum value of baudrate
  136. * @format_support Boolean type array mark whether this device support
  137. * the format refer to \p atDeviceFormat or not.
  138. * @parity_support Boolean type array mark whether this device support
  139. * the parity refer to \p atDeviceParity or not.
  140. */
  141. typedef struct
  142. {
  143. uint32_t baud_max;
  144. uint32_t baud_min;
  145. bool format_support[7];
  146. bool parity_support[4];
  147. } atDeviceAttr_t;
  148. /**
  149. * @brief AT device data structure
  150. */
  151. struct atDevice
  152. {
  153. /** delete function */
  154. void (*destroy)(atDevice_t *th);
  155. /** open function */
  156. bool (*open)(atDevice_t *th);
  157. /** close function */
  158. void (*close)(atDevice_t *th);
  159. /** write function */
  160. int (*write)(atDevice_t *th, const void *data, size_t size);
  161. /** read function */
  162. int (*read)(atDevice_t *th, void *data, size_t size);
  163. /** get read available bytes function */
  164. int (*read_avail)(atDevice_t *th);
  165. /** get write available space function */
  166. int (*write_avail)(atDevice_t *th);
  167. /** set baudrate function */
  168. void (*set_baud)(atDevice_t *th, size_t baud);
  169. /** set format function */
  170. void (*set_format)(atDevice_t *th, atDeviceFormat_t format, atDeviceParity_t parity);
  171. /** set flow control */
  172. bool (*set_flow_ctrl)(atDevice_t *th, atDeviceRXFC_t rxfc, atDeviceTXFC_t txfc);
  173. /** set auto sleep timeout */
  174. void (*set_auto_sleep)(atDevice_t *th, int timeout);
  175. /** check if the device is ready*/
  176. bool isReady;
  177. /** get device attributes */
  178. const atDeviceAttr_t *(*get_attribute)(atDevice_t *th);
  179. /** the dispatch */
  180. atDispatch_t *recv;
  181. };
  182. /**
  183. * @brief CMUX configuration
  184. */
  185. typedef struct
  186. {
  187. uint8_t transparency; ///< 0: basic, 1: advanced
  188. uint8_t subset; ///< 0: UIH, 1: UI, 2: I
  189. uint8_t port_speed; ///< transmission rate
  190. int max_frame_size; ///< maximum frame size
  191. uint8_t ack_timer; ///< acknowledgement timer in units of ten milliseconds
  192. uint8_t max_retrans_count; ///< maximum number of re-transmissions
  193. uint8_t resp_timer; ///< response timer for the multiplexer control channel in units of ten milliseconds
  194. uint8_t wakeup_resp_timer; ///< wake up response timer in seconds
  195. uint8_t window_size; ///< window size, for Advanced option with Error-Recovery Mode
  196. } atCmuxConfig_t;
  197. /**
  198. * @brief enum type of command mode engine prompt finish mode
  199. *
  200. * In prompt mode, BACKSPACE will be chcked. When BACKSPACE is input
  201. * in the middle, the previous character will be removed from the
  202. * buffer.
  203. */
  204. typedef enum atCmdPromptEndMode
  205. {
  206. AT_PROMPT_END_CTRL_Z, ///< ended with CTRL-Z
  207. AT_PROMPT_END_ESC, ///< ended with ESCAPE
  208. AT_PROMPT_END_OVERFLOW ///< provided buffer overflow
  209. } atCmdPromptEndMode_t;
  210. /**
  211. * @brief UART AT device configuration
  212. */
  213. typedef struct
  214. {
  215. uint32_t name; ///< uart name, such as DRV_NAME_UART1
  216. size_t baud; ///< baud rate
  217. atDeviceFormat_t format; ///< character framing format
  218. atDeviceParity_t parity; ///< character framing parity
  219. bool rts_enable; ///< hw flow control, rts enable
  220. bool cts_enable; ///< hw flow control, cts enable
  221. } atDeviceUartConfig_t;
  222. /**
  223. * @brief virtual AT device configuration
  224. */
  225. typedef struct
  226. {
  227. uint32_t name; ///< device name, only used in trace
  228. osiPipe_t *rx_pipe; ///< AT RX pipe
  229. osiPipe_t *tx_pipe; ///< AT TX pipe
  230. } atDeviceVirtConfig_t;
  231. #ifdef CONFIG_QUEC_PROJECT_FEATURE_UART
  232. typedef struct
  233. {
  234. atDevice_t ops; // API
  235. drvUart_t *drv;
  236. atDeviceUartConfig_t config; // UART configuration
  237. osiWork_t *work;
  238. uint32_t pending_event;
  239. atDeviceAttr_t attr;
  240. } atDeviceUart_t;
  241. typedef struct
  242. {
  243. atDevice_t ops; // API
  244. uint32_t name;
  245. drvSerial_t *serial;
  246. } atDeviceUsrl_t;
  247. #endif
  248. #endif /* end CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE */
  249. /**
  250. * @brief callback function type after prompt mode is finished
  251. *
  252. * @param ctx provided callback context
  253. * @param end_mode prompt end mode, refer to \p atCmdPromptEndMode_t
  254. * @param size received data size
  255. */
  256. typedef void (*atCmdPromptCB_t)(void *ctx, atCmdPromptEndMode_t end_mode, size_t size);
  257. /**
  258. * @brief callback function type in command bypass mode
  259. *
  260. * @param ctx provided callback context
  261. * @param data received data pointer
  262. * @param size received data size
  263. * @return
  264. * - consumed bytes, it can be less than \p size
  265. * - 0 if nothing consumed
  266. */
  267. typedef int (*atCmdBypassCB_t)(void *ctx, const void *data, size_t size);
  268. /**
  269. * @brief callback function type in data bypass mode
  270. *
  271. * @param ctx provided callback context
  272. * @param data received data pointer
  273. * @param size received data size
  274. * @return
  275. * - consumed bytes, it can be less than \p size
  276. * - 0 if nothing consumed
  277. */
  278. typedef int (*atDataBypassCB_t)(void *ctx, const void *data, size_t size);
  279. /**
  280. * @brief set AT device dispatch
  281. *
  282. * @param th AT device pointer, must be valid
  283. * @param recv AT dispatch pointer, must be valid
  284. */
  285. static inline void atDeviceSetDispatch(atDevice_t *th, atDispatch_t *recv) { th->recv = recv; }
  286. /**
  287. * @brief get AT device dispatch
  288. *
  289. * @param th AT device pointer, must be valid
  290. * @return AT dispatch pointer
  291. */
  292. static inline atDispatch_t *atDeviceGetDispatch(atDevice_t *th) { return th->recv; }
  293. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  294. /**
  295. * @brief create UART AT device
  296. *
  297. * After create, the device is in *close* state. \p atDeviceOpen
  298. * should be called before access the device.
  299. *
  300. * @param cfg UART AT device configuration, must be valid
  301. * this /p cfg can be change by the function,
  302. * invalid items will be corrected to bestfit
  303. * @return
  304. * - UART AT device pointer
  305. * - NULL if out of memory
  306. */
  307. atDevice_t *atDeviceUartCreate(atDeviceUartConfig_t *cfg);
  308. /**
  309. * @brief create UART AT device
  310. *
  311. * Comparing to \p atDeviceUartCreate, the parameter is an already created
  312. * UART device.
  313. *
  314. * @param drv UART AT device driver, must be valid
  315. * @return
  316. * - UART AT device pointer
  317. * - NULL if out of memory, or invalid parmameter
  318. */
  319. atDevice_t *atDeviceUartCreateWithDevice(struct drvUart *drv);
  320. /**
  321. * @brief create serial AT device
  322. *
  323. * This used to create USB CDC/ACM AT device.
  324. *
  325. * @param name serial device name, such as DRV_NAME_USRL_COM0
  326. * @return
  327. * - serial AT device pointer
  328. * - NULL if out of memory
  329. */
  330. atDevice_t *atDeviceUserialCreate(uint32_t name);
  331. /**
  332. * @brief create diag at device
  333. *
  334. * This used to create diag AT device
  335. *
  336. * @return
  337. * - NULL fail
  338. * - other the at device
  339. */
  340. atDevice_t *atDeviceDiagCreate();
  341. /**
  342. * @brief create virtual AT device
  343. *
  344. * @param cfg virtual AT device configuration, must be valid
  345. * @return
  346. * - AT device pointer
  347. * - NULL if out of memory
  348. */
  349. atDevice_t *atDeviceVirtCreate(const atDeviceVirtConfig_t *cfg);
  350. /**
  351. * @brief delete the AT device
  352. *
  353. * When \p th is NULL, nothing will be done.
  354. *
  355. * @param th AT device to be deleted
  356. */
  357. void atDeviceDelete(atDevice_t *th);
  358. /**
  359. * @brief open the AT device for read and write
  360. *
  361. * @param th AT device, must be valid
  362. * @return
  363. * - true on success
  364. */
  365. bool atDeviceOpen(atDevice_t *th);
  366. /**
  367. * @brief close the AT device
  368. *
  369. * @param th AT device, must be valid
  370. */
  371. void atDeviceClose(atDevice_t *th);
  372. /**
  373. * @brief write data to AT device
  374. *
  375. * AT device will try to write all data. When the output buffer is
  376. * full, it will wait.
  377. *
  378. * Usually, AT device will define a *reasonable* timeout. At timeout,
  379. * the written size may be less than specified size.
  380. *
  381. * When \p size is 0, nothing will be done.
  382. *
  383. * @param th AT device, must be valid
  384. * @param data data pointer to be written, must be valid if \p size
  385. * is not zero
  386. * @param size data size
  387. * @return
  388. * - written byte count
  389. * - -1 if parameter is invalid, or device error
  390. */
  391. int atDeviceWrite(atDevice_t *th, const void *data, size_t size);
  392. /**
  393. * @brief read data from AT device
  394. *
  395. * It will just read from the device receive buffer. When the buffer
  396. * is empty, return 0. Even the receive buffer is not empty, the return
  397. * size may be less than the specified size.
  398. *
  399. * When \p size is 0, nothing will be done.
  400. *
  401. * @param th AT device, must be valid
  402. * @param data mempry pointer for read, must be valid if \p size
  403. * is not zero
  404. * @param size memory size
  405. * @return
  406. * - read byte count
  407. * - -1 if parameter is invalid, or device error
  408. */
  409. int atDeviceRead(atDevice_t *th, void *data, size_t size);
  410. /**
  411. * @brief AT device read available bytes
  412. *
  413. * @param th AT device, must be valid
  414. * @return
  415. * - (-1) Parameter error
  416. * - OTHERS (>=0) Available size in byte
  417. */
  418. int atDeviceReadAvail(atDevice_t *th);
  419. /**
  420. * @brief AT device write available space
  421. *
  422. * @param th AT device, must be valid
  423. * @return
  424. * - (-1) Parameter error
  425. * - OTHERS (>=0) Available size in byte
  426. */
  427. int atDeviceWriteAvail(atDevice_t *th);
  428. /**
  429. * @brief set AT device baudrate
  430. *
  431. * @param th AT device, must be valid
  432. * @param baud baud rate
  433. */
  434. void atDeviceSetBaudrate(atDevice_t *th, size_t baud);
  435. /**
  436. * @brief set AT device charactor format
  437. *
  438. * @param th AT device, must be valid
  439. * @param format refer to \p atDeviceFormat_t
  440. * @param parity refer to \p atDeviceParity_t
  441. */
  442. void atDeviceSetFormat(atDevice_t *th, atDeviceFormat_t format, atDeviceParity_t parity);
  443. /**
  444. * @brief set AT device flow control
  445. *
  446. * @param th AT device, must be valid
  447. * @param rxfc flow control of RX
  448. * @param txfc flow control of TX
  449. * @return
  450. * - true on success
  451. * - false on failed
  452. */
  453. bool atDeviceSetFlowCtrl(atDevice_t *th, atDeviceRXFC_t rxfc, atDeviceTXFC_t txfc);
  454. /**
  455. * @brief set AT device auto sleep
  456. *
  457. * Refer to \p drvUartSetAutoSleep
  458. *
  459. * @param th AT device, must be valid
  460. * @param timeout auto sleep wait time after transfer done. It can be
  461. * 0 but not recommended. Negative value to disable
  462. * auto sleep feature.
  463. */
  464. void atDeviceSetAutoSleep(atDevice_t *th, int timeout);
  465. /**
  466. * @brief get AT device attribute
  467. *
  468. * @param th AT device, must be valid
  469. * @return the attribute of this device
  470. */
  471. const atDeviceAttr_t *atDeviceAttribute(atDevice_t *th);
  472. #endif /* end CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE */
  473. /**
  474. * @brief create a dispatch of device
  475. *
  476. * @param device AT device pointer, must be valid
  477. * @return the created dispatch pointer
  478. */
  479. atDispatch_t *atDispatchCreate(atDevice_t *device);
  480. /**
  481. * @brief create dispatch for device or cmux DLC
  482. *
  483. * When \p device is not NULL, the dispatch is for device. When
  484. * \p cmuxParent is not NULL, the dispatch is for cmux DLC. It is
  485. * invalid that both of \p device and \p cmuxParent are not NULL,
  486. * or both of \p device and \p cmuxParent are NULL.
  487. *
  488. * @param device AT device
  489. * @param cmuxParent parent cmux mode engine
  490. * @param dlci when create dispatch for cmux DLC, it is the
  491. * DLC number
  492. * @return
  493. * - the created dispatch
  494. * - NULL if out of memory
  495. */
  496. atDispatch_t *atDispatchDlcCreate(atDevice_t *device, atCmuxEngine_t *cmuxParent, int dlci);
  497. /**
  498. * @brief delete a dispatch
  499. *
  500. * When \p th is NULL, nothing will be done.
  501. *
  502. * When engines of the dispatch is busy, the dispatch won't be deleted
  503. * immediately. Rather, it will be deleted after engines is non-busy.
  504. * When the dispatch is in delete pending mode, engine output will be
  505. * disabled.
  506. *
  507. * @param th the dispatch pointer, must be valid
  508. */
  509. void atDispatchDelete(atDispatch_t *th);
  510. /**
  511. * @brief read data from AT device
  512. *
  513. * Usually, it is called (directly or through thread callback) when there
  514. * are input data in AT device.
  515. *
  516. * @param th the dispatch pointer, must be valid
  517. */
  518. void atDispatchRead(atDispatch_t *th);
  519. /**
  520. * @brief send event to AT thread itself to consume more data
  521. *
  522. * AT thread is working in non-blocking asynchronous mode. When downstream
  523. * is ready, it is needed to notify upstream to consume more data. Event
  524. * is used to avoid too many levels of function calls.
  525. *
  526. * @param th the dispatch pointer, must be valid
  527. */
  528. void atDispatchReadLater(atDispatch_t *th);
  529. /**
  530. * @brief notify dispatch that device is broken
  531. *
  532. * This can be called outside AT thread.
  533. *
  534. * @param th the dispatch pointer, must be valid
  535. */
  536. void atDispatchDeviceBroken(atDispatch_t *th);
  537. /**
  538. * @brief get AT device of the dispatch
  539. *
  540. * When the dispatch is binded to CMUX DLC, the device of the parent
  541. * CMUX engine will be returned.
  542. *
  543. * @param th the dispatch pointer, must be valid
  544. * @return the AT device pointer
  545. */
  546. atDevice_t *atDispatchGetDevice(atDispatch_t *th);
  547. /**
  548. * @brief get the command mode engine the dispatch
  549. *
  550. * Command mode engine of dispatch will be always created. This will
  551. * return the command mode engine, no matter which dispatch mode is.
  552. *
  553. * @param th the dispatch pointer, must be valid
  554. * @return the command mode engine
  555. */
  556. atCmdEngine_t *atDispatchGetCmdEngine(atDispatch_t *th);
  557. /**
  558. * @brief get the data mode engine the dispatch
  559. *
  560. * Data mode engine of dispatch will be created when dispatch is changed
  561. * to data mode. So, the returned pointer may be NULL if not in data mode.
  562. *
  563. * @param th the dispatch pointer, must be valid
  564. * @return the data mode engine
  565. */
  566. atDataEngine_t *atDispatchGetDataEngine(atDispatch_t *th);
  567. /**
  568. * @brief get the paremt cmux mode engine
  569. *
  570. * When the dispatch is not created for cmux DLC, it will return NULL.
  571. *
  572. * @param th the dispatch pointer, must be valid
  573. * @return the parent cmux mode engine
  574. */
  575. atCmuxEngine_t *atDispatchGetParentCmuxEngine(atDispatch_t *th);
  576. /**
  577. * @brief check whether in command mode
  578. *
  579. * @param th the dispatch pointer, must be valid
  580. * @return
  581. * - true if in command mode
  582. * - false if not in command mode
  583. */
  584. bool atDispatchIsCmdMode(atDispatch_t *th);
  585. /**
  586. * @brief check whether in data mode
  587. *
  588. * @param th the dispatch pointer, must be valid
  589. * @return
  590. * - true if in data mode
  591. * - false if not in data mode
  592. */
  593. bool atDispatchIsDataMode(atDispatch_t *th);
  594. /**
  595. * @brief check whether dispatch is in cmux mode
  596. *
  597. * @param th the dispatch pointer, must be valid
  598. * @return
  599. * - true if the dispatch is in cmux mode
  600. * - false if the dispatch is not in cmux mode
  601. */
  602. bool atDispatchIsCmuxMode(atDispatch_t *th);
  603. #ifdef CONFIG_QUEC_PROJECT_FEATURE
  604. extern int atDispatchGetDlci(atDispatch_t *th);
  605. #endif
  606. /**
  607. * @brief set dispatch to command mode
  608. *
  609. * @param th the dispatch pointer, must be valid
  610. */
  611. void atDispatchSetCmdMode(atDispatch_t *th);
  612. /**
  613. * @brief set dispatch to data mode
  614. *
  615. * @param th the dispatch pointer, must be valid
  616. */
  617. void atDispatchSetDataMode(atDispatch_t *th);
  618. /**
  619. * @brief finish data mode and set dispatch to command mode
  620. *
  621. * @param th the dispatch pointer, must be valid
  622. */
  623. void atDispatchEndDataMode(atDispatch_t *th);
  624. /**
  625. * @brief finish data PPP mode and set dispatch to command mode
  626. *
  627. * Comparing to \p atDispatchEndDataMode, the dispatch will still checking
  628. * whether incoming data are valid PPP packets. Only when the incoming
  629. * data are invalid PPP packets, or configured timeout reaches, dispatch
  630. * will start to push data to command line engine.
  631. *
  632. * @param th the dispatch pointer, must be valid
  633. */
  634. void atDispatchEndDataPppMode(atDispatch_t *th);
  635. /**
  636. * @brief dispatch end cmux mode
  637. *
  638. * It must be called in CMUX mode.
  639. *
  640. * @param th the dispatch pointer, must be valid
  641. */
  642. void atDispatchEndCmuxMode(atDispatch_t *th);
  643. /**
  644. * @brief check whether dispatch in data escape mode
  645. *
  646. * @param th the dispatch pointer, must be valid
  647. */
  648. bool atDispatchInDataEscape(atDispatch_t *th);
  649. /**
  650. * @brief set dispatch to cmux mode, with specified configuration
  651. *
  652. * @param th the dispatch pointer, must be valid
  653. * @param cfg cmux configuration
  654. */
  655. void atDispatchSetCmuxMode(atDispatch_t *th, const atCmuxConfig_t *cfg);
  656. /**
  657. * @brief get the global AT dispatch list
  658. *
  659. * @return the global AT dispatch list head
  660. */
  661. osiSlistHead_t *atDispatchGetList(void);
  662. /**
  663. * @brief end cmux mode for all dispatches in cmux mode
  664. *
  665. * It will check all dispatches, and end cmux mode forcedly if the dispatch
  666. * is in cmux mode.
  667. *
  668. * @return the count of dispatches in cmux mode
  669. */
  670. unsigned atDispatchEndCmuxModeAll(void);
  671. /**
  672. * @brief check whether it is a valid dispatch pointer
  673. *
  674. * @param p the dispatch pointer to be checked
  675. * @return
  676. * - true if \p p is valid dispatch pointer
  677. * - false if \p p is not valid dispatch pointer
  678. */
  679. bool atDispatchIsValid(atDispatch_t *p);
  680. /**
  681. * @brief dispatch write data
  682. *
  683. * When the dispatch is for device, \p data will be written to device.
  684. * Otherwise, \p data will be written the corresponding DLC.
  685. *
  686. * @param th the dispatch pointer, must be valid
  687. * @param data data to be written, can't be NULL is size is not 0
  688. * @param size data size
  689. */
  690. void atDispatchWrite(atDispatch_t *th, const uint8_t *data, unsigned size);
  691. /**
  692. * @brief push data to dispatch
  693. *
  694. * Usually it is called by cmux engine, to push data to dispatch for
  695. * DLC. At push, all data will be consumed or cached in dispatch.
  696. *
  697. * @param th the dispatch pointer, must be valid
  698. * @param data data to be pushed to dispatch, can't be NULL is size is not 0
  699. * @param size data size
  700. */
  701. void atDispatchPushData(atDispatch_t *th, const void *data, size_t size);
  702. /**
  703. * @brief create AT command mode engine
  704. *
  705. * @param dispatch parent dispatch of the command mode engine
  706. * @return
  707. * - command mode engine pointer
  708. * - NULL if out of memory
  709. */
  710. atCmdEngine_t *atCmdEngineCreate(atDispatch_t *dispatch);
  711. /**
  712. * @brief delete AT command mode engine
  713. *
  714. * @param th command mode engine, must be valid
  715. */
  716. void atCmdEngineDelete(atCmdEngine_t *th);
  717. /**
  718. * @brief push data to command engine
  719. *
  720. * It will be called by dispatch. When dispatch received data, either in
  721. * read mode or push mode, dispatch will push the data to engine.
  722. *
  723. * @param th cmux mode engine, must be valid
  724. * @param data data to be pushed, can't be NULL if \p size is not 0.
  725. * @param size data size
  726. * @return
  727. * - consumed data size
  728. * - -1 if command engine is busy
  729. */
  730. int atCmdPushData(atCmdEngine_t *th, const void *data, size_t size);
  731. /**
  732. * @brief write data to AT command mode engine
  733. *
  734. * When the parent dispatch is not in command mode, nothing will be done.
  735. *
  736. * It will wait all data are written (maybe in buffer of AT device).
  737. *
  738. * Though the data should be printable characters in typical cases,
  739. * non-printable characters are permitted. It can be used to output
  740. * binary data if needed.
  741. *
  742. * @param th command mode engine, must be valid
  743. * @param data data pointer to be write, can't be NULL is size is not 0
  744. * @param size data size
  745. */
  746. void atCmdWrite(atCmdEngine_t *th, const void *data, size_t size);
  747. /**
  748. * @brief set command mode engine to command line mode
  749. *
  750. * Command line mode is the mode to receive AT commands.
  751. *
  752. * @param th command mode engine, must be valid
  753. */
  754. void atCmdSetLineMode(atCmdEngine_t *th);
  755. /**
  756. * @brief set command mode engine to command line mode
  757. *
  758. * Prompt mode is the mode defined in \p CMGS and \p CMGW.
  759. *
  760. * Though the main purpose of this mode is for \p CMGS and \p CMGW, it
  761. * can be used in other command handling, if the behavior is the same.
  762. *
  763. * In prompt mode, the input won't come to command line buffer, rather
  764. * the input will be written to the provided buffer.
  765. *
  766. * @param th command mode engine, must be valid
  767. * @param cb callback after the prompt input is finished
  768. * @param cb_ctx callback context
  769. * @param buff buffer pointer for input data, can't be NULL
  770. * @param buff_size buffer size
  771. * @return
  772. * - true on success
  773. * - false on invalid parameter, the mode isn't changed
  774. */
  775. bool atCmdSetPromptMode(atCmdEngine_t *th, atCmdPromptCB_t cb, void *cb_ctx, void *buff, size_t buff_size);
  776. /**
  777. * @brief set command mode engine to bypass mode
  778. *
  779. * In bypass mode, the input will be passed as parameter of callback. Also,
  780. * the input won't be echoed no matter the setting of *ATE*.
  781. *
  782. * After bypass mode input is finished, \p atCmdSetLineMode should be called
  783. * explicitly.
  784. *
  785. * Typical usage is to receive binary data.
  786. *
  787. * @param th command mode engine, must be valid
  788. * @param cb callback when there are input data
  789. * @param cb_ctx callback context
  790. * @return
  791. * - true on success
  792. * - false on invalid parameter
  793. */
  794. bool atCmdSetBypassMode(atCmdEngine_t *th, atCmdBypassCB_t cb, void *cb_ctx);
  795. /**
  796. * @brief notify AT device baudrate is changed by AT command
  797. *
  798. * AT device flow control change commands: +IPR
  799. *
  800. * @param th command mode engine, must be valid
  801. */
  802. void atCmdDeviceSetBaudNeeded(atCmdEngine_t *th);
  803. /**
  804. * @brief notify AT device format is changed by AT command
  805. *
  806. * When AT device format is changed, the new setting can't be applied to
  807. * AT device immediately. Rather, AT device format should be changed after
  808. * the command line is finished. This is for AT command handler to notify
  809. * that AT device format change.
  810. *
  811. * AT device format change commands:
  812. * - +IPR
  813. * - +ICF
  814. *
  815. * @param th command mode engine, must be valid
  816. */
  817. void atCmdDeviceSetFormatNeeded(atCmdEngine_t *th);
  818. /**
  819. * @brief notify AT device flow control is changed by AT command
  820. *
  821. * AT device flow control change commands: +IFC
  822. *
  823. * @param th command mode engine, must be valid
  824. */
  825. void atCmdDeviceSetIfcNeeded(atCmdEngine_t *th);
  826. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  827. /**
  828. * @brief ignore remain commands in the command line
  829. *
  830. * @param th command mode engine, must be valid
  831. */
  832. void atCmdClearRemains(atCmdEngine_t *th);
  833. #endif
  834. /**
  835. * @brief handle one command finish
  836. *
  837. * @param engine command mode engine, must be valid
  838. * @param ok whether the command is success or fail
  839. */
  840. void atCmdFinalHandle(atCmdEngine_t *engine, bool ok);
  841. /**
  842. * @brief notify one command in command line is finished
  843. *
  844. * One command line may contain multiple AT commands. After each command
  845. * handling is finished, this should be called. And command mode engine
  846. * will schedule to handle the next command, if available.
  847. *
  848. * @param th command mode engine, must be valid
  849. */
  850. void atCmdCommandFinished(atCmdEngine_t *th);
  851. /**
  852. * @brief AT command timeout handler function type
  853. *
  854. * @param cmd current AT command in handling
  855. */
  856. typedef void (*atCommandTimeoutHandler_t)(atCommand_t *cmd);
  857. /**
  858. * @brief set command timeout handler
  859. *
  860. * Set AT command handler timeout handler. At timeout, \p handler will be
  861. * called.
  862. *
  863. * It is not needed to unregister the timeout handler manually. At
  864. * \p atCmdFinalHandle, the timeout handler will be cleared automatically.
  865. *
  866. * When \p handler is NULL, the previously registered timeout handler will
  867. * be cleared, through it is not needed in typical cases.
  868. *
  869. * This *must* be called in AT command handler.
  870. *
  871. * Usually, the timeout handler shall do the command clean up, and response
  872. * a final result code. For example:
  873. *
  874. * \code{.cpp}
  875. * static void _timeout(atCommand_t *cmd)
  876. * {
  877. * // clean up ...
  878. * atCmdRespCmdError(cmd->engine, cme_error_code);
  879. * }
  880. *
  881. * void AT_COMMAND_HANDLE(atCommand_t *cmd)
  882. * {
  883. * // ......
  884. * atCmdSetTimeoutHandler(cmd->engine, ms, _timeout);
  885. * }
  886. * \endcode
  887. *
  888. * When the AT command will switch channel to data or CMUX mode, the timeout
  889. * handler may be called when the channel is not in command mode. Usually, it
  890. * is not wanted. And it is suggested not to set timeout handler if the
  891. * command will switch mode.
  892. *
  893. * @param th command mode engine, must be valid
  894. * @param timeout timeout in milliseconds
  895. * @param handler timeout handler
  896. * @return
  897. * - true on success
  898. * - false if there are no AT command in handling
  899. */
  900. bool atCmdSetTimeoutHandler(atCmdEngine_t *th, uint32_t timeout, atCommandTimeoutHandler_t handler);
  901. /**
  902. * @brief AT command abort handler function type
  903. *
  904. * @param cmd current AT command in handling
  905. */
  906. typedef void (*atCommandAbortHandler_t)(atCommand_t *cmd);
  907. /**
  908. * @brief set command handler abort handler
  909. *
  910. * By default, when an AT command is in handling, further input will be
  911. * hold. Only after the current AT command finished, further input will
  912. * be parsed.
  913. *
  914. * When a valid \p handler is registered, \p handler will be called on
  915. * *any* input character. Usually, final result code shall be responsed
  916. * in \p handler to abort the current command handling.
  917. *
  918. * When command engine is not in command line mode (such as prompt mode),
  919. * abort handler won't be called in further input.
  920. *
  921. * It is not needed to unregister the abort handler manually. At
  922. * \p atCmdFinalHandle, the abort handler will be cleared automatically.
  923. *
  924. * When \p handler is NULL, the previously registered abort handler will
  925. * be cleared.
  926. *
  927. * Usually, the timeout handler shall do the command clean up, and response
  928. * a final result code. For example:
  929. *
  930. * \code{.cpp}
  931. * static void _aborted(atCommand_t *cmd)
  932. * {
  933. * // clean up ...
  934. * atCmdRespCmdError(cmd->engine, cme_error_code);
  935. * }
  936. *
  937. * void AT_COMMAND_HANDLE(atCommand_t *cmd)
  938. * {
  939. * // ......
  940. * atCmdSetAbortHandler(cmd->engine, _aborted);
  941. * }
  942. * \endcode
  943. *
  944. * @param th command mode engine, must be valid
  945. * @param handler abort handler
  946. * @return
  947. * - true on success
  948. * - false if there are no command in handling
  949. */
  950. bool atCmdSetAbortHandler(atCmdEngine_t *th, atCommandAbortHandler_t handler);
  951. /**
  952. * @brief check whether in command line mode
  953. *
  954. * @param th command mode engine, must be valid
  955. * @return
  956. * - true if in command line mode
  957. * - false if not in command line mode
  958. */
  959. bool atCmdIsLineMode(atCmdEngine_t *th);
  960. /**
  961. * @brief check whether in prompt mode
  962. *
  963. * @param th command mode engine, must be valid
  964. * @return
  965. * - true if in prompt mode
  966. * - false if not in prompt mode
  967. */
  968. bool atCmdIsPromptMode(atCmdEngine_t *th);
  969. /**
  970. * @brief check whether in bypass mode
  971. *
  972. * @param th command mode engine, must be valid
  973. * @return
  974. * - true if in bypass mode
  975. * - false if not in bypass mode
  976. */
  977. bool atCmdIsBypassMode(atCmdEngine_t *th);
  978. /**
  979. * @brief get this or parent dispatch in cmux mode
  980. *
  981. * When the channel is in cmux mode, return the dispatch of current channel.
  982. * When the channel is a sub-channel of cmux mode, return the dispatch of
  983. * parent channel, which is in cmux mode. Otherwise, return NULL.
  984. *
  985. * @param th command mode engine, must be valid
  986. * @return
  987. * - dispatch of current or parent channel, which is in cmux mode
  988. * - NULL if neither current nor parent channel is in cmux mode
  989. */
  990. atDispatch_t *atCmdGetDispatchInCmux(atCmdEngine_t *th);
  991. /**
  992. * @brief set the binded SIM
  993. *
  994. * @param th command mode engine, must be valid
  995. * @param sim SIM number
  996. */
  997. void atCmdSetSim(atCmdEngine_t *th, uint8_t sim);
  998. /**
  999. * @brief get the binded SIM
  1000. *
  1001. * @param th command mode engine, must be valid
  1002. * @return the binded SIM number
  1003. */
  1004. uint8_t atCmdGetSim(atCmdEngine_t *th);
  1005. /**
  1006. * @brief get the parent dispatch
  1007. *
  1008. * @param th command mode engine, must be valid
  1009. * @return the parent dispatch
  1010. */
  1011. atDispatch_t *atCmdGetDispatch(atCmdEngine_t *th);
  1012. /**
  1013. * @brief get the pointer of per channel settings
  1014. *
  1015. * It returns the per channel settings, and it can be modified
  1016. * directly.
  1017. *
  1018. * @param th command mode engine, must be valid
  1019. * @return per channel settings pointer
  1020. */
  1021. atChannelSetting_t *atCmdChannelSetting(atCmdEngine_t *th);
  1022. /**
  1023. * @brief check whether it is the first infomation text
  1024. *
  1025. * Depends on AT channel setting, there are extra characters for
  1026. * the first information text.
  1027. *
  1028. * @param th command mode engine, must be valid
  1029. * @return
  1030. * - true if it is the first information text, that is there
  1031. * are no information text is output before.
  1032. * - false if not
  1033. */
  1034. bool atCmdIsFirstInfoText(atCmdEngine_t *th);
  1035. /**
  1036. * @brief set the first infomation text flag
  1037. *
  1038. * @param th command mode engine, must be valid
  1039. * @param is_first the first information text flag
  1040. */
  1041. void atCmdSetFirstInfoText(atCmdEngine_t *th, bool is_first);
  1042. /**
  1043. * @brief check whether the command list is empty
  1044. *
  1045. * A command line will contain multiple AT commands. This will check
  1046. * whether the command list is empty.
  1047. *
  1048. * @param th command mode engine, must be valid
  1049. * @return
  1050. * - true if the command list is empty
  1051. * - false if not
  1052. */
  1053. bool atCmdListIsEmpty(atCmdEngine_t *th);
  1054. /**
  1055. * @brief get the channel index
  1056. *
  1057. * Channel index is an internal index for each channel. Application
  1058. * shouldn't assume the meaning of the channel index. Also, there are
  1059. * no API to find channel by index.
  1060. *
  1061. * @param th command mode engine, must be valid
  1062. * @return the internal channel index
  1063. */
  1064. uint8_t atCmdChannelIndex(atCmdEngine_t *th);
  1065. #ifndef CONFIG_QUEC_PROJECT_FEATURE_ATC_PARSE
  1066. /**
  1067. * @brief increase reference for command engine
  1068. *
  1069. * In case that the command engine is needed for certain asynchronous
  1070. * handling, this should be called to indicate that the command engine
  1071. * shouldn't be deleted. After the asynchronous handling is done,
  1072. * \p atCmdDeceaseRef should be called to indicate the command engine
  1073. * is no longer needed.
  1074. *
  1075. * @param th command mode engine, must be valid
  1076. */
  1077. void atCmdIncreaseRef(atCmdEngine_t *th);
  1078. /**
  1079. * @brief decrease reference for command engine
  1080. *
  1081. * @param th command mode engine, must be valid
  1082. */
  1083. void atCmdDeceaseRef(atCmdEngine_t *th);
  1084. #endif
  1085. /**
  1086. * @brief set comman engine urc enable or disable
  1087. *
  1088. * When disabled, all urc will be ignored for the specified command engine.
  1089. * This is a non-standard feature. By default, urc is enabled.
  1090. *
  1091. * @param th command mode engine, must be valid
  1092. * @param enable true for enable urc, false for disable
  1093. */
  1094. void atCmdSetUrcEnable(atCmdEngine_t *th, bool enable);
  1095. /**
  1096. * @brief whether urc is enabled for command engine
  1097. *
  1098. * @param th command mode engine, must be valid
  1099. * @return
  1100. * - true if urc is enabled for the command engine
  1101. * - flase if urc is disabled
  1102. */
  1103. bool atCmdIsUrcEnabled(atCmdEngine_t *th);
  1104. /**
  1105. * @brief set comman engine urc enable or disable
  1106. *
  1107. * When disabled, all urc will be ignored for the specified command engine.
  1108. * This is a non-standard feature. By default, urc is enabled.
  1109. *
  1110. * @param th command mode engine, must be valid
  1111. * @param enable true for enable urc, false for disable
  1112. */
  1113. void atCmdSetUrcEnable(atCmdEngine_t *th, bool enable);
  1114. /**
  1115. * @brief whether urc is enabled for command engine
  1116. *
  1117. * @param th command mode engine, must be valid
  1118. * @return
  1119. * - true if urc is enabled for the command engine
  1120. * - flase if urc is disabled
  1121. */
  1122. bool atCmdIsUrcEnabled(atCmdEngine_t *th);
  1123. /**
  1124. * @brief indicate start of URC output
  1125. *
  1126. * URC output may be buffered, and it may be needed to drop oldest URC
  1127. * at buffer full. This indicates the start of URC output, to keep record
  1128. * of starting position of each URC in buffer.
  1129. *
  1130. * @param th command mode engine, must be valid
  1131. */
  1132. void atCmdUrcStart(atCmdEngine_t *th);
  1133. /**
  1134. * @brief indicate end of URC output
  1135. *
  1136. * @param th command mode engine, must be valid
  1137. */
  1138. void atCmdUrcStop(atCmdEngine_t *th);
  1139. /**
  1140. * @brief write URC data to AT command mode engine
  1141. *
  1142. * The URC data will be buffered, and output at \p atCmdUrcFlush.
  1143. *
  1144. * @param th command mode engine, must be valid
  1145. * @param data data pointer to be write, can't be NULL is size is not 0
  1146. * @param size data size
  1147. */
  1148. void atCmdUrcWrite(atCmdEngine_t *th, const void *data, size_t size);
  1149. /**
  1150. * @brief clear (discard) all buffered URC data
  1151. *
  1152. * @param th command mode engine, must be valid
  1153. */
  1154. void atCmdUrcClear(atCmdEngine_t *th);
  1155. /**
  1156. * @brief flush (output) all buffered URC data
  1157. *
  1158. * @param th command mode engine, must be valid
  1159. */
  1160. void atCmdUrcFlush(atCmdEngine_t *th);
  1161. /**
  1162. * @brief call async function in worker thread
  1163. *
  1164. * \p call will be executed in AT command channel worker thread. So, don't
  1165. * call AT response APIs in \p call.
  1166. *
  1167. * @param th command mode engine, must be valid
  1168. * @param call async call function
  1169. * @param ctx async call context
  1170. */
  1171. void atCmdWorkerCall(atCmdEngine_t *th, osiCallback_t call, void *ctx);
  1172. /**
  1173. * @brief create AT data mode engine
  1174. *
  1175. * @param dispatch parent dispatch of the data mode engine
  1176. * @return
  1177. * - data mode engine pointer
  1178. * - NULL if out of memory
  1179. */
  1180. atDataEngine_t *atDataEngineCreate(atDispatch_t *dispatch);
  1181. /**
  1182. * @brief delete AT data mode engine
  1183. *
  1184. * @param th data mode engine, must be valid
  1185. */
  1186. void atDataEngineDelete(atDataEngine_t *th);
  1187. /**
  1188. * @brief push data to data engine
  1189. *
  1190. * It will be called by dispatch. When dispatch received data, dispatch
  1191. * will push the data to engine.
  1192. *
  1193. * @param th data mode engine, must be valid
  1194. * @param data data to be pushed, can't be NULL if \p size is not 0.
  1195. * @param size data size
  1196. * @return
  1197. * - consumed data size
  1198. * - -1 if data engine is busy
  1199. */
  1200. int atDataPushData(atDataEngine_t *th, const void *data, size_t size);
  1201. /**
  1202. * @brief write data to data mode engine
  1203. *
  1204. * When the parent dispatch is not in data mode, nothing will be done.
  1205. *
  1206. * It will wait all data are written (maybe in buffer of AT device).
  1207. *
  1208. * @param th data mode engine, must be valid
  1209. * @param data data pointer to be write, can't be NULL is size is not 0
  1210. * @param size data size
  1211. */
  1212. void atDataWrite(atDataEngine_t *th, const void *data, size_t size);
  1213. /**
  1214. * @brief set data mode engine to PPP mode
  1215. *
  1216. * @param th data mode engine, must be valid
  1217. * @param ppp the PPP context pointer
  1218. */
  1219. void atDataSetPPPMode(atDataEngine_t *th, void *ppp);
  1220. /* @brief Exit data mode engine to PPP mode
  1221. *
  1222. * @param th data mode engine, must be valid
  1223. */
  1224. void atDataExitPPPMode(atDataEngine_t *th);
  1225. /**
  1226. * @brief set data mode engine to bypass mode
  1227. *
  1228. * @param th data mode engine, must be valid
  1229. * @param cb callback when there are input data
  1230. * @param cb_ctx callback context
  1231. * @return
  1232. * - true on success
  1233. * - false on invalid parameter
  1234. */
  1235. void atDataSetBypassMode(atDataEngine_t *th, atDataBypassCB_t cb, void *cb_ctx);
  1236. /**
  1237. * @brief check whether in PPP mode
  1238. *
  1239. * @param th data mode engine, must be valid
  1240. * @return
  1241. * - true if in PPP mode
  1242. * - false if not in PPP mode
  1243. */
  1244. bool atDataIsPPPMode(atDataEngine_t *th);
  1245. /**
  1246. * @brief check whether in bypass mode
  1247. *
  1248. * @param th data mode engine, must be valid
  1249. * @return
  1250. * - true if in bypass mode
  1251. * - false if not in bypass mode
  1252. */
  1253. bool atDataIsBypassMode(atDataEngine_t *th);
  1254. /**
  1255. * @brief get the parent dispatch
  1256. *
  1257. * @param th data mode engine, must be valid
  1258. * @return the parent dispatch
  1259. */
  1260. atDispatch_t *atDataGetDispatch(atDataEngine_t *th);
  1261. /**
  1262. * @brief get the PPP context
  1263. *
  1264. * @param th data mode engine, must be valid
  1265. * @return the PPP context pointer
  1266. */
  1267. void *atDataEngineGetPppSession(atDataEngine_t *th);
  1268. /**
  1269. * @brief clear the pppsession pointer
  1270. *
  1271. * @param th data mode engine, must be valid
  1272. *
  1273. */
  1274. void atDataClearPPPSession(atDataEngine_t *th);
  1275. /**
  1276. * @brief create AT cmux mode engine
  1277. *
  1278. * @param dispatch parent dispatch of the cmux mode engine
  1279. * @param cfg cmux configuration
  1280. * @return
  1281. * - cmux mode engine pointer
  1282. * - NULL if out of memory
  1283. */
  1284. atCmuxEngine_t *atCmuxEngineCreate(atDispatch_t *dispatch, const atCmuxConfig_t *cfg);
  1285. /**
  1286. * @brief delete AT cmux mode engine
  1287. *
  1288. * @param th cmux mode engine, must be valid
  1289. */
  1290. void atCmuxEngineDelete(atCmuxEngine_t *th);
  1291. /**
  1292. * @brief push data to cmux engine
  1293. *
  1294. * It will be called by dispatch. When dispatch received data, dispatch
  1295. * will push the data to engine.
  1296. *
  1297. * @param th cmux mode engine, must be valid
  1298. * @param data data to be pushed, can't be NULL if \p size is not 0.
  1299. * @param size data size
  1300. * @return
  1301. * - consumed data size
  1302. * - -1 if cmux engine is busy
  1303. */
  1304. int atCmuxPushData(atCmuxEngine_t *th, const void *data, size_t size);
  1305. /**
  1306. * @brief write data to specified DLC of AT cmux mode engine
  1307. *
  1308. * It will wait all data are written (maybe in buffer of AT device).
  1309. * The data will be sent in UIH packet.
  1310. *
  1311. * @param th cmux mode engine, must be valid
  1312. * @param data data pointer to be write, can't be NULL is size is not 0
  1313. * @param size data size
  1314. * @param dlci DLC number
  1315. */
  1316. void atCmuxDlcWrite(atCmuxEngine_t *th, const void *data, size_t size, int dlci);
  1317. /**
  1318. * @brief get the cmux mode engine current configuration
  1319. *
  1320. * @param th cmux mode engine, must be valid
  1321. * @return the cmux mode engine current configuration
  1322. */
  1323. atCmuxConfig_t atCmuxGetConfig(atCmuxEngine_t *th);
  1324. /**
  1325. * @brief get max frame size of AT cmux mode engine
  1326. *
  1327. * @param th cmux mode engine, must be valid
  1328. * @return
  1329. * - max frame size
  1330. */
  1331. int atCmuxMaxFrameSize(atCmuxEngine_t *th);
  1332. /**
  1333. * @brief get device of AT cmux mode engine
  1334. *
  1335. * @param th cmux mode engine, must be valid
  1336. * @return
  1337. * - cmux configuration
  1338. */
  1339. atDevice_t *atCmuxGetDevice(atCmuxEngine_t *th);
  1340. /**
  1341. * @brief get the parent dispatch
  1342. *
  1343. * @param th cmux mode engine, must be valid
  1344. * @return the parent dispatch
  1345. */
  1346. atDispatch_t *atCmuxGetDispatch(atCmuxEngine_t *th);
  1347. /**
  1348. * @brief Send CTRL msg MSC
  1349. *
  1350. * @param th cmux mode engine, must be valid
  1351. * @param cr 1:ctrl command, 0:ctrl response
  1352. * @param dlci virtual circuit path
  1353. * @param v24 v.24 control signal
  1354. * @param br default 0
  1355. */
  1356. void atCmuxSendCtrlMSC(atCmuxEngine_t *th, uint8_t cr, uint8_t dlci, uint8_t v24, uint8_t br);
  1357. /**
  1358. * @brief callback function for async AT command
  1359. *
  1360. * For async command, the callback shall be registered though \a cfwRequestUTI
  1361. * to wait CFW event with specified UTI, or through \a atSetPendingIdCmd
  1362. * to wait speficied event ID. When the matching event is arrived, this
  1363. * callback will be called (and the registration will be removed automatically).
  1364. */
  1365. typedef void (*atCommandAsyncCB_t)(atCommand_t *cmd, const osiEvent_t *event);
  1366. /**
  1367. * @brief register an ID pending command
  1368. *
  1369. * When the event with specified ID arrived, the callback will be called.
  1370. *
  1371. * @param cmd the AT command context
  1372. * @param id event ID
  1373. * @param handler the callback to be called
  1374. * @return
  1375. * - true if success
  1376. * - false on error. Refer to \a osiEventDispatchRegister
  1377. */
  1378. bool atSetPendingIdCmd(atCommand_t *cmd, uint32_t id, atCommandAsyncCB_t handler);
  1379. /**
  1380. * @brief init at device as an alarm owner
  1381. */
  1382. void atAlarmInit();
  1383. /**
  1384. * @brief schedule a callback running in AT thread
  1385. *
  1386. * It is just \p osiThreadCallback with AT thread as parameter.
  1387. *
  1388. * @param cb callback to be executed
  1389. * @param ctx callback context
  1390. */
  1391. void atEngineCallback(osiCallback_t cb, void *ctx);
  1392. /**
  1393. * @brief find the channel in data mode
  1394. *
  1395. * Now only one channel can be set in data mode (that is, PPP). This will
  1396. * find the data mode channel if exists. When there are no channel in data
  1397. * mode it will return NULL.
  1398. *
  1399. * @return
  1400. * - the channel in data mode if exists
  1401. * - NULL if no channels are in data mode
  1402. */
  1403. atDispatch_t *atFindDataChannel(void);
  1404. /**
  1405. * @brief AT dispatch mode switch cause
  1406. */
  1407. typedef enum atModeSwitchCause
  1408. {
  1409. /**
  1410. * @brief start data mode
  1411. */
  1412. AT_MODE_SWITCH_DATA_START,
  1413. /**
  1414. * @brief end data mode
  1415. */
  1416. AT_MODE_SWITCH_DATA_END,
  1417. /**
  1418. * @brief end data PPP mode
  1419. *
  1420. * Refer to \p atDispatchEndDataPppMode.
  1421. */
  1422. AT_MODE_SWITCH_DATA_PPP_END,
  1423. /**
  1424. * @brief escape from data mode to command line mode
  1425. *
  1426. * The data engine will be kept, and push data to command line engine.
  1427. * Typical usage is to process command temporally.
  1428. */
  1429. AT_MODE_SWITCH_DATA_ESCAPE,
  1430. /**
  1431. * @brief resume data mode from escaped command line mode
  1432. */
  1433. AT_MODE_SWITCH_DATA_RESUME,
  1434. } atModeSwitchCause_t;
  1435. void atEngineSchedule(osiCallback_t cb, void *cb_ctx);
  1436. void atEngineModeSwitch(atModeSwitchCause_t cause, atDispatch_t *d);
  1437. void atEngineSetDeviceAutoSleep(bool enabled);
  1438. void atMemFreeLater(void *ptr);
  1439. void atMemUndoFreeLater(void *ptr);
  1440. void atEngineEarlyInit(void);
  1441. void atEngineStart(void);
  1442. osiThread_t *atEngineGetThreadId(void);
  1443. bool atEventRegister(uint32_t id, osiEventHandler_t handler);
  1444. bool atEventsRegister(uint32_t id, ...);
  1445. void AT_SetAsyncTimerMux(atCmdEngine_t *cmd, uint32_t timeout);
  1446. bool atCmdEngineIsValid(atCmdEngine_t *cmd);
  1447. /**
  1448. * @brief whether there are commands running in any channel
  1449. *
  1450. * When channel is not in command mode, the *hold* command won't be regarded
  1451. * as running command. For example, when a channel enters data mode by ATD
  1452. * command, the ATD is not regards as running command even it is not finished.
  1453. *
  1454. * Usually, it can be use a hint for not blocking at engine.
  1455. *
  1456. * @return
  1457. * -true if there are commands running in any channel
  1458. * -false if not
  1459. */
  1460. bool atEngineIsCmdRunning(void);
  1461. /**
  1462. * @brief AT dispatch ModeSwitch handler function
  1463. *
  1464. * @param cmd current ATdispatch pointer
  1465. * @param cause ModeSwitch cause
  1466. */
  1467. typedef void (*atDispatchModeSwitchHandler_t)(atDispatch_t *th, atModeSwitchCause_t cause);
  1468. /**
  1469. * @brief set ModeSwitch handler
  1470. *
  1471. * Set AT ModeSwitch handler. At ModeSwitch, \p handler will be
  1472. * called.
  1473. *
  1474. * When \p handler is NULL, the previously registered ModeSwitch handler will
  1475. * be cleared, through it is not needed in typical cases.
  1476. *
  1477. * This *must* be called in AT command handler.
  1478. *
  1479. *
  1480. * @param th the dispatch pointer, must be valid
  1481. * @param handler ModeSwitch handler
  1482. */
  1483. void atDispatchSetModeSwitchHandler(atDispatch_t *th, atDispatchModeSwitchHandler_t handler);
  1484. /**
  1485. * @brief invoke ModeSwitch handler
  1486. *
  1487. * Invoke AT ModeSwitch handler. Call handler.
  1488. *
  1489. *
  1490. * @param th the dispatch pointer, must be valid
  1491. * @param cause ModeSwitch cause
  1492. */
  1493. void atDispatchInvokeModeSwitchHandle(atDispatch_t *th, atModeSwitchCause_t cause);
  1494. /**
  1495. * @brief default ppp ModeSwitch handler
  1496. *
  1497. * @param th the dispatch pointer, must be valid
  1498. * @param cause ModeSwitch cause
  1499. */
  1500. void pppAtModeSwitchHandler(atDispatch_t *th, atModeSwitchCause_t cause);
  1501. /**
  1502. * @brief create usb serial AT channel
  1503. *
  1504. * @param name the usb serial name
  1505. * @return
  1506. * - succeed create the channel or fail
  1507. */
  1508. bool atEngineCreateUSerialChannel(uint32_t name);
  1509. /**
  1510. * @brief create uart AT channel
  1511. *
  1512. * @param name the UART name
  1513. * @return
  1514. * - succeed create the channel or fail
  1515. */
  1516. bool atEngineCreateUartChannel(uint32_t name);
  1517. /**
  1518. * @brief get AT device ready status
  1519. *
  1520. * @param th AT device pointer, must be valid
  1521. * @return
  1522. * - ture device ready or fail not ready
  1523. */
  1524. bool atDispatchGetDeviceReadyStatus(atDispatch_t *th);
  1525. /**
  1526. * @brief set AT device ready status
  1527. *
  1528. * @param th AT device pointer, must be valid
  1529. * @param bReady Whether device is ready;
  1530. * @return
  1531. * NONE
  1532. */
  1533. void atDispatchSetDeviceReadyStatus(atDispatch_t *th, bool bReady);
  1534. /**
  1535. * @brief User customize the baud rate
  1536. *
  1537. * @param th AT device pointer, must be valid
  1538. * @return
  1539. * NONE
  1540. */
  1541. void atCmdDeviceEnableUsrBaud(atCmdEngine_t *th);
  1542. /**
  1543. * @brief User customize the icf config
  1544. *
  1545. * @param th AT device pointer, must be valid
  1546. * @return
  1547. * NONE
  1548. */
  1549. void atCmdDeviceSetUsrFormatNeeded(atCmdEngine_t *th);
  1550. #ifdef __cplusplus
  1551. }
  1552. #endif
  1553. #endif