nRF Connect SDK API 0.1.0
Loading...
Searching...
No Matches
at_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
16#ifndef AT_UTILS_H__
17#define AT_UTILS_H__
18
19#include <zephyr/types.h>
20#include <stddef.h>
21#include <ctype.h>
22
23#define AT_PARAM_SEPARATOR ','
24#define AT_RSP_SEPARATOR ':'
25#define AT_CMD_SEPARATOR '='
26#define AT_CMD_READ_TEST_IDENTIFIER '?'
27#define AT_CMD_BUFFER_TERMINATOR 0
28#define AT_CMD_STRING_IDENTIFIER '\"'
29#define AT_STANDARD_NOTIFICATION_PREFIX '+'
30#define AT_PROP_NOTIFICATION_PREFX '%'
31#define AT_CUSTOM_COMMAND_PREFX '#'
32
44static inline bool is_notification(char chr)
45{
46 if ((chr == AT_STANDARD_NOTIFICATION_PREFIX) ||
47 (chr == AT_PROP_NOTIFICATION_PREFX) ||
48 (chr == AT_CUSTOM_COMMAND_PREFX)) {
49 return true;
50 }
51
52 return false;
53}
54
65static inline bool is_valid_command_char(char chr)
66{
67 if (isalpha((int)chr) || isdigit((int)chr)) {
68 return true;
69 }
70
71 return false;
72}
73
84static inline bool is_valid_notification_char(char chr)
85{
86 chr = toupper((int)chr);
87
88 if (((chr >= 'A') && (chr <= 'Z')) || (chr == '_')) {
89 return true;
90 }
91
92 return false;
93}
94
106static inline bool is_terminated(char chr)
107{
108 if (chr == AT_CMD_BUFFER_TERMINATOR) {
109 return true;
110 }
111
112 return false;
113}
114
126static inline bool is_separator(char chr)
127{
128 if ((chr == AT_PARAM_SEPARATOR) || (chr == AT_RSP_SEPARATOR) ||
129 (chr == AT_CMD_SEPARATOR)) {
130 return true;
131 }
132
133 return false;
134}
135
146static inline bool is_lfcr(char chr)
147{
148 if ((chr == '\r') || (chr == '\n')) {
149 return true;
150 }
151
152 return false;
153}
154
166static inline bool is_dblquote(char chr)
167{
168 if (chr == '"') {
169 return true;
170 }
171
172 return false;
173}
174
185static inline bool is_array_start(char chr)
186{
187 if (chr == '(') {
188 return true;
189 }
190
191 return false;
192}
193
204static inline bool is_array_stop(char chr)
205{
206 if (chr == ')') {
207 return true;
208 }
209
210 return false;
211}
212
223static inline bool is_number(char chr)
224{
225 if (isdigit((int)chr) || (chr == '-') || (chr == '+')) {
226 return true;
227 }
228
229 return false;
230}
231
242static inline bool is_command(const char *str)
243{
244 if (strlen(str) < 2) {
245 return false;
246 }
247
248 if ((toupper((int)str[0]) != 'A') || (toupper((int)str[1]) != 'T')) {
249 return false;
250 }
251
252 /* Third character has be one of the command special characters.
253 * The special case is a lone "AT" command.
254 */
255 if ((str[2] == AT_STANDARD_NOTIFICATION_PREFIX) ||
256 (str[2] == AT_PROP_NOTIFICATION_PREFX) ||
257 (str[2] == AT_CUSTOM_COMMAND_PREFX) ||
258 is_lfcr(str[2]) || is_terminated(str[2])) {
259 return true;
260 }
261
262 return false;
263}
264
276static bool is_clac(const char *str)
277{
278 /* skip leading <CR><LF>, if any, as check not from index 0 */
279 while (is_lfcr(*str)) {
280 str++;
281 }
282
283 if (strlen(str) < 4) {
284 return false;
285 }
286
287 if ((toupper(str[0]) != 'A') || (toupper(str[1]) != 'T')) {
288 /* Not an AT command */
289 return false;
290 }
291
292 if ((toupper(str[2]) != '+') && (toupper(str[2]) != '%')) {
293 /* Neither AT+ nor AT% */
294 return false;
295 }
296
297 if ((toupper(str[2]) == '%') && (toupper(str[3]) == 'X')) {
298 /* Ignore AT%X to avoid false detect (read resp XCOEX0 etc.) */
299 return false;
300 }
301
302 return true;
303}
306#endif /* AT_UTILS_H__ */
static bool is_clac(const char *str)
Check if a string is a beginning of an AT CLAC response.
Definition: at_utils.h:276
static bool is_command(const char *str)
Check if a string is a beginning of an AT command.
Definition: at_utils.h:242
#define AT_STANDARD_NOTIFICATION_PREFIX
Definition: at_utils.h:29
static bool is_terminated(char chr)
Check if the character identifies the end of the buffer.
Definition: at_utils.h:106
static bool is_array_start(char chr)
Check if character is an array start character.
Definition: at_utils.h:185
#define AT_CUSTOM_COMMAND_PREFX
Definition: at_utils.h:31
static bool is_lfcr(char chr)
Check if character linefeed or carry return characters.
Definition: at_utils.h:146
#define AT_PROP_NOTIFICATION_PREFX
Definition: at_utils.h:30
static bool is_array_stop(char chr)
Check if character is an array stop character.
Definition: at_utils.h:204
static bool is_number(char chr)
Check if character is a number character (including + and -)
Definition: at_utils.h:223