Skip to content

Commit

Permalink
lib: at_parser: add new AT parser
Browse files Browse the repository at this point in the history
Add new AT parser

Signed-off-by: Mirko Covizzi <mirko.covizzi@nordicsemi.no>
  • Loading branch information
MirkoCovizzi committed Apr 10, 2024
1 parent 67dfb47 commit d3504d6
Show file tree
Hide file tree
Showing 16 changed files with 6,558 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ IncludeCategories:
- Regex: '.*'
Priority: 3
IndentCaseLabels: false
IndentGotoLabels: false
IndentWidth: 8
InsertBraces: true
SpaceBeforeParens: ControlStatementsExceptControlMacros
SpaceBeforeInheritanceColon: False
SortIncludes: Never
UseTab: Always
WhitespaceSensitiveMacros:
Expand Down
2 changes: 2 additions & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Kconfig* @tejlmand
/lib/bin/ @rlubos @lemrey
/lib/adp536x/ @nrfconnect/ncs-cia
/lib/at_cmd_parser/ @rlubos
/lib/at_parser/ @MirkoCovizzi
/lib/at_cmd_custom/ @eivindj-nordic
/lib/at_host/ @rlubos
/lib/at_monitor/ @lemrey @rlubos
Expand Down Expand Up @@ -287,6 +288,7 @@ Kconfig* @tejlmand
/tests/drivers/lpuart/ @nordic-krch
/tests/drivers/nrfx_integration_test/ @anangl
/tests/lib/at_cmd_parser/ @rlubos
/tests/lib/at_parser/ @MirkoCovizzi
/tests/lib/at_cmd_custom/ @eivindj-nordic
/tests/lib/date_time/ @trantanen @tokangas
/tests/lib/edge_impulse/ @pdunaj @MarekPieta
Expand Down
121 changes: 121 additions & 0 deletions include/modem/at_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#ifndef AT_PARSER_H__
#define AT_PARSER_H__

#include <stdbool.h>
#include <zephyr/types.h>

#ifdef __cplusplus
extern "C" {
#endif

enum at_token_type {
AT_TOKEN_TYPE_INVALID = 0,
AT_TOKEN_TYPE_CMD_TEST,
AT_TOKEN_TYPE_CMD_READ,
AT_TOKEN_TYPE_CMD_SET,
AT_TOKEN_TYPE_NOTIF,
AT_TOKEN_TYPE_INT,
AT_TOKEN_TYPE_QUOTED_STRING,
AT_TOKEN_TYPE_ARRAY,
AT_TOKEN_TYPE_EMPTY,
AT_TOKEN_TYPE_STRING,
AT_TOKEN_TYPE_RESP
};

enum at_token_variant {
AT_TOKEN_VARIANT_NO_COMMA = 0,
AT_TOKEN_VARIANT_COMMA
};

struct at_token {
const char *start;
uint16_t len;
uint8_t type :4;
uint8_t variant :1;
} __attribute__ ((__packed__));

struct at_parser {
const char *ptr;
size_t count;
struct {
size_t cmd_count;
size_t notif_count;
size_t subparam_count;
size_t string_count;
size_t end_count;
} counters;
struct at_token prev_token;
bool is_next_empty;
bool initialized;
};

/**
* @brief Initialize an AT parser.
*
* This function initializes an AT parser with the given AT command string.
*
* @param parser Pointer to the @ref at_parser structure to be initialized.
* @param at Pointer to a null-terminated string containing the AT command string to be
* parsed.
*
* @retval 0 If the operation was successful.
* @retval -EINVAL @p parser or @p at are NULL.
*/
int at_parser_init(struct at_parser *parser, const char *at);

/**
* @brief Parse the next token from the AT command string.
*
* This function parses the next token from the AT command string using the parser
* state.
*
* @param parser Pointer to the @ref at_parser structure that holds the state of the parser.
* @param token Pointer to an @ref at_token structure where the parsed token will be stored.
*
* @retval 0 If the operation was successful.
* @retval -EINVAL @p parser or @p token are NULL or the parser state is invalid,
* @retval -EPERM @p parser has not been initialized,
* @retval -EIO There is nothing left to parse in the AT command string,
* @retval -EBADMSG The token parsed is invalid.
*/
int at_parser_tok(struct at_parser *parser, struct at_token *token);

/**
* @brief Seek to the specified token index in the AT command string.
*
* This function parses the next token from the AT command string using the parser
* state.
*
* @param parser Pointer to the @ref at_parser structure that holds the state of the parser.
* @param index Zero-based index of the token to seek to within the AT command string.
* @param token Pointer to an @ref at_token structure where the parsed token will be stored.
*
* @retval 0 If the operation was successful.
* @retval -EINVAL @p parser or @p token are NULL or the parser state is invalid,
* @retval -EPERM @p parser has not been initialized,
* @retval -EIO There is nothing left to parse in the AT command string,
* @retval -EBADMSG The token parsed is invalid.
*/
int at_parser_seek(struct at_parser *parser, size_t index, struct at_token *token);
int at_token_line_parse(struct at_token *tokens, size_t num_tokens, const char *at,
const char **next_at);
int at_token_uint16_get(const struct at_token *token, uint16_t *value);
int at_token_int16_get(const struct at_token *token, int16_t *value);
int at_token_uint32_get(const struct at_token *token, uint32_t *value);
int at_token_int32_get(const struct at_token *token, int32_t *value);
int at_token_int64_get(const struct at_token *token, int64_t *value);
int at_token_string_get(const struct at_token *token, char *value, size_t *len);
size_t at_token_valid_count_get(const struct at_token *tokens, size_t num_tokens);
enum at_token_type at_cmd_type_get(const char *at);

#ifdef __cplusplus
}
#endif

#endif /* AT_PARSER_H__ */
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_subdirectory_ifdef(CONFIG_AT_CMD_CUSTOM at_cmd_custom)
add_subdirectory_ifdef(CONFIG_AT_MONITOR at_monitor)
add_subdirectory_ifdef(CONFIG_AT_HOST_LIBRARY at_host)
add_subdirectory_ifdef(CONFIG_AT_CMD_PARSER at_cmd_parser)
add_subdirectory_ifdef(CONFIG_AT_PARSER at_parser)
add_subdirectory_ifdef(CONFIG_LTE_LINK_CONTROL lte_link_control)
add_subdirectory_ifdef(CONFIG_MODEM_BATTERY modem_battery)
add_subdirectory_ifdef(CONFIG_MODEM_INFO modem_info)
Expand Down
1 change: 1 addition & 0 deletions lib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rsource "at_cmd_custom/Kconfig"
rsource "at_host/Kconfig"
rsource "dk_buttons_and_leds/Kconfig"
rsource "at_cmd_parser/Kconfig"
rsource "at_parser/Kconfig"
rsource "modem_battery/Kconfig"
rsource "modem_info/Kconfig"
rsource "pdn/Kconfig"
Expand Down
13 changes: 13 additions & 0 deletions lib/at_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

zephyr_library()
zephyr_library_sources(
at_parser.c
at_match.c
)

zephyr_include_directories(include)
9 changes: 9 additions & 0 deletions lib/at_parser/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

config AT_PARSER
bool "AT parser library"
depends on NEWLIB_LIBC || EXTERNAL_LIBC || PICOLIBC
Loading

0 comments on commit d3504d6

Please sign in to comment.