AT (Hayes) command set library

AT (Hayes) command set library. More...

Detailed Description

AT (Hayes) command set library.

This module provides functions to interact with devices using AT commands.

Most functions compare the bytes echoed by the device with what they intended to send, and bail out if there's no match.

Furthermore, the library tries to cope with difficulties regarding different line endings. It usually sends "<command><CR>", but expects "<command>\LF\CR" as echo.

As a debugging aid, when compiled with "-DAT_PRINT_INCOMING=1", every input byte gets printed.

Files

file  at.h
 AT (Hayes) library interface.
 

Data Structures

struct  at_urc_t
 Unsolicited result code data structure. More...
 
struct  at_dev_t
 AT device structure. More...
 

Macros

#define AT_SEND_EOL   "\r"
 End of line character to send after the AT command.
 
#define AT_SEND_ECHO   1
 Enable/disable the expected echo after an AT command is sent.
 
#define AT_SEND_EOL_LEN   (sizeof(AT_SEND_EOL) - 1)
 Shortcut for getting send end of line length.
 
#define AT_RECV_EOL_1   "\r"
 1st end of line character received (S3 aka CR character for a modem)
 
#define AT_RECV_EOL_2   "\n"
 1st end of line character received (S4 aka LF character for a modem)
 
#define AT_RECV_OK   "OK"
 default OK reply of an AT device
 
#define AT_RECV_ERROR   "ERROR"
 default ERROR reply of an AT device
 
#define AT_BUF_SIZE   (128)
 Internal buffer size used to process unsolicited result code data.
 

Typedefs

typedef void(* at_urc_cb_t) (void *arg, const char *code)
 Unsolicited result code callback. More...
 

Functions

int at_dev_init (at_dev_t *dev, uart_t uart, uint32_t baudrate, char *buf, size_t bufsize)
 Initialize AT device struct. More...
 
int at_send_cmd_wait_ok (at_dev_t *dev, const char *command, uint32_t timeout)
 Simple command helper. More...
 
int at_send_cmd_wait_prompt (at_dev_t *dev, const char *command, uint32_t timeout)
 Send AT command, wait for a prompt. More...
 
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. More...
 
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. More...
 
int at_expect_bytes (at_dev_t *dev, const char *bytes, uint32_t timeout)
 Expect bytes from device. More...
 
void at_send_bytes (at_dev_t *dev, const char *bytes, size_t len)
 Send raw bytes to a device. More...
 
int at_send_cmd (at_dev_t *dev, const char *command, uint32_t timeout)
 Send command to device. More...
 
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. More...
 
void at_drain (at_dev_t *dev)
 Drain device input buffer. More...
 
void at_dev_poweron (at_dev_t *dev)
 Power device on. More...
 
void at_dev_poweroff (at_dev_t *dev)
 Power device off. More...
 
void at_add_urc (at_dev_t *dev, at_urc_t *urc)
 Add a callback for an unsolicited response code. More...
 
void at_remove_urc (at_dev_t *dev, at_urc_t *urc)
 Remove an unsolicited response code from the list. More...
 
void at_process_urc (at_dev_t *dev, uint32_t timeout)
 Process out-of-band data received from the device. More...
 

Typedef Documentation

◆ at_urc_cb_t

typedef void(* at_urc_cb_t) (void *arg, const char *code)

Unsolicited result code callback.

Parameters
[in]argoptional argument
[in]codeurc string received from the device

Definition at line 93 of file at.h.

Function Documentation

◆ at_add_urc()

void at_add_urc ( at_dev_t dev,
at_urc_t urc 
)

Add a callback for an unsolicited response code.

Parameters
[in]devdevice to operate on
[in]urcunsolicited result code to register

◆ 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.

Parameters
[in]devstruct to initialize
[in]uartUART the device is connected to
[in]baudratebaudrate of the device
[in]bufinput buffer
[in]bufsizesize of buf
Returns
0 on success
<0 otherwise

◆ at_dev_poweroff()

void at_dev_poweroff ( at_dev_t dev)

Power device off.

Parameters
[in]devdevice to power off

◆ at_dev_poweron()

void at_dev_poweron ( at_dev_t dev)

Power device on.

Parameters
[in]devdevice to power on

◆ at_drain()

void at_drain ( at_dev_t dev)

Drain device input buffer.

This function drains any possible bytes waiting in the device's input buffer.

Parameters
[in]devdevice to operate on

◆ at_expect_bytes()

int at_expect_bytes ( at_dev_t dev,
const char *  bytes,
uint32_t  timeout 
)

Expect bytes from device.

Parameters
[in]devdevice to operate on
[in]bytesbuffer containing bytes to expect (NULL-terminated)
[in]timeouttimeout (in usec)
Returns
0 on success
<0 otherwise

◆ at_process_urc()

void at_process_urc ( at_dev_t dev,
uint32_t  timeout 
)

Process out-of-band data received from the device.

Parameters
[in]devdevice to operate on
[in]timeouttimeout (in usec)

◆ 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.

Parameters
[in]devdevice to operate on
[in]resp_bufbuffer to store line
[in]lensize of resp_buf
[in]keep_eoltrue to keep the CR character in the response
[in]timeouttimeout (in usec)
Returns
line length on success
<0 on error

◆ at_remove_urc()

void at_remove_urc ( at_dev_t dev,
at_urc_t urc 
)

Remove an unsolicited response code from the list.

Parameters
[in]devdevice to operate on
[in]urcunsolicited result code to remove

◆ at_send_bytes()

void at_send_bytes ( at_dev_t dev,
const char *  bytes,
size_t  len 
)

Send raw bytes to a device.

Parameters
[in]devdevice to operate on
[in]bytesbuffer containing bytes to send
[in]lennumber of bytes to send

◆ at_send_cmd()

int at_send_cmd ( at_dev_t dev,
const char *  command,
uint32_t  timeout 
)

Send command to device.

Parameters
[in]devdevice to operate on
[in]commandcommand to send
[in]timeouttimeout (in usec)
Returns
0 on success
<0 otherwise

◆ 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.

This function sends the supplied command, then returns all response lines until the device sends "OK".

If a line starts with "ERROR" or "+CME ERROR:", or the buffer is full, the function returns -1.

Parameters
[in]devdevice to operate on
[in]commandcommand to send
[out]resp_bufbuffer for storing response
[in]lenlen of resp_buf
[in]keep_eoltrue to keep the CR character in the response
[in]timeouttimeout (in usec)
Returns
length of response on success
<0 on error

◆ 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.

This function sends the supplied command, then waits and returns one line of response.

A possible empty line will be skipped.

Parameters
[in]devdevice to operate on
[in]commandcommand to send
[out]resp_bufbuffer for storing response
[in]lenlen of buffer
[in]timeouttimeout (in usec)
Returns
length of response on success
<0 on error

◆ at_send_cmd_wait_ok()

int at_send_cmd_wait_ok ( at_dev_t dev,
const char *  command,
uint32_t  timeout 
)

Simple command helper.

This function sends an AT command to the device and waits for "OK".

Parameters
[in]devdevice to operate on
[in]commandcommand string to send
[in]timeouttimeout (in usec)
Returns
0 when device answers "OK"
<0 otherwise

◆ 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.

This function sends the supplied command, then waits for the prompt (>) character and returns

Parameters
[in]devdevice to operate on
[in]commandcommand string to send
[in]timeouttimeout (in usec)
Returns
0 when prompt is received
<0 otherwise