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 
55 #ifndef AT_H
56 #define AT_H
57 
58 #include <stdint.h>
59 #include <unistd.h>
60 #include <stdbool.h>
61 
62 #include "isrpipe.h"
63 #include "periph/uart.h"
64 #include "clist.h"
65 #include "kernel_defines.h"
66 
67 #include "event.h"
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
81 #if IS_ACTIVE(CONFIG_AT_SEND_EOL_WINDOWS)
82 #define CONFIG_AT_SEND_EOL "\r\n"
83 #elif IS_ACTIVE(CONFIG_AT_SEND_EOL_UNIX)
84 #define CONFIG_AT_SEND_EOL "\n"
85 #elif IS_ACTIVE(CONFIG_AT_SEND_EOL_MAC)
86 #define CONFIG_AT_SEND_EOL "\r"
87 #endif
88 
89 #ifndef CONFIG_AT_SEND_EOL
90 #define CONFIG_AT_SEND_EOL "\r"
91 #endif
92 
97 #ifdef DOXYGEN
98 #define CONFIG_AT_SEND_SKIP_ECHO
99 #endif
100 
107 #ifndef AT_SEND_ECHO
108 #if IS_ACTIVE(CONFIG_AT_SEND_SKIP_ECHO)
109 #define AT_SEND_ECHO 0
110 #else
111 #define AT_SEND_ECHO 1
112 #endif
113 #endif
114 
118 #ifndef AT_RECV_EOL_1
119 #define AT_RECV_EOL_1 "\r"
120 #endif
121 
125 #ifndef AT_RECV_EOL_2
126 #define AT_RECV_EOL_2 "\n"
127 #endif
128 
132 #ifndef CONFIG_AT_RECV_OK
133 #define CONFIG_AT_RECV_OK "OK"
134 #endif
135 
139 #ifndef CONFIG_AT_RECV_ERROR
140 #define CONFIG_AT_RECV_ERROR "ERROR"
141 #endif
142 
143 #if defined(MODULE_AT_URC) || DOXYGEN
144 
153 #ifndef CONFIG_AT_BUF_SIZE_EXP
154 #define CONFIG_AT_BUF_SIZE_EXP (7U)
155 #endif
156 
161 #ifndef AT_BUF_SIZE
162 #define AT_BUF_SIZE (1 << CONFIG_AT_BUF_SIZE_EXP)
163 #endif
164 
171 typedef void (*at_urc_cb_t)(void *arg, const char *code);
172 
176 typedef struct {
179  const char *code;
180  void *arg;
181 } at_urc_t;
182 
183 #endif /* MODULE_AT_URC */
184 
186 #define AT_SEND_EOL_LEN (sizeof(CONFIG_AT_SEND_EOL) - 1)
187 
191 typedef struct {
194 #ifdef MODULE_AT_URC
195  clist_node_t urc_list;
196 #ifdef MODULE_AT_URC_ISR
197  bool awaiting_response;
198  event_t event;
199 #endif
200 #endif
201 } at_dev_t;
202 
215 int at_dev_init(at_dev_t *dev, uart_t uart, uint32_t baudrate, char *buf, size_t bufsize);
216 
229 int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
230 
244 int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);
245 
263 ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf, size_t len, uint32_t timeout);
264 
286 ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf,
287  size_t len, bool keep_eol, uint32_t timeout);
288 
299 int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
300 
316 int at_recv_bytes_until_string(at_dev_t *dev, const char *string,
317  char *bytes, size_t *bytes_len,
318  uint32_t timeout);
319 
327 void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
328 
339 ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
340 
351 int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout);
352 
365 ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout);
366 
375 void at_drain(at_dev_t *dev);
376 
382 void at_dev_poweron(at_dev_t *dev);
383 
389 void at_dev_poweroff(at_dev_t *dev);
390 
391 #if defined(MODULE_AT_URC) || DOXYGEN
392 
398 void at_add_urc(at_dev_t *dev, at_urc_t *urc);
399 
406 void at_remove_urc(at_dev_t *dev, at_urc_t *urc);
407 
414 void at_process_urc(at_dev_t *dev, uint32_t timeout);
415 #endif
416 
417 #ifdef __cplusplus
418 }
419 #endif
420 
421 #endif /* AT_H */
422 
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:176
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:177
AT device structure.
Definition: at.h:191
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.
event structure
Definition: event.h:141
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:180
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:193
const char * code
URC string which must match.
Definition: at.h:179
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:192
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.
Common macros and compiler attributes/pragmas configuration.
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:171
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:178
Context structure for isrpipe.
Definition: isrpipe.h:37