Loading...
Searching...
No Matches
at.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
112
113#include <stdint.h>
114#include <unistd.h>
115#include <stdbool.h>
116
117#include "isrpipe.h"
118#include "periph/uart.h"
119#include "clist.h"
120#include "kernel_defines.h"
121
122#include "event.h"
123
124#ifdef __cplusplus
125extern "C" {
126#endif
127
136#if IS_ACTIVE(CONFIG_AT_SEND_EOL_WINDOWS)
137#define CONFIG_AT_SEND_EOL "\r\n"
138#elif IS_ACTIVE(CONFIG_AT_SEND_EOL_UNIX)
139#define CONFIG_AT_SEND_EOL "\n"
140#elif IS_ACTIVE(CONFIG_AT_SEND_EOL_MAC)
141#define CONFIG_AT_SEND_EOL "\r"
142#endif
143
144#ifndef CONFIG_AT_SEND_EOL
145#define CONFIG_AT_SEND_EOL "\r"
146#endif
147
152#ifdef DOXYGEN
153#define CONFIG_AT_SEND_SKIP_ECHO
154#endif
155
159#ifndef AT_RECV_EOL_1
160#define AT_RECV_EOL_1 "\r"
161#endif
162
166#ifndef AT_RECV_EOL_2
167#define AT_RECV_EOL_2 "\n"
168#endif
169
173#define AT_RECV_EOL AT_RECV_EOL_1 AT_RECV_EOL_2
174
178#ifndef CONFIG_AT_RECV_OK
179#define CONFIG_AT_RECV_OK "OK"
180#endif
181
185#ifndef CONFIG_AT_RECV_ERROR
186#define CONFIG_AT_RECV_ERROR "ERROR"
187#endif
189
190#if defined(MODULE_AT_URC) || DOXYGEN
197typedef void (*at_urc_cb_t)(void *arg, const char *code);
198
202typedef struct {
205 const char *code;
206 void *arg;
207} at_urc_t;
208
209#endif /* MODULE_AT_URC */
210
212#define AT_ERR_EXTENDED 200
213
215#define AT_SEND_EOL_LEN (sizeof(CONFIG_AT_SEND_EOL) - 1)
216
220typedef struct {
223 char *rp_buf;
224 size_t rp_buf_size;
225#ifdef MODULE_AT_URC
226 clist_node_t urc_list;
227#ifdef MODULE_AT_URC_ISR
228 bool awaiting_response;
229 event_t event;
230#endif
231#endif
232} at_dev_t;
233
237typedef struct {
239 uint32_t baudrate;
240 char *rx_buf;
241 size_t rx_buf_size;
249 char *rp_buf;
250 size_t rp_buf_size;
252
263static inline char const *at_get_err_info(at_dev_t const *dev)
264{
265 return dev->rp_buf;
266}
267
276int at_dev_init(at_dev_t *dev, at_dev_init_t const *init);
277
294int at_send_cmd_wait_ok(at_dev_t *dev, const char *command, uint32_t timeout);
295
313int at_send_cmd_wait_prompt(at_dev_t *dev, const char *command, uint32_t timeout);
314
326int at_wait_prompt(at_dev_t *dev, uint32_t timeout);
349ssize_t at_send_cmd_get_resp(at_dev_t *dev, const char *command, char *resp_buf,
350 size_t len, uint32_t timeout);
377ssize_t at_get_resp_with_prefix(at_dev_t *dev, const char *resp_prefix,
378 char *resp_buf, size_t len, uint32_t timeout);
407ssize_t at_send_cmd_get_resp_wait_ok(at_dev_t *dev, const char *command, const char *resp_prefix,
408 char *resp_buf, size_t len, uint32_t timeout);
409
436ssize_t at_send_cmd_get_lines(at_dev_t *dev, const char *command, char *resp_buf,
437 size_t len, uint32_t timeout);
438
449int at_expect_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
450
461int at_wait_bytes(at_dev_t *dev, const char *bytes, uint32_t timeout);
462
478int at_recv_bytes_until_string(at_dev_t *dev, const char *string,
479 char *bytes, size_t *bytes_len,
480 uint32_t timeout);
481
489void at_send_bytes(at_dev_t *dev, const char *bytes, size_t len);
490
501ssize_t at_recv_bytes(at_dev_t *dev, char *bytes, size_t len, uint32_t timeout);
502
515int at_send_cmd(at_dev_t *dev, const char *command, uint32_t timeout);
516
531int at_parse_resp(at_dev_t *dev, char const *resp);
532
548ssize_t at_readline(at_dev_t *dev, char *resp_buf, size_t len, bool keep_eol, uint32_t timeout);
549
566ssize_t at_readline_skip_empty(at_dev_t *dev, char *resp_buf, size_t len,
567 bool keep_eol, uint32_t timeout);
568
584int at_wait_ok(at_dev_t *dev, uint32_t timeout);
585
595
602
609
610#if defined(MODULE_AT_URC) || DOXYGEN
617void at_add_urc(at_dev_t *dev, at_urc_t *urc);
618
626
633void at_process_urc(at_dev_t *dev, uint32_t timeout);
634
644void at_postprocess_urc(at_dev_t *dev, char *buf);
654void at_postprocess_urc_all(at_dev_t *dev, char *buf);
655#endif
656
657#ifdef __cplusplus
658}
659#endif
660
Circular linked list.
list_node_t clist_node_t
List node structure.
Definition clist.h:104
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:197
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.
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.
static char const * at_get_err_info(at_dev_t const *dev)
Get extended error information of the last command sent.
Definition at.h:263
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:109
struct event event_t
event structure forward declaration
Definition event.h:135
isrpipe Interface
Common macros and compiler attributes/pragmas configuration.
AT device initialization parameters.
Definition at.h:237
char * rp_buf
Response parsing buffer - used for classifying DCE responses and holding detailed error information.
Definition at.h:249
size_t rp_buf_size
response parsing buffer size
Definition at.h:250
uint32_t baudrate
UART device baudrate.
Definition at.h:239
uart_t uart
UART device where the AT device is attached.
Definition at.h:238
char * rx_buf
UART rx buffer.
Definition at.h:240
size_t rx_buf_size
UART rx buffer size.
Definition at.h:241
AT device structure.
Definition at.h:220
uart_t uart
UART device where the AT device is attached.
Definition at.h:222
isrpipe_t isrpipe
isrpipe used for getting data from uart
Definition at.h:221
size_t rp_buf_size
response parsing buffer size
Definition at.h:224
char * rp_buf
response parsing buffer
Definition at.h:223
Unsolicited result code data structure.
Definition at.h:202
at_urc_cb_t cb
callback
Definition at.h:204
clist_node_t list_node
node list
Definition at.h:203
void * arg
optional argument
Definition at.h:206
const char * code
URC string which must match.
Definition at.h:205
event structure
Definition event.h:145
Context structure for isrpipe.
Definition isrpipe.h:36
Low-level UART peripheral driver interface definition.