Browse Source

modem: chat: Add runtime APIs for chat scripts

Add runtime APIs for chat scripts.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
pull/73382/head
Bjarki Arge Andreasen 1 year ago committed by Alberto Escolar
parent
commit
000784b91d
  1. 55
      include/zephyr/modem/chat.h
  2. 48
      subsys/modem/modem_chat.c
  3. 39
      tests/subsys/modem/modem_chat/src/main.c

55
include/zephyr/modem/chat.h

@ -465,6 +465,61 @@ int modem_chat_script_chat_set_response_matches(struct modem_chat_script_chat *s @@ -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

48
subsys/modem/modem_chat.c

@ -1029,3 +1029,51 @@ void modem_chat_script_chat_set_timeout(struct modem_chat_script_chat *script_ch @@ -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;
}

39
tests/subsys/modem/modem_chat/src/main.c

@ -734,6 +734,45 @@ ZTEST(modem_chat, test_runtime_script_chat) @@ -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 */
/*************************************************************************************************/

Loading…
Cancel
Save