modem_chat: Allow timeout script chat commands
Modem chat scripts should be able to add a script command which is simply a delay. The validation of chat scripts currently disallows chat script commands which only have a timeout set. Update modem_chat to allow modem chat script commands which only have a timeout. Additionally do a bit of cleanup to remove duplicate code. Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
This commit is contained in:
parent
aa30d07563
commit
2e9279f369
@ -142,6 +142,42 @@ static void modem_chat_script_clear_response_matches(struct modem_chat *chat)
|
||||
chat->matches_size[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = 0;
|
||||
}
|
||||
|
||||
static bool modem_chat_script_chat_has_request(struct modem_chat *chat)
|
||||
{
|
||||
const struct modem_chat_script_chat *script_chat =
|
||||
&chat->script->script_chats[chat->script_chat_it];
|
||||
|
||||
return script_chat->request_size > 0;
|
||||
}
|
||||
|
||||
static bool modem_chat_script_chat_has_matches(struct modem_chat *chat)
|
||||
{
|
||||
const struct modem_chat_script_chat *script_chat =
|
||||
&chat->script->script_chats[chat->script_chat_it];
|
||||
|
||||
return script_chat->response_matches_size > 0;
|
||||
}
|
||||
|
||||
static uint16_t modem_chat_script_chat_get_send_timeout(struct modem_chat *chat)
|
||||
{
|
||||
const struct modem_chat_script_chat *script_chat =
|
||||
&chat->script->script_chats[chat->script_chat_it];
|
||||
|
||||
return script_chat->timeout;
|
||||
}
|
||||
|
||||
static bool modem_chat_script_chat_has_send_timeout(struct modem_chat *chat)
|
||||
{
|
||||
return modem_chat_script_chat_get_send_timeout(chat) > 0;
|
||||
}
|
||||
|
||||
static void modem_chat_script_chat_schedule_send_timeout(struct modem_chat *chat)
|
||||
{
|
||||
uint16_t timeout = modem_chat_script_chat_get_send_timeout(chat);
|
||||
|
||||
k_work_schedule(&chat->script_send_timeout_work, K_MSEC(timeout));
|
||||
}
|
||||
|
||||
static void modem_chat_script_next(struct modem_chat *chat, bool initial)
|
||||
{
|
||||
const struct modem_chat_script_chat *script_chat;
|
||||
@ -166,13 +202,15 @@ static void modem_chat_script_next(struct modem_chat *chat, bool initial)
|
||||
|
||||
script_chat = &chat->script->script_chats[chat->script_chat_it];
|
||||
|
||||
/* Check if request must be sent */
|
||||
if (script_chat->request_size > 0) {
|
||||
/* Continue script */
|
||||
if (modem_chat_script_chat_has_request(chat)) {
|
||||
LOG_DBG("sending: %.*s", script_chat->request_size, script_chat->request);
|
||||
modem_chat_script_clear_response_matches(chat);
|
||||
modem_chat_script_send(chat);
|
||||
} else {
|
||||
} else if (modem_chat_script_chat_has_matches(chat)) {
|
||||
modem_chat_script_set_response_matches(chat);
|
||||
} else {
|
||||
modem_chat_script_chat_schedule_send_timeout(chat);
|
||||
}
|
||||
}
|
||||
|
||||
@ -226,22 +264,6 @@ static void modem_chat_script_abort_handler(struct k_work *item)
|
||||
modem_chat_script_stop(chat, MODEM_CHAT_SCRIPT_RESULT_ABORT);
|
||||
}
|
||||
|
||||
static bool modem_chat_script_chat_is_no_response(struct modem_chat *chat)
|
||||
{
|
||||
const struct modem_chat_script_chat *script_chat =
|
||||
&chat->script->script_chats[chat->script_chat_it];
|
||||
|
||||
return (script_chat->response_matches_size == 0);
|
||||
}
|
||||
|
||||
static uint16_t modem_chat_script_chat_get_send_timeout(struct modem_chat *chat)
|
||||
{
|
||||
const struct modem_chat_script_chat *script_chat =
|
||||
&chat->script->script_chats[chat->script_chat_it];
|
||||
|
||||
return script_chat->timeout;
|
||||
}
|
||||
|
||||
/* Returns true when request part has been sent */
|
||||
static bool modem_chat_send_script_request_part(struct modem_chat *chat)
|
||||
{
|
||||
@ -286,7 +308,6 @@ static bool modem_chat_send_script_request_part(struct modem_chat *chat)
|
||||
static void modem_chat_script_send_handler(struct k_work *item)
|
||||
{
|
||||
struct modem_chat *chat = CONTAINER_OF(item, struct modem_chat, script_send_work);
|
||||
uint16_t timeout;
|
||||
|
||||
if (chat->script == NULL) {
|
||||
return;
|
||||
@ -313,15 +334,12 @@ static void modem_chat_script_send_handler(struct k_work *item)
|
||||
break;
|
||||
}
|
||||
|
||||
if (modem_chat_script_chat_is_no_response(chat)) {
|
||||
timeout = modem_chat_script_chat_get_send_timeout(chat);
|
||||
if (timeout == 0) {
|
||||
modem_chat_script_next(chat, false);
|
||||
} else {
|
||||
k_work_schedule(&chat->script_send_timeout_work, K_MSEC(timeout));
|
||||
}
|
||||
} else {
|
||||
if (modem_chat_script_chat_has_matches(chat)) {
|
||||
modem_chat_script_set_response_matches(chat);
|
||||
} else if (modem_chat_script_chat_has_send_timeout(chat)) {
|
||||
modem_chat_script_chat_schedule_send_timeout(chat);
|
||||
} else {
|
||||
modem_chat_script_next(chat, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -773,7 +791,8 @@ int modem_chat_run_script_async(struct modem_chat *chat, const struct modem_chat
|
||||
/* Validate script commands */
|
||||
for (uint16_t i = 0; i < script->script_chats_size; i++) {
|
||||
if ((script->script_chats[i].request_size == 0) &&
|
||||
(script->script_chats[i].response_matches_size == 0)) {
|
||||
(script->script_chats[i].response_matches_size == 0) &&
|
||||
(script->script_chats[i].timeout == 0)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user