Skip to content

Commit cc4e1d9

Browse files
SeppoTakalocfriedt
authored andcommitted
samples: net: cellular_modem: Use static event handler
Use statically registered event handler. Otherwise we might get stuck waiting for event that has already happened. Network events are not keeping the state. Signed-off-by: Seppo Takalo <[email protected]>
1 parent 7438031 commit cc4e1d9

File tree

1 file changed

+47
-12
lines changed
  • samples/net/cellular_modem/src

1 file changed

+47
-12
lines changed

samples/net/cellular_modem/src/main.c

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,25 @@
1515

1616
#include <zephyr/drivers/cellular.h>
1717

18-
#define SAMPLE_TEST_ENDPOINT_HOSTNAME CONFIG_SAMPLE_CELLULAR_MODEM_ENDPOINT_HOSTNAME
18+
#define L4_EVENT_MASK \
19+
(NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED | NET_EVENT_DNS_SERVER_ADD)
20+
#define SAMPLE_TEST_ENDPOINT_HOSTNAME CONFIG_SAMPLE_CELLULAR_MODEM_ENDPOINT_HOSTNAME
1921
#define SAMPLE_TEST_ENDPOINT_UDP_ECHO_PORT (7780)
2022
#define SAMPLE_TEST_ENDPOINT_UDP_RECEIVE_PORT (7781)
2123
#define SAMPLE_TEST_PACKET_SIZE (1024)
2224
#define SAMPLE_TEST_ECHO_PACKETS (16)
2325
#define SAMPLE_TEST_TRANSMIT_PACKETS (128)
26+
#define L4_CONNECTED 1
27+
#define L4_DNS_ADDED 2
2428

2529
const struct device *modem = DEVICE_DT_GET(DT_ALIAS(modem));
2630

2731
static uint8_t sample_test_packet[SAMPLE_TEST_PACKET_SIZE];
2832
static uint8_t sample_recv_buffer[SAMPLE_TEST_PACKET_SIZE];
2933
static bool sample_test_dns_in_progress;
3034
static struct dns_addrinfo sample_test_dns_addrinfo;
31-
35+
struct net_if *ppp_iface;
36+
K_EVENT_DEFINE(l4_event);
3237
K_SEM_DEFINE(dns_query_sem, 0, 1);
3338

3439
static uint8_t sample_prng_random(void)
@@ -406,9 +411,32 @@ int sample_transmit_packets(struct sockaddr *ai_addr, socklen_t ai_addrlen, uint
406411
return 0;
407412
}
408413

414+
static void l4_event_handler(uint64_t event, struct net_if *iface, void *info, size_t info_length,
415+
void *user_data)
416+
{
417+
if (iface != ppp_iface) {
418+
return;
419+
}
420+
421+
switch (event) {
422+
case NET_EVENT_L4_CONNECTED:
423+
k_event_post(&l4_event, L4_CONNECTED);
424+
break;
425+
case NET_EVENT_DNS_SERVER_ADD:
426+
k_event_post(&l4_event, L4_DNS_ADDED);
427+
break;
428+
case NET_EVENT_L4_DISCONNECTED:
429+
k_event_set(&l4_event, 0);
430+
break;
431+
default:
432+
break;
433+
}
434+
}
435+
436+
NET_MGMT_REGISTER_EVENT_HANDLER(l4_events, L4_EVENT_MASK, l4_event_handler, NULL);
437+
409438
int main(void)
410439
{
411-
struct net_if *const iface = net_if_get_first_by_type(&NET_L2_GET_NAME(PPP));
412440
uint16_t *port;
413441
int ret;
414442

@@ -419,25 +447,33 @@ int main(void)
419447

420448
init_sample_test_packet();
421449

450+
ppp_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(PPP));
451+
422452
printk("Powering on modem\n");
423453
pm_device_action_run(modem, PM_DEVICE_ACTION_RESUME);
424454

425455
printk("Bring up network interface\n");
426-
ret = net_if_up(iface);
456+
ret = net_if_up(ppp_iface);
427457
if (ret < 0) {
428458
printk("Failed to bring up network interface\n");
429459
return -1;
430460
}
431461

432-
printk("Waiting for L4 connected & DNS server added\n");
433-
ret = net_mgmt_event_wait_on_iface(iface, NET_EVENT_L4_CONNECTED | NET_EVENT_DNS_SERVER_ADD,
434-
NULL, NULL, NULL, K_SECONDS(120));
462+
printk("Waiting for L4 connected\n");
463+
ret = k_event_wait(&l4_event, L4_CONNECTED, false, K_SECONDS(120));
435464

436-
if (ret != 0) {
465+
if (ret != L4_CONNECTED) {
437466
printk("L4 was not connected in time\n");
438467
return -1;
439468
}
440469

470+
printk("Waiting for DNS server added\n");
471+
ret = k_event_wait(&l4_event, L4_DNS_ADDED, false, K_SECONDS(10));
472+
if (ret != L4_DNS_ADDED) {
473+
printk("DNS server was not added in time\n");
474+
return -1;
475+
}
476+
441477
printk("Retrieving cellular info\n");
442478
print_cellular_info();
443479

@@ -495,9 +531,8 @@ int main(void)
495531
pm_device_action_run(modem, PM_DEVICE_ACTION_RESUME);
496532

497533
printk("Waiting for L4 connected\n");
498-
ret = net_mgmt_event_wait_on_iface(iface, NET_EVENT_L4_CONNECTED, NULL, NULL, NULL,
499-
K_SECONDS(60));
500-
if (ret != 0) {
534+
ret = k_event_wait(&l4_event, L4_CONNECTED, false, K_SECONDS(120));
535+
if (ret != L4_CONNECTED) {
501536
printk("L4 was not connected in time\n");
502537
return -1;
503538
}
@@ -514,7 +549,7 @@ int main(void)
514549
return -1;
515550
}
516551

517-
ret = net_if_down(iface);
552+
ret = net_if_down(ppp_iface);
518553
if (ret < 0) {
519554
printk("Failed to bring down network interface\n");
520555
return -1;

0 commit comments

Comments
 (0)