Skip to content

Commit 7c9f287

Browse files
bjarki-andreasencarlescufi
authored andcommitted
modem: chat: Start waiting for response after request is sent
This PR makes the modem_chat wait until a chat script chat request has been sent before accepting responses to said request. This helps ensure that an unsolicitet response is not mistaken for a response to a request, which is especially problematic if the chat script chat is using an any match. For example, start chat script sending AT+CGMI, expecting the modem name as a response followed by OK > start script, waiting for response immediately > start sending "AT+CGMI" > receive "+CEREG 1,0" while sending > script accepts "+CEREG 1,0" as the response to "AT+CGMI" > "AT+CGMI" sent > receive "QUECTEL BG95" > receive "OK" script handler got "+CEREG 1,0" instead of "QUECTEL BG95" After this PR: > start script > start sending AT+CGMI > receive "+CEREG 1,0" while sending > "AT+CGMI" sent > start waiting for response > receive "QUECTEL BG95" > script accepts "QUECTEL BG95" as response to "AT+CGMI" > receive "OK" Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 85995e5 commit 7c9f287

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

subsys/modem/modem_chat.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ static void modem_chat_script_stop(struct modem_chat *chat, enum modem_chat_scri
9595
chat->matches[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = NULL;
9696
chat->matches_size[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = 0;
9797

98-
/* Cancel timeout work */
98+
/* Cancel work */
9999
k_work_cancel_delayable(&chat->script_timeout_work);
100+
k_work_cancel_delayable(&chat->script_send_work);
101+
k_work_cancel_delayable(&chat->script_send_timeout_work);
100102

101103
/* Clear script running state */
102104
atomic_clear_bit(&chat->script_state, MODEM_CHAT_SCRIPT_STATE_RUNNING_BIT);
@@ -118,6 +120,21 @@ static void modem_chat_script_send(struct modem_chat *chat)
118120
k_work_schedule(&chat->script_send_work, K_NO_WAIT);
119121
}
120122

123+
static void modem_chat_script_set_response_matches(struct modem_chat *chat)
124+
{
125+
const struct modem_chat_script_chat *script_chat =
126+
&chat->script->script_chats[chat->script_chat_it];
127+
128+
chat->matches[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = script_chat->response_matches;
129+
chat->matches_size[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = script_chat->response_matches_size;
130+
}
131+
132+
static void modem_chat_script_clear_response_matches(struct modem_chat *chat)
133+
{
134+
chat->matches[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = NULL;
135+
chat->matches_size[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = 0;
136+
}
137+
121138
static void modem_chat_script_next(struct modem_chat *chat, bool initial)
122139
{
123140
const struct modem_chat_script_chat *script_chat;
@@ -142,14 +159,13 @@ static void modem_chat_script_next(struct modem_chat *chat, bool initial)
142159

143160
script_chat = &chat->script->script_chats[chat->script_chat_it];
144161

145-
/* Set response command handlers */
146-
chat->matches[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = script_chat->response_matches;
147-
chat->matches_size[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = script_chat->response_matches_size;
148-
149-
/* Check if work must be sent */
162+
/* Check if request must be sent */
150163
if (script_chat->request_size > 0) {
151164
LOG_DBG("sending: %.*s", script_chat->request_size, script_chat->request);
165+
modem_chat_script_clear_response_matches(chat);
152166
modem_chat_script_send(chat);
167+
} else {
168+
modem_chat_script_set_response_matches(chat);
153169
}
154170
}
155171

@@ -322,6 +338,8 @@ static void modem_chat_script_send_handler(struct k_work *item)
322338
} else {
323339
k_work_schedule(&chat->script_send_timeout_work, K_MSEC(timeout));
324340
}
341+
} else {
342+
modem_chat_script_set_response_matches(chat);
325343
}
326344
}
327345

0 commit comments

Comments
 (0)