1  /*
2   * Copyright (C) 2021 Huawei Device Co., Ltd.
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  #ifndef GAP_DEF_H
16  #define GAP_DEF_H
17  
18  #include "gap.h"
19  #include "gap_le.h"
20  
21  #include "alarm.h"
22  #include "list.h"
23  
24  #ifdef __cplusplus
25  extern "C" {
26  #endif
27  
28  #define GAP_BREDR_SUPPORT
29  #define GAP_LE_SUPPORT
30  
31  #define BT_ADDR_FMT "%02X:%02X:**:**:**:%02X"
32  #define BT_ADDR_FMT_OUTPUT(addr) (addr)[5], (addr)[4], (addr)[0]
33  
34  #define BT_ADDR_NULL \
35      {                \
36          {0}, 0       \
37      }
38  
39  #define BT_COD_SIZE 3
40  #define COD_UINT_TO_ARRAY(cod)                                      \
41      {                                                               \
42          ((cod)&0xff), (((cod) >> 8) & 0xff), (((cod) >> 16) & 0xff) \
43      }
44  #define COD_ARRAY_TO_UINT(cod) ((uint32_t)(cod)[0] | (uint32_t)((cod)[1] << 8) | (uint32_t)((cod)[2] << 16))
45  
46  #define LAP_GENERAL_INQUIRY_ACCESS 0x9E8B33
47  #define LAP_LIMITED_INQUIRY_ACCESS 0x9E8B00
48  
49  #define NO_SCANS_ENABLED 0
50  #define INQUIRY_SCAN_ENABLED_PAGE_SCAN_DISABLED 1
51  #define INQUIRY_SCAN_DISABLED_PAGE_SCAN_ENABLED 2
52  #define INQUIRY_SCAN_ENABLED_PAGE_SCAN_ENABLED 3
53  
54  #define CURRENT_IAC_MAX_NUM 0x02
55  #define EXTENDED_SCAN_PHY_MAX_NUM 3
56  
57  #define COD_LIMITED_DISCOVERABLE_BIT (1 << 13)
58  
59  #define GAP_EIR_FEC_REQUIRED 0x00
60  
61  #define GAP_INVALID_ACL_HANDLE 0xFFFF
62  
63  #define GAP_HCI_DISC_REASON_AUTH_FAIL 0x05
64  
65  #define GAP_DISC_ACL_WAIT_TIME 4000
66  #define GAP_USE_ACL_CONNECTION_TIME 30000
67  #define GAP_SEC_WAIT_ENCRYPT_TIME 2000
68  #define GAP_WAIT_EX_ADV_TERMINATED 1500
69  
70  #define GAP_LINK_KEY_TYPE_UNKNOWN 0xFF
71  #define AUTHENTICATION_UNKNOWN_MITM 0xFF
72  
73  #define AUTH_REQ_DEFAULT (SMP_AUTH_REQ_BONDING | SMP_AUTH_REQ_BIT_MITM | SMP_AUTH_REQ_BIT_SC)
74  
75  #define LE_KEY_TYPE_UNAUTHENTICATION SMP_AUTH_FLAG_NO
76  #define LE_KEY_TYPE_AUTHENTICATION SMP_AUTH_FLAG_YES
77  #define LE_KEY_TYPE_UNKNOWN 0xFF
78  
79  #ifdef GAP_BREDR_SUPPORT
80  
81  #define INQUIRY_RESULT_SAVE_MAX 40
82  
83  #define UNSUPPORT_SECURE_CONNECTIONS 0x00
84  #define SUPPORT_SECURE_CONNECTIONS 0x01
85  
86  #define GAP_PAIR_RETRY_COUNT 5
87  #define GAP_PAIR_RETRY_WAIT_TIME 500
88  
89  enum ScanModeStatus {
90      GAP_SCANMODE_STATUS_IDLE,
91      GAP_SCANMODE_STATUS_CLOSING,
92      GAP_SCANMODE_STATUS_SETTING,
93  };
94  
95  enum InquiryStatus {
96      GAP_INQUIRY_STATUS_IDLE,
97      GAP_INQUIRY_STATUS_START,
98      GAP_INQUIRY_STATUS_CANCEL,
99  };
100  
101  enum RemoteNameStatus {
102      GAP_REMOTE_NAME_STATUS_IDLE,
103      GAP_REMOTE_NAME_STATUS_START,
104      GAP_REMOTE_NAME_STATUS_CANCEL,
105  };
106  
107  enum DeviceSecurityStatus {
108      GAP_DEV_SEC_STATUS_IDLE,
109      GAP_DEV_SEC_STATUS_ACTION,
110      GAP_DEV_SEC_STATUS_WAIT_DISC,
111  };
112  
113  enum DeviceAuthenticationStatus {
114      GAP_AUTH_STATUS_IDLE,
115      GAP_AUTH_STATUS_ACTION,
116      GAP_AUTH_STATUS_WAIT_RETRY,
117      GAP_AUTH_STATUS_RETRY,
118  };
119  
120  enum DeviceEncryptionStatus {
121      GAP_ENC_STATUS_IDLE,
122      GAP_ENC_STATUS_ACTION,
123  };
124  
125  enum DeviceSecurityEvent {
126      GAP_SEC_EVENT_CONNECT_COMP,
127      GAP_SEC_EVENT_FEATURE_COMP,
128      GAP_SEC_EVENT_UNAUTH_SUCCESS,
129      GAP_SEC_EVENT_AUTH_SUCCESS,
130      GAP_SEC_EVENT_AUTH_FAILED,
131      GAP_SEC_EVENT_ENC_SUCCESS,
132      GAP_SEC_EVENT_ENC_FAILED,
133      GAP_SEC_EVENT_WAIT_ENC_TIMEOUT,
134      GAP_SEC_EVENT_ACL_DISCONNECT,
135  };
136  
137  enum SecurityRequestStatus {
138      GAP_SEC_REQ_STATUS_WAIT_CONNECT,
139      GAP_SEC_REQ_STATUS_WAIT_FEATURE,
140      GAP_SEC_REQ_STATUS_WAIT_ENCRYPT,
141      GAP_SEC_REQ_STATUS_WAIT_ACTION,
142      GAP_SEC_REQ_STATUS_ACTION,
143      GAP_SEC_REQ_STATUS_SUCCESS,
144      GAP_SEC_REQ_STATUS_FAILED,
145  };
146  
147  enum EncryptionRequestStatus {
148      GAP_SET_ENCRYPTION_STATUS_IDLE,
149      GAP_SET_ENCRYPTION_STATUS_SETTING,
150  };
151  
152  enum LESecurityActionStatus {
153      GAP_LE_SECURITY_STATUS_IDLE,
154      GAP_LE_SECURITY_STATUS_PAIR,
155  };
156  
157  enum LEConnParamUpdateStatus {
158      GAP_LE_CONN_PARAM_UPDATE_IDLE,
159      GAP_LE_CONN_PARAM_UPDATE_RECV_L2CAP,
160      GAP_LE_CONN_PARAM_UPDATE_RECV_HCI,
161  };
162  
163  typedef struct {
164      uint8_t status;
165      GapSetScanModeResultCallback callback;
166      void *context;
167      GapDiscoverModeInfo discoverMode;
168      GapConnectableModeInfo connectableMode;
169  } ScanModeBlock;
170  
171  typedef struct {
172      BtAddr addr;
173      uint8_t repetitionMode;
174      uint16_t clockOffset;
175  } InquiryResultInfo;
176  
177  typedef struct {
178      uint8_t status;
179  } InquiryBlock;
180  
181  typedef struct {
182      uint8_t status;
183  } RemoteNameBlock;
184  
185  typedef struct {
186      List *registerlist;
187      List *requestlist;
188  } ProfileSecurityBlock;
189  
190  typedef struct {
191      BtAddr addr;
192      GapServiceSecurityInfo info;
193      uint16_t securityMode;
194  } ProfileSecurityInfo;
195  
196  typedef struct {
197      List *devicelist;
198  } ConnectionInfoBlock;
199  
200  typedef struct {
201      BtAddr addr;
202      bool needAuthentication;
203      bool needUnauthentication;
204      bool needEncryption;
205      bool needAuthorization;
206      bool needMITM;
207      GapServiceSecurityInfo info;
208      void *context;
209      bool doCallback;
210      GapSecurityResultCallback callback;
211      uint8_t status;
212      uint8_t hciStatus;
213      uint8_t retryCount;
214      Alarm *waitRetryalarm;
215  } RequestSecInfo;
216  
217  typedef struct {
218      BtAddr addr;
219      Alarm *alarm;
220      uint16_t handle;
221      uint8_t status;
222      uint8_t authenticationStatus;
223      uint8_t encryptionStatus;
224      uint8_t linkkeyType;
225      uint8_t localIOCap;
226      uint8_t remoteAuthReq;
227      bool inDedicatedBonding;
228      bool isAuthentication;
229      bool isEncryption;
230      bool supportSSP;
231      RequestSecInfo *actionReq;
232      Alarm *aclAlarm;
233      Alarm *waitEncryptAlarm;
234  } DeviceInfo;
235  
236  typedef struct {
237      uint8_t status;
238      GapEncryptionChangeCallback callback;
239      void *context;
240  } EncryptionBlock;
241  
242  typedef struct {
243      bool isEnable;
244      uint8_t bondableMode;
245      uint32_t classOfDevice;
246      GAP_SecurityMode secMode;
247      GapOOBData oobData192;
248      GapOOBData oobData256;
249      bool keyMissingRetry;
250      ScanModeBlock scanModeBlock;
251      InquiryBlock inquiryBlock;
252      RemoteNameBlock remoteNameBlock;
253      ProfileSecurityBlock profileSecBlock;
254      ConnectionInfoBlock connectionInfoBlock;
255      EncryptionBlock encryptionBlock;
256  } GapBredrMng;
257  
258  #endif
259  
260  #ifdef GAP_LE_SUPPORT
261  
262  typedef struct {
263      BtAddr addr;
264      BtAddr identityAddr;
265      uint8_t role;
266      uint8_t bondableMode;
267      uint8_t minEncKeySize;
268      GAP_LeSecMode1Level mode1Level;
269      GAP_LeSecMode2Level mode2Level;
270      uint8_t IRK[GAP_IRK_SIZE];
271  } LeLocalInfo;
272  
273  typedef struct {
274      uint8_t result;
275      GAP_LeSecurityStatus secReqStatus;
276      GapLeRequestSecurityResult callback;
277      void *context;
278  } LeSecurityReq;
279  
280  typedef struct {
281      uint8_t status;
282      uint8_t id;
283  } LeConnParamUpdateReq;
284  
285  typedef struct {
286      BtAddr addr;
287      uint16_t handle;
288      uint8_t role;
289      uint8_t securityStatus;
290      uint8_t encryptionStatus;
291      uint8_t pairMethod;
292      uint8_t keyType;
293      uint8_t keySize;
294      bool isRemoteSecurityRequest;
295      bool isLocalSecurityRequest;
296      BtAddr peerAddr;
297      BtAddr ownAddr;
298      Alarm *alarm;
299      bool ownAddrUpdated;
300      LeSecurityReq *securityReq;
301      LeConnParamUpdateReq *paramUpdateReq;
302      bool localSigningExists;
303      bool remoteSigningExists;
304      LeSignKey localSigningInfo;
305      LeSignKey remoteSigningInfo;
306  } LeDeviceInfo;
307  
308  typedef struct {
309      List *deviceList;
310  } LeConnectionInfoBlock;
311  
312  typedef struct {
313      bool isPairing;
314      BtAddr addr;
315  } LeBondBlock;
316  
317  typedef struct {
318      List *RequestList;
319  } LeSignatureBlock;
320  
321  typedef struct {
322      struct {
323          bool processing;
324          GenResPriAddrResult callback;
325          void *context;
326      } generationInfo;
327  
328      List *reportRPAResolveList;
329  } LeRandomAddressBlock;
330  
331  typedef struct {
332      uint8_t advHandle;
333      uint8_t randomAddress[BT_ADDRESS_SIZE];
334  } LeExAdvInfo;
335  
336  typedef struct {
337      List *exAdvInfoList;
338      uint8_t exAdvMaxNumber;
339      uint16_t exAdvDataMaxLen;
340  } LeExAdvBlock;
341  
342  typedef struct {
343      bool isEnable;
344      LeLocalInfo local;
345      LeBondBlock bondBlock;
346      LeConnectionInfoBlock connectionInfoBlock;
347      LeSignatureBlock signatureBlock;
348      LeRandomAddressBlock randomAddressBlock;
349      LeExAdvBlock exAdvBlock;
350  } GapLeMng;
351  
352  #endif
353  
354  typedef struct {
355      int traceLevel;
356  #ifdef GAP_BREDR_SUPPORT
357      GapBredrMng bredr;
358  #endif
359  #ifdef GAP_LE_SUPPORT
360      GapLeMng le;
361  #endif
362  } GapMng;
363  
364  #ifdef __cplusplus
365  }
366  #endif
367  
368  #endif
369