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 
9 #pragma once
10 
116 #include <stdint.h>
117 #include <unistd.h>
118 #include <stdbool.h>
119 
120 #include "isrpipe.h"
121 #include "periph/uart.h"
122 #include "clist.h"
123 #include "kernel_defines.h"
124 
125 #include "event.h"
126 
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130 
139 #if IS_ACTIVE(CONFIG_AT_SEND_EOL_WINDOWS)
140 #define CONFIG_AT_SEND_EOL "\r\n"
141 #elif IS_ACTIVE(CONFIG_AT_SEND_EOL_UNIX)
142 #define CONFIG_AT_SEND_EOL "\n"
143 #elif IS_ACTIVE(CONFIG_AT_SEND_EOL_MAC)
144 #define CONFIG_AT_SEND_EOL "\r"
145 #endif
146 
147 #ifndef CONFIG_AT_SEND_EOL
148 #define CONFIG_AT_SEND_EOL "\r"
149 #endif
150 
155 #ifdef DOXYGEN
156 #define CONFIG_AT_SEND_SKIP_ECHO
157 #endif
158 
162 #ifndef AT_RECV_EOL_1
163 #define AT_RECV_EOL_1 "\r"
164 #endif
165 
169 #ifndef AT_RECV_EOL_2
170 #define AT_RECV_EOL_2 "\n"
171 #endif
172 
176 #define AT_RECV_EOL AT_RECV_EOL_1 AT_RECV_EOL_2
177 
181 #ifndef CONFIG_AT_RECV_OK
182 #define CONFIG_AT_RECV_OK "OK"
183 #endif
184 
188 #ifndef CONFIG_AT_RECV_ERROR
189 #define CONFIG_AT_RECV_ERROR "ERROR"
190 #endif
193 #if defined(MODULE_AT_URC) || DOXYGEN
200 typedef void (*at_urc_cb_t)(void *arg, const char *code);
201 
205 typedef struct {
208  const char *code;
209  void *arg;
210 } at_urc_t;
211 
212 #endif /* MODULE_AT_URC */
213 
215 #define AT_ERR_EXTENDED 200
216 
218 #define AT_SEND_EOL_LEN (sizeof(CONFIG_AT_SEND_EOL) - 1)
219 
223 typedef struct {
226  char *rp_buf;
227  size_t rp_buf_size;
228 #ifdef MODULE_AT_URC
229  clist_node_t urc_list;
230 #ifdef MODULE_AT_URC_ISR
231  bool awaiting_response;
232  event_t event;
233 #endif
234 #endif
235 } at_dev_t;
236 
240 typedef struct {
242  uint32_t baudrate;
243  char *rx_buf;
244  size_t rx_buf_size;
252  char *rp_buf;
253  size_t rp_buf_size;
254 } at_dev_init_t;
255 
266 static inline char const *at_get_err_info(at_dev_t const *dev)
267 {
268  return dev->rp_buf;
269 }
279 int at_dev_init(at_dev_t *dev, at_dev_init_t const *init);
280 
297 int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
298 
316 int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);
317 
329 int at_wait_prompt(at_dev_t *dev, uint32_t timeout);
352 ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf,
353  size_t len, uint32_t timeout);
380 ssize_t at_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
381  char *resp_buf, size_t len, uint32_t timeout);
410 ssize_t at_send_cmd_get_resp_wait_ok(at_dev_t *dev, const char *command, const char *resp_prefix,
411  char *resp_buf, size_t len, uint32_t timeout);
412 
439 ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf,
440  size_t len, uint32_t timeout);
441 
452 int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
453 
464 int at_wait_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
465 
481 int at_recv_bytes_until_string(at_dev_t *dev, const char *string,
482  char *bytes, size_t *bytes_len,
483  uint32_t timeout);
484 
492 void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
493 
504 ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
505 
518 int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout);
519 
534 int at_parse_resp(at_dev_t *dev, char const *resp);
535 
551 ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout);
552 
569 ssize_t at_readline_skip_empty(at_dev_t *dev, char *resp_buf, size_t len,
570  bool keep_eol, uint32_t timeout);
571 
587 int at_wait_ok(at_dev_t *dev, uint32_t timeout);
588 
597 void at_drain(at_dev_t *dev);
598 
605 
612 
613 #if defined(MODULE_AT_URC) || DOXYGEN
620 void at_add_urc(at_dev_t *dev, at_urc_t *urc);
621 
628 void at_remove_urc(at_dev_t *dev, at_urc_t *urc);
629 
636 void at_process_urc(at_dev_t *dev, uint32_t timeout);
637 
647 void at_postprocess_urc(at_dev_t *dev, char *buf);
657 void at_postprocess_urc_all(at_dev_t *dev, char *buf);
658 #endif
659 
660 #ifdef __cplusplus
661 }
662 #endif
663 
Circular linked list.
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:200
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:266
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:84
isrpipe Interface
Common macros and compiler attributes/pragmas configuration.
AT device initialization parameters.
Definition: at.h:240
char * rp_buf
Response parsing buffer - used for classifying DCE responses and holding detailed error information.
Definition: at.h:252
size_t rp_buf_size
response parsing buffer size
Definition: at.h:253
uint32_t baudrate
UART device baudrate.
Definition: at.h:242
uart_t uart
UART device where the AT device is attached.
Definition: at.h:241
char * rx_buf
UART rx buffer.
Definition: at.h:243
size_t rx_buf_size
UART rx buffer size.
Definition: at.h:244
AT device structure.
Definition: at.h:223
uart_t uart
UART device where the AT device is attached.
Definition: at.h:225
isrpipe_t isrpipe
isrpipe used for getting data from uart
Definition: at.h:224
size_t rp_buf_size
response parsing buffer size
Definition: at.h:227
char * rp_buf
response parsing buffer
Definition: at.h:226
Unsolicited result code data structure.
Definition: at.h:205
at_urc_cb_t cb
callback
Definition: at.h:207
clist_node_t list_node
node list
Definition: at.h:206
void * arg
optional argument
Definition: at.h:209
const char * code
URC string which must match.
Definition: at.h:208
event structure
Definition: event.h:145
Context structure for isrpipe.
Definition: isrpipe.h:36
List node structure.
Definition: list.h:39
Low-level UART peripheral driver interface definition.