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

Committer:
Przemyslaw Stekiel
Date:
Wed Oct 24 13:38:36 2018 +0200
Revision:
6:c66df92cf71b
Parent:
4:987d54985478
Improve style of printed messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
VeijoPesonen 0:bc6951bb0712 1
VeijoPesonen 0:bc6951bb0712 2 #include "EthernetInterface.h"
VeijoPesonen 0:bc6951bb0712 3 #include "mbed.h"
VeijoPesonen 0:bc6951bb0712 4 #include "nsapi_types.h"
VeijoPesonen 0:bc6951bb0712 5
VeijoPesonen 0:bc6951bb0712 6 // Network interface
VeijoPesonen 0:bc6951bb0712 7 EthernetInterface eth;
Przemyslaw Stekiel 6:c66df92cf71b 8 Mutex print_mutex;
Przemyslaw Stekiel 6:c66df92cf71b 9
Przemyslaw Stekiel 6:c66df92cf71b 10 void safe_print(const char * msg)
Przemyslaw Stekiel 6:c66df92cf71b 11 {
Przemyslaw Stekiel 6:c66df92cf71b 12 print_mutex.lock();
Przemyslaw Stekiel 6:c66df92cf71b 13 printf(msg);
Przemyslaw Stekiel 6:c66df92cf71b 14 print_mutex.unlock();
Przemyslaw Stekiel 6:c66df92cf71b 15 }
VeijoPesonen 0:bc6951bb0712 16
VeijoPesonen 0:bc6951bb0712 17 void status_callback(nsapi_event_t status, intptr_t param)
VeijoPesonen 0:bc6951bb0712 18 {
Przemyslaw Stekiel 6:c66df92cf71b 19 safe_print("Connection status changed!\r\n");
VeijoPesonen 0:bc6951bb0712 20 switch(param) {
VeijoPesonen 0:bc6951bb0712 21 case NSAPI_STATUS_LOCAL_UP:
Przemyslaw Stekiel 6:c66df92cf71b 22 safe_print("Local IP address set!\r\n");
VeijoPesonen 0:bc6951bb0712 23 break;
VeijoPesonen 0:bc6951bb0712 24 case NSAPI_STATUS_GLOBAL_UP:
Przemyslaw Stekiel 6:c66df92cf71b 25 safe_print("Global IP address set!\r\n");
VeijoPesonen 0:bc6951bb0712 26 break;
VeijoPesonen 0:bc6951bb0712 27 case NSAPI_STATUS_DISCONNECTED:
Przemyslaw Stekiel 6:c66df92cf71b 28 safe_print("No connection to network!\r\n");
VeijoPesonen 0:bc6951bb0712 29 break;
VeijoPesonen 0:bc6951bb0712 30 case NSAPI_STATUS_CONNECTING:
Przemyslaw Stekiel 6:c66df92cf71b 31 safe_print("Connecting to network!\r\n");
VeijoPesonen 0:bc6951bb0712 32 break;
VeijoPesonen 0:bc6951bb0712 33 default:
Przemyslaw Stekiel 6:c66df92cf71b 34 safe_print("Not supported\r\n");
VeijoPesonen 0:bc6951bb0712 35 break;
VeijoPesonen 0:bc6951bb0712 36 }
VeijoPesonen 0:bc6951bb0712 37 }
VeijoPesonen 0:bc6951bb0712 38
VeijoPesonen 0:bc6951bb0712 39 int main()
VeijoPesonen 0:bc6951bb0712 40 {
Przemyslaw Stekiel 6:c66df92cf71b 41 safe_print("Status callback example!\r\n");
VeijoPesonen 0:bc6951bb0712 42
VeijoPesonen 0:bc6951bb0712 43 eth.attach(&status_callback);
VeijoPesonen 3:a0e64683327b 44 eth.set_blocking(false);
VeijoPesonen 3:a0e64683327b 45
Przemyslaw Stekiel 6:c66df92cf71b 46 safe_print("Connecting...\r\n");
VeijoPesonen 0:bc6951bb0712 47 eth.connect();
VeijoPesonen 3:a0e64683327b 48
Przemyslaw Stekiel 6:c66df92cf71b 49 safe_print("Connecting started...\r\n");
VeijoPesonen 0:bc6951bb0712 50 wait(10);
VeijoPesonen 3:a0e64683327b 51
Przemyslaw Stekiel 6:c66df92cf71b 52 safe_print("Disconnect\r\n");
VeijoPesonen 0:bc6951bb0712 53 eth.disconnect();
VeijoPesonen 3:a0e64683327b 54
Przemyslaw Stekiel 6:c66df92cf71b 55 safe_print("Done\r\n");
VeijoPesonen 3:a0e64683327b 56 }