summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/NWK/include/private/nwkFrame.h
blob: 2a6781a5372f81d19936eb67e6cc68f6c07cd5fb (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
/**************************************************************************//**
  \file nwkFrame.h

  \brief Network frames header file.

  \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:
    2007-07-01 S. Vladykin - Created.
    2009-06-12 M. Gekk     - Refactoring.
   Last change:
    $Id: nwkFrame.h 18245 2011-08-16 09:23:15Z arazinkov $
 ******************************************************************************/
#if !defined _NWK_FRAME_H
#define _NWK_FRAME_H

/******************************************************************************
                               Includes section
 ******************************************************************************/
#include <nwkConfig.h>
#include <types.h>
#include <nwkSystem.h>
#include <appFramework.h>
#include <nwkCommon.h>

/******************************************************************************
                               Define(s) section
 ******************************************************************************/
#define NWK_HEADER(outPkt) ((NwkFrameHeader_t *)(NWK_GET_OUTPKT_DATA(outPkt)))
#define IS_DATA(nwkHeader) \
  (NWK_FRAMETYPE_DATA == (nwkHeader)->frameControl.frameType)
#define IS_COMMAND(nwkHeader) \
  (NWK_FRAMETYPE_COMMAND == (nwkHeader)->frameControl.frameType)

/******************************************************************************
                                Types section
 ******************************************************************************/
/** Values of the Frame Type Sub-Field. ZigBee spec r17, Table 3.37. */
typedef enum _NwkFrameTypeFlag_t
{
  NWK_FRAMETYPE_DATA = 0,
  NWK_FRAMETYPE_COMMAND = 1,
/**//**
 * \brief Inter-PAN delivery modes
 * ZigBee Smart Energy profile specification r15
 * B.4 Frame Format, page 90
 */
  NWK_FRAMETYPE_INTERPAN = 3
} NwkFrameTypeFlag_t;

/** Values of the Discover Route Sub-Field. ZigBee spec r17, Table 3.38. */
typedef enum _NwkDiscoverRouteFlag_t
{
  NWK_DISCOVER_ROUTE_SUPPRESS = 0,
  NWK_DISCOVER_ROUTE_ENABLE = 1
} NwkDiscoverRouteFlag_t;

/** Values of the Multicast Mode Sub-Field. ZigBee spec r17, Table 3.39. */
typedef enum _NwkMulticastMode_t
{
  NWK_FRAME_MULTICAST_NON_MEMBER_MODE = 0,
  NWK_FRAME_MULTICAST_MEMBER_MODE = 1
} NwkMulticastMode_t;

/** NWK command frame identifiers. ZigBee spec r17, Table 3.40. */
typedef enum _NwkCmdIdentifier_t
{
  NWK_CMD_ROUTE_REQUEST   = 0x01, /**< ZigBee spec r17, 3.4.1 */
  NWK_CMD_ROUTE_REPLY     = 0x02, /**< ZigBee spec r17, 3.4.2 */
  NWK_CMD_NETWORK_STATUS  = 0x03, /**< ZigBee spec r17, 3.4.3 */
  NWK_CMD_LEAVE           = 0x04, /**< ZigBee spec r17, 3.4.4 */
  NWK_CMD_ROUTE_RECORD    = 0x05, /**< ZigBee spec r17, 3.4.5 */
  NWK_CMD_REJOIN_REQUEST  = 0x06, /**< ZigBee spec r17, 3.4.6 */
  NWK_CMD_REJOIN_RESPONSE = 0x07, /**< ZigBee spec r17, 3.4.7 */
  NWK_CMD_LINK_STATUS     = 0x08, /**< ZigBee spec r17, 3.4.8 */
  NWK_CMD_NETWORK_REPORT  = 0x09, /**< ZigBee spec r17, 3.4.9 */
  NWK_CMD_NETWORK_UPDATE  = 0x0a  /**< ZigBee spec r17, 3.4.10 */
} NwkCmdIdentifier_t;

/** Type of command id field. */
typedef uint8_t NwkCommandIdField_t;

/** Frame Control Field. ZigBee spec r17, 3.3.1.1, page 308. */
/* The frame control field is 16 bits in length and contains
 * information defining the frame type, addressing and sequencing
 * fields and other control flags.*/
BEGIN_PACK
typedef struct PACK _NwkFrameControl_t
{
  LITTLE_ENDIAN_OCTET(3, (
    /** The frame type sub-field is 2 bits in length and shall be set to one of
     * the nonreserved values: NWK_FRAMETYPE_DATA or NWK_FRAMETYPE_COMMAND. */
    NwkBitField_t frameType       : 2,
    /** The protocol version sub-field shall be set to a number reflecting
     * the ZigBee NWK protocol version in use. The protocol version in use
     * on a particular device shall be made available as the value of
     * the NWK constant nwkcProtocolVersion. */
    NwkBitField_t protocolVersion : 4,
    /** The discover route sub-field may be used to control route discovery
     * operations for the transit of this frame. */
    NwkBitField_t discoverRoute   : 2
  ))
  LITTLE_ENDIAN_OCTET(6, (
    /** The multicast flag sub-field is 1 bit in length and has the value 0
     * if the frame is a unicast or broadcast frame and the value 1 if it is
     * a multicast frame. The multicast control field of the NWK header shall
     * be present only if the multicast flag has the value 1.
     * ZigBee spec r17, 3.3.1.1.4, page 309. */
    NwkBitField_t multicastFlag   : 1,
    /** The security sub-field shall have a value of 1 if, and only if,
     * the frame is to have NWK security operations enabled. If security for
     * this frame is implemented at another layer or disabled entirely,
     * it shall have a value of 0. */
    NwkBitField_t security        : 1,
    /** The source route sub-field shall have a value of 1 if and only
     * if a source route subframe is present in the NWK header.
     * ZigBee spec r17, 3.3.1.1.6, page 308. */
    NwkBitField_t sourceRoute     : 1,
    /** The destination IEEE address sub-field shall have a value of 1 if,
     * and only if, the NWK header is to include the full IEEE address
     * of the destination. */
    NwkBitField_t dstExtAddr      : 1,
    /** The source IEEE address sub-field shall have a value of 1 if, and
     * only if, the NWK header is to include the full IEEE address
     * of the source device. */
    NwkBitField_t srcExtAddr      : 1,
    NwkBitField_t reserved        : 3
  ))
} NwkFrameControl_t;
END_PACK

/** Type of unaligned extended IEEE address. */
BEGIN_PACK
typedef union PACK _NwkFrameExtAddr_t
{
  ExtAddr_t value;
  uint8_t raw[sizeof(ExtAddr_t)];
} NwkFrameExtAddr_t;
END_PACK

/** The multicast control sub-field is 1 octet in length and shall only be
 * present if the multicast flag sub-field has a value of 1. It is divided into
 * three sub-fields as illustrated in ZigBee spec r17, Figure 3.7. */
BEGIN_PACK
typedef struct PACK _NwkFrameMulticastField_t
{
  LITTLE_ENDIAN_OCTET(3, (
    /** The multicast mode sub-field indicates whether the frame is to be
     * transmitted using member or non-member mode. Member mode is used to
     * propagate multicasts between the devices that are members of
     * the destination group. Nonmember mode is used to transmit a multicast
     * frame from a device that is not a member of the multicast group to
     * a device that is a member of the multicast group. */
    NwkBitField_t mode               : 2,
    /** The nonmember radius sub-field indicates the range of a member mode
     * multicast when relayed by devices that are not members of the
     * destination group. */
    NwkBitField_t nonMemberRadius    : 3,
    /** The maximum value of the NonmemberRadius sub-field for this frame.
     * ZigBee spec r17, 3.3.1.8.3, page 312. */
    NwkBitField_t maxNonMemberRadius : 3
  ))
} NwkFrameMulticastField_t;
END_PACK

/** Source route subframe format. ZigBee spec r18, Figure 3.8, page 316. */
BEGIN_PACK
typedef struct PACK _NwkSourceRouteSubframe_t
{
  /** The relay count sub-field indicates the number of relays contained in
   * the relay list. ZigBee spec r18, 3.3.1.9.1, page 316. */
  uint8_t relayCount;
  /** The relay index sub-field indicates the index of the next relay in
   * the relay list subfield to which the packet will be transmitted.
   * ZigBee spec r18, 3.3.1.9.2, page 316. */
  uint8_t relayIndex;
  /** The relay list sub-field shall contain the list of relay addresses.
   * ZigBee spec r18, 3.3.1.9.3, page 316. */
  ShortAddr_t relayList[NWK_MAX_SOURCE_ROUTE];
} NwkSourceRouteSubframe_t;
END_PACK

/** Type of parsed NWK header. */
typedef struct _NwkParseHeader_t
{
  /** Pointer to a destination extended address in a original NWK header. */
  const NwkFrameExtAddr_t *dstExt;
  /** Pointer to a source extended address in a original NWK header. */
  const NwkFrameExtAddr_t *srcExt;
  /** Pointer to a multicast field. */
  NwkFrameMulticastField_t *multicast;
  /** Pointer to a source route subframe. */
  NwkSourceRouteSubframe_t *sourceRouteSubframe;
  /** Pointer to first octet of NWK payload. */
  uint8_t *payload;
  /** Size of NWK header in octets. */
  NwkLength_t headerSize;
  /** Size of NWK payload in octets. */
  NwkLength_t payloadSize;
  /** Source MAC short address. */
  ShortAddr_t macSrcAddr;
  /** Link quality indicator of received frame. */
  Lqi_t lqi;
  /** Received signal strength indication. */
  Rssi_t rssi;
  /** Extended address from auxilary header. It's address of previous hop. */
  ExtAddr_t auxExtAddr;
} NwkParseHeader_t;

/** Type of a NWK header, which comprises frame control, addressing
 * and sequencing information. */
BEGIN_PACK
typedef struct PACK _NwkFrameHeader_t
{
  /** The frame control field shall be formatted as illustrated in
   * ZigBee spec r17, Figure 3.6. */
  NwkFrameControl_t frameControl;
  /** The destination address field shall always be present and shall be 2
   * octets in length. If the multicast flag sub-field of the frame control
   * field has the value 0, the destination address field shall hold the 16-bit
   * network address of the destination device or a broadcast address
   * (see Table 3.54). If the multicast flag sub-field has the value 1,
   * the destination address field shall hold the 16-bit Group ID of the
   * destination multicast group. ZigBee spec r17, 3.3.1.2, page 310. */
  ShortAddr_t dstAddr;
  /** The source address field shall always be present. It shall always be 2
   * octets in length and shall hold the network address of the source device of
   * the frame. ZigBee spec r17, 3.3.1.3, page 310. */
  ShortAddr_t srcAddr;
  /** The radius field shall always be present. It will be 1 octet in length and
   * specifies the range of a radius-limited transmission. The field shall be
   * decremented by 1 by each receiving device. */
  NwkRadius_t radius;
  /** The sequence number field is present in every frame and is 1 octet
   * in length. The sequence number value shall be incremented by 1 with each
   * new frame transmitted */
  NwkSequenceNumber_t sequenceNumber;
  union PACK
  {
    struct PACK
    {
      /** The destination IEEE address field, if present, contains the 64-bit
       * IEEE address corresponding to the 16-bit network address contained in
       * the destination address field of the NWK header.
       * ZigBee spec r17, 3.3.1.6, page 310. */
      NwkFrameExtAddr_t dstExt;
      /** The source IEEE address field, if present, contains the 64-bit
       * IEEE address corresponding to the 16-bit network address contained
       * in the source address field. */
      NwkFrameExtAddr_t srcExt;
      uint8_t payload[1];
    } dst_src;
    struct PACK
    {
      NwkFrameExtAddr_t srcExt;
      uint8_t payload[1];
    } src;
    struct PACK
    {
      /** The multicast control sub-field is 1 octet in length and shall only
       * be present if the multicast flag sub-field has a value of 1. It is
       * divided into three sub-fields as illustrated in ZigBee spec r17,
       * Figure 3.7. */
      NwkFrameMulticastField_t control;
      uint8_t payload[1];
    } multicast;
    struct PACK
    {
      NwkFrameExtAddr_t dstExt;
      NwkFrameExtAddr_t srcExt;
      NwkFrameMulticastField_t control;
      uint8_t payload[1];
    } dst_src_multicast;
    /** First octet of NWK payload. */
    uint8_t payload[1];
  } field;
} NwkFrameHeader_t;
END_PACK

/** The beacon payload shall contain the information shown in ZigBee spec r17,
 * Table 3.56. This enables the NWK layer to provide additional information
 * to new devices that are performing network discovery and allows these new
 * devices to more efficiently select a network and a particular neighbor
 * to join. */
BEGIN_PACK
typedef struct PACK _NwkBeaconPayload_t
{
  /** This field identifies the network layer protocols in use and,
   * for purposes of this specification, shall always be set to 0,
   * indicating the ZigBee protocols. */
  uint8_t protocolId;
  struct PACK
  {
    LITTLE_ENDIAN_OCTET(2, (
      /** A ZigBee stack profile identifier. */
      NwkBitField_t stackProfile       :4,
      /** The version of the ZigBee protocol. */
      NwkBitField_t nwkProtocolVersion :4
    ))
    LITTLE_ENDIAN_OCTET(4, (
      NwkBitField_t reserved           :2,
      /** This value is set to 1 if this device is capable of accepting join
       * requests from router capable devices and is set to 0 otherwise. */
      NwkBitField_t routerCapacity     :1,
      /** The network depth of this device. A value of 0x00 indicates that
       * this device is the ZigBee coordinator for the network. */
      NwkBitField_t deviceDepth        :4,
      /** This value is set to 1 if the device is capable of accepting join
       * requests from end devices seeking to join the network and is set
       * to 0 otherwise. */
      NwkBitField_t endDeviceCapacity  :1
    ))
  } field;
  /** The globally unique ID for the PAN of which the beaconing
   * device is a member. */
  ExtPanId_t nwkExtendedPanid;
  /** This value indicates the difference in time, measured in symbols,
   * between the beacon transmission time of the device and the beacon
   * transmission time of its parent. */
  uint8_t txOffset[3];
  /** This field reflects the value of nwkUpdateId from the NIB. */
  NwkUpdateId_t updateId;
} NwkBeaconPayload_t;
END_PACK

/******************************************************************************
                             Prototypes section
 ******************************************************************************/
/******************************************************************************
  \brief Clear frame, then fill it with commonly used fields.

  \param[in] nwkHeader - pointer to header.
  \return None.
 ******************************************************************************/
NWK_PRIVATE void nwkPrepareCommonFrame(NwkFrameHeader_t *const nwkHeader);

/******************************************************************************
  \brief Parse floating fields of frame.

  \param[in] nwkHeader - header to parse.
  \param[in] parse - structure with pointer to frame fields,
                     NULL - if field is not present, correct pointer otherwise.
  \return None.
 ******************************************************************************/
NWK_PRIVATE void nwkParseHeader(NwkFrameHeader_t *const nwkHeader,
  NwkParseHeader_t *const parse);

/**************************************************************************//**
  \brief Gets maximum default radius. It's 2*maxDepth or 255.

  \return 2 * maxDepth if maxDepth less 128 otherwise 255.
 ******************************************************************************/
NWK_PRIVATE NwkRadius_t nwkGetDefaultRadius(void);

#endif /* _NWK_FRAME_H */
/** eof nwkFrame.h */