From 000784b91d837a98559185809439f20ec77aabbc Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Thu, 29 Feb 2024 11:06:18 +0100 Subject: [PATCH] modem: chat: Add runtime APIs for chat scripts Add runtime APIs for chat scripts. Signed-off-by: Bjarki Arge Andreasen --- include/zephyr/modem/chat.h | 55 ++++++++++++++++++++++++ subsys/modem/modem_chat.c | 48 +++++++++++++++++++++ tests/subsys/modem/modem_chat/src/main.c | 39 +++++++++++++++++ 3 files changed, 142 insertions(+) diff --git a/include/zephyr/modem/chat.h b/include/zephyr/modem/chat.h index be5cf4b8f11..98778fb8841 100644 --- a/include/zephyr/modem/chat.h +++ b/include/zephyr/modem/chat.h @@ -465,6 +465,61 @@ int modem_chat_script_chat_set_response_matches(struct modem_chat_script_chat *s void modem_chat_script_chat_set_timeout(struct modem_chat_script_chat *script_chat, uint16_t timeout_ms); +/** + * @brief Initialize modem chat script + * @param script Modem chat script instance + */ +void modem_chat_script_init(struct modem_chat_script *script); + +/** + * @brief Set modem chat script name + * @param script Modem chat script instance + * @param name Name to set + * @note The lifetime of name must match or exceed the lifetime of script + */ +void modem_chat_script_set_name(struct modem_chat_script *script, const char *name); + +/** + * @brief Set modem chat script chats + * @param script Modem chat script instance + * @param script_chats Chat script array to set + * @param script_chats_size Size of chat script array + * @note The lifetime of script_chats must match or exceed the lifetime of script + * + * @retval 0 if successful, negative errno code otherwise + */ +int modem_chat_script_set_script_chats(struct modem_chat_script *script, + const struct modem_chat_script_chat *script_chats, + uint16_t script_chats_size); + +/** + * @brief Set modem chat script abort matches + * @param script Modem chat script instance + * @param abort_matches Abort match array to set + * @param abort_matches_size Size of abort match array + * @note The lifetime of abort_matches must match or exceed the lifetime of script + * + * @retval 0 if successful, negative errno code otherwise + */ +int modem_chat_script_set_abort_matches(struct modem_chat_script *script, + const struct modem_chat_match *abort_matches, + uint16_t abort_matches_size); + +/** + * @brief Set modem chat script callback + * @param script Modem chat script instance + * @param callback Callback to set + */ +void modem_chat_script_set_callback(struct modem_chat_script *script, + modem_chat_script_callback callback); + +/** + * @brief Set modem chat script timeout + * @param script Modem chat script instance + * @param timeout_s Timeout in seconds + */ +void modem_chat_script_set_timeout(struct modem_chat_script *script, uint32_t timeout_s); + #ifdef __cplusplus } #endif diff --git a/subsys/modem/modem_chat.c b/subsys/modem/modem_chat.c index ffd0fadf465..f0c580f11ec 100644 --- a/subsys/modem/modem_chat.c +++ b/subsys/modem/modem_chat.c @@ -1029,3 +1029,51 @@ void modem_chat_script_chat_set_timeout(struct modem_chat_script_chat *script_ch { script_chat->timeout = timeout; } + +void modem_chat_script_init(struct modem_chat_script *script) +{ + memset(script, 0, sizeof(struct modem_chat_script)); + script->name = ""; +} + +void modem_chat_script_set_name(struct modem_chat_script *script, const char *name) +{ + script->name = name; +} + +int modem_chat_script_set_script_chats(struct modem_chat_script *script, + const struct modem_chat_script_chat *script_chats, + uint16_t script_chats_size) +{ + if (!modem_chat_validate_array(script_chats, script_chats_size)) { + return -EINVAL; + } + + script->script_chats = script_chats; + script->script_chats_size = script_chats_size; + return 0; +} + +int modem_chat_script_set_abort_matches(struct modem_chat_script *script, + const struct modem_chat_match *abort_matches, + uint16_t abort_matches_size) +{ + if (!modem_chat_validate_array(abort_matches, abort_matches_size)) { + return -EINVAL; + } + + script->abort_matches = abort_matches; + script->abort_matches_size = abort_matches_size; + return 0; +} + +void modem_chat_script_set_callback(struct modem_chat_script *script, + modem_chat_script_callback callback) +{ + script->callback = callback; +} + +void modem_chat_script_set_timeout(struct modem_chat_script *script, uint32_t timeout_s) +{ + script->timeout = timeout_s; +} diff --git a/tests/subsys/modem/modem_chat/src/main.c b/tests/subsys/modem/modem_chat/src/main.c index fd5e773b08e..f9883428993 100644 --- a/tests/subsys/modem/modem_chat/src/main.c +++ b/tests/subsys/modem/modem_chat/src/main.c @@ -734,6 +734,45 @@ ZTEST(modem_chat, test_runtime_script_chat) zassert_equal(ret, -EINVAL, "Should have failed to set response matches"); } +ZTEST(modem_chat, test_runtime_script) +{ + int ret; + struct modem_chat_script test_script; + struct modem_chat_script_chat test_script_chats[2]; + struct modem_chat_match test_abort_matches[2]; + + modem_chat_script_init(&test_script); + zassert_equal(strlen(test_script.name), 0, "Failed to set default name"); + + ret = modem_chat_script_set_script_chats(&test_script, test_script_chats, + ARRAY_SIZE(test_script_chats)); + zassert_ok(ret, "Failed to set script chats"); + zassert_equal(test_script.script_chats, test_script_chats, + "Failed to set script_chats"); + zassert_equal(test_script.script_chats_size, ARRAY_SIZE(test_script_chats), + "Failed to set script_chats_size"); + + ret = modem_chat_script_set_script_chats(&test_script, test_script_chats, 0); + zassert_equal(ret, -EINVAL, "Should have failed to set script chats"); + + ret = modem_chat_script_set_script_chats(&test_script, NULL, 1); + zassert_equal(ret, -EINVAL, "Should have failed to set script chats"); + + ret = modem_chat_script_set_abort_matches(&test_script, test_abort_matches, + ARRAY_SIZE(test_abort_matches)); + zassert_ok(ret, "Failed to set abort matches"); + zassert_equal(test_script.abort_matches, test_abort_matches, + "Failed to set script_chats"); + zassert_equal(test_script.abort_matches_size, ARRAY_SIZE(test_abort_matches), + "Failed to set script_chats_size"); + + ret = modem_chat_script_set_abort_matches(&test_script, test_abort_matches, 0); + zassert_equal(ret, -EINVAL, "Should have failed to set abort matches"); + + ret = modem_chat_script_set_abort_matches(&test_script, NULL, 1); + zassert_equal(ret, -EINVAL, "Should have failed to set abort matches"); +} + /*************************************************************************************************/ /* Test suite */ /*************************************************************************************************/