e87eb5fb
vrobic
RPL + serveur ntp ok
|
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
|
/*
* 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>
|
93480153
vrobic
modif dynamic_app
|
28
|
#include "periph/gpio.h"
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
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
|
#include "../../boards/stm32f4discovery/include/board.h"
#include "../../boards/stm32f4discovery/include/periph_conf.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 "thread.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"
#include "net/gnrc/rpl.h"
#include "net/gnrc/rpl/structs.h"
#include "net/gnrc/rpl/dodag.h"
#include "utlist.h"
#include "trickle.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)
|
93480153
vrobic
modif dynamic_app
|
87
88
89
|
#define PWM_FREQ 1000
#define PWM_RES 1000
#define DEADLINE 15000
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
extern int udp_cmd(int argc, char **argv);
static const shell_command_t shell_commands[] = {
{ "udp", "send data over UDP and listen on UDP ports", udp_cmd },
{ NULL, NULL, NULL }
};
// addr ipv6 link local node 1: fe80::3734:510e:3317:3402
uint8_t node1[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x37,0x34,0x51,0x0e,0x33,0x17,0x34,0x02};
// addr ipv6 link local node 2: fe80::3634:5110:3473:3762
uint8_t node2[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x34,0x51,0x10,0x34,0x73,0x37,0x62};
// addr ipv6 link local node 3: fe80::3734:510e:330b:342a
|
ceb4c7ca
root
changement carte ...
|
105
|
uint8_t node3[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x31,0x33,0x47,0x11,0x37,0x2c,0x34,0x12};
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// addr ipv6 link local node 4: fe80::3734:510b:330b:340a
uint8_t node4[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x37,0x34,0x51,0x0b,0x33,0x0b,0x34,0x0a};
// addr ipv6 link local node 5: fe80::3734:510b:330a:341e
uint8_t node5[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x37,0x34,0x51,0x0b,0x33,0x0a,0x34,0x1e};
//static char _stack_server[GNRC_PKTDUMP_STACKSIZE];
char pwm_stack[THREAD_STACKSIZE_MAIN];
char sock_server_stack[THREAD_STACKSIZE_MAIN];
char sock_client_stack[THREAD_STACKSIZE_MAIN];
char sock_time_server_stack[THREAD_STACKSIZE_MAIN];
// static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
// KERNEL_PID_UNDEF);
//
// kernel_pid_t gnrc_server_pid = KERNEL_PID_UNDEF;
//
kernel_pid_t server, client, time_server;
int ordre = 0;
int64_t offset = 0;
int timer_run = 0;
int tourne = 0;
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;
|
93480153
vrobic
modif dynamic_app
|
129
|
static ntp_packet_t sntp_packet;
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
130
131
|
typedef struct tableau {
|
93480153
vrobic
modif dynamic_app
|
132
133
|
uint32_t heure_actuelle;
char donnees[2];
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
134
135
136
137
138
139
140
141
|
}Data;
/***************** RPL functions *****************/
int crea_rpl_dodag_root(char *arg1, char *arg2)
{
uint8_t instance_id = (uint8_t) atoi(arg1);
ipv6_addr_t dodag_id;
|
93480153
vrobic
modif dynamic_app
|
142
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
143
144
145
146
|
if (ipv6_addr_from_str(&dodag_id, arg2) == NULL) {
puts("error: <dodag_id> must be a valid IPv6 address");
return 1;
}
|
93480153
vrobic
modif dynamic_app
|
147
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
148
149
150
151
152
|
gnrc_rpl_instance_t *inst = NULL;
inst = gnrc_rpl_root_init(instance_id, &dodag_id, false, false);
if (inst == NULL) {
char addr_str[IPV6_ADDR_MAX_STR_LEN];
printf("error: could not add DODAG (%s) to instance (%d)\n",
|
93480153
vrobic
modif dynamic_app
|
153
|
ipv6_addr_to_str(addr_str, &dodag_id, sizeof(addr_str)), instance_id);
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
154
155
|
return 1;
}
|
93480153
vrobic
modif dynamic_app
|
156
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
157
158
159
|
printf("successfully added a new RPL DODAG\n");
return 0;
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
160
161
|
/***************** /RPL functions ****************/
|
93480153
vrobic
modif dynamic_app
|
162
|
void *sock_time_server_thread(void *arg)
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
163
164
165
|
{
(void) arg;
local_ntp.port = NTP_PORT;
|
93480153
vrobic
modif dynamic_app
|
166
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
167
|
if (sock_udp_create(&sock_ntp, &local_ntp, NULL, 0) < 0) {
|
93480153
vrobic
modif dynamic_app
|
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
|
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) {
puts("Received a message");
//printf("TT: %lu\n", byteorder_ntohl(sntp_packet.transmit.seconds));
// printf("%c\n",remote.addr.ipv6[15]);
//xtimer_ticks64_t now = xtimer_now64();
// heure actuelle du serveur
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());
//printf("heure actuelle : %lu\n",xtimer_now_usec());
//printf("TT2: %lu\n", byteorder_ntohl(sntp_packet.transmit.seconds));
//memset(&sntp_packet, 0, sizeof(sntp_packet));
//ntp_packet_set_vn(&sntp_packet);
//ntp_packet_set_mode(&sntp_packet, NTP_MODE_SERVER);
if (sock_udp_send(&sock_ntp, &sntp_packet, sizeof(sntp_packet), &remote) < 0) {
puts("Error sending reply");
}
}
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
197
|
return NULL;
|
93480153
vrobic
modif dynamic_app
|
198
|
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
199
|
|
ceb4c7ca
root
changement carte ...
|
200
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
201
202
203
204
|
void *sock_server_thread(void *arg)
{
(void) arg;
Data buf;
|
93480153
vrobic
modif dynamic_app
|
205
206
|
int compteur = 5;
int deadline;
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
207
208
209
210
211
|
local.port = 1234;
sock_udp_ep_t server = { .port = NTP_PORT, .family = AF_INET6 };
ipv6_addr_from_str((ipv6_addr_t *)&server.addr, "dead:beef::3402");
if (sock_udp_create(&sock, &local, NULL, 0) < 0) {
|
93480153
vrobic
modif dynamic_app
|
212
213
214
215
216
|
puts("Error creating UDP sock");
return NULL;
}
if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) {
puts("Error in synchronization");
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
217
|
}
|
93480153
vrobic
modif dynamic_app
|
218
219
|
offset = sntp_get_offset();
printf("offset : %i\n",(int)offset);
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
220
221
|
while (1) {
|
93480153
vrobic
modif dynamic_app
|
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
|
sock_udp_ep_t remote;
ssize_t res;
if ((res = sock_udp_recv(&sock, &buf, sizeof(buf), 5 * SEC_IN_USEC,&remote)) >= 0) {
deadline = xtimer_now_usec() + offset - buf.heure_actuelle;
printf("tps de transmission : %i\n",deadline);
if(buf.donnees[0] == 'g' && buf.donnees[1] == 'o') {
if(deadline >= DEADLINE && compteur >=5) {
compteur = 0;
if(tourne == 1){
pwm_set(PWM_DEV(0),1,987);
pwm_set(PWM_DEV(1),1,960);
}
if(timer_run == 0) {
if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) {
puts("Error sending reply");
}
gpio_clear(LED1_PIN);
gpio_clear(LED2_PIN);
gpio_set(LED0_PIN);
timer_set(TIMER_DEV(1),0,25200);
printf("relance timer\n");
if (sntp_sync(&server, SOCK_NO_TIMEOUT) < 0) {
puts("Error in synchronization");
return NULL;
}
offset = sntp_get_offset();
printf("offset : %i\n",(int)offset);
timer_run = 1;
}
}
if(deadline >= DEADLINE && compteur < 5) {
compteur = 0;
}
if(deadline<=DEADLINE && compteur >=5) {
pwm_set(PWM_DEV(0),1,992);
pwm_set(PWM_DEV(1),1,658);
tourne = 1;
timer_run=0;
timer_clear(TIMER_DEV(1),0);
printf("clear timer\n");
if (sock_udp_send(&sock, "n", sizeof("n"), &remote) < 0) {
puts("Error sending reply");
}
gpio_clear(LED2_PIN);
gpio_clear(LED0_PIN);
gpio_set(LED1_PIN);
}
if(deadline<=DEADLINE && compteur <5) {
compteur++;
}
printf("compteur : %d\n",compteur);
memset(&buf,0,sizeof(buf));
}
}else{
puts("msg non recu");
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
279
280
281
282
283
|
}
return NULL;
}
|
ceb4c7ca
root
changement carte ...
|
284
|
void *sock_client_thread(void *arg)
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
285
|
{
|
93480153
vrobic
modif dynamic_app
|
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
|
(void) arg;
Data data;
int vitesse=2;
uint8_t buf[3];
ssize_t res;
data.donnees[0] = 'g';
data.donnees[1] = 'o';
//uint8_t paquet[];
sock_udp_ep_t remote = { .family = AF_INET6 };
remote.port = 1234;
remote.addr.ipv6[0] = 0xde;
remote.addr.ipv6[1] = 0xad;
remote.addr.ipv6[2] = 0xbe;
remote.addr.ipv6[3] = 0xef;
remote.addr.ipv6[14] = 0x34;
remote.addr.ipv6[15] = 0x1e;
// memcpy(remote.addr.ipv6,addr.u8,IPV6_ADDR_BIT_LEN);
while (1) {
data.heure_actuelle = xtimer_now_usec();
if (sock_udp_send(&sock, &data, sizeof(data), &remote) < 0) {
puts("Error sending message");
}
puts("Send a message");
if(vitesse==2) {
gpio_clear(LED0_PIN);
gpio_toggle(LED1_PIN);
}
else {
gpio_clear(LED1_PIN);
gpio_toggle(LED0_PIN);
}
if ((res = sock_udp_recv(&sock, buf, sizeof(buf), 0.05 * SEC_IN_USEC,NULL)) < 0) {
if (res == -ETIMEDOUT) {
puts("Timed out");
}
else {
puts("Error receiving message");
}
}
else {
printf("Received message: \"");
for (int i = 0; i < res; i++) {
printf("%c", buf[i]);
}
if(buf[0] == 'n')
vitesse = 2;
else vitesse = 1;
printf("\"\n");
}
xtimer_sleep(vitesse);
}
return NULL;
|
ceb4c7ca
root
changement carte ...
|
340
|
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
341
|
|
93480153
vrobic
modif dynamic_app
|
342
|
static void arret_urgence(void *arg,int channel)
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
343
|
{
|
93480153
vrobic
modif dynamic_app
|
344
345
346
347
348
349
350
351
352
|
pwm_set(PWM_DEV(0),1,0);
pwm_set(PWM_DEV(1),1,0);
tourne = 0;
timer_run = 0;
gpio_clear(LED0_PIN);
gpio_clear(LED1_PIN);
gpio_set(LED2_PIN);
printf("Arret d'urgence\n");
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
353
354
|
/*static void degradation(void *arg,int channel)
|
93480153
vrobic
modif dynamic_app
|
355
356
357
358
359
360
361
362
|
* {
* ordre=2;
* //pwm_set(PWM_DEV(0),1,0);
* printf("Ralentissement\n");
* timer_set(TIMER_DEV(1),0,25200);
* }*/
static void _init_timer(void)
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
363
364
365
366
367
368
369
370
371
372
373
374
375
|
{
printf("ok timer\n");
//timer_init(XTIMER_DEV, CLOCK_CORECLOCK/2 ,°radation,NULL);
//timer_set(XTIMER_DEV, 0, 8400);
//timer_irq_enable(XTIMER_DEV);
timer_init(TIMER_DEV(1), CLOCK_CORECLOCK/2 ,&arret_urgence,NULL);
timer_irq_enable(TIMER_DEV(1));
}
static void _init_pwm(void)
{
pwm_init(PWM_DEV(0), PWM_LEFT, PWM_FREQ, PWM_RES);
pwm_set(PWM_DEV(0),1,0);
|
93480153
vrobic
modif dynamic_app
|
376
377
378
379
|
pwm_init(PWM_DEV(1), PWM_LEFT, PWM_FREQ, PWM_RES);
pwm_set(PWM_DEV(1),1,0);
// thread_create(pwm_stack,sizeof(pwm_stack),7,THREAD_CREATE_STACKTEST,pwm_thread,NULL,"pwm_thread");
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
380
|
}
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
381
382
|
|
93480153
vrobic
modif dynamic_app
|
383
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
384
385
386
387
388
389
390
391
392
|
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);
|
93480153
vrobic
modif dynamic_app
|
393
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
394
395
396
397
398
|
//addresses gobales
addr.u8[0] = 0xde;
addr.u8[1] = 0xad;
addr.u8[2] = 0xbe;
addr.u8[3] = 0xef;
|
93480153
vrobic
modif dynamic_app
|
399
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
400
|
res = gnrc_netapi_get(ifs[0], NETOPT_ADDRESS, 0, hwaddr, sizeof(hwaddr));
|
93480153
vrobic
modif dynamic_app
|
401
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
402
403
404
405
|
if (res >= 0) {
addr.u8[14] = *hwaddr;
addr.u8[15] = *(hwaddr+1);
}
|
93480153
vrobic
modif dynamic_app
|
406
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
407
|
memcpy(tmp_addr.u8,addr.u8,IPV6_ADDR_BIT_LEN);
|
93480153
vrobic
modif dynamic_app
|
408
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
409
410
411
412
|
gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST);
/* model ipv6 addr: dead:beef::Hwaddr */
if((addr.u8[14]==0x34)&&(addr.u8[15]==0x02)){
crea_rpl_dodag_root("1", "dead:beef::3402");
|
93480153
vrobic
modif dynamic_app
|
413
414
|
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");
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
415
|
}else if((addr.u8[14]==0x37)&&(addr.u8[15]==0x62)){
|
93480153
vrobic
modif dynamic_app
|
416
|
|
ceb4c7ca
root
changement carte ...
|
417
|
}else if((addr.u8[14]==0x34)&&(addr.u8[15]==0x12)){
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
418
419
420
|
}else if((addr.u8[14]==0x34)&&(addr.u8[15]==0x0a)){
}else if((addr.u8[14]==0x34)&&(addr.u8[15]==0x1e)){
|
93480153
vrobic
modif dynamic_app
|
421
422
423
424
|
_init_timer();
_init_pwm();
xtimer_sleep(2);
server=thread_create(sock_server_stack,sizeof(sock_server_stack),6,THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread");
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
425
426
427
428
429
430
431
432
|
}else{
puts("new node ?");
}
}
int main(void)
{
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
433
434
|
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
puts("RIOT network stack example application");
|
93480153
vrobic
modif dynamic_app
|
435
|
|
e87eb5fb
vrobic
RPL + serveur ntp ok
|
436
437
438
439
440
441
442
443
444
445
|
_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;
}
|