samples: net: dumb_http_server_mt: Add connection mgr support Start the listeners after the "connected" event from connection manager has been received. Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
diff --git a/samples/net/sockets/dumb_http_server_mt/prj.conf b/samples/net/sockets/dumb_http_server_mt/prj.conf index 73fbb32..4464f05 100644 --- a/samples/net/sockets/dumb_http_server_mt/prj.conf +++ b/samples/net/sockets/dumb_http_server_mt/prj.conf
@@ -15,6 +15,7 @@ CONFIG_NET_MAX_CONN=20 CONFIG_NET_MAX_CONTEXTS=20 CONFIG_NET_STATISTICS=y +CONFIG_NET_CONNECTION_MANAGER=y # Network buffers CONFIG_NET_PKT_RX_COUNT=96
diff --git a/samples/net/sockets/dumb_http_server_mt/src/main.c b/samples/net/sockets/dumb_http_server_mt/src/main.c index 28adc71..f3aff87 100644 --- a/samples/net/sockets/dumb_http_server_mt/src/main.c +++ b/samples/net/sockets/dumb_http_server_mt/src/main.c
@@ -14,6 +14,10 @@ #include <net/socket.h> #include <net/tls_credentials.h> +#include <net/net_mgmt.h> +#include <net/net_event.h> +#include <net/net_conn_mgr.h> + #define MY_PORT 8080 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) @@ -58,6 +62,9 @@ static k_tid_t tcp6_handler_tid[CONFIG_NET_SAMPLE_NUM_HANDLERS]; #endif +static struct net_mgmt_event_callback mgmt_cb; +static bool connected; +K_SEM_DEFINE(run_app, 0, 1); K_SEM_DEFINE(quit_lock, 0, 1); static bool running_status; static bool want_to_quit; @@ -77,6 +84,44 @@ process_tcp6, NULL, NULL, NULL, THREAD_PRIORITY, 0, -1); +#define EVENT_MASK (NET_EVENT_L4_CONNECTED | \ + NET_EVENT_L4_DISCONNECTED) + +static void event_handler(struct net_mgmt_event_callback *cb, + uint32_t mgmt_event, struct net_if *iface) +{ + if ((mgmt_event & EVENT_MASK) != mgmt_event) { + return; + } + + if (want_to_quit) { + k_sem_give(&run_app); + want_to_quit = false; + } + + if (mgmt_event == NET_EVENT_L4_CONNECTED) { + LOG_INF("Network connected"); + + connected = true; + k_sem_give(&run_app); + + return; + } + + if (mgmt_event == NET_EVENT_L4_DISCONNECTED) { + if (connected == false) { + LOG_INF("Waiting network to be connected"); + } else { + LOG_INF("Network disconnected"); + connected = false; + } + + k_sem_reset(&run_app); + + return; + } +} + static ssize_t sendall(int sock, const void *buf, size_t len) { while (len) { @@ -374,6 +419,25 @@ } #endif + if (IS_ENABLED(CONFIG_NET_CONNECTION_MANAGER)) { + net_mgmt_init_event_callback(&mgmt_cb, + event_handler, EVENT_MASK); + net_mgmt_add_event_callback(&mgmt_cb); + + net_conn_mgr_resend_status(); + } + + if (!IS_ENABLED(CONFIG_NET_CONNECTION_MANAGER)) { + /* If the config library has not been configured to start the + * app only after we have a connection, then we can start + * it right away. + */ + k_sem_give(&run_app); + } + + /* Wait for the connection. */ + k_sem_take(&run_app, K_FOREVER); + start_listener(); k_sem_take(&quit_lock, K_FOREVER);