-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.cpp
180 lines (155 loc) · 5.4 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright (c) 2018 Arm Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#if !defined(MBED_CONF_NSAPI_SOCKET_STATS_ENABLED)
#error [NOT_SUPPORTED] Socket Statistics not supported
#endif
#define SAMPLE_TIME_MS 10
#define COMPLETED_FLAG (1UL << 0)
PlatformMutex stdio_mutex;
EventFlags threadFlag;
void print_socket_stats()
{
static mbed_stats_socket_t stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
int iteration = 0;
while (COMPLETED_FLAG != threadFlag.get()) {
int count = SocketStats::mbed_stats_socket_get_each(&stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
for (int i = 0; i < count; i++) {
stdio_mutex.lock();
printf("Iteration: %d ", iteration);
printf(" ID: %p ", stats[i].reference_id);
switch (stats[i].state) {
case SOCK_CLOSED:
printf(" State: Closed ");
break;
case SOCK_OPEN:
printf(" State: Open ");
break;
case SOCK_CONNECTED:
printf(" State: Connected");
break;
case SOCK_LISTEN:
printf(" State: Listen ");
break;
default:
printf(" State: Error ");
break;
}
if (NSAPI_TCP == stats[i].proto) {
printf(" Proto: TCP ");
} else {
printf(" Proto: UDP ");
}
printf(" Sent: %d ", stats[i].sent_bytes);
printf(" Recv: %d ", stats[i].recv_bytes);
printf(" Time: %lld\n", stats[i].last_change_tick);
stdio_mutex.unlock();
}
iteration++;
ThisThread::sleep_for(SAMPLE_TIME_MS);
}
// Now allow the stats thread to simply exit by itself gracefully.
}
// Network interface
NetworkInterface *net;
int main()
{
// Bring up the ethernet interface
printf("Mbed OS Socket statistics example\n");
net = NetworkInterface::get_default_instance();
if (!net) {
printf("Error! No network interface found.\n");
return 0;
}
nsapi_size_or_error_t result = net->connect();
if (result != 0) {
printf("Error! net->connect() returned: %d\n", result);
return result;
}
// Show the network address
SocketAddress a;
net->get_ip_address(&a);
printf("IP address: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");
net->get_netmask(&a);
printf("Netmask: %s\n", a.get_ip_address() ? a.get_ip_address() : "None");
net->get_gateway(&a);
printf("Gateway: %s\n\n", a.get_ip_address() ? a.get_ip_address() : "None");
Thread *thread = new Thread(osPriorityNormal1, 2048);
thread->start(print_socket_stats);
// Open a socket on the network interface, and create a TCP connection to api.ipify.org
TCPSocket socket;
// Send a simple http request
char sbuffer[] = "GET / HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
nsapi_size_t size = strlen(sbuffer);
result = socket.open(net);
if (result != 0) {
stdio_mutex.lock();
printf("Error! socket.open() returned: %d\n", result);
stdio_mutex.unlock();
}
int remaining = 256;
int rcount = 0;
char *buffer = new char[256];
char *p = buffer;
result = NetworkInterface::get_default_instance()->gethostbyname("api.ipify.org", &a);
if (result != NSAPI_ERROR_OK) {
printf("Error! DNS resolution failed with %d\n", result);
goto DISCONNECT;
}
a.set_port(80);
result = socket.connect(a);
if (result != 0) {
stdio_mutex.lock();
printf("Error! socket.connect() returned: %d\n", result);
stdio_mutex.unlock();
goto DISCONNECT;
}
// Loop until whole request sent
while (size) {
result = socket.send(sbuffer + result, size);
if (result < 0) {
stdio_mutex.lock();
printf("Error! socket.send() returned: %d\n", result);
stdio_mutex.unlock();
goto DISCONNECT;
}
size -= result;
}
// Receive an HTTP response and print out the response line
while (0 < (result = socket.recv(p, remaining))) {
p += result;
rcount += result;
remaining -= result;
}
if (result < 0) {
stdio_mutex.lock();
printf("Error! socket.recv() returned: %d\n", result);
stdio_mutex.unlock();
goto DISCONNECT;
}
DISCONNECT:
delete[] buffer;
// Close the socket to return its memory and bring down the network interface
socket.close();
// Bring down the ethernet interface
net->disconnect();
ThisThread::sleep_for(SAMPLE_TIME_MS);
threadFlag.set(COMPLETED_FLAG);
thread->join();
delete thread;
printf("End of Mbed OS Socket statistics example\n");
}