diff --git a/src/NetworkHandler.cpp b/src/NetworkHandler.cpp index 24d17c0..ad93c9b 100644 --- a/src/NetworkHandler.cpp +++ b/src/NetworkHandler.cpp @@ -13,6 +13,16 @@ EthernetInterface eth; +// create an atomic boolean which lets us verify if the network is connected or +// not, set to false by default + +std::atomic is_connected = false; + +// declare the status callback function to avoid ordering issues, and make it +// static so no other part of the program can call it + +static void status_callback(nsapi_event_t status, intptr_t param); + // create a function to allow other part of the program to connect to the // network @@ -22,10 +32,23 @@ void network_disconnect(void) { eth.disconnect(); } -// set up a status callback function to monitor for changes +// create a function to set the network interface in async mode and attach the +// status callback. This should only be called by main. -std::atomic is_connected = false; -void status_callback(nsapi_event_t status, intptr_t param) { +void network_init(void) { + eth.set_blocking(false); + tr_debug("Network interface now in async mode."); + eth.attach(&status_callback); + tr_debug("Status callback now attached to monitor changes."); +} + +// function to check weather the network is connected + +bool network_is_connected(void) { return is_connected; } + +// define the status callback to monitor for changes + +static void status_callback(nsapi_event_t status, intptr_t param) { tr_debug("Detected a connection status change."); switch (param) { case NSAPI_STATUS_CONNECTING: @@ -53,20 +76,6 @@ } } -// create a function to set the network interface in async mode and attach the -// status callback. This should only be called by main. - -void network_init(void) { - eth.set_blocking(false); - tr_debug("Network interface now in async mode."); - eth.attach(&status_callback); - tr_debug("Status callback now attached to monitor changes."); -} - -// function to check weather the network is connected - -bool network_is_connected(void) { return is_connected; } - // allow other code to grab the network interface and use it // do not hold on to this interface in case it changes