-
Notifications
You must be signed in to change notification settings - Fork 1
/
mobisense.c
2763 lines (2389 loc) · 95.6 KB
/
mobisense.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
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
/**---------------------------------------------------------------
*KTH - The Royal Institute of Technology - STOCKHOLM - Sweden
* School of Electrical Engineering
* Automatic Control Lab
*----------------------------------------------------------------
* Research Project in Mobile Wireless Sensor Networks
* Supervisor: Prof. Mikael Johansson
* Co-Supervisor:
*
*----------------------------------------------------------------
* Author: Antonio Gonga - gonga@kth.se
* Date Created : June 3rd, 2009
* Revision: Jan 21st, 2010
* Major Revision: Feb 12th, 2010, 04:19:45'AM
* Version: 10.7
*----------------------------------------------------------------
*Mobile MAC aims to provide medium access layer mobility to WSN
*nodes. This version is aimed to support uIP mobility.
*----------------------------------------------------------------*/
#include "contiki.h"
#include "net/mac/mobile_mac.h"
#include "net/rime/packetbuf.h"
#include "sys/rtimer.h"
#include "net/rime.h"
#include "lib/random.h"
#include "dev/leds.h"
#include "dev/cc2420.h"
#include "node-id.h"
#include <string.h>
/**-------------------------------------------------------------*/
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#if WITH_UIP6
#define PRINTMACADDR(addr) PRINTF(" fe80:0000:0000:00000:02%02x:%02x%02x:%02x%02x:%02x%02x \n", ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7])
#endif
#else
#define PRINTF(...)
#if WITH_UIP6
#define PRINTMACADDR(addr)
#endif
#endif
/**-------------------------------------------------------------*/
/**GENERAL DECLARATIONS
*
*
*/
/**-------------------------------------------------------------*/
#define TIME_DIFF(a,b) ((signed short)((a)-(b)) < 0)
#if RIMEADDR_SIZE == 2
const rimeaddr_t sink_node_addr = { { 1, 0 } };
#endif
#if RIMEADDR_SIZE == 8
//const rimeaddr_t sink_node_addr ={{0, 18, 116, 0, 17, 125, 68, 149}};
const rimeaddr_t sink_node_addr ={{0, 18, 116, 1, 0, 1, 1, 1}};
#endif
#if WITH_UIP6
#ifndef NETWORK_PERFORMANCE
#define NETWORK_PERFORMANCE 160
#endif
#endif /**WITH_UIP6*/
/**-------------------------------------------------------------*/
#define MAX_CHILDS 8
/*#if NODE_IS_SINK
#define MAX_CHILDS 8
#endif*/
/**-------------------------------------------------------------*/
/*#if NODE_IS_ROUTER
#define MAX_MOBILES 8
#define MAX_ROUTERS 1
#define MAX_CHILDS (MAX_MOBILES )
#endif*/
/**-------------------------------------------------------------*/
/*#if NODE_IS_MOBILE
#define MAX_CHILDS 8
#endif*/
/**-------------------------------------------------------------*/
#define COMMON_CH 26
#ifdef CONF_SINK_CH
#define SINK_CH SINK_CONF_CH
#else /*SINK_CONF_CH*/
#define SINK_CH 25
#endif /*SINK_CONF_CH*/
/**-------------------------------------------------------------*/
#define ONE_KILO 1024
#define ONE_MSEC (RTIMER_SECOND/ONE_KILO)
/*64 clock ticks ~8.0ms*/
#define TS (8*ONE_MSEC)
#define HALF_MSEC (ONE_MSEC/2)
#define GUARD_TIME (TS/4) /**~2.0ms*/
#if WITH_UIP6
#ifndef NETWORK_HDR_LEN
#define NETWORK_HDR_LEN 5
#endif /**NETWORK_HDR_LEN*/
#define UIP_NTWK_PERFORMANCE 100
#endif /**WITH_UIP6*/
/**-------------------------------------------------------------*/
enum{
SINK_DL_TX_TS = (8*TS), /**8TS=62.5ms*/
SINK_UL_RX_TS = (64*TS), /**500ms*/
ROUTER_UL_TX_TS = (32*TS), /**250ms*/
HDVR_TX_TS = (4*TS),
ROUTER_DL_TX_TS = (SINK_DL_TX_TS),
BW_ROUTER_TS = ((ROUTER_UL_TX_TS/TS) - MAX_CHILDS),
/**-------------------------------------------------------------*/
#if CONF_SUPER_FRAME_DURATION
SUPER_FRAME_DURATION = (TS*CONF_SUPER_FRAME_DURATION),
#else
SUPER_FRAME_DURATION = (SINK_DL_TX_TS + SINK_UL_RX_TS + HDVR_TX_TS),
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_MOBILE
MOB_AWAKE_OFFSET_1 = (SINK_UL_RX_TS - GUARD_TIME -TS),
MOB_AWAKE_OFFSET_2 = (ROUTER_UL_TX_TS - GUARD_TIME -TS),
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
AWAKE_PERIOD = ( SUPER_FRAME_DURATION - TS),
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_MOBILE
UL_QUEUE_SIZE = 25,
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER
#if WITH_UIP6
UL_QUEUE_SIZE = 15,
DL_QUEUE_SIZE = 5,
#else
UL_QUEUE_SIZE = 30,
DL_QUEUE_SIZE = 7,
#endif
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_SINK
DL_QUEUE_SIZE = 30,
#endif
}; /*end of enumeration*/
/**-------------------------------------------------------------*/
/**
*
* message types between sensor nodes.*/
enum{
XPTO,
SINK_BEACON, /**a sink beacon message*/
ROUTER_BEACON, /**a router beacon message*/
MOBILE_SLOT_REQ, /**a slot request from a mobile node to a router node*/
ROUTER_SLOT_REQ, /**a slot request from a router node to the sink node*/
NODE_SLOT_RELEASE, /**mobile requests slot release... handover in progress*/
NODE_STATUS_UPDATE, /**similar to KEEP alive, sent every 5 frames*/
NODE_UPDATE_ALL, /**upadate list of childs*/
HANDOVER_INFO, /**router special info to mobile nodes*/
DATA_RATE_UPDATE, /**mobile node data rate update*/
};
/**-------------------------------------------------------------*/
enum{
SINK_DATA =100, /*data originated or forwarded by the sink node*/
ROUTER_DATA=101, /*data originated or forarded by a router node*/
MOBILE_DATA=102, /*mobile node data*/
};
enum{
INACTIVE = 0,
ACTIVE = 1,
};
/**-------------------------------------------------------------*/
typedef enum{
NOTHING,
NODE_SLEEP,
NODE_TX_DOWNLINK,
NODE_TX_UPLINK,
NODE_RX_DOWLINK,
NODE_RX_UPLINK,
NODE_RX_SYNCH,
NODE_INFO_TX_RX,
NODE_WRITE_BUF,
NODE_RX_INFRA,
NODE_TX_ADMIN_INFO,
NODE_ROUTER_SCHEDULE,
}node_state_t;
/**-------------------------------------------------------------*/
//typedef u32_t my_clock_t;
typedef rtimer_clock_t my_clock_t;
#define NORM_CLOCK(a) ((u32_t)a%(65536UL))
static uint8_t beacon_seqno = 0;
/**-------------------------------------------------------------*/
static volatile node_state_t node_state = NOTHING;
#if NODE_IS_ROUTER || NODE_IS_MOBILE
static volatile node_state_t ul_buf_state = NOTHING;
#endif /*NODE_IS_ROUTER || NODE_IS_MOBILE*/
#if NODE_IS_SINK || NODE_IS_ROUTER
static volatile node_state_t dl_buf_state = NOTHING;
#endif /*NODE_IS_SINK || NODE_IS_ROUTER*/
/**-------------------------------------------------------------*/
/**every echanged message, is precedeed by a header. This header gives
*the type of message, and consequently how the message will be treated.
*/
typedef struct{
uint8_t type; ///MSG_TYPE DATA, CTRL_CH1,2,3 OR NOTHING
rimeaddr_t src; ///WHERE THE MESSAGE COMES FROM
rimeaddr_t dest; ///TO WHICH SENSOR THE MESSAGES IS DESTINATED TO
uint8_t pld_len; ///PAYLOAD LENGTH ...
}rdc_hdr_t;
/**-------------------------------------------------------------*/
/**Synchronization packet(cluster head synchronization packet)*/
typedef struct synch{
uint8_t seqno; /*sequence number*/
uint8_t n_childs; /*n_childs*/
uint8_t free_slots;
uint8_t n_routers; /*n_routers*/
uint8_t nr_hops;
uint8_t ch_offset; /*router channel offset*/
}synch_msg_t;
/*--------------------------------------------------------------*/
typedef struct{
rdc_hdr_t hdr;
uint8_t from; /**type of a node where it came from*/
uint8_t hops; /*how many hops to the sink*/
uint8_t n_nodes; /* number of nodes including routers*/
uint8_t ch_id; /*channel id: at which channel this router is operating*/
uint8_t group_id; /*explain later*/
}admin_info_t;
/**-------------------------------------------------------------*/
/*each slot request response message contains the requester id
*and the respective offset.
*/
typedef struct slots{
uint8_t ts_offset; /*slot Index/factor 0<= P1 < Num_neighs*/
uint8_t assig_slots; /*assigned slots*/
rimeaddr_t node_id; /*nodeID*/
}slot_dist_t;
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER
typedef struct{
uint8_t ttl;
uint8_t state;
rimeaddr_t node_id;
}mobile_state_t;
/**-------------------------------------------------------------*/
typedef enum{
INSERT,
LOOKUP,
REMOVE,
}operation_t;
#define HDVR_LEN 6
mobile_state_t handoff_list[HDVR_LEN]; /*I hope that all mobiles will not evaporate at the same time :)*/
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_SINK
typedef struct{
uint8_t odd_slots;
uint8_t even_slots;
uint8_t free_slots;
}sink_slot_dist_t;
sink_slot_dist_t sink_slots_tbl[] = {
{ 0, 0, 64}, /*0*/
{ 28, 29, 31}, /*1*/
{ 29, 29, 8}, /*2*/
{ 29, 30, 7}, /*3*/
{ 30, 30, 6}, /*4*/
{ 30, 31, 5}, /*5*/
{ 31, 31, 4}, /*6*/
{ 31, 32, 2}, /*7*/
{ 32, 32, 0}, /*8*/
};
#endif /*NODE_IS_SINK*/
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
/*handover table info. Each mobile node keeps this table in order to
*select the best candidate to connect to in advent of power drop
*/
typedef struct{
uint8_t state;
uint8_t ch_id;
signed char pdbm;
#if NODE_IS_ROUTER
uint8_t from; /*type of a node where it came from*/
uint8_t hops; /*how many hops to the sink*/
#endif /*end of NODE_IS_ROUTER*/
}neigh_conn_info_t;
static neigh_conn_info_t neigh_adv_list[MAX_CHILDS];
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
/*the child list is used to distribute offsets as well as for routing data
*/
typedef struct node_list{
uint8_t ttl;
uint8_t type; //node type
uint8_t state;
uint8_t data_rate; //or share
uint8_t assign_slots;
rimeaddr_t node_id;
}node_list_t;
static node_list_t node_list[MAX_CHILDS];
#endif /*NODE_IS_SINK || NODE_IS_ROUTER*/
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER || NODE_IS_MOBILE
typedef struct{
u32_t ad_t0; /**admission request time*/
u32_t ad_tn; /**admission response time_n*/
u32_t hv_t0; /**slot_release time_0*/
u32_t hv_tn; /**readimission response time*/
u32_t tx_packets;
#if !WITH_UIP6
uint8_t rp_type;
uint8_t eff_d_rate;
u32_t nc_t0; /**boot time*/
u32_t nc_tn; /**last admission response time*/
u32_t tx_bytes;
#endif
rimeaddr_t c_router; /**current router: is a router that a mobile node is attached to*/
rimeaddr_t n_router; /**next_router: is a router a mobile node moves, will then become the current router*/
}network_statistics_t;
#endif /*NODE_IS_ROUTER || NODE_IS_MOBILE*/
/**-------------------------------------------------------------*/
#define MIN_DATA_RATE 1
#define MAX_SYNCHR_BEACONS 128
#define SLOT_MSG_LEN sizeof(slot_dist_t)
#define ADMIN_MSG_LEN sizeof(admin_info_t)
#define SYNCH_MSG_LEN sizeof(synch_msg_t)
#define RDC_HDR_LEN sizeof(rdc_hdr_t)
/**-------------------------------------------------------------*/
typedef struct{
rdc_hdr_t hdr;
uint8_t data[PACKETBUF_SIZE - RDC_HDR_LEN];
}rdc_msg_t;
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER
#define TH_POWER (-80)
#endif
#if NODE_IS_MOBILE
#define TH_POWER (-80)
#endif
/**-------------------------------------------------------------*/
/**------------------radio specific-----------------------------*/
static volatile uint8_t radio_on = 0;
static volatile uint8_t radio_off = 0;
/**-------------------------------------------------------------*/
/**according to cc2420 DataSheet, PdBm = rssi+(-45)*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
/**At this received power, the node starts with the handover procedure.
*A new candicate is selected from the handover table. The handover procedure
*is similiar to the admission procedure. Once the candidate is selected, the
*next step is to send a request slot message, and wait for request response
*message.
*/
static const uint8_t data_channels[] = {25, 13, 18, 21, 20, 15, 22, 17, 12, 19, 14, 11, 16};
int16_t PWR_DBM(){
int16_t recv_rssi = cc2420_last_rssi;
return (int16_t) ((recv_rssi > 0x80) ? (recv_rssi - 256 - 45) : (recv_rssi - 45));
}
#endif
/**-------------------------------------------------------------*/
static volatile uint8_t cluster_size = 0;
static struct rtimer generic_timer;
/**-------------------------------------------------------------*/
#if NODE_IS_SINK
static volatile my_clock_t infra_rx_to;
static volatile my_clock_t infra_rx_tn;
static volatile my_clock_t synch_tx_to;
static volatile uint8_t even_window = 0, odd_window = 0;
#endif /*NODE_IS_SINK*/
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER
static volatile my_clock_t infra_rx_to;
static volatile my_clock_t infra_rx_tn;
static volatile my_clock_t hdvr_tx_to;
static volatile my_clock_t ul_tx_sch; /*at which time UL TX will be scheduled*/
static volatile my_clock_t radio_off_to; /*if there is no data to receive*/
//static my_clock_t node_awake_time; /*a very complex paramenter..explained later*/
static volatile uint8_t sum_data_rates= 1;
static volatile uint8_t slots_offset = 0;
#endif /*NODE_IS_ROUTER*/
/**-------------------------------------------------------------*/
#if NODE_IS_MOBILE
static volatile uint8_t n_routers = 0;
static volatile uint8_t handover = 0;
static volatile uint8_t occupied_ts = 0;
/*duty_cycle specific*/
static my_clock_t hdvr_rx_to;
#if RATE_CONTROL
extern uint8_t node_data_rate;
static void (* rate_update_func)( uint8_t len);
#endif /*RATE_CONTROL*/
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
static volatile my_clock_t ref_timer;
static my_clock_t radio_clock;
static volatile my_clock_t synch_rx_to;
/*uplink tx timer specific*/
static volatile my_clock_t ul_tx_to;
static volatile my_clock_t ul_tx_tn;
/*Evaluation variables */
static volatile my_clock_t ad_t0 = 0; /*admission request time*/
static volatile my_clock_t ad_tn = 0; /*admission response time_n*/
static volatile my_clock_t hv_t0 = 0; /*slot_release time_0*/
static volatile my_clock_t hv_tn = 0; /*readimission response time*/
static volatile my_clock_t nc_t0 = 0; /*boot time*/
static volatile my_clock_t nc_tn = 0; /*last admission response time*/
static uint8_t ass_d_rate = 0;
static u32_t n_tx_bytes = 0;
static u32_t n_tx_packets = 0;
static volatile uint8_t is_there_data = 0;
static volatile uint8_t report_type=0;
static volatile uint8_t ad_t0_lock = 1;
static volatile uint8_t ad_tn_lock = 0;
static volatile uint8_t hv_t0_lock = 0;
static volatile uint8_t hv_tn_lock = 0;
static volatile uint8_t nc_tn_lock = 1;
static volatile uint8_t send_stats_pkt = 0;
static rimeaddr_t curr_router;
static rimeaddr_t next_router;
/*admission specific*/
static volatile uint8_t atempts = 0;
static volatile uint8_t is_scanning = 0;
static volatile uint8_t admitted = 0;
static uint8_t data_rate = MIN_DATA_RATE;
static volatile uint8_t data_rate_update = 0;
static volatile uint8_t is_there_pkt = 0;
/*synch packet specific*/
static volatile uint8_t ch_offset = 0;
static volatile uint8_t ts_offset = 0;
static volatile uint8_t free_slots = 0;
static volatile uint8_t assign_slots = 0;
static uint8_t nr_hops = 0;
static uint8_t num_neighbors = 0;
static uint8_t group_id = 0;
/**radio channel specific*/
static volatile uint8_t uplink_ch = 0;
/*uplink data buffer operations specific*/
static volatile uint8_t ul_b_full = 0;
static volatile uint8_t next_to_send_ul = 0;
static volatile uint8_t last_queued_ul = 0;
static rdc_msg_t *ul_queue[UL_QUEUE_SIZE];
static rdc_msg_t ul_queue_bufs[UL_QUEUE_SIZE];
#endif /*NODE_IS_ROUTER || NODE_IS_MOBILE*/
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
static volatile my_clock_t dl_tx_to; /*downlink time to*/
static volatile my_clock_t dl_tx_tn; /*downlink time tn. tn-to =AT= AS*/
/*downlink buffers specific*/
static volatile uint8_t dl_b_full = 0;
static volatile uint8_t next_to_send_dl = 0;
static volatile uint8_t last_queued_dl = 0;
static rdc_msg_t *dl_queue[DL_QUEUE_SIZE];
static rdc_msg_t dl_queue_bufs[DL_QUEUE_SIZE];
#endif /*NODE_IS_SINK || NODE_IS_ROUTER*/
/**-------------------------------------------------------------*/
static const struct radio_driver *radio;
static void (* receiver_callback)(const struct mac_driver *);
/*---------------------------------------------------------------------------*/
static void (*inpacket_callback )(rimeaddr_t *, void* data, uint8_t len);
/*---------------------------------------------------------------------------*/
#if WITH_UIP6
static void(*uip_callback_func)(rimeaddr_t *, rimeaddr_t *, void* data, uint8_t len);
#endif
/**-------------------------------------------------------------*/
static int turn_radio_off();
static int turn_radio_on();
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
static char uplink_transmitter(struct rtimer *t, void* ptr);
static char slot_request(struct rtimer *t, void* ptr);
#endif /*NODE_IS_ROUTER || NODE_IS_MOBILE*/
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
static char tx_admin_info(struct rtimer *t, void* ptr);
static char downlink_transmitter(struct rtimer *t, void* ptr);
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_MOBILE
static char mobile_power_cycle(struct rtimer *t, void* ptr);
#endif
#if NODE_IS_ROUTER
static char router_power_cycle(struct rtimer *t, void* ptr);
static uint8_t mobile_has_left(rimeaddr_t *addr, operation_t operation);
#endif
#if NODE_IS_SINK
static char sink_power_cycle(struct rtimer *t, void* ptr);
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
inline static void get_node_statistics(void *ptr){
network_statistics_t *stat = (network_statistics_t*)ptr;
#if !WITH_UIP6
stat->rp_type = report_type;
stat->nc_t0 = nc_t0;
stat->nc_tn = nc_tn;
stat->tx_bytes = n_tx_bytes;
stat->eff_d_rate = ass_d_rate;
#endif
stat->ad_t0 = ad_t0;
stat->ad_tn = ad_tn;
stat->hv_t0 = hv_t0;
stat->hv_tn = hv_tn;
stat->tx_packets = n_tx_packets; /*we also count the stats packet..*/
rimeaddr_copy(&stat->c_router, &curr_router);
rimeaddr_copy(&stat->n_router, &next_router);
}
#endif /*NODE_IS_ROUTER || NODE_IS_MOBILE*/
/**-------------------------------------------------------------*/
void set_hdr_type(rdc_hdr_t *hdr, uint8_t type){
hdr->type = type;
}
/**-------------------------------------------------------------*/
uint8_t get_hdr_type(rdc_hdr_t *hdr){
return hdr->type;
}
/**-------------------------------------------------------------*/
/*turns the radio on*/
static void on(void) {
if (radio_on == 0) {
radio_on = 1;
radio->on();
}
}
/**-------------------------------------------------------------*/
/*turns the radio off*/
static void off(void) {
if (radio_on != 0) {
radio_on = 0;
radio->off();
}
}
/**-------------------------------------------------------------*/
/*initializes the data buffers*/
void data_buffers_init() {
uint8_t k;
#if NODE_IS_SINK || NODE_IS_ROUTER
for (k = 0; k < DL_QUEUE_SIZE; k++) {
dl_queue[k] = &dl_queue_bufs[k];
}
#endif
#if NODE_IS_MOBILE || NODE_IS_ROUTER
for (k = 0; k < UL_QUEUE_SIZE; k++) {
ul_queue[k] = &ul_queue_bufs[k];
}
#endif
}
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
/*initializes the child list*/
static inline void init_child_list() {
uint8_t k;
node_list_t *info;
for(k = 0; k < MAX_CHILDS; k++){
info = &node_list[k];
memset(info, 0, sizeof(node_list_t));
}
}
#endif
/**-------------------------------------------------------------*/
/*returts the childs list length*/
#if NODE_IS_SINK || NODE_IS_ROUTER
static uint8_t child_list_len() {
uint8_t k, len = 0;
node_list_t *l_ptr;
for(k = 0; k < MAX_CHILDS; k++){
l_ptr = &node_list[k];
if(l_ptr->state == ACTIVE){
len = len+1;
}
}
return len;
}
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
/*adds a new entry to the childs list*/
inline static void child_add(rimeaddr_t *addr, uint8_t rate, uint8_t type) {
uint8_t k, exists = 0;
uint8_t pos = MAX_CHILDS;
node_list_t *nodes;
for (k = 0; k < MAX_CHILDS; k++) {
nodes = &node_list[k];
if(nodes->state == ACTIVE){
if(rimeaddr_cmp(&nodes->node_id, addr)){
nodes->ttl = 0;
exists = 1;
//PRINTF("FOUND node %d.%d at pos:%d\n",addr->u8[0], addr->u8[1], k);
return;
}
}else{
pos = k;
}
}
if(exists == 0 && pos < MAX_CHILDS){
node_list[pos].ttl = 0;
node_list[pos].type = type;
node_list[pos].state = ACTIVE;
node_list[pos].data_rate = rate;
rimeaddr_copy(&node_list[pos].node_id, addr);
cluster_size = cluster_size + 1;
//PRINTF("adding %d.%d at pos:%d\n",addr->u8[0], addr->u8[1], pos);
#if NODE_IS_ROUTER
data_rate_update = 1;
#endif
}
}
/**-------------------------------------------------------------*/
/*adds a new entry to the childs list*/
inline static uint8_t is_there_packet_to(rimeaddr_t *addr){
uint8_t k = 0, data_len = 0;
rdc_msg_t *dlb;
#if NODE_IS_ROUTER
uint8_t count = 0;
k = next_to_send_dl ;
#endif
for( ; k < DL_QUEUE_SIZE; k++){
dlb = dl_queue[k];
data_len = dlb->hdr.pld_len;
if(data_len){
#if NODE_IS_SINK
/*let the routers filter the packet.*/
return 1;
#endif
#if NODE_IS_ROUTER
count++;
if(rimeaddr_cmp(addr, &dlb->hdr.dest) ||
rimeaddr_cmp(&dlb->hdr.dest, &rimeaddr_null)){
return 1;
}
if(count >= 16){
return 0;
}
#endif
data_len = 0;
}
if((k + 1) == last_queued_dl){
return 0;
}
}
return 0;
}
#endif /*NODE_IS_SINK || NODE_IS_ROUTER*/
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_MOBILE
/**-------------------------------------------------------------*/
/*initializes the handover table*/
inline void neigh_adv_list_init() {
uint8_t k;
neigh_conn_info_t *info_init;
for(k = 0; k < MAX_CHILDS; k++){
info_init = &neigh_adv_list[k];
memset(info_init, 0, sizeof(neigh_conn_info_t));
}
}
/**-------------------------------------------------------------*/
/*returns the handover table size/length*/
static uint8_t neigh_adv_list_len() {
uint8_t k, len = 0;
neigh_conn_info_t *info;
for(k = 0; k < MAX_CHILDS; k++){
info = &neigh_adv_list[k];
if(info->state == ACTIVE){
len = len +1;
}
}
return len;
}
/**-------------------------------------------------------------*/
inline static void neigh_adv_list_best(){
uint8_t k;
uint8_t new_ch_id = 0, exist = 0;
uint8_t best_hops = 100;
signed char power=TH_POWER;
neigh_conn_info_t *best;
#if NODE_IS_ROUTER
new_ch_id = neigh_adv_list[0].ch_id; //neigh_adv_list[0].ch_id;
uplink_ch = data_channels[new_ch_id];
return;
#endif
#if NODE_IS_MOBILE
for(k = 0; k < MAX_CHILDS; k++){
best = &neigh_adv_list[k];
if(best->state == ACTIVE){
if(TIME_DIFF(power, best->pdbm)){
new_ch_id = best->ch_id;
power = best->pdbm;
best->pdbm=-94;
exist = 1;
}
}
}
if(exist){
uplink_ch = data_channels[new_ch_id];
ch_offset = new_ch_id;
PRINTF("ch_offset_gonga: %d\n", ch_offset);
}
#endif
}
/**-------------------------------------------------------------*/
/*add a new entry to the handover table*/
static void parents_add(admin_info_t *adv_msg) {
uint8_t exists = 0, k;
uint8_t pos = MAX_CHILDS;
signed char pdbm = PWR_DBM();
neigh_conn_info_t *info;
#if NODE_IS_ROUTER
/*in this short version, am only interested to connect routers to the sink*/
if(adv_msg->hops == 0){
neigh_adv_list[0].state = ACTIVE;
neigh_adv_list[0].ch_id = adv_msg->ch_id;
neigh_adv_list[0].pdbm = pdbm;
neigh_adv_list[0].from = adv_msg->from;
neigh_adv_list[0].hops = adv_msg->hops; /*closer router */
return;
}
#endif
#if NODE_IS_MOBILE
if(adv_msg->hops == 0){
return;
}
for (k = 0; k < MAX_CHILDS; k++) {
info = &neigh_adv_list[k];
if(info->state == ACTIVE){
if (info->ch_id == adv_msg->ch_id){
if ((adv_msg->n_nodes < MAX_CHILDS) && TIME_DIFF(TH_POWER, pdbm)) {
info->pdbm = pdbm; //update
}else{
info->ch_id = 0;
info->state = INACTIVE; //remove
}
exists = 1;
return;
}
}else{
pos = k;
}
}
if (exists == 0 && pos < MAX_CHILDS) {
if (TIME_DIFF(TH_POWER, pdbm) && (adv_msg->n_nodes < MAX_CHILDS)) {
neigh_adv_list[pos].state = ACTIVE;
neigh_adv_list[pos].ch_id = adv_msg->ch_id;
neigh_adv_list[pos].pdbm = pdbm;
//PRINTF("...adding ch_id: %d. power:%d pos: %d\n", ch_id, pdbm, pos);
}
}
#endif /*NODE_IS_MOBILE*/
}
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
/*a mobile node is about to initiate handover, so a slot release message is
*received at the parent router node to remove thde node from the childs list
*/
static uint8_t child_remove(rimeaddr_t *addr) {
uint8_t k;
node_list_t *rm_ptr;
for (k = 0; k < MAX_CHILDS; k++) {
rm_ptr = &node_list[k];
if (rm_ptr->state == ACTIVE) {
if (rimeaddr_cmp(&rm_ptr->node_id, addr)) {
rm_ptr->state = INACTIVE;
rm_ptr->data_rate = 0;
return 1;
}
}
}
return 0;
}
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_SINK || NODE_IS_ROUTER
inline static void child_data_rates(){
node_list_t *child_list;
#if NODE_IS_SINK
uint8_t k, idx = 0;
even_window = odd_window = 0;
#endif
#if NODE_IS_ROUTER
uint8_t k, rates=0;
uint8_t childs_len = 0;
uint8_t available_slots = 0;
sum_data_rates = data_rate;
#endif
for (k = 0; k < MAX_CHILDS; k++) {
child_list = &node_list[k];
if (child_list->state == ACTIVE){
#if NODE_IS_ROUTER
rates = rates + child_list->data_rate;
sum_data_rates = sum_data_rates + rates;
childs_len = childs_len + 1;
#endif
#if NODE_IS_SINK
if(idx % 2 == 0){
even_window = even_window + child_list->data_rate;
}else{
odd_window = odd_window + child_list->data_rate;
}
idx = idx + 1;
#endif
}
}
#if NODE_IS_ROUTER
slots_offset = 0;
//cluster_size = childs_len;
available_slots = (BW_ROUTER_TS-(MAX_CHILDS/2 - childs_len/2));
for (k = 0; k < MAX_CHILDS; k++) {
child_list = &node_list[k];
if (child_list->state == ACTIVE){
uint8_t slots_per_node = 0;
#if WITH_UIP6
slots_per_node = (uint8_t)((59375*child_list->data_rate*available_slots)/(100000 *sum_data_rates));
#else
slots_per_node = (uint8_t)((child_list->data_rate*available_slots)/(sum_data_rates));
#endif
/**if the proportional chunk is higher than the requested data rate
*fix the chuck to the data rate.*/
if(slots_per_node >= child_list->data_rate){
slots_per_node = (child_list->data_rate/2) + (child_list->data_rate % 2 == 1 ? 1 : 0);
}
if((slots_per_node == 0) ||(child_list->data_rate == MIN_DATA_RATE)){
slots_per_node = MIN_DATA_RATE;
}
child_list->assign_slots = slots_per_node;
slots_offset = slots_offset + slots_per_node;
}
}
#endif
}
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER
/*used to check if a specific node exists in our childs list.
*/
static uint8_t child_exist(rimeaddr_t *addr){
uint8_t k;
node_list_t *ex_ptr;
for (k = 0; k < MAX_CHILDS; k++) {
ex_ptr = &node_list[k];
if (ex_ptr->state == ACTIVE){
if( rimeaddr_cmp(&ex_ptr->node_id, addr)) {
ex_ptr->ttl = 0;
return 1;
}
}
}
return 0;
}
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER
/**used to check if a specific node exists in our childs list*/
static uint8_t mobile_has_left(rimeaddr_t *addr, operation_t operation){
uint8_t k;
mobile_state_t *mob_list;
uint8_t exist = 0, pos = HDVR_LEN;
for(k = 0; k < HDVR_LEN; k++){
mob_list = &handoff_list[k];
if(mob_list->state == ACTIVE){
mob_list->ttl = mob_list->ttl + 1;
if(mob_list->ttl >= 4 && operation == REMOVE){
mob_list->ttl = 0;
mob_list->state = INACTIVE;
return 1;
}
if(rimeaddr_cmp(&mob_list->node_id, addr)){
exist = 1;
if(operation == LOOKUP){
return 1;
}
}
}else{
pos = k;
}
}
if((operation == INSERT) && (exist == 0 ) && (pos < HDVR_LEN)){
handoff_list[pos].state = ACTIVE;
handoff_list[pos].ttl = 0;
rimeaddr_copy(&handoff_list[pos].node_id, addr);
return 1;
}
//PRINTF("there is no such mobile node \n");
return 0;
}
#endif
/**-------------------------------------------------------------*/
#if NODE_IS_ROUTER || NODE_IS_SINK
void child_set_state(rimeaddr_t *addr, uint8_t state, uint8_t rate) {
uint8_t k;
node_list_t *c_elem;
for (k = 0; k < MAX_CHILDS; k++ ) {
c_elem = &node_list[k];
if(c_elem->state == ACTIVE){
//the node still alive, then update it
if(state == NODE_STATUS_UPDATE && rimeaddr_cmp(&c_elem->node_id, addr)) {
c_elem->ttl = 0;
c_elem->data_rate = rate;
}
if(state == DATA_RATE_UPDATE && rimeaddr_cmp(&c_elem->node_id, addr)) {
c_elem->ttl = 0;
c_elem->data_rate = rate;
#if NODE_IS_ROUTER
data_rate_update = 1; //newx round, send update data rate
#endif
}
if(state == NODE_UPDATE_ALL ){
c_elem->ttl = c_elem->ttl + 1;
if(c_elem->ttl >= 7) {
c_elem->state = INACTIVE;
/**if we have not heard from the node for 3 beacon periods, purge it*/
}
}
} /**end of state == ACTIVE*/
} /**end of the for loop*/
}
#endif
/**---------------------------------------------------------------------------*/
#if NODE_IS_MOBILE
/**Core procedure used for scanning the medium. It's mostly used by mobile sensor
*nodes when accessing the network, or when performing the handover procedure.
*This the procedure that sets the new candidate up. i.e, the new best router.
*/
static char mobile_chead_selection(struct rtimer *t, void *ptr){
uint8_t ret;
neigh_adv_list_best();
if (uplink_ch >= 11 && uplink_ch < 26) {