-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.cpp
223 lines (187 loc) · 5.91 KB
/
ping.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* This file is part of Wio Helium Monitor.
*
* Wio Helium Monitor is free software created by Paul Pinault aka disk91.
* You can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Wio Helium Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wio LoRaWan Field Tester. If not, see <https://www.gnu.org/licenses/>.
*
* Author : Paul Pinault (disk91.com)
*/
// TODO : to get ping working, the file SeeeD_Arduino_rpcWifi/ping.c
// needs to be modified with the following ping_recv function
//
// The main problem is related to the recv function not respecting the 1000 ms timeout and exiting after 8ms
/*
static void ping_recv(int s) {
char buf[64];
int fromlen, len;
struct sockaddr_in from;
struct ip_hdr *iphdr;
struct icmp_echo_hdr *iecho = NULL;
char ipa[16];
struct timeval begin;
struct timeval end;
uint64_t micros_begin;
uint64_t micros_end;
float elapsed;
// Register begin time
gettimeofday(&begin, NULL);
uint32_t start=millis();
uint32_t stop=start;
// Send
while ((len = recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0 || ((uint32_t)(stop-start)) < 1000 ) {
stop=millis();
if (len >= (int)(sizeof(struct ip_hdr) + sizeof(struct icmp_echo_hdr))) {
// Register end time
gettimeofday(&end, NULL);
/// Get from IP address
ip4_addr_t fromaddr;
inet_addr_to_ip4addr(&fromaddr, &from.sin_addr);
strcpy(ipa, inet_ntoa(fromaddr));
// Get echo
iphdr = (struct ip_hdr *)buf;
iecho = (struct icmp_echo_hdr *)(buf + (IPH_HL(iphdr) * 4));
// Print ....
if ((iecho->id == PING_ID) && ( ntohs(iecho->seqno) == ping_seq_num) ) {
received++;
// Get elapsed time in milliseconds
micros_begin = begin.tv_sec * 1000000;
micros_begin += begin.tv_usec;
micros_end = end.tv_sec * 1000000;
micros_end += end.tv_usec;
elapsed = (float)(micros_end - micros_begin) / (float)1000.0;
// Update statistics
// Mean and variance are computed in an incremental way
if (elapsed < min_time) {
min_time = elapsed;
}
if (elapsed > max_time) {
max_time = elapsed;
}
last_mean_time = mean_time;
mean_time = (((received - 1) * mean_time) + elapsed) / received;
if (received > 1) {
var_time = var_time + ((elapsed - last_mean_time) * (elapsed - mean_time));
}
// Print ...
//rpc_printf("%d bytes from %s: icmp_seq=%d time=%.3f ms\r\n", len, ipa,
// ntohs(iecho->seqno), elapsed
//);
//rpc_printf("%d code %d type %d seq_exp %d dur\r\n",ICMPH_CODE(iecho), ICMPH_TYPE(iecho), ping_seq_num, (stop-start));
return;
} else {
//rpc_printf("receive id %X, seq %d wseq %d\r\n",iecho->id,ntohs(iecho->seqno),ping_seq_num - 1);
// TODO
}
} else {
// rpc_printf("receive len %X",len);
}
}
//stop=millis();
//rpc_printf("### quitting after %d ms\r\n", (stop-start));
if (len < 0) {
// log_d("Request timeout for icmp_seq %d\r\n", ping_seq_num);
}
}
*/
/*
* Garbage collection in erpc_port_freertos
*/
/*
// -------------------------
// Hack it
//#include <Arduino.h>
void * allocHisto[128];
int ptr = 0;
void erpcs_initMallocHisto() {
ptr = 0;
}
void erpc_addMalloc(void * m) {
if ( ptr == 128 ) {
// Serial.printf("Malloc not recorded\r\n");
return;
}
// search for free block
for ( int i = 0 ; i < ptr ; i++ ) {
if ( allocHisto[i] == NULL ) {
allocHisto[i] = m;
return;
}
}
// otherwize add a new one
allocHisto[ptr] = m;
ptr++;
}
void erpc_delMalloc(void * m) {
for ( int i = 0 ; i < ptr ; i++ ) {
if ( allocHisto[i] == m ) {
allocHisto[i] = NULL;
break;
}
}
}
void erpc_cleanMalloc() {
int cleaned = 0;
for ( int i = 0 ; i < ptr ; i++ ) {
if ( allocHisto[i] != NULL ) {
cleaned++;
vPortFree(allocHisto[i]);
}
}
//Serial.printf("Cleaned %d\r\n",cleaned);
ptr = 0;
}
// ----
void *erpc_malloc(size_t size)
{
if(size == 0)
{
return NULL;
}
void *p = pvPortMalloc(size);
// Hack it
if ( p != NULL ) {
erpc_addMalloc(p);
}
// ...
return p;
}
void erpc_free(void *ptr)
{
// Hack ...
erpc_delMalloc(ptr);
// ...
vPortFree(ptr);
}
*/
/** =========================================================
* Modify to make sure the
* erpc_transport_arbirator.cpp
*
* Modification not kept ... as a consequence, trying to execute
* a GET on a non responding server will crash the WIO
* a timeout will make watchdog rebooting ...
*/
/*
erpc_status_t TransportArbitrator::clientReceive(client_token_t token)
{
assert(token != 0 && "invalid client token");
// Convert token to pointer to info struct for this client receive request.
PendingClientInfo *info = reinterpret_cast<PendingClientInfo *>(token);
// Wait on the semaphore until we're signaled.
//info->m_sem.get(Semaphore::kWaitForever);
info->m_sem.get(5000000);
removePendingClient(info);
return kErpcStatus_Success;
}
*/