main.c
8.44 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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) 2015 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief Example application for demonstrating the RIOT network stack
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include "board.h"
#include "periph_conf.h"
#include "periph/gpio.h"
#include "net/sock/udp.h"
#include "net/gnrc/ipv6.h"
#include "net/af.h"
#include "net/sixlowpan.h"
#include "net/gnrc/pktdump.h"
#include "shell.h"
#include "shell_commands.h"
#include "msg.h"
#include "thread.h"
#include "sched.h"
#include "kernel_types.h"
#include "net/netstats.h"
#include "net/ipv6/addr.h"
#include "periph/timer.h"
#include "net/gnrc/ipv6/netif.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netapi.h"
#include "net/netopt.h"
#include "net/gnrc/pkt.h"
#include "net/gnrc/pktbuf.h"
#include "net/gnrc/netif/hdr.h"
#include "net/gnrc/sixlowpan/netif.h"
#include "net/fib.h"
#include "net/gnrc/udp.h"
#include "periph/pwm.h"
#include "od.h"
#include "net/sntp.h"
#include "net/ntp_packet.h"
#ifdef MODULE_SCHEDSTATISTICS
#include "xtimer.h"
#endif
#ifdef MODULE_TLSF
#include "tlsf.h"
#endif
#define MAIN_QUEUE_SIZE (8)
/**
* @brief The maximal expected link layer address length in byte
*/
#define MAX_ADDR_LEN (8U)
/**
* @brief The default IPv6 prefix length if not specified.
*/
#define SC_NETIF_IPV6_DEFAULT_PREFIX_LEN (64)
#define _STACKSIZE (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
#define MSG_TYPE_ISR (0x3456)
#define MAXPAC 100
#define MAXLEN 1024
#define ECHOLEN 1
//addr ipv6 link local node 1: fe80::1210:642c:1432:1702
uint8_t node1[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x10,0x64,0x2c,0x14,0x32,0x17,0x02};
// addr ipv6 link local node 2: fe80::1210:6432:140f:1732
//uint8_t node2[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x10,0x64,0x32,0x14,0x0f,0x17,0x32};
//addr ipv6 link local node 3: fe80::1210:642d:1439:1736
uint8_t node3[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x10,0x64,0x2d,0x14,0x39,0x17,0x36};
char sock_server_stack[THREAD_STACKSIZE_MAIN];
char sock_client_stack[THREAD_STACKSIZE_MAIN];
char sock_time_server_stack[THREAD_STACKSIZE_MAIN];
kernel_pid_t server, client,time_server;
sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
sock_udp_t sock;
sock_udp_ep_t local_ntp = SOCK_IPV6_EP_ANY;
sock_udp_t sock_ntp;
static ntp_packet_t sntp_packet;
void *sock_time_server_thread(void *arg)
{
(void) arg;
local_ntp.port = NTP_PORT;
if (sock_udp_create(&sock_ntp, &local_ntp, NULL, 0) < 0) {
puts("Error creating UDP sock");
return NULL;
}
while (1) {
sock_udp_ep_t remote;
ssize_t res;
if ((res = sock_udp_recv(&sock_ntp,&sntp_packet, sizeof(sntp_packet), SOCK_NO_TIMEOUT,
&remote)) >= 0) {
sntp_packet.receive.seconds=byteorder_htonl( xtimer_now_usec());
sntp_packet.origin.seconds=sntp_packet.transmit.seconds;
sntp_packet.transmit.seconds=byteorder_htonl( xtimer_now_usec());
if (sock_udp_send(&sock_ntp, &sntp_packet, sizeof(sntp_packet), &remote) < 0) {
puts("Error sending reply");
}
}
}
return NULL;
}
typedef struct list {
uint32_t send_time;
char packet[MAXLEN];
}Data;
// calculate the time diff between start and end (in us)
uint32_t delay(uint32_t send_time, uint32_t recv_time)
{
return(recv_time-send_time);
}
// calculate the min/max/average value from an array
void rttd(float t[], int size, uint32_t time)
{
float avg;
float min = t[0];
float max = 0.0;
float sum=0.0;
int i;
for(i=0; i< size; i++){
sum += t[i];
if(t[i] > max) {max = t[i];}
if(t[i] < min) {min = t[i];}
}
avg = sum/(float)size;
if(size!=0)printf("Round-trip delay: min/avg/max = %f/%f/%f ms, time = %f s\n",min*1000,avg*1000,max*1000,(float)time/1000000);
}
void *sock_server_thread(void *arg)
{
(void) arg;
local.port = 1234;
Data buf;
int64_t offset = 0;
int latency;
sock_udp_ep_t server = { .port = NTP_PORT, .family = AF_INET6 };
ipv6_addr_from_str((ipv6_addr_t *)&server.addr, "baad:a555::1702");
if (sock_udp_create(&sock, &local, NULL, 0) < 0)
{
puts("Error creating UDP sock");
return NULL;
}
if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) {
puts("Error in synchronization");
return NULL;
}
offset = sntp_get_offset();
printf("offset: %i \n",(int)offset);
while (1)
{
sock_udp_ep_t remote;
size_t res;
if ((res = sock_udp_recv(&sock,&buf, sizeof(buf),SOCK_NO_TIMEOUT ,&remote)) < 0)
{
puts("Can't receive datagram\n");
}
latency = xtimer_now_usec() + offset - buf.send_time;
printf("latency : %i\n",latency);
//puts("Got a UDP datagram\n");
//send ack
if (sock_udp_send(&sock,&buf,sizeof(buf), &remote) != res)
{
puts("Error sending reply");
}
}
return NULL;
}
void *sock_client_thread(void *arg)
{
(void) arg;
ssize_t res;
Data data;
Data data2;
//int i;
//uint32_t timebuf;
sock_udp_ep_t remote = { .family = AF_INET6 };
remote.port = 1234;
remote.addr.ipv6[0] = 0xba;
remote.addr.ipv6[1] = 0xad;
remote.addr.ipv6[2] = 0xa5;
remote.addr.ipv6[3] = 0x55;
remote.addr.ipv6[14] = 0x17;
remote.addr.ipv6[15] = 0x36;
/* fill up the echo message */
for (i = 0; i < ECHOLEN; i++) {
data.donnees[i] = 'e';
}
for (;;)
{
data.send_time = xtimer_now_usec();
//test=xtimer_now_usec();
printf("Heure server: %" PRIu32 "\n",data.heure_actuelle);
if (sock_udp_send(&sock,&data,sizeof(data), &remote) < 0)
{
puts("Error sending message");
}
if ((res = sock_udp_recv(&sock,&data2,sizeof(data2),1 * US_PER_SEC,NULL)) < 0)
{
if (res == -ETIMEDOUT)
{
puts("Timedout");
}
else
{
puts("Error receiving message");
}
}
else{
//end = xtimer_now_usec();
//timebuf = delay(test,end);
// printf("latency: %" PRIu32 "\n",timebuf);
//printf("heure reception: %" PRIu32 "\n",end);
xtimer_sleep(1);
}
}
return NULL;
}
static void init_interface(void)
{
kernel_pid_t ifs[GNRC_NETIF_NUMOF];
ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED;
ipv6_addr_t tmp_addr= IPV6_ADDR_UNSPECIFIED;
uint8_t hwaddr[MAX_ADDR_LEN];
int res;
gnrc_netif_get(ifs);
//addresses gobales
addr.u8[0] = 0xba;
addr.u8[1] = 0xad;
addr.u8[2] = 0xa5;
addr.u8[3] = 0x55;
res = gnrc_netapi_get(ifs[0], NETOPT_ADDRESS, 0, hwaddr, sizeof(hwaddr));
if (res >= 0) {
addr.u8[14] = *hwaddr;
addr.u8[15] = *(hwaddr+1);
}
memcpy(tmp_addr.u8,addr.u8,IPV6_ADDR_BIT_LEN);
gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST);
/* model ipv6 addr: baad:a555::Hwaddr */
if((addr.u8[14]==0x17)&&(addr.u8[15]==0x02))
{
tmp_addr.u8[14] = 0x17;
tmp_addr.u8[15] = 0x36;
fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node3, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
client=thread_create(sock_client_stack,sizeof(sock_client_stack),8,THREAD_CREATE_STACKTEST,sock_client_thread,NULL,"sock_client_thread");
time_server=thread_create(sock_time_server_stack,sizeof(sock_time_server_stack),6,THREAD_CREATE_STACKTEST,sock_time_server_thread,NULL,"sock_time_server_thread");
}
else if((addr.u8[14]==0x17)&&(addr.u8[15]==0x36))
{
tmp_addr.u8[14] = 0x17;
tmp_addr.u8[15] = 0x02;
fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node1, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE);
server=thread_create(sock_server_stack,sizeof(sock_server_stack),6,THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread");
}
else
{
puts("new node?");
}
}
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
static const shell_command_t shell_commands[] = {
{ NULL, NULL, NULL }
};
int main(void)
{
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
puts("RIOT network stack example application");
init_interface();
/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
/* should be never reached */
return 0;
}