|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Embeint Inc |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +#include <zephyr/ztest.h> |
| 10 | +#include <zephyr/net/net_if.h> |
| 11 | +#include <zephyr/net/net_event.h> |
| 12 | +#include <zephyr/net/conn_mgr_monitor.h> |
| 13 | +#include <zephyr/net/conn_mgr_connectivity.h> |
| 14 | + |
| 15 | +K_SEM_DEFINE(l4_connected, 0, 1); |
| 16 | +K_SEM_DEFINE(l4_disconnected, 0, 1); |
| 17 | + |
| 18 | +static void l4_event_handler(struct net_mgmt_event_callback *cb, uint32_t event, |
| 19 | + struct net_if *iface) |
| 20 | +{ |
| 21 | + switch (event) { |
| 22 | + case NET_EVENT_L4_CONNECTED: |
| 23 | + k_sem_give(&l4_connected); |
| 24 | + break; |
| 25 | + case NET_EVENT_L4_DISCONNECTED: |
| 26 | + k_sem_give(&l4_disconnected); |
| 27 | + break; |
| 28 | + default: |
| 29 | + break; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +ZTEST(conn_mgr_nsos, test_conn_mgr_nsos_opt) |
| 34 | +{ |
| 35 | + struct net_if *iface = net_if_get_default(); |
| 36 | + k_timeout_t conn_delay; |
| 37 | + size_t optlen; |
| 38 | + |
| 39 | + /* Default delay is 1 second */ |
| 40 | + optlen = sizeof(conn_delay); |
| 41 | + zassert_equal(0, conn_mgr_if_get_opt(iface, 0, &conn_delay, &optlen)); |
| 42 | + zassert_equal(sizeof(conn_delay), optlen); |
| 43 | + zassert_true(K_TIMEOUT_EQ(K_SECONDS(1), conn_delay)); |
| 44 | + |
| 45 | + /* Delay can be updated */ |
| 46 | + conn_delay = K_SECONDS(2); |
| 47 | + zassert_equal(0, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay))); |
| 48 | + optlen = sizeof(conn_delay); |
| 49 | + zassert_equal(0, conn_mgr_if_get_opt(iface, 0, &conn_delay, &optlen)); |
| 50 | + zassert_equal(sizeof(conn_delay), optlen); |
| 51 | + zassert_true(K_TIMEOUT_EQ(K_SECONDS(2), conn_delay)); |
| 52 | + |
| 53 | + /* Reset to 1 second */ |
| 54 | + conn_delay = K_SECONDS(1); |
| 55 | + zassert_equal(0, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay))); |
| 56 | +} |
| 57 | + |
| 58 | +ZTEST(conn_mgr_nsos, test_conn_mgr_nsos_opt_error) |
| 59 | +{ |
| 60 | + struct net_if *iface = net_if_get_default(); |
| 61 | + k_timeout_t conn_delay = K_SECONDS(1); |
| 62 | + size_t optlen; |
| 63 | + |
| 64 | + /* Bad option */ |
| 65 | + optlen = sizeof(conn_delay); |
| 66 | + zassert_equal(-EINVAL, conn_mgr_if_get_opt(iface, 1, &conn_delay, &optlen)); |
| 67 | + zassert_equal(-EINVAL, conn_mgr_if_set_opt(iface, 1, &conn_delay, sizeof(conn_delay))); |
| 68 | + |
| 69 | + /* Bad output size */ |
| 70 | + optlen = sizeof(conn_delay) - 1; |
| 71 | + zassert_equal(-EINVAL, conn_mgr_if_get_opt(iface, 0, &conn_delay, &optlen)); |
| 72 | + zassert_equal(-EINVAL, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay) - 1)); |
| 73 | +} |
| 74 | + |
| 75 | +ZTEST(conn_mgr_nsos, test_conn_mgr_nsos) |
| 76 | +{ |
| 77 | + struct net_if *iface = net_if_get_default(); |
| 78 | + k_timeout_t conn_delay, conn_delay_default; |
| 79 | + size_t optlen = sizeof(conn_delay_default); |
| 80 | + |
| 81 | + /* Store default delay */ |
| 82 | + zassert_equal(0, conn_mgr_if_get_opt(iface, 0, &conn_delay_default, &optlen)); |
| 83 | + |
| 84 | + /* Not connecting by default */ |
| 85 | + zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_SECONDS(2))); |
| 86 | + |
| 87 | + /* Trigger the connection */ |
| 88 | + zassert_equal(0, conn_mgr_if_connect(iface)); |
| 89 | + |
| 90 | + /* Default time to connection is 1 second */ |
| 91 | + zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_MSEC(950))); |
| 92 | + zassert_equal(0, k_sem_take(&l4_connected, K_MSEC(100))); |
| 93 | + zassert_true(net_if_is_up(iface)); |
| 94 | + |
| 95 | + /* Small sleep to allow for network stack to return to idle */ |
| 96 | + k_sleep(K_MSEC(500)); |
| 97 | + |
| 98 | + /* Disconnect (actioned immediately) */ |
| 99 | + zassert_equal(0, conn_mgr_if_disconnect(iface)); |
| 100 | + zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(100))); |
| 101 | + zassert_false(net_if_is_up(iface)); |
| 102 | + |
| 103 | + /* Try again with custom timeout */ |
| 104 | + conn_delay = K_MSEC(500); |
| 105 | + zassert_equal(0, conn_mgr_if_set_opt(iface, 0, &conn_delay, sizeof(conn_delay))); |
| 106 | + |
| 107 | + /* Trigger the connection */ |
| 108 | + zassert_equal(0, conn_mgr_if_connect(iface)); |
| 109 | + |
| 110 | + /* Should connect after 500ms this time */ |
| 111 | + zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_MSEC(450))); |
| 112 | + zassert_equal(0, k_sem_take(&l4_connected, K_MSEC(100))); |
| 113 | + zassert_true(net_if_is_up(iface)); |
| 114 | + |
| 115 | + /* Small sleep to allow for network stack to return to idle */ |
| 116 | + k_sleep(K_MSEC(500)); |
| 117 | + |
| 118 | + /* Disconnect (actioned immediately) */ |
| 119 | + zassert_equal(0, conn_mgr_if_disconnect(iface)); |
| 120 | + zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(100))); |
| 121 | + zassert_false(net_if_is_up(iface)); |
| 122 | + |
| 123 | + /* Reset to default value */ |
| 124 | + zassert_equal( |
| 125 | + 0, conn_mgr_if_set_opt(iface, 0, &conn_delay_default, sizeof(conn_delay_default))); |
| 126 | +} |
| 127 | + |
| 128 | +static void test_init(void *state) |
| 129 | +{ |
| 130 | + k_sem_take(&l4_connected, K_NO_WAIT); |
| 131 | + k_sem_take(&l4_disconnected, K_NO_WAIT); |
| 132 | +} |
| 133 | + |
| 134 | +static void test_after(void *fixture) |
| 135 | +{ |
| 136 | + conn_mgr_all_if_disconnect(false); |
| 137 | + conn_mgr_all_if_down(false); |
| 138 | +} |
| 139 | + |
| 140 | +static void *testsuite_init(void) |
| 141 | +{ |
| 142 | + static struct net_mgmt_event_callback l4_callback; |
| 143 | + struct net_if *iface = net_if_get_default(); |
| 144 | + struct in_addr addr; |
| 145 | + |
| 146 | + net_mgmt_init_event_callback(&l4_callback, l4_event_handler, |
| 147 | + NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED); |
| 148 | + net_mgmt_add_event_callback(&l4_callback); |
| 149 | + |
| 150 | + conn_mgr_all_if_down(false); |
| 151 | + |
| 152 | + /* Add an IP address so that NET_EVENT_L4_CONNECTED can trigger */ |
| 153 | + net_addr_pton(AF_INET, "192.0.2.1", &addr); |
| 154 | + net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); |
| 155 | + |
| 156 | + return NULL; |
| 157 | +} |
| 158 | + |
| 159 | +ZTEST_SUITE(conn_mgr_nsos, NULL, testsuite_init, test_init, test_after, NULL); |
0 commit comments