modem: chat: Add runtime API to modem_chat_script_chat

Add APIs allowing for modifying the modem_chat_script_chat
safely at runtime.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
This commit is contained in:
Bjarki Arge Andreasen 2024-02-28 07:17:15 +01:00 committed by Alberto Escolar
commit 2a575a6bf5
3 changed files with 115 additions and 0 deletions

View file

@ -426,6 +426,45 @@ void modem_chat_match_set_partial(struct modem_chat_match *chat_match, bool part
*/ */
void modem_chat_match_enable_wildcards(struct modem_chat_match *chat_match, bool enable); void modem_chat_match_enable_wildcards(struct modem_chat_match *chat_match, bool enable);
/**
* @brief Initialize modem chat script chat
* @param script_chat Modem chat script chat instance
*/
void modem_chat_script_chat_init(struct modem_chat_script_chat *script_chat);
/**
* @brief Set request of modem chat script chat instance
* @param script_chat Modem chat script chat instance
* @param request Request to set
* @note The lifetime of request must match or exceed the lifetime of script_chat
* @warning Always call this API after request is modified
*
* @retval 0 if successful, negative errno code otherwise
*/
int modem_chat_script_chat_set_request(struct modem_chat_script_chat *script_chat,
const char *request);
/**
* @brief Set modem chat script chat matches
* @param script_chat Modem chat script chat instance
* @param response_matches Response match array to set
* @param response_matches_size Size of response match array
* @note The lifetime of response_matches must match or exceed the lifetime of script_chat
*
* @retval 0 if successful, negative errno code otherwise
*/
int modem_chat_script_chat_set_response_matches(struct modem_chat_script_chat *script_chat,
const struct modem_chat_match *response_matches,
uint16_t response_matches_size);
/**
* @brief Set modem chat script chat timeout
* @param script_chat Modem chat script chat instance
* @param timeout_ms Timeout in milliseconds
*/
void modem_chat_script_chat_set_timeout(struct modem_chat_script_chat *script_chat,
uint16_t timeout_ms);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -764,6 +764,12 @@ static void modem_chat_pipe_callback(struct modem_pipe *pipe, enum modem_pipe_ev
} }
} }
static bool modem_chat_validate_array(const void *array, size_t size)
{
return ((array == NULL) && (size == 0)) ||
((array != NULL) && (size > 0));
}
#if CONFIG_MODEM_STATS #if CONFIG_MODEM_STATS
static uint32_t get_receive_buf_size(struct modem_chat *chat) static uint32_t get_receive_buf_size(struct modem_chat *chat)
{ {
@ -983,3 +989,43 @@ void modem_chat_match_enable_wildcards(struct modem_chat_match *match, bool enab
{ {
match->wildcards = enable; match->wildcards = enable;
} }
void modem_chat_script_chat_init(struct modem_chat_script_chat *script_chat)
{
memset(script_chat, 0, sizeof(struct modem_chat_script_chat));
}
int modem_chat_script_chat_set_request(struct modem_chat_script_chat *script_chat,
const char *request)
{
size_t size;
size = strnlen(request, UINT16_MAX + 1);
if (size == (UINT16_MAX + 1)) {
return -ENOMEM;
}
script_chat->request = request;
script_chat->request_size = (uint16_t)size;
return 0;
}
int modem_chat_script_chat_set_response_matches(struct modem_chat_script_chat *script_chat,
const struct modem_chat_match *response_matches,
uint16_t response_matches_size)
{
if (!modem_chat_validate_array(response_matches, response_matches_size)) {
return -EINVAL;
}
script_chat->response_matches = response_matches;
script_chat->response_matches_size = response_matches_size;
return 0;
}
void modem_chat_script_chat_set_timeout(struct modem_chat_script_chat *script_chat,
uint16_t timeout)
{
script_chat->timeout = timeout;
}

View file

@ -704,6 +704,36 @@ ZTEST(modem_chat, test_runtime_match)
zassert_equal(test_match.wildcards, false); zassert_equal(test_match.wildcards, false);
} }
ZTEST(modem_chat, test_runtime_script_chat)
{
int ret;
struct modem_chat_script_chat test_script_chat;
struct modem_chat_match test_response_matches[2];
modem_chat_script_chat_init(&test_script_chat);
ret = modem_chat_script_chat_set_request(&test_script_chat, "AT345");
zassert_ok(ret, "Failed to set request");
zassert_ok(strcmp(test_script_chat.request, "AT345"), "Failed to set script_chat");
zassert_equal(test_script_chat.request_size, 5, "Failed to set size of script_chat");
ret = modem_chat_script_chat_set_response_matches(&test_script_chat,
test_response_matches,
ARRAY_SIZE(test_response_matches));
zassert_ok(ret, "Failed to set response matches");
zassert_equal(test_script_chat.response_matches, test_response_matches,
"Failed to set response_matches");
zassert_equal(test_script_chat.response_matches_size, ARRAY_SIZE(test_response_matches),
"Failed to set response_matches");
ret = modem_chat_script_chat_set_response_matches(&test_script_chat,
test_response_matches, 0);
zassert_equal(ret, -EINVAL, "Should have failed to set response matches");
ret = modem_chat_script_chat_set_response_matches(&test_script_chat, NULL, 1);
zassert_equal(ret, -EINVAL, "Should have failed to set response matches");
}
/*************************************************************************************************/ /*************************************************************************************************/
/* Test suite */ /* Test suite */
/*************************************************************************************************/ /*************************************************************************************************/