1 // SPDX-License-Identifier: GPL-2.0
3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2021 Intel Corporation
8 #include <net/bluetooth/bluetooth.h>
9 #include <net/bluetooth/hci_core.h>
10 #include <net/bluetooth/mgmt.h>
14 #define PNP_INFO_SVCLASS_ID 0x1200
16 static u8 eir_append_name(u8 *eir, u16 eir_len, u8 type, u8 *data, u8 data_len)
18 u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
20 /* If data is already NULL terminated just pass it directly */
21 if (data[data_len - 1] == '\0')
22 return eir_append_data(eir, eir_len, type, data, data_len);
24 memcpy(name, data, HCI_MAX_SHORT_NAME_LENGTH);
25 name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
27 return eir_append_data(eir, eir_len, type, name, sizeof(name));
30 u8 eir_append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
35 /* no space left for name (+ NULL + type + len) */
36 if ((HCI_MAX_AD_LENGTH - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 3)
39 /* use complete name if present and fits */
40 complete_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
41 if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
42 return eir_append_name(ptr, ad_len, EIR_NAME_COMPLETE,
43 hdev->dev_name, complete_len + 1);
45 /* use short name if present */
46 short_len = strnlen(hdev->short_name, sizeof(hdev->short_name));
48 return eir_append_name(ptr, ad_len, EIR_NAME_SHORT,
50 short_len == HCI_MAX_SHORT_NAME_LENGTH ?
51 short_len : short_len + 1);
53 /* use shortened full name if present, we already know that name
54 * is longer then HCI_MAX_SHORT_NAME_LENGTH
57 return eir_append_name(ptr, ad_len, EIR_NAME_SHORT,
59 HCI_MAX_SHORT_NAME_LENGTH);
64 u8 eir_append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
66 return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
69 u8 eir_append_service_data(u8 *eir, u16 eir_len, u16 uuid, u8 *data,
72 eir[eir_len++] = sizeof(u8) + sizeof(uuid) + data_len;
73 eir[eir_len++] = EIR_SERVICE_DATA;
74 put_unaligned_le16(uuid, &eir[eir_len]);
75 eir_len += sizeof(uuid);
76 memcpy(&eir[eir_len], data, data_len);
82 static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
84 u8 *ptr = data, *uuids_start = NULL;
90 list_for_each_entry(uuid, &hdev->uuids, list) {
96 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
100 if (uuid16 == PNP_INFO_SVCLASS_ID)
106 uuids_start[1] = EIR_UUID16_ALL;
110 /* Stop if not enough space to put next UUID */
111 if ((ptr - data) + sizeof(u16) > len) {
112 uuids_start[1] = EIR_UUID16_SOME;
116 *ptr++ = (uuid16 & 0x00ff);
117 *ptr++ = (uuid16 & 0xff00) >> 8;
118 uuids_start[0] += sizeof(uuid16);
124 static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
126 u8 *ptr = data, *uuids_start = NULL;
127 struct bt_uuid *uuid;
132 list_for_each_entry(uuid, &hdev->uuids, list) {
133 if (uuid->size != 32)
139 uuids_start[1] = EIR_UUID32_ALL;
143 /* Stop if not enough space to put next UUID */
144 if ((ptr - data) + sizeof(u32) > len) {
145 uuids_start[1] = EIR_UUID32_SOME;
149 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
151 uuids_start[0] += sizeof(u32);
157 static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
159 u8 *ptr = data, *uuids_start = NULL;
160 struct bt_uuid *uuid;
165 list_for_each_entry(uuid, &hdev->uuids, list) {
166 if (uuid->size != 128)
172 uuids_start[1] = EIR_UUID128_ALL;
176 /* Stop if not enough space to put next UUID */
177 if ((ptr - data) + 16 > len) {
178 uuids_start[1] = EIR_UUID128_SOME;
182 memcpy(ptr, uuid->uuid, 16);
184 uuids_start[0] += 16;
190 void eir_create(struct hci_dev *hdev, u8 *data)
195 name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
201 ptr[1] = EIR_NAME_SHORT;
203 ptr[1] = EIR_NAME_COMPLETE;
206 /* EIR Data length */
207 ptr[0] = name_len + 1;
209 memcpy(ptr + 2, hdev->dev_name, name_len);
211 ptr += (name_len + 2);
214 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
216 ptr[1] = EIR_TX_POWER;
217 ptr[2] = (u8)hdev->inq_tx_power;
222 if (hdev->devid_source > 0) {
224 ptr[1] = EIR_DEVICE_ID;
226 put_unaligned_le16(hdev->devid_source, ptr + 2);
227 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
228 put_unaligned_le16(hdev->devid_product, ptr + 6);
229 put_unaligned_le16(hdev->devid_version, ptr + 8);
234 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
235 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
236 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
239 u8 eir_create_per_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
241 struct adv_info *adv = NULL;
244 /* Return 0 when the current instance identifier is invalid. */
246 adv = hci_find_adv_instance(hdev, instance);
252 memcpy(ptr, adv->per_adv_data, adv->per_adv_data_len);
253 ad_len += adv->per_adv_data_len;
254 ptr += adv->per_adv_data_len;
260 u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
262 struct adv_info *adv = NULL;
263 u8 ad_len = 0, flags = 0;
266 /* Return 0 when the current instance identifier is invalid. */
268 adv = hci_find_adv_instance(hdev, instance);
273 instance_flags = hci_adv_instance_flags(hdev, instance);
275 /* If instance already has the flags set skip adding it once
278 if (adv && eir_get_data(adv->adv_data, adv->adv_data_len, EIR_FLAGS,
282 /* The Add Advertising command allows userspace to set both the general
283 * and limited discoverable flags.
285 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
286 flags |= LE_AD_GENERAL;
288 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
289 flags |= LE_AD_LIMITED;
291 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
292 flags |= LE_AD_NO_BREDR;
294 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
295 /* If a discovery flag wasn't provided, simply use the global
299 flags |= mgmt_get_adv_discov_flags(hdev);
301 /* If flags would still be empty, then there is no need to
302 * include the "Flags" AD field".
316 memcpy(ptr, adv->adv_data, adv->adv_data_len);
317 ad_len += adv->adv_data_len;
318 ptr += adv->adv_data_len;
321 if (instance_flags & MGMT_ADV_FLAG_TX_POWER) {
324 if (ext_adv_capable(hdev)) {
326 adv_tx_power = adv->tx_power;
328 adv_tx_power = hdev->adv_tx_power;
330 adv_tx_power = hdev->adv_tx_power;
333 /* Provide Tx Power only if we can provide a valid value for it */
334 if (adv_tx_power != HCI_TX_POWER_INVALID) {
336 ptr[1] = EIR_TX_POWER;
337 ptr[2] = (u8)adv_tx_power;
347 static u8 create_default_scan_rsp(struct hci_dev *hdev, u8 *ptr)
351 if (hdev->appearance)
352 scan_rsp_len = eir_append_appearance(hdev, ptr, scan_rsp_len);
354 return eir_append_local_name(hdev, ptr, scan_rsp_len);
357 u8 eir_create_scan_rsp(struct hci_dev *hdev, u8 instance, u8 *ptr)
359 struct adv_info *adv;
363 return create_default_scan_rsp(hdev, ptr);
365 adv = hci_find_adv_instance(hdev, instance);
369 if ((adv->flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance)
370 scan_rsp_len = eir_append_appearance(hdev, ptr, scan_rsp_len);
372 memcpy(&ptr[scan_rsp_len], adv->scan_rsp_data, adv->scan_rsp_len);
374 scan_rsp_len += adv->scan_rsp_len;
376 if (adv->flags & MGMT_ADV_FLAG_LOCAL_NAME)
377 scan_rsp_len = eir_append_local_name(hdev, ptr, scan_rsp_len);
382 void *eir_get_service_data(u8 *eir, size_t eir_len, u16 uuid, size_t *len)
384 while ((eir = eir_get_data(eir, eir_len, EIR_SERVICE_DATA, len))) {
385 u16 value = get_unaligned_le16(eir);