-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
840 lines (687 loc) · 24.4 KB
/
main.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#include "contiki.h"
#include "net/rime/rime.h"
#include "dev/leds.h"
#include "dev/zoul-sensors.h" // Sensor functions
#include "dev/adc-zoul.h"
#include "dev/cc2538-rf.h"
#include <stdio.h> // For printf
#include <stdlib.h>
#include <math.h>
#include <random.h>
#include "project_conf.h"
#include "print_func.h"
#include "struct.h"
// Connections
static struct unicast_conn rep_conn;
static struct unicast_conn data_conn;
static struct broadcast_conn req_conn;
static struct unicast_conn ack_conn;
/*table functions*/
static void addEntryToDiscoveryTable(struct DISCOVERY_TABLE* req_info);
static char updateRoutingTable(REP_PACKAGE *rep, const linkaddr_t *from);
static void clearDiscovery(REP_PACKAGE* rep);
void addToWaitingTable(DATA_PACKAGE *data);
void addToWaitingAckTable(DATA_PACKAGE *data);
static char isDuplicateReq(REQ_PACKAGE* req);
/*callback functions*/
static void reply_callback(struct unicast_conn *c, const linkaddr_t *from);
static void request_callback(struct broadcast_conn *c, const linkaddr_t *from);
static void data_callback(struct unicast_conn *c, const linkaddr_t *from);
static void ack_callback(struct unicast_conn *c, const linkaddr_t *from);
/*callbacks*/
static const struct unicast_callbacks data_cbk = {data_callback};
static const struct unicast_callbacks rep_cbk = {reply_callback};
static const struct broadcast_callbacks req_cbk = {request_callback};
static const struct unicast_callbacks ack_cbk = {ack_callback};
/*transmit functions*/
static void senddata(DATA_PACKAGE *data, int next);
static void sendreq(REQ_PACKAGE* req);
static void sendrep(REP_PACKAGE* rep, int next);
static void sendack(ACK_PACKAGE *ack, int pre);
/*getdata functions*/
static int getTemperatureValue();
static int getLuxValue();
static int getNextHop();
static struct WAITING_TABLE waitingTable[MAX_WAIT_DATA];
static struct WAITING_ACK_TABLE waitingackTable[MAX_WAIT_DATA];
static struct DISCOVERY_TABLE discoveryTable[MAX_TABLE_SIZE];
static struct ROUTING_TABLE routingTable;
/*---------------------processes---------------*/
PROCESS(initial_process, "Initialize the routing table and open connections");
PROCESS(data_process, "Send data.");
PROCESS(request_process, "Handle REQ_PACKAGE messages");
PROCESS(reply_process, "Send reply.");
PROCESS(aging_process, "Decide the expiration of tables");
AUTOSTART_PROCESSES(&initial_process, &data_process, &request_process, &reply_process, &aging_process);
PROCESS_THREAD(initial_process, ev, data){
PROCESS_EXITHANDLER({
unicast_close(&rep_conn);
unicast_close(&data_conn);
broadcast_close(&req_conn);
unicast_close(&ack_conn);
});
PROCESS_BEGIN();
printf("--------Process initialized--------\n");
// initialize routing table
routingTable.dest.u8[0] = (DESTINATION >> 8) & 0xFF;
routingTable.dest.u8[1] = DESTINATION & 0xFF;
routingTable.valid = 0;
routingTable.hops = INF_HOPS;
routingTable.rssi = INF_RSSI;
// open connections
unicast_open(&data_conn, DATA_CHANNEL, &data_cbk);
unicast_open(&rep_conn, RREP_CHANNEL, &rep_cbk);
broadcast_open(&req_conn, RREQ_CHANNEL, &req_cbk);
unicast_open(&ack_conn, ACK_CHANNEL, &ack_cbk);
PROCESS_END();
}
PROCESS_THREAD(data_process, ev, data){
static struct etimer et;
static DATA_PACKAGE data_pkg;
static int id = 1;
static struct DISCOVERY_TABLE discovery;
static int next;
static int initial_delay;
PROCESS_BEGIN();
initial_delay = random_rand() % DATA_DELTA_TIME;
etimer_set(&et, (CLOCK_CONF_SECOND) * initial_delay );
while(1){
leds_off(LEDS_GREEN);
PROCESS_WAIT_EVENT_UNTIL(ev != sensors_event);
leds_on(LEDS_GREEN);
// Send own data
if(ev == PROCESS_EVENT_TIMER && linkaddr_node_addr.u8[1]!=0x01){
printf("\n--------Generate new data--------\n");
// Set message
strcpy(data_pkg.head, DATA_H);
data_pkg.dest.u8[0] = (DESTINATION >> 8) & 0xFF;
data_pkg.dest.u8[1] = DESTINATION & 0xFF;
data_pkg.id = id;
data_pkg.src.u8[0] = linkaddr_node_addr.u8[0];
data_pkg.src.u8[1] = linkaddr_node_addr.u8[1];
sprintf(data_pkg.message, DATA_PAY, getTemperatureValue(), getLuxValue());
data_pkg.hops = 0;
for(int i = 0; i<MAX_NODES; i++){
data_pkg.route[i].u8[1] = 0;
}
data_pkg.route[0].u8[1] = linkaddr_node_addr.u8[1];
printf("route is %d,%d,%d,%d,%d,%d\n", data_pkg.route[0].u8[1], data_pkg.route[1].u8[1], data_pkg.route[2].u8[1], data_pkg.route[3].u8[1], data_pkg.route[4].u8[1], data_pkg.route[5].u8[1]);
id = (id<99) ? id+1 : 1;
// Send data every 20 seconds
etimer_set(&et, CLOCK_CONF_SECOND * DATA_DELTA_TIME);
}
// Forward data
else if(ev != PROCESS_EVENT_TIMER){
printf("Forwarding data...\n");
strcpy(data_pkg.head, ((DATA_PACKAGE*)data)->head);
data_pkg.dest.u8[0] = ((DATA_PACKAGE*)data)->dest.u8[0];
data_pkg.dest.u8[1] = ((DATA_PACKAGE*)data)->dest.u8[1];
data_pkg.id = ((DATA_PACKAGE*)data)->id;
data_pkg.src.u8[0] = ((DATA_PACKAGE*)data)->src.u8[0];
data_pkg.src.u8[1] = ((DATA_PACKAGE*)data)->src.u8[1];
strcpy(data_pkg.message,((DATA_PACKAGE*)data)->message);
data_pkg.hops = ((DATA_PACKAGE*)data)->hops + 1;
int h = data_pkg.hops;
printf("hops in this forwarding data is %d\n",h);
printf("data package route now is:%d,%d,%d,%d,%d,%d\n",((DATA_PACKAGE*)data)->route[0].u8[1],((DATA_PACKAGE*)data)->route[1].u8[1],
((DATA_PACKAGE*)data)->route[2].u8[1],((DATA_PACKAGE*)data)->route[3].u8[1],((DATA_PACKAGE*)data)->route[4].u8[1],
((DATA_PACKAGE*)data)->route[5].u8[1]);
for(int i=0; i<h; i++){
data_pkg.route[i] = ((DATA_PACKAGE*)data)->route[i];
}
printf("the current h. route in data packet is%d, my adress is %d \n",data_pkg.route[h-1].u8[1], linkaddr_node_addr.u8[1]);
if(data_pkg.route[h-1].u8[1] != linkaddr_node_addr.u8[1]){
data_pkg.route[h].u8[1] = linkaddr_node_addr.u8[1];
}
printf("route is updated to %d,%d,%d,%d,%d,%d\n", data_pkg.route[0].u8[1], data_pkg.route[1].u8[1], data_pkg.route[2].u8[1], data_pkg.route[3].u8[1], data_pkg.route[4].u8[1], data_pkg.route[5].u8[1]);
}
if(linkaddr_node_addr.u8[1]!=0x01){
next = getNextHop();
// If the route exists
if(next!=0){
printf("Route exists, so sending message\n");
printf("Sending message: %s\n", data_pkg.message);
printf("The data package id is:%d, src is :%d\n",data_pkg.id,data_pkg.src.u8[1]);
senddata(&data_pkg, next);
// add to waiting table wait for ack
addToWaitingAckTable(&data_pkg);
}
// Otherwise send request broadcast
else{
printf("There is no existing route to %d.\n", data_pkg.dest.u8[1]);
addToWaitingTable(&data_pkg);
discovery.id = data_pkg.id;
discovery.src.u8[0] = linkaddr_node_addr.u8[0];
discovery.src.u8[1] = linkaddr_node_addr.u8[1];
discovery.dest.u8[0] = data_pkg.dest.u8[0];
discovery.dest.u8[1] = data_pkg.dest.u8[1];
discovery.snd.u8[0] = linkaddr_node_addr.u8[0];
discovery.snd.u8[1] = linkaddr_node_addr.u8[1];
process_post(&request_process, PROCESS_EVENT_CONTINUE, &discovery);
}
}
}
PROCESS_END();
}
/**
* Perform the outgoing Request route
*/
PROCESS_THREAD(request_process, ev, data)
{
static REQ_PACKAGE req;
PROCESS_BEGIN();
while(1)
{
leds_off(LEDS_YELLOW);
PROCESS_WAIT_EVENT_UNTIL(ev != sensors_event);
leds_on(LEDS_YELLOW);
strcpy(req.head, REQ_H);
req.id = ((struct DISCOVERY_TABLE*)data)->id;
req.src = ((struct DISCOVERY_TABLE*)data)->src;
req.dest = ((struct DISCOVERY_TABLE*)data)->dest;
addEntryToDiscoveryTable((struct DISCOVERY_TABLE*)data); //create entry in routing discovery table
sendreq(&req); //broadcasts the REQUEST
}
PROCESS_END();
}
/**
* Perform the outgoing Reply route
*/
PROCESS_THREAD(reply_process, ev, data)
{
static REP_PACKAGE rep;
PROCESS_BEGIN();
while(1)
{
leds_off(LEDS_YELLOW);
PROCESS_WAIT_EVENT_UNTIL(ev != sensors_event);
leds_on(LEDS_YELLOW);
strcpy(rep.head, REP_H);
rep.id = ((REP_PACKAGE*)data)->id;
rep.src = ((REP_PACKAGE*)data)->src;
rep.dest = ((REP_PACKAGE*)data)->dest;
rep.hops = ((REP_PACKAGE*)data)->hops;
rep.rssi = ((REP_PACKAGE*)data)->rssi;
sendrep(&rep, ((REP_PACKAGE*)data)->from.u8[1]); //broadcasts the REQUEST
}
PROCESS_END();
}
/**
* check the aging table periodically
*/
PROCESS_THREAD(aging_process, ev, data)
{
static struct etimer et;
static int flag;
static int i;
static int next;
static struct DISCOVERY_TABLE discovery;
PROCESS_BEGIN();
leds_off(LEDS_RED);
while(1)
{
etimer_set(&et, CLOCK_CONF_SECOND * TIME_FACTOR);
PROCESS_WAIT_EVENT_UNTIL(ev != sensors_event);
leds_off(LEDS_RED);
// Clean routingTable
flag = 0;
if(routingTable.age > 0 && routingTable.valid ==1)
{
routingTable.age--;
// if age has run out (route too old)
if(routingTable.age == 0)
{
routingTable.valid = 0;
routingTable.next.u8[0]= 0xFF;
routingTable.next.u8[1]= 0xFF;
routingTable.hops = INF_HOPS;
routingTable.rssi = INF_RSSI;
printf("route to %d has expired!\n",routingTable.dest.u8[1]);
leds_on(LEDS_RED);
flag++;
}
}
if (flag != 0) // if the routing table has changed
printRoutingTable(routingTable);
// Clean discovery table
flag = 0;
for(i=0; i<MAX_TABLE_SIZE; i++)
{
if(discoveryTable[i].age > 0 && discoveryTable[i].valid ==1)
{
discoveryTable[i].age --;
// if age has run out (route request too old)
if(discoveryTable[i].age == 0)
{
discoveryTable[i].valid = 0;
printf("REQUEST from %d to %d (ID:%d) has expired!\n",
discoveryTable[i].src.u8[1], discoveryTable[i].dest.u8[1], discoveryTable[i].id);
flag++;
}
}
}
if (flag != 0) // if the discovery table has changed
printDiscoveryTable(discoveryTable);
// Refresh waiting table
flag = 0;
for(i=0; i<MAX_WAIT_DATA; i++)
{
if(waitingTable[i].valid != 0)
{
next = getNextHop();
if (next != 0) // if the request in the waiting table find a route
{
senddata(&waitingTable[i].data_pkg, next);
waitingTable[i].valid = 0;
addToWaitingAckTable(&waitingTable[i].data_pkg);
flag++;
}
else
{
waitingTable[i].age--;
if (waitingTable[i].age == 0)
{
waitingTable[i].valid = 0;
flag++;
}
}
}
}
if (flag != 0) // if the waiting table has changed
printWaitingTable(waitingTable);
// Refresh waiting ack table
flag = 0;
for(i=0; i<MAX_WAIT_DATA; i++)
{
if(waitingackTable[i].valid != 0)
{
waitingackTable[i].age--;
if (waitingackTable[i].age == 0)
{
waitingackTable[i].valid = 0;
printf("There is now no more existing route to %d, so the node %d is going to request again from itself\n",
waitingackTable[i].data_pkg.dest.u8[1], linkaddr_node_addr.u8[1]);
discovery.id = waitingackTable[i].data_pkg.id;
discovery.src.u8[0] = linkaddr_node_addr.u8[0];
discovery.src.u8[1] = linkaddr_node_addr.u8[1];
discovery.dest.u8[0] = waitingackTable[i].data_pkg.dest.u8[0];
discovery.dest.u8[1] = waitingackTable[i].data_pkg.dest.u8[1];
discovery.snd.u8[0] = linkaddr_node_addr.u8[0];
discovery.snd.u8[1] = linkaddr_node_addr.u8[1];
process_post(&request_process, PROCESS_EVENT_CONTINUE, &discovery);
flag++;
}
}
}
if (flag != 0) // if the waiting table has changed
printWaitingackTable(waitingackTable);
}
PROCESS_END();
}
/*---------------------callback functions---------------*/
static void data_callback(struct unicast_conn *c, const linkaddr_t *from){
static DATA_PACKAGE data;
static ACK_PACKAGE ack;
printf("\n--------Data received--------\n");
packetbuf_copyto(&data);
printf("From: %d\n", from->u8[1]);
if(strcmp(data.head,DATA_H) == 0){
packetbuf_clear();
// if the destination is itself
if(data.dest.u8[1] == linkaddr_node_addr.u8[1]){
printf("%s;%d;%d;%d;%s;%d,%d,%d,%d,%d,%d;%d\n", data.head, data.id, data.src.u8[1], data.dest.u8[1], data.message,
data.route[0].u8[1], data.route[1].u8[1], data.route[2].u8[1], data.route[3].u8[1],
data.route[4].u8[1], data.route[5].u8[1], data.hops);
}
// otherwise
else{
process_post(&data_process, PROCESS_EVENT_CONTINUE, &data);
}
// send ACK
strcpy(ack.head, ACK_H);
ack.id = data.id;
ack.src = data.src;
sendack(&ack, from->u8[1]);
}
// case not data package
else{
printf("Incorrect data package\n");
}
packetbuf_clear();
}
static void ack_callback(struct unicast_conn *c, const linkaddr_t *from){
static ACK_PACKAGE ack;
static char ack_packet[ACK_LEN];
printf("\n--------Ack received--------\n");
packetbuf_copyto(&ack);
// printf("ack from: %d, src: %d\n", from->u8[1], ack.src.u8[1]);
// printf("ACK;\n");
if(strcmp(ack.head, ACK_H) == 0){
packetbuf_clear();
for(int i = 0; i < MAX_WAIT_DATA; i++){
if(waitingackTable[i].data_pkg.id == ack.id
&& waitingackTable[i].data_pkg.src.u8[1] == ack.src.u8[1]){
waitingackTable[i].valid = 0;
printWaitingackTable(waitingackTable);
}
}
}
// case not ack package
else{
printf("Incorrect ack package: %s\n", ack_packet);
}
packetbuf_clear();
}
/**
* called when receive a packet on REP_CHANNEL
*/
static void reply_callback(struct unicast_conn *c, const linkaddr_t *from)
{
static REP_PACKAGE rep;
static int i;
printf("\n--------Reply received--------\n");
packetbuf_copyto(&rep);
// printf("REP;\n");
// if REPLY package received
if(strcmp(rep.head, REP_H) == 0)
{
packetbuf_clear();
// printf("REPLY received from %d [ID:%d, Dest:%d, Src:%d, Hops:%d, RSSI: %d]\n",
// from->u8[1], rep.id, rep.dest.u8[1], rep.src.u8[1], rep.hops, rep.rssi);
// check if reply table updates
if(updateRoutingTable(&rep, from))
{
// if the source is not me forward reply to all nodes waiting
if(rep.src.u8[1] != linkaddr_node_addr.u8[1])
{
// printf("the source is not me, so i am sending reply to:%d\n",rep.src.u8[1]);
for(i=0; i<MAX_TABLE_SIZE; i++){
if(discoveryTable[i].valid != 0
&& discoveryTable[i].id == rep.id
&& discoveryTable[i].src.u8[1] == rep.src.u8[1]){
rep.hops = rep.hops + 1;
rep.from.u8[1] = discoveryTable[i].snd.u8[1];
// sendrep(&rep, discoveryTable[i].snd.u8[1]);
process_post(&reply_process, PROCESS_EVENT_CONTINUE, &rep);
discoveryTable[i].valid = 0;
// printf("sending reply to %d\n",discoveryTable[i].snd.u8[1]);
}
}
}
clearDiscovery(&rep);
}
}
packetbuf_clear();
}
/**
* called when receive a packet on REQ_CHANNEL
*/
static void request_callback(struct broadcast_conn *c, const linkaddr_t *from)
{
static struct DISCOVERY_TABLE req_info;
static REQ_PACKAGE req;
static REP_PACKAGE rep;
printf("\n--------Request received--------\n");
packetbuf_copyto(&req);
// printf("REQ;\n");
// case REQUEST package received
if(strcmp(req.head, REQ_H) == 0)
{
packetbuf_clear();
// printf("ROUTE_REQUEST received from %d [ID:%d, Dest:%d, Src:%d]\n",
// from->u8[1], req.id, req.dest.u8[1], req.src.u8[1]);
// case destination is me
if(req.dest.u8[1] == linkaddr_node_addr.u8[1])
{
rep.id = req.id;
rep.src = req.src;
rep.dest = req.dest;
rep.hops = 0;
rep.rssi = INF_RSSI;
rep.from.u8[1] = from->u8[1];
//sends a REPLY to the Request sender
// sendrep(&rep, from->u8[1]);
process_post(&reply_process, PROCESS_EVENT_CONTINUE, &rep);
}
// case the destination is NOT me AND the Request is new
else if(isDuplicateReq(&req)==0)
{
if (routingTable.valid != 0){ //If route exists
rep.id = req.id;
rep.src = req.src;
rep.dest = req.dest;
rep.hops = routingTable.hops + 1;
rep.rssi = routingTable.rssi;
rep.from.u8[1] = from->u8[1];
// sendrep(&rep, from->u8[1]);
process_post(&reply_process, PROCESS_EVENT_CONTINUE, &rep);
}else{
req_info.id = req.id;
req_info.src = req.src;
req_info.dest = req.dest;
req_info.snd.u8[1] = from->u8[1];
//wakes up the request process
process_post(&request_process, PROCESS_EVENT_CONTINUE, &req_info);
}
}
// case the Request is duplicated
else
{
printf("Duplicated REQUEST: Discarded!\n");
}
packetbuf_clear();
}
packetbuf_clear();
}
/*---------------------table functions---------------*/
/**
* add entry to discovery table
*/
static void addEntryToDiscoveryTable(struct DISCOVERY_TABLE* req_info)
{
for(int i=0; i<MAX_TABLE_SIZE; i++){
if(discoveryTable[i].valid == 0){
discoveryTable[i].id = req_info->id;
discoveryTable[i].src = req_info->src;
discoveryTable[i].dest = req_info->dest;
discoveryTable[i].snd = req_info->snd;
discoveryTable[i].valid = 1;
discoveryTable[i].age = MAX_DISCOVERY_TIME;
break;
}
}
printDiscoveryTable(discoveryTable);
}
/**
* Add the data to the waiting table
*/
void addToWaitingTable(DATA_PACKAGE *data){
for(int i=0; i<MAX_WAIT_DATA; i++) {
if(waitingTable[i].data_pkg.src.u8[1] == data->src.u8[1]
&& waitingTable[i].data_pkg.id == data->id
&& waitingTable[i].valid == 1) {
return;
}
}
for(int i=0; i<MAX_WAIT_DATA; i++) {
if(waitingTable[i].valid == 0) {
printf("Add data into (route) waiting table.\n");
waitingTable[i].data_pkg = *data;
waitingTable[i].age = MAX_QUEUEING_TIME;
waitingTable[i].valid = 1;
printWaitingTable(waitingTable);
return;
}
}
printf("There is no more space in waiting table.\n");
}
/**
* Add the data to the waiting table waiting for acknowledge
*/
void addToWaitingAckTable(DATA_PACKAGE *data){
for(int i=0; i<MAX_WAIT_DATA; i++) {
if(waitingackTable[i].data_pkg.src.u8[1] == data->src.u8[1]
&& waitingackTable[i].data_pkg.id == data->id
&& waitingackTable[i].valid == 1) {
return;
}
}
for(int i=0; i<MAX_WAIT_DATA; i++) {
if(waitingackTable[i].valid == 0) {
printf("Add data into ack waiting table.\n");
waitingackTable[i].data_pkg = *data;
waitingackTable[i].age = MAX_ACK_WAIT_TIME;
waitingackTable[i].valid = 1;
//printWaitingTable(waitingTable);
return;
}
}
printf("There is no more space in waiting ack table.\n");
return;
}
/**
* If better route is found, update routing table and return 1
* Otherwise return 0
*/
static char updateRoutingTable(REP_PACKAGE *rep, const linkaddr_t *from)
{
int16_t rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI);
printf("rssi:%d\n",rssi);
rssi = abs(rssi);
rep->rssi = (rep->rssi * rep->hops + rssi) / (rep->hops + 1);
//if the ROUTE_REPLY received shows a better path
if(rep->rssi < routingTable.rssi)
{
//UPDATES the routing discovery table!
routingTable.dest = rep->dest;
routingTable.hops = rep->hops;
routingTable.next.u8[1] = from->u8[1];
routingTable.age = MAX_ROUTE_TIME;
routingTable.valid = 1;
routingTable.rssi = rep->rssi;
printRoutingTable(routingTable);
return 1;
}
return 0;
}
/**
* clear discovery table
*/
static void clearDiscovery(REP_PACKAGE *rep)
{
int i;
for(i=0; i<MAX_TABLE_SIZE; i++){
if(discoveryTable[i].valid != 0
&& discoveryTable[i].id == rep->id
&& discoveryTable[i].src.u8[1] == rep->src.u8[1] // enable for ditinguish on reply id
&& discoveryTable[i].dest.u8[1] == rep->dest.u8[1])
discoveryTable[i].valid = 0;
}
}
/**
* check if the received Request was already in the discovery Table
*/
static char isDuplicateReq(REQ_PACKAGE *req)
{
for(int i=0; i<MAX_TABLE_SIZE; i++){
if(discoveryTable[i].valid != 0
&& discoveryTable[i].id == req->id
&& discoveryTable[i].src.u8[1] == req->src.u8[1]
&& discoveryTable[i].dest.u8[1] == req->dest.u8[1])
return 1;
}
return 0;
}
/*---------------------get data functions---------------*/
/**
* Get the Temperature value from the sensor.
*/
static int getTemperatureValue(){
return cc2538_temp_sensor.value(CC2538_SENSORS_VALUE_TYPE_CONVERTED);
}
/**
* Get the lux value from the sensor.
*/
static int getLuxValue(){
/* Configure the ADC ports */
adc_zoul.configure(SENSORS_HW_INIT, ZOUL_SENSORS_ADC1 | ZOUL_SENSORS_ADC3);
float m =1.5099;
float b = 42.466;
static uint16_t adc3_value;
adc3_value = adc_zoul.value(ZOUL_SENSORS_ADC3) >> 4;
uint8_t adc_input = adc3_value;
//Read voltage from the phidget interface
int Vref = 5;
//Convert the voltage in lux with the provided formula
int voltage = (adc_input * Vref * 200)/4096;
int lux = m * voltage + b;
//Return the value of the light with maximum value equal to 1000
if (lux>1000){
lux = 1000;
}
return lux;
}
/**
* Return the next hop to the destination
*/
static int getNextHop(){
if (routingTable.valid != 0){
return routingTable.next.u8[1];
}
return 0;
}
/*---------------------transmit functions---------------*/
/**
* Send data
*/
static void senddata(DATA_PACKAGE *data, int next){
static linkaddr_t next_addr;
next_addr.u8[1]=next;
next_addr.u8[0]=0x00;
printf("\n--------Data sending--------\n");
printf("sizeof DATA_PACKAGE, %d\n", sizeof(DATA_PACKAGE));
packetbuf_clear();
// packetbuf_copyfrom(packet, DATA_LEN);
packetbuf_copyfrom(data, sizeof(DATA_PACKAGE));
unicast_send(&data_conn, &next_addr);
//packetbuf_clear();
printf("Sending DATA {%s} to %d via %d \n",
data->message, data->dest.u8[1], next);
}
/**
* broadcast the request
*/
static void sendreq(REQ_PACKAGE* req)
{
printf("\n--------Request sending--------\n");
packetbuf_clear();
packetbuf_copyfrom(req, sizeof(REQ_PACKAGE));
broadcast_send(&req_conn);
printf("Broadcasting Request to %d with ID:%d and Source:%d\n",req->dest.u8[1], req->id, req->src.u8[1]);
}
/**
* send the REPLY message
*/
static void sendrep(REP_PACKAGE* rep, int next)
{
static linkaddr_t next_addr;
next_addr.u8[1]=next;
next_addr.u8[0]=0;
printf("\n--------Reply sending--------\n");
packetbuf_clear();
packetbuf_copyfrom(rep, sizeof(REP_PACKAGE));
unicast_send(&rep_conn, &next_addr);
printf("next_addr is now:%d\n", next_addr.u8[1]);
printf("Sending ROUTE_REPLY toward %d via %d [ID:%d, Dest:%d, Src:%d, Hops:%d]\n",
rep->src.u8[1], next, rep->id, rep->dest.u8[1], rep->src.u8[1], rep->hops);
}
static void sendack(ACK_PACKAGE *ack, int pre){
static linkaddr_t pre_addr;
pre_addr.u8[1]=pre;
pre_addr.u8[0]=0;
printf("\n--------Ack sending--------\n");
packetbuf_clear();
packetbuf_copyfrom(ack, sizeof(ACK_PACKAGE));
unicast_send(&ack_conn, &pre_addr);
printf("Sending ACK to %d\n", pre);
}