summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/APS/include/private/apsSkke.h
blob: 0758c7e4500d4a9d9ff51fc3e6f7c3b950d4708b (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
/**************************************************************************//**
  \file  apsSkke.h

  \brief Private interface of symmetric-key key establishment implementation.

  \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:
    2011-04-23 Max Gekk - Created.
   Last change:
    $Id: apsSkke.h 17813 2011-07-04 14:11:12Z dloskutnikov $
 ******************************************************************************/
#if !defined _APS_SKKE_H
#define _APS_SKKE_H

/******************************************************************************
                               Includes section
 ******************************************************************************/
#include <apsCommand.h>
#if defined _SECURITY_ && defined _LINK_SECURITY_ && defined _HI_SECURITY_ 
#include <apsmeEstablishKey.h>

/******************************************************************************
                              Define(s) section
 ******************************************************************************/
/** Size of the data field in SKKE command frames. */
#define APS_SKKE_DATA_SIZE 16U
/* Maximum value of SKKE TTL. */
#define APS_SKKE_INFINITY_TTL UINT32_MAX

/******************************************************************************
                                Types section
 ******************************************************************************/
/* Type of internal state of SKKE responder or initiator. */
typedef enum _ApsSkkeState_t
{
  APS_SKKE_REQ_STATE,
  APS_WAIT_SKKE_2_STATE,
  APS_WAIT_SKKE_4_STATE,
  APS_SKKE_RESP_STATE,
  APS_WAIT_SKKE_3_STATE
} ApsSkkeState_t;

BEGIN_PACK
/** Type of generic SKKE command frame. See ZigBee spec r19, page 478. */
typedef struct PACK _ApsSkkeFrame_t
{
  ApduCommandHeader_t header;
  /** The 64-bit extended address of the device that acts as the initiator
   *  in the key-establishment protocol. See ZigBee spec r19, 4.4.9.1.2 */
  uint8_t initiator[sizeof(ExtAddr_t)];
  /** The 64-bit extended address of the device that acts as the responder
   *  in the key-establishment protocol. See ZigBee spec r19, 4.4.9.1.3 */
  uint8_t responder[sizeof(ExtAddr_t)];
  /** The content of the data field depends on the command identifier field:
   * QEU for SKKE-1, QEV for SKKE-2, MacTag2 for SKKE-3, MacTag1 for SKKE-4. */
  uint8_t data[APS_SKKE_DATA_SIZE];
} ApsSkkeFrame_t;
END_PACK

/******************************************************************************
                              Prototypes section
 ******************************************************************************/
/**************************************************************************//**
  \brief Process an incoming SKKE command frame.

  \param[in] commandInd - pointer to parameters of the received command:
                            - secured or not,
                            - extended address of source node,
                            - pointer to NLDE-DATA.indication's parameters.

  \return 'true' - free NWK buffer for a SKKE command otherwise return 'false'.
 ******************************************************************************/
APS_PRIVATE bool apsSkkeInd(const ApsCommandInd_t *const commandInd);

/**************************************************************************//**
  \brief Initializes the common substructure of SKKE requests or response.

  \param[in, out] skkeElem - pointer to the common element of SKKE request
                        or SKKE response. \sa ApsSkkeElem_t
  \param[in] state - initial state of the SKKE request.
  \param[in] ttl - time to live of the SKKE request.
  \param[in] apsSkkeConf - confirmation function of the SKKE request or response.

  \return None.
 ******************************************************************************/
APS_PRIVATE void apsInitSkkeElem(ApsSkkeElem_t *const skkeElem,
  const ApsSkkeState_t state, const uint32_t ttl,
  void (* apsSkkeConf)(struct _ApsSkkeElem_t*, APS_SkkeStatus_t));

/**************************************************************************//**
  \brief Find a first SKKE element by state and remove it from internal queue.

  \param[in] state - state of the SKKE request or response.

  \return Pointer to SKKE element or NULL if element isn't found.
 ******************************************************************************/
APS_PRIVATE ApsSkkeElem_t* apsFetchSkkeElem(const ApsSkkeState_t state);

/**************************************************************************//**
  \brief Is there an SKKE element with the given state?

  \param[in] state - state of the SKKE request or response.

  \return 'true' if any element is found otherwise return 'false'.
 ******************************************************************************/
APS_PRIVATE bool apsIsThereSkkeElem(const ApsSkkeState_t state);

/**************************************************************************//**
  \brief Set new TTL of the SKKE element.

  \param[in, out] skkeElem - pointer to the common substructure of SKKE request
                        or SKKE response. \sa ApsSkkeElem_t
  \param[in] ttl - time to live of the SKKE request or response.
  \return None.
 ******************************************************************************/
APS_PRIVATE void apsSetSkkeTTL(ApsSkkeElem_t *const skkeElem, const uint32_t ttl);

/**************************************************************************//**
  \brief Set new state of the SKKE element.

  \param[in, out] skkeElem - pointer to the common substructure of SKKE request
                        or SKKE response. \sa ApsSkkeElem_t
  \param[in] state - new state of the SKKE request or response.
  \return None.
 ******************************************************************************/
APS_PRIVATE void apsSetSkkeState(ApsSkkeElem_t *const skkeElem,
  const ApsSkkeState_t state);

/**************************************************************************//**
  \brief Allocate memory for SKKE frame and NWK_DataReq_t structure in NWK.

  \param[in] allocNwkDataReq - pointer to NWK DataReq allocation's parameters.
  \param[in] nwkAllocDataConf - callback function. NWK-layer calls this function
                                when memory has been allocated.
  \return None.
 ******************************************************************************/
APS_PRIVATE void apsSkkeAllocFrameReq(NWK_AllocDataReq_t *const allocNwkDataReq,
  void (*nwkAllocDataConf)(NWK_AllocDataConf_t*));

/**************************************************************************//**
  \brief Prepare and fill fields of APS SKKE command frame.

  \param[in] allocConf - pointer to confirmation structure with pointer to
                         allocated NWK_DataReq_t structure.
  \param[in] commandId - identifier of APS security command frame.
  \param[in] nwkDataReq - pointer to field in the SKKE request (reponse)
                          for saving pointer to allocated NWK_DataReq_t structure.
  \param[in] data - pointer to value for the data field of APS SKKE frame.
  \param[in] initiatorAddress - pointer to extended address of SKKE initiator.
  \param[in] responderAddress - pointer to extended address of SKKE responser.

  \return None.
 ******************************************************************************/
APS_PRIVATE void apsPrepareSkkeCommand(NWK_AllocDataConf_t *const allocConf,
  const ApsCommandId_t commandId, NWK_DataReq_t **const skkeNwkDataReq,
  const uint8_t *const data, const ExtAddr_t *const initiatorAddress,
  const ExtAddr_t *const responderAddress);

/**************************************************************************//**
  \brief Prepare NWK_DataReq_t structure and send SKKE command frame.

  \param[in] nwkDataReq - pointer to parameters of NLDE-DATA.request.
  \param[in] nwkDataConf - NLDE-DATA confirm callback function's pointer.
  \param[in] dstShortAddr - short address of partner (initiator or responder).
  \param[in] secure - enable security on NWK layer.

  \return None.
 ******************************************************************************/
APS_PRIVATE void apsSkkeNwkDataReq(NWK_DataReq_t *const nwkDataReq,
  void (* nwkDataConf)(NWK_DataConf_t*), const ShortAddr_t dstShortAddr,
  const bool secure);

/**************************************************************************//**
  \brief Process confirmation from NWK layer of transmission SKKE command.

  \param[in] nwkDataConf - pointer to NLDE-DATA confirmation parameters.
  \param[in] state - state of SKKE request or response for which SKKE command
                     was transmitted.
  \param[in] apsSkkeCompareNwkReq - matching function of SKKE request
                          or response with NWK DataReq_t structure.
  \param[in] txErrorStatus - return this SKKE status if transmission of SKKE
                          command is failure.
  \return None.
 ******************************************************************************/
APS_PRIVATE void apsSkkeNwkDataConf(NWK_DataConf_t *const nwkDataConf,
  const ApsSkkeState_t state,
  bool (*apsSkkeCompareNwkReq)(const ApsSkkeElem_t*, NWK_DataReq_t*),
  const APS_SkkeStatus_t txErrorStatus);

/**************************************************************************//**
  \brief Process indication from NWK layer about SKKE command.

  \param[in] nwkDataInd - pointer to NLDE-DATA indication parameters.
  \param[in] state - state of SKKE request or response for which SKKE command
                     is expected.
  \param[in] apsSkkeCompareNwkInd - matching function of SKKE request
                          or response with NWK DataInd_t structure.
  \param[in, out] skkeFrame - pointer to pointer to SKKE raw frame.
  \return Pointer to SKKE element or NULL if it insn't found.
 ******************************************************************************/
APS_PRIVATE ApsSkkeElem_t* apsFindSkkeElemByNwkInd(const NWK_DataInd_t *const nwkDataInd,
  const ApsSkkeState_t state,
  bool (*apsSkkeCompareNwkInd)(const ApsSkkeElem_t*, const ApsSkkeFrame_t*),
  ApsSkkeFrame_t **skkeFrame);

/**************************************************************************//**
  \brief Reset the internal queue and variables of SKKE component.
 ******************************************************************************/
APS_PRIVATE void apsSkkeReset(void);

#else  /* not _HI_SECURITY_ */

#define apsSkkeInd NULL
#define apsSkkeReset() ((void)0)
#endif /* _HI_SECURITY_ */

#endif /* _APS_SKKE_H */
/** eof apsSkke.h */