1616*/
1717#include " mbed.h"
1818
19- #if !defined(MBED_CONF_NSAPI_SOCKET_STATS_ENABLE)
19+ #if !defined(MBED_CONF_NSAPI_SOCKET_STATS_ENABLE)
2020#error [NOT_SUPPORTED] Socket Statistics not supported
2121#endif
2222
2323#define SAMPLE_TIME 10 // milli-sec
24+ #define COMPLETED_FLAG (1UL << 0 )
25+
26+ PlatformMutex stdio_mutex;
27+ EventFlags threadFlag;
2428
2529void print_stats ()
2630{
@@ -30,10 +34,10 @@ void print_stats()
3034
3135 memset (&stats[0 ], 0 , sizeof (mbed_stats_socket_t ) * MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
3236 printf (" %-15s%-15s%-15s%-15s%-15s%-15s%-15s\n " , " Num" , " ID" , " State" , " Proto" , " Sent" , " Recv" , " Time" );
33- while (1 ) {
34-
37+ while (COMPLETED_FLAG != threadFlag.get ()) {
3538 count = SocketStats::mbed_stats_socket_get_each (&stats[0 ], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
3639 for (int i = 0 ; i < count; i++) {
40+ stdio_mutex.lock ();
3741 printf (" \n %-15d" , num);
3842 printf (" %-15p" , stats[i].reference_id );
3943
@@ -63,40 +67,33 @@ void print_stats()
6367 printf (" %-15d" , stats[i].sent_bytes );
6468 printf (" %-15d" , stats[i].recv_bytes );
6569 printf (" %-15lld\n " , stats[i].last_change_tick );
70+ stdio_mutex.unlock ();
6671 }
6772 num++;
6873 ThisThread::sleep_for (SAMPLE_TIME);
6974 }
75+ // Now allow the stats thread to simply exit by itself gracefully.
7076}
7177
7278// Network interface
7379NetworkInterface *net;
7480
7581int main ()
7682{
77- Thread *thread = new Thread (osPriorityNormal1, 2048 );
78- int remaining;
79- int rcount;
80- char *p;
81- char *buffer = new char [256 ];
82- nsapi_size_or_error_t result;
83-
84- thread->start (print_stats);
8583 // Bring up the ethernet interface
8684 printf (" Mbed OS Socket statistics example\n " );
8785
8886#ifdef MBED_MAJOR_VERSION
8987 printf (" Mbed OS version: %d.%d.%d\n\n " , MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
9088#endif
91-
9289 net = NetworkInterface::get_default_instance ();
9390
9491 if (!net) {
95- printf (" Error! No network inteface found.\n " );
92+ printf (" Error! No network interface found.\n " );
9693 return 0 ;
9794 }
9895
99- result = net->connect ();
96+ nsapi_size_or_error_t result = net->connect ();
10097 if (result != 0 ) {
10198 printf (" Error! net->connect() returned: %d\n " , result);
10299 return result;
@@ -110,6 +107,9 @@ int main()
110107 printf (" Netmask: %s\n " , netmask ? netmask : " None" );
111108 printf (" Gateway: %s\n " , gateway ? gateway : " None" );
112109
110+ Thread *thread = new Thread (osPriorityNormal1, 2048 );
111+ thread->start (print_stats);
112+
113113 // Open a socket on the network interface, and create a TCP connection to api.ipify.org
114114 TCPSocket socket;
115115 // Send a simple http request
@@ -118,54 +118,72 @@ int main()
118118
119119 result = socket.open (net);
120120 if (result != 0 ) {
121+ stdio_mutex.lock ();
121122 printf (" Error! socket.open() returned: %d\n " , result);
123+ stdio_mutex.unlock ();
122124 }
123125
126+ int remaining = 256 ;
127+ int rcount = 0 ;
128+ char *buffer = new char [256 ];
129+ char *p = buffer;
130+
124131 result = socket.connect (" api.ipify.org" , 80 );
125132 if (result != 0 ) {
133+ stdio_mutex.lock ();
126134 printf (" Error! socket.connect() returned: %d\n " , result);
135+ stdio_mutex.unlock ();
127136 goto DISCONNECT;
128137 }
129138
130139 // Loop until whole request sent
131140 while (size) {
132141 result = socket.send (sbuffer + result, size);
133142 if (result < 0 ) {
143+ stdio_mutex.lock ();
134144 printf (" Error! socket.send() returned: %d\n " , result);
145+ stdio_mutex.unlock ();
135146 goto DISCONNECT;
136147 }
137148 size -= result;
149+ stdio_mutex.lock ();
138150 printf (" sent %d [%.*s]\n " , result, strstr (sbuffer, " \r\n " ) - sbuffer, sbuffer);
151+ stdio_mutex.unlock ();
139152 }
140153
141- // Receieve an HTTP response and print out the response line
142- remaining = 256 ;
143- rcount = 0 ;
144- p = buffer;
154+ // Receive an HTTP response and print out the response line
145155 while (0 < (result = socket.recv (p, remaining))) {
146156 p += result;
147157 rcount += result;
148158 remaining -= result;
149159 }
150160 if (result < 0 ) {
161+ stdio_mutex.lock ();
151162 printf (" Error! socket.recv() returned: %d\n " , result);
163+ stdio_mutex.unlock ();
152164 goto DISCONNECT;
153165 }
154166 // the HTTP response code
167+ stdio_mutex.lock ();
155168 printf (" recv %d [%.*s]\n " , rcount, strstr (buffer, " \r\n " ) - buffer, buffer);
169+ stdio_mutex.unlock ();
156170
157171 // The api.ipify.org service also gives us the device's external IP address
158172 p = strstr (buffer, " \r\n\r\n " ) + 4 ;
173+ stdio_mutex.lock ();
159174 printf (" External IP address: %.*s\n " , rcount - (p - buffer), p);
160- delete[] buffer ;
175+ stdio_mutex. unlock () ;
161176
162177DISCONNECT:
178+ delete[] buffer;
179+
163180 // Close the socket to return its memory and bring down the network interface
164181 socket.close ();
165182
166- ThisThread::sleep_for (SAMPLE_TIME);
167183 // Bring down the ethernet interface
168184 net->disconnect ();
169- thread->terminate ();
185+ threadFlag.set (COMPLETED_FLAG);
186+ thread->join ();
187+ delete thread;
170188 printf (" Done\n " );
171189}
0 commit comments