-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzcl.c
4908 lines (4278 loc) · 145 KB
/
zcl.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
/**************************************************************************************************
Filename: zcl.c
Revised: $Date: 2014-06-25 18:07:01 -0700 (Wed, 25 Jun 2014) $
Revision: $Revision: 39221 $
Description: This file contains the Zigbee Cluster Library Foundation functions.
Copyright 2006-2014 Texas Instruments Incorporated. All rights reserved.
IMPORTANT: Your use of this Software is limited to those specific rights
granted under the terms of a software license agreement between the user
who downloaded the software, his/her employer (which must be your employer)
and Texas Instruments Incorporated (the "License"). You may not use this
Software unless you agree to abide by the terms of the License. The License
limits your use, and you acknowledge, that the Software may not be modified,
copied or distributed unless embedded on a Texas Instruments microcontroller
or used solely and exclusively in conjunction with a Texas Instruments radio
frequency transceiver, which is integrated into your product. Other than for
the foregoing purpose, you may not use, reproduce, copy, prepare derivative
works of, modify, distribute, perform, display or sell this Software and/or
its documentation for any purpose.
YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
Should you have any questions regarding your right to use this Software,
contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/
/*********************************************************************
* INCLUDES
*/
#include "ZComDef.h"
#include "AF.h"
#include "zcl.h"
#include "zcl_general.h"
#if defined ( INTER_PAN )
#include "stub_aps.h"
#endif
/*********************************************************************
* MACROS
*/
/*** Frame Control ***/
#define zcl_FCType( a ) ( (a) & ZCL_FRAME_CONTROL_TYPE )
#define zcl_FCManuSpecific( a ) ( (a) & ZCL_FRAME_CONTROL_MANU_SPECIFIC )
#define zcl_FCDirection( a ) ( (a) & ZCL_FRAME_CONTROL_DIRECTION )
#define zcl_FCDisableDefaultRsp( a ) ( (a) & ZCL_FRAME_CONTROL_DISABLE_DEFAULT_RSP )
/*** Attribute Access Control ***/
#define zcl_AccessCtrlRead( a ) ( (a) & ACCESS_CONTROL_READ )
#define zcl_AccessCtrlWrite( a ) ( (a) & ACCESS_CONTROL_WRITE )
#define zcl_AccessCtrlCmd( a ) ( (a) & ACCESS_CONTROL_CMD )
#define zcl_AccessCtrlAuthRead( a ) ( (a) & ACCESS_CONTROL_AUTH_READ )
#define zcl_AccessCtrlAuthWrite( a ) ( (a) & ACCESS_CONTROL_AUTH_WRITE )
#define zclParseCmd( a, b ) zclCmdTable[(a)].pfnParseInProfile( (b) )
#define zclProcessCmd( a, b ) zclCmdTable[(a)].pfnProcessInProfile( (b) )
#define zcl_DefaultRspCmd( zclHdr ) ( zcl_ProfileCmd( (zclHdr).fc.type ) && \
(zclHdr).fc.manuSpecific == 0 && \
(zclHdr).commandID == ZCL_CMD_DEFAULT_RSP )
// Commands that have corresponding responses
#define CMD_HAS_RSP( cmd ) ( (cmd) == ZCL_CMD_READ || \
(cmd) == ZCL_CMD_WRITE || \
(cmd) == ZCL_CMD_WRITE_UNDIVIDED || \
(cmd) == ZCL_CMD_CONFIG_REPORT || \
(cmd) == ZCL_CMD_READ_REPORT_CFG || \
(cmd) == ZCL_CMD_DISCOVER_ATTRS || \
(cmd) == ZCL_CMD_DISCOVER_CMDS_RECEIVED || \
(cmd) == ZCL_CMD_DISCOVER_CMDS_GEN || \
(cmd) == ZCL_CMD_DISCOVER_ATTRS_EXT || \
(cmd) == ZCL_CMD_DEFAULT_RSP ) // exception
/*********************************************************************
* CONSTANTS
*/
/*********************************************************************
* TYPEDEFS
*/
typedef struct zclLibPlugin
{
struct zclLibPlugin *next;
uint16 startClusterID; // starting cluster ID
uint16 endClusterID; // ending cluster ID
zclInHdlr_t pfnIncomingHdlr; // function to handle incoming message
} zclLibPlugin_t;
// Command record list
typedef struct zclCmdRecsList
{
struct zclCmdRecsList *pNext;
uint8 endpoint;
uint8 numCommands;
CONST zclCommandRec_t *pCmdRecs;
} zclCmdRecsList_t;
// Attribute record list item
typedef struct zclAttrRecsList
{
struct zclAttrRecsList *next;
uint8 endpoint; // Used to link it into the endpoint descriptor
zclReadWriteCB_t pfnReadWriteCB;// Read or Write attribute value callback function
zclAuthorizeCB_t pfnAuthorizeCB;// Authorize Read or Write operation
uint8 numAttributes; // Number of the following records
CONST zclAttrRec_t *attrs; // attribute records
} zclAttrRecsList;
/* #ifdef ZCL_REPORT
// Config Report record list item
typedef struct zclConfigReportRecsList
{
struct zclConfigReportRecsList *next;
uint8 endpoint; // Used to link it into the endpoint descriptor
uint8 numConfigReportRec; // Number of the following records
CONST zclConfigReportRec_t *configReportRecs; // configReportRecs records
} zclConfigReportRecsList;
#endif // ZCL_REPORT */
// Cluster option list item
typedef struct zclClusterOptionList
{
struct zclClusterOptionList *next;
uint8 endpoint; // Used to link it into the endpoint descriptor
uint8 numOptions; // Number of the following records
zclOptionRec_t *options; // option records
} zclClusterOptionList;
typedef void *(*zclParseInProfileCmd_t)( zclParseCmd_t *pCmd );
typedef uint8 (*zclProcessInProfileCmd_t)( zclIncoming_t *pInMsg );
typedef struct
{
zclParseInProfileCmd_t pfnParseInProfile;
zclProcessInProfileCmd_t pfnProcessInProfile;
} zclCmdItems_t;
/*********************************************************************
* GLOBAL VARIABLES
*/
#if !defined ( ZCL_STANDALONE )
uint8 zcl_TaskID;
// The task Id of the Application where the unprocessed Foundation
// Command/Response messages will be sent to.
uint8 zcl_RegisteredMsgTaskID = TASK_NO_TASK;
#endif
// The Application should register its attribute data validation function
zclValidateAttrData_t zcl_ValidateAttrDataCB = (zclValidateAttrData_t)NULL;
// ZCL Sequence number
uint8 zcl_SeqNum = 0x00;
uint8 zcl_TransID = 0; // This is the unique message ID (counter)
static uint8 savedZCLTransSeqNum = 0;
/*********************************************************************
* EXTERNAL VARIABLES
*/
/*********************************************************************
* EXTERNAL FUNCTIONS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
static zclLibPlugin_t *plugins = (zclLibPlugin_t *)NULL;
#if defined ( ZCL_DISCOVER )
static zclCmdRecsList_t *gpCmdList = (zclCmdRecsList_t *)NULL;
#endif
static zclAttrRecsList *attrList = (zclAttrRecsList *)NULL;
static zclClusterOptionList *clusterOptionList = (zclClusterOptionList *)NULL;
//- add by simen
#if defined ( ZCL_REPORT )
static zclConfigReportRecsList *configReportRecsList = (zclConfigReportRecsList *)NULL;
#endif
static afIncomingMSGPacket_t *rawAFMsg = (afIncomingMSGPacket_t *)NULL;
/*********************************************************************
* LOCAL FUNCTIONS
*/
static uint8 *zclBuildHdr( zclFrameHdr_t *hdr, uint8 *pData );
static uint8 zclCalcHdrSize( zclFrameHdr_t *hdr );
static zclLibPlugin_t *zclFindPlugin( uint16 clusterID, uint16 profileID );
#if defined ( ZCL_DISCOVER )
static zclCmdRecsList_t *zclFindCmdRecsList( uint8 endpoint );
#endif
static zclAttrRecsList *zclFindAttrRecsList( uint8 endpoint );
static zclOptionRec_t *zclFindClusterOption( uint8 endpoint, uint16 clusterID );
static uint8 zclGetClusterOption( uint8 endpoint, uint16 clusterID );
static void zclSetSecurityOption( uint8 endpoint, uint16 clusterID, uint8 enable );
static uint8 zcl_DeviceOperational( uint8 srcEP, uint16 clusterID, uint8 frameType, uint8 cmd, uint16 profileID );
#if defined ( ZCL_READ ) || defined ( ZCL_WRITE )
static zclReadWriteCB_t zclGetReadWriteCB( uint8 endpoint );
static zclAuthorizeCB_t zclGetAuthorizeCB( uint8 endpoint );
#endif // ZCL_READ || ZCL_WRITE
#ifdef ZCL_READ
ZStatus_t zclReadAttrData( uint8 *pAttrData, zclAttrRec_t *pAttr, uint16 *pDataLen );
static uint16 zclGetAttrDataLengthUsingCB( uint8 endpoint, uint16 clusterID, uint16 attrId );
static ZStatus_t zclReadAttrDataUsingCB( uint8 endpoint, uint16 clusterId, uint16 attrId,
uint8 *pAttrData, uint16 *pDataLen );
static ZStatus_t zclAuthorizeRead( uint8 endpoint, afAddrType_t *srcAddr, zclAttrRec_t *pAttr );
static void *zclParseInReadRspCmd( zclParseCmd_t *pCmd );
static uint8 zclProcessInReadCmd( zclIncoming_t *pInMsg );
#endif // ZCL_READ
#ifdef ZCL_WRITE
static ZStatus_t zclWriteAttrData( uint8 endpoint, afAddrType_t *srcAddr,
zclAttrRec_t *pAttr, zclWriteRec_t *pWriteRec );
static ZStatus_t zclWriteAttrDataUsingCB( uint8 endpoint, afAddrType_t *srcAddr,
zclAttrRec_t *pAttr, uint8 *pAttrData );
static ZStatus_t zclAuthorizeWrite( uint8 endpoint, afAddrType_t *srcAddr, zclAttrRec_t *pAttr );
static void *zclParseInWriteRspCmd( zclParseCmd_t *pCmd );
static uint8 zclProcessInWriteCmd( zclIncoming_t *pInMsg );
static uint8 zclProcessInWriteUndividedCmd( zclIncoming_t *pInMsg );
#endif // ZCL_WRITE
#ifdef ZCL_REPORT
static void *zclParseInConfigReportRspCmd( zclParseCmd_t *pCmd );
static void *zclParseInReadReportCfgRspCmd( zclParseCmd_t *pCmd );
//- add by simen
//static zclConfigReportRecsList *zclFindConfigReportRecsList( uint8 endpoint );
#endif // ZCL_REPORT
static void *zclParseInDefaultRspCmd( zclParseCmd_t *pCmd );
#ifdef ZCL_DISCOVER
static uint8 zclFindNextCmdRec( uint8 endpoint, uint16 clusterID, uint8 commandID, uint8 direction, uint8 *pCmdID, zclCommandRec_t *pCmd );
static uint8 zclFindNextAttrRec( uint8 endpoint, uint16 clusterID, uint8 direction, uint16 *attrId, zclAttrRec_t *pAttr );
static void *zclParseInDiscCmdsRspCmd( zclParseCmd_t *pCmd );
static void *zclParseInDiscAttrsRspCmd( zclParseCmd_t *pCmd );
static void *zclParseInDiscAttrsExtRspCmd( zclParseCmd_t *pCmd );
static uint8 zclProcessInDiscCmd( zclIncoming_t *pInMsg );
static uint8 zclProcessInDiscAttrs( zclIncoming_t *pInMsg );
static void zclProcessInDiscAttrsCmd( zclIncoming_t *pInMsg, zclDiscoverAttrsCmd_t *pDiscoverCmd, uint8 attrLenBuf );
static void zclProcessInDiscAttrsExtCmd( zclIncoming_t *pInMsg, zclDiscoverAttrsCmd_t *pDiscoverCmd, uint8 attrLenBuf );
#endif // ZCL_DISCOVER
/*********************************************************************
* Parse Profile Command Function Table
*/
static CONST zclCmdItems_t zclCmdTable[] =
{
#ifdef ZCL_READ
/* ZCL_CMD_READ */ { zclParseInReadCmd, zclProcessInReadCmd },
/* ZCL_CMD_READ_RSP */ { zclParseInReadRspCmd, zcl_HandleExternal },
#else
/* ZCL_CMD_READ */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_READ_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
#endif // ZCL_READ
#ifdef ZCL_WRITE
/* ZCL_CMD_WRITE */ { zclParseInWriteCmd, zclProcessInWriteCmd },
/* ZCL_CMD_WRITE_UNDIVIDED */ { zclParseInWriteCmd, zclProcessInWriteUndividedCmd },
/* ZCL_CMD_WRITE_RSP */ { zclParseInWriteRspCmd, zcl_HandleExternal },
/* ZCL_CMD_WRITE_NO_RSP */ { zclParseInWriteCmd, zclProcessInWriteCmd },
#else
/* ZCL_CMD_WRITE */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_WRITE_UNDIVIDED */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_WRITE_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_WRITE_NO_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
#endif // ZCL_WRITE
#ifdef ZCL_REPORT
/* ZCL_CMD_CONFIG_REPORT */ { zclParseInConfigReportCmd, zcl_HandleExternal },
/* ZCL_CMD_CONFIG_REPORT_RSP */ { zclParseInConfigReportRspCmd, zcl_HandleExternal },
/* ZCL_CMD_READ_REPORT_CFG */ { zclParseInReadReportCfgCmd, zcl_HandleExternal },
/* ZCL_CMD_READ_REPORT_CFG_RSP */ { zclParseInReadReportCfgRspCmd, zcl_HandleExternal },
/* ZCL_CMD_REPORT */ { zclParseInReportCmd, zcl_HandleExternal },
#else
/* ZCL_CMD_CONFIG_REPORT */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_CONFIG_REPORT_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_READ_REPORT_CFG */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_READ_REPORT_CFG_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_REPORT */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
#endif // ZCL_REPORT
/* ZCL_CMD_DEFAULT_RSP */ { zclParseInDefaultRspCmd, zcl_HandleExternal },
#ifdef ZCL_DISCOVER
/* ZCL_CMD_DISCOVER_ATTRS */ { zclParseInDiscAttrsCmd, zclProcessInDiscAttrs },
/* ZCL_CMD_DISCOVER_ATTRS_RSP */ { zclParseInDiscAttrsRspCmd, zcl_HandleExternal },
/* *not supported* READ_ATTRS_STRCT */ { NULL, (zclProcessInProfileCmd_t)NULL },
/* *not supported* WRITE_ATTRS_STRCT */ { NULL, (zclProcessInProfileCmd_t)NULL },
/* *not supported* WRITE_ATTRS_STRCT_RSP */ { NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_CMDS_RECEIVED */ { zclParseInDiscCmdsCmd, zclProcessInDiscCmd },
/* ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP */ { zclParseInDiscCmdsRspCmd, zcl_HandleExternal },
/* ZCL_CMD_DISCOVER_CMDS_GEN */ { zclParseInDiscCmdsCmd, zclProcessInDiscCmd },
/* ZCL_CMD_DISCOVER_CMDS_GEN_RSP */ { zclParseInDiscCmdsRspCmd, zcl_HandleExternal },
/* ZCL_CMD_DISCOVER_ATTRS_EXT */ { zclParseInDiscAttrsCmd, zclProcessInDiscAttrs },
/* ZCL_CMD_DISCOVER_ATTRS_EXT_RSP */ { zclParseInDiscAttrsExtRspCmd, zcl_HandleExternal },
#else
/* ZCL_CMD_DISCOVER_ATTRS */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_ATTRS_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* *not supported* READ_ATTRS_STRCT */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* *not supported* WRITE_ATTRS_STRCT */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* *not supported* WRITE_ATTRS_STRCT_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_CMDS_RECEIVED */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_CMDS_GEN */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_CMDS_GEN_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_ATTRS_EXT */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
/* ZCL_CMD_DISCOVER_ATTRS_EXT_RSP */ { (zclParseInProfileCmd_t)NULL, (zclProcessInProfileCmd_t)NULL },
#endif // ZCL_DISCOVER
};
/*********************************************************************
* PUBLIC FUNCTIONS
*********************************************************************/
#if !defined ( ZCL_STANDALONE )
/*********************************************************************
* @fn zcl_Init
*
* @brief Initialization function for the zcl layer.
*
* @param task_id - ZCL task id
*
* @return none
*/
void zcl_Init( uint8 task_id )
{
zcl_TaskID = task_id;
}
#endif
#if !defined ( ZCL_STANDALONE )
/*********************************************************************
* @fn zcl_event_loop
*
* @brief Event Loop Processor for zcl.
*
* @param task_id - task id
* @param events - event bitmap
*
* @return unprocessed events
*/
uint16 zcl_event_loop( uint8 task_id, uint16 events )
{
uint8 *msgPtr;
(void)task_id; // Intentionally unreferenced parameter
if ( events & SYS_EVENT_MSG )
{
msgPtr = osal_msg_receive( zcl_TaskID );
while ( msgPtr != NULL )
{
uint8 dealloc = TRUE;
if ( *msgPtr == AF_INCOMING_MSG_CMD )
{
zcl_ProcessMessageMSG( (afIncomingMSGPacket_t *)msgPtr );
}
else if ( zcl_RegisteredMsgTaskID != TASK_NO_TASK )
{
// send it to another task to process.
osal_msg_send( zcl_RegisteredMsgTaskID, msgPtr );
dealloc = FALSE;
}
// Release the memory
if ( dealloc )
{
osal_msg_deallocate( msgPtr );
}
// Next
msgPtr = osal_msg_receive( zcl_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
// Discard unknown events
return 0;
}
#endif
#if !defined ( ZCL_STANDALONE )
/*********************************************************************
* @fn zcl_registerForMsg
*
* @brief The ZCL is setup to send all incoming Foundation Command/Response
* messages that aren't processed to one task (if a task is
* registered).
*
* @param taskId - task Id of the Application where commands will be sent to
*
* @return TRUE if task registeration successful, FALSE otherwise
*********************************************************************/
uint8 zcl_registerForMsg( uint8 taskId )
{
// Allow only the first task
if ( zcl_RegisteredMsgTaskID == TASK_NO_TASK )
{
zcl_RegisteredMsgTaskID = taskId;
return ( true );
}
return ( false );
}
#endif
#if !defined ( ZCL_STANDALONE )
/*********************************************************************
* @fn zcl_HandleExternal
*
* @brief
*
* @param pInMsg - incoming message to process
*
* @return TRUE
*/
uint8 zcl_HandleExternal( zclIncoming_t *pInMsg )
{
zclIncomingMsg_t *pCmd;
if ( zcl_RegisteredMsgTaskID == TASK_NO_TASK )
{
return ( TRUE );
}
pCmd = (zclIncomingMsg_t *)osal_msg_allocate( sizeof ( zclIncomingMsg_t ) );
if ( pCmd != NULL )
{
// fill in the message
pCmd->hdr.event = ZCL_INCOMING_MSG;
pCmd->zclHdr = pInMsg->hdr;
pCmd->clusterId = pInMsg->msg->clusterId;
pCmd->srcAddr = pInMsg->msg->srcAddr;
pCmd->endPoint = pInMsg->msg->endPoint;
pCmd->attrCmd = pInMsg->attrCmd;
// Application will free the attrCmd buffer
pInMsg->attrCmd = NULL;
/* send message through task message */
osal_msg_send( zcl_RegisteredMsgTaskID, (uint8 *)pCmd );
}
return ( TRUE );
}
#endif
/*********************************************************************
* @fn zcl_getRawAFMsg
*
* @brief Call to get original unprocessed AF message
* (not parsed by ZCL).
*
* NOTE: This function can only be called during a ZCL callback function
* and the calling function must NOT change any data in the message.
*
* @param none
*
* @return pointer to original AF message, NULL if not processing
* AF message.
*/
afIncomingMSGPacket_t *zcl_getRawAFMsg( void )
{
return ( rawAFMsg );
}
/*********************************************************************
* @fn zcl_getParsedTransSeqNum
*
* @brief Call to the get the transaction sequence number from
* the incoming message.
*
* NOTE: This function can only be called during a ZCL callback function
* and the calling function must NOT change any data in the message.
*
* @param none
*
* @return transaction sequence number.
*/
uint8 zcl_getParsedTransSeqNum( void )
{
return ( savedZCLTransSeqNum );
}
/*********************************************************************
* @fn zcl_registerPlugin
*
* @brief Add a Cluster Library handler
*
* @param startClusterID - starting cluster ID
* @param endClusterID - ending cluster ID
* @param pfnHdlr - function pointer to incoming message handler
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_registerPlugin( uint16 startClusterID,
uint16 endClusterID, zclInHdlr_t pfnIncomingHdlr )
{
zclLibPlugin_t *pNewItem;
zclLibPlugin_t *pLoop;
// Fill in the new profile list
pNewItem = zcl_mem_alloc( sizeof( zclLibPlugin_t ) );
if ( pNewItem == NULL )
{
return (ZMemError);
}
// Fill in the plugin record.
pNewItem->next = (zclLibPlugin_t *)NULL;
pNewItem->startClusterID = startClusterID;
pNewItem->endClusterID = endClusterID;
pNewItem->pfnIncomingHdlr = pfnIncomingHdlr;
// Find spot in list
if ( plugins == NULL )
{
plugins = pNewItem;
}
else
{
// Look for end of list
pLoop = plugins;
while ( pLoop->next != NULL )
{
pLoop = pLoop->next;
}
// Put new item at end of list
pLoop->next = pNewItem;
}
return ( ZSuccess );
}
#ifdef ZCL_DISCOVER
/*********************************************************************
* @fn zcl_registerCmdList
*
* @brief Register a Command List with ZCL Foundation
*
* @param endpoint - endpoint the attribute list belongs to
* @param newCmdList - array of command records
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_registerCmdList( uint8 endpoint, CONST uint8 cmdListSize, CONST zclCommandRec_t newCmdList[] )
{
zclCmdRecsList_t *pNewItem;
zclCmdRecsList_t *pLoop;
// Fill in the new profile list
pNewItem = zcl_mem_alloc( sizeof( zclCmdRecsList_t ) );
if ( pNewItem == NULL )
{
return (ZMemError);
}
pNewItem->pNext = (zclCmdRecsList_t *)NULL;
pNewItem->endpoint = endpoint;
pNewItem->numCommands = cmdListSize;
pNewItem->pCmdRecs = newCmdList;
// Find spot in list
if ( gpCmdList == NULL )
{
gpCmdList = pNewItem;
}
else
{
// Look for end of list
pLoop = gpCmdList;
while ( pLoop->pNext != NULL )
{
pLoop = pLoop->pNext;
}
// Put new item at end of list
pLoop->pNext = pNewItem;
}
return ( ZSuccess );
}
#endif // ZCL_DISCOVER
//-- MOD START
#ifdef ZCL_REPORT
/*********************************************************************
* @fn zcl_registerConfigReportRecList
*
* @brief Register an ConfigReportRec List with ZCL Foundation
*
* @param endpoint - endpoint the attribute list belongs to
* @param numConfigReportRec - number of attributes in list
* @param newConfigReportRecList - array of Attribute records.
* NOTE: THE ATTRIBUTE IDs (FOR A CLUSTER) MUST BE IN
* ASCENDING ORDER. OTHERWISE, THE DISCOVERY RESPONSE
* COMMAND WILL NOT HAVE THE RIGHT ATTRIBUTE INFO
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_registerConfigReportRecList( uint8 endpoint, uint8 numConfigReportRec, zclConfigReportRec_t newConfigReportRecList[] )
{
zclConfigReportRecsList *pNewItem;
zclConfigReportRecsList *pLoop;
// Fill in the new profile list
pNewItem = zcl_mem_alloc( sizeof( zclConfigReportRecsList ) );
if ( pNewItem == NULL )
{
return (ZMemError);
}
pNewItem->next = (zclConfigReportRecsList *)NULL;
pNewItem->endpoint = endpoint;
pNewItem->numConfigReportRec = numConfigReportRec;
pNewItem->configReportRecs = newConfigReportRecList;
// Find spot in list
if ( configReportRecsList == NULL )
{
configReportRecsList = pNewItem;
}
else
{
// Look for end of list
pLoop = configReportRecsList;
while ( pLoop->next != NULL )
{
pLoop = pLoop->next;
}
// Put new item at end of list
pLoop->next = pNewItem;
}
return ( ZSuccess );
}
#endif // ZCL_REPORT
//-- MOD END
/*********************************************************************
* @fn zcl_registerAttrList
*
* @brief Register an Attribute List with ZCL Foundation
*
* @param endpoint - endpoint the attribute list belongs to
* @param numAttr - number of attributes in list
* @param newAttrList - array of Attribute records.
* NOTE: THE ATTRIBUTE IDs (FOR A CLUSTER) MUST BE IN
* ASCENDING ORDER. OTHERWISE, THE DISCOVERY RESPONSE
* COMMAND WILL NOT HAVE THE RIGHT ATTRIBUTE INFO
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_registerAttrList( uint8 endpoint, uint8 numAttr, CONST zclAttrRec_t newAttrList[] )
{
zclAttrRecsList *pNewItem;
zclAttrRecsList *pLoop;
// Fill in the new profile list
pNewItem = zcl_mem_alloc( sizeof( zclAttrRecsList ) );
if ( pNewItem == NULL )
{
return (ZMemError);
}
pNewItem->next = (zclAttrRecsList *)NULL;
pNewItem->endpoint = endpoint;
pNewItem->pfnReadWriteCB = NULL;
pNewItem->numAttributes = numAttr;
pNewItem->attrs = newAttrList;
// Find spot in list
if ( attrList == NULL )
{
attrList = pNewItem;
}
else
{
// Look for end of list
pLoop = attrList;
while ( pLoop->next != NULL )
{
pLoop = pLoop->next;
}
// Put new item at end of list
pLoop->next = pNewItem;
}
return ( ZSuccess );
}
/*********************************************************************
* @fn zcl_registerClusterOptionList
*
* @brief Register a Cluster Option List with ZCL Foundation
*
* @param endpoint - endpoint the option list belongs to
* @param numOption - number of options in list
* @param optionList - array of cluster option records.
*
* NOTE: This API should be called to enable 'Application
* Link Key' security and/or 'APS ACK' for a specific
* Cluster. The 'Application Link Key' is discarded
* if security isn't enabled on the device.
* The default behavior is 'Network Key' when security
* is enabled and no 'APS ACK' for the ZCL messages.
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_registerClusterOptionList( uint8 endpoint, uint8 numOption, zclOptionRec_t optionList[] )
{
zclClusterOptionList *pNewItem;
zclClusterOptionList *pLoop;
// Fill in the new profile list
pNewItem = zcl_mem_alloc( sizeof( zclClusterOptionList ) );
if ( pNewItem == NULL )
{
return (ZMemError);
}
pNewItem->next = (zclClusterOptionList *)NULL;
pNewItem->endpoint = endpoint;
pNewItem->numOptions = numOption;
pNewItem->options = optionList;
// Find spot in list
if ( clusterOptionList == NULL )
{
clusterOptionList = pNewItem;
}
else
{
// Look for end of list
pLoop = clusterOptionList;
while ( pLoop->next != NULL )
{
pLoop = pLoop->next;
}
// Put new item at end of list
pLoop->next = pNewItem;
}
return ( ZSuccess );
}
/*********************************************************************
* @fn zcl_registerValidateAttrData
*
* @brief Add a validation function for attribute data
*
* @param pfnValidateAttrData - function pointer to validate routine
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_registerValidateAttrData( zclValidateAttrData_t pfnValidateAttrData )
{
zcl_ValidateAttrDataCB = pfnValidateAttrData;
return ( ZSuccess );
}
/*********************************************************************
* @fn zcl_registerReadWriteCB
*
* @brief Register the application's callback function to read/write
* attribute data, and authorize read/write operation.
*
* Note: The pfnReadWriteCB callback function is only required
* when the attribute data format is unknown to ZCL. The
* callback function gets called when the pointer 'dataPtr'
* to the attribute value is NULL in the attribute database
* registered with the ZCL.
*
* Note: The pfnAuthorizeCB callback function is only required
* when the Read/Write operation on an attribute requires
* authorization (i.e., attributes with ACCESS_CONTROL_AUTH_READ
* or ACCESS_CONTROL_AUTH_WRITE access permissions).
*
* @param endpoint - application's endpoint
* @param pfnReadWriteCB - function pointer to read/write routine
* @param pfnAuthorizeCB - function pointer to authorize read/write operation
*
* @return ZSuccess if successful. ZFailure, otherwise.
*/
ZStatus_t zcl_registerReadWriteCB( uint8 endpoint, zclReadWriteCB_t pfnReadWriteCB,
zclAuthorizeCB_t pfnAuthorizeCB )
{
zclAttrRecsList *pRec = zclFindAttrRecsList( endpoint );
if ( pRec != NULL )
{
pRec->pfnReadWriteCB = pfnReadWriteCB;
pRec->pfnAuthorizeCB = pfnAuthorizeCB;
return ( ZSuccess );
}
return ( ZFailure );
}
/*********************************************************************
* @fn zcl_DeviceOperational
*
* @brief Used to see whether or not the device can send or respond
* to application level commands.
*
* @param srcEP - source endpoint
* @param clusterID - cluster ID
* @param frameType - command type
* @param cmd - command ID
*
* @return TRUE if device is operational, FALSE otherwise
*/
static uint8 zcl_DeviceOperational( uint8 srcEP, uint16 clusterID,
uint8 frameType, uint8 cmd, uint16 profileID )
{
zclAttrRec_t attrRec;
uint8 deviceEnabled = DEVICE_ENABLED; // default value
(void)profileID; // Intentionally unreferenced parameter
// If the device is Disabled (DeviceEnabled attribute is set to Disabled), it
// cannot send or respond to application level commands, other than commands
// to read or write attributes. Note that the Identify cluster cannot be
// disabled, and remains functional regardless of this setting.
if ( zcl_ProfileCmd( frameType ) && cmd <= ZCL_CMD_WRITE_NO_RSP )
{
return ( TRUE );
}
if ( clusterID == ZCL_CLUSTER_ID_GEN_IDENTIFY )
{
return ( TRUE );
}
// Is device enabled?
if ( zclFindAttrRec( srcEP, ZCL_CLUSTER_ID_GEN_BASIC,
ATTRID_BASIC_DEVICE_ENABLED, &attrRec ) )
{
#ifdef ZCL_READ
zclReadAttrData( &deviceEnabled, &attrRec, NULL );
#endif
}
return ( deviceEnabled == DEVICE_ENABLED ? TRUE : FALSE );
}
/*********************************************************************
* @fn zcl_SendCommand
*
* @brief Used to send Profile and Cluster Specific Command messages.
*
* NOTE: The calling application is responsible for incrementing
* the Sequence Number.
*
* @param srcEp - source endpoint
* @param destAddr - destination address
* @param clusterID - cluster ID
* @param cmd - command ID
* @param specific - whether the command is Cluster Specific
* @param direction - client/server direction of the command
* @param disableDefaultRsp - disable Default Response command
* @param manuCode - manufacturer code for proprietary extensions to a profile
* @param seqNumber - identification number for the transaction
* @param cmdFormatLen - length of the command to be sent
* @param cmdFormat - command to be sent
*
* @return ZSuccess if OK
*/
ZStatus_t zcl_SendCommand( uint8 srcEP, afAddrType_t *destAddr,
uint16 clusterID, uint8 cmd, uint8 specific, uint8 direction,
uint8 disableDefaultRsp, uint16 manuCode, uint8 seqNum,
uint16 cmdFormatLen, uint8 *cmdFormat )
{
endPointDesc_t *epDesc;
zclFrameHdr_t hdr;
uint8 *msgBuf;
uint16 msgLen;
uint8 *pBuf;
uint8 options;
ZStatus_t status;
epDesc = afFindEndPointDesc( srcEP );
if ( epDesc == NULL )
{
return ( ZInvalidParameter ); // EMBEDDED RETURN
}
#if defined ( INTER_PAN )
if ( StubAPS_InterPan( destAddr->panId, destAddr->endPoint ) )
{
options = AF_TX_OPTIONS_NONE;
}
else
#endif
{
options = zclGetClusterOption( srcEP, clusterID );
// The cluster might not have been defined to use security but if this message
// is in response to another message that was using APS security this message
// will be sent with APS security
if ( !( options & AF_EN_SECURITY ) )
{
afIncomingMSGPacket_t *origPkt = zcl_getRawAFMsg();
if ( ( origPkt != NULL ) && ( origPkt->SecurityUse == TRUE ) )
{
options |= AF_EN_SECURITY;
}
}
}
zcl_memset( &hdr, 0, sizeof( zclFrameHdr_t ) );
// Not Profile wide command (like READ, WRITE)
if ( specific )
{
hdr.fc.type = ZCL_FRAME_TYPE_SPECIFIC_CMD;
}
else
{
hdr.fc.type = ZCL_FRAME_TYPE_PROFILE_CMD;
}
if ( ( epDesc->simpleDesc == NULL ) ||
( zcl_DeviceOperational( srcEP, clusterID, hdr.fc.type,
cmd, epDesc->simpleDesc->AppProfId ) == FALSE ) )
{
return ( ZFailure ); // EMBEDDED RETURN
}
// Fill in the Maufacturer Code
if ( manuCode != 0 )
{
hdr.fc.manuSpecific = 1;
hdr.manuCode = manuCode;
}
// Set the Command Direction
if ( direction )
{
hdr.fc.direction = ZCL_FRAME_SERVER_CLIENT_DIR;
}
else
{
hdr.fc.direction = ZCL_FRAME_CLIENT_SERVER_DIR;
}
// Set the Disable Default Response field
if ( disableDefaultRsp )
{
hdr.fc.disableDefaultRsp = 1;
}
else
{
hdr.fc.disableDefaultRsp = 0;
}
// Fill in the Transaction Sequence Number
hdr.transSeqNum = seqNum;
// Fill in the command
hdr.commandID = cmd;
// calculate the needed buffer size
msgLen = zclCalcHdrSize( &hdr );