modem: chat: Track size of script_chat request to avoid strlen()

This commit optimizes the performance of the script chats by
storing the size of requests with the chat script chat, negating
the need to use strlen() within the modem_chat module every time
a chat script chat request is sent.

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
This commit is contained in:
Bjarki Arge Andreasen 2023-09-16 10:52:22 +02:00 committed by Fabio Baltieri
commit 586504bed8
2 changed files with 32 additions and 24 deletions

View file

@ -35,21 +35,19 @@ typedef void (*modem_chat_match_callback)(struct modem_chat *chat, char **argv,
* @brief Modem chat match
*/
struct modem_chat_match {
/* Match array */
/** Match array */
const uint8_t *match;
/** Size of match */
const uint8_t match_size;
/* Separators array */
/** Separators array */
const uint8_t *separators;
/** Size of separators array */
const uint8_t separators_size;
/* Set if modem chat instance shall use wildcards when matching */
/** Set if modem chat instance shall use wildcards when matching */
const uint8_t wildcards : 1;
/* Set if script shall not continue to next step in case of match */
/** Set if script shall not continue to next step in case of match */
const uint8_t partial : 1;
/* Type of modem chat instance */
/** Type of modem chat instance */
const modem_chat_match_callback callback;
};
@ -90,9 +88,11 @@ struct modem_chat_match {
* @brief Modem chat script chat
*/
struct modem_chat_script_chat {
/** Request to send to modem formatted as char string */
const char *request;
/** Expected responses to request */
/** Request to send to modem */
const uint8_t *request;
/** Size of request */
uint8_t request_size;
/** Array of expected responses to request */
const struct modem_chat_match *const response_matches;
/** Number of elements in expected responses */
const uint16_t response_matches_size;
@ -102,19 +102,28 @@ struct modem_chat_script_chat {
#define MODEM_CHAT_SCRIPT_CMD_RESP(_request, _response_match) \
{ \
.request = _request, .response_matches = &_response_match, \
.response_matches_size = 1, .timeout = 0, \
.request = (uint8_t *)(_request), \
.request_size = (uint8_t)(sizeof(_request) - 1), \
.response_matches = &_response_match, \
.response_matches_size = 1, \
.timeout = 0, \
}
#define MODEM_CHAT_SCRIPT_CMD_RESP_MULT(_request, _response_matches) \
{ \
.request = _request, .response_matches = _response_matches, \
.response_matches_size = ARRAY_SIZE(_response_matches), .timeout = 0, \
.request = (uint8_t *)(_request), \
.request_size = (uint8_t)(sizeof(_request) - 1), \
.response_matches = _response_matches, \
.response_matches_size = ARRAY_SIZE(_response_matches), \
.timeout = 0, \
}
#define MODEM_CHAT_SCRIPT_CMD_RESP_NONE(_request, _timeout) \
{ \
.request = _request, .response_matches = NULL, .response_matches_size = 0, \
.request = (uint8_t *)(_request), \
.request_size = (uint8_t)(sizeof(_request) - 1), \
.response_matches = NULL, \
.response_matches_size = 0, \
.timeout = _timeout, \
}

View file

@ -135,8 +135,8 @@ static void modem_chat_script_next(struct modem_chat *chat, bool initial)
chat->matches_size[MODEM_CHAT_MATCHES_INDEX_RESPONSE] = script_chat->response_matches_size;
/* Check if work must be sent */
if (strlen(script_chat->request) > 0) {
LOG_DBG("sending: %s", script_chat->request);
if (script_chat->request_size > 0) {
LOG_DBG("sending: %.*s", script_chat->request_size, script_chat->request);
modem_chat_script_send(chat);
}
}
@ -196,18 +196,17 @@ static bool modem_chat_script_send_request(struct modem_chat *chat)
const struct modem_chat_script_chat *script_chat =
&chat->script->script_chats[chat->script_chat_it];
uint16_t script_chat_request_size = strlen(script_chat->request);
uint8_t *script_chat_request_start;
uint16_t script_chat_request_remaining;
int ret;
/* Validate data to send */
if (script_chat_request_size == chat->script_send_request_pos) {
if (script_chat->request_size == chat->script_send_request_pos) {
return true;
}
script_chat_request_start = (uint8_t *)&script_chat->request[chat->script_send_request_pos];
script_chat_request_remaining = script_chat_request_size - chat->script_send_request_pos;
script_chat_request_remaining = script_chat->request_size - chat->script_send_request_pos;
/* Send data through pipe */
ret = modem_pipe_transmit(chat->pipe, script_chat_request_start,
@ -222,7 +221,7 @@ static bool modem_chat_script_send_request(struct modem_chat *chat)
chat->script_send_request_pos += (uint16_t)ret;
/* Check if data remains */
if (chat->script_send_request_pos < script_chat_request_size) {
if (chat->script_send_request_pos < script_chat->request_size) {
return false;
}
@ -750,7 +749,7 @@ 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 ((strlen(script->script_chats[i].request) == 0) &&
if ((script->script_chats[i].request_size == 0) &&
(script->script_chats[i].response_matches_size == 0)) {
return -EINVAL;
}