An example hot to register a network connection state change callback. Possible state changes are listed on the example.

Supported in Mbed OS version 5.8

main.cpp

Committer:
Przemyslaw Stekiel
Date:
12 months ago
Revision:
6:c66df92cf71b
Parent:
4:987d54985478

File content as of revision 6:c66df92cf71b:


#include "EthernetInterface.h"
#include "mbed.h"
#include "nsapi_types.h"

// Network interface
EthernetInterface eth;
Mutex print_mutex;

void safe_print(const char * msg)
{
	print_mutex.lock();
	printf(msg);
	print_mutex.unlock();
}

void status_callback(nsapi_event_t status, intptr_t param)
{
    safe_print("Connection status changed!\r\n");
    switch(param) {
        case NSAPI_STATUS_LOCAL_UP:
            safe_print("Local IP address set!\r\n");
            break;
        case NSAPI_STATUS_GLOBAL_UP:
            safe_print("Global IP address set!\r\n");
            break;
        case NSAPI_STATUS_DISCONNECTED:
            safe_print("No connection to network!\r\n");
            break;
        case NSAPI_STATUS_CONNECTING:
            safe_print("Connecting to network!\r\n");
            break;
        default:
            safe_print("Not supported\r\n");
            break;
    }
}

int main()
{
    safe_print("Status callback example!\r\n");

    eth.attach(&status_callback);
    eth.set_blocking(false);
    
    safe_print("Connecting...\r\n");
    eth.connect();
    
    safe_print("Connecting started...\r\n");
    wait(10);
    
    safe_print("Disconnect\r\n");
    eth.disconnect();
    
    safe_print("Done\r\n");    
}