|
12 | 12 | #include <zephyr/net/conn_mgr_monitor.h> |
13 | 13 | #include <zephyr/net/conn_mgr_connectivity.h> |
14 | 14 |
|
| 15 | +#include <zephyr/net/socket.h> |
| 16 | + |
15 | 17 | K_SEM_DEFINE(l4_connected, 0, 1); |
16 | 18 | K_SEM_DEFINE(l4_disconnected, 0, 1); |
17 | 19 |
|
@@ -125,10 +127,69 @@ ZTEST(conn_mgr_nsos, test_conn_mgr_nsos) |
125 | 127 | 0, conn_mgr_if_set_opt(iface, 0, &conn_delay_default, sizeof(conn_delay_default))); |
126 | 128 | } |
127 | 129 |
|
| 130 | +ZTEST(conn_mgr_nsos, test_conn_mgr_nsos_idle) |
| 131 | +{ |
| 132 | + struct net_if *iface = net_if_get_default(); |
| 133 | + struct sockaddr_in v4addr; |
| 134 | + int sock, rc; |
| 135 | + |
| 136 | + /* 2 second idle timeout */ |
| 137 | + conn_mgr_if_set_idle_timeout(iface, 2); |
| 138 | + |
| 139 | + /* Trigger the connection */ |
| 140 | + zassert_equal(0, conn_mgr_if_connect(iface)); |
| 141 | + zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2))); |
| 142 | + |
| 143 | + /* Connection should terminate after 2 seconds due to inactivity */ |
| 144 | + zassert_equal(-EAGAIN, k_sem_take(&l4_disconnected, K_MSEC(1900))); |
| 145 | + zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(500))); |
| 146 | + |
| 147 | + /* Connect again */ |
| 148 | + zassert_equal(0, conn_mgr_if_connect(iface)); |
| 149 | + zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2))); |
| 150 | + |
| 151 | + /* Send data after a second (to localhost) */ |
| 152 | + rc = zsock_inet_pton(AF_INET, "127.0.0.1", (void *)&v4addr); |
| 153 | + zassert_equal(1, rc); |
| 154 | + v4addr.sin_family = AF_INET; |
| 155 | + v4addr.sin_port = htons(1234); |
| 156 | + |
| 157 | + sock = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 158 | + rc = zsock_sendto(sock, "TEST", 4, 0, (const struct sockaddr *)&v4addr, sizeof(v4addr)); |
| 159 | + zassert_equal(4, rc); |
| 160 | + |
| 161 | + /* Should have reset the idle timeout */ |
| 162 | + zassert_equal(-EAGAIN, k_sem_take(&l4_disconnected, K_MSEC(1900))); |
| 163 | + zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(500))); |
| 164 | + |
| 165 | + /* Set the interface to persistent */ |
| 166 | + conn_mgr_if_set_flag(iface, CONN_MGR_IF_PERSISTENT, true); |
| 167 | + |
| 168 | + /* Trigger the connection */ |
| 169 | + zassert_equal(0, conn_mgr_if_connect(iface)); |
| 170 | + zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2))); |
| 171 | + |
| 172 | + /* Interface should disconnect due to idle */ |
| 173 | + zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(2100))); |
| 174 | + /* But it should also come back up automatically */ |
| 175 | + zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2))); |
| 176 | + |
| 177 | + /* Clear the persistent flag, times out and doesn't reconnect */ |
| 178 | + conn_mgr_if_set_flag(iface, CONN_MGR_IF_PERSISTENT, false); |
| 179 | + zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(2100))); |
| 180 | + zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_MSEC(2100))); |
| 181 | + |
| 182 | + /* Cleanup socket */ |
| 183 | + zsock_close(sock); |
| 184 | +} |
| 185 | + |
128 | 186 | static void test_init(void *state) |
129 | 187 | { |
| 188 | + struct net_if *iface = net_if_get_default(); |
| 189 | + |
130 | 190 | k_sem_take(&l4_connected, K_NO_WAIT); |
131 | 191 | k_sem_take(&l4_disconnected, K_NO_WAIT); |
| 192 | + conn_mgr_if_set_idle_timeout(iface, CONN_MGR_IF_NO_TIMEOUT); |
132 | 193 | } |
133 | 194 |
|
134 | 195 | static void test_after(void *fixture) |
|
0 commit comments