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:
VeijoPesonen
Date:
Mon Feb 26 11:13:01 2018 +0000
Revision:
4:987d54985478
Parent:
3:a0e64683327b
Child:
6:c66df92cf71b
Add print to clarify usage of nonblocking mode

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;
VeijoPesonen 0:bc6951bb0712 8
VeijoPesonen 0:bc6951bb0712 9 void status_callback(nsapi_event_t status, intptr_t param)
VeijoPesonen 0:bc6951bb0712 10 {
VeijoPesonen 0:bc6951bb0712 11 printf("Connection status changed!\r\n");
VeijoPesonen 0:bc6951bb0712 12 switch(param) {
VeijoPesonen 0:bc6951bb0712 13 case NSAPI_STATUS_LOCAL_UP:
VeijoPesonen 0:bc6951bb0712 14 printf("Local IP address set!\r\n");
VeijoPesonen 0:bc6951bb0712 15 break;
VeijoPesonen 0:bc6951bb0712 16 case NSAPI_STATUS_GLOBAL_UP:
VeijoPesonen 0:bc6951bb0712 17 printf("Global IP address set!\r\n");
VeijoPesonen 0:bc6951bb0712 18 break;
VeijoPesonen 0:bc6951bb0712 19 case NSAPI_STATUS_DISCONNECTED:
VeijoPesonen 0:bc6951bb0712 20 printf("No connection to network!\r\n");
VeijoPesonen 0:bc6951bb0712 21 break;
VeijoPesonen 0:bc6951bb0712 22 case NSAPI_STATUS_CONNECTING:
VeijoPesonen 0:bc6951bb0712 23 printf("Connecting to network!\r\n");
VeijoPesonen 0:bc6951bb0712 24 break;
VeijoPesonen 0:bc6951bb0712 25 default:
VeijoPesonen 0:bc6951bb0712 26 printf("Not supported");
VeijoPesonen 0:bc6951bb0712 27 break;
VeijoPesonen 0:bc6951bb0712 28 }
VeijoPesonen 0:bc6951bb0712 29 }
VeijoPesonen 0:bc6951bb0712 30
VeijoPesonen 0:bc6951bb0712 31 int main()
VeijoPesonen 0:bc6951bb0712 32 {
VeijoPesonen 0:bc6951bb0712 33 printf("Status callback example!\r\n");
VeijoPesonen 0:bc6951bb0712 34
VeijoPesonen 0:bc6951bb0712 35 eth.attach(&status_callback);
VeijoPesonen 3:a0e64683327b 36 eth.set_blocking(false);
VeijoPesonen 3:a0e64683327b 37
VeijoPesonen 0:bc6951bb0712 38 printf("Connecting...\n");
VeijoPesonen 0:bc6951bb0712 39 eth.connect();
VeijoPesonen 3:a0e64683327b 40
VeijoPesonen 4:987d54985478 41 printf("Connecting started...\n");
VeijoPesonen 0:bc6951bb0712 42 wait(10);
VeijoPesonen 3:a0e64683327b 43
VeijoPesonen 0:bc6951bb0712 44 printf("Disconnect\n");
VeijoPesonen 0:bc6951bb0712 45 eth.disconnect();
VeijoPesonen 3:a0e64683327b 46
VeijoPesonen 3:a0e64683327b 47 printf("Done\n");
VeijoPesonen 3:a0e64683327b 48 }