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 
33 #ifndef AT_H
34 #define AT_H
35 
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <stdbool.h>
39 
40 #include "isrpipe.h"
41 #include "periph/uart.h"
42 #include "clist.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 #ifndef AT_SEND_EOL
49 
50 #define AT_SEND_EOL "\r"
51 #endif
52 
53 #ifndef AT_SEND_ECHO
54 
55 #define AT_SEND_ECHO 1
56 #endif
57 
59 #define AT_SEND_EOL_LEN (sizeof(AT_SEND_EOL) - 1)
60 
61 #ifndef AT_RECV_EOL_1
62 
63 #define AT_RECV_EOL_1 "\r"
64 #endif
65 
66 #ifndef AT_RECV_EOL_2
67 
68 #define AT_RECV_EOL_2 "\n"
69 #endif
70 
71 #ifndef AT_RECV_OK
72 
73 #define AT_RECV_OK "OK"
74 #endif
75 
76 #ifndef AT_RECV_ERROR
77 
78 #define AT_RECV_ERROR "ERROR"
79 #endif
80 
81 #if defined(MODULE_AT_URC) || DOXYGEN
82 #ifndef AT_BUF_SIZE
83 
84 #define AT_BUF_SIZE (128)
85 #endif
86 
93 typedef void (*at_urc_cb_t)(void *arg, const char *code);
94 
98 typedef struct {
101  const char *code;
102  void *arg;
103 } at_urc_t;
104 
105 #endif /* MODULE_AT_URC */
106 
110 typedef struct {
113 #ifdef MODULE_AT_URC
114  clist_node_t urc_list;
115 #endif
116 } at_dev_t;
117 
130 int at_dev_init(at_dev_t *dev, uart_t uart, uint32_t baudrate, char *buf, size_t bufsize);
131 
144 int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
145 
159 int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);
160 
178 ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf, size_t len, uint32_t timeout);
179 
201 ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf,
202  size_t len, bool keep_eol, uint32_t timeout);
203 
214 int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
215 
231 int at_recv_bytes_until_string(at_dev_t *dev, const char *string,
232  char *bytes, size_t *bytes_len,
233  uint32_t timeout);
234 
242 void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
243 
254 ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
255 
266 int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout);
267 
280 ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout);
281 
290 void at_drain(at_dev_t *dev);
291 
297 void at_dev_poweron(at_dev_t *dev);
298 
304 void at_dev_poweroff(at_dev_t *dev);
305 
306 #if defined(MODULE_AT_URC) || DOXYGEN
307 
313 void at_add_urc(at_dev_t *dev, at_urc_t *urc);
314 
321 void at_remove_urc(at_dev_t *dev, at_urc_t *urc);
322 
329 void at_process_urc(at_dev_t *dev, uint32_t timeout);
330 #endif
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif /* AT_H */
337 
isrpipe Interface
int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout)
Simple command helper.
Low-level UART peripheral driver interface definition.
Unsolicited result code data structure.
Definition: at.h:98
void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len)
Send raw bytes to a device.
int at_dev_init(at_dev_t *dev, uart_t uart, uint32_t baudrate, char *buf, size_t bufsize)
Initialize AT device struct.
unsigned int uart_t
Define default UART type identifier.
Definition: uart.h:78
clist_node_t list_node
node list
Definition: at.h:99
AT device structure.
Definition: at.h:110
void at_remove_urc(at_dev_t *dev, at_urc_t *urc)
Remove an unsolicited response code from the list.
void at_process_urc(at_dev_t *dev, uint32_t timeout)
Process out-of-band data received from the device.
void at_dev_poweroff(at_dev_t *dev)
Power device off.
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 * arg
optional argument
Definition: at.h:102
void at_drain(at_dev_t *dev)
Drain device input buffer.
ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout)
Send AT command, wait for multiline response.
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.
uart_t uart
UART device where the AT device is attached.
Definition: at.h:112
const char * code
URC string which must match.
Definition: at.h:101
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...
List node structure.
Definition: list.h:40
int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout)
Expect bytes from device.
isrpipe_t isrpipe
isrpipe used for getting data from uart
Definition: at.h:111
ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout)
Receive raw bytes from a device.
void at_dev_poweron(at_dev_t *dev)
Power device on.
int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout)
Send command to device.
Circular linked list.
void(* at_urc_cb_t)(void *arg, const char *code)
Unsolicited result code callback.
Definition: at.h:93
int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout)
Send AT command, wait for a prompt.
void at_add_urc(at_dev_t *dev, at_urc_t *urc)
Add a callback for an unsolicited response code.
at_urc_cb_t cb
callback
Definition: at.h:100
Context structure for isrpipe.
Definition: isrpipe.h:37