at.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
114 #ifndef AT_H
115 #define AT_H
116 
117 #include <stdint.h>
118 #include <unistd.h>
119 #include <stdbool.h>
120 
121 #include "isrpipe.h"
122 #include "periph/uart.h"
123 #include "clist.h"
124 #include "kernel_defines.h"
125 
126 #include "event.h"
127 
128 #ifdef __cplusplus
129 extern "C" {
130 #endif
131 
140 #if IS_ACTIVE(CONFIG_AT_SEND_EOL_WINDOWS)
141 #define CONFIG_AT_SEND_EOL "\r\n"
142 #elif IS_ACTIVE(CONFIG_AT_SEND_EOL_UNIX)
143 #define CONFIG_AT_SEND_EOL "\n"
144 #elif IS_ACTIVE(CONFIG_AT_SEND_EOL_MAC)
145 #define CONFIG_AT_SEND_EOL "\r"
146 #endif
147 
148 #ifndef CONFIG_AT_SEND_EOL
149 #define CONFIG_AT_SEND_EOL "\r"
150 #endif
151 
156 #ifdef DOXYGEN
157 #define CONFIG_AT_SEND_SKIP_ECHO
158 #endif
159 
163 #ifndef AT_RECV_EOL_1
164 #define AT_RECV_EOL_1 "\r"
165 #endif
166 
170 #ifndef AT_RECV_EOL_2
171 #define AT_RECV_EOL_2 "\n"
172 #endif
173 
177 #define AT_RECV_EOL AT_RECV_EOL_1 AT_RECV_EOL_2
178 
182 #ifndef CONFIG_AT_RECV_OK
183 #define CONFIG_AT_RECV_OK "OK"
184 #endif
185 
189 #ifndef CONFIG_AT_RECV_ERROR
190 #define CONFIG_AT_RECV_ERROR "ERROR"
191 #endif
194 #if defined(MODULE_AT_URC) || DOXYGEN
201 typedef void (*at_urc_cb_t)(void *arg, const char *code);
202 
206 typedef struct {
209  const char *code;
210  void *arg;
211 } at_urc_t;
212 
213 #endif /* MODULE_AT_URC */
214 
216 #define AT_ERR_EXTENDED 200
217 
219 #define AT_SEND_EOL_LEN (sizeof(CONFIG_AT_SEND_EOL) - 1)
220 
224 typedef struct {
227  char *rp_buf;
228  size_t rp_buf_size;
229 #ifdef MODULE_AT_URC
230  clist_node_t urc_list;
231 #ifdef MODULE_AT_URC_ISR
232  bool awaiting_response;
233  event_t event;
234 #endif
235 #endif
236 } at_dev_t;
237 
241 typedef struct {
243  uint32_t baudrate;
244  char *rx_buf;
245  size_t rx_buf_size;
253  char *rp_buf;
254  size_t rp_buf_size;
255 } at_dev_init_t;
256 
267 static inline char const *at_get_err_info(at_dev_t const *dev)
268 {
269  return dev->rp_buf;
270 }
280 int at_dev_init(at_dev_t *dev, at_dev_init_t const *init);
281 
298 int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
299 
317 int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);
318 
330 int at_wait_prompt(at_dev_t *dev, uint32_t timeout);
353 ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf,
354  size_t len, uint32_t timeout);
381 ssize_t at_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
382  char *resp_buf, size_t len, uint32_t timeout);
411 ssize_t at_send_cmd_get_resp_wait_ok(at_dev_t *dev, const char *command, const char *resp_prefix,
412  char *resp_buf, size_t len, uint32_t timeout);
413 
440 ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf,
441  size_t len, uint32_t timeout);
442 
453 int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
454 
465 int at_wait_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
466 
482 int at_recv_bytes_until_string(at_dev_t *dev, const char *string,
483  char *bytes, size_t *bytes_len,
484  uint32_t timeout);
485 
493 void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
494 
505 ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
506 
519 int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout);
520 
535 int at_parse_resp(at_dev_t *dev, char const *resp);
536 
552 ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout);
553 
570 ssize_t at_readline_skip_empty(at_dev_t *dev, char *resp_buf, size_t len,
571  bool keep_eol, uint32_t timeout);
572 
588 int at_wait_ok(at_dev_t *dev, uint32_t timeout);
589 
598 void at_drain(at_dev_t *dev);
599 
606 
613 
614 #if defined(MODULE_AT_URC) || DOXYGEN
621 void at_add_urc(at_dev_t *dev, at_urc_t *urc);
622 
629 void at_remove_urc(at_dev_t *dev, at_urc_t *urc);
630 
637 void at_process_urc(at_dev_t *dev, uint32_t timeout);
638 
648 void at_postprocess_urc(at_dev_t *dev, char *buf);
658 void at_postprocess_urc_all(at_dev_t *dev, char *buf);
659 #endif
660 
661 #ifdef __cplusplus
662 }
663 #endif
664 
665 #endif /* AT_H */
Circular linked list.
Low-level UART peripheral driver interface definition.
void at_add_urc(at_dev_t *dev, at_urc_t *urc)
Add a callback for an unsolicited response code.
int at_wait_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout)
Repeatedly calls at_expect_bytes() until a match or timeout occurs.
int at_wait_ok(at_dev_t *dev, uint32_t timeout)
Wait for an OK response.
void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len)
Send raw bytes to a device.
ssize_t at_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix, char *resp_buf, size_t len, uint32_t timeout)
Wait for a response with a specific prefix.
ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf, size_t len, uint32_t timeout)
Send AT command, wait for response.
void at_process_urc(at_dev_t *dev, uint32_t timeout)
Process out-of-band data received from the device.
ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout)
Receive raw bytes from a device.
int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout)
Send command to device.
int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout)
Simple command helper.
void(* at_urc_cb_t)(void *arg, const char *code)
Unsolicited result code callback.
Definition: at.h:201
void at_dev_poweroff(at_dev_t *dev)
Power device off.
ssize_t at_send_cmd_get_resp_wait_ok(at_dev_t *dev, const char *command, const char *resp_prefix, char *resp_buf, size_t len, uint32_t timeout)
Send AT command, wait for response plus OK.
int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout)
Expect bytes from device.
ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout)
Read a line from device.
ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf, size_t len, uint32_t timeout)
Send AT command, wait for multiline response.
static char const * at_get_err_info(at_dev_t const *dev)
Get extended error information of the last command sent.
Definition: at.h:267
int at_recv_bytes_until_string(at_dev_t *dev, const char *string, char *bytes, size_t *bytes_len, uint32_t timeout)
Receives bytes into bytes buffer until the string pattern string is received or the buffer is full.
int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout)
Send AT command, wait for a prompt.
int at_parse_resp(at_dev_t *dev, char const *resp)
Parse a response from the device.
ssize_t at_readline_skip_empty(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout)
Read a line from device, skipping a possibly empty line.
void at_postprocess_urc(at_dev_t *dev, char *buf)
Process one URC from the provided buffer.
void at_drain(at_dev_t *dev)
Drain device input buffer.
int at_dev_init(at_dev_t *dev, at_dev_init_t const *init)
Initialize AT device struct.
int at_wait_prompt(at_dev_t *dev, uint32_t timeout)
Waits for the prompt character (>).
void at_postprocess_urc_all(at_dev_t *dev, char *buf)
Process all URCs from the provided buffer.
void at_remove_urc(at_dev_t *dev, at_urc_t *urc)
Remove an unsolicited response code from the list.
void at_dev_poweron(at_dev_t *dev)
Power device on.
uint_fast8_t uart_t
Define default UART type identifier.
Definition: uart.h:85
isrpipe Interface
Common macros and compiler attributes/pragmas configuration.
AT device initialization parameters.
Definition: at.h:241
char * rp_buf
Response parsing buffer - used for classifying DCE responses and holding detailed error information.
Definition: at.h:253
size_t rp_buf_size
response parsing buffer size
Definition: at.h:254
uint32_t baudrate
UART device baudrate.
Definition: at.h:243
uart_t uart
UART device where the AT device is attached.
Definition: at.h:242
char * rx_buf
UART rx buffer.
Definition: at.h:244
size_t rx_buf_size
UART rx buffer size.
Definition: at.h:245
AT device structure.
Definition: at.h:224
uart_t uart
UART device where the AT device is attached.
Definition: at.h:226
isrpipe_t isrpipe
isrpipe used for getting data from uart
Definition: at.h:225
size_t rp_buf_size
response parsing buffer size
Definition: at.h:228
char * rp_buf
response parsing buffer
Definition: at.h:227
Unsolicited result code data structure.
Definition: at.h:206
at_urc_cb_t cb
callback
Definition: at.h:208
clist_node_t list_node
node list
Definition: at.h:207
void * arg
optional argument
Definition: at.h:210
const char * code
URC string which must match.
Definition: at.h:209
event structure
Definition: event.h:148
Context structure for isrpipe.
Definition: isrpipe.h:37
List node structure.
Definition: list.h:40