Mistake on this page?
Report an issue in GitHub or email us
dm_conn.h
Go to the documentation of this file.
1 /*************************************************************************************************/
2 /*!
3  * \file
4  *
5  * \brief DM connection management module.
6  *
7  * Copyright (c) 2016-2018 Arm Ltd. All Rights Reserved.
8  *
9  * Copyright (c) 2019 Packetcraft, Inc.
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 /*************************************************************************************************/
24 #ifndef DM_CONN_H
25 #define DM_CONN_H
26 
27 #include "dm_api.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**************************************************************************************************
34  Macros
35 **************************************************************************************************/
36 
37 /* Uninitialized HCI handle */
38 #define DM_CONN_HCI_HANDLE_NONE 0xFFFF
39 
40 /* Action set initializer */
41 #define DM_CONN_ACT_SET_INIT(n) ((n) << 4)
42 
43 /* Get action set ID from action */
44 #define DM_CONN_ACT_SET_ID(action) ((action) >> 4)
45 
46 /* Get action ID from action */
47 #define DM_CONN_ACT_ID(action) ((action) & 0x0F)
48 
49 /**************************************************************************************************
50  Data Types
51 **************************************************************************************************/
52 
53 /*! DM conn event handler messages for state machine */
54 enum
55 {
56  /* messages from API */
57  DM_CONN_MSG_API_OPEN = DM_MSG_START(DM_ID_CONN), /*!< Open Connection */
58  DM_CONN_MSG_API_CLOSE, /*!< Close Connection */
59  DM_CONN_MSG_API_ACCEPT, /*!< Accept Connection */
60 
61  /* messages from HCI */
62  DM_CONN_MSG_HCI_LE_CONN_CMPL_FAIL, /*!< HCI LE Connection Complete Failure Event */
63  DM_CONN_MSG_HCI_LE_CONN_CMPL, /*!< HCI LE Connection Compelte Event */
64  DM_CONN_MSG_HCI_DISCONNECT_CMPL, /*!< HCI Disconnection Complete Event */
65  DM_CONN_MSG_HCI_LE_CONN_UPDATE_CMPL, /*!< HCI LE Connection Update Complete Event */
66  DM_CONN_MSG_HCI_LE_CREATE_CONN_CANCEL_CMD_CMPL, /*!< HCI LE Create Connection Cancel Command Complet Event */
67 };
68 
69 /* Number of messages */
70 #define DM_CONN_NUM_MSGS (DM_CONN_MSG_HCI_LE_CREATE_CONN_CANCEL_CMD_CMPL - DM_CONN_MSG_API_OPEN + 1)
71 
72 /* DM conn event handler messages, non-state machine */
73 enum
74 {
75  DM_CONN_MSG_API_FEAT = DM_MSG_START(DM_ID_CONN_2),
76  DM_CONN_MSG_API_READ_RSSI,
77  DM_CONN_MSG_API_REM_CONN_PARAM_REQ_REPLY,
78  DM_CONN_MSG_API_REM_CONN_PARAM_REQ_NEG_REPLY,
79  DM_CONN_MSG_API_SET_DATA_LEN,
80  DM_CONN_MSG_API_WRITE_AUTH_TO,
81  DM_CONN_MSG_API_REQ_PEER_SCA
82 };
83 
84 /*! DM conn event handler messages for connection update */
85 enum
86 {
87  /* messages from API */
88  DM_CONN_MSG_API_UPDATE_MASTER = DM_MSG_START(DM_ID_CONN_UPD), /*!< Master Connection Parameter Update */
89  DM_CONN_MSG_API_UPDATE_SLAVE, /*!< Slave Connecteion Parameter Update */
90 
91  /* messages from L2C */
92  DM_CONN_MSG_L2C_UPDATE_IND, /*!< L2CAP Parameter update indication */
93  DM_CONN_MSG_L2C_UPDATE_CNF, /*!< L2CAP Parameter update confirmation */
94 };
95 
96 /* Number of messages */
97 #define DM_CONN_UPD_NUM_MSGS (DM_CONN_MSG_L2C_UPDATE_CNF - DM_CONN_MSG_API_UPDATE_MASTER + 1)
98 
99 /* State machine action function sets */
100 enum
101 {
102  DM_CONN_ACT_SET_MAIN,
103  DM_CONN_ACT_SET_MASTER,
104  DM_CONN_ACT_SET_SLAVE,
105  DM_CONN_NUM_ACT_SETS
106 };
107 
108 /*! State machine actions */
109 enum
110 {
111  DM_CONN_SM_ACT_NONE = DM_CONN_ACT_SET_INIT(DM_CONN_ACT_SET_MAIN), /*!< No Action */
112  DM_CONN_SM_ACT_CLOSE, /*!< Process Connection Close */
113  DM_CONN_SM_ACT_CONN_OPENED, /*!< Procoess Connection Opened */
114  DM_CONN_SM_ACT_CONN_FAILED, /*!< Process Connection Failed */
115  DM_CONN_SM_ACT_CONN_CLOSED, /*!< Process Connection Closed */
116  DM_CONN_SM_ACT_HCI_UPDATED, /*!< Process HCI Connection Update */
117 
118  DM_CONN_SM_ACT_OPEN = DM_CONN_ACT_SET_INIT(DM_CONN_ACT_SET_MASTER), /*!< Process Master Connection Open */
119  DM_CONN_SM_ACT_CANCEL_OPEN, /*!< Process Master Cancel Connection Open */
120 
121  DM_CONN_SM_ACT_ACCEPT = DM_CONN_ACT_SET_INIT(DM_CONN_ACT_SET_SLAVE), /*!< Process Slave Connection Accept */
122  DM_CONN_SM_ACT_CANCEL_ACCEPT, /*!< Process Slave Cancel Connection Accept */
123  DM_CONN_SM_ACT_CONN_ACCEPTED, /*!< Process Slave Connection Accepted */
124  DM_CONN_SM_ACT_ACCEPT_FAILED /*!< Process Slave Connection Accept Failure */
125 };
126 
127 /*! Connection update actions */
128 enum
129 {
130  DM_CONN_UPD_ACT_NONE = DM_CONN_ACT_SET_INIT(DM_CONN_ACT_SET_MAIN), /*!< No Action */
131 
132  DM_CONN_UPD_ACT_UPDATE_MASTER = DM_CONN_ACT_SET_INIT(DM_CONN_ACT_SET_MASTER), /*!< Process Master Connection Parameter Update */
133  DM_CONN_UPD_ACT_L2C_UPDATE_IND, /*!< Process Master L2CAP Connection Parameter Update Indication */
134 
135  DM_CONN_UPD_ACT_UPDATE_SLAVE = DM_CONN_ACT_SET_INIT(DM_CONN_ACT_SET_SLAVE), /*!< Process Slave Connection Update */
136  DM_CONN_UPD_ACT_L2C_UPDATE_CNF /*!< Process Slave L2CAP Connection Parameter Update Confirmation */
137 };
138 
139 /*! State machine states */
140 enum
141 {
142  DM_CONN_SM_ST_IDLE, /*!< Idle State */
143  DM_CONN_SM_ST_CONNECTING, /*!< Connecting State */
144  DM_CONN_SM_ST_ACCEPTING, /*!< Accepting State */
145  DM_CONN_SM_ST_CONNECTED, /*!< Connected State */
146  DM_CONN_SM_ST_DISCONNECTING, /*!< Disconnecting State */
147  DM_CONN_SM_NUM_STATES
148 };
149 
150 /* Data structure for DM_CONN_MSG_API_OPEN and DM_CONN_MSG_API_ACCEPT */
151 typedef struct
152 {
153  wsfMsgHdr_t hdr;
154  uint8_t initPhys;
155  uint8_t advHandle;
156  uint8_t advType;
157  uint16_t duration;
158  uint8_t maxEaEvents;
159  bdAddr_t peerAddr;
160  uint8_t addrType;
161  uint8_t clientId;
163 
164 /*! Data structure for DM_CONN_MSG_API_CLOSE */
165 typedef struct
166 {
167  wsfMsgHdr_t hdr;
168  uint8_t reason;
169  uint8_t clientId;
171 
172 /*! Data structure for DM_CONN_MSG_API_UPDATE_MASTER and SLAVE */
173 typedef struct
174 {
175  wsfMsgHdr_t hdr;
176  hciConnSpec_t connSpec;
178 
179 /*! Data structure for DM_CONN_MSG_L2C_UPDATE_IND */
180 typedef struct
181 {
182  wsfMsgHdr_t hdr;
183  hciConnSpec_t *pConnSpec;
184  uint8_t identifier;
186 
187 /*! Data structure for DM_CONN_MSG_L2C_UPDATE_CNF */
188 typedef struct
189 {
190  wsfMsgHdr_t hdr;
191  uint16_t result;
193 
194 /*! Union of all DM Conn state machine messages */
195 typedef union
196 {
197  wsfMsgHdr_t hdr;
198  dmConnApiOpen_t apiOpen;
199  dmConnApiClose_t apiClose;
200  dmConnApiUpdate_t apiUpdate;
201  dmConnL2cUpdateInd_t l2cUpdateInd;
202  dmConnL2cUpdateCnf_t l2cUpdateCnf;
203  hciLeConnCmplEvt_t hciLeConnCmpl;
204  hciDisconnectCmplEvt_t hciDisconnectCmpl;
205  hciLeConnUpdateCmplEvt_t hciLeConnUpdateCmpl;
206 } dmConnMsg_t;
207 
208 /*! Data structure for DM_CONN_MSG_API_FEAT */
209 typedef struct
210 {
211  wsfMsgHdr_t hdr;
212  uint16_t handle;
213  uint8_t type;
214  uint8_t feature;
216 
217 /*! Data structure for DM_CONN_MSG_API_READ_RSSI */
218 typedef struct
219 {
220  wsfMsgHdr_t hdr;
222 
223 /*! Data structure for DM_CONN_MSG_API_REM_CONN_PARAM_REQ_REPLY */
224 typedef struct
225 {
226  wsfMsgHdr_t hdr;
227  hciConnSpec_t connSpec;
229 
230 /*! Data structure for DM_CONN_MSG_API_REM_CONN_PARAM_REQ_NEG_REPLY */
231 typedef struct
232 {
233  wsfMsgHdr_t hdr;
234  uint8_t reason;
236 
237 /*! Data structure for DM_CONN_MSG_API_SET_DATA_LEN */
238 typedef struct
239 {
240  wsfMsgHdr_t hdr;
241  uint16_t txOctets;
242  uint16_t txTime;
244 
245 /*! Data structure for DM_CONN_MSG_API_WRITE_AUTH_TO */
246 typedef struct
247 {
248  wsfMsgHdr_t hdr;
249  uint16_t timeout;
251 
252 /*! Data structure for DM_CONN_MSG_API_REQ_PEER_SCA */
253 typedef struct
254 {
255  wsfMsgHdr_t hdr;
257 
258 /*! Union of all DM Conn 2 messages */
259 typedef union
260 {
261  wsfMsgHdr_t hdr;
262  dmConnApiFeat_t apiFeat;
263  dmConnApiReadRssi_t apiReadRssi;
264  dmConnApiRemConnParamReqReply_t apiRemConnParamReqReply;
265  dmConnApiRemConnParamReqNegReply_t apiRemConnParamReqNegReply;
266  dmConnApiSetDataLen_t apiSetDataLen;
267  dmConnApiWriteAuthPayloadTo_t apiWriteAuthPayloadTo;
268  dmConnApiReqPeerSca_t apiReqPeerSca;
269 } dmConn2Msg_t;
270 
271 /*! Connection control block */
272 typedef struct
273 {
274  bdAddr_t peerAddr;
275  bdAddr_t localAddr;
276  uint16_t handle;
277  uint16_t idleMask;
278  dmConnId_t connId;
279  bool_t updating;
280  bool_t usingLtk;
281  uint8_t peerAddrType;
282  uint8_t localAddrType;
283  uint8_t state;
284  uint8_t inUse;
285  uint8_t secLevel;
286  uint8_t tmpSecLevel;
287  uint8_t role;
288 
289  /* enhanced fields */
290  bdAddr_t localRpa;
291  bdAddr_t peerRpa;
292 
293  uint32_t features;
294  bool_t featuresPresent;
295 } dmConnCcb_t;
296 
297 /*! Action function */
298 typedef void (*dmConnAct_t)(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
299 
300 /*! Control block of the DM conn module */
301 typedef struct
302 {
304  dmCback_t connCback[DM_CLIENT_ID_MAX];
305  hciConnSpec_t connSpec[DM_NUM_PHYS];
306  uint16_t scanInterval[DM_NUM_PHYS];
307  uint16_t scanWindow[DM_NUM_PHYS];
308 } dmConnCb_t;
309 
310 /**************************************************************************************************
311  Global Variables
312 **************************************************************************************************/
313 
314 /*! State machine action sets */
315 extern dmConnAct_t *dmConnActSet[DM_CONN_NUM_ACT_SETS];
316 
317 /*! Connection update action sets */
318 extern dmConnAct_t *dmConnUpdActSet[DM_CONN_NUM_ACT_SETS];
319 
320 /* Action set for master connection update */
321 extern const dmConnAct_t dmConnUpdActSetMaster[];
322 
323 /* Action set for slave connection update */
324 extern const dmConnAct_t dmConnUpdActSetSlave[];
325 
326 /*! Control block */
327 extern dmConnCb_t dmConnCb;
328 
329 /**************************************************************************************************
330  Function Declarations
331 **************************************************************************************************/
332 
333 /* utility functions */
334 dmConnCcb_t *dmConnCcbAlloc(uint8_t *pAddr);
335 void dmConnCcbDealloc(dmConnCcb_t *pCcb);
336 dmConnCcb_t *dmConnCcbByHandle(uint16_t handle);
337 dmConnCcb_t *dmConnCcbByBdAddr(uint8_t *pAddr);
338 dmConnCcb_t *dmConnCcbById(dmConnId_t connId);
339 uint8_t dmConnNum(void);
340 dmConnId_t dmConnOpenAccept(uint8_t clientId, uint8_t initPhys, uint8_t advHandle, uint8_t advType,
341  uint16_t duration, uint8_t maxEaEvents, uint8_t addrType, uint8_t *pAddr,
342  uint8_t role);
343 void dmConnExecCback(dmConnMsg_t *pMsg);
344 
345 /* component inteface */
346 void dmConnReset(void);
347 void dmConnMsgHandler(wsfMsgHdr_t *pMsg);
348 void dmConnHciHandler(hciEvt_t *pEvent);
349 
350 /* component 2 inteface */
351 void dmConn2MsgHandler(wsfMsgHdr_t *pMsg);
352 void dmConn2HciHandler(hciEvt_t *pEvent);
353 
354 /* connection update inteface */
355 void dmConnUpdMsgHandler(wsfMsgHdr_t *pMsg);
356 
357 /* state machine */
358 void dmConnSmExecute(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
359 
360 /* connection update action execute */
361 void dmConnUpdExecute(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
362 
363 /* main action functions */
364 void dmConnSmActNone(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
365 void dmConnSmActClose(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
366 void dmConnSmActConnOpened(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
367 void dmConnSmActConnFailed(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
368 void dmConnSmActConnClosed(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
369 void dmConnSmActHciUpdated(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
370 
371 /* connection update main action function */
372 void dmConnUpdActNone(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
373 
374 /* common master action functions */
375 void dmConnSmActCancelOpen(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
376 
377 /* connection update common master action functions */
378 void dmConnUpdActUpdateMaster(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
379 void dmConnUpdActL2cUpdateInd(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
380 
381 /* legacy master action functions */
382 void dmConnSmActOpen(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
383 
384 /* extended master action functions */
385 void dmExtConnSmActOpen(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
386 
387 /* connection update common slave action functions */
388 void dmConnUpdActUpdateSlave(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
389 void dmConnUpdActL2cUpdateCnf(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
390 
391 /* legacy slave action functions */
392 void dmConnSmActAccept(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
393 void dmConnSmActCancelAccept(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
394 void dmConnSmActConnAccepted(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
395 void dmConnSmActAcceptFailed(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
396 
397 /* extended slave action functions */
398 void dmExtConnSmActAccept(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
399 void dmExtConnSmActCancelAccept(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
400 void dmExtConnSmActConnAccepted(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
401 void dmExtConnSmActAcceptFailed(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg);
402 
403 #ifdef __cplusplus
404 };
405 #endif
406 
407 #endif /* DM_CONN_H */
uint8_t dmConnId_t
Connection identifier.
Definition: dm_api.h:588
LE connection complete event.
Definition: hci_api.h:143
dmConnAct_t * dmConnActSet[DM_CONN_NUM_ACT_SETS]
void(* dmConnAct_t)(dmConnCcb_t *pCcb, dmConnMsg_t *pMsg)
Definition: dm_conn.h:298
uint8_t bdAddr_t[6]
BD address data type.
Definition: bda.h:67
#define DM_CLIENT_ID_MAX
For internal use only.
Definition: dm_api.h:128
Connection specification type.
Definition: hci_api.h:1023
LE connection update complete event.
Definition: hci_api.h:171
void(* dmCback_t)(dmEvt_t *pDmEvt)
Callback type.
Definition: dm_api.h:868
Union of all event types.
Definition: hci_api.h:931
Device Manager subsystem API.
#define DM_CONN_MAX
Maximum number of connections.
Definition: cfg_stack.h:81
Disconnect complete event.
Definition: hci_api.h:162
dmConnAct_t * dmConnUpdActSet[DM_CONN_NUM_ACT_SETS]
Common message structure passed to event handler.
Definition: wsf_os.h:106
#define DM_NUM_PHYS
Number of scanner and initiator PHYs (LE 1M, LE 2M and LE Coded): must be set to 1 for legacy scanner...
Definition: cfg_stack.h:97
dmConnCb_t dmConnCb
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.