-
Notifications
You must be signed in to change notification settings - Fork 1
/
cllc.c
2178 lines (1927 loc) · 65.3 KB
/
cllc.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
/******************************************************************************
@file cllc.c
@brief Coordinator Logical Link Controller
This module is the Coordinator Logical Link Controller for the application.
Group: WCS LPC
$Target Device: DEVICES $
******************************************************************************
$License: BSD3 2016 $
******************************************************************************
$Release Name: PACKAGE NAME $
$Release Date: PACKAGE RELEASE DATE $
*****************************************************************************/
/******************************************************************************
Includes
*****************************************************************************/
#include <string.h>
#include "cllc.h"
#include "csf.h"
#include "mac_util.h"
#ifdef __unix__
#include "csf_linux.h"
#include "cllc_linux.h"
#endif
#ifdef FEATURE_SECURE_COMMISSIONING
#ifdef USE_DMM
#include "remote_display.h"
#include <sm_commissioning_gatt_profile.h>
#endif /* USE_DMM */
#include "sm_ti154.h"
#include "icall_osal_rom_jt.h"
#endif /* FEATURE_SECURE_COMMISSIONING */
#ifdef POWER_MEAS
#include "collector.h"
#endif
#ifndef __unix__
#include "advanced_config.h"
#endif
/******************************************************************************
Constants and definitions
*****************************************************************************/
#define CLLC_CHAN_LOWEST 0
/* Returns if the specific bit in the scan channel map array is set */
#define CLLC_IS_CHANNEL_MASK_SET(a, c) \
(*((uint8_t*)(a) + ((c) - CLLC_CHAN_LOWEST) / 8) & \
((uint8_t) 1 << (((c) - CLLC_CHAN_LOWEST) % 8)))
#define CLLC_DEFAULT_KEY_SOURCE {0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}
#define CLLC_INVALID_PAN 0xFFFF
#define CLLC_JOIN_PERMIT_ON 0xFFFFFFFF
#define CLLC_MAX_ENERGY 255
#define CLLC_PAN_NOT_FOUND 0x0000
#define CLLC_SET_CHANNEL(a,b) (a)[(b)>>3] |= (1 << ((b) & 7))
/*! MPM Constants for start request */
#define CLLC_OFFSET_TIMESLOT 0
#define CLLC_EBEACONORDER 15
#define CLLC_NBPANEBEACONORDER 16383
/*! FH default PIB values */
/*! value for ApiMac_FHAttribute_routingCost PIB */
#define CLLC_FH_ROUTING_COST 0x00
/*! value for ApiMac_FHAttribute_routingMethod PIB */
#define CLLC_FH_ROUTING_METHOD 0x01
/*! value for ApiMac_FHAttribute_eapolReady PIB */
#define CLLC_FH_EAPOL_READY 0x01
/*! value for ApiMac_FHAttribute_fanTPSVersion PIB */
#define CLLC_FH_FANTPSVERSION 0x01
/*! value for ApiMac_FHAttribute_gtk0Hash PIB */
#define CLLC_FH_GTK0HASH {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
/*! value for ApiMac_FHAttribute_gtk1Hash PIB */
#define CLLC_FH_GTK1HASH {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
/*! value for ApiMac_FHAttribute_gtk2Hash PIB */
#define CLLC_FH_GTK2HASH {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
/*! value for ApiMac_FHAttribute_gtk3Hash PIB */
#define CLLC_FH_GTK3HASH {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
#define CLLC_FH_PANVERSION 0x0000
#define CLLC_FH_MAX_TRICKLE CONFIG_TRICKLE_MAX_CLK_DURATION
#define CLLC_FH_MIN_TRICKLE CONFIG_TRICKLE_MIN_CLK_DURATION
#define PA_HOP_NEIGHBOR_FOUND_MASK 0x1
#define PA_FIXED_NEIGHBOR_FOUND_MASK 0x2
#define PC_HOP_NEIGHBOR_FOUND_MASK 0x4
#define PC_FIXED_NEIGHBOR_FOUND_MASK 0x8
#define USIE_FIXED_CHANNEL_OFFSET_CP0 6
#define USIE_FIXED_CHANNEL_OFFSET_CP1 10
/*!
Variable to start the assignment of short addresses by the coordinator
to each the device that associates to it
*/
#define CLLC_ASSOC_DEVICE_STARTING_NUMBER 1
/*! link quality */
#define CONFIG_LINKQUALITY 1
/*! percent filter */
#define CONFIG_PERCENTFILTER 0xFF
/******************************************************************************
Security constants and definitions
*****************************************************************************/
#define KEY_TABLE_ENTRIES 1
#define KEY_ID_LOOKUP_ENTRIES 1
#define KEY_DEVICE_TABLE_ENTRIES 8
#define KEY_USAGE_TABLE_ENTRIES 1
#define SECURITY_LEVEL_ENTRIES 1
#define MAC_FRAME_TYPE_DATA 1
#define MAC_DATA_REQ_FRAME 4
/******************************************************************************
Structures
*****************************************************************************/
/* building block for PAN descriptor linked list used to store PAN
descriptors received during beacon and scan */
typedef struct
{
void *pNext;
ApiMac_panDesc_t panDescList;
} panDescList_t;
/* Coordinator information, used to store default parameters */
typedef struct
{
uint8_t channel;
uint16_t panID;
uint16_t shortAddr;
Cllc_states_t currentCllcState;
Cllc_coord_states_t currentCoordState;
} coordInformation_t;
/******************************************************************************
Global variables
*****************************************************************************/
/* Task pending events */
uint16_t Cllc_events = 0;
/* Association table */
Cllc_associated_devices_t Cllc_associatedDevList[CONFIG_MAX_DEVICES];
Cllc_statistics_t Cllc_statistics;
/**
* Variable to start the assignment of short addresses by the coordinator
* to each the device that associates to it
*/
uint16_t Cllc_devShortAddr = 0x0001;
uint8_t CONST Cllc_keySource[] = CLLC_DEFAULT_KEY_SOURCE;
/******************************************************************************
Local variables
*****************************************************************************/
/* Default variables structure for coordinator */
STATIC coordInformation_t coordInfoBlock =
{
CLLC_CHAN_LOWEST,
0x0001,
#ifndef __unix__
CONFIG_COORD_SHORT_ADDR,
#else
CONFIG_COORD_SHORT_ADDR_DEFAULT,
#endif
Cllc_states_initWaiting,
Cllc_coordStates_initialized
};
/* default channel mask */
#ifndef __unix__
#if CONFIG_FH_ENABLE
STATIC uint8_t chanMask[APIMAC_154G_CHANNEL_BITMAP_SIZ] =
FH_ASYNC_CHANNEL_MASK;
#else
STATIC uint8_t chanMask[APIMAC_154G_CHANNEL_BITMAP_SIZ] =
CONFIG_CHANNEL_MASK;
#endif
#else
STATIC uint8_t chanMask[APIMAC_154G_CHANNEL_BITMAP_SIZ];
#endif
/* Linked list to store incoming PAN descriptors */
STATIC panDescList_t *pPANDesclist = NULL;
/* number of devices associated with the coordinator */
STATIC uint16_t Cllc_numOfDevices = 0;
/* copy of MAC API callbacks */
STATIC ApiMac_callbacks_t macCallbacksCopy = { 0 };
/* copy of CLLC callbacks */
STATIC Cllc_callbacks_t *pCllcCallbacksCopy = (Cllc_callbacks_t *) NULL;
#ifndef __unix__
STATIC CONST uint8_t fhNetname[APIMAC_FH_NET_NAME_SIZE_MAX] = CONFIG_FH_NETNAME;
#else
#define fhNetname ((uint8_t *)(&CONFIG_FH_NETNAME[0]))
#endif
STATIC CONST uint8_t fhGtkHash0[] = CLLC_FH_GTK0HASH;
STATIC CONST uint8_t fhGtkHash1[] = CLLC_FH_GTK1HASH;
STATIC CONST uint8_t fhGtkHash2[] = CLLC_FH_GTK2HASH;
STATIC CONST uint8_t fhGtkHash3[] = CLLC_FH_GTK3HASH;
#ifndef __unix__
STATIC uint32_t fhPAtrickleTime = CLLC_FH_MIN_TRICKLE;
STATIC uint32_t fhPCtrickleTime = CLLC_FH_MIN_TRICKLE;
#else
STATIC uint32_t fhPAtrickleTime;
STATIC uint32_t fhPCtrickleTime;
#endif
/* set of channels on which target nodes are expected to listen */
STATIC uint8_t optPAChMask[APIMAC_154G_CHANNEL_BITMAP_SIZ];
/* set of channels on which target nodes are expected to listen */
STATIC uint8_t optPCChMask[APIMAC_154G_CHANNEL_BITMAP_SIZ];
/* Flag to specify whether set of expected target channel is known */
STATIC uint8_t optAsyncFlag = false;
#ifndef __unix__
#if CONFIG_FH_ENABLE
/* FH Channel Mask */
STATIC uint8_t fhChannelMask[] = CONFIG_FH_CHANNEL_MASK;
STATIC uint8_t asyncChannelMask[] = FH_ASYNC_CHANNEL_MASK;
#endif /* CONFIG_FH_ENABLE */
#else
STATIC uint8_t fhChannelMask[];
STATIC uint8_t asyncChannelMask[];
#endif
#ifdef FEATURE_MAC_SECURITY
/******************************************************************************
Local security variables
*****************************************************************************/
STATIC CONST ApiMac_keyIdLookupDescriptor_t keyIdLookupList[] =
{
{
/* Key identity data */
{ 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x03 },
0x01 /* 9 octets */
}
};
/* Key device list can be modified at run time */
STATIC CONST ApiMac_keyDeviceDescriptor_t keyDeviceList[] =
{
{ 0x00, false, false },
{ 0x00, false, false },
{ 0x00, false, false },
{ 0x00, false, false },
{ 0x00, false, false },
{ 0x00, false, false },
{ 0x00, false, false },
{ 0x00, false, false }
};
STATIC CONST ApiMac_keyUsageDescriptor_t keyUsageList[] =
{
{ MAC_FRAME_TYPE_DATA, MAC_DATA_REQ_FRAME }
};
STATIC ApiMac_keyDescriptor_t keyTable[] =
{
{
(ApiMac_keyIdLookupDescriptor_t *)keyIdLookupList,
KEY_ID_LOOKUP_ENTRIES,
(ApiMac_keyDeviceDescriptor_t *)keyDeviceList,
KEY_DEVICE_TABLE_ENTRIES,
(ApiMac_keyUsageDescriptor_t *)keyUsageList,
KEY_USAGE_TABLE_ENTRIES,
KEY_TABLE_DEFAULT_KEY,
0 /* frame counter */
}
};
STATIC CONST ApiMac_securityPibSecurityLevelEntry_t securityLevelEntry =
{
0,
{ MAC_FRAME_TYPE_DATA, MAC_DATA_REQ_FRAME, 0, false }
};
STATIC CONST ApiMac_secLevel_t secLevel = ApiMac_secLevel_encMic32;
STATIC CONST ApiMac_keyIdMode_t secKeyIdMode = ApiMac_keyIdMode_8;
STATIC CONST uint8_t secKeyIndex = 3; /* cant be zero for implicit key identifier */
#ifndef __unix__
STATIC bool macSecurity = CONFIG_SECURE;
#else
#define macSecurity CONFIG_SECURE /* linux, we use the config variable */
bool networkStarted = 0;
#endif
#endif /* FEATURE_MAC_SECURITY */
#ifdef FEATURE_SECURE_COMMISSIONING
/* Copy of sensor device info received in Assoc Request: to be used for commissioning */
STATIC ApiMac_deviceDescriptor_t assocDevInfo;
/* Copy of RX on Idle info received in Assoc Request: to be used for commissioning */
STATIC bool assocDevRxOnIdle;
#endif /* FEATURE_SECURE_COMMISSIONING */
/******************************************************************************
Local Function Prototypes
*****************************************************************************/
/* CLLC callbacks */
static void assocIndCb(ApiMac_mlmeAssociateInd_t *pData);
static void beaconNotifyIndCb(ApiMac_mlmeBeaconNotifyInd_t *pData);
static void scanCnfCb(ApiMac_mlmeScanCnf_t *pData);
static void startCnfCb(ApiMac_mlmeStartCnf_t *pData);
static void disassocIndCb(ApiMac_mlmeDisassociateInd_t *pData);
static void disassocCnfCb(ApiMac_mlmeDisassociateCnf_t *pData);
static void wsAsyncIndCb(ApiMac_mlmeWsAsyncInd_t *pData);
static void dataIndCb(ApiMac_mcpsDataInd_t *pData);
static void orphanIndCb(ApiMac_mlmeOrphanInd_t *pData);
static void commStatusIndCb(ApiMac_mlmeCommStatusInd_t *pCommStatusInd);
#ifdef __unix__
static void resetIndCb(ApiMac_mcpsResetInd_t *pResetInd);
#endif
static void switchState(Cllc_coord_states_t newState);
static void processState(Cllc_coord_states_t state);
static void maintainAssocTable(ApiMac_deviceDescriptor_t *pDevInfo,
ApiMac_capabilityInfo_t *pCapInfo,
int8_t rssi,
uint16_t status,
bool mode);
static void configureStartParam(uint8_t channel);
/* PAN decriptor list management functions */
static void addToPANList(ApiMac_panDesc_t *pData);
static void clearPANList(void);
static bool findInList(ApiMac_panDesc_t *pData);
/* Scan results parsing */
static uint8_t findBestChannel(uint8_t *pResults);
static uint16_t findChannel(uint16_t panID,uint8_t channel);
static void updateState(Cllc_states_t state);
static void sendAsyncReq(uint8_t frameType);
static void joinPermitExpired(void);
static void sendStartReq(bool startFH);
static void sendScanReq(ApiMac_scantype_t type);
static void setTrickleTime(uint32_t *pTrickleTime, uint8_t frameType);
static void processIncomingFHframe(uint8_t frameType);
static void processIncomingAsyncUSIE(uint8_t frameType, uint8_t* pIEContent);
/******************************************************************************
Public Functions
*****************************************************************************/
/*!
Initialize this module.
Public function defined in cllc.h
This init function saves all of the mac callbacks in macCallbacksCopy,
and then overwrites them with the application callbacks.
pCllcCbs is passed in from Cllc_Callbacks defined in collector.c.
*/
void Cllc_init(ApiMac_callbacks_t *pMacCbs, Cllc_callbacks_t *pCllcCbs)
{
uint16_t panId = CONFIG_PAN_ID;
#ifdef __unix__
CLLC_LINUX_init(chanMask, &coordInfoBlock.shortAddr, &fhPAtrickleTime, &fhPCtrickleTime);
#endif
/* Initialize coordinator PAN ID if a valid value is defined in config */
if((panId != 0x0000) && (panId != CLLC_INVALID_PAN))
{
coordInfoBlock.panID = panId;
}
/* Save MAC API callback */
memcpy(&macCallbacksCopy, pMacCbs, sizeof(ApiMac_callbacks_t));
pCllcCallbacksCopy = pCllcCbs;
/* overwrite callbacks with llc callbacks
* set the callbacks defined in the application */
pMacCbs->pAssocIndCb = assocIndCb;
pMacCbs->pStartCnfCb = startCnfCb;
pMacCbs->pDisassociateIndCb = disassocIndCb;
pMacCbs->pDisassociateCnfCb = disassocCnfCb;
pMacCbs->pDataIndCb = dataIndCb;
pMacCbs->pCommStatusCb = commStatusIndCb;
#ifdef __unix__
pMacCbs->pResetIndCb = resetIndCb;
#endif
if(!CONFIG_FH_ENABLE)
{
pMacCbs->pBeaconNotifyIndCb = beaconNotifyIndCb;
pMacCbs->pScanCnfCb = scanCnfCb;
pMacCbs->pOrphanIndCb = orphanIndCb;
}
else
{
pMacCbs->pWsAsyncIndCb = wsAsyncIndCb;
}
/* initialize association table */
memset(Cllc_associatedDevList, 0xFF,
(sizeof(Cllc_associated_devices_t) * CONFIG_MAX_DEVICES));
ApiMac_mlmeSetReqBool(ApiMac_attribute_RxOnWhenIdle,true);
/* set PIB items */
/* setup short address */
ApiMac_mlmeSetReqUint16(ApiMac_attribute_shortAddress,
coordInfoBlock.shortAddr);
#if defined( POWER_MEAS ) || defined (TIMAC_AGAMA_FPGA)
/* Always set association permit to 1*/
ApiMac_mlmeSetReqBool(ApiMac_attribute_associatePermit, true);
#endif
if(CONFIG_FH_ENABLE)
{
uint8_t excludeChannels[APIMAC_154G_CHANNEL_BITMAP_SIZ];
uint8_t sizeOfChannelMask, idx;
/* Always set association permit to 1 for FH */
ApiMac_mlmeSetReqBool(ApiMac_attribute_associatePermit, true);
#ifndef __unix__
uint8_t configChannelMask[] = CONFIG_FH_CHANNEL_MASK;
#else
uint8_t configChannelMask[APIMAC_154G_CHANNEL_BITMAP_SIZ];
memcpy( configChannelMask, linux_CONFIG_FH_CHANNEL_MASK, sizeof(configChannelMask) );
#endif
/* initialize app clocks */
Csf_initializeTrickleClock();
/* set PIB to FH coordinator */
ApiMac_mlmeSetFhReqUint8(ApiMac_FHAttribute_unicastChannelFunction, 2);
ApiMac_mlmeSetFhReqUint8(ApiMac_FHAttribute_broadcastChannelFunction,
2);
ApiMac_mlmeSetFhReqUint8(ApiMac_FHAttribute_unicastDwellInterval,
CONFIG_DWELL_TIME);
ApiMac_mlmeSetFhReqUint8(ApiMac_FHAttribute_broadcastDwellInterval,
FH_BROADCAST_DWELL_TIME);
ApiMac_mlmeSetFhReqUint32(ApiMac_FHAttribute_BCInterval,
(FH_BROADCAST_INTERVAL >> 1));
/* set up the number of NON-sleep and sleep device
* the order is important. Need to set up the number of non-sleep first
*/
ApiMac_mlmeSetFhReqUint16(ApiMac_FHAttribute_numNonSleepDevice,
FH_NUM_NON_SLEEPY_HOPPING_NEIGHBORS);
ApiMac_mlmeSetFhReqUint16(ApiMac_FHAttribute_numSleepDevice,
FH_NUM_NON_SLEEPY_FIXED_CHANNEL_NEIGHBORS);
/* set Exclude Channels */
sizeOfChannelMask = sizeof(configChannelMask)/sizeof(uint8_t);
if(sizeOfChannelMask > APIMAC_154G_CHANNEL_BITMAP_SIZ)
{
sizeOfChannelMask = APIMAC_154G_CHANNEL_BITMAP_SIZ;
}
memset(excludeChannels, 0, APIMAC_154G_CHANNEL_BITMAP_SIZ);
for(idx = 0; idx < sizeOfChannelMask; idx++)
{
excludeChannels[idx] = ~configChannelMask[idx];
}
ApiMac_mlmeSetFhReqArray(ApiMac_FHAttribute_unicastExcludedChannels,
excludeChannels);
ApiMac_mlmeSetFhReqArray(ApiMac_FHAttribute_broadcastExcludedChannels,
excludeChannels);
}
}
/*!
Cllc task processing.
Public function defined in cllc.h
*/
void Cllc_process(void)
{
/* The LLC has an event */
if(Cllc_events & CLLC_PA_EVT)
{
if(CONFIG_FH_ENABLE)
{
setTrickleTime(&fhPAtrickleTime,
ApiMac_wisunAsyncFrame_advertisement);
}
/* Clear the event */
Util_clearEvent(&Cllc_events, CLLC_PA_EVT);
}
/* The LLC has an PC event */
if(Cllc_events & CLLC_PC_EVT)
{
if(CONFIG_FH_ENABLE)
{
setTrickleTime(&fhPCtrickleTime, ApiMac_wisunAsyncFrame_config);
}
/* Clear the event */
Util_clearEvent(&Cllc_events, CLLC_PC_EVT);
}
/* Process state change event */
if(Cllc_events & CLLC_STATE_CHANGE_EVT)
{
/* Process LLC Event */
processState(coordInfoBlock.currentCoordState);
/* Clear the event */
Util_clearEvent(&Cllc_events, CLLC_STATE_CHANGE_EVT);
}
/* Process join permit event */
if(Cllc_events & CLLC_JOIN_EVT)
{
joinPermitExpired();
/* Clear the event */
Util_clearEvent(&Cllc_events, CLLC_JOIN_EVT);
}
}
/*!
Set PANID
Public function defined in cllc.h
*/
void Cllc_setFormingPanId(uint16_t panId)
{
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting)
{
coordInfoBlock.panID = panId;
}
}
/*!
Get PANID
Public function defined in cllc.h
*/
void Cllc_getFormingPanId(uint16_t *pPanId)
{
static uint8_t panIdInitialized = 0;
Llc_netInfo_t netInfo;
/* If this is the second time that a network has existed,
* and a reset took place, restore the old value from NV */
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting && panIdInitialized != 0)
{
if(Csf_getNetworkInformation(&netInfo))
{
*pPanId = netInfo.devInfo.panID;
}
}
else
{
/* If this is the first time the collector is setting the panId, use the value that
* was either compiled or selected through CUI. */
*pPanId = coordInfoBlock.panID;
panIdInitialized = 1;
}
}
/*!
Set Channel Mask
Public function defined in cllc.h
*/
void Cllc_setChanMask(uint8_t *_chanMask)
{
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting)
{
#ifndef __unix__
#if CONFIG_FH_ENABLE
memcpy(fhChannelMask, _chanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#else
memcpy((void *)chanMask, _chanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#endif
#else
memcpy((void *)chanMask, _chanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#endif
}
}
/*!
Get Channel Mask
Public function defined in cllc.h
*/
void Cllc_getChanMask(uint8_t *_chanMask)
{
#ifndef __unix__
#if CONFIG_FH_ENABLE
memcpy(_chanMask, fhChannelMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#else
memcpy(_chanMask, chanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#endif
#else
memcpy(_chanMask, chanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#endif
}
/*!
Set Async Channel Mask
Public function defined in cllc.h
*/
void Cllc_setAsyncChanMask(uint8_t *asyncChanMask)
{
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting)
{
#ifndef __unix__
#if CONFIG_FH_ENABLE
memcpy(asyncChannelMask, asyncChanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#endif
#else
if(CONFIG_FH_ENABLE)
{
memcpy(asyncChannelMask, asyncChanMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
}
#endif
}
}
/*!
Get Async Channel Mask
Public function defined in cllc.h
*/
void Cllc_getAsyncChanMask(uint8_t *asyncChanMask)
{
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting)
{
#ifndef __unix__
#if CONFIG_FH_ENABLE
memcpy(asyncChanMask, asyncChannelMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
#endif
#else
if(CONFIG_FH_ENABLE)
{
memcpy(asyncChanMask, asyncChannelMask, APIMAC_154G_CHANNEL_BITMAP_SIZ);
}
#endif
}
}
#ifdef FEATURE_MAC_SECURITY
/*!
Set default security key
Public function defined in cllc.h
*/
void Cllc_setDefaultKey(uint8_t *key)
{
/* Frame Counter is set to 0 because no packets have been
* exchanged yet */
uint32_t frameCounter = 0;
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting)
{
/* Copy the key passed in into the key table */
memcpy((void *)keyTable[0].key, key, APIMAC_KEY_MAX_LEN);
Cllc_securityInit(frameCounter, key);
}
}
/*!
Get default security key
Public function defined in cllc.h
*/
void Cllc_getDefaultKey(uint8_t *key)
{
memcpy(key, keyTable[0].key, APIMAC_KEY_MAX_LEN);
}
#endif /* FEATURE_MAC_SECURITY */
/*!
Set the collector (Full Function Device - FFD) address
Public function defined in cllc.h
*/
void Cllc_setFfdShortAddr(uint16_t addr)
{
if(coordInfoBlock.currentCllcState == Cllc_states_initWaiting)
{
coordInfoBlock.shortAddr = addr;
}
}
/*!
Get the collector (Full Function Device - FFD) address
Public function defined in cllc.h
*/
void Cllc_getFfdShortAddr(uint16_t *addr)
{
*addr = coordInfoBlock.shortAddr;
}
/*!
Get the current PHY ID of the collector device
Public function defined in cllc.h
*/
uint8_t Cllc_getFreq(void)
{
return (CONFIG_PHY_ID);
}
/*!
Get the current channel of the collector device
Public function defined in cllc.h
*/
uint8_t Cllc_getChan(void)
{
return coordInfoBlock.channel;
}
/*!
Get the current state of the sensor device
Public function defined in cllc.h
*/
uint8_t Cllc_getProvState(void)
{
return (coordInfoBlock.currentCllcState);
}
/*!
Start network
Public function defined in cllc.h
*/
void Cllc_startNetwork(void)
{
/* update state */
updateState(Cllc_states_startingCoordinator);
if(!CONFIG_FH_ENABLE)
{
/*
Start active scan request to determine channel and PAN ID
for coordinator
*/
switchState(Cllc_coordStates_scanActive);
}
else
{
uint8_t startChan = 0;
for(startChan = 0; startChan < APIMAC_154G_MAX_NUM_CHANNEL; startChan++)
{
if(CLLC_IS_CHANNEL_MASK_SET(chanMask, startChan))
{
coordInfoBlock.channel = startChan;
break;
}
}
/* start req */
switchState(Cllc_coordStates_scanEdCnf);
}
}
/*!
Restore network
Public function defined in cllc.h
*/
void Cllc_restoreNetwork(Llc_netInfo_t *pNetworkInfo, uint16_t numDevices,
Llc_deviceListItem_t *pDevList)
{
uint16_t i = 0;
/* set state */
updateState(Cllc_states_initRestoringCoordinator);
coordInfoBlock.panID = pNetworkInfo->devInfo.panID;
/* Populate network info according to type of network */
if(pNetworkInfo->fh == true)
{
ApiMac_mlmeSetReqArray(ApiMac_attribute_extendedAddress,
(uint8_t*)(pNetworkInfo->devInfo.extAddress));
}
else
{
coordInfoBlock.channel = pNetworkInfo->channel;
}
ApiMac_mlmeSetReqUint16(ApiMac_attribute_shortAddress,
pNetworkInfo->devInfo.shortAddress);
sendStartReq(pNetworkInfo->fh);
if (pDevList)
{
/* repopulate association table */
for(i = 0; i < numDevices; i++, pDevList++)
{
/* Add to association table */
maintainAssocTable(&pDevList->devInfo, &pDevList->capInfo, 1, 0,
(false));
}
}
else
{
Llc_deviceListItem_t item;
/* repopulate association table */
for(i = 0; i < numDevices; i++)
{
Csf_getDeviceItem(i, &item);
#ifdef FEATURE_MAC_SECURITY
/* Add device to security device table */
Cllc_addSecDevice(item.devInfo.panID,
item.devInfo.shortAddress,
&item.devInfo.extAddress,
item.rxFrameCounter);
#endif /* FEATURE_MAC_SECURITY */
/* Add to association table */
maintainAssocTable(&item.devInfo, &item.capInfo, 1, 0,
(false));
#ifdef FEATURE_SECURE_COMMISSIONING
{
/* Mark the devices that need to be re-commissioned */
Cllc_associated_devices_t *pExistingDevice;
pExistingDevice = Cllc_findDevice(item.devInfo.shortAddress);
if(pExistingDevice != NULL)
{
pExistingDevice->reCM_status = SM_RE_CM_REQUIRED;
/* Do not update key refresh info here. It should be done when CM is done */
}
}
#endif /* FEATURE_SECURE_COMMISSIONING */
}
}
}
/*!
Set Join Permit On or Off
Public function defined in cllc.h
*/
ApiMac_status_t Cllc_setJoinPermit(uint32_t duration)
{
uint32_t joinDuration = 0;
if(duration > 0)
{
/* set join permit */
ApiMac_mlmeSetReqBool(ApiMac_attribute_associatePermit, true);
updateState(Cllc_states_joiningAllowed);
if(duration != CLLC_JOIN_PERMIT_ON)
{
/* set timer for duration */
joinDuration = duration;
}
}
else
{
/* set join permit */
ApiMac_mlmeSetReqBool(ApiMac_attribute_associatePermit, false);
updateState(Cllc_states_joiningNotAllowed);
}
Csf_setJoinPermitClock(joinDuration);
return (ApiMac_status_success);
}
/*!
Remove Device
Public function defined in cllc.h
*/
void Cllc_removeDevice(ApiMac_sAddrExt_t *pExtAddr)
{
int8_t i = 0;
uint16_t shortAddr = Csf_getDeviceShort(pExtAddr);
if(shortAddr != CSF_INVALID_SHORT_ADDR)
{
for(i = 0; i < CONFIG_MAX_DEVICES; i++)
{
if(Cllc_associatedDevList[i].shortAddr == shortAddr)
{
#ifdef FEATURE_MAC_SECURITY
/* Delete the device from the key table */
ApiMac_secDeleteDevice(pExtAddr);
#endif
#ifdef FEATURE_SECURE_COMMISSIONING
if (SM_Current_State == SM_CM_InProgress)
{
SM_stopCMProcess();
return;
}
else
{
SM_removeEntryFromSeedKeyTable(pExtAddr);
}
#endif /* FEATURE_SECURE_COMMISSIONING */
/* Clear the entry - delete */
memset(&Cllc_associatedDevList[i], 0xFF,
sizeof(Cllc_associated_devices_t));
/* remove from NV */
Csf_removeDeviceListItem(pExtAddr);
/* update CUI */
#ifndef __unix__
Csf_deviceDisassocUpdate(shortAddr);
#else
ApiMac_sAddr_t sAddr;
sAddr.addr.shortAddr = shortAddr;
sAddr.addrMode = ApiMac_addrType_short;
Csf_deviceDisassocUpdate(&sAddr);
#endif
/* The corresponding device is removed, return from the function call */
return;
}
}
}
}
/*!
Send disassociation request.
Public function defined in cllc.h
*/
void Cllc_sendDisassociationRequest(uint16_t shortAddr,bool rxOnIdle)
{
ApiMac_mlmeDisassociateReq_t disassocReq;
memset(&disassocReq, 0, sizeof(ApiMac_mlmeDisassociateReq_t));
disassocReq.deviceAddress.addrMode = ApiMac_addrType_short;
disassocReq.deviceAddress.addr.shortAddr = shortAddr;
disassocReq.devicePanId = coordInfoBlock.panID;
disassocReq.disassociateReason = ApiMac_disassocateReason_coord;
if(rxOnIdle == false)
{ /* Sleep device */
disassocReq.txIndirect = true;
}
else
{ /* Non-sleep device */
disassocReq.txIndirect = false;
}
ApiMac_mlmeDisassociateReq(&disassocReq);
}
#ifdef FEATURE_MAC_SECURITY
/*!
Initialize the MAC Security
Public function defined in cllc.h
If a key is provided, use it to initialize the security module.
Otherwise, use the one in the lookup table and pass in NULL for the key.
*/
void Cllc_securityInit(uint32_t frameCounter, uint8_t *key)
{
if(macSecurity == true)
{
ApiMac_secAddKeyInitFrameCounter_t secInfo;
if(key != NULL)
{
/* Copy the updated key into the table if one is provided */
memcpy((void *)keyTable[0].key, key, APIMAC_KEY_MAX_LEN);
}
/* Set the key based on whatever is stored in the key table */
memcpy(secInfo.key, keyTable[0].key, APIMAC_KEY_MAX_LEN);
secInfo.frameCounter = frameCounter;
secInfo.replaceKeyIndex = 0;
secInfo.newKeyFlag = true;
secInfo.lookupDataSize = APIMAC_KEY_LOOKUP_LONG_LEN;
memcpy(secInfo.lookupData, keyIdLookupList[0].lookupData,
(APIMAC_MAX_KEY_LOOKUP_LEN));
#ifdef FEATURE_SECURE_COMMISSIONING
secInfo.networkKey = true;
#endif /* FEATURE_SECURE_COMMISSIONING */
ApiMac_secAddKeyInitFrameCounter(&secInfo);
ApiMac_mlmeSetSecurityReqArray(
ApiMac_securityAttribute_defaultKeySource,
(void *) Cllc_keySource);
ApiMac_mlmeSetSecurityReqStruct(ApiMac_securityAttribute_keyTable,
(void *) NULL);
/* Set the number of security keys */
ApiMac_mlmeSetSecurityReqUint16(ApiMac_securityAttribute_keyTableEntries, KEY_TABLE_ENTRIES);
/* Write a security level entry to PIB */
ApiMac_mlmeSetSecurityReqStruct(
ApiMac_securityAttribute_securityLevelEntry,
(void *)&securityLevelEntry);
/* Set the MAC security */
ApiMac_mlmeSetReqBool(ApiMac_attribute_securityEnabled, macSecurity);
}
}
/*!
Fill in the security structure
Public function defined in cllc.h
*/
void Cllc_securityFill(ApiMac_sec_t *pSec)
{