Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime chat script support for modem_chat module #62703

Conversation

bjarki-andreasen
Copy link
Collaborator

@bjarki-andreasen bjarki-andreasen commented Sep 15, 2023

Introduction

This PR makes it possible to construct modem chat scripts and modify them at runtime.

The problem

Some commands require runtime modification, including the response to said commands.

An example is this one for setting the 1PPS configuration of a GNSS modem:

send: "$PAIR752,3,200*DA"
receive: "$PAIR001,752,0*3B"

The parameters of the commands will change at runtime, so the chat script
needs to be modifiable at runtime.

The solution

Create a template script in RAM, that is modified before being run by the modem_chat instance.

/* Initialize script */
uint8_t match[32];
uint8_t request[32];

struct modem_chat_match match = {
	.match = match,
	.match_size = 0,
	.separators = NULL,
	.separators_size = 0,
	.wildcards = false,
	.partial = false,
	.callback = NULL,
};

struct modem_chat_script_chat script_chat = {
	.request = request,
	.request_size = 0;
	.response_matches = &match,
	.response_matches_size = 1,
	.timeout = 0,
};

struct modem_chat_script script = {
	.name = "config",
	.script_chats = &script_chat,
	.script_chats_size = 1,
	.abort_matches = NULL,
	.abort_matches_size = 0,
	.callback = NULL,
	.timeout = 1,
};

/* Modify script */
int ret = snprintk(match, sizeof(match), "$PAIR752,%u,%u*DA", 3, 200);
match.match_size = ret;
ret = snprintk(request, sizeof(request), "$PAIR001,%03u,0*3B", 752);
script_chat.request_size = ret;

/* Run script */
modem_chat_run_script(&chat, &stack_script);

Additional features

This PR also adds a function to synchronously run a chat script, and updates the naming scheme of the modem_chat module to clearly distinguish between the sync and async variant of the modem_chat_run_script() function, modem_chat_run_script_async().

@bjarki-andreasen bjarki-andreasen force-pushed the feature_add_sync_funcs_to_modem_chat branch from 418bc46 to 7f4c703 Compare September 15, 2023 12:35
Bjarki Arge Andreasen added 2 commits September 15, 2023 15:52
This commit adds one new feature, modem_chat_run_script(),
which synchronously runs a script.

The existing function modem_chat_script_run() is async,
and doesn't follow the naming convention created for the
other modem modules, specifically, all functions are sync,
unless appended with _async.

To preserve backwards compatibility, the existing function
modem_chat_script_run() will remain until deprecated. It
simply calls modem_chat_run_script_async().

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
This commit adds tests to validate the behavior of the
new script run functions.

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
@bjarki-andreasen bjarki-andreasen force-pushed the feature_add_sync_funcs_to_modem_chat branch from 7f4c703 to f560bf4 Compare September 15, 2023 13:53
This commit updates the modem_cellular driver to use the
new naming scheme for the modem_chat functions.

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
@bjarki-andreasen bjarki-andreasen force-pushed the feature_add_sync_funcs_to_modem_chat branch from 16cff87 to d3b0c5e Compare September 15, 2023 18:55
@bjarki-andreasen bjarki-andreasen changed the title Feature add sync funcs to modem chat Add runtime chat script support for modem_chat module Sep 15, 2023
@bjarki-andreasen bjarki-andreasen force-pushed the feature_add_sync_funcs_to_modem_chat branch 2 times, most recently from 03d18d4 to 30a3a2a Compare September 16, 2023 08:18
@bjarki-andreasen bjarki-andreasen marked this pull request as ready for review September 16, 2023 08:21
@bjarki-andreasen bjarki-andreasen self-assigned this Sep 16, 2023
@bjarki-andreasen bjarki-andreasen force-pushed the feature_add_sync_funcs_to_modem_chat branch from 30a3a2a to a0efd27 Compare September 16, 2023 08:59
rerickson1
rerickson1 previously approved these changes Sep 16, 2023
Bjarki Arge Andreasen added 3 commits September 18, 2023 08:31
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 removes const declarations for members
inside structures used for chat scripts to allow for
modifying them at runtime.

Macros like MODEM_CHAT_SCRIPT_DEFINE() cover the
whole struct when declaring them const, ensuring
they remain placed in ROM.

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
This commit adds a test to ensure chat scripts can
be created and modified at runtime.

Signed-off-by: Bjarki Arge Andreasen <bjarkix123@gmail.com>
Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@fabiobaltieri fabiobaltieri merged commit f007f21 into zephyrproject-rtos:main Sep 18, 2023
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants