2017-08-01 07:37:29

by Yunhan Wang

[permalink] [raw]
Subject: how to add local name in scan response when setting MGMT_ADV_FLAG_LOCAL_NAME in Bluez

Hi

I am trying to do some small experiments that add local name in the
scan response, should I use data[0] to store both adv_data and
scan_rsp?

I tried to add local name(=E2=80=99TEST=E2=80=99) in data[0] under
mgmt_cp_add_advertising on
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/src/advertising.c#n=
484,
it seems it does not work, any idea?

Thanks
Best wishes
Yunhan

static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
{
struct mgmt_cp_add_advertising *cp;
uint8_t param_len;
uint8_t *adv_data;
size_t adv_data_len;
uint32_t flags =3D 0;

uint8_t scan_rsp[] =3D {
=E2=80=99T=E2=80=99,
=E2=80=99E=E2=80=99,
=E2=80=99S=E2=80=99,
=E2=80=99T=E2=80=99
};
size_t scan_rsp_len =3D sizeof(scan_rsp);

DBG("Refreshing advertisement: %s", client->path);

if (client->type =3D=3D AD_TYPE_PERIPHERAL)
flags =3D MGMT_ADV_FLAG_CONNECTABLE | MGMT_ADV_FLAG_DISCOV |
MGMT_ADV_FLAG_LOCAL_NAME;

if (client->include_tx_power)
flags |=3D MGMT_ADV_FLAG_TX_POWER;

adv_data =3D bt_ad_generate(client->data, &adv_data_len);

if (!adv_data || (adv_data_len > calc_max_adv_len(client, flags))) {
error("Advertising data too long or couldn't be generated.");

return g_dbus_create_error(client->reg, ERROR_INTERFACE
".InvalidLength",
"Advertising data too long.");
}

param_len =3D sizeof(struct mgmt_cp_add_advertising) + adv_data_len +
scan_rsp_len;

cp =3D malloc0(param_len);

if (!cp) {
error("Couldn't allocate for MGMT!");

free(adv_data);

return btd_error_failed(client->reg, "Failed");
}

cp->flags =3D htobl(flags);
cp->instance =3D client->instance;
cp->adv_data_len =3D adv_data_len;
cp->scan_rsp_len =3D scan_rsp_len;
memcpy(cp->data, adv_data, adv_data_len);
memcpy(cp->data + adv_data_len, scan_rsp_data, scan_rsp_len);
free(adv_data);

if (!mgmt_send(client->manager->mgmt, MGMT_OP_ADD_ADVERTISING,
client->manager->mgmt_index, param_len, cp,
add_adv_callback, client, NULL)) {
DBG("Failed to add Advertising Data");

free(cp);

return btd_error_failed(client->reg, "Failed");
}

free(cp);

return NULL;
}

Reference:
In lib/mgmt.h,

#define MGMT_OP_ADD_ADVERTISING 0x003E
struct mgmt_cp_add_advertising {
uint8_t instance;
uint32_t flags;
uint16_t duration;
uint16_t timeout;
uint8_t adv_data_len;
uint8_t scan_rsp_len;
uint8_t data[0];
} __packed;
>From mgmt-api.txt, I am

Add Advertising Command
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Command Code: 0x003e
Controller Index: <controller id>
Command Parameters: Instance (1 Octet)
Flags (4 Octets)
Duration (2 Octets)
Timeout (2 Octets)
Adv_Data_Len (1 Octet)
Scan_Rsp_len (1 Octet)
Adv_Data (0-255 Octets)
Scan_Rsp (0-255 Octets)
=E2=80=A6...
With the Flags value the type of advertising is controlled and
the following flags are defined:

0 Switch into Connectable mode
1 Advertise as Discoverable
2 Advertise as Limited Discoverable
3 Add Flags field to Adv_Data
4 Add TX Power field to Adv_Data
5 Add Appearance field to Scan_Rsp
6 Add Local Name in Scan_Rsp


2017-08-02 23:54:56

by Marcel Holtmann

[permalink] [raw]
Subject: Re: how to add local name in scan response when setting MGMT_ADV_FLAG_LOCAL_NAME in Bluez

Hi Yunnan,

> Thanks for your reply. I think kernel should support scan response
> because I am able to set different local name in scan rsp using hci
> socket instead of Dbus API. Have you ever post your patch or sample
> code in this thread or any other place? May I have a try on your
> patch? It would be doable and useful to put Local name change as
> property, like include tx power, so that we can set it in scan rsp
> when enabling advertisement.

have you actually checked that local name is supported via advertising instances. The Read Advertising Features mgmt command is pretty clear in its description that support for adding local name in scan response is optional. The command is also pretty clear in that if that flag is given, the local name is managed via the kernel and can only be modified via Set Local Name mgmt command. If you want to use any other name in scan response, you need to not specify that flag.

Using raw HCI sockets is like packet injection. That does not count. Also you will never make that work correctly since you essentially by-passing the kernel and programming the hardware directly.

Regards

Marcel


2017-08-01 15:42:38

by Yunhan Wang

[permalink] [raw]
Subject: Re: how to add local name in scan response when setting MGMT_ADV_FLAG_LOCAL_NAME in Bluez

Hi Luiz

Thanks for your reply. I think kernel should support scan response
because I am able to set different local name in scan rsp using hci
socket instead of Dbus API. Have you ever post your patch or sample
code in this thread or any other place? May I have a try on your
patch? It would be doable and useful to put Local name change as
property, like include tx power, so that we can set it in scan rsp
when enabling advertisement.

Thanks
Best wishes
Yunhan

On Tue, Aug 1, 2017 at 5:24 AM, Luiz Augusto von Dentz
<[email protected]> wrote:
> Hi Yunhan,
>
> On Tue, Aug 1, 2017 at 1:48 PM, Luiz Augusto von Dentz
> <[email protected]> wrote:
>> Hi Yunhan,
>>
>> On Tue, Aug 1, 2017 at 1:21 PM, Luiz Augusto von Dentz
>> <[email protected]> wrote:
>>> Hi Yunhan,
>>>
>>> On Tue, Aug 1, 2017 at 10:37 AM, Yunhan Wang <[email protected]> wrote=
:
>>>> Hi
>>>>
>>>> I am trying to do some small experiments that add local name in the
>>>> scan response, should I use data[0] to store both adv_data and
>>>> scan_rsp?
>>>>
>>>> I tried to add local name(=E2=80=99TEST=E2=80=99) in data[0] under
>>>> mgmt_cp_add_advertising on
>>>> https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/src/advertisin=
g.c#n484,
>>>> it seems it does not work, any idea?
>>>
>>> Seems to be a limitation in the kernel, also setting the adapter name
>>> doesn't change anything either so it seems it simply does not include
>>> any scan response. I guess we would have to add support for that.
>>
>> Actually the kernel does seem to set the scan response data, but is empt=
y:
>>
>> < HCI Command: LE Set Scan Response Data (0x08|0x0009) plen 32
>> #8499 13:40:40.663565
>> Length: 0
>>> HCI Event: Command Complete (0x0e) plen 4 #8500=
13:40:40.664554
>> LE Set Scan Response Data (0x08|0x0009) ncmd 1
>> Status: Success (0x00)
>
> Ive sent some patches that should enable to use the local adapter name
> in the scan data, if you do intend to use a different name then we
> might gonna need to add a different property or rework the IncludeName
> to be a string and if empty use the local adapter name.
>
>>>> Thanks
>>>> Best wishes
>>>> Yunhan
>>>>
>>>> static DBusMessage *refresh_advertisement(struct btd_adv_client *clien=
t)
>>>> {
>>>> struct mgmt_cp_add_advertising *cp;
>>>> uint8_t param_len;
>>>> uint8_t *adv_data;
>>>> size_t adv_data_len;
>>>> uint32_t flags =3D 0;
>>>>
>>>> uint8_t scan_rsp[] =3D {
>>>> =E2=80=99T=E2=80=99,
>>>> =E2=80=99E=E2=80=99,
>>>> =E2=80=99S=E2=80=99,
>>>> =E2=80=99T=E2=80=99
>>>> };
>>>> size_t scan_rsp_len =3D sizeof(scan_rsp);
>
> This needs to be in raw scan data format aka TLV.
>
>>>> DBG("Refreshing advertisement: %s", client->path);
>>>>
>>>> if (client->type =3D=3D AD_TYPE_PERIPHERAL)
>>>> flags =3D MGMT_ADV_FLAG_CONNECTABLE | MGMT_ADV_FLAG_DISCOV |
>>>> MGMT_ADV_FLAG_LOCAL_NAME;
>>>>
>>>> if (client->include_tx_power)
>>>> flags |=3D MGMT_ADV_FLAG_TX_POWER;
>>>>
>>>> adv_data =3D bt_ad_generate(client->data, &adv_data_len);
>>>>
>>>> if (!adv_data || (adv_data_len > calc_max_adv_len(client, flags))) {
>>>> error("Advertising data too long or couldn't be generated.");
>>>>
>>>> return g_dbus_create_error(client->reg, ERROR_INTERFACE
>>>> ".InvalidLength",
>>>> "Advertising data too long.");
>>>> }
>>>>
>>>> param_len =3D sizeof(struct mgmt_cp_add_advertising) + adv_data_len +
>>>> scan_rsp_len;
>>>>
>>>> cp =3D malloc0(param_len);
>>>>
>>>> if (!cp) {
>>>> error("Couldn't allocate for MGMT!");
>>>>
>>>> free(adv_data);
>>>>
>>>> return btd_error_failed(client->reg, "Failed");
>>>> }
>>>>
>>>> cp->flags =3D htobl(flags);
>>>> cp->instance =3D client->instance;
>>>> cp->adv_data_len =3D adv_data_len;
>>>> cp->scan_rsp_len =3D scan_rsp_len;
>>>> memcpy(cp->data, adv_data, adv_data_len);
>>>> memcpy(cp->data + adv_data_len, scan_rsp_data, scan_rsp_len);
>>>> free(adv_data);
>>>>
>>>> if (!mgmt_send(client->manager->mgmt, MGMT_OP_ADD_ADVERTISING,
>>>> client->manager->mgmt_index, param_len, cp,
>>>> add_adv_callback, client, NULL)) {
>>>> DBG("Failed to add Advertising Data");
>>>>
>>>> free(cp);
>>>>
>>>> return btd_error_failed(client->reg, "Failed");
>>>> }
>>>>
>>>> free(cp);
>>>>
>>>> return NULL;
>>>> }
>>>>
>>>> Reference:
>>>> In lib/mgmt.h,
>>>>
>>>> #define MGMT_OP_ADD_ADVERTISING 0x003E
>>>> struct mgmt_cp_add_advertising {
>>>> uint8_t instance;
>>>> uint32_t flags;
>>>> uint16_t duration;
>>>> uint16_t timeout;
>>>> uint8_t adv_data_len;
>>>> uint8_t scan_rsp_len;
>>>> uint8_t data[0];
>>>> } __packed;
>>>> From mgmt-api.txt, I am
>>>>
>>>> Add Advertising Command
>>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>>>>
>>>> Command Code: 0x003e
>>>> Controller Index: <controller id>
>>>> Command Parameters: Instance (1 Octet)
>>>> Flags (4 Octets)
>>>> Duration (2 Octets)
>>>> Timeout (2 Octets)
>>>> Adv_Data_Len (1 Octet)
>>>> Scan_Rsp_len (1 Octet)
>>>> Adv_Data (0-255 Octets)
>>>> Scan_Rsp (0-255 Octets)
>>>> =E2=80=A6...
>>>> With the Flags value the type of advertising is controlled and
>>>> the following flags are defined:
>>>>
>>>> 0 Switch into Connectable mode
>>>> 1 Advertise as Discoverable
>>>> 2 Advertise as Limited Discoverable
>>>> 3 Add Flags field to Adv_Data
>>>> 4 Add TX Power field to Adv_Data
>>>> 5 Add Appearance field to Scan_Rsp
>>>> 6 Add Local Name in Scan_Rsp
>>>> --
>>>> To unsubscribe from this list: send the line "unsubscribe linux-blueto=
oth" in
>>>> the body of a message to [email protected]
>>>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>>
>>>
>>>
>>> --
>>> Luiz Augusto von Dentz
>>
>>
>>
>> --
>> Luiz Augusto von Dentz
>
>
>
> --
> Luiz Augusto von Dentz

2017-08-01 12:24:40

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: how to add local name in scan response when setting MGMT_ADV_FLAG_LOCAL_NAME in Bluez

Hi Yunhan,

On Tue, Aug 1, 2017 at 1:48 PM, Luiz Augusto von Dentz
<[email protected]> wrote:
> Hi Yunhan,
>
> On Tue, Aug 1, 2017 at 1:21 PM, Luiz Augusto von Dentz
> <[email protected]> wrote:
>> Hi Yunhan,
>>
>> On Tue, Aug 1, 2017 at 10:37 AM, Yunhan Wang <[email protected]> wrote:
>>> Hi
>>>
>>> I am trying to do some small experiments that add local name in the
>>> scan response, should I use data[0] to store both adv_data and
>>> scan_rsp?
>>>
>>> I tried to add local name(=E2=80=99TEST=E2=80=99) in data[0] under
>>> mgmt_cp_add_advertising on
>>> https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/src/advertising=
.c#n484,
>>> it seems it does not work, any idea?
>>
>> Seems to be a limitation in the kernel, also setting the adapter name
>> doesn't change anything either so it seems it simply does not include
>> any scan response. I guess we would have to add support for that.
>
> Actually the kernel does seem to set the scan response data, but is empty=
:
>
> < HCI Command: LE Set Scan Response Data (0x08|0x0009) plen 32
> #8499 13:40:40.663565
> Length: 0
>> HCI Event: Command Complete (0x0e) plen 4 #8500 =
13:40:40.664554
> LE Set Scan Response Data (0x08|0x0009) ncmd 1
> Status: Success (0x00)

Ive sent some patches that should enable to use the local adapter name
in the scan data, if you do intend to use a different name then we
might gonna need to add a different property or rework the IncludeName
to be a string and if empty use the local adapter name.

>>> Thanks
>>> Best wishes
>>> Yunhan
>>>
>>> static DBusMessage *refresh_advertisement(struct btd_adv_client *client=
)
>>> {
>>> struct mgmt_cp_add_advertising *cp;
>>> uint8_t param_len;
>>> uint8_t *adv_data;
>>> size_t adv_data_len;
>>> uint32_t flags =3D 0;
>>>
>>> uint8_t scan_rsp[] =3D {
>>> =E2=80=99T=E2=80=99,
>>> =E2=80=99E=E2=80=99,
>>> =E2=80=99S=E2=80=99,
>>> =E2=80=99T=E2=80=99
>>> };
>>> size_t scan_rsp_len =3D sizeof(scan_rsp);

This needs to be in raw scan data format aka TLV.

>>> DBG("Refreshing advertisement: %s", client->path);
>>>
>>> if (client->type =3D=3D AD_TYPE_PERIPHERAL)
>>> flags =3D MGMT_ADV_FLAG_CONNECTABLE | MGMT_ADV_FLAG_DISCOV |
>>> MGMT_ADV_FLAG_LOCAL_NAME;
>>>
>>> if (client->include_tx_power)
>>> flags |=3D MGMT_ADV_FLAG_TX_POWER;
>>>
>>> adv_data =3D bt_ad_generate(client->data, &adv_data_len);
>>>
>>> if (!adv_data || (adv_data_len > calc_max_adv_len(client, flags))) {
>>> error("Advertising data too long or couldn't be generated.");
>>>
>>> return g_dbus_create_error(client->reg, ERROR_INTERFACE
>>> ".InvalidLength",
>>> "Advertising data too long.");
>>> }
>>>
>>> param_len =3D sizeof(struct mgmt_cp_add_advertising) + adv_data_len +
>>> scan_rsp_len;
>>>
>>> cp =3D malloc0(param_len);
>>>
>>> if (!cp) {
>>> error("Couldn't allocate for MGMT!");
>>>
>>> free(adv_data);
>>>
>>> return btd_error_failed(client->reg, "Failed");
>>> }
>>>
>>> cp->flags =3D htobl(flags);
>>> cp->instance =3D client->instance;
>>> cp->adv_data_len =3D adv_data_len;
>>> cp->scan_rsp_len =3D scan_rsp_len;
>>> memcpy(cp->data, adv_data, adv_data_len);
>>> memcpy(cp->data + adv_data_len, scan_rsp_data, scan_rsp_len);
>>> free(adv_data);
>>>
>>> if (!mgmt_send(client->manager->mgmt, MGMT_OP_ADD_ADVERTISING,
>>> client->manager->mgmt_index, param_len, cp,
>>> add_adv_callback, client, NULL)) {
>>> DBG("Failed to add Advertising Data");
>>>
>>> free(cp);
>>>
>>> return btd_error_failed(client->reg, "Failed");
>>> }
>>>
>>> free(cp);
>>>
>>> return NULL;
>>> }
>>>
>>> Reference:
>>> In lib/mgmt.h,
>>>
>>> #define MGMT_OP_ADD_ADVERTISING 0x003E
>>> struct mgmt_cp_add_advertising {
>>> uint8_t instance;
>>> uint32_t flags;
>>> uint16_t duration;
>>> uint16_t timeout;
>>> uint8_t adv_data_len;
>>> uint8_t scan_rsp_len;
>>> uint8_t data[0];
>>> } __packed;
>>> From mgmt-api.txt, I am
>>>
>>> Add Advertising Command
>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>>>
>>> Command Code: 0x003e
>>> Controller Index: <controller id>
>>> Command Parameters: Instance (1 Octet)
>>> Flags (4 Octets)
>>> Duration (2 Octets)
>>> Timeout (2 Octets)
>>> Adv_Data_Len (1 Octet)
>>> Scan_Rsp_len (1 Octet)
>>> Adv_Data (0-255 Octets)
>>> Scan_Rsp (0-255 Octets)
>>> =E2=80=A6...
>>> With the Flags value the type of advertising is controlled and
>>> the following flags are defined:
>>>
>>> 0 Switch into Connectable mode
>>> 1 Advertise as Discoverable
>>> 2 Advertise as Limited Discoverable
>>> 3 Add Flags field to Adv_Data
>>> 4 Add TX Power field to Adv_Data
>>> 5 Add Appearance field to Scan_Rsp
>>> 6 Add Local Name in Scan_Rsp
>>> --
>>> To unsubscribe from this list: send the line "unsubscribe linux-bluetoo=
th" in
>>> the body of a message to [email protected]
>>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>>
>>
>>
>> --
>> Luiz Augusto von Dentz
>
>
>
> --
> Luiz Augusto von Dentz



--=20
Luiz Augusto von Dentz

2017-08-01 10:48:15

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: how to add local name in scan response when setting MGMT_ADV_FLAG_LOCAL_NAME in Bluez

Hi Yunhan,

On Tue, Aug 1, 2017 at 1:21 PM, Luiz Augusto von Dentz
<[email protected]> wrote:
> Hi Yunhan,
>
> On Tue, Aug 1, 2017 at 10:37 AM, Yunhan Wang <[email protected]> wrote:
>> Hi
>>
>> I am trying to do some small experiments that add local name in the
>> scan response, should I use data[0] to store both adv_data and
>> scan_rsp?
>>
>> I tried to add local name(=E2=80=99TEST=E2=80=99) in data[0] under
>> mgmt_cp_add_advertising on
>> https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/src/advertising.=
c#n484,
>> it seems it does not work, any idea?
>
> Seems to be a limitation in the kernel, also setting the adapter name
> doesn't change anything either so it seems it simply does not include
> any scan response. I guess we would have to add support for that.

Actually the kernel does seem to set the scan response data, but is empty:

< HCI Command: LE Set Scan Response Data (0x08|0x0009) plen 32
#8499 13:40:40.663565
Length: 0
> HCI Event: Command Complete (0x0e) plen 4 #8500 1=
3:40:40.664554
LE Set Scan Response Data (0x08|0x0009) ncmd 1
Status: Success (0x00)

>> Thanks
>> Best wishes
>> Yunhan
>>
>> static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
>> {
>> struct mgmt_cp_add_advertising *cp;
>> uint8_t param_len;
>> uint8_t *adv_data;
>> size_t adv_data_len;
>> uint32_t flags =3D 0;
>>
>> uint8_t scan_rsp[] =3D {
>> =E2=80=99T=E2=80=99,
>> =E2=80=99E=E2=80=99,
>> =E2=80=99S=E2=80=99,
>> =E2=80=99T=E2=80=99
>> };
>> size_t scan_rsp_len =3D sizeof(scan_rsp);
>>
>> DBG("Refreshing advertisement: %s", client->path);
>>
>> if (client->type =3D=3D AD_TYPE_PERIPHERAL)
>> flags =3D MGMT_ADV_FLAG_CONNECTABLE | MGMT_ADV_FLAG_DISCOV |
>> MGMT_ADV_FLAG_LOCAL_NAME;
>>
>> if (client->include_tx_power)
>> flags |=3D MGMT_ADV_FLAG_TX_POWER;
>>
>> adv_data =3D bt_ad_generate(client->data, &adv_data_len);
>>
>> if (!adv_data || (adv_data_len > calc_max_adv_len(client, flags))) {
>> error("Advertising data too long or couldn't be generated.");
>>
>> return g_dbus_create_error(client->reg, ERROR_INTERFACE
>> ".InvalidLength",
>> "Advertising data too long.");
>> }
>>
>> param_len =3D sizeof(struct mgmt_cp_add_advertising) + adv_data_len +
>> scan_rsp_len;
>>
>> cp =3D malloc0(param_len);
>>
>> if (!cp) {
>> error("Couldn't allocate for MGMT!");
>>
>> free(adv_data);
>>
>> return btd_error_failed(client->reg, "Failed");
>> }
>>
>> cp->flags =3D htobl(flags);
>> cp->instance =3D client->instance;
>> cp->adv_data_len =3D adv_data_len;
>> cp->scan_rsp_len =3D scan_rsp_len;
>> memcpy(cp->data, adv_data, adv_data_len);
>> memcpy(cp->data + adv_data_len, scan_rsp_data, scan_rsp_len);
>> free(adv_data);
>>
>> if (!mgmt_send(client->manager->mgmt, MGMT_OP_ADD_ADVERTISING,
>> client->manager->mgmt_index, param_len, cp,
>> add_adv_callback, client, NULL)) {
>> DBG("Failed to add Advertising Data");
>>
>> free(cp);
>>
>> return btd_error_failed(client->reg, "Failed");
>> }
>>
>> free(cp);
>>
>> return NULL;
>> }
>>
>> Reference:
>> In lib/mgmt.h,
>>
>> #define MGMT_OP_ADD_ADVERTISING 0x003E
>> struct mgmt_cp_add_advertising {
>> uint8_t instance;
>> uint32_t flags;
>> uint16_t duration;
>> uint16_t timeout;
>> uint8_t adv_data_len;
>> uint8_t scan_rsp_len;
>> uint8_t data[0];
>> } __packed;
>> From mgmt-api.txt, I am
>>
>> Add Advertising Command
>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>>
>> Command Code: 0x003e
>> Controller Index: <controller id>
>> Command Parameters: Instance (1 Octet)
>> Flags (4 Octets)
>> Duration (2 Octets)
>> Timeout (2 Octets)
>> Adv_Data_Len (1 Octet)
>> Scan_Rsp_len (1 Octet)
>> Adv_Data (0-255 Octets)
>> Scan_Rsp (0-255 Octets)
>> =E2=80=A6...
>> With the Flags value the type of advertising is controlled and
>> the following flags are defined:
>>
>> 0 Switch into Connectable mode
>> 1 Advertise as Discoverable
>> 2 Advertise as Limited Discoverable
>> 3 Add Flags field to Adv_Data
>> 4 Add TX Power field to Adv_Data
>> 5 Add Appearance field to Scan_Rsp
>> 6 Add Local Name in Scan_Rsp
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-bluetoot=
h" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>
> --
> Luiz Augusto von Dentz



--=20
Luiz Augusto von Dentz

2017-08-01 10:21:40

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: how to add local name in scan response when setting MGMT_ADV_FLAG_LOCAL_NAME in Bluez

Hi Yunhan,

On Tue, Aug 1, 2017 at 10:37 AM, Yunhan Wang <[email protected]> wrote:
> Hi
>
> I am trying to do some small experiments that add local name in the
> scan response, should I use data[0] to store both adv_data and
> scan_rsp?
>
> I tried to add local name(=E2=80=99TEST=E2=80=99) in data[0] under
> mgmt_cp_add_advertising on
> https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/src/advertising.c=
#n484,
> it seems it does not work, any idea?

Seems to be a limitation in the kernel, also setting the adapter name
doesn't change anything either so it seems it simply does not include
any scan response. I guess we would have to add support for that.

> Thanks
> Best wishes
> Yunhan
>
> static DBusMessage *refresh_advertisement(struct btd_adv_client *client)
> {
> struct mgmt_cp_add_advertising *cp;
> uint8_t param_len;
> uint8_t *adv_data;
> size_t adv_data_len;
> uint32_t flags =3D 0;
>
> uint8_t scan_rsp[] =3D {
> =E2=80=99T=E2=80=99,
> =E2=80=99E=E2=80=99,
> =E2=80=99S=E2=80=99,
> =E2=80=99T=E2=80=99
> };
> size_t scan_rsp_len =3D sizeof(scan_rsp);
>
> DBG("Refreshing advertisement: %s", client->path);
>
> if (client->type =3D=3D AD_TYPE_PERIPHERAL)
> flags =3D MGMT_ADV_FLAG_CONNECTABLE | MGMT_ADV_FLAG_DISCOV |
> MGMT_ADV_FLAG_LOCAL_NAME;
>
> if (client->include_tx_power)
> flags |=3D MGMT_ADV_FLAG_TX_POWER;
>
> adv_data =3D bt_ad_generate(client->data, &adv_data_len);
>
> if (!adv_data || (adv_data_len > calc_max_adv_len(client, flags))) {
> error("Advertising data too long or couldn't be generated.");
>
> return g_dbus_create_error(client->reg, ERROR_INTERFACE
> ".InvalidLength",
> "Advertising data too long.");
> }
>
> param_len =3D sizeof(struct mgmt_cp_add_advertising) + adv_data_len +
> scan_rsp_len;
>
> cp =3D malloc0(param_len);
>
> if (!cp) {
> error("Couldn't allocate for MGMT!");
>
> free(adv_data);
>
> return btd_error_failed(client->reg, "Failed");
> }
>
> cp->flags =3D htobl(flags);
> cp->instance =3D client->instance;
> cp->adv_data_len =3D adv_data_len;
> cp->scan_rsp_len =3D scan_rsp_len;
> memcpy(cp->data, adv_data, adv_data_len);
> memcpy(cp->data + adv_data_len, scan_rsp_data, scan_rsp_len);
> free(adv_data);
>
> if (!mgmt_send(client->manager->mgmt, MGMT_OP_ADD_ADVERTISING,
> client->manager->mgmt_index, param_len, cp,
> add_adv_callback, client, NULL)) {
> DBG("Failed to add Advertising Data");
>
> free(cp);
>
> return btd_error_failed(client->reg, "Failed");
> }
>
> free(cp);
>
> return NULL;
> }
>
> Reference:
> In lib/mgmt.h,
>
> #define MGMT_OP_ADD_ADVERTISING 0x003E
> struct mgmt_cp_add_advertising {
> uint8_t instance;
> uint32_t flags;
> uint16_t duration;
> uint16_t timeout;
> uint8_t adv_data_len;
> uint8_t scan_rsp_len;
> uint8_t data[0];
> } __packed;
> From mgmt-api.txt, I am
>
> Add Advertising Command
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> Command Code: 0x003e
> Controller Index: <controller id>
> Command Parameters: Instance (1 Octet)
> Flags (4 Octets)
> Duration (2 Octets)
> Timeout (2 Octets)
> Adv_Data_Len (1 Octet)
> Scan_Rsp_len (1 Octet)
> Adv_Data (0-255 Octets)
> Scan_Rsp (0-255 Octets)
> =E2=80=A6...
> With the Flags value the type of advertising is controlled and
> the following flags are defined:
>
> 0 Switch into Connectable mode
> 1 Advertise as Discoverable
> 2 Advertise as Limited Discoverable
> 3 Add Flags field to Adv_Data
> 4 Add TX Power field to Adv_Data
> 5 Add Appearance field to Scan_Rsp
> 6 Add Local Name in Scan_Rsp
> --
> To unsubscribe from this list: send the line "unsubscribe linux-bluetooth=
" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html



--=20
Luiz Augusto von Dentz