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 u8 eir_append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
21 /* no space left for name (+ NULL + type + len) */
22 if ((HCI_MAX_AD_LENGTH - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 3)
25 /* use complete name if present and fits */
26 complete_len = strlen(hdev->dev_name);
27 if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
28 return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
29 hdev->dev_name, complete_len + 1);
31 /* use short name if present */
32 short_len = strlen(hdev->short_name);
34 return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
35 hdev->short_name, short_len + 1);
37 /* use shortened full name if present, we already know that name
38 * is longer then HCI_MAX_SHORT_NAME_LENGTH
41 u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
43 memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH);
44 name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
46 return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name,
53 u8 eir_append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
55 return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
58 static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
60 u8 *ptr = data, *uuids_start = NULL;
66 list_for_each_entry(uuid, &hdev->uuids, list) {
72 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
76 if (uuid16 == PNP_INFO_SVCLASS_ID)
82 uuids_start[1] = EIR_UUID16_ALL;
86 /* Stop if not enough space to put next UUID */
87 if ((ptr - data) + sizeof(u16) > len) {
88 uuids_start[1] = EIR_UUID16_SOME;
92 *ptr++ = (uuid16 & 0x00ff);
93 *ptr++ = (uuid16 & 0xff00) >> 8;
94 uuids_start[0] += sizeof(uuid16);
100 static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
102 u8 *ptr = data, *uuids_start = NULL;
103 struct bt_uuid *uuid;
108 list_for_each_entry(uuid, &hdev->uuids, list) {
109 if (uuid->size != 32)
115 uuids_start[1] = EIR_UUID32_ALL;
119 /* Stop if not enough space to put next UUID */
120 if ((ptr - data) + sizeof(u32) > len) {
121 uuids_start[1] = EIR_UUID32_SOME;
125 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
127 uuids_start[0] += sizeof(u32);
133 static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
135 u8 *ptr = data, *uuids_start = NULL;
136 struct bt_uuid *uuid;
141 list_for_each_entry(uuid, &hdev->uuids, list) {
142 if (uuid->size != 128)
148 uuids_start[1] = EIR_UUID128_ALL;
152 /* Stop if not enough space to put next UUID */
153 if ((ptr - data) + 16 > len) {
154 uuids_start[1] = EIR_UUID128_SOME;
158 memcpy(ptr, uuid->uuid, 16);
160 uuids_start[0] += 16;
166 void eir_create(struct hci_dev *hdev, u8 *data)
171 name_len = strlen(hdev->dev_name);
177 ptr[1] = EIR_NAME_SHORT;
179 ptr[1] = EIR_NAME_COMPLETE;
182 /* EIR Data length */
183 ptr[0] = name_len + 1;
185 memcpy(ptr + 2, hdev->dev_name, name_len);
187 ptr += (name_len + 2);
190 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
192 ptr[1] = EIR_TX_POWER;
193 ptr[2] = (u8)hdev->inq_tx_power;
198 if (hdev->devid_source > 0) {
200 ptr[1] = EIR_DEVICE_ID;
202 put_unaligned_le16(hdev->devid_source, ptr + 2);
203 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
204 put_unaligned_le16(hdev->devid_product, ptr + 6);
205 put_unaligned_le16(hdev->devid_version, ptr + 8);
210 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
211 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
212 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
215 u8 eir_create_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
217 struct adv_info *adv = NULL;
218 u8 ad_len = 0, flags = 0;
221 /* Return 0 when the current instance identifier is invalid. */
223 adv = hci_find_adv_instance(hdev, instance);
228 instance_flags = hci_adv_instance_flags(hdev, instance);
230 /* If instance already has the flags set skip adding it once
233 if (adv && eir_get_data(adv->adv_data, adv->adv_data_len, EIR_FLAGS,
237 /* The Add Advertising command allows userspace to set both the general
238 * and limited discoverable flags.
240 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
241 flags |= LE_AD_GENERAL;
243 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
244 flags |= LE_AD_LIMITED;
246 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
247 flags |= LE_AD_NO_BREDR;
249 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
250 /* If a discovery flag wasn't provided, simply use the global
254 flags |= mgmt_get_adv_discov_flags(hdev);
256 /* If flags would still be empty, then there is no need to
257 * include the "Flags" AD field".
271 memcpy(ptr, adv->adv_data, adv->adv_data_len);
272 ad_len += adv->adv_data_len;
273 ptr += adv->adv_data_len;
276 if (instance_flags & MGMT_ADV_FLAG_TX_POWER) {
279 if (ext_adv_capable(hdev)) {
281 adv_tx_power = adv->tx_power;
283 adv_tx_power = hdev->adv_tx_power;
285 adv_tx_power = hdev->adv_tx_power;
288 /* Provide Tx Power only if we can provide a valid value for it */
289 if (adv_tx_power != HCI_TX_POWER_INVALID) {
291 ptr[1] = EIR_TX_POWER;
292 ptr[2] = (u8)adv_tx_power;
302 static u8 create_default_scan_rsp(struct hci_dev *hdev, u8 *ptr)
306 if (hdev->appearance)
307 scan_rsp_len = eir_append_appearance(hdev, ptr, scan_rsp_len);
309 return eir_append_local_name(hdev, ptr, scan_rsp_len);
312 u8 eir_create_scan_rsp(struct hci_dev *hdev, u8 instance, u8 *ptr)
314 struct adv_info *adv;
318 return create_default_scan_rsp(hdev, ptr);
320 adv = hci_find_adv_instance(hdev, instance);
324 if ((adv->flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance)
325 scan_rsp_len = eir_append_appearance(hdev, ptr, scan_rsp_len);
327 memcpy(&ptr[scan_rsp_len], adv->scan_rsp_data, adv->scan_rsp_len);
329 scan_rsp_len += adv->scan_rsp_len;
331 if (adv->flags & MGMT_ADV_FLAG_LOCAL_NAME)
332 scan_rsp_len = eir_append_local_name(hdev, ptr, scan_rsp_len);