• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "btm_controller.h"
17 
18 #include <securec.h>
19 
20 #include "btstack.h"
21 #include "hci/hci.h"
22 #include "hci/hci_def.h"
23 #include "hci/hci_error.h"
24 #include "log.h"
25 #include "platform/include/allocator.h"
26 #include "platform/include/event.h"
27 
28 #include "btm.h"
29 
30 #define WAIT_CMD_TIMEOUT 15000
31 
32 #define EXTENDED_FEATURES_PAGE_2 2
33 
34 #define CHECK_RESULT_BREAK(x) \
35     if ((x) != BT_NO_ERROR) { \
36         break;                \
37     }
38 
39 // Setup Controller
40 static HciEventCallbacks g_hciEventCallbacks;
41 
42 static Event *g_waitSetupController = NULL;
43 
44 static HciResetReturnParam g_hciResetResult;
45 static HciReadBufferSizeReturnParam g_readBufferSizeResult;
46 static HciHostBufferSizeReturnParam g_hostBufferSizeResult;
47 static HciReadLocalVersionInformationReturnParam g_readLocalVersionResult;
48 static HciReadBdAddrReturnParam g_readBdAddrResult;
49 static HciReadLocalSupportedCommandsReturnParam g_readLocalSupportedCommandsResult;
50 static HciReadLocalSupportedFeaturesReturnParam g_readLocalSupportedFeaturesResult;
51 static HciSetEventMaskReturnParam g_setEventMaskResult;
52 static uint8_t g_readLocalSupportedCodecsResult;
53 static BtmLocalSupportedCodecs g_localSupportedCodecs;
54 static HciReadLocalExtendedFeaturesReturnParam g_readLocalExtendedFeaturesResult[MAX_EXTENED_FEATURES_PAGE_COUNT];
55 static HciWriteLeHostSupportReturnParam g_writeLeHostSupportedResult;
56 
57 static HciLeReadBufferSizeReturnParam g_leReadBufferSizeResult;
58 static HciLeReadLocalSupportedFeaturesReturnParam g_leReadLocalSupportedFeaturesResult;
59 static HciLeSetEventMaskReturnParam g_leSetEventMaskResult;
60 static HciLeReadWhiteListSizeReturnParam g_leReadWhiteListSizeResult;
61 static HciLeReadResolvingListSizeReturnParam g_leReadResolvingListSizeResult;
62 
BtmControllerOnResetComplete(const HciResetReturnParam * returnParam)63 static void BtmControllerOnResetComplete(const HciResetReturnParam *returnParam)
64 {
65     g_hciResetResult = *returnParam;
66     EventSet(g_waitSetupController);
67 }
68 
BtmControllerOnReadBufferSizeComplete(const HciReadBufferSizeReturnParam * returnParam)69 static void BtmControllerOnReadBufferSizeComplete(const HciReadBufferSizeReturnParam *returnParam)
70 {
71     g_readBufferSizeResult = *returnParam;
72     EventSet(g_waitSetupController);
73 }
74 
BtmControllerOnHostBufferSizeComplete(const HciHostBufferSizeReturnParam * returnParam)75 static void BtmControllerOnHostBufferSizeComplete(const HciHostBufferSizeReturnParam *returnParam)
76 {
77     g_hostBufferSizeResult = *returnParam;
78     EventSet(g_waitSetupController);
79 }
80 
BtmControllerOnReadLocalVersionInformationComplete(const HciReadLocalVersionInformationReturnParam * returnParam)81 static void BtmControllerOnReadLocalVersionInformationComplete(
82     const HciReadLocalVersionInformationReturnParam *returnParam)
83 {
84     g_readLocalVersionResult = *returnParam;
85     EventSet(g_waitSetupController);
86 }
87 
BtmControllerOnReadBdAddrComplete(const HciReadBdAddrReturnParam * returnParam)88 static void BtmControllerOnReadBdAddrComplete(const HciReadBdAddrReturnParam *returnParam)
89 {
90     g_readBdAddrResult = *returnParam;
91     EventSet(g_waitSetupController);
92 }
93 
BtmControllerOnReadLocalSupportedCommandsComplete(const HciReadLocalSupportedCommandsReturnParam * returnParam)94 static void BtmControllerOnReadLocalSupportedCommandsComplete(
95     const HciReadLocalSupportedCommandsReturnParam *returnParam)
96 {
97     g_readLocalSupportedCommandsResult = *returnParam;
98     EventSet(g_waitSetupController);
99 }
100 
BtmControllerOnReadLocalSupportedFeaturesComlete(const HciReadLocalSupportedFeaturesReturnParam * returnParam)101 static void BtmControllerOnReadLocalSupportedFeaturesComlete(
102     const HciReadLocalSupportedFeaturesReturnParam *returnParam)
103 {
104     g_readLocalSupportedFeaturesResult = *returnParam;
105     EventSet(g_waitSetupController);
106 }
107 
BtmControllerOnReadLocalExtendedFeaturesComplete(const HciReadLocalExtendedFeaturesReturnParam * returnParam)108 static void BtmControllerOnReadLocalExtendedFeaturesComplete(const HciReadLocalExtendedFeaturesReturnParam *returnParam)
109 {
110     if (returnParam->pageNumber < MAX_EXTENED_FEATURES_PAGE_COUNT) {
111         g_readLocalExtendedFeaturesResult[returnParam->pageNumber] = *returnParam;
112     }
113     EventSet(g_waitSetupController);
114 }
115 
BtmControllerOnSetEventMaskComplete(const HciSetEventMaskReturnParam * returnParam)116 static void BtmControllerOnSetEventMaskComplete(const HciSetEventMaskReturnParam *returnParam)
117 {
118     g_setEventMaskResult = *returnParam;
119     EventSet(g_waitSetupController);
120 }
121 
BtmControllerCopySupportedCodecs(const HciReadLocalSupportedCodecsReturnParam * returnParam)122 static void BtmControllerCopySupportedCodecs(const HciReadLocalSupportedCodecsReturnParam *returnParam)
123 {
124     g_localSupportedCodecs.numberOfSupportedCodecs = returnParam->numberOfSupportedCodecs;
125     if (g_localSupportedCodecs.numberOfSupportedCodecs) {
126         g_localSupportedCodecs.supportedCodecs =
127             MEM_MALLOC.alloc(sizeof(uint8_t) * g_localSupportedCodecs.numberOfSupportedCodecs);
128         if (g_localSupportedCodecs.supportedCodecs) {
129             for (uint8_t i = 0; i < g_localSupportedCodecs.numberOfSupportedCodecs; i++) {
130                 g_localSupportedCodecs.supportedCodecs[i] = returnParam->supportedCodecs[i];
131             }
132         }
133     }
134 }
135 
BtmControllerCopyVendorSpecificCodecs(const HciReadLocalSupportedCodecsReturnParam * returnParam)136 static void BtmControllerCopyVendorSpecificCodecs(const HciReadLocalSupportedCodecsReturnParam *returnParam)
137 {
138     g_localSupportedCodecs.numberOfSupportedVendorSpecificCodecs = returnParam->numberOfSupportedVendorSpecificCodecs;
139     if (g_localSupportedCodecs.numberOfSupportedVendorSpecificCodecs) {
140         g_localSupportedCodecs.vendorSpecificCodecs = MEM_MALLOC.alloc(
141             sizeof(BtmVendorSpecificCodec) * g_localSupportedCodecs.numberOfSupportedVendorSpecificCodecs);
142         if (g_localSupportedCodecs.vendorSpecificCodecs) {
143             for (uint8_t i = 0; i < g_localSupportedCodecs.numberOfSupportedVendorSpecificCodecs; i++) {
144                 g_localSupportedCodecs.vendorSpecificCodecs[i].companyID =
145                     returnParam->vendorSpecificCodecs[i].companyID;
146                 g_localSupportedCodecs.vendorSpecificCodecs[i].vendorDefinedCodecID =
147                     returnParam->vendorSpecificCodecs[i].vendorDefinedCodecID;
148             }
149         }
150     }
151 }
152 
BtmControllerOnReadLocalSupportedCodecs(const HciReadLocalSupportedCodecsReturnParam * returnParam)153 static void BtmControllerOnReadLocalSupportedCodecs(const HciReadLocalSupportedCodecsReturnParam *returnParam)
154 {
155     g_readLocalSupportedCodecsResult = returnParam->status;
156     if (g_readLocalSupportedCodecsResult == HCI_SUCCESS) {
157         BtmControllerCopySupportedCodecs(returnParam);
158         BtmControllerCopyVendorSpecificCodecs(returnParam);
159     }
160 
161     EventSet(g_waitSetupController);
162 }
163 
BtmControllerOnLeReadBufferSizeComplete(const HciLeReadBufferSizeReturnParam * returnParam)164 static void BtmControllerOnLeReadBufferSizeComplete(const HciLeReadBufferSizeReturnParam *returnParam)
165 {
166     g_leReadBufferSizeResult = *returnParam;
167     EventSet(g_waitSetupController);
168 }
169 
BtmControllerOnLeReadLocalSupportedFeaturesComplete(const HciLeReadLocalSupportedFeaturesReturnParam * returnParam)170 static void BtmControllerOnLeReadLocalSupportedFeaturesComplete(
171     const HciLeReadLocalSupportedFeaturesReturnParam *returnParam)
172 {
173     g_leReadLocalSupportedFeaturesResult = *returnParam;
174     EventSet(g_waitSetupController);
175 }
176 
BtmControllerOnLeSetEventMaskComplete(const HciLeSetEventMaskReturnParam * returnParam)177 static void BtmControllerOnLeSetEventMaskComplete(const HciLeSetEventMaskReturnParam *returnParam)
178 {
179     g_leSetEventMaskResult = *returnParam;
180     EventSet(g_waitSetupController);
181 }
182 
BtmControllerOnLeReadWhiteListSizeComplete(const HciLeReadWhiteListSizeReturnParam * returnParam)183 static void BtmControllerOnLeReadWhiteListSizeComplete(const HciLeReadWhiteListSizeReturnParam *returnParam)
184 {
185     g_leReadWhiteListSizeResult = *returnParam;
186     EventSet(g_waitSetupController);
187 }
188 
BtmControllerOnLeReadResolvingListSizeComplete(const HciLeReadResolvingListSizeReturnParam * returnParam)189 static void BtmControllerOnLeReadResolvingListSizeComplete(const HciLeReadResolvingListSizeReturnParam *returnParam)
190 {
191     g_leReadResolvingListSizeResult = *returnParam;
192     EventSet(g_waitSetupController);
193 }
194 
BtmControllerOnWriteLeHostSupportedComplete(const HciWriteLeHostSupportReturnParam * returnParam)195 static void BtmControllerOnWriteLeHostSupportedComplete(const HciWriteLeHostSupportReturnParam *returnParam)
196 {
197     g_writeLeHostSupportedResult = *returnParam;
198     EventSet(g_waitSetupController);
199 }
200 
BtmIsControllerSupportedReadLocalSupportedCodecsCommand()201 bool BtmIsControllerSupportedReadLocalSupportedCodecsCommand()
202 {
203     return HCI_SUPPORT_READ_LOCAL_SUPPORTED_CODECS(g_readLocalSupportedCommandsResult.supportedCommands);
204 }
205 
BtmIsControllerSupportedEnhancedSetupSynchronousConnection()206 bool BtmIsControllerSupportedEnhancedSetupSynchronousConnection()
207 {
208     return HCI_SUPPORT_ENHANCED_SETUP_SYNCHRONOUS_CONNECTION(g_readLocalSupportedCommandsResult.supportedCommands);
209 }
210 
BtmIsControllerSupportedEnhancedAcceptSynchronousConnection()211 bool BtmIsControllerSupportedEnhancedAcceptSynchronousConnection()
212 {
213     return HCI_SUPPORT_ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION(g_readLocalSupportedCommandsResult.supportedCommands);
214 }
215 
BtmIsControllerSupportedLeReadLocalP256PublicKey()216 static bool BtmIsControllerSupportedLeReadLocalP256PublicKey()
217 {
218     return HCI_SUPPORT_LE_READ_LOCAL_P_256_PUBLIC_KEY(g_readLocalSupportedCommandsResult.supportedCommands);
219 }
220 
BtmIsControllerSupportedLeGenerateDhKey()221 static bool BtmIsControllerSupportedLeGenerateDhKey()
222 {
223     return HCI_SUPPORT_LE_GENERATE_DH_KEY(g_readLocalSupportedCommandsResult.supportedCommands);
224 }
225 
BtmIsControllerSupportedLeSetPrivacyMode()226 bool BtmIsControllerSupportedLeSetPrivacyMode()
227 {
228     return HCI_SUPPORT_LE_SET_PRIVACY_MODE(g_readLocalSupportedCommandsResult.supportedCommands);
229 }
230 
231 static HciEventCallbacks g_hciEventCallbacks = {
232     .resetComplete = BtmControllerOnResetComplete,
233     .readBufferSizeComplete = BtmControllerOnReadBufferSizeComplete,
234     .hostBufferSizeComplete = BtmControllerOnHostBufferSizeComplete,
235     .readLocalVersionInformationComplete = BtmControllerOnReadLocalVersionInformationComplete,
236     .readBdAddrComplete = BtmControllerOnReadBdAddrComplete,
237     .readLocalSupportedCommandsComplete = BtmControllerOnReadLocalSupportedCommandsComplete,
238     .readLocalSupportedFeaturesComplete = BtmControllerOnReadLocalSupportedFeaturesComlete,
239     .readLocalExtendedFeaturesComplete = BtmControllerOnReadLocalExtendedFeaturesComplete,
240     .setEventMaskComplete = BtmControllerOnSetEventMaskComplete,
241     .readLocalSupportedCodecsComplete = BtmControllerOnReadLocalSupportedCodecs,
242     .writeLeHostSupportComplete = BtmControllerOnWriteLeHostSupportedComplete,
243 
244     .leSetEventMaskComplete = BtmControllerOnLeSetEventMaskComplete,
245     .leReadBufferSizeComplete = BtmControllerOnLeReadBufferSizeComplete,
246     .leReadLocalSupportedFeaturesComplete = BtmControllerOnLeReadLocalSupportedFeaturesComplete,
247     .leReadWhiteListSizeComplete = BtmControllerOnLeReadWhiteListSizeComplete,
248     .leReadResolvingListSizeComplete = BtmControllerOnLeReadResolvingListSizeComplete,
249 };
250 
BtmGetLeEventMask()251 static uint64_t BtmGetLeEventMask()
252 {
253     uint64_t leEventMask = LE_EVENT_MASK_DEFAULT;
254 
255     // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
256     // Table 3.2: Bluetooth Controller supporting LE requirements
257 
258     // C6
259     if (BTM_IsControllerSupportConnectionParametersRequestProcedure()) {
260         leEventMask |= LE_EVENT_MASK_LE_REMOTE_CONNECTION_PARAMETER_REQUEST_EVENT;
261     }
262     // C8
263     if (BTM_IsControllerSupportLeDataPacketLengthExtension()) {
264         leEventMask |= LE_EVENT_MASK_LE_DATA_LENGTH_CHANGE_EVENT;
265     }
266     // C9
267     if (BTM_IsControllerSupportLlPrivacy()) {
268         leEventMask |= LE_EVENT_MASK_LE_DIRECTED_ADVERTISING_REPORT_EVENT;
269     }
270     // C11
271     if (BTM_IsControllerSupportLe2MPhy() || BTM_IsControllerSupportLeCodedPhy()) {
272         leEventMask |= LE_EVENT_MASK_LE_PHY_UPDATE_COMPLETE_EVENT;
273     }
274     // C17
275     if (BTM_IsControllerSupportLeExtendedAdvertising()) {
276         leEventMask |=
277             (LE_EVENT_MASK_LE_SCAN_REQUEST_RECEIVED_EVENT | LE_EVENT_MASK_LE_EXTENDED_ADVERTISING_SET_TERMINATED_EVENT);
278     }
279     // C19
280     if (BTM_IsControllerSupportLeExtendedAdvertising()) {
281         leEventMask |=
282             (LE_EVENT_MASK_LE_EXTENDED_SCAN_TIMEOUT_EVENT | LE_EVENT_MASK_LE_EXTENDED_ADVERTISING_REPORT_EVENT);
283     }
284     // C20
285     if (BTM_IsControllerSupportLePeriodicAdvertising()) {
286         leEventMask |= (LE_EVENT_MASK_LE_PERIODIC_ADVERTISING_REPORT_EVENT |
287                         LE_EVENT_MASK_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHED_EVENT |
288                         LE_EVENT_MASK_LE_PERIODIC_ADVERTISING_SYNC_LOST_EVENT);
289     }
290     // C22
291     if (BTM_IsControllerSupportChannelSelectionAlgorithm2()) {
292         leEventMask |= LE_EVENT_MASK_LE_CHANNEL_SELECTION_ALGORITHM_EVENT;
293     }
294     // C23
295     if (BTM_IsControllerSupportLlPrivacy() || BTM_IsControllerSupportLeExtendedAdvertising()) {
296         leEventMask |= LE_EVENT_MASK_LE_ENHANCED_CONNECTION_COMPLETE_EVENT;
297     }
298     // Optional
299     if (BtmIsControllerSupportedLeReadLocalP256PublicKey()) {
300         leEventMask |= LE_EVENT_MASK_LE_READ_LOCAL_P256_PUBLIC_KEY_COMPLETE_EVENT;
301     }
302     if (BtmIsControllerSupportedLeGenerateDhKey()) {
303         leEventMask |= LE_EVENT_MASK_LE_GENERATE_DHKEY_COMPLETE_EVENT;
304     }
305 
306     return leEventMask;
307 }
308 
BtmHciReset()309 static int BtmHciReset()
310 {
311     int result = HCI_Reset();
312     if (result != BT_NO_ERROR) {
313         LOG_ERROR("HCI_Reset failed: %{public}d", result);
314         return result;
315     }
316     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
317         if (g_hciResetResult.status != HCI_SUCCESS) {
318             LOG_ERROR("HCI_Reset status: 0x%02x", g_hciResetResult.status);
319             result = BT_OPERATION_FAILED;
320         }
321     } else {
322         LOG_ERROR("HCI_Reset Timeout");
323         result = BT_OPERATION_FAILED;
324     }
325 
326     return result;
327 }
328 
BtmReadBufferSize()329 static int BtmReadBufferSize()
330 {
331     int result = HCI_ReadBufferSize();
332     if (result != BT_NO_ERROR) {
333         LOG_ERROR("HCI_ReadBufferSize failed: %{public}d", result);
334         return result;
335     }
336     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
337         switch (g_readBufferSizeResult.status) {
338             case HCI_SUCCESS:
339             case HCI_UNKNOWN_HCI_COMMAND:
340                 result = BT_NO_ERROR;
341                 break;
342             default:
343                 LOG_ERROR("HCI_ReadBufferSize status: 0x%02x", g_readBufferSizeResult.status);
344                 result = BT_OPERATION_FAILED;
345                 break;
346         }
347     } else {
348         LOG_ERROR("HCI_ReadBufferSize Timeout");
349         result = BT_OPERATION_FAILED;
350     }
351     return result;
352 }
353 
BtmHostBufferSize()354 static int BtmHostBufferSize()
355 {
356     HciHostBufferSizeCmdParam hostBufferSizeParam = {
357         .hostAclDataPacketLength = L2CAP_MTU_SIZE,
358         .hostSynchronousDataPacketLength = SCO_HOST_BUFFER_SIZE,
359         .hostTotalNumAclDataPackets = HOST_ACL_DATA_PACKETS,
360         .hostTotalNumSynchronousDataPackets = HOST_SCO_DATA_PACKETS,
361     };
362     int result = HCI_HostBufferSize(&hostBufferSizeParam);
363     if (result != BT_NO_ERROR) {
364         LOG_ERROR("HCI_HostBufferSize failed: %{public}d", result);
365         return result;
366     }
367     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
368         switch (g_hostBufferSizeResult.status) {
369             case HCI_SUCCESS:
370             case HCI_UNKNOWN_HCI_COMMAND:
371                 result = BT_NO_ERROR;
372                 break;
373             default:
374                 LOG_ERROR("HCI_HostBufferSize status: 0x%02x", g_hostBufferSizeResult.status);
375                 result = BT_OPERATION_FAILED;
376                 break;
377         }
378     } else {
379         LOG_ERROR("HCI_HostBufferSize Timeout");
380         result = BT_OPERATION_FAILED;
381     }
382     return result;
383 }
384 
BtmReadLocalVersionInformation()385 static int BtmReadLocalVersionInformation()
386 {
387     int result = HCI_ReadLocalVersionInformation();
388     if (result != BT_NO_ERROR) {
389         LOG_ERROR("HCI_ReadLocalVersionInformation failed: %{public}d", result);
390         return result;
391     }
392     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
393         if (g_readLocalVersionResult.status != HCI_SUCCESS) {
394             LOG_ERROR("HCI_ReadLocalVersionInformation status: 0x%02x", g_readLocalVersionResult.status);
395             result = BT_OPERATION_FAILED;
396         }
397     } else {
398         LOG_ERROR("HCI_ReadLocalVersionInformation Timeout");
399         result = BT_OPERATION_FAILED;
400     }
401     return result;
402 }
403 
BtmReadBdAddr()404 static int BtmReadBdAddr()
405 {
406     int result = HCI_ReadBdAddr();
407     if (result != BT_NO_ERROR) {
408         LOG_ERROR("HCI_ReadBdAddr failed: %{public}d", result);
409         return result;
410     }
411     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
412         if (g_readBdAddrResult.status != HCI_SUCCESS) {
413             LOG_INFO("No public address");
414             result = BT_NO_ERROR;
415         }
416     } else {
417         LOG_ERROR("HCI_ReadBdAddr Timeout");
418         result = BT_OPERATION_FAILED;
419     }
420     return result;
421 }
422 
BtmReadLocalSupportedCommands()423 static int BtmReadLocalSupportedCommands()
424 {
425     int result = HCI_ReadLocalSupportedCommands();
426     if (result != BT_NO_ERROR) {
427         LOG_ERROR("HCI_ReadLocalSupportedCommands failed: %{public}d", result);
428         return result;
429     }
430     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
431         if (g_readLocalSupportedCommandsResult.status != HCI_SUCCESS) {
432             LOG_ERROR("HCI_ReadLocalSupportedCommands status: 0x%02x", g_readLocalSupportedCommandsResult.status);
433             result = BT_OPERATION_FAILED;
434         }
435     } else {
436         LOG_ERROR("HCI_ReadLocalSupportedCommands Timeout");
437         result = BT_OPERATION_FAILED;
438     }
439     return result;
440 }
441 
BtmReadLocalSupportedFeatures()442 static int BtmReadLocalSupportedFeatures()
443 {
444     int result = HCI_ReadLocalSupportedFeatures();
445     if (result != BT_NO_ERROR) {
446         LOG_ERROR("HCI_ReadLocalSupportedFeatures failed: %{public}d", result);
447         return result;
448     }
449     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
450         if (g_readLocalSupportedFeaturesResult.status != HCI_SUCCESS) {
451             LOG_ERROR("HCI_ReadLocalSupportedFeatures status: 0x%02x", g_readLocalSupportedFeaturesResult.status);
452             result = BT_OPERATION_FAILED;
453         }
454     } else {
455         LOG_ERROR("HCI_ReadLocalSupportedFeatures Timeout");
456         result = BT_OPERATION_FAILED;
457     }
458     return result;
459 }
460 
BtmReadLocalExtendedFeatures()461 static int BtmReadLocalExtendedFeatures()
462 {
463     int result;
464 
465     HciReadLocalExtendedFeaturesParam readLocalExtendedFeaturesParam = {
466         .pageNumber = 0,
467     };
468 
469     for (uint8_t i = 0; i < MAX_EXTENED_FEATURES_PAGE_COUNT; i++) {
470         readLocalExtendedFeaturesParam.pageNumber = i;
471 
472         result = HCI_ReadLocalExtendedFeatures(&readLocalExtendedFeaturesParam);
473         if (result != BT_NO_ERROR) {
474             LOG_ERROR("HCI_ReadLocalExtendedFeatures failed: %{public}d", result);
475             break;
476         }
477         if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) != 0) {
478             LOG_ERROR("HCI_ReadLocalExtendedFeatures Timeout");
479             result = BT_OPERATION_FAILED;
480             break;
481         }
482 
483         switch (g_readLocalExtendedFeaturesResult[i].status) {
484             case HCI_SUCCESS:
485             case HCI_UNKNOWN_HCI_COMMAND:
486                 result = BT_NO_ERROR;
487                 break;
488             default:
489                 LOG_ERROR("HCI_ReadLocalExtendedFeatures status: 0x%02x", g_readLocalExtendedFeaturesResult[i].status);
490                 result = BT_OPERATION_FAILED;
491                 break;
492         }
493 
494         if (result != BT_NO_ERROR) {
495             break;
496         }
497 
498         if (g_readLocalExtendedFeaturesResult[i].pageNumber >= g_readLocalExtendedFeaturesResult[i].maximunPageNumber) {
499             break;
500         }
501     }
502 
503     return result;
504 }
505 
BtmSetEventMask()506 static int BtmSetEventMask()
507 {
508     HciSetEventMaskParam setEventMaskParam = {
509         .eventMask = HCI_EVENT_MASK_CORE_5_0,
510     };
511     int result = HCI_SetEventMask(&setEventMaskParam);
512     if (result != BT_NO_ERROR) {
513         LOG_ERROR("HCI_SetEventMask failed: %{public}d", result);
514         return result;
515     }
516     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
517         if (g_setEventMaskResult.status != HCI_SUCCESS) {
518             LOG_ERROR("HCI_SetEventMask status: 0x%02x", g_setEventMaskResult.status);
519             result = BT_OPERATION_FAILED;
520         }
521     } else {
522         LOG_ERROR("HCI_SetEventMask Timeout");
523         result = BT_OPERATION_FAILED;
524     }
525     return result;
526 }
527 
BtmReadLocalSupportedCodecs()528 static int BtmReadLocalSupportedCodecs()
529 {
530     int result = HCI_ReadLocalSupportedCodecs();
531     if (result != BT_NO_ERROR) {
532         LOG_ERROR("HCI_ReadLocalSupportedCodecs failed: %{public}d", result);
533         return result;
534     }
535     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
536         if (g_readLocalSupportedCodecsResult != HCI_SUCCESS) {
537             LOG_ERROR("HCI_ReadLocalSupportedCodecs status: 0x%02x", g_readLocalSupportedCodecsResult);
538             result = BT_OPERATION_FAILED;
539         }
540     } else {
541         LOG_ERROR("HCI_ReadLocalSupportedCodecs Timeout");
542         result = BT_OPERATION_FAILED;
543     }
544     return result;
545 }
546 
BtmWriteLeHostSupport()547 static int BtmWriteLeHostSupport()
548 {
549     HciWriteLeHostSupportParam writeLeHostSupportParam = {
550         .leSupportedHost = HCI_LE_SUPPORTED_HOST_ENABLED,
551         .simultaneousLeHost = HCI_SIMULTANEOUS_LE_HOST_DISABLED,
552     };
553     int result = HCI_WriteLeHostSupport(&writeLeHostSupportParam);
554     if (result != BT_NO_ERROR) {
555         LOG_ERROR("HCI_WriteLeHostSupport failed: %{public}d", result);
556         return result;
557     }
558     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
559         switch (g_writeLeHostSupportedResult.status) {
560             case HCI_SUCCESS:
561             case HCI_UNKNOWN_HCI_COMMAND:
562                 result = BT_NO_ERROR;
563                 break;
564             default:
565                 LOG_ERROR("HCI_WriteLeHostSupport status: 0x%02x", g_writeLeHostSupportedResult.status);
566                 result = BT_OPERATION_FAILED;
567                 break;
568         }
569     } else {
570         LOG_ERROR("HCI_WriteLeHostSupport Timeout");
571         result = BT_OPERATION_FAILED;
572     }
573     return result;
574 }
575 
BtmLeReadBufferSize()576 static int BtmLeReadBufferSize()
577 {
578     int result = HCI_LeReadBufferSize();
579     if (result != BT_NO_ERROR) {
580         LOG_ERROR("HCI_LeReadBufferSize failed: %{public}d", result);
581         return result;
582     }
583     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
584         if (g_leReadBufferSizeResult.status != HCI_SUCCESS) {
585             LOG_ERROR("HCI_LeReadBufferSize status: 0x%02x", g_leReadBufferSizeResult.status);
586             result = BT_OPERATION_FAILED;
587         }
588     } else {
589         LOG_ERROR("HCI_LeReadBufferSize Timeout");
590         result = BT_OPERATION_FAILED;
591     }
592     return result;
593 }
594 
BtmLeReadWhiteListSize()595 static int BtmLeReadWhiteListSize()
596 {
597     int result = HCI_LeReadWhiteListSize();
598     if (result != BT_NO_ERROR) {
599         LOG_ERROR("HCI_LeReadWhiteListSize failed: %{public}d", result);
600         return result;
601     }
602     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
603         if (g_leReadWhiteListSizeResult.status != HCI_SUCCESS) {
604             LOG_ERROR("HCI_LeReadWhiteListSize status: 0x%02x", g_leReadWhiteListSizeResult.status);
605             result = BT_OPERATION_FAILED;
606         }
607     } else {
608         LOG_ERROR("HCI_LeReadWhiteListSize Timeout");
609         result = BT_OPERATION_FAILED;
610     }
611     return result;
612 }
613 
BtmLeReadLocalSupportedFeatures()614 static int BtmLeReadLocalSupportedFeatures()
615 {
616     int result = HCI_LeReadLocalSupportedFeatures();
617     if (result != BT_NO_ERROR) {
618         LOG_ERROR("HCI_LeReadLocalSupportedFeatures failed: %{public}d", result);
619         return result;
620     }
621     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
622         if (g_leReadLocalSupportedFeaturesResult.status != HCI_SUCCESS) {
623             LOG_ERROR("HCI_LeReadLocalSupportedFeatures status: 0x%02x", g_leReadLocalSupportedFeaturesResult.status);
624             result = BT_OPERATION_FAILED;
625         }
626     } else {
627         LOG_ERROR("HCI_LeReadLocalSupportedFeatures Timeout");
628         result = BT_OPERATION_FAILED;
629     }
630     return result;
631 }
632 
BtmLeReadResolvingListSize()633 static int BtmLeReadResolvingListSize()
634 {
635     int result = HCI_LeReadResolvingListSize();
636     if (result != BT_NO_ERROR) {
637         LOG_ERROR("HCI_LeReadResolvingListSize failed: %{public}d", result);
638         return result;
639     }
640     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
641         if (g_leReadResolvingListSizeResult.status != HCI_SUCCESS) {
642             LOG_ERROR("HCI_LeReadResolvingListSize status: 0x%02x", g_setEventMaskResult.status);
643             result = BT_OPERATION_FAILED;
644         }
645     } else {
646         LOG_ERROR("HCI_LeReadResolvingListSize Timeout");
647         result = BT_OPERATION_FAILED;
648     }
649     return result;
650 }
651 
BtmLeSetEventMask()652 static int BtmLeSetEventMask()
653 {
654     HciLeSetEventMaskParam lsSetEventMaskParam = {
655         .leEventMask = BtmGetLeEventMask(),
656     };
657     int result = HCI_LeSetEventMask(&lsSetEventMaskParam);
658     if (result != BT_NO_ERROR) {
659         LOG_ERROR("HCI_LeSetEventMask failed: %{public}d", result);
660         return result;
661     }
662     if (EventWait(g_waitSetupController, WAIT_CMD_TIMEOUT) == 0) {
663         if (g_leSetEventMaskResult.status != HCI_SUCCESS) {
664             LOG_ERROR("HCI_LeSetEventMask status: 0x%02x", g_leSetEventMaskResult.status);
665             result = BT_OPERATION_FAILED;
666         }
667     } else {
668         LOG_ERROR("HCI_LeSetEventMask Timeout");
669         result = BT_OPERATION_FAILED;
670     }
671     return result;
672 }
673 
BtmInitLeFeature()674 static int BtmInitLeFeature()
675 {
676     int result;
677 
678     do {
679         result = BtmWriteLeHostSupport();
680         CHECK_RESULT_BREAK(result);
681 
682         result = BtmLeReadBufferSize();
683         CHECK_RESULT_BREAK(result);
684 
685         HCI_SetLeBufferSize(
686             g_leReadBufferSizeResult.hcLeAclDataPacketLength, g_leReadBufferSizeResult.hcTotalNumLeDataPackets);
687 
688         result = BtmLeReadWhiteListSize();
689         CHECK_RESULT_BREAK(result);
690 
691         result = BtmLeReadLocalSupportedFeatures();
692         CHECK_RESULT_BREAK(result);
693 
694         if (BTM_IsControllerSupportLlPrivacy()) {
695             result = BtmLeReadResolvingListSize();
696             CHECK_RESULT_BREAK(result);
697         }
698 
699         result = BtmLeSetEventMask();
700     } while (0);
701 
702     return result;
703 }
704 
BtmInitController()705 int BtmInitController()
706 {
707     int result;
708 
709     HCI_RegisterEventCallbacks(&g_hciEventCallbacks);
710 
711     g_waitSetupController = EventCreate(true);
712 
713     do {
714         // Reset Command
715         result = BtmHciReset();
716         CHECK_RESULT_BREAK(result);
717 
718         // Read Buffer Size Command
719         result = BtmReadBufferSize();
720         CHECK_RESULT_BREAK(result);
721 
722         HCI_SetBufferSize(
723             g_readBufferSizeResult.hcAclDataPacketLength, g_readBufferSizeResult.hcTotalNumAclDataPackets);
724 
725         // Host Buffer Size Command
726         result = BtmHostBufferSize();
727         CHECK_RESULT_BREAK(result);
728 
729         // Read Local Version Information Command
730         result = BtmReadLocalVersionInformation();
731         CHECK_RESULT_BREAK(result);
732 
733         // Read BD_ADDR Command
734         result = BtmReadBdAddr();
735         CHECK_RESULT_BREAK(result);
736 
737         // Read Local Supported Commands Command
738         result = BtmReadLocalSupportedCommands();
739         CHECK_RESULT_BREAK(result);
740 
741         // Read Local Supported Features Command
742         result = BtmReadLocalSupportedFeatures();
743         CHECK_RESULT_BREAK(result);
744 
745         // Read Local Extended Features Command
746         result = BtmReadLocalExtendedFeatures();
747         CHECK_RESULT_BREAK(result);
748 
749         // Set Event Mask Command
750         result = BtmSetEventMask();
751         CHECK_RESULT_BREAK(result);
752 
753         if (BtmIsControllerSupportedReadLocalSupportedCodecsCommand()) {
754             BtmReadLocalSupportedCodecs();
755         }
756 
757         if (BTM_IsControllerSupportLe()) {
758             result = BtmInitLeFeature();
759         }
760     } while (0);
761 
762     EventDelete(g_waitSetupController);
763     g_waitSetupController = NULL;
764 
765     HCI_DeregisterEventCallbacks(&g_hciEventCallbacks);
766 
767     return result;
768 }
769 
BTM_IsControllerSupportBrEdr()770 bool BTM_IsControllerSupportBrEdr()
771 {
772     return HCI_SUPPORT_BREDR(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
773 }
774 
BTM_IsControllerSupportLe()775 bool BTM_IsControllerSupportLe()
776 {
777     return HCI_SUPPORT_LE(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
778 }
779 
BTM_IsControllerSupportRssiInquiryResponse()780 bool BTM_IsControllerSupportRssiInquiryResponse()
781 {
782     return HCI_SUPPORT_RSSI_INQUIRY_RESPONSE(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
783 }
784 
BTM_IsControllerSupportEirInquiryResponse()785 bool BTM_IsControllerSupportEirInquiryResponse()
786 {
787     return HCI_SUPPORT_EIR_INQUIRY_RESPONSE(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
788 }
789 
BTM_IsControllerSupportRoleSwitch()790 bool BTM_IsControllerSupportRoleSwitch()
791 {
792     return HCI_SUPPORT_ROLE_SWITCH(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
793 }
794 
BTM_IsControllerSupportHoldMode()795 bool BTM_IsControllerSupportHoldMode()
796 {
797     return HCI_SUPPORT_HOLD_MODE(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
798 }
799 
BTM_IsControllerSupportSniffMode()800 bool BTM_IsControllerSupportSniffMode()
801 {
802     return HCI_SUPPORT_SNIFF_MODE(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
803 }
804 
BTM_IsControllerSupportEsco()805 bool BTM_IsControllerSupportEsco()
806 {
807     return HCI_SUPPORT_ESCO_EV3(g_readLocalSupportedFeaturesResult.lmpFeatures.raw) ||
808            HCI_SUPPORT_ESCO_EV4(g_readLocalSupportedFeaturesResult.lmpFeatures.raw) ||
809            HCI_SUPPORT_ESCO_EV5(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
810 }
811 
BTM_IsControllerSupportSecureSimplePairing()812 bool BTM_IsControllerSupportSecureSimplePairing()
813 {
814     return HCI_SUPPORT_SECURE_SIMPLE_PAIRING(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
815 }
816 
BTM_IsControllerSupportSecureConnections()817 bool BTM_IsControllerSupportSecureConnections()
818 {
819     return HCI_SUPPORT_SECURE_CONNECTIONS(
820         g_readLocalExtendedFeaturesResult[EXTENDED_FEATURES_PAGE_2].extendedLMPFeatures);
821 }
822 
BTM_IsControllerSupportNonFlushablePacketBoundaryFlag()823 bool BTM_IsControllerSupportNonFlushablePacketBoundaryFlag()
824 {
825     return HCI_SUPPORT_NON_FLUSHABLE_PACKET_BONDARY_FLAG(g_readLocalSupportedFeaturesResult.lmpFeatures.raw);
826 }
827 
BTM_IsControllerSupportLePing()828 bool BTM_IsControllerSupportLePing()
829 {
830     return HCI_SUPPORT_LE_PING(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
831 }
832 
BTM_IsControllerSupportLlPrivacy()833 bool BTM_IsControllerSupportLlPrivacy()
834 {
835     return HCI_SUPPORT_LL_PRIVACY(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
836 }
837 
BTM_IsControllerSupportLe2MPhy()838 bool BTM_IsControllerSupportLe2MPhy()
839 {
840     return HCI_SUPPORT_LE_2M_PHY(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
841 }
842 
BTM_IsControllerSupportLeCodedPhy()843 bool BTM_IsControllerSupportLeCodedPhy()
844 {
845     return HCI_SUPPORT_LE_CODED_PHY(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
846 }
847 
BTM_IsControllerSupportLeExtendedAdvertising()848 bool BTM_IsControllerSupportLeExtendedAdvertising()
849 {
850     return HCI_SUPPORT_LE_EXTENDED_ADVERTISING(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
851 }
852 
BTM_IsControllerSupportLeDataPacketLengthExtension()853 bool BTM_IsControllerSupportLeDataPacketLengthExtension()
854 {
855     return HCI_SUPPORT_LE_DATA_PACKET_LENGTH_EXTENSION(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
856 }
857 
BTM_IsControllerSupportChannelSelectionAlgorithm2()858 bool BTM_IsControllerSupportChannelSelectionAlgorithm2()
859 {
860     return HCI_SUPPURT_CHANNEL_SELECTION_ALGORITHM_2(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
861 }
862 
BTM_IsControllerSupportConnectionParametersRequestProcedure()863 bool BTM_IsControllerSupportConnectionParametersRequestProcedure()
864 {
865     return HCI_SUPPORT_CONNECTION_PARAMETERS_REQUEST_PROCEDURE(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
866 }
867 
BTM_IsControllerSupportLePeriodicAdvertising()868 bool BTM_IsControllerSupportLePeriodicAdvertising()
869 {
870     return HCI_SUPPORT_LE_PERIODIC_ADVERTISING(g_leReadLocalSupportedFeaturesResult.leFeatures.raw);
871 }
872 
BtmGetLocalSupportedFeature(HciLmpFeatures * lmpFeature)873 bool BtmGetLocalSupportedFeature(HciLmpFeatures *lmpFeature)
874 {
875     *lmpFeature = g_readLocalSupportedFeaturesResult.lmpFeatures;
876     return true;
877 }
878 
BTM_GetLocalAddr(BtAddr * addr)879 int BTM_GetLocalAddr(BtAddr *addr)
880 {
881     if (addr == NULL) {
882         return BT_BAD_PARAM;
883     }
884 
885     int result = BT_NO_ERROR;
886     if (g_readBdAddrResult.status != HCI_SUCCESS) {
887         result = BT_BAD_STATUS;
888     } else {
889         errno_t err = memcpy_s(addr->addr, BT_ADDRESS_SIZE, g_readBdAddrResult.bdAddr.raw, BT_ADDRESS_SIZE);
890         if (err == EOK) {
891             addr->type = BT_PUBLIC_DEVICE_ADDRESS;
892         } else {
893             result = BT_NO_MEMORY;
894         }
895     }
896     return result;
897 }
898 
BTM_GetLocalSupportedFeatures(uint8_t features[8])899 int BTM_GetLocalSupportedFeatures(uint8_t features[8])
900 {
901     int result = BT_NO_ERROR;
902 
903     if (g_readLocalSupportedFeaturesResult.status == BT_NO_ERROR) {
904         errno_t err = memcpy_s(
905             features, LMP_FEATURES_SIZE, g_readLocalSupportedFeaturesResult.lmpFeatures.raw, LMP_FEATURES_SIZE);
906         if (err != EOK) {
907             result = BT_NO_MEMORY;
908         }
909     } else {
910         result = BT_BAD_STATUS;
911     }
912 
913     return result;
914 }
915 
BTM_GetLocalVersionInformation(BtmLocalVersionInformation * localVersion)916 int BTM_GetLocalVersionInformation(BtmLocalVersionInformation *localVersion)
917 {
918     if (localVersion == NULL) {
919         return BT_BAD_PARAM;
920     }
921 
922     if (g_readLocalVersionResult.status != HCI_SUCCESS) {
923         return BT_OPERATION_FAILED;
924     }
925 
926     localVersion->hciVersion = g_readLocalVersionResult.hciVersion;
927     localVersion->hciRevision = g_readLocalVersionResult.hciRevision;
928     localVersion->lmpVersion = g_readLocalVersionResult.lmpVersion;
929     localVersion->manufacturerName = g_readLocalVersionResult.manufacturerName;
930     localVersion->lmpSubversion = g_readLocalVersionResult.lmpSubversion;
931 
932     return BT_NO_ERROR;
933 }
934 
BtmGetWhiteListSize(uint8_t * whiteListSize)935 int BtmGetWhiteListSize(uint8_t *whiteListSize)
936 {
937     if (g_leReadWhiteListSizeResult.status != HCI_SUCCESS) {
938         return BT_OPERATION_FAILED;
939     }
940 
941     *whiteListSize = g_leReadWhiteListSizeResult.whiteListSize;
942 
943     return BT_NO_ERROR;
944 }
945 
BtmGetResolvingListSize(uint8_t * resolvingListSize)946 int BtmGetResolvingListSize(uint8_t *resolvingListSize)
947 {
948     if (g_leReadResolvingListSizeResult.status != HCI_SUCCESS) {
949         return BT_OPERATION_FAILED;
950     }
951 
952     *resolvingListSize = g_leReadResolvingListSizeResult.resolvingListSize;
953 
954     return BT_NO_ERROR;
955 }
956 
BTM_GetLocalSupportedCodecs(BtmLocalSupportedCodecs ** localSupportedCodes)957 int BTM_GetLocalSupportedCodecs(BtmLocalSupportedCodecs **localSupportedCodes)
958 {
959     if (g_readLocalSupportedCodecsResult != HCI_SUCCESS) {
960         return BT_OPERATION_FAILED;
961     }
962 
963     *localSupportedCodes = &g_localSupportedCodecs;
964 
965     return BT_NO_ERROR;
966 }
967 
BTM_GetAclDataPacketLength(uint16_t * aclDataPacketLength)968 int BTM_GetAclDataPacketLength(uint16_t *aclDataPacketLength)
969 {
970     if (g_readBufferSizeResult.status != HCI_SUCCESS) {
971         return BT_OPERATION_FAILED;
972     }
973 
974     *aclDataPacketLength = g_readBufferSizeResult.hcAclDataPacketLength;
975 
976     return BT_NO_ERROR;
977 }
978 
BTM_GetLeAclDataPacketLength(uint16_t * leAclDataPacketLength)979 int BTM_GetLeAclDataPacketLength(uint16_t *leAclDataPacketLength)
980 {
981     if (g_leReadBufferSizeResult.status != HCI_SUCCESS) {
982         return BT_OPERATION_FAILED;
983     }
984 
985     if (g_leReadBufferSizeResult.hcLeAclDataPacketLength) {
986         *leAclDataPacketLength = g_leReadBufferSizeResult.hcLeAclDataPacketLength;
987     } else {
988         *leAclDataPacketLength = g_readBufferSizeResult.hcAclDataPacketLength;
989     }
990 
991     return BT_NO_ERROR;
992 }
993 
BtmCloseController()994 void BtmCloseController()
995 {
996     if (g_localSupportedCodecs.supportedCodecs) {
997         MEM_MALLOC.free(g_localSupportedCodecs.supportedCodecs);
998         g_localSupportedCodecs.supportedCodecs = NULL;
999     }
1000     if (g_localSupportedCodecs.vendorSpecificCodecs) {
1001         MEM_MALLOC.free(g_localSupportedCodecs.vendorSpecificCodecs);
1002         g_localSupportedCodecs.vendorSpecificCodecs = NULL;
1003     }
1004 }