forked from ARMmbed/mbed-os-example-socket-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (143 loc) · 5.11 KB
/
main.cpp
File metadata and controls
167 lines (143 loc) · 5.11 KB
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
/* 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_ENABLE)
#error [NOT_SUPPORTED] Socket Statistics not supported
#endif
#define SAMPLE_TIME 10 // milli-sec
void print_stats()
{
mbed_stats_socket_t stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
static int num = 0;
int count;
memset(&stats[0], 0, sizeof(mbed_stats_socket_t) * MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
printf("%-15s%-15s%-15s%-15s%-15s%-15s%-15s\n", "Num", "ID", "State", "Proto", "Sent", "Recv", "Time");
while (1) {
count = SocketStats::mbed_stats_socket_get_each(&stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
for (int i = 0; i < count; i++) {
printf("\n%-15d", num);
printf("%-15p", stats[i].reference_id);
switch (stats[i].state) {
case SOCK_CLOSED:
printf("%-15s", "Closed"); break;
case SOCK_OPEN:
printf("%-15s", "Open"); break;
case SOCK_CONNECTED:
printf("%-15s", "Connected"); break;
case SOCK_LISTEN:
printf("%-15s", "Listen"); break;
default:
printf("%-15s", "Error"); break;
}
if (NSAPI_TCP == stats[i].proto) {
printf("%-15s", "TCP");
} else {
printf("%-15s", "UDP");
}
printf("%-15d", stats[i].sent_bytes);
printf("%-15d", stats[i].recv_bytes);
printf("%-15lld\n", stats[i].last_change_tick);
}
num++;
ThisThread::sleep_for(SAMPLE_TIME);
}
}
// Network interface
NetworkInterface *net;
int main()
{
Thread *thread = new Thread(osPriorityNormal1, 2048);
int remaining;
int rcount;
char *p;
char *buffer = new char[256];
nsapi_size_or_error_t result;
thread->start(print_stats);
// Bring up the ethernet interface
printf("Mbed OS Socket example\n");
#ifdef MBED_MAJOR_VERSION
printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
net = NetworkInterface::get_default_instance();
if (!net) {
printf("Error! No network inteface found.\n");
return 0;
}
result = net->connect();
if (result != 0) {
printf("Error! net->connect() returned: %d\n", result);
return result;
}
// Show the network address
const char *ip = net->get_ip_address();
const char *netmask = net->get_netmask();
const char *gateway = net->get_gateway();
printf("IP address: %s\n", ip ? ip : "None");
printf("Netmask: %s\n", netmask ? netmask : "None");
printf("Gateway: %s\n", gateway ? gateway : "None");
// 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) {
printf("Error! socket.open() returned: %d\n", result);
}
result = socket.connect("api.ipify.org", 80);
if (result != 0) {
printf("Error! socket.connect() returned: %d\n", result);
goto DISCONNECT;
}
// Loop until whole request sent
while(size) {
result = socket.send(sbuffer+result, size);
if (result < 0) {
printf("Error! socket.send() returned: %d\n", result);
goto DISCONNECT;
}
size -= result;
printf("sent %d [%.*s]\n", result, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
}
// Receieve an HTTP response and print out the response line
remaining = 256;
rcount = 0;
p = buffer;
while (0 < (result = socket.recv(p, remaining))) {
p += result;
rcount += result;
remaining -= result;
}
if (result < 0) {
printf("Error! socket.recv() returned: %d\n", result);
goto DISCONNECT;
}
// the HTTP response code
printf("recv %d [%.*s]\n", rcount, strstr(buffer, "\r\n")-buffer, buffer);
// The api.ipify.org service also gives us the device's external IP address
p = strstr(buffer, "\r\n\r\n")+4;
printf("External IP address: %.*s\n", rcount-(p-buffer), p);
delete[] buffer;
DISCONNECT:
// Close the socket to return its memory and bring down the network interface
socket.close();
ThisThread::sleep_for(SAMPLE_TIME);
// Bring down the ethernet interface
net->disconnect();
thread->terminate();
printf("Done\n");
}