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 
at_send_bytes
void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len)
Send raw bytes to a device.
at_urc_t::list_node
clist_node_t list_node
node list
Definition: at.h:177
kernel_defines.h
Common macros and compiler attributes/pragmas configuration.
at_send_cmd_wait_prompt
int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout)
Send AT command, wait for a prompt.
at_urc_t
Unsolicited result code data structure.
Definition: at.h:176
at_dev_poweroff
void at_dev_poweroff(at_dev_t *dev)
Power device off.
event
event structure
Definition: event.h:142
at_urc_t::arg
void * arg
optional argument
Definition: at.h:180
at_recv_bytes
ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout)
Receive raw bytes from a device.
at_readline
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
unsigned int uart_t
Define default UART type identifier.
Definition: uart.h:76
at_dev_poweron
void at_dev_poweron(at_dev_t *dev)
Power device on.
at_process_urc
void at_process_urc(at_dev_t *dev, uint32_t timeout)
Process out-of-band data received from the device.
at_send_cmd
int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout)
Send command to device.
uart.h
Low-level UART peripheral driver interface definition.
at_dev_t::uart
uart_t uart
UART device where the AT device is attached.
Definition: at.h:193
at_urc_t::code
const char * code
URC string which must match.
Definition: at.h:179
isrpipe.h
isrpipe Interface
clist.h
Circular linked list.
at_remove_urc
void at_remove_urc(at_dev_t *dev, at_urc_t *urc)
Remove an unsolicited response code from the list.
at_urc_cb_t
void(* at_urc_cb_t)(void *arg, const char *code)
Unsolicited result code callback.
Definition: at.h:171
at_drain
void at_drain(at_dev_t *dev)
Drain device input buffer.
at_dev_t
AT device structure.
Definition: at.h:191
list_node
List node structure.
Definition: list.h:40
at_dev_t::isrpipe
isrpipe_t isrpipe
isrpipe used for getting data from uart
Definition: at.h:192
at_recv_bytes_until_string
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.
at_send_cmd_get_resp
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.
at_urc_t::cb
at_urc_cb_t cb
callback
Definition: at.h:178
at_expect_bytes
int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout)
Expect bytes from device.
at_send_cmd_wait_ok
int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout)
Simple command helper.
isrpipe_t
Context structure for isrpipe.
Definition: isrpipe.h:37
at_add_urc
void at_add_urc(at_dev_t *dev, at_urc_t *urc)
Add a callback for an unsolicited response code.
at_send_cmd_get_lines
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.
at_dev_init
int at_dev_init(at_dev_t *dev, uart_t uart, uint32_t baudrate, char *buf, size_t bufsize)
Initialize AT device struct.