Skip to content

Commit 525ac93

Browse files
committed
lib: at_cmd_parser: Parse AT commands
at_cmd_parser was good for parsing AT responses and notifications, but could not parse AT commands. Fix this by adding extra state in the parser. Signed-off-by: Robert Lubos <[email protected]>
1 parent 3d72432 commit 525ac93

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lib/at_cmd_parser/at_cmd_parser.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum at_parser_state {
2424
NUMBER,
2525
SMS_PDU,
2626
NOTIFICATION,
27+
COMMAND,
2728
OPTIONAL,
2829
};
2930

@@ -49,6 +50,9 @@ static int at_parse_detect_type(const char **str, int index)
4950
* notification ID, (eg +CEREG:)
5051
*/
5152
set_new_state(NOTIFICATION);
53+
} else if ((index == 0) && is_command(tmpstr)) {
54+
/* Next, check if we deal with command (eg AT+CCLK) */
55+
set_new_state(COMMAND);
5256
} else if (index == 0) {
5357
/* If the string start without an notification
5458
* ID, we treat the whole string as one string
@@ -124,7 +128,17 @@ static int at_parse_process_element(const char **str,
124128
at_params_string_put(list,
125129
index, start_ptr,
126130
tmpstr - start_ptr);
131+
} else if (state == COMMAND) {
132+
const char *start_ptr = tmpstr;
133+
134+
tmpstr += sizeof("AT+") - 1;
127135

136+
while (is_valid_notification_char(*tmpstr)) {
137+
tmpstr++;
138+
}
139+
140+
at_params_string_put(list, index, start_ptr,
141+
tmpstr - start_ptr);
128142
} else if (state == OPTIONAL) {
129143
at_params_empty_put(list, index);
130144

lib/at_cmd_parser/at_utils.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#include <ctype.h>
2121

2222
#define AT_PARAM_SEPARATOR ','
23-
#define AT_CMD_SEPARATOR ':'
23+
#define AT_RSP_SEPARATOR ':'
24+
#define AT_CMD_SEPARATOR '='
2425
#define AT_CMD_BUFFER_TERMINATOR 0
2526
#define AT_CMD_STRING_IDENTIFIER '\"'
2627
#define AT_STANDARD_NOTIFICATION_PREFIX '+'
2728
#define AT_PROP_NOTIFICATION_PREFX '%'
29+
#define AT_CUSTOM_COMMAND_PREFX '#'
2830

2931
/**
3032
* @brief Check if character is a notification start character
@@ -47,6 +49,33 @@ static inline bool is_notification(char chr)
4749
return false;
4850
}
4951

52+
/**
53+
* @brief Check if a string is a beginning of an AT command
54+
*
55+
* This function will check if the character is a "AT+" or "AT%" which
56+
* identifies an AT command.
57+
*
58+
* @param[in] str String to examine
59+
*
60+
* @retval true If the string is an AT command
61+
* @retval false Otherwise
62+
*/
63+
static inline bool is_command(const char *str)
64+
{
65+
if (strlen(str) < 3) {
66+
return false;
67+
}
68+
69+
if ((toupper(str[0]) == 'A') && (toupper(str[1]) == 'T') &&
70+
((str[2] == AT_STANDARD_NOTIFICATION_PREFIX) ||
71+
(str[2] == AT_PROP_NOTIFICATION_PREFX) ||
72+
(str[2] == AT_CUSTOM_COMMAND_PREFX))) {
73+
return true;
74+
}
75+
76+
return false;
77+
}
78+
5079
/**
5180
* @brief Verify that the character is a valid character
5281
*
@@ -102,6 +131,7 @@ static inline bool is_terminated(char chr)
102131
static inline bool is_separator(char chr)
103132
{
104133
if ((chr == AT_PARAM_SEPARATOR) ||
134+
(chr == AT_RSP_SEPARATOR) ||
105135
(chr == AT_CMD_SEPARATOR)) {
106136
return true;
107137
}

0 commit comments

Comments
 (0)