-
Notifications
You must be signed in to change notification settings - Fork 0
/
SRTreeC.nc
1818 lines (1538 loc) · 50.2 KB
/
SRTreeC.nc
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "SimpleRoutingTree.h"
module SRTreeC
{
// Interfaces used
uses interface Boot;
uses interface SplitControl as RadioControl;
uses interface Random;
uses interface ParameterInit<uint16_t> as Seed;
uses interface Packet as RoutingPacket;
uses interface AMSend as RoutingAMSend;
uses interface AMPacket as RoutingAMPacket;
uses interface AMSend as MeasAMSend;
uses interface AMPacket as MeasAMPacket;
uses interface Packet as MeasPacket;
uses interface Timer<TMilli> as RoutingMsgTimer;
uses interface Timer<TMilli> as RoundTimer;
uses interface Timer<TMilli> as SendMeasTimer;
uses interface LocalTime<TMilli> as TimeNow;
uses interface Receive as RoutingReceive;
uses interface Receive as MeasReceive;
uses interface PacketQueue as RoutingSendQueue;
uses interface PacketQueue as RoutingReceiveQueue;
uses interface PacketQueue as MeasSendQueue;
uses interface PacketQueue as MeasReceiveQueue;
}
implementation
{
// Counter holding current round
uint16_t roundCounter;
// RoutingMsg and MeasMsg messages
message_t radioRoutingSendPkt;
message_t radioMeasSendPkt;
// Flag showing if routing is finished on first round
bool FinishedRouting = FALSE;
uint8_t curdepth;
uint16_t parentID;
// Measurement of node
uint8_t measurement;
uint16_t previousQuery;
// Array holding the queries executed
uint8_t select[MAX_QUERIES];
// tct for Tina (percentage)
uint8_t tct;
// Query mode: Extended TAG->0, Tina->1
uint8_t mode;
// Array holding children values received
ChildInfo children[MAX_CHILDREN];
// Tasks
task void sendRoutingTask();
task void receiveRoutingTask();
task void sendMeasTask();
task void receiveMeasTask();
// Functions
uint32_t calculateQuery(uint8_t op, uint8_t nodeMeas);
////////////////////
event void Boot.booted()
{
uint8_t i;
uint16_t seedNum;
FILE *f;
// Start radio
call RadioControl.start();
// Initialisation
roundCounter = 0;
for (i = 0; i < MAX_QUERIES; i++)
{
select[i]=0;
}
// Change random's seed to seperate it from TOS_NODE_ID
f = fopen("/dev/urandom", "r");
fread(&seedNum, sizeof(seedNum), 1, f);
fclose(f);
call Seed.init(seedNum + TOS_NODE_ID + 1);
// Node initialisation
if(TOS_NODE_ID==0)
{
curdepth=0;
parentID=0;
dbg("Boot", "curdepth = %d , parentID= %d \n", curdepth , parentID);
// Assign random query mode (Extended(0) or Tina(1))
mode = (call Random.rand16())%2;
dbg("Boot", "\n\nMODE(extended -> 0, Tina -> 1 ): %d\n\n", mode);
}
else
{
curdepth=-1;
parentID=-1;
dbg("Boot", "curdepth = %d , parentID= %d \n", curdepth , parentID);
}
}
///////////////////
event void RadioControl.startDone(error_t err)
{
uint8_t i;
if (err == SUCCESS)
{
dbg("Radio" , "Radio initialized successfully!!!\n");
// Children initialisation (childID==0 means no child)
for (i=0; i < MAX_CHILDREN; i++)
{
children[i].childID = 0;
children[i].sum = 0;
children[i].count = 0;
children[i].max = 0;
children[i].min = 0;
children[i].sumsq = 0;
dbg("Tests", "Init child: %d, childID: %d, sum: %d, count: %d, max: %d, min: %d, sumsq: %d\n", i, children[i].childID, children[i].sum, children[i].count, children[i].max, children[i].min, children[i].sumsq);
}
// Timer to start sending measurements after routing
call SendMeasTimer.startOneShot(TIMER_ROUTING_DURATION);
// Timer to change round on new epoch
call RoundTimer.startPeriodicAt(-(BOOT_TIME), TIMER_PERIOD_MILLI);
if (TOS_NODE_ID==0)
{
// Timer to start sending RoutingMsg
call RoutingMsgTimer.startOneShot(TIMER_FAST_PERIOD);
}
}
else
{
// error
dbg("Radio" , "Radio initialization failed! Retrying...\n");
call RadioControl.start();
}
}
///////////////////
event void RadioControl.stopDone(error_t err)
{
// error
dbg("Radio", "Radio stopped!\n");
}
////////////////////////////
event void RoutingMsgTimer.fired()
{
message_t tmp;
error_t enqueueDone;
void* rpkt;
uint8_t i;
dbg("SRTreeC", "RoutingMsgTimer fired!\n");
// Round 1
roundCounter+=1;
if (TOS_NODE_ID==0)
{
// num holding number of queries(1 or 2 - Only on Extended TAG)
uint8_t num;
// If TINA
if (mode == 1)
{
// Random query
select[0] = ((call Random.rand16())%4) + 1;
// Random tct
tct = (call Random.rand16())%51;
dbg("Tina", "Query selected for TINA is: %d with tct: %d\n", select[0], tct);
}
// If Extended TAG
else
{
// Random number of queries
num = ((call Random.rand16())%2) + 1;
dbg("Extend", "We have %d queries!\n", num);
// Assign randome query/queries
for (i=0; i < num; i++)
{
select[i] = ((call Random.rand16())%6) + 1;
// If num==2 the second query must be different than the first
while (select[1] == select[0])
{
select[1] =((call Random.rand16())%6) + 1;
}
dbg("Extend", "Query %d is: %d\n", (i+1), select[i]);
}
}
// Round 1
dbg("RoutingMsg", "##################################### \n");
dbg("RoutingMsg", "####### ROUND %u ############## \n", roundCounter);
dbg("RoutingMsg", "#####################################\n");
}
// error
if(call RoutingSendQueue.full())
{
dbg("SRTreeC", "RoutingSendQueue is FULL!!! \n");
return;
}
// If we have 4 routing fields(Tina || Extended TAG with num==2)
if (mode == 1 || (mode == 0 && select[1] != 0))
{
// Get payload for Routing4field
rpkt = (Routing4field*) (call RoutingPacket.getPayload(&tmp, sizeof(Routing4field)));
dbg("SRTreeC","Creating Routing4field...\n");
// error
if(rpkt==NULL)
{
dbg("SRTreeC","RoutingMsgTimer.fired(): No valid payload... \n");
return;
}
// Assign values
atomic
{
((Routing4field*)rpkt)->mode = mode;
((Routing4field*)rpkt)->select = select[0];
((Routing4field*)rpkt)->depth = curdepth;
// If Tina, use select2ortct to store tct
if (mode == 1)
{
((Routing4field*)rpkt)->select2ortct = tct;
}
// If Extended TAG with num==2, use select2ortct to store the second query
else
{
((Routing4field*)rpkt)->select2ortct = select[1];
}
}
}
// Extended TAG with num==1
else
{
// Get payload for Routing3field
rpkt = (Routing3field*) (call RoutingPacket.getPayload(&tmp, sizeof(Routing3field)));
dbg("SRTreeC","Creating Routing3field...\n");
// error
if(rpkt==NULL)
{
dbg("SRTreeC","RoutingMsgTimer.fired(): No valid payload... \n");
return;
}
// Assign values
atomic
{
((Routing3field*)rpkt)->mode = mode;
((Routing3field*)rpkt)->select = select[0];
((Routing3field*)rpkt)->depth = curdepth;
}
}
dbg("SRTreeC" , "Sending RoutingMsg... \n");
// Enqueue
enqueueDone=call RoutingSendQueue.enqueue(tmp);
if( enqueueDone==SUCCESS)
{
// Post send of Routing message
if (call RoutingSendQueue.size()==1)
{
dbg("SRTreeC", "SendTask() posted!!\n");
post sendRoutingTask();
}
dbg("SRTreeC","RoutingMsg enqueued successfully in SendingQueue!!!\n");
}
else
{
// error
dbg("SRTreeC","RoutingMsg failed to be enqueued in SendingQueue!!!");
}
}
///////////////////////
event void RoutingAMSend.sendDone(message_t * msg , error_t err)
{
dbg("SRTreeC" , "Package sent %s \n", (err==SUCCESS)?"True":"False");
if(!(call RoutingSendQueue.empty()))
{
// error - post again
post sendRoutingTask();
}
}
//////////////////////////////
event message_t* RoutingReceive.receive( message_t * msg , void * payload, uint8_t len)
{
error_t enqueueDone;
message_t tmp;
uint16_t msource;
// Find message source
msource = call RoutingAMPacket.source(msg);
dbg("SRTreeC", "### RoutingReceive.receive() start ##### \n");
dbg("SRTreeC", "Something received!!! from %u \n", msource);
// Save message on temp var
atomic
{
memcpy(&tmp,msg,sizeof(message_t));
}
// Enqueue message
enqueueDone=call RoutingReceiveQueue.enqueue(tmp);
if(enqueueDone == SUCCESS)
{
// Post receive task
post receiveRoutingTask();
}
else
{
// error
dbg("SRTreeC","RoutingMsg enqueue failed!!! \n");
}
dbg("SRTreeC", "### RoutingReceive.receive() end ##### \n");
return msg;
}
/////////////////
event void RoundTimer.fired()
{
roundCounter++;
// Change round and print
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "##################################### \n");
dbg("Measurements", "####### ROUND %u ############## \n", roundCounter);
dbg("Measurements", "#####################################\n");
}
}
/////////////////
event void SendMeasTimer.fired()
{
message_t tmp;
error_t enqueueDone;
OneMeas8bit* measpkt;
uint8_t test;
// SendMeasTimer fires on round 1
if (!FinishedRouting)
{
dbg("RoutingMsg", "FinishedRouting!\n");
FinishedRouting = TRUE; // Activate flag
// Start sending measurements every epoch on 200ms window depending on depth (LIFO)
// Removes simulation's boot time on round 1
// Each node is shifted (Node 0 sends final result 200ms before epoch)
// Also uses an offset of NODE_ID*3 to reduce conflicts on nodes being in the same depth
call SendMeasTimer.startPeriodicAt(((-(BOOT_TIME)-((curdepth+1)*TIMER_FAST_PERIOD))+(TOS_NODE_ID*3)), TIMER_PERIOD_MILLI);
}
// SemdMeasTimer fires on round 2+
else
{
dbg("Measurements", "NodeID = %d curdepth= %d\n", TOS_NODE_ID, curdepth);
// if node is 0, we just print the final result
if (TOS_NODE_ID != 0)
{
dbg("Measurements", "Starting Data transmission to parent!\n");
}
dbg("SRTreeC", "SendMeasTimer fired!\n");
// Assign random measurement(0..50) on node
measurement = (call Random.rand16())%50;
dbg("Measurements", "measurement is: %d\n", measurement);
// error
if(call MeasSendQueue.full())
{
dbg("SRTreeC", "MeasSendQueue full!\n");
return;
}
// Create the message holding the measurement
call MeasAMPacket.setDestination(&tmp, parentID);
measpkt = (OneMeas8bit*) (call MeasPacket.getPayload(&tmp, sizeof(OneMeas8bit)));
call MeasPacket.setPayloadLength(&tmp,sizeof(OneMeas8bit));
// error
if(measpkt==NULL)
{
dbg("SRTreeC","SendMeasTimer.fired(): No valid payload... \n");
return;
}
measpkt->measurement = measurement;
dbg("SRTreeC" , "Sending MeasMsg... \n");
// Enqueue
enqueueDone=call MeasSendQueue.enqueue(tmp);
if (enqueueDone == SUCCESS)
{
if (call MeasSendQueue.size() == 1)
{
// Post send
post sendMeasTask();
}
dbg("SRTreeC","MeasMsg enqueued successfully in MeasSendQueue!!!\n");
}
else
{
// error
dbg("SRTreeC","MeasMsg failed to be enqueued in MeasSendQueue!!!\n");
}
}
}
/////////////////////////////
event void MeasAMSend.sendDone(message_t* msg , error_t err)
{
// Check if message is sent successfully
dbg("SRTreeC", "A Measure package sent... %s \n",(err==SUCCESS)?"True":"False");
if(!(call MeasSendQueue.empty()))
{
post sendMeasTask();
}
}
/////////////////////////////
event message_t* MeasReceive.receive( message_t* msg , void* payload , uint8_t len)
{
error_t enqueueDone;
message_t tmp;
uint16_t msource;
// Find message source
msource = call MeasAMPacket.source(msg);
dbg("Measurements", "Measurement received from: %d\n", msource);
dbg("SRTreeC", "### MeasReceive.receive() start ##### \n");
dbg("SRTreeC", "Some measurement received!!! from %u \n", msource);
// save message on temp var
atomic
{
memcpy(&tmp,msg,sizeof(message_t));
}
// enqueue message
enqueueDone=call MeasReceiveQueue.enqueue(tmp);
if( enqueueDone== SUCCESS)
{
// Post receive task
post receiveMeasTask();
}
else
{
// error
dbg("SRTreeC","MeasMsg enqueue failed!!! \n");
}
dbg("SRTreeC", "### MeasReceive.receive() end ##### \n");
return msg;
}
////////////////////////////////////////////////////////////////////
////////////// Tasks implementations //////////////////////////////
///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
task void sendRoutingTask()
{
error_t sendDone;
// error
if (call RoutingSendQueue.empty())
{
dbg("SRTreeC","sendRoutingTask(): Q is empty!\n");
return;
}
// dequeue message
radioRoutingSendPkt = call RoutingSendQueue.dequeue();
// Send Routing4field message (if Tina || Extended TAG with 2 queries)
if (mode == 1 || (mode == 0 && select[1] != 0))
{
sendDone=call RoutingAMSend.send(AM_BROADCAST_ADDR,&radioRoutingSendPkt,sizeof(Routing4field));
}
// Send Routing3field message (if Extended TAG with 1 query)
else
{
sendDone=call RoutingAMSend.send(AM_BROADCAST_ADDR,&radioRoutingSendPkt,sizeof(Routing3field));
}
if ( sendDone== SUCCESS)
{
dbg("SRTreeC","sendRoutingTask(): Send returned success!!!\n");
}
else
{
// error
dbg("SRTreeC","send failed!!!\n");
}
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
task void receiveRoutingTask()
{
uint8_t len;
message_t radioRoutingRecPkt;
uint16_t msource;
// dequeue message
radioRoutingRecPkt= call RoutingReceiveQueue.dequeue();
// find payload length
len = call RoutingPacket.payloadLength(&radioRoutingRecPkt);
dbg("Tests", "len: %d, sizeof(Routing4field): %d, sizeof(Routing3field): %d\n", len, sizeof(Routing4field), sizeof(Routing3field));
// Find message source
msource = call RoutingAMPacket.source(&radioRoutingRecPkt);
dbg("SRTreeC","ReceiveRoutingTask(): len=%u \n",len);
// Correct message
if(len == sizeof(Routing4field) || len == sizeof(Routing3field))
{
// Node doesn't have a parent
if ( (parentID<0)||(parentID>=65535))
{
// Tina || Extended TAG with 2 queries
if (len == sizeof(Routing4field))
{
// Get packet's payload
Routing4field* rpkt = (Routing4field*) (call RoutingPacket.getPayload(&radioRoutingRecPkt,len));
dbg("SRTreeC" , "receiveRoutingTask(): source= %d , depth= %d \n", msource , rpkt->depth);
// Assign values received
parentID = msource;
mode = rpkt->mode;
curdepth = rpkt->depth + 1;
select[0] = rpkt->select;
dbg("RoutingMsg" , "New parent for NodeID= %d : curdepth= %d , parentID= %d \n", TOS_NODE_ID ,curdepth , parentID);
// If Tina, save select2ortct as tct
if (mode == 1)
{
tct = rpkt->select2ortct;
dbg("Tina", "tinaSelect= %d , tct=%d\n", select[0], tct);
}
// If Extended TAG with 2 queries, save select2ortct as second query
else
{
select[1] = rpkt->select2ortct;
dbg("Extend", "ExtendSelect1= %d, ExtendSelect2= %d\n", select[0], select[1]);
}
}
// Extened TAG with 1 query
else
{
// Get packet's payload
Routing3field* rpkt = (Routing3field*) (call RoutingPacket.getPayload(&radioRoutingRecPkt,len));
dbg("SRTreeC" , "receiveRoutingTask(): source= %d , depth= %d \n", msource , rpkt->depth);
// Assign values received
parentID = msource;
mode = rpkt->mode;
curdepth = rpkt->depth + 1;
select[0] = rpkt->select;
dbg("RoutingMsg" , "New parent for NodeID= %d : curdepth= %d , parentID= %d \n", TOS_NODE_ID ,curdepth , parentID);
dbg("Extend", "ExtendSelect= %d\n", select[0]);
}
// Forward routing message to find new children
call RoutingMsgTimer.startOneShot(TIMER_FAST_PERIOD);
}
else
{
// Node already has parent
dbg("SRTreeC" , "NodeID= %d : Already has a parent: curdepth= %d, parentID= %d \n", TOS_NODE_ID ,curdepth , parentID);
}
}
else
{
// error
dbg("SRTreeC","receiveRoutingTask():Empty message!!! \n");
return;
}
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
task void sendMeasTask()
{
uint8_t mlen, nodeMeas, i;//, skip;
error_t sendDone;
uint16_t mdest;
OneMeas8bit* mpayload;
bool TinaPass = FALSE;
message_t tmp;
// error
if (call MeasSendQueue.empty())
{
dbg("SRTreeC","sendMeasTask(): Q is empty!\n");
return;
}
// Dequeue message
radioMeasSendPkt = call MeasSendQueue.dequeue();
// Get payload's length
mlen=call MeasPacket.payloadLength(&radioMeasSendPkt);
// Get payload
mpayload= call MeasPacket.getPayload(&radioMeasSendPkt,mlen);
// error
if(mlen!= sizeof(OneMeas8bit))
{
dbg("SRTreeC", "\t\t sendMeasTask(): Unknown message!!\n");
return;
}
// Save measurement
nodeMeas = mpayload->measurement;
// Tina || Extended TAG with 1 query
if (mode == 1 || (mode == 0 && select[1] == 0))
{
// Query: SUM
if (select[0] == SUM)
{
uint16_t query;
OneMeas16bit* measpkt;
// We send the message on a OneMeas16bit struct (One 16-bit number)
call MeasPacket.setPayloadLength(&tmp,sizeof(OneMeas16bit));
measpkt = (OneMeas16bit*) (call MeasPacket.getPayload(&tmp, sizeof(OneMeas16bit)));
// error
if(measpkt==NULL)
{
dbg("SRTreeC","SendMeasTimer.fired(): No valid payload... \n");
return;
}
// Calculate SUM
query = calculateQuery(SUM, nodeMeas);
// Node 0 - print results
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "\n");
dbg("Measurements" , "FINAL RESULTS: SUM: %d\n", query);
dbg("Measurements", "\n");
}
else if (mode == 1) // Tina
{
// If query result is outside tct or we are on first round - send message
if (roundCounter == 1 || query > previousQuery + ((float) tct / 100) * previousQuery || query < previousQuery - ((float) tct / 100) * previousQuery)
{
// Set flag true
TinaPass = TRUE;
dbg("Tina", "Measurement passes tct! Previous query: %d, Now sent: %d\n", previousQuery, query);
}
// Query result is within tct - don't send message
else
{
dbg("Tina", "Measurement doesn't pass tct! Previous query: %d, Now measured: %d\n", previousQuery, query);
}
// Query result passes tct - send message
if (TinaPass)
{
// Prepare message
atomic
{
// Save query result as last query sent
previousQuery = query;
call MeasAMPacket.setDestination(&tmp, parentID);
((OneMeas16bit*) measpkt)->measurement = query;
}
}
}
else // Extended TAG
{
atomic
{
call MeasAMPacket.setDestination(&tmp, parentID);
((OneMeas16bit*) measpkt)->measurement = query;
}
}
}
// Query: AVG (Only on Extended TAG)
else if (select[0] == AVG)
{
uint16_t sum;
uint8_t count;
TwoMeasMixedbit* measpkt;
// We send the message on a TwoMeasMixedbit struct (One 16-bit and one 8-bit number)
call MeasPacket.setPayloadLength(&tmp,sizeof(TwoMeasMixedbit));
measpkt = (TwoMeasMixedbit*) (call MeasPacket.getPayload(&tmp, sizeof(TwoMeasMixedbit)));
// error
if(measpkt==NULL)
{
dbg("SRTreeC","SendMeasTimer.fired(): No valid payload... \n");
return;
}
// Calculate queries
sum = calculateQuery(SUM, nodeMeas);
count = calculateQuery(COUNT, nodeMeas);
// Node 0 - print results
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "\n");
dbg("Measurements" , "FINAL RESULTS: AVG: %.2f\n", (sum / (float) count));
dbg("Measurements", "\n");
}
else
{
// Prepare message
atomic
{
call MeasAMPacket.setDestination(&tmp, parentID);
((TwoMeasMixedbit*) measpkt)->measurement16bit = sum;
((TwoMeasMixedbit*) measpkt)->measurement8bit = count;
}
}
}
// Query: VAR (Only on Extended TAG)
else if (select[0] == VAR)
{
uint32_t sumsq;
uint16_t sum;
uint8_t count;
VarMeasSimple* measpkt;
// We send the message on a VarMeasSimple struct (One 32-bit, one 16-bit and one 8-bit number)
call MeasPacket.setPayloadLength(&tmp,sizeof(VarMeasSimple));
measpkt = (VarMeasSimple*) (call MeasPacket.getPayload(&tmp, sizeof(VarMeasSimple)));
// error
if(measpkt==NULL)
{
dbg("SRTreeC","SendMeasTimer.fired(): No valid payload... \n");
return;
}
// Calculate queries
sumsq = calculateQuery(SUMSQ, nodeMeas);
sum = calculateQuery(SUM, nodeMeas);
count = calculateQuery(COUNT, nodeMeas);
// Node 0 - print results
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "\n");
dbg("Measurements" , "FINAL RESULTS: VAR: %.2f\n", ((sumsq / (float) count) - ((sum / (float) count) * (sum / (float) count))));
dbg("Measurements", "\n");
}
else
{
// Prepare message
atomic
{
call MeasAMPacket.setDestination(&tmp, parentID);
((VarMeasSimple*) measpkt)->measurement32bit = sumsq;
((VarMeasSimple*) measpkt)->measurement16bit = sum;
((VarMeasSimple*) measpkt)->measurement8bit = count;
}
}
}
// Queries: MAX || MIN || COUNT
else
{
uint8_t query;
OneMeas8bit* measpkt;
// We send the message on a OneMeas8bit struct (One 8-bit number)
call MeasPacket.setPayloadLength(&tmp,sizeof(OneMeas8bit));
measpkt = (OneMeas8bit*) (call MeasPacket.getPayload(&tmp, sizeof(OneMeas8bit)));
// error
if(measpkt==NULL)
{
dbg("SRTreeC","SendMeasTimer.fired(): No valid payload... \n");
return;
}
// If query: MAX
if (select[0] == MAX)
{
// Calculate query
query = calculateQuery(MAX, nodeMeas);
// Node 0 - print results
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "\n");
dbg("Measurements" , "FINAL RESULT: MAX: %d\n", query);
dbg("Measurements", "\n");
}
}
// If query: MIN
else if (select[0] == MIN)
{
// Node 0 - print results
query = calculateQuery(MIN, nodeMeas);
// Node 0 - print results
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "\n");
dbg("Measurements" , "FINAL RESULT: MIN: %d\n", query);
dbg("Measurements", "\n");
}
}
// If query: COUNT
else
{
// Calculate query
query = calculateQuery(COUNT, nodeMeas);
// Node 0 - print results
if (TOS_NODE_ID == 0)
{
dbg("Measurements", "\n");
dbg("Measurements" , "FINAL RESULT: COUNT: %d\n", query);
dbg("Measurements", "\n");
}
}
// TINA - Node!=0
if (mode == 1 && TOS_NODE_ID != 0)
{
// If query result is outside tct or we are on first round - send message
if (roundCounter == 1 || query > previousQuery + ((float) tct / 100) * previousQuery || query < previousQuery - ((float) tct / 100) * previousQuery)
{
// Set flag to true
TinaPass = TRUE;
dbg("Tina", "Measurement passes tct! Previous query: %d, Now sent: %d\n", previousQuery, query);
}
// Query result is inside tct - don't send message
else
{
dbg("Tina", "Measurement doesn't pass tct! Previous query: %d, Now measured: %d\n", previousQuery, query);
}
// Query result passes tct - send message
if (TinaPass)
{
// Prepare message
atomic
{
// Save query result as last query sent
previousQuery = query;
call MeasAMPacket.setDestination(&tmp, parentID);
((OneMeas8bit*) measpkt)->measurement = query;
}
}
}
// Extended TAG with 1 query
else if (TOS_NODE_ID != 0)
{
atomic
{
call MeasAMPacket.setDestination(&tmp, parentID);
((OneMeas8bit*) measpkt)->measurement = query;
}
}
}
}
else // Extended mode with 2 queries
{
// Queries: MAX-MIN || MAX-COUNT || MIN-COUNT
if ((select[0] == MAX || select[0] == MIN || select[0] == COUNT) && (select[1] == MAX || select[1] == MIN || select[1] == COUNT))
{
uint8_t query[MAX_QUERIES], measurementQueries[MAX_QUERIES], curQuery;
TwoMeas8bit* measpkt;
curQuery = 0; // curQuery holds the query we are currently on(0 or 1)
// We send the message on a TwoMeas8bit struct (Two 8-bit numbers and one holding the type of queries)
call MeasPacket.setPayloadLength(&tmp,sizeof(TwoMeas8bit));
measpkt = (TwoMeas8bit*) (call MeasPacket.getPayload(&tmp, sizeof(TwoMeas8bit)));
// error
if(measpkt==NULL)
{
dbg("SRTreeC","SendMeasTimer.fired(): No valid payload... \n");
return;
}
// One of the queries is MAX
if (select[0] == MAX || select[1] == MAX)
{
// Calculate query on an 8-bit array holding the result and save the type of query
query[curQuery] = calculateQuery(MAX, nodeMeas);
measurementQueries[curQuery] = MAX;
// Node 0 - print result
if (TOS_NODE_ID == 0)
{
dbg("Measurements" , "FINAL RESULTS: MAX: %d\n", query[curQuery]);
}
// First query found
curQuery++;
}
// One of the queries is MIN
if (select[0] == MIN || select[1] == MIN)
{
// Calculate query on an 8-bit array holding the result and save the type of query
query[curQuery] = calculateQuery(MIN, nodeMeas);
measurementQueries[curQuery] = MIN;
// Node 0 - print result
if (TOS_NODE_ID == 0)
{
dbg("Measurements" , "FINAL RESULTS: MIN: %d\n", query[curQuery]);
}
// First query found
curQuery++;
}
// One of the queries is COUNT
if (select[0] == COUNT || select[1] == COUNT)
{
// Calculate query on an 8-bit array holding the result and save the type of query
query[curQuery] = calculateQuery(COUNT, nodeMeas);
measurementQueries[curQuery] = COUNT;
// Node 0 - print result
if (TOS_NODE_ID == 0)