samples: net: gsm: should be able to start/stop freely

The gsm modem should be able to be started or stopped freely,
independent from the networking state.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
This commit is contained in:
Yong Cong Sin 2022-03-14 22:19:58 +08:00 committed by Anas Nashif
commit e401b0a9f3

View file

@ -18,18 +18,21 @@ LOG_MODULE_REGISTER(sample_gsm_ppp, LOG_LEVEL_DBG);
static const struct device *gsm_dev;
static struct net_mgmt_event_callback mgmt_cb;
static bool connected;
static bool starting = IS_ENABLED(CONFIG_GSM_PPP_AUTOSTART);
static int cmd_sample_modem_suspend(const struct shell *shell,
size_t argc, char *argv[])
{
if (!connected) {
shell_fprintf(shell, SHELL_NORMAL, "Not connected.\n");
ARG_UNUSED(argc);
ARG_UNUSED(argv);
if (!starting) {
shell_fprintf(shell, SHELL_NORMAL, "Modem is already stopped.\n");
return -ENOEXEC;
}
gsm_ppp_stop(gsm_dev);
starting = false;
return 0;
}
@ -37,18 +40,16 @@ static int cmd_sample_modem_suspend(const struct shell *shell,
static int cmd_sample_modem_resume(const struct shell *shell,
size_t argc, char *argv[])
{
if (starting) {
shell_fprintf(shell, SHELL_NORMAL,
"Please wait for network connection.\n");
return -ENOEXEC;
}
ARG_UNUSED(argc);
ARG_UNUSED(argv);
if (connected) {
shell_fprintf(shell, SHELL_NORMAL, "Already connected.\n");
if (starting) {
shell_fprintf(shell, SHELL_NORMAL, "Modem is already started.\n");
return -ENOEXEC;
}
gsm_ppp_start(gsm_dev);
starting = true;
return 0;
}
@ -70,22 +71,21 @@ SHELL_CMD_REGISTER(sample, &sample_commands,
static void event_handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event, struct net_if *iface)
{
ARG_UNUSED(cb);
ARG_UNUSED(iface);
if ((mgmt_event & (NET_EVENT_L4_CONNECTED
| NET_EVENT_L4_DISCONNECTED)) != mgmt_event) {
return;
}
starting = false;
if (mgmt_event == NET_EVENT_L4_CONNECTED) {
LOG_INF("Network connected");
connected = true;
return;
}
if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
LOG_INF("Network disconnected");
connected = false;
return;
}
}