1 /*!
2  * @file at.h
3  *
4  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
5  */
6 
7 #ifndef _AT_PARSER_H_
8 #define _AT_PARSER_H_
9 
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 /** @addtogroup aos_at at
14  *  AT parser, which is used to ease the operation of AT modules.
15  *
16  *  @{
17  */
18 
19 /**
20  * AT dev type
21  */
22 typedef enum {
23     AT_DEV_UART = 0,
24     AT_DEV_USB = 1,
25     /* add more types here when necessary */
26     AT_DEV_TYPE_MAX
27 } at_dev_type_t;
28 
29 /*
30 * AT dev ops type
31 */
32 typedef struct {
33    at_dev_type_t type;
34    int (*init)(void *dev);
35    int (*recv)(void *dev, void *data, uint32_t expect_size,
36                uint32_t *recv_size, uint32_t timeout);
37    int (*send)(void *dev, void *data, uint32_t size,
38                uint32_t timeout);
39    int (*deinit)(void *dev);
40 } at_dev_ops_t;
41 
42 /**
43  * The reply format of the AT commands.
44  */
45 typedef struct {
46     char *reply_prefix;                 //!< reply prefix
47     char *reply_success_postfix;        //!< reply postfix indicating success
48     char *reply_fail_postfix;           //!< reply postfix indicating failure
49 } at_reply_config_t;
50 
51 /**
52  * The config for the AT device.
53  */
54 typedef struct {
55     uint8_t            port;                 //!< dev port. Compulsory
56     at_dev_type_t      type;                 //!< dev type. Compulsory
57     void               *dev_cfg;             //!< dev config. Compulsory. For UART, see uart_config_t in hal/uart.h
58     at_reply_config_t  reply_cfg;            //!< AT receive prefix and postfix. Compulsory.
59     char               *send_delimiter;      //!< AT sending delimiter between command and data. Optional, "\r"
60     uint32_t           timeout_ms;           //!< AT send or receive timeout in millisecond. Optional, 1000 ms by defaut
61     uint8_t            send_wait_prompt;     //!< whether AT send waits prompt before sending data. Optional, 0 by default
62     uint32_t           prompt_timeout_ms;    //!< AT send wait prompt timeout in millisecond. Optional, 200 ms by default
63     uint8_t            send_data_no_wait;    //!< whether AT send waits response after sending data. Optional, 0 by default
64     int                recv_task_priority;   //!< AT recv task priority. Optional, 32 by default
65     int                recv_task_stacksize;  //!< AT recv task stacksize. Optional, 1K by default
66 } at_config_t;
67 
68 #ifndef bool
69 #define bool unsigned char
70 #endif
71 
72 #ifndef true
73 #define true 1
74 #endif
75 #ifndef false
76 #define false 0
77 #endif
78 
79 /**
80  * The is the defition of the AT oob callback function.
81  *
82  * @param[in]  arg     The arguments for the callback function.
83  * @param[in]  buf     The buffer to store the result when user uses
84  *                     prefix and postfix to match a patterned AT event,
85  *                     e.g. domain-to-ip event on some platforms.
86  * @param[in]  buflen  The length of the buffer.
87  *
88  * @return none.
89  */
90 typedef void (*at_recv_cb)(void *arg, char *buf, int buflen);
91 
92 /**
93  * Initialization for AT parser module
94  *
95  * @return success: 0; fail: -1
96  */
97 int at_init(void);
98 
99 /**
100  * Deinitialization for AT parser module
101  *
102  * @return success: 0; fail: -1
103  */
104 int at_deinit(void);
105 
106 /**
107  * Add a new AT device
108  *
109  * @param[in]  config  AT config (e.g. port number, AT send delimiter).
110  *                     See definition for at_config_t. MUST not be NULL.
111  *
112  * @return success: fd for AT device; fail: -1
113  */
114 int at_add_dev(at_config_t *config);
115 
116 /**
117  * Delete AT device by fd
118  *
119  * @param[in]  fd  fd for AT device
120  *
121  * @return success: 0; fail: -1
122  */
123 int at_delete_dev(int fd);
124 
125 /**
126  * AT send (format: command + delimiter + data) and wait reply
127  *
128  * @param[in]      fd           fd for AT device
129  * @param[in]      cmd          AT command sending buf. MUST not be NULL.
130  * @param[in]      cmdlen       AT command length.
131  * @param[in]      delimiter    whether sending delimiter, usually value is true
132  * @param[in]      data data    sending buf. NULL if no data.
133  * @param[in]      datalen      data length. Zero if no data.
134  * @param[in,out]  replybuf     reply buffer. MUST not be NULL.
135  * @param[in]      bufsize      reply buffer size
136  * @param[in]      atcmdconfig  AT cmd reply format config. Use default if NULL
137  *
138  * @return success: 0; fail: -1
139  */
140 int at_send_wait_reply(int fd, const char *cmd, int cmdlen, bool delimiter,
141                        const char *data, int datalen,
142                        char *replybuf, int bufsize,
143                        const at_reply_config_t *atcmdconfig);
144 
145 /**
146  * AT send (format: data + delimiter) and does not wait reply
147  *
148  * @param[in]  fd         fd for AT device
149  * @param[in]  data       sending buffer.
150  * @param[in]  datalen    sending length.
151  * @param[in]  delimiter  whether sending delimiter, usually value is false
152  *
153  * @return success: 0; fail: -1
154  */
155 int at_send_no_reply(int fd, const char *data, int datalen, bool delimiter);
156 
157 
158 /**
159  * AT read for certain bytes of data
160  *
161  * @param[in]      fd        fd for AT device
162  * @param[in,out]  outbuf    output buffer.
163  * @param[in]      readsize  read size.
164  *
165  * @return success: read length; fail: -1
166  */
167 int at_read(int fd, char *outbuf, int readsize);
168 
169 
170 /**
171  * AT register callback for recv
172  *
173  * @param[in]      fd       fd for AT device
174  * @param[in]      prefix   interested string.
175  * @param[in]      postfix  intersted postfix.
176  * @param[in,out]  recvbuf  recv buffer.
177  * @param[in]      bufsize  recv buffer len.
178  * @param[in]      cb       callback handle function.
179  * @param[in]      arg      callback handle function args.
180  *
181  * @return success: 0; fail: -1
182  *
183  * @note
184  * - The recvbuf and bufsize must be present if postfix is used,
185  *   which are used to store the result and length if any data
186  *   that match the prefix & postfix pattern.
187  */
188 int at_register_callback(int fd, const char *prefix, const char *postfix, char *recvbuf,
189                          int bufsize, at_recv_cb cb, void *arg);
190 
191 
192 /**
193  * AT yield receive function. Only used in single task scenario
194  *
195  * @param[in]      fd           fd for AT device.
196  * @param[in,out]  replybuf     reply buffer.
197  * @param[in]      bufsize      reply buffer size.
198  * @param[in]      atcmdconfig  AT cmd reply format config. Use default if NULL.
199  * @param[in]      timeout_ms   receive timeout in millisecond.
200  *
201  * @return success: 0; fail: -1
202  */
203 int at_yield(int fd, char *replybuf, int bufsize, const at_reply_config_t *atcmdconfig,
204              int timeout_ms);
205 
206 /** @} */ /* end of group aos_at */
207 
208 #endif
209