@@ -18,12 +18,23 @@ LOG_MODULE_REGISTER(net_echo_server_sample, LOG_LEVEL_DBG);
1818#include <net/net_core.h>
1919#include <net/tls_credentials.h>
2020
21+ #include <net/net_mgmt.h>
22+ #include <net/net_event.h>
23+ #include <net/net_conn_mgr.h>
24+
2125#include "common.h"
2226#include "certificate.h"
2327
2428#define APP_BANNER "Run echo server"
2529
2630static struct k_sem quit_lock ;
31+ static struct net_mgmt_event_callback mgmt_cb ;
32+ static bool connected ;
33+ K_SEM_DEFINE (run_app , 0 , 1 );
34+ static bool want_to_quit ;
35+
36+ #define EVENT_MASK (NET_EVENT_L4_CONNECTED | \
37+ NET_EVENT_L4_DISCONNECTED)
2738
2839struct configs conf = {
2940 .ipv4 = {
@@ -39,6 +50,67 @@ void quit(void)
3950 k_sem_give (& quit_lock );
4051}
4152
53+ static void start_udp_and_tcp (void )
54+ {
55+ LOG_INF ("Starting..." );
56+
57+ if (IS_ENABLED (CONFIG_NET_TCP )) {
58+ start_tcp ();
59+ }
60+
61+ if (IS_ENABLED (CONFIG_NET_UDP )) {
62+ start_udp ();
63+ }
64+ }
65+
66+ static void stop_udp_and_tcp (void )
67+ {
68+ LOG_INF ("Stopping..." );
69+
70+ if (IS_ENABLED (CONFIG_NET_UDP )) {
71+ stop_udp ();
72+ }
73+
74+ if (IS_ENABLED (CONFIG_NET_TCP )) {
75+ stop_tcp ();
76+ }
77+ }
78+
79+ static void event_handler (struct net_mgmt_event_callback * cb ,
80+ u32_t mgmt_event , struct net_if * iface )
81+ {
82+ if ((mgmt_event & EVENT_MASK ) != mgmt_event ) {
83+ return ;
84+ }
85+
86+ if (want_to_quit ) {
87+ k_sem_give (& run_app );
88+ want_to_quit = false;
89+ }
90+
91+ if (mgmt_event == NET_EVENT_L4_CONNECTED ) {
92+ LOG_INF ("Network connected" );
93+
94+ connected = true;
95+ k_sem_give (& run_app );
96+
97+ return ;
98+ }
99+
100+ if (mgmt_event == NET_EVENT_L4_DISCONNECTED ) {
101+ if (connected == false) {
102+ LOG_INF ("Waiting network to be connected" );
103+ } else {
104+ LOG_INF ("Network disconnected" );
105+ connected = false;
106+ }
107+
108+ k_sem_reset (& run_app );
109+
110+ return ;
111+ }
112+ }
113+
42114static void init_app (void )
43115{
44116#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS ) || \
@@ -94,12 +166,24 @@ static void init_app(void)
94166 }
95167#endif
96168
169+ if (IS_ENABLED (CONFIG_NET_CONNECTION_MANAGER )) {
170+ net_mgmt_init_event_callback (& mgmt_cb ,
171+ event_handler , EVENT_MASK );
172+ net_mgmt_add_event_callback (& mgmt_cb );
173+
174+ net_conn_mgr_resend_status ();
175+ }
176+
97177 init_vlan ();
98178}
99179
100180static int cmd_sample_quit (const struct shell * shell ,
101181 size_t argc , char * argv [])
102182{
183+ want_to_quit = true;
184+
185+ net_conn_mgr_resend_status ();
186+
103187 quit ();
104188
105189 return 0 ;
@@ -119,23 +203,22 @@ void main(void)
119203{
120204 init_app ();
121205
122- if (IS_ENABLED (CONFIG_NET_TCP )) {
123- start_tcp ();
206+ if (!IS_ENABLED (CONFIG_NET_CONNECTION_MANAGER )) {
207+ /* If the config library has not been configured to start the
208+ * app only after we have a connection, then we can start
209+ * it right away.
210+ */
211+ k_sem_give (& run_app );
124212 }
125213
126- if (IS_ENABLED (CONFIG_NET_UDP )) {
127- start_udp ();
128- }
129-
130- k_sem_take (& quit_lock , K_FOREVER );
214+ /* Wait for the connection. */
215+ k_sem_take (& run_app , K_FOREVER );
131216
132- LOG_INF ( "Stopping..." );
217+ start_udp_and_tcp ( );
133218
134- if (IS_ENABLED (CONFIG_NET_TCP )) {
135- stop_tcp ();
136- }
219+ k_sem_take (& quit_lock , K_FOREVER );
137220
138- if (IS_ENABLED ( CONFIG_NET_UDP ) ) {
139- stop_udp ();
221+ if (connected ) {
222+ stop_udp_and_tcp ();
140223 }
141224}
0 commit comments