summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/ZCL/include/zclParser.h
blob: 141d5b1f5bc8b93362b56270eb1e7c4133b9b4fa (plain)
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
/************************************************************************//**
  \file zclParser.h

  \brief
    The header file describes the ZCL Parser interface

    The file describes the interface and types of ZCL Parser

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
    02.12.08 A. Potashov - Created.
******************************************************************************/

#ifndef _ZCLPARSER_H
#define _ZCLPARSER_H

/******************************************************************************
                   Includes section
******************************************************************************/
#include <types.h>
//#include <zclDbg.h>
#include <zcl.h>
#include <zclMemoryManager.h>
#include <queue.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
/*ZCL Header Frame Type sub-field value*/
#define ZCL_FRAME_CONTROL_FRAME_TYPE_UNIVERSAL_COMMAND            0x00
#define ZCL_FRAME_CONTROL_FRAME_TYPE_SPECIFIC_COMMAND             0x01

/*ZCL Header Manufacturer Specific sub-field value*/
#define ZCL_FRAME_CONTROL_MANUFACTURER_NONSPECIFIC                0x00
#define ZCL_FRAME_CONTROL_MANUFACTURER_SPECIFIC                   0x01

#define ZCL_FRAME_SIMPLE_HEADER_LENGTH                            3
#define ZCL_FRAME_HEADER_MANUFACTURER_CODE_LENGTH                 2
#define ZCL_FRAME_HEADER_WITH_MANUFACTURER_CODE_LENGTH            \
        (ZCL_FRAME_SIMPLE_HEADER_LENGTH + ZCL_FRAME_HEADER_MANUFACTURER_CODE_LENGTH)

#ifndef ZCL_STANDARD_REQ_TYPE
#define ZCL_STANDARD_REQ_TYPE                                     ZCL_FRAME_CONTROL_FRAME_TYPE_UNIVERSAL_COMMAND
#endif
#ifndef ZCL_SPECIAL_REQ_TYPE
#define ZCL_SPECIAL_REQ_TYPE                                      ZCL_FRAME_CONTROL_FRAME_TYPE_SPECIFIC_COMMAND
#endif

#ifndef ZCL_MAX_STANDARD_DATA_TYPE_SIZE
#define ZCL_MAX_STANDARD_DATA_TYPE_SIZE                           8
#endif

/*************************************************************************//**
 \brief Defines the Descrete Kind of ZCL Data Type (used in #ZCL_DataTypeDescriptor_t)
*****************************************************************************/
#ifndef ZCL_DATA_TYPE_DESCRETE_KIND
#define ZCL_DATA_TYPE_DESCRETE_KIND                               0x00
#endif

/*************************************************************************//**
 \brief Defines the Analog Kind of ZCL Data Type (used in #ZCL_DataTypeDescriptor_t)
*****************************************************************************/
#ifndef ZCL_DATA_TYPE_ANALOG_KIND
#define ZCL_DATA_TYPE_ANALOG_KIND                                 0x01
#endif

/******************************************************************************
                   Types section
******************************************************************************/

typedef enum
{
  ZCL_PARSER_IDLE_STATE,
  ZCL_PARSER_RECEIVED_PACKET_STATE,
  ZCL_PARSER_SENDING_RESPONSE_STATE,
} ZclParserState_t;

typedef struct
{
  //ZclParserState_t state;
  uint8_t dataIndAmount;
  QueueDescriptor_t dataIndQueue;
} ZclParserMem_t;

BEGIN_PACK
typedef struct PACK
{
  union PACK
  {
    struct PACK
    {
      LITTLE_ENDIAN_OCTET(5,(
        uint8_t frameType             :2, //Frame type sub-field
        uint8_t manufacturerSpecific  :1, //Manufacturer specific sub-field
        uint8_t direction             :1, //Direction sub-field
        uint8_t defaultResponse       :1, //Disable default response sub-field
        uint8_t reserved              :3  //Reserved bits. Must have a zero value
      ))
    };
    uint8_t uint8;
  };
} ZclFrameControlField_t;

typedef struct PACK
{
  ZclFrameControlField_t  frameControl;
  uint16_t                manufacturerCode;
  uint8_t                 sequenceNumber;
  uint8_t                 commandId;
} ZclFrameHeader_t;

typedef struct PACK
{
  ZclFrameHeader_t  header;
  uint8_t           payload[1];
} ZclFrame_t;

/******************************************************************************
 Read Attributes commands
******************************************************************************/
/** \brief ZCL read attributes command record */
typedef struct PACK
{
  uint16_t attributeId;
} ZclReadAttributesRecord_t;

/** \brief ZCL Read attributes command payload */
typedef struct PACK
{
  ZclReadAttributesRecord_t records[1];
} ZclReadAttributesCommand_t;

typedef struct PACK
{
  uint8_t   elementType;
  uint16_t  elementsAmount;
  struct PACK
  {
    uint8_t   value[1];
  } elements[1];
} ZclReadAttributesResponseArraySetBagValue_t;

typedef struct PACK
{
  uint16_t  elementsAmount;
  struct PACK
  {
    uint8_t   elementType;
    uint8_t   value[1];
  } elements [1];
} ZclReadAttributesResponseStructValue_t;

/** \brief ZCL read attributes response command record */
typedef struct PACK
{
  ZCL_AttributeId_t attributeId;
  uint8_t           status;
  uint8_t           type;
  union PACK
  {
    uint8_t         simpleValue[1];
    ZclReadAttributesResponseArraySetBagValue_t arraySetBagValue;
    ZclReadAttributesResponseStructValue_t      structValue;
  };
} ZclReadAttributesResponseRecord_t;

/** \brief ZCL read attributes response command payload */
typedef struct PACK
{
  ZclReadAttributesResponseRecord_t records[1];
} ZclReadAttributesResponseCommand_t;

/******************************************************************************
 Write Attributes commands
******************************************************************************/
/** \brief ZCL write attributes command record */
typedef struct PACK
{
  ZCL_AttributeId_t attributeId;
  uint8_t  type;
  uint8_t  value[1];
} ZclWriteAttributesRecord_t;

/** \brief ZCL Wrire attributes command payload */
typedef struct PACK
{
  ZclWriteAttributesRecord_t  records[1];
} ZclWriteAttributesCommand_t;

/** \brief ZCL Wrire attributes undivided command payload */
typedef ZclWriteAttributesCommand_t ZclWriteAttributesUndividedCommand_t;

/** \brief ZCL Wrire attributes no response command payload */
typedef ZclWriteAttributesCommand_t ZclWriteAttributesNoResponseCommand_t;

/** \brief ZCL write attributes response command record */
typedef struct PACK
{
  uint8_t  status;
  uint16_t attributeId;
} ZclWriteAttributesResponseRecord_t;

/** \brief ZCL Wrire attributes response command payload */
typedef struct PACK
{
  ZclWriteAttributesResponseRecord_t  records[1];
} ZclWriteAttributesResponseCommand_t;

/******************************************************************************
 Configure reporting command
******************************************************************************/
/** \brief ZCL configure reporting command record */
typedef struct PACK
{
  uint8_t   direction;
  uint16_t  attributeId;
  uint16_t  minimumReportingInterval;
  uint16_t  maximumReportingInterval;
  uint8_t   reportableChange[1];
  uint16_t  timeoutPeriod;
} ZclConfigureReportingRecord_t;

/** \brief ZCL configure reporting command payload */
typedef struct PACK
{
  ZclConfigureReportingRecord_t  records[1];
} ZclConfigureReportingCommand_t;

/** \brief ZCL configure reporting response command record */
typedef struct PACK
{
  uint8_t   status;
  uint8_t   direction;
  uint16_t  attributeId;
} ZclConfigureReportingResponseRecord_t;

/** \brief ZCL configure reporting response command payload */
typedef struct PACK
{
  ZclConfigureReportingResponseRecord_t  records[1];
} ZclConfigureReportingResponseCommand_t;

END_PACK

typedef struct
{
  ZCL_GeneralCommandId_t  commandId;
  void                    *primitive;
} ZclBuffer_t;

typedef enum
{
  ZCL_PARSER_SUCCESS_STATUS               = 0,
  ZCL_PARSER_UNKNOWN_COMMAND_STATUS       = 1,
  ZCL_PARSER_BAD_FORMAT_STATUS            = 2,
  ZCL_PARSER_INVALID_PARAMETER_STATUS     = 3
} ZclParserStatus_t;

typedef enum
{
  ZCL_REQUEST_TYPE,
  ZCL_RESPONSE_TYPE,
  ZCL_EVENT_TYPE,
  ZCL_UNSPECIFIED_COMMAND_TYPE
} ZCL_CommandType_t;

typedef struct
{
  uint8_t   headerLength;
  uint8_t   payloadLength;
  uint8_t   frameType;
  uint8_t   direction;
  uint8_t   defaultResponse;
  uint8_t   manufacturerSpecific;
  uint16_t  manufacturerCode;
  uint8_t   sequenceNumber;
  uint8_t   commandId;
  uint8_t   *header;
  uint8_t   *payload;
} ZclFrameDescriptor_t;

typedef struct
{
  ZclFrameDescriptor_t *frameDescriptor;
  APS_DataInd_t        *ind;
  ZCL_Status_t          commandStatus;
  bool                  noApsSecurity;
  bool                  relevantResponse;
} ZclAuxParseData_t;

/******************************************************************************
                   Constants section
******************************************************************************/

/******************************************************************************
                   External variables section
******************************************************************************/
/**************************************************************************//**
\brief ZCL memory global object
******************************************************************************/
extern ZclParserMem_t zclParserMem;

/******************************************************************************
                   Prototypes section
******************************************************************************/
/*************************************************************************//**
  \brief Reset parser queue and clear memory.
*****************************************************************************/
void zclParserInit(void);

/**************************************************************************//**
\brief Form zcl header.

\param[in] - dataReq - pointer to data request;
\param[in] - reqType - type of the request;
\param[in] - direction - cluster\server side;
\param[in] - commandId - zcl command identification;
\param[in] - defaultResponse - presence or absence of the default responde;
\param[in] - manufacSpecCode - manufacturer specific code;
\return header length
******************************************************************************/
uint8_t zclFormRequest(APS_DataReq_t *dataReq, uint8_t reqType, uint8_t direction, uint8_t commandId, uint8_t defaultResponse, uint16_t manufacSpecCode);

/*************************************************************************//**
  \brief Response has been received.

  \param[in] auxData - pointer to structure with command payload and
  descriptor of received command frame.
*****************************************************************************/
void zclResponseInd(ZclAuxParseData_t *auxData);

/*************************************************************************//**
\brief Routine of Read Reporting Configuration Command.

\param[in] apsDataInd - pointer to aps payload
\param[in] frameDescriptor - pointer to parsed data structure from payload
*****************************************************************************/
void zclReadReportingConfigurationHandler(APS_DataInd_t *apsDataInd, ZclFrameDescriptor_t *frameDescriptor);

/*************************************************************************//**
\brief Routine of Configure Reporting Command.

\param[in] apsDataInd - pointer to aps payload
\param[in] frameDescriptor - pointer to parsed data structure from payload
*****************************************************************************/
void zclConfigureReportingHandler(APS_DataInd_t *apsDataInd, ZclFrameDescriptor_t *frameDescriptor);

/*************************************************************************//**
  \brief Report has been received.

  \param[in] apsData - report payload
  \param[in] frameDesc - descriptor of received report frame
*****************************************************************************/
void zclReportInd(APS_DataInd_t *apsData, ZclFrameDescriptor_t *frameDescriptor);

/*************************************************************************//**
  \brief Special cluster command has been received.

  \param[in] auxData - pointer to structure with command payload and
  descriptor of received command frame.
*****************************************************************************/
void zclCommandInd(ZclAuxParseData_t *auxData);

/*************************************************************************//**
  \brief The attribute has been read or written.

  \param[in] apsDataInd - attribute payload
  \param[in] direction - direction
  \param[in] event - event type
  \param[in] attributeId - attribute identifier
*****************************************************************************/
void zclAttributeEventInd(APS_DataInd_t *apsDataInd, uint8_t direction, ZCL_AttributeEvent_t event, ZCL_AttributeId_t attributeId);

/*************************************************************************//**
  \brief Configuration has been received.

  \param[in] apsData - configuration payload
  \param[in] frameDesc - descriptor of received report frame
*****************************************************************************/
void zclConfigurationInd(APS_DataInd_t *apsData, ZclFrameDescriptor_t *frameDesc);

/*************************************************************************//**
  \brief Indication from CAPS.

  \param[in] ind - buffer for data indication.
*****************************************************************************/
void zclDataInd(APS_DataInd_t *ind);

/*************************************************************************//**
  \brief     Security Type get by Cluster Id function and by endpointId, on which
             was registered cluster.
             Returns Security Type should be used with ClusterId specified
  \param[in] clusterId - cluster identifier
  \param[in] endpointId - end point identifier
  \return    ZCL_NETWORK_KEY_CLUSTER_SECURITY - NWK Key Security should be used
             ZCL_APPLICATION_LINK_KEY_CLUSTER_SECURITY - APS Link Key Security
             shoud be used.
             If cluster is unknown, security key will be determined by security mode,
             i.e. ZCL_NETWORK_KEY_CLUSTER_SECURITY for Standard Security,
                  ZCL_APPLICATION_LINK_KEY_CLUSTER_SECURITY for High Security.
*****************************************************************************/
uint8_t zclGetSecurityTypeByClusterId(ClusterId_t clusterId, Endpoint_t endpointId);

/**************************************************************************//**
  \brief Get next registered ZCL endpoint descriptor by previous.

  \param[in] prev - previous ZCL endpoint descriptor pointer.It is must be non NULL
                    sequential access required if. And it is must be NULL the
                    first descriptor as registered endpoints queue head element
                    access required if.

  \return Valid ZCL endpoint descriptor pointer descriptor found if,
           NULL - other case.
 ******************************************************************************/
ZCL_DeviceEndpoint_t* zclNextEndpoint(ZCL_DeviceEndpoint_t *prev);

/*************************************************************************//**
  \brief Find endpoint descriptor by endpoint id.

  \param[in] endpointId - endpoint unique identifier.
  \return endpoint descriptor if found, NULL otherwise.
*****************************************************************************/
ZCL_DeviceEndpoint_t *zclGetEndpoint(Endpoint_t endpointId);

/******************************************************************************
                   Inline static functions section
******************************************************************************/
/******************************************************************************
\brief Function returns point to ZCL memory object
******************************************************************************/
static inline ZclParserMem_t *zclParserMemReq(void)
{
  return ((ZclParserMem_t *)&zclParserMem);
}

#endif  //#ifndef _ZCLPARSER_H

//eof zclParser.h