forked from discreetmayor/ti154_collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.c
2498 lines (2143 loc) · 78.6 KB
/
collector.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 collector.c
@brief TIMAC 2.0 Collector Example Application
Group: WCS LPC
$Target Device: DEVICES $
******************************************************************************
$License: BSD3 2016 $
******************************************************************************
$Release Name: PACKAGE NAME $
$Release Date: PACKAGE RELEASE DATE $
*****************************************************************************/
/******************************************************************************
Includes
*****************************************************************************/
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <libgen.h>
#include <inttypes.h>
#include "mac_util.h"
#include "api_mac.h"
#include "cllc.h"
#include "csf.h"
#include "smsgs.h"
#include "collector.h"
#include "log.h"
#include "oad_protocol.h"
#include "oad_storage.h"
#include "oad_image_header.h"
/******************************************************************************
Constants and definitions
*****************************************************************************/
#if !defined(CONFIG_AUTO_START)
#if defined(AUTO_START)
#define CONFIG_AUTO_START 1
#else
#define CONFIG_AUTO_START 0
#endif
#endif
/* Default MSDU Handle rollover */
#define MSDU_HANDLE_MAX 0x3F
/* App marker in MSDU handle */
#define APP_MARKER_MSDU_HANDLE 0x80
/* App Config request marker for the MSDU handle */
#define APP_CONFIG_MSDU_HANDLE 0x40
/* Ramp data request marker for the MSDU handle */
#define RAMP_DATA_MSDU_HANDLE 0x20
/* App Broadcast Cmd Msg marker for the MSDU Handle */
#define APP_BROADCAST_MSDU_HANDLE 0x20
/* Default configuration frame control */
#define CONFIG_FRAME_CONTROL (Smsgs_dataFields_tempSensor | \
Smsgs_dataFields_lightSensor | \
Smsgs_dataFields_humiditySensor | \
Smsgs_dataFields_msgStats | \
Smsgs_dataFields_configSettings | \
Smsgs_dataFields_custom)
/* Delay for config request retry in busy network */
#define CONFIG_DELAY 2000
#define CONFIG_RESPONSE_DELAY 3*CONFIG_DELAY
/* Tracking timeouts */
#define TRACKING_CNF_DELAY_TIME 2000 /* in milliseconds */
#if (CONFIG_PHY_ID == APIMAC_50KBPS_915MHZ_PHY_1) || \
(CONFIG_PHY_ID == APIMAC_50KBPS_868MHZ_PHY_3) || \
(CONFIG_PHY_ID == APIMAC_50KBPS_433MHZ_PHY_128)
#define SYMBOL_DURATION (SYMBOL_DURATION_50_kbps) //us
#elif (CONFIG_PHY_ID == APIMAC_200KBPS_915MHZ_PHY_132) || \
(CONFIG_PHY_ID == APIMAC_200KBPS_868MHZ_PHY_133)
#define SYMBOL_DURATION (SYMBOL_DURATION_200_kbps) //us
#elif (CONFIG_PHY_ID == APIMAC_5KBPS_915MHZ_PHY_129) || \
(CONFIG_PHY_ID == APIMAC_5KBPS_433MHZ_PHY_130) || \
(CONFIG_PHY_ID == APIMAC_5KBPS_868MHZ_PHY_131)
#define SYMBOL_DURATION (SYMBOL_DURATION_LRM) //us
#elif (CONFIG_PHY_ID == APIMAC_250KBPS_IEEE_PHY_0) // 2.4g
#define SYMBOL_DURATION (SYMBOL_DURATION_250_kbps) //us
#else
#define SYMBOL_DURATION (SYMBOL_DURATION_50_kbps) //us
#endif
#if (CONFIG_MAC_BEACON_ORDER != NON_BEACON_ORDER)
/* This is 3 times the polling interval used in beacon mode. */
#define TRACKING_TIMEOUT_TIME ((1<<CONFIG_MAC_BEACON_ORDER) * 960 * SYMBOL_DURATION * 3 / 1000) /*in milliseconds*/
#else
#define TRACKING_TIMEOUT_TIME (CONFIG_POLLING_INTERVAL * 3) /*in milliseconds*/
#endif
/* Initial delay before broadcast transmissions are started in FH mode */
#define BROADCAST_CMD_START_TIME 60000
/* Assoc Table (CLLC) status settings */
#define ASSOC_CONFIG_SENT 0x0100 /* Config Req sent */
#define ASSOC_CONFIG_RSP 0x0200 /* Config Rsp received */
#define ASSOC_CONFIG_MASK 0x0300 /* Config mask */
#define ASSOC_TRACKING_SENT 0x1000 /* Tracking Req sent */
#define ASSOC_TRACKING_RSP 0x2000 /* Tracking Rsp received */
#define ASSOC_TRACKING_RETRY 0x4000 /* Tracking Req retried */
#define ASSOC_TRACKING_ERROR 0x8000 /* Tracking Req error */
#define ASSOC_TRACKING_MASK 0xF000 /* Tracking mask */
#define MAX_OAD_FILES 10
#define TURBO_OAD_HEADER_LEN 64
#ifdef TIRTOS_IN_ROM
#define IMG_HDR_ADDR 0x04F0
#else
#define IMG_HDR_ADDR 0x0000
#endif
/* Delta image info segment constants */
#define IMG_DELTA_SEG_ID 0x05
#define DELTA_SEG_LEN 0x14
#define DELTA_SEG_IS_DELTA_IMG_OFFSET 0x08
#define DELTA_SEG_HEADER_VERSION_OFFSET 0x09
#define DELTA_SEG_VERSION_OFFSET 0x0A
#define DELTA_SEG_MEMORY_CFG_OFFSET 0x0B
#define DELTA_SEG_OLD_IMG_CRC_OFFSET 0x0C
#define DELTA_SEG_NEW_IMG_LEN_OFFSET 0x10
#define DELTA_SEG_NOT_FOUND -1
/* OAD header constants */
#define OAD_FIXED_HDR_LEN 0x2C
#define OAD_SEG_ID_OFFSET 0x00
#define OAD_SEG_LEN_OFFSET 0x04
typedef struct
{
uint8_t oad_file_id;
char oad_file[256];
}oadFile_t;
/******************************************************************************
Global variables
*****************************************************************************/
/* Task pending events */
uint16_t Collector_events = 0;
/*! Collector statistics */
Collector_statistics_t Collector_statistics;
/******************************************************************************
Local variables
*****************************************************************************/
static void *sem;
/*! true if the device was restarted */
static bool restarted = false;
/*! CLLC State */
static Cllc_states_t cllcState = Cllc_states_initWaiting;
/*! Device's PAN ID */
static uint16_t devicePanId = 0xFFFF;
/*! Device's Outgoing MSDU Handle values */
static uint8_t deviceTxMsduHandle = 0;
static bool fhEnabled = false;
static oadFile_t oad_file_list[MAX_OAD_FILES] = {{0}};
static uint16_t oadBNumBlocks;
/******************************************************************************
Local function prototypes
*****************************************************************************/
static void initializeClocks(void);
static void cllcStartedCB(Llc_netInfo_t *pStartedInfo);
static ApiMac_assocStatus_t cllcDeviceJoiningCB(
ApiMac_deviceDescriptor_t *pDevInfo,
ApiMac_capabilityInfo_t *pCapInfo);
static void cllcStateChangedCB(Cllc_states_t state);
static void dataCnfCB(ApiMac_mcpsDataCnf_t *pDataCnf);
static void dataIndCB(ApiMac_mcpsDataInd_t *pDataInd);
static void disassocIndCB(ApiMac_mlmeDisassociateInd_t *pDisassocInd);
static void disassocCnfCB(ApiMac_mlmeDisassociateCnf_t *pDisassocCnf);
static void processStartEvent(void);
#ifndef PROCESS_JS
static void processConfigResponse(ApiMac_mcpsDataInd_t *pDataInd);
static void processSensorData(ApiMac_mcpsDataInd_t *pDataInd);
#endif
static void processTrackingResponse(ApiMac_mcpsDataInd_t *pDataInd);
static void processToggleLedResponse(ApiMac_mcpsDataInd_t *pDataInd);
static void processDeviceTypeResponse(ApiMac_mcpsDataInd_t *pDataInd);
static void processOadData(ApiMac_mcpsDataInd_t *pDataInd);
static Cllc_associated_devices_t *findDevice(ApiMac_sAddr_t *pAddr);
static Cllc_associated_devices_t *findDeviceStatusBit(uint16_t mask, uint16_t statusBit);
static uint8_t getMsduHandle(Smsgs_cmdIds_t msgType);
static bool sendMsg(Smsgs_cmdIds_t type, uint16_t dstShortAddr, bool rxOnIdle,
uint16_t len,
uint8_t *pData);
static void generateConfigRequests(void);
static void generateTrackingRequests(void);
static void generateBroadcastCmd(void);
static void sendTrackingRequest(Cllc_associated_devices_t *pDev);
static void commStatusIndCB(ApiMac_mlmeCommStatusInd_t *pCommStatusInd);
static void pollIndCB(ApiMac_mlmePollInd_t *pPollInd);
static void processDataRetry(ApiMac_sAddr_t *pAddr);
static void processConfigRetry(void);
static void processIdentifyLedRequest(ApiMac_mcpsDataInd_t *pDataInd);
static void orphanIndCb(ApiMac_mlmeOrphanInd_t *pData);
static void oadFwVersionRspCb(void* pSrcAddr, char *fwVersionStr);
static void oadImgIdentifyRspCb(void* pSrcAddr, uint8_t status);
static void oadBlockReqCb(void* pSrcAddr, uint8_t imgId, uint16_t blockNum, uint16_t multiBlockSize);
static void oadResetRspCb(void* pSrcAddr);
static void* oadRadioAccessAllocMsg(uint32_t size);
static OADProtocol_Status_t oadRadioAccessPacketSend(void* pDstAddr, uint8_t *pMsg, uint32_t msgLen);
static long findDeltaSeg(FILE* pFile);
/******************************************************************************
Callback tables
*****************************************************************************/
/*! API MAC Callback table */
ApiMac_callbacks_t Collector_macCallbacks =
{
/*! Associate Indicated callback */
NULL,
/*! Associate Confirmation callback */
NULL,
/*! Disassociate Indication callback */
disassocIndCB,
/*! Disassociate Confirmation callback */
disassocCnfCB,
/*! Beacon Notify Indication callback */
NULL,
/*! Orphan Indication callback */
orphanIndCb,
/*! Scan Confirmation callback */
NULL,
/*! Start Confirmation callback */
NULL,
/*! Sync Loss Indication callback */
NULL,
/*! Poll Confirm callback */
NULL,
/*! Comm Status Indication callback */
commStatusIndCB,
/*! Poll Indication Callback */
pollIndCB,
/*! Data Confirmation callback */
dataCnfCB,
/*! Data Indication callback */
dataIndCB,
/*! Reset Indication callback */
NULL,
/*! Purge Confirm callback */
NULL,
/*! WiSUN Async Indication callback */
NULL,
/*! WiSUN Async Confirmation callback */
NULL,
/*! Unprocessed message callback */
NULL
};
static Cllc_callbacks_t cllcCallbacks =
{
/*! Coordinator Started Indication callback */
cllcStartedCB,
/*! Device joining callback */
cllcDeviceJoiningCB,
/*! The state has changed callback */
cllcStateChangedCB
};
static OADProtocol_RadioAccessFxns_t oadRadioAccessFxns =
{
oadRadioAccessAllocMsg,
oadRadioAccessPacketSend
};
static OADProtocol_MsgCBs_t oadMsgCallbacks =
{
/*! Incoming FW Req */
NULL,
/*! Incoming FW Version Rsp */
oadFwVersionRspCb,
/*! Incoming Image Identify Req */
NULL,
/*! Incoming Image Identify Rsp */
oadImgIdentifyRspCb,
/*! Incoming OAD Block Req */
oadBlockReqCb,
/*! Incoming OAD Block Rsp */
NULL,
/*! Incoming OAD Reset Req */
NULL,
/*! Incoming OAD Reset Rsp*/
oadResetRspCb
};
/******************************************************************************
Public Functions
*****************************************************************************/
/*!
Initialize this application.
Public function defined in collector.h
*/
void Collector_init(void)
{
OADProtocol_Params_t OADProtocol_params;
/* Initialize the collector's statistics */
memset(&Collector_statistics, 0, sizeof(Collector_statistics_t));
/* Initialize the MAC */
sem = ApiMac_init(CONFIG_FH_ENABLE);
ApiMac_mlmeSetReqUint8(ApiMac_attribute_phyCurrentDescriptorId,
(uint8_t)CONFIG_PHY_ID);
ApiMac_mlmeSetReqUint8(ApiMac_attribute_channelPage,
(uint8_t)CONFIG_CHANNEL_PAGE);
/* Initialize the Coordinator Logical Link Controller */
Cllc_init(&Collector_macCallbacks, &cllcCallbacks);
/* Register the MAC Callbacks */
ApiMac_registerCallbacks(&Collector_macCallbacks);
/* Initialize the platform specific functions */
Csf_init(sem);
/* Set the indirect persistent timeout */
if(CONFIG_MAC_BEACON_ORDER != NON_BEACON_ORDER)
{
ApiMac_mlmeSetReqUint16(ApiMac_attribute_transactionPersistenceTime,
BCN_MODE_INDIRECT_PERSISTENT_TIME);
}
else
{
ApiMac_mlmeSetReqUint16(ApiMac_attribute_transactionPersistenceTime,
INDIRECT_PERSISTENT_TIME);
}
/* Initialize PA/LNA if enabled */
ApiMac_mlmeSetReqUint8(ApiMac_attribute_rangeExtender,
(uint8_t)CONFIG_RANGE_EXT_MODE);
ApiMac_mlmeSetReqUint8(ApiMac_attribute_phyTransmitPowerSigned,
(uint8_t)CONFIG_TRANSMIT_POWER);
/* Set Min BE */
ApiMac_mlmeSetReqUint8(ApiMac_attribute_backoffExponent,
(uint8_t)CONFIG_MIN_BE);
/* Set Max BE */
ApiMac_mlmeSetReqUint8(ApiMac_attribute_maxBackoffExponent,
(uint8_t)CONFIG_MAX_BE);
/* Set MAC MAX CSMA Backoffs */
ApiMac_mlmeSetReqUint8(ApiMac_attribute_maxCsmaBackoffs,
(uint8_t)CONFIG_MAC_MAX_CSMA_BACKOFFS);
/* Set MAC MAX Frame Retries */
ApiMac_mlmeSetReqUint8(ApiMac_attribute_maxFrameRetries,
(uint8_t)CONFIG_MAX_RETRIES);
#ifdef FCS_TYPE16
/* Set the fcs type */
ApiMac_mlmeSetReqBool(ApiMac_attribute_fcsType,
(bool)1);
#endif
/* Initialize the app clocks */
initializeClocks();
if(CONFIG_FH_ENABLE && (FH_BROADCAST_DWELL_TIME > 0))
{
/* Start broadcast frame transmissions in FH mode if broadcast dwell time
* is greater than zero */
Csf_setBroadcastClock(BROADCAST_CMD_START_TIME);
}
if(CONFIG_AUTO_START)
{
/* Start the device */
Util_setEvent(&Collector_events, COLLECTOR_START_EVT);
}
OADProtocol_Params_init(&OADProtocol_params);
OADProtocol_params.pRadioAccessFxns = &oadRadioAccessFxns;
OADProtocol_params.pProtocolMsgCallbacks = &oadMsgCallbacks;
OADProtocol_open(&OADProtocol_params);
}
/*!
Application task processing.
Public function defined in collector.h
*/
void Collector_process(void)
{
/* Start the collector device in the network */
if(Collector_events & COLLECTOR_START_EVT)
{
if(cllcState == Cllc_states_initWaiting)
{
processStartEvent();
}
/* Clear the event */
Util_clearEvent(&Collector_events, COLLECTOR_START_EVT);
}
/* Is it time to send the next tracking message? */
if(Collector_events & COLLECTOR_TRACKING_TIMEOUT_EVT)
{
/* Process Tracking Event */
generateTrackingRequests();
/* Clear the event */
Util_clearEvent(&Collector_events, COLLECTOR_TRACKING_TIMEOUT_EVT);
}
/*
The generate a config request for all associated devices that need one
*/
if(Collector_events & COLLECTOR_CONFIG_EVT)
{
generateConfigRequests();
/* Clear the event */
Util_clearEvent(&Collector_events, COLLECTOR_CONFIG_EVT);
}
/*
Collector generate a broadcast command message for FH mode
*/
if(Collector_events & COLLECTOR_BROADCAST_TIMEOUT_EVT)
{
/* Clear the event */
Util_clearEvent(&Collector_events, COLLECTOR_BROADCAST_TIMEOUT_EVT);
if(FH_BROADCAST_INTERVAL > 0 && (!CERTIFICATION_TEST_MODE))
{
generateBroadcastCmd();
/* set clock for next broadcast command */
Csf_setBroadcastClock(FH_BROADCAST_INTERVAL);
}
}
/* Process LLC Events */
Cllc_process();
/* Allow the Specific functions to process */
Csf_processEvents();
/*
Don't process ApiMac messages until all of the collector events
are processed.
*/
if(Collector_events == 0)
{
/* Wait for response message or events */
ApiMac_processIncoming();
}
}
/*!
Build and send the configuration message to a device.
Public function defined in collector.h
*/
Collector_status_t Collector_sendConfigRequest(ApiMac_sAddr_t *pDstAddr,
uint16_t frameControl,
uint32_t reportingInterval,
uint32_t pollingInterval)
{
Collector_status_t status = Collector_status_invalid_state;
/* Are we in the right state? */
if(cllcState >= Cllc_states_started)
{
Llc_deviceListItem_t item;
/* Is the device a known device? */
if(Csf_getDevice(pDstAddr, &item))
{
uint8_t buffer[SMSGS_CONFIG_REQUEST_MSG_LENGTH];
uint8_t *pBuf = buffer;
/* Build the message */
*pBuf++ = (uint8_t)Smsgs_cmdIds_configReq;
*pBuf++ = Util_loUint16(frameControl);
*pBuf++ = Util_hiUint16(frameControl);
*pBuf++ = Util_breakUint32(reportingInterval, 0);
*pBuf++ = Util_breakUint32(reportingInterval, 1);
*pBuf++ = Util_breakUint32(reportingInterval, 2);
*pBuf++ = Util_breakUint32(reportingInterval, 3);
*pBuf++ = Util_breakUint32(pollingInterval, 0);
*pBuf++ = Util_breakUint32(pollingInterval, 1);
*pBuf++ = Util_breakUint32(pollingInterval, 2);
*pBuf = Util_breakUint32(pollingInterval, 3);
if((sendMsg(Smsgs_cmdIds_configReq, item.devInfo.shortAddress,
item.capInfo.rxOnWhenIdle,
(SMSGS_CONFIG_REQUEST_MSG_LENGTH),
buffer)) == true)
{
status = Collector_status_success;
Collector_statistics.configRequestAttempts++;
/* set timer for retry in case response is not received */
Csf_setConfigClock(CONFIG_DELAY);
}
else
{
processConfigRetry();
}
}
}
return (status);
}
/*!
Update the collector statistics
Public function defined in collector.h
*/
void Collector_updateStats( void )
{
/* update the stats from the MAC */
ApiMac_mlmeGetReqUint32(ApiMac_attribute_diagRxSecureFail,
&Collector_statistics.rxDecryptFailures);
ApiMac_mlmeGetReqUint32(ApiMac_attribute_diagTxSecureFail,
&Collector_statistics.txEncryptFailures);
}
/*!
Build and send the toggle led message to a device.
Public function defined in collector.h
*/
Collector_status_t Collector_sendToggleLedRequest(ApiMac_sAddr_t *pDstAddr)
{
Collector_status_t status = Collector_status_invalid_state;
/* Are we in the right state? */
if(cllcState >= Cllc_states_started)
{
Llc_deviceListItem_t item;
/* Is the device a known device? */
if(Csf_getDevice(pDstAddr, &item))
{
uint8_t buffer[SMSGS_TOGGLE_LED_REQUEST_MSG_LEN];
/* Build the message */
buffer[0] = (uint8_t)Smsgs_cmdIds_toggleLedReq;
sendMsg(Smsgs_cmdIds_toggleLedReq, item.devInfo.shortAddress,
item.capInfo.rxOnWhenIdle,
SMSGS_TOGGLE_LED_REQUEST_MSG_LEN,
buffer);
status = Collector_status_success;
}
else
{
status = Collector_status_deviceNotFound;
}
}
return(status);
}
Collector_status_t Collector_sendCustomCommand(ApiMac_sAddr_t *pDstAddr, uint8_t *state, uint16_t length) {
Collector_status_t status = Collector_status_invalid_state;
if(cllcState >= Cllc_states_started)
{
Llc_deviceListItem_t item;
if(Csf_getDevice(pDstAddr, &item))
{
uint8_t buffer[length+sizeof(uint8_t)];
uint8_t *pBuf = buffer;
*pBuf = (uint8_t)Smsgs_cmdIds_customCommand;
int i = 0;
for(i = 0; i < length; i++ ) {
pBuf[i+1] = *(state+i);
}
sendMsg(Smsgs_cmdIds_customCommand, item.devInfo.shortAddress,
item.capInfo.rxOnWhenIdle,
length+1,
buffer);
status = Collector_status_success;
}
else
{
status = Collector_status_deviceNotFound;
}
}
return(status);
}
/*!
* @brief Build and send the device type request message to a device.
*
* @param pDstAddr - destination address of the device to send the message
*
* @return Collector_status_success, Collector_status_invalid_state
* or Collector_status_deviceNotFound
*
* Public function defined in collector.h
*/
Collector_status_t Collector_sendDeviceTypeRequest(ApiMac_sAddr_t *pDstAddr)
{
Collector_status_t status = Collector_status_invalid_state;
/* Are we in the right state? */
if(cllcState >= Cllc_states_started)
{
Llc_deviceListItem_t item;
/* Is the device a known device? */
if(Csf_getDevice(pDstAddr, &item))
{
uint8_t buffer[SMSGS_DEVICE_TYPE_REQUEST_MSG_LEN];
/* Build the message */
buffer[0] = (uint8_t)Smsgs_cmdIds_DeviceTypeReq;
sendMsg(Smsgs_cmdIds_DeviceTypeReq, item.devInfo.shortAddress,
item.capInfo.rxOnWhenIdle,
SMSGS_DEVICE_TYPE_REQUEST_MSG_LEN,
buffer);
status = Collector_status_success;
}
else
{
status = Collector_status_deviceNotFound;
}
}
return(status);
}
/*!
Stores the file name associated with the file_id provided
in file_name
Returns
Public function defined in collector.h
*/
Collector_status_t Collector_getFileName(uint32_t file_id, char* file_name, size_t max_len)
{
Collector_status_t status;
if (file_id >= MAX_OAD_FILES)
{
status = Collector_status_invalid_file_id;
}
else
{
strncpy(file_name, basename(oad_file_list[file_id].oad_file), max_len);
status = Collector_status_success;
}
return status;
}
/*!
updates the FW list.
Public function defined in collector.h
*/
uint32_t Collector_updateFwList(char *new_oad_file)
{
uint32_t oad_file_idx;
uint32_t oad_file_id;
bool found = false;
LOG_printf( LOG_DBG_COLLECTOR, "Collector_updateFwList: new oad file: %s\n",
new_oad_file);
/* Does OAD file exist */
for(oad_file_idx = 0; oad_file_idx < MAX_OAD_FILES; oad_file_idx++)
{
if(strcmp(new_oad_file, oad_file_list[oad_file_idx].oad_file) == 0)
{
LOG_printf( LOG_DBG_COLLECTOR, "Collector_updateFwList: found ID: %d\n",
oad_file_list[oad_file_idx].oad_file_id);
oad_file_id = oad_file_list[oad_file_idx].oad_file_id;
found = true;
break;
}
}
if(!found)
{
static uint32_t latest_oad_file_idx = 0;
static uint32_t latest_oad_file_id = 0;
oad_file_id = latest_oad_file_id;
oad_file_list[latest_oad_file_idx].oad_file_id = oad_file_id;
strncpy(oad_file_list[latest_oad_file_idx].oad_file, new_oad_file, 256);
LOG_printf( LOG_DBG_COLLECTOR, "Collector_updateFwList: Added %s, ID %d\n",
oad_file_list[latest_oad_file_idx].oad_file,
oad_file_list[latest_oad_file_idx].oad_file_id);
latest_oad_file_id++;
latest_oad_file_idx++;
if(latest_oad_file_idx == MAX_OAD_FILES)
{
latest_oad_file_idx = 0;
}
}
return oad_file_id;
}
/*!
Send OAD version request message.
Public function defined in collector.h
*/
Collector_status_t Collector_sendFwVersionRequest(ApiMac_sAddr_t *pDstAddr)
{
Collector_status_t status = Collector_status_invalid_state;
if(OADProtocol_sendFwVersionReq((void*) pDstAddr) == OADProtocol_Status_Success)
{
status = Collector_status_success;
}
return status;
}
/*!
Send OAD Target Reset Request message.
Public function defined in collector.h
*/
Collector_status_t Collector_sendResetReq(ApiMac_sAddr_t *pDstAddr)
{
Collector_status_t status = Collector_status_invalid_state;
if(OADProtocol_sendOadResetReq((void*) pDstAddr) == OADProtocol_Status_Success)
{
status = Collector_status_success;
}
return status;
}
/*!
Send OAD version request message.
Public function defined in collector.h
*/
Collector_status_t Collector_startFwUpdate(ApiMac_sAddr_t *pDstAddr, uint32_t oad_file_id)
{
Collector_status_t status = Collector_status_invalid_state;
uint8_t imgInfoData[OADProtocol_AGAMA_IMAGE_HDR_LEN];
uint32_t oad_file_idx;
FILE *oadFile;
for(oad_file_idx = 0; oad_file_idx < MAX_OAD_FILES; oad_file_idx++)
{
if(oad_file_list[oad_file_idx].oad_file_id == oad_file_id)
{
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: opening file: %s\n",
oad_file_list[oad_file_idx].oad_file);
oadFile = fopen(oad_file_list[oad_file_idx].oad_file, "r");
break;
}
}
if(oadFile)
{
const uint32_t chameleonOADHdrLen = 16;
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: opened file....\n");
//Seek set to start of oad header in the image
fseek(oadFile, IMG_HDR_ADDR, SEEK_SET);
//Read the first chameleonOADHdrLen bytes to determine between Agama and Chameleon
fread(imgInfoData, 1, chameleonOADHdrLen, oadFile);
OADStorage_imgIdentifyPld_t imgIdPld;
imgHdr_t* pImgHdr = (imgHdr_t*) imgInfoData;
if((strncmp((const char*) pImgHdr->fixedHdr.imgID, CC26X2_OAD_IMG_ID_VAL, OAD_IMG_ID_LEN ) == 0)
|| (strncmp((const char*) pImgHdr->fixedHdr.imgID, CC13X2_OAD_IMG_ID_VAL, OAD_IMG_ID_LEN) == 0))
{
LOG_printf( LOG_DBG_COLLECTOR, "collector_startfwupdate: Binary identified as an Agama image\n");
/*
* Since it is known to be an Agama binary, the rest of the header must be read from the file
* into the imgInfoData buffer.
*/
uint32_t remReadLen = OADProtocol_AGAMA_IMAGE_HDR_LEN - chameleonOADHdrLen;
if(fread(&imgInfoData[chameleonOADHdrLen], 1, remReadLen, oadFile) == remReadLen)
{
//copy image identify payload
memcpy(imgIdPld.imgID, pImgHdr->fixedHdr.imgID, 8);
imgIdPld.bimVer = pImgHdr->fixedHdr.bimVer;
imgIdPld.metaVer = pImgHdr->fixedHdr.metaVer;
imgIdPld.imgCpStat = pImgHdr->fixedHdr.imgCpStat;
imgIdPld.crcStat = pImgHdr->fixedHdr.crcStat;
imgIdPld.imgType = pImgHdr->fixedHdr.imgType;
imgIdPld.imgNo = pImgHdr->fixedHdr.imgNo;
imgIdPld.len = pImgHdr->fixedHdr.len;
memcpy(imgIdPld.softVer, pImgHdr->fixedHdr.softVer, 4);
// Check if binary is a delta image
imgIdPld.isDeltaImg = false;
long deltaSegPos = findDeltaSeg(oadFile);
if(deltaSegPos != DELTA_SEG_NOT_FOUND)
{
uint8_t oadSeg[DELTA_SEG_LEN];
fseek(oadFile, deltaSegPos, SEEK_SET);
fread(oadSeg, 1, DELTA_SEG_LEN, oadFile);
// Binaries may have delta segments, but not have a delta payload
if (oadSeg[DELTA_SEG_IS_DELTA_IMG_OFFSET])
{
imgIdPld.isDeltaImg = true;
imgIdPld.toadMetaVer = oadSeg[DELTA_SEG_HEADER_VERSION_OFFSET];
imgIdPld.toadVer = oadSeg[DELTA_SEG_VERSION_OFFSET];
imgIdPld.memoryCfg = oadSeg[DELTA_SEG_MEMORY_CFG_OFFSET];
imgIdPld.oldImgCrc = *((uint32_t*)(&oadSeg[DELTA_SEG_OLD_IMG_CRC_OFFSET]));
imgIdPld.newImgLen = *((uint32_t*)(&oadSeg[DELTA_SEG_NEW_IMG_LEN_OFFSET]));
}
}
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: sending ImgIdentifyReq, Img Len 0x%x\n",
pImgHdr->fixedHdr.len);
oadBNumBlocks = pImgHdr->fixedHdr.len / OAD_BLOCK_SIZE;
if(pImgHdr->fixedHdr.len % OAD_BLOCK_SIZE)
{
//there are some remaining bytes in an additional block
oadBNumBlocks++;
}
if(OADProtocol_sendImgIdentifyReq((void*) pDstAddr, oad_file_id,
(uint8_t*) &imgIdPld) == OADProtocol_Status_Success)
{
status = Collector_status_success;
}
}
else
{
LOG_printf(LOG_DBG_COLLECTOR, "Collector_startFwUpdate: Error reading remaining %u bytes of header",
remReadLen);
status = Collector_status_invalid_file;
}
}
else
{
/*
* If it is not an Agama image, we must determine between a Turbo OAD image and a regular
* Chameleon image. The header of a Chameleon image does not contain a unique ID val.
* Therefor we will use the unique ID val of a Turbo OAD image to destinguish between them.
*/
//Turbo OAD header is always placed at 0x00 no matter where TIRTOS was defined to be.
fseek(oadFile, 0x00, SEEK_SET);
if(fread(imgInfoData, 1, TURBO_OAD_HEADER_LEN, oadFile) == TURBO_OAD_HEADER_LEN)
{
if(strncmp((const char *)&imgInfoData[16], "TURBOOAD", sizeof("TURBOOAD")) != 0)
{
//If it is not a TURBO OAD image we must assume it is a regular Chameleon image
fseek(oadFile, IMG_HDR_ADDR, SEEK_SET);
fread(imgInfoData, 1, chameleonOADHdrLen, oadFile);
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: Binary identified as a Chameleon image\n");
}
else
{
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: Binary identified as a Turbo image\n");
}
uint32_t oadImgLen = ((imgInfoData[6]) | (imgInfoData[7] << 8));
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: sending ImgIdentifyReq, Img Len 0x%x\n",
oadImgLen);
oadBNumBlocks = oadImgLen / (OAD_BLOCK_SIZE >> 2);
if(oadImgLen < (OAD_BLOCK_SIZE >> 2))
{
//necessary when oadImgLen is less than one block (common in Turbo oad)
oadBNumBlocks++;
}
else if(oadImgLen % (OAD_BLOCK_SIZE >> 2))
{
//there are some remaining bytes in an additional block
oadBNumBlocks++;
}
if(OADProtocol_sendImgIdentifyReq((void*) pDstAddr, oad_file_id, imgInfoData)
== OADProtocol_Status_Success)
{
status = Collector_status_success;
}
}
else
{
LOG_printf(LOG_DBG_COLLECTOR, "Collector_startFwUpdate: Error reading first 64 bytes of binary");
status = Collector_status_invalid_file;
}
}
fclose(oadFile);
}
else
{
LOG_printf( LOG_DBG_COLLECTOR, "Collector_startFwUpdate: could not open file: %s\n",
oad_file_list[oad_file_idx].oad_file);
status = Collector_status_invalid_file;
}
return status;
}
/*!
Find if a device is present.
Public function defined in collector.h
*/
Collector_status_t Collector_findDevice(ApiMac_sAddr_t *pAddr)
{
Collector_status_t status = Collector_status_deviceNotFound;
if(findDevice(pAddr))
{
status = Collector_status_success;
}
return status;
}
/******************************************************************************
Local Functions
*****************************************************************************/
/*!
* @brief Initialize the clocks.
*/
static void initializeClocks(void)
{
/* Initialize the tracking clock */
Csf_initializeTrackingClock();
Csf_initializeConfigClock();
Csf_initializeBroadcastClock();
Csf_initializeIdentifyClock();
}
/*!
* @brief CLLC Started callback.
*
* @param pStartedInfo - pointer to network information
*/
static void cllcStartedCB(Llc_netInfo_t *pStartedInfo)
{
devicePanId = pStartedInfo->devInfo.panID;
if(pStartedInfo->fh == true)
{
fhEnabled = true;
}
/* updated the user */
Csf_networkUpdate(restarted, pStartedInfo);
/* Start the tracking clock */
Csf_setTrackingClock(TRACKING_DELAY_TIME);
}
/*!