Skip to content

Commit

Permalink
modem: chat: Add helpers and test for dynamic chat scripts
Browse files Browse the repository at this point in the history
This commit adds helpers for initializing all aspects of a
chat script at runtime.

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
  • Loading branch information
Bjarki Arge Andreasen committed Sep 15, 2023
1 parent 67d4c83 commit 16cff87
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/zephyr/modem/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,33 @@ int modem_chat_init(struct modem_chat *chat, const struct modem_chat_config *con
*/
int modem_chat_attach(struct modem_chat *chat, struct modem_pipe *pipe);

/**
* @brief Initialize chat match structure
* @param chat_match Chat match structure to initialize
*/
void modem_chat_match_init(struct modem_chat_match *chat_match, const char *match,
const char *separators, bool wildcards, bool partial,
modem_chat_match_callback callback);

/**
* @brief Initialize chat script chat structure
* @param script_chat Chat script chat structure to initialize
*/
void modem_chat_script_chat_init(struct modem_chat_script_chat *script_chat, const char *request,
const struct modem_chat_match *response_matches,
uint16_t response_matches_size, uint16_t timeout_ms);

/**
* @brief Initialize chat script structure
* @param chat_match Chat script structure to initialize
*/
void modem_chat_script_init(struct modem_chat_script *script, const char *name,
const struct modem_chat_script_chat *script_chats,
uint16_t script_chats_size,
const struct modem_chat_match *abort_matches,
uint16_t abort_matches_size,
modem_chat_script_callback callback, uint32_t timeout);

/**
* @brief Run script asynchronously
* @param chat Chat instance
Expand Down
39 changes: 39 additions & 0 deletions subsys/modem/modem_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,45 @@ int modem_chat_attach(struct modem_chat *chat, struct modem_pipe *pipe)
return 0;
}

void modem_chat_match_init(struct modem_chat_match *chat_match, const char *match,
const char *separators, bool wildcards, bool partial,
modem_chat_match_callback callback)
{
chat_match->match = match;
chat_match->match_size = strlen(match);
chat_match->separators = separators;
chat_match->separators_size = strlen(separators);
chat_match->wildcards = wildcards;
chat_match->partial = partial;
chat_match->callback = callback;
}

void modem_chat_script_chat_init(struct modem_chat_script_chat *script_chat, const char *request,
const struct modem_chat_match *response_matches,
uint16_t response_matches_size, uint16_t timeout_ms)
{
script_chat->request = request;
script_chat->response_matches = response_matches;
script_chat->response_matches_size = response_matches_size;
script_chat->timeout = timeout_ms;
}

void modem_chat_script_init(struct modem_chat_script *script, const char *name,
const struct modem_chat_script_chat *script_chats,
uint16_t script_chats_size,
const struct modem_chat_match *abort_matches,
uint16_t abort_matches_size,
modem_chat_script_callback callback, uint32_t timeout)
{
script->name = name;
script->script_chats = script_chats;
script->script_chats_size = script_chats_size;
script->abort_matches = abort_matches;
script->abort_matches_size = abort_matches_size;
script->callback = callback;
script->timeout = timeout;
}

int modem_chat_run_script_async(struct modem_chat *chat, const struct modem_chat_script *script)
{
bool script_is_running;
Expand Down
19 changes: 19 additions & 0 deletions tests/subsys/modem/modem_chat/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,25 @@ ZTEST(modem_chat, test_script_run_sync_abort)
"Echo script should time out and return -EAGAIN");
}

ZTEST(modem_chat, test_script_run_dynamic_script_sync)
{
char match[] = "AT";
char separators[] = ",";
char request[] = "AT";
char name[] = "Dynamic";

struct modem_chat_match stack_response_match;
struct modem_chat_script_chat stack_script_chat;
struct modem_chat_script stack_script;

modem_chat_match_init(&stack_response_match, match, separators, false, false, NULL);
modem_chat_script_chat_init(&stack_script_chat, request, &stack_response_match, 1, 0);
modem_chat_script_init(&stack_script, name, &stack_script_chat, 1, NULL, 0, NULL, 1);

modem_backend_mock_prime(&mock, &at_echo_transaction);
zassert_ok(modem_chat_run_script(&cmd, &stack_script), "Failed to run script");
}

/*************************************************************************************************/
/* Test suite */
/*************************************************************************************************/
Expand Down

0 comments on commit 16cff87

Please sign in to comment.