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

Revision:
6:c66df92cf71b
Parent:
4:987d54985478
--- a/main.cpp	Tue Mar 27 10:04:24 2018 +0000
+++ b/main.cpp	Wed Oct 24 13:38:36 2018 +0200
@@ -5,44 +5,52 @@
 
 // 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)
 {
-    printf("Connection status changed!\r\n");
+    safe_print("Connection status changed!\r\n");
     switch(param) {
         case NSAPI_STATUS_LOCAL_UP:
-            printf("Local IP address set!\r\n");
+            safe_print("Local IP address set!\r\n");
             break;
         case NSAPI_STATUS_GLOBAL_UP:
-            printf("Global IP address set!\r\n");
+            safe_print("Global IP address set!\r\n");
             break;
         case NSAPI_STATUS_DISCONNECTED:
-            printf("No connection to network!\r\n");
+            safe_print("No connection to network!\r\n");
             break;
         case NSAPI_STATUS_CONNECTING:
-            printf("Connecting to network!\r\n");
+            safe_print("Connecting to network!\r\n");
             break;
         default:
-            printf("Not supported");
+            safe_print("Not supported\r\n");
             break;
     }
 }
 
 int main()
 {
-    printf("Status callback example!\r\n");
+    safe_print("Status callback example!\r\n");
 
     eth.attach(&status_callback);
     eth.set_blocking(false);
     
-    printf("Connecting...\n");
+    safe_print("Connecting...\r\n");
     eth.connect();
     
-    printf("Connecting started...\n");
+    safe_print("Connecting started...\r\n");
     wait(10);
     
-    printf("Disconnect\n");
+    safe_print("Disconnect\r\n");
     eth.disconnect();
     
-    printf("Done\n");    
+    safe_print("Done\r\n");    
 }