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 "hci/hci.h"
17 
18 #include <securec.h>
19 
20 #include "btstack.h"
21 #include "platform/include/allocator.h"
22 
23 #include "hci_cmd.h"
24 
25 #define BITS_IN_BYTE 8
26 
27 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
28 // 7.8.1 LE Set Event Mask Command
HCI_LeSetEventMask(const HciLeSetEventMaskParam * param)29 int HCI_LeSetEventMask(const HciLeSetEventMaskParam *param)
30 {
31     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_EVENT_MASK, (void *)param, sizeof(HciLeSetEventMaskParam));
32     return HciSendCmd(cmd);
33 }
34 
35 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
36 // 7.8.2 LE Read Buffer Size Command
HCI_LeReadBufferSize()37 int HCI_LeReadBufferSize()
38 {
39     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_BUFFER_SIZE, NULL, 0);
40     return HciSendCmd(cmd);
41 }
42 
43 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
44 // 7.8.3 LE Read Local Supported Features Command
HCI_LeReadLocalSupportedFeatures()45 int HCI_LeReadLocalSupportedFeatures()
46 {
47     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_LOCAL_SUPPORTED_FEATURES, NULL, 0);
48     return HciSendCmd(cmd);
49 }
50 
51 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
52 // 7.8.4 LE Set Random Address Command
HCI_LeSetRandomAddress(const HciLeSetRandomAddressParam * param)53 int HCI_LeSetRandomAddress(const HciLeSetRandomAddressParam *param)
54 {
55     if (param == NULL) {
56         return BT_BAD_PARAM;
57     }
58 
59     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_RANDOM_ADDRESS, (void *)param, sizeof(HciLeSetRandomAddressParam));
60     return HciSendCmd(cmd);
61 }
62 
63 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
64 // 7.8.5 LE Set Advertising Parameters Command
HCI_LeSetAdvertisingParameters(const HciLeSetAdvertisingParametersParam * param)65 int HCI_LeSetAdvertisingParameters(const HciLeSetAdvertisingParametersParam *param)
66 {
67     if (param == NULL) {
68         return BT_BAD_PARAM;
69     }
70 
71     HciCmd *cmd =
72         HciAllocCmd(HCI_LE_SET_ADVERTISING_PARAMETERS, (void *)param, sizeof(HciLeSetAdvertisingParametersParam));
73     return HciSendCmd(cmd);
74 }
75 
76 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
77 // 7.8.6 LE Read Advertising Channel Tx Power Command
HCI_LeReadAdvertisingChannelTxPower()78 int HCI_LeReadAdvertisingChannelTxPower()
79 {
80     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_ADVERTISING_CHANNEL_TX_POWER, NULL, 0);
81     return HciSendCmd(cmd);
82 }
83 
84 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
85 // 7.8.7 LE Set Advertising Data Command
HCI_LeSetAdvertisingData(const HciLeSetAdvertisingDataParam * param)86 int HCI_LeSetAdvertisingData(const HciLeSetAdvertisingDataParam *param)
87 {
88     if (param == NULL) {
89         return BT_BAD_PARAM;
90     }
91 
92     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_ADVERTISING_DATA, (void *)param, sizeof(HciLeSetAdvertisingDataParam));
93     return HciSendCmd(cmd);
94 }
95 
96 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
97 // 7.8.8 LE Set Scan Response Data Command
HCI_LeSetScanResponseData(const HciLeSetScanResponseDataParam * param)98 int HCI_LeSetScanResponseData(const HciLeSetScanResponseDataParam *param)
99 {
100     if (param == NULL) {
101         return BT_BAD_PARAM;
102     }
103 
104     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_SCAN_RESPONSE_DATA, (void *)param, sizeof(HciLeSetScanResponseDataParam));
105     return HciSendCmd(cmd);
106 }
107 
108 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
109 // 7.8.9 LE Set Advertising Enable Command
HCI_LeSetAdvertisingEnable(const HciLeSetAdvertisingEnableParam * param)110 int HCI_LeSetAdvertisingEnable(const HciLeSetAdvertisingEnableParam *param)
111 {
112     if (param == NULL) {
113         return BT_BAD_PARAM;
114     }
115 
116     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_ADVERTISING_ENABLE, (void *)param, sizeof(HciLeSetAdvertisingEnableParam));
117     return HciSendCmd(cmd);
118 }
119 
120 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
121 // 7.8.10 LE Set Scan Parameters Command
HCI_LeSetScanParameters(const HciLeSetScanParametersParam * param)122 int HCI_LeSetScanParameters(const HciLeSetScanParametersParam *param)
123 {
124     if (param == NULL) {
125         return BT_BAD_PARAM;
126     }
127 
128     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_SCAN_PARAMETERS, (void *)param, sizeof(HciLeSetScanParametersParam));
129     return HciSendCmd(cmd);
130 }
131 
132 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
133 // 7.8.11 LE Set Scan Enable Command
HCI_LeSetScanEnable(const HciLeSetScanEnableParam * param)134 int HCI_LeSetScanEnable(const HciLeSetScanEnableParam *param)
135 {
136     if (param == NULL) {
137         return BT_BAD_PARAM;
138     }
139 
140     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_SCAN_ENABLE, (void *)param, sizeof(HciLeSetScanEnableParam));
141     return HciSendCmd(cmd);
142 }
143 
144 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
145 // 7.8.12 LE Create Connection Command
HCI_LeCreateConnection(const HciLeCreateConnectionParam * param)146 int HCI_LeCreateConnection(const HciLeCreateConnectionParam *param)
147 {
148     if (param == NULL) {
149         return BT_BAD_PARAM;
150     }
151 
152     HciCmd *cmd = HciAllocCmd(HCI_LE_CREATE_CONNECTION, (void *)param, sizeof(HciLeCreateConnectionParam));
153     return HciSendCmd(cmd);
154 }
155 
156 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
157 // 7.8.13 LE Create Connection Cancel Command
HCI_LeCreateConnectionCancel()158 int HCI_LeCreateConnectionCancel()
159 {
160     HciCmd *cmd = HciAllocCmd(HCI_LE_CREATE_CONNECTION_CANCEL, NULL, 0);
161     return HciSendCmd(cmd);
162 }
163 
164 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
165 // 7.8.14 LE Read WL Size Command
HCI_LeReadWhiteListSize()166 int HCI_LeReadWhiteListSize()
167 {
168     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_WHITE_LIST_SIZE, NULL, 0);
169     return HciSendCmd(cmd);
170 }
171 
172 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
173 // 7.8.15 LE Clear WL Command
HCI_LeClearWhiteList()174 int HCI_LeClearWhiteList()
175 {
176     HciCmd *cmd = HciAllocCmd(HCI_LE_CLEAR_WHITE_LIST, NULL, 0);
177     return HciSendCmd(cmd);
178 }
179 
180 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
181 // 7.8.16 LE Add Device To WL Command
HCI_LeAddDeviceToWhiteList(const HciLeAddDeviceToWhiteListParam * param)182 int HCI_LeAddDeviceToWhiteList(const HciLeAddDeviceToWhiteListParam *param)
183 {
184     if (param == NULL) {
185         return BT_BAD_PARAM;
186     }
187 
188     HciCmd *cmd = HciAllocCmd(HCI_LE_ADD_DEVICE_TO_WHITE_LIST, (void *)param, sizeof(HciLeAddDeviceToWhiteListParam));
189     return HciSendCmd(cmd);
190 }
191 
192 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
193 // 7.8.17 LE Remove Device From WL Command
HCI_LeRemoveDeviceFromWhiteList(const HciLeRemoveDeviceFromWhiteListParam * param)194 int HCI_LeRemoveDeviceFromWhiteList(const HciLeRemoveDeviceFromWhiteListParam *param)
195 {
196     if (param == NULL) {
197         return BT_BAD_PARAM;
198     }
199 
200     HciCmd *cmd =
201         HciAllocCmd(HCI_LE_REMOVE_DEVICE_FROM_WHITE_LIST, (void *)param, sizeof(HciLeRemoveDeviceFromWhiteListParam));
202     return HciSendCmd(cmd);
203 }
204 
205 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
206 // 7.8.18 LE Connection Update Command
HCI_LeConnectionUpdate(const HciLeConnectionUpdateParam * param)207 int HCI_LeConnectionUpdate(const HciLeConnectionUpdateParam *param)
208 {
209     if (param == NULL) {
210         return BT_BAD_PARAM;
211     }
212 
213     HciCmd *cmd = HciAllocCmd(HCI_LE_CONNECTION_UPDATE, (void *)param, sizeof(HciLeConnectionUpdateParam));
214     return HciSendCmd(cmd);
215 }
216 
217 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
218 // 7.8.19 LE Set Host Channel Classification Command
HCI_LeSetHostChannelClassification(const HciLeSetHostChannelClassificationParam * param)219 int HCI_LeSetHostChannelClassification(const HciLeSetHostChannelClassificationParam *param)
220 {
221     if (param == NULL) {
222         return BT_BAD_PARAM;
223     }
224 
225     HciCmd *cmd = HciAllocCmd(
226         HCI_LE_SET_HOST_CHANNEL_CLASSIFICATION, (void *)param, sizeof(HciLeSetHostChannelClassificationParam));
227     return HciSendCmd(cmd);
228 }
229 
230 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
231 // 7.8.20 LE Read Channel Map Command
HCI_LeReadChannelMap(const HciLeReadChannelMapParam * param)232 int HCI_LeReadChannelMap(const HciLeReadChannelMapParam *param)
233 {
234     if (param == NULL) {
235         return BT_BAD_PARAM;
236     }
237 
238     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_CHANNEL_MAP, (void *)param, sizeof(HciLeReadChannelMapParam));
239     return HciSendCmd(cmd);
240 }
241 
242 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
243 // 7.8.21 LE Read Remote Features Command
HCI_LeReadRemoteFeatures(const HciLeReadRemoteFeaturesParam * param)244 int HCI_LeReadRemoteFeatures(const HciLeReadRemoteFeaturesParam *param)
245 {
246     if (param == NULL) {
247         return BT_BAD_PARAM;
248     }
249 
250     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_REMOTE_FEATURES, (void *)param, sizeof(HciLeReadRemoteFeaturesParam));
251     return HciSendCmd(cmd);
252 }
253 
254 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
255 // 7.8.22 LE Encrypt Command
HCI_LeEncrypt(const HciLeEncryptParam * param)256 int HCI_LeEncrypt(const HciLeEncryptParam *param)
257 {
258     if (param == NULL) {
259         return BT_BAD_PARAM;
260     }
261 
262     HciCmd *cmd = HciAllocCmd(HCI_LE_ENCRYPT, (void *)param, sizeof(HciLeEncryptParam));
263     return HciSendCmd(cmd);
264 }
265 
266 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
267 // 7.8.23 LE Rand Command
HCI_LeRand()268 int HCI_LeRand()
269 {
270     HciCmd *cmd = HciAllocCmd(HCI_LE_RAND, NULL, 0);
271     return HciSendCmd(cmd);
272 }
273 
274 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
275 // 7.8.24 LE Start Encryption Command
HCI_LeStartEncryption(const HciLeStartEncryptionParam * param)276 int HCI_LeStartEncryption(const HciLeStartEncryptionParam *param)
277 {
278     if (param == NULL) {
279         return BT_BAD_PARAM;
280     }
281 
282     HciCmd *cmd = HciAllocCmd(HCI_LE_START_ENCRYPTION, (void *)param, sizeof(HciLeStartEncryptionParam));
283     return HciSendCmd(cmd);
284 }
285 
286 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
287 // 7.8.25 LE Long Term Key Request Reply Command
HCI_LeLongTermKeyRequestReply(const HciLeLongTermKeyRequestReplyParam * param)288 int HCI_LeLongTermKeyRequestReply(const HciLeLongTermKeyRequestReplyParam *param)
289 {
290     if (param == NULL) {
291         return BT_BAD_PARAM;
292     }
293 
294     HciCmd *cmd =
295         HciAllocCmd(HCI_LE_LONG_TERM_KEY_REQUEST_REPLY, (void *)param, sizeof(HciLeLongTermKeyRequestReplyParam));
296     return HciSendCmd(cmd);
297 }
298 
299 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
300 // 7.8.26 LE Long Term Key Request Negative Reply Command
HCI_LeLongTermKeyRequestNegativeReply(const HciLeLongTermKeyRequestNegativeReplyParam * param)301 int HCI_LeLongTermKeyRequestNegativeReply(const HciLeLongTermKeyRequestNegativeReplyParam *param)
302 {
303     if (param == NULL) {
304         return BT_BAD_PARAM;
305     }
306 
307     HciCmd *cmd = HciAllocCmd(
308         HCI_LE_LONG_TERM_KEY_REQUEST_NEGATIVE_REPLY, (void *)param, sizeof(HciLeLongTermKeyRequestNegativeReplyParam));
309     return HciSendCmd(cmd);
310 }
311 
312 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
313 // 7.8.31 LE Remote Connection Parameter Request Reply Command
HCI_LeRemoteConnectionParameterRequestReply(const HciLeRemoteConnectionParameterRequestReplyParam * param)314 int HCI_LeRemoteConnectionParameterRequestReply(const HciLeRemoteConnectionParameterRequestReplyParam *param)
315 {
316     if (param == NULL) {
317         return BT_BAD_PARAM;
318     }
319 
320     HciCmd *cmd = HciAllocCmd(HCI_LE_REMOTE_CONNECTION_PARAMETER_REQUEST_REPLY,
321         (void *)param,
322         sizeof(HciLeRemoteConnectionParameterRequestReplyParam));
323     return HciSendCmd(cmd);
324 }
325 
326 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
327 // 7.8.32 LE Remote Connection Parameter Request Negative Reply Command
HCI_LeRemoteConnectionParameterRequestNegativeReply(const HciLeRemoteConnectionParameterRequestNegativeReplyParam * param)328 int HCI_LeRemoteConnectionParameterRequestNegativeReply(
329     const HciLeRemoteConnectionParameterRequestNegativeReplyParam *param)
330 {
331     if (param == NULL) {
332         return BT_BAD_PARAM;
333     }
334 
335     HciCmd *cmd = HciAllocCmd(HCI_LE_REMOTE_CONNECTION_PARAMETER_REQUEST_NEGATIVE_REPLY,
336         (void *)param,
337         sizeof(HciLeRemoteConnectionParameterRequestNegativeReplyParam));
338     return HciSendCmd(cmd);
339 }
340 
341 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
342 // 7.8.36 LE Read Local P-256 Public Key Command
HCI_LeReadLocalP256PublicKey()343 int HCI_LeReadLocalP256PublicKey()
344 {
345     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_LOCAL_P256_PUBLIC_KEY, NULL, 0);
346     return HciSendCmd(cmd);
347 }
348 
349 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
350 // 7.8.37 LE Generate DHKey Command
HCI_LeGenerateDHKey(const HciLeGenerateDHKeyParam * param)351 int HCI_LeGenerateDHKey(const HciLeGenerateDHKeyParam *param)
352 {
353     if (param == NULL) {
354         return BT_BAD_PARAM;
355     }
356 
357     HciCmd *cmd = HciAllocCmd(HCI_LE_GENERATE_DHKEY, (void *)param, sizeof(HciLeGenerateDHKeyParam));
358     return HciSendCmd(cmd);
359 }
360 
361 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
362 // 7.8.38 LE Add Device To Resolving List Command
HCI_LeAddDeviceToResolvingList(const HciLeAddDeviceToResolvingListParam * param)363 int HCI_LeAddDeviceToResolvingList(const HciLeAddDeviceToResolvingListParam *param)
364 {
365     if (param == NULL) {
366         return BT_BAD_PARAM;
367     }
368 
369     HciCmd *cmd =
370         HciAllocCmd(HCI_LE_ADD_DEVICE_TO_RESOLVING_LIST, (void *)param, sizeof(HciLeAddDeviceToResolvingListParam));
371     return HciSendCmd(cmd);
372 }
373 
374 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
375 // 7.8.39 LE Remove Device From Resolving List Command
HCI_LeRemoveDeviceFromResolvingList(const HciLeRemoveDeviceFromResolvingListParam * param)376 int HCI_LeRemoveDeviceFromResolvingList(const HciLeRemoveDeviceFromResolvingListParam *param)
377 {
378     if (param == NULL) {
379         return BT_BAD_PARAM;
380     }
381 
382     HciCmd *cmd = HciAllocCmd(
383         HCI_LE_REMOVE_DEVICE_FROM_RESOLVING_LIST, (void *)param, sizeof(HciLeRemoveDeviceFromResolvingListParam));
384     return HciSendCmd(cmd);
385 }
386 
387 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
388 // 7.8.40 LE Clear Resolving List Command
HCI_LeClearResolvingList()389 int HCI_LeClearResolvingList()
390 {
391     HciCmd *cmd = HciAllocCmd(HCI_LE_CLEAR_RESOLVING_LIST, NULL, 0);
392     return HciSendCmd(cmd);
393 }
394 
395 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
396 // 7.8.41 LE Read Resolving List Size Command
HCI_LeReadResolvingListSize()397 int HCI_LeReadResolvingListSize()
398 {
399     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_RESOLVING_LIST_SIZE, NULL, 0);
400     return HciSendCmd(cmd);
401 }
402 
403 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
404 // 7.8.44 LE Set Address Resolution Enable Command
HCI_LeSetAddressResolutionEnable(const HciLeSetAddressResolutionEnableParam * param)405 int HCI_LeSetAddressResolutionEnable(const HciLeSetAddressResolutionEnableParam *param)
406 {
407     if (param == NULL) {
408         return BT_BAD_PARAM;
409     }
410 
411     HciCmd *cmd =
412         HciAllocCmd(HCI_LE_SET_ADDRESS_RESOLUTION_ENABLE, (void *)param, sizeof(HciLeSetAddressResolutionEnableParam));
413     return HciSendCmd(cmd);
414 }
415 
416 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
417 // 7.8.52 LE Set Advertising Set Random Address Command
HCI_LeSetAdvertisingSetRandomAddress(const HciLeSetAdvertisingSetRandomAddressParam * param)418 int HCI_LeSetAdvertisingSetRandomAddress(const HciLeSetAdvertisingSetRandomAddressParam *param)
419 {
420     if (param == NULL) {
421         return BT_BAD_PARAM;
422     }
423 
424     HciCmd *cmd = HciAllocCmd(
425         HCI_LE_SET_ADVERTISING_SET_RANDOM_ADDRESS, (void *)param, sizeof(HciLeSetAdvertisingSetRandomAddressParam));
426     return HciSendCmd(cmd);
427 }
428 
429 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
430 // 7.8.53 LE Set Extended Advertising Parameters Command
HCI_LeSetExtendedAdvertisingParameters(const HciLeSetExtendedAdvertisingParametersParam * param)431 int HCI_LeSetExtendedAdvertisingParameters(const HciLeSetExtendedAdvertisingParametersParam *param)
432 {
433     if (param == NULL) {
434         return BT_BAD_PARAM;
435     }
436 
437     HciCmd *cmd = HciAllocCmd(
438         HCI_LE_SET_EXTENDED_ADVERTISING_PARAMETERS, (void *)param, sizeof(HciLeSetExtendedAdvertisingParametersParam));
439     return HciSendCmd(cmd);
440 }
441 
442 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
443 // 7.8.54 LE Set Extended Advertising Data Command
HCI_LeSetExtendedAdvertisingData(const HciLeSetExtendedAdvertisingDataParam * param)444 int HCI_LeSetExtendedAdvertisingData(const HciLeSetExtendedAdvertisingDataParam *param)
445 {
446     if (param == NULL) {
447         return BT_BAD_PARAM;
448     }
449 
450     const size_t length =
451         sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + param->advertisingDataLength;
452     uint8_t *buf = MEM_MALLOC.alloc(length);
453     if (buf == NULL) {
454         return BT_NO_MEMORY;
455     }
456 
457     uint16_t index = 0;
458     buf[index] = param->advertisingHandle;
459     index += sizeof(uint8_t);
460     buf[index] = param->operation;
461     index += sizeof(uint8_t);
462     buf[index] = param->fragmentPreference;
463     index += sizeof(uint8_t);
464     buf[index] = param->advertisingDataLength;
465     index += sizeof(uint8_t);
466 
467     if (param->advertisingDataLength > 0) {
468         (void)memcpy_s(buf + index, length - index, param->advertisingData, param->advertisingDataLength);
469     }
470 
471     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_EXTENDED_ADVERTISING_DATA, (void *)buf, length);
472 
473     int result = HciSendCmd(cmd);
474     MEM_MALLOC.free(buf);
475     return result;
476 }
477 
478 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
479 // 7.8.55 LE Set Extended Scan Response Data Command
HCI_LeSetExtendedScanResponseData(const HciLeSetExtendedScanResponseDataParam * param)480 int HCI_LeSetExtendedScanResponseData(const HciLeSetExtendedScanResponseDataParam *param)
481 {
482     if (param == NULL) {
483         return BT_BAD_PARAM;
484     }
485 
486     const size_t length =
487         sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + param->scanResponseDataLength;
488     uint8_t *buf = MEM_MALLOC.alloc(length);
489     if (buf == NULL) {
490         return BT_NO_MEMORY;
491     }
492 
493     uint16_t index = 0;
494     buf[index] = param->advertisingHandle;
495     index += sizeof(uint8_t);
496     buf[index] = param->operation;
497     index += sizeof(uint8_t);
498     buf[index] = param->fragmentPreference;
499     index += sizeof(uint8_t);
500     buf[index] = param->scanResponseDataLength;
501     index += sizeof(uint8_t);
502     if (param->scanResponseDataLength > 0) {
503         (void)memcpy_s(buf + index, length - index, param->scanResponseData, param->scanResponseDataLength);
504     }
505 
506     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_EXTENDED_SCAN_RESPONSE_DATA, (void *)buf, length);
507     int result = HciSendCmd(cmd);
508 
509     MEM_MALLOC.free(buf);
510     return result;
511 }
512 
513 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
514 // 7.8.56 LE Set Extended Advertising Enable Command
HCI_LeSetExtendedAdvertisingEnable(const HciLeSetExtendedAdvertisingEnableParam * param)515 int HCI_LeSetExtendedAdvertisingEnable(const HciLeSetExtendedAdvertisingEnableParam *param)
516 {
517     if (param == NULL) {
518         return BT_BAD_PARAM;
519     }
520 
521     const size_t length =
522         sizeof(uint8_t) + sizeof(uint8_t) + sizeof(HciLeExtendedAdvertisingParamSet) * param->numberofSets;
523     uint8_t *buf = MEM_MALLOC.alloc(length);
524     if (buf == NULL) {
525         return BT_NO_MEMORY;
526     }
527 
528     uint16_t index = 0;
529     buf[index] = param->enable;
530     index += sizeof(uint8_t);
531     buf[index] = param->numberofSets;
532     index += sizeof(uint8_t);
533     for (uint8_t i = 0; i < param->numberofSets; i++) {
534         (void)memcpy_s(buf + index, length - index, param->sets + i, sizeof(HciLeExtendedAdvertisingParamSet));
535         index += sizeof(HciLeExtendedAdvertisingParamSet);
536     }
537 
538     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_EXTENDED_ADVERTISING_ENABLE, (void *)buf, length);
539 
540     int result = HciSendCmd(cmd);
541 
542     MEM_MALLOC.free(buf);
543 
544     return result;
545 }
546 
547 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
548 // 7.8.57 LE Read Maximum Advertising Data Length Command
HCI_LeReadMaximumAdvertisingDataLength()549 int HCI_LeReadMaximumAdvertisingDataLength()
550 {
551     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH, NULL, 0);
552     return HciSendCmd(cmd);
553 }
554 
555 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
556 // 7.8.58 LE Read Number of Supported Advertising Sets Command
HCI_LeReadNumberofSupportedAdvertisingSets()557 int HCI_LeReadNumberofSupportedAdvertisingSets()
558 {
559     HciCmd *cmd = HciAllocCmd(HCI_LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS, NULL, 0);
560     return HciSendCmd(cmd);
561 }
562 
563 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
564 // 7.8.59 LE Remove Advertising Set Command
HCI_LeRemoveAdvertisingSet(const HciLeRemoveAdvertisingSetParam * param)565 int HCI_LeRemoveAdvertisingSet(const HciLeRemoveAdvertisingSetParam *param)
566 {
567     if (param == NULL) {
568         return BT_BAD_PARAM;
569     }
570 
571     HciCmd *cmd = HciAllocCmd(HCI_LE_REMOVE_ADVERTISING_SET, (void *)param, sizeof(HciLeRemoveAdvertisingSetParam));
572     return HciSendCmd(cmd);
573 }
574 
575 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
576 // 7.8.60 LE Clear Advertising Sets Command
HCI_LeClearAdvertisingSets()577 int HCI_LeClearAdvertisingSets()
578 {
579     HciCmd *cmd = HciAllocCmd(HCI_LE_CLEAR_ADVERTISING_SETS, NULL, 0);
580     return HciSendCmd(cmd);
581 }
582 
583 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
584 // 7.8.64 LE Set Extended Scan Parameters Command
HCI_LeSetExtendedScanParameters(const HciLeSetExtendedScanParametersParam * param)585 int HCI_LeSetExtendedScanParameters(const HciLeSetExtendedScanParametersParam *param)
586 {
587     if (param == NULL) {
588         return BT_BAD_PARAM;
589     }
590 
591     uint8_t numberOfSets = 0;
592     for (uint8_t i = 0; i < BITS_IN_BYTE; i++) {
593         if ((param->ScanningPHYs >> i) & 0x01) {
594             numberOfSets++;
595         }
596     }
597     const size_t length =
598         sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + sizeof(HciLeExtendedScanParametersSet) * numberOfSets;
599     uint8_t *buf = MEM_MALLOC.alloc(length);
600     if (buf == NULL) {
601         return BT_NO_MEMORY;
602     }
603 
604     uint16_t index = 0;
605     buf[index] = param->ownAddressType;
606     index += sizeof(uint8_t);
607     buf[index] = param->ScanningFilterPolicy;
608     index += sizeof(uint8_t);
609     buf[index] = param->ScanningPHYs;
610     index += sizeof(uint8_t);
611 
612     for (uint8_t i = 0; i < numberOfSets; i++) {
613         (void)memcpy_s(buf + index, length - index, param->sets + i, sizeof(HciLeExtendedScanParametersSet));
614         index += sizeof(HciLeExtendedScanParametersSet);
615     }
616 
617     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_EXTENDED_SCAN_PARAMETERS, (void *)buf, length);
618     int result = HciSendCmd(cmd);
619 
620     MEM_MALLOC.free(buf);
621 
622     return result;
623 }
624 
625 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
626 // 7.8.65 LE Set Extended Scan Enable Command
HCI_LeSetExtendedScanEnable(const HciLeSetExtendedScanEnableParam * param)627 int HCI_LeSetExtendedScanEnable(const HciLeSetExtendedScanEnableParam *param)
628 {
629     if (param == NULL) {
630         return BT_BAD_PARAM;
631     }
632 
633     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_EXTENDED_SCAN_ENABLE, (void *)param, sizeof(HciLeSetExtendedScanEnableParam));
634     return HciSendCmd(cmd);
635 }
636 
637 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
638 // 7.8.66 LE Extended Create Connection Command
HCI_LeExtenedCreateConnection(const HciLeExtendedCreateConnectionParam * param)639 int HCI_LeExtenedCreateConnection(const HciLeExtendedCreateConnectionParam *param)
640 {
641     if (param == NULL) {
642         return BT_BAD_PARAM;
643     }
644 
645     uint8_t countOfSets = 0;
646     if (param->initiatingPhys & LE_1M_PHY) {
647         countOfSets++;
648     }
649     if (param->initiatingPhys & LE_2M_PHY) {
650         countOfSets++;
651     }
652     if (param->initiatingPhys & LE_CODED_PHY) {
653         countOfSets++;
654     }
655 
656     const size_t length = sizeof(uint8_t) + sizeof(uint8_t) + sizeof(uint8_t) + sizeof(HciBdAddr) + sizeof(uint8_t) +
657                           sizeof(HciLeConnectionParamSet) * countOfSets;
658     uint8_t *buf = MEM_MALLOC.alloc(length);
659     if (buf == NULL) {
660         return BT_NO_MEMORY;
661     }
662 
663     uint16_t index = 0;
664 
665     buf[index] = param->initiatingFilterPolicy;
666     index += sizeof(uint8_t);
667 
668     buf[index] = param->ownAddressType;
669     index += sizeof(uint8_t);
670 
671     buf[index] = param->peerAddressType;
672     index += sizeof(uint8_t);
673 
674     (void)memcpy_s(buf + index, length - index, param->peerAddress.raw, sizeof(HciBdAddr));
675     index += sizeof(HciBdAddr);
676 
677     buf[index] = param->initiatingPhys;
678     index += sizeof(uint8_t);
679 
680     for (uint8_t i = 0; i < countOfSets; i++) {
681         (void)memcpy_s(buf + index, length - index, param->sets + i, sizeof(HciLeConnectionParamSet));
682         index += sizeof(HciLeConnectionParamSet);
683     }
684 
685     HciCmd *cmd = HciAllocCmd(HCI_LE_EXTENDED_CREATE_CONNECTION, (void *)buf, length);
686     int result = HciSendCmd(cmd);
687 
688     MEM_MALLOC.free(buf);
689 
690     return result;
691 }
692 
693 // BLUETOOTH SPECIFICATION Version 5.0 | Vol 2, Part E
694 // 7.8.77 LE Set Privacy Mode Command
HCI_LeSetPrivacyMode(const HciLeSetPrivacyModeParam * param)695 int HCI_LeSetPrivacyMode(const HciLeSetPrivacyModeParam *param)
696 {
697     HciCmd *cmd = HciAllocCmd(HCI_LE_SET_PRIVACY_MODE, (void *)param, sizeof(HciLeSetPrivacyModeParam));
698     return HciSendCmd(cmd);
699 }
700