1 /*
2 * Copyright (c) 2020, Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr.h>
8 #include <sys/printk.h>
9 #include <shell/shell.h>
10 #include <drivers/uart.h>
11 #include <net/net_mgmt.h>
12 #include <net/net_event.h>
13 #include <net/net_conn_mgr.h>
14 #include <drivers/gsm_ppp.h>
15
16 #include <logging/log.h>
17 LOG_MODULE_REGISTER(sample_gsm_ppp, LOG_LEVEL_DBG);
18
19 static const struct device *gsm_dev;
20 static struct net_mgmt_event_callback mgmt_cb;
21 static bool connected;
22 static bool starting = true;
23
cmd_sample_modem_suspend(const struct shell * shell,size_t argc,char * argv[])24 static int cmd_sample_modem_suspend(const struct shell *shell,
25 size_t argc, char *argv[])
26 {
27 if (!connected) {
28 shell_fprintf(shell, SHELL_NORMAL, "Not connected.\n");
29 return -ENOEXEC;
30 }
31
32 gsm_ppp_stop(gsm_dev);
33
34 return 0;
35 }
36
cmd_sample_modem_resume(const struct shell * shell,size_t argc,char * argv[])37 static int cmd_sample_modem_resume(const struct shell *shell,
38 size_t argc, char *argv[])
39 {
40 if (starting) {
41 shell_fprintf(shell, SHELL_NORMAL,
42 "Please wait for network connection.\n");
43 return -ENOEXEC;
44 }
45
46 if (connected) {
47 shell_fprintf(shell, SHELL_NORMAL, "Already connected.\n");
48 return -ENOEXEC;
49 }
50
51 gsm_ppp_start(gsm_dev);
52
53 return 0;
54 }
55
56 SHELL_STATIC_SUBCMD_SET_CREATE(sample_commands,
57 SHELL_CMD(resume, NULL,
58 "Resume the modem\n",
59 cmd_sample_modem_resume),
60 SHELL_CMD(suspend, NULL,
61 "Suspend the modem\n",
62 cmd_sample_modem_suspend),
63 SHELL_SUBCMD_SET_END
64 );
65
66 SHELL_CMD_REGISTER(sample, &sample_commands,
67 "Sample application commands", NULL);
68
69
event_handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)70 static void event_handler(struct net_mgmt_event_callback *cb,
71 uint32_t mgmt_event, struct net_if *iface)
72 {
73 if ((mgmt_event & (NET_EVENT_L4_CONNECTED
74 | NET_EVENT_L4_DISCONNECTED)) != mgmt_event) {
75 return;
76 }
77
78 starting = false;
79
80 if (mgmt_event == NET_EVENT_L4_CONNECTED) {
81 LOG_INF("Network connected");
82 connected = true;
83 return;
84 }
85
86 if (mgmt_event == NET_EVENT_L4_DISCONNECTED) {
87 LOG_INF("Network disconnected");
88 connected = false;
89 return;
90 }
91 }
92
main(void)93 int main(void)
94 {
95 const struct device *uart_dev =
96 DEVICE_DT_GET(DT_BUS(DT_INST(0, zephyr_gsm_ppp)));
97
98 gsm_dev = DEVICE_DT_GET(DT_INST(0, zephyr_gsm_ppp));
99
100 LOG_INF("Board '%s' APN '%s' UART '%s' device %p (%s)",
101 CONFIG_BOARD, CONFIG_MODEM_GSM_APN,
102 DT_BUS_LABEL(DT_INST(0, zephyr_gsm_ppp)), uart_dev,
103 gsm_dev->name);
104
105 net_mgmt_init_event_callback(&mgmt_cb, event_handler,
106 NET_EVENT_L4_CONNECTED |
107 NET_EVENT_L4_DISCONNECTED);
108 net_mgmt_add_event_callback(&mgmt_cb);
109
110 return 0;
111 }
112