2017-11-02 14:15:13

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 1/7] doc/advertising-api: Add Duration and Timeout

From: Luiz Augusto von Dentz <[email protected]>

This adds duration and timeout so application can control the periods
which the advertisement stays active.
---
doc/advertising-api.txt | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/doc/advertising-api.txt b/doc/advertising-api.txt
index b10010a19..15e64d38a 100644
--- a/doc/advertising-api.txt
+++ b/doc/advertising-api.txt
@@ -84,6 +84,17 @@ Properties string Type

Possible values: as found on GAP Service.

+ uint16_t Duration
+
+ Duration of the advertisement in seconds. If there are
+ other applications advertising no duration is set the
+ default is 2 seconds.
+
+ uint16_t Timeout
+
+ Timeout of the advertisement in seconds. This defines
+ the lifetime of the advertisement.
+
LE Advertising Manager hierarchy
================================

--
2.13.6



2017-11-02 14:24:15

by Szymon Janc

[permalink] [raw]
Subject: Re: [PATCH BlueZ 5/7] core: Add LEAdvertising1 to bluetooth.conf

Hi Luiz,

On Thursday, 2 November 2017 15:15:17 CET Luiz Augusto von Dentz wrote:
> From: Luiz Augusto von Dentz <[email protected]>
>
> LEAdvertising1 must be included in order to be able to call Release.
> ---
> src/bluetooth.conf | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/src/bluetooth.conf b/src/bluetooth.conf
> index 60e75955c..74a2d3147 100644
> --- a/src/bluetooth.conf
> +++ b/src/bluetooth.conf
> @@ -20,7 +20,7 @@
> <allow send_interface="org.bluez.CyclingSpeedWatcher1"/>
> <allow send_interface="org.bluez.GattCharacteristic1"/>
> <allow send_interface="org.bluez.GattDescriptor1"/>
> - <allow send_interface="org.bluez.LEAdvertisement1"/>
> + <allow send_interface="org.bluez.LEAdvertisement1"/>
> <allow send_interface="org.freedesktop.DBus.ObjectManager"/>
> <allow send_interface="org.freedesktop.DBus.Properties"/>
> </policy>

Some rebase issue with this and patch 4/7 ?

Also subject has a typo LEAdvertis_ing_1

--
pozdrawiam
Szymon Janc

2017-11-02 14:15:19

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 7/7] tests: Remove test-thermometer

From: Luiz Augusto von Dentz <[email protected]>

Thermomether interfaces no longer exists.
---
test/test-thermometer | 99 ---------------------------------------------------
1 file changed, 99 deletions(-)
delete mode 100755 test/test-thermometer

diff --git a/test/test-thermometer b/test/test-thermometer
deleted file mode 100755
index 7e67c234d..000000000
--- a/test/test-thermometer
+++ /dev/null
@@ -1,99 +0,0 @@
-#!/usr/bin/python
-
-from __future__ import absolute_import, print_function, unicode_literals
-
-'''
-Thermometer test script
-'''
-
-from optparse import OptionParser, make_option
-import sys
-import dbus
-import dbus.service
-import dbus.mainloop.glib
-try:
- from gi.repository import GObject
-except ImportError:
- import gobject as GObject
-import bluezutils
-
-BUS_NAME = 'org.bluez'
-THERMOMETER_MANAGER_INTERFACE = 'org.bluez.ThermometerManager1'
-THERMOMETER_WATCHER_INTERFACE = 'org.bluez.ThermometerWatcher1'
-THERMOMETER_INTERFACE = 'org.bluez.Thermometer1'
-
-class Watcher(dbus.service.Object):
- @dbus.service.method(THERMOMETER_WATCHER_INTERFACE,
- in_signature="oa{sv}", out_signature="")
- def MeasurementReceived(self, device, measure):
- print("%s measurement received from %s" % (measure["Measurement"], device))
- print("Exponent: ", measure["Exponent"])
- print("Mantissa: ", measure["Mantissa"])
- print("Unit: ", measure["Unit"])
-
- if "Time" in measure:
- print("Time: ", measure["Time"])
-
- if "Type" in measure:
- print("Type: ", measure["Type"])
-
-def properties_changed(interface, changed, invalidated):
- if interface != THERMOMETER_INTERFACE:
- return
- for name, value in changed.iteritems():
- print("Property %s changed: %s" % (name, str(value)))
-
-if __name__ == "__main__":
- dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
-
- bus = dbus.SystemBus()
-
- option_list = [
- make_option("-i", "--adapter", action="store",
- type="string", dest="adapter"),
- make_option("-b", "--device", action="store",
- type="string", dest="address"),
- ]
-
- parser = OptionParser(option_list=option_list)
-
- (options, args) = parser.parse_args()
-
- if not options.address:
- print("Usage: %s [-i <adapter>] -b <bdaddr> [command]" % (sys.argv[0]))
- print("Possible commands:")
- print("\tEnableIntermediateMeasurement")
- sys.exit(1)
-
- managed_objects = bluezutils.get_managed_objects()
- adapter = bluezutils.find_adapter_in_objects(managed_objects,
- options.adapter)
- adapter_path = adapter.object_path
-
- thermometer_manager = dbus.Interface(bus.get_object(BUS_NAME,
- adapter_path), THERMOMETER_MANAGER_INTERFACE)
-
- device = bluezutils.find_device_in_objects(managed_objects,
- options.address,
- options.adapter)
- device_path = device.object_path
-
- bus.add_signal_receiver(properties_changed, bus_name=BUS_NAME,
- path=device_path,
- dbus_interface="org.freedesktop.DBus.Properties",
- signal_name="PropertiesChanged")
-
- path = "/test/watcher"
- watcher = Watcher(bus, path)
-
- thermometer_manager.RegisterWatcher(path)
-
- if len(args) > 0:
- if args[0] == "EnableIntermediateMeasurement":
- thermometer_manager.EnableIntermediateMeasurement(path)
- else:
- print("unknown command")
- sys.exit(1)
-
- mainloop = GObject.MainLoop()
- mainloop.run()
--
2.13.6


2017-11-02 14:15:18

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 6/7] core: Cleanup bluetooth.conf

From: Luiz Augusto von Dentz <[email protected]>

This removes interfaces that no longer exists.
---
src/bluetooth.conf | 4 ----
1 file changed, 4 deletions(-)

diff --git a/src/bluetooth.conf b/src/bluetooth.conf
index 74a2d3147..0c0b221bb 100644
--- a/src/bluetooth.conf
+++ b/src/bluetooth.conf
@@ -13,11 +13,7 @@
<allow send_interface="org.bluez.Agent1"/>
<allow send_interface="org.bluez.MediaEndpoint1"/>
<allow send_interface="org.bluez.MediaPlayer1"/>
- <allow send_interface="org.bluez.ThermometerWatcher1"/>
- <allow send_interface="org.bluez.AlertAgent1"/>
<allow send_interface="org.bluez.Profile1"/>
- <allow send_interface="org.bluez.HeartRateWatcher1"/>
- <allow send_interface="org.bluez.CyclingSpeedWatcher1"/>
<allow send_interface="org.bluez.GattCharacteristic1"/>
<allow send_interface="org.bluez.GattDescriptor1"/>
<allow send_interface="org.bluez.LEAdvertisement1"/>
--
2.13.6


2017-11-02 14:15:17

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 5/7] core: Add LEAdvertising1 to bluetooth.conf

From: Luiz Augusto von Dentz <[email protected]>

LEAdvertising1 must be included in order to be able to call Release.
---
src/bluetooth.conf | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bluetooth.conf b/src/bluetooth.conf
index 60e75955c..74a2d3147 100644
--- a/src/bluetooth.conf
+++ b/src/bluetooth.conf
@@ -20,7 +20,7 @@
<allow send_interface="org.bluez.CyclingSpeedWatcher1"/>
<allow send_interface="org.bluez.GattCharacteristic1"/>
<allow send_interface="org.bluez.GattDescriptor1"/>
- <allow send_interface="org.bluez.LEAdvertisement1"/>
+ <allow send_interface="org.bluez.LEAdvertisement1"/>
<allow send_interface="org.freedesktop.DBus.ObjectManager"/>
<allow send_interface="org.freedesktop.DBus.Properties"/>
</policy>
--
2.13.6


2017-11-02 14:15:16

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 4/7] advertising: Use client proxy to release

From: Luiz Augusto von Dentz <[email protected]>

This uses g_dbus_proxy_method_call instead of build the message
manually.
---
src/advertising.c | 13 ++-----------
src/bluetooth.conf | 1 +
2 files changed, 3 insertions(+), 11 deletions(-)

diff --git a/src/advertising.c b/src/advertising.c
index 31a5fff5c..94a8c4050 100644
--- a/src/advertising.c
+++ b/src/advertising.c
@@ -137,20 +137,11 @@ static gboolean client_free_idle_cb(void *data)
static void client_release(void *data)
{
struct btd_adv_client *client = data;
- DBusMessage *message;

DBG("Releasing advertisement %s, %s", client->owner, client->path);

- message = dbus_message_new_method_call(client->owner, client->path,
- LE_ADVERTISEMENT_IFACE,
- "Release");
-
- if (!message) {
- error("Couldn't allocate D-Bus message");
- return;
- }
-
- g_dbus_send_message(btd_get_dbus_connection(), message);
+ g_dbus_proxy_method_call(client->proxy, "Release", NULL, NULL, NULL,
+ NULL);
}

static void client_destroy(void *data)
diff --git a/src/bluetooth.conf b/src/bluetooth.conf
index 10d2d3688..60e75955c 100644
--- a/src/bluetooth.conf
+++ b/src/bluetooth.conf
@@ -20,6 +20,7 @@
<allow send_interface="org.bluez.CyclingSpeedWatcher1"/>
<allow send_interface="org.bluez.GattCharacteristic1"/>
<allow send_interface="org.bluez.GattDescriptor1"/>
+ <allow send_interface="org.bluez.LEAdvertisement1"/>
<allow send_interface="org.freedesktop.DBus.ObjectManager"/>
<allow send_interface="org.freedesktop.DBus.Properties"/>
</policy>
--
2.13.6


2017-11-02 14:15:15

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 3/7] client: Add set-advertise-{duration, timeout}

From: Luiz Augusto von Dentz <[email protected]>

This adds the following command which can be used to control the
advertisement intervals:

[bluetooth]# set-advertise-duration 4
[bluetooth]# set-advertise-timeout 4
[bluetooth]# advertise on
[CHG] Controller B8:8A:60:D8:17:D7 SupportedInstances: 0x04
[CHG] Controller B8:8A:60:D8:17:D7 ActiveInstances: 0x01
Advertising object registered
[CHG] Controller B8:8A:60:D8:17:D7 SupportedInstances: 0x05
[CHG] Controller B8:8A:60:D8:17:D7 ActiveInstances: 0x00
---
client/advertising.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
client/advertising.h | 2 ++
client/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 94 insertions(+)

diff --git a/client/advertising.c b/client/advertising.c
index e950aa278..56093f387 100644
--- a/client/advertising.c
+++ b/client/advertising.c
@@ -59,6 +59,8 @@ static struct ad {
char *type;
char *local_name;
uint16_t local_appearance;
+ uint16_t duration;
+ uint16_t timeout;
char **uuids;
size_t uuids_len;
struct service_data service;
@@ -323,6 +325,32 @@ static gboolean get_appearance(const GDBusPropertyTable *property,
return TRUE;
}

+static gboolean duration_exits(const GDBusPropertyTable *property, void *data)
+{
+ return ad.duration;
+}
+
+static gboolean get_duration(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *user_data)
+{
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, &ad.duration);
+
+ return TRUE;
+}
+
+static gboolean timeout_exits(const GDBusPropertyTable *property, void *data)
+{
+ return ad.timeout;
+}
+
+static gboolean get_timeout(const GDBusPropertyTable *property,
+ DBusMessageIter *iter, void *user_data)
+{
+ dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, &ad.timeout);
+
+ return TRUE;
+}
+
static const GDBusPropertyTable ad_props[] = {
{ "Type", "s", get_type },
{ "ServiceUUIDs", "as", get_uuids, NULL, uuids_exists },
@@ -332,6 +360,8 @@ static const GDBusPropertyTable ad_props[] = {
{ "Includes", "as", get_includes, NULL, includes_exists },
{ "LocalName", "s", get_local_name, NULL, local_name_exits },
{ "Appearance", "q", get_appearance, NULL, appearance_exits },
+ { "Duration", "q", get_duration, NULL, duration_exits },
+ { "Timeout", "q", get_timeout, NULL, timeout_exits },
{ }
};

@@ -592,3 +622,23 @@ void ad_advertise_local_appearance(DBusConnection *conn, uint16_t value)

g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Appearance");
}
+
+void ad_advertise_duration(DBusConnection *conn, uint16_t value)
+{
+ if (ad.duration == value)
+ return;
+
+ ad.duration = value;
+
+ g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Duration");
+}
+
+void ad_advertise_timeout(DBusConnection *conn, uint16_t value)
+{
+ if (ad.timeout == value)
+ return;
+
+ ad.timeout = value;
+
+ g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, "Timeout");
+}
diff --git a/client/advertising.h b/client/advertising.h
index 6868518e7..bb9aed210 100644
--- a/client/advertising.h
+++ b/client/advertising.h
@@ -32,3 +32,5 @@ void ad_advertise_name(DBusConnection *conn, bool value);
void ad_advertise_appearance(DBusConnection *conn, bool value);
void ad_advertise_local_name(DBusConnection *conn, const char *name);
void ad_advertise_local_appearance(DBusConnection *conn, uint16_t value);
+void ad_advertise_duration(DBusConnection *conn, uint16_t value);
+void ad_advertise_timeout(DBusConnection *conn, uint16_t value);
diff --git a/client/main.c b/client/main.c
index 3536c9524..37a411ba9 100644
--- a/client/main.c
+++ b/client/main.c
@@ -2376,6 +2376,44 @@ static void cmd_set_advertise_appearance(const char *arg)
ad_advertise_local_appearance(dbus_conn, value);
}

+static void cmd_set_advertise_duration(const char *arg)
+{
+ long int value;
+ char *endptr = NULL;
+
+ if (arg == NULL || strlen(arg) == 0) {
+ rl_printf("Missing value argument\n");
+ return;
+ }
+
+ value = strtol(arg, &endptr, 0);
+ if (!endptr || *endptr != '\0' || value > UINT16_MAX) {
+ rl_printf("Invalid argument\n");
+ return;
+ }
+
+ ad_advertise_duration(dbus_conn, value);
+}
+
+static void cmd_set_advertise_timeout(const char *arg)
+{
+ long int value;
+ char *endptr = NULL;
+
+ if (arg == NULL || strlen(arg) == 0) {
+ rl_printf("Missing value argument\n");
+ return;
+ }
+
+ value = strtol(arg, &endptr, 0);
+ if (!endptr || *endptr != '\0' || value > UINT16_MAX) {
+ rl_printf("Invalid argument\n");
+ return;
+ }
+
+ ad_advertise_timeout(dbus_conn, value);
+}
+
static const struct {
const char *cmd;
const char *arg;
@@ -2428,6 +2466,10 @@ static const struct {
"Enable/disable local name to be advertised" },
{ "set-advertise-appearance", "<value>", cmd_set_advertise_appearance,
"Set custom appearance to be advertised" },
+ { "set-advertise-duration", "<seconds>", cmd_set_advertise_duration,
+ "Set advertise duration" },
+ { "set-advertise-timeout", "<seconds>", cmd_set_advertise_timeout,
+ "Set advertise timeout" },
{ "set-scan-filter-uuids", "[uuid1 uuid2 ...]",
cmd_set_scan_filter_uuids, "Set scan filter uuids" },
{ "set-scan-filter-rssi", "[rssi]", cmd_set_scan_filter_rssi,
--
2.13.6


2017-11-02 14:15:14

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 2/7] advertising: Add implementation of Duration and Timeout

From: Luiz Augusto von Dentz <[email protected]>

This adds implementation of Duration and Timeout property and properly
set them in Add Advertising:

@ MGMT Command: Add Advertising (0x003e) plen 11
Instance: 1
Flags: 0x00000003
Switch into Connectable mode
Advertise as Discoverable
Duration: 4
Timeout: 0
Advertising data length: 0
Scan response length: 0
---
src/advertising.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)

diff --git a/src/advertising.c b/src/advertising.c
index d4d95c1c3..31a5fff5c 100644
--- a/src/advertising.c
+++ b/src/advertising.c
@@ -64,6 +64,9 @@ struct btd_adv_client {
char *path;
char *name;
uint16_t appearance;
+ uint16_t duration;
+ uint16_t timeout;
+ unsigned int to_id;
GDBusClient *client;
GDBusProxy *proxy;
DBusMessage *reg;
@@ -97,6 +100,9 @@ static void client_free(void *data)
{
struct btd_adv_client *client = data;

+ if (client->to_id > 0)
+ g_source_remove(client->to_id);
+
if (client->client) {
g_dbus_client_set_disconnect_watch(client->client, NULL, NULL);
g_dbus_client_unref(client->client);
@@ -491,6 +497,48 @@ static bool parse_appearance(DBusMessageIter *iter,
return true;
}

+static bool parse_duration(DBusMessageIter *iter,
+ struct btd_adv_client *client)
+{
+ if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16)
+ return false;
+
+ dbus_message_iter_get_basic(iter, &client->duration);
+
+ return true;
+}
+
+static gboolean client_timeout(void *user_data)
+{
+ struct btd_adv_client *client = user_data;
+
+ DBG("");
+
+ client->to_id = 0;
+
+ client_release(client);
+ client_remove(client);
+
+ return FALSE;
+}
+
+static bool parse_timeout(DBusMessageIter *iter,
+ struct btd_adv_client *client)
+{
+ if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_UINT16)
+ return false;
+
+ dbus_message_iter_get_basic(iter, &client->timeout);
+
+ if (client->to_id)
+ g_source_remove(client->to_id);
+
+ client->to_id = g_timeout_add_seconds(client->timeout, client_timeout,
+ client);
+
+ return true;
+}
+
static struct adv_parser {
const char *name;
bool (*func)(DBusMessageIter *iter, struct btd_adv_client *client);
@@ -503,6 +551,8 @@ static struct adv_parser {
{ "Includes", parse_includes },
{ "LocalName", parse_local_name },
{ "Appearance", parse_appearance },
+ { "Duration", parse_duration },
+ { "Timeout", parse_timeout },
{ },
};

@@ -611,6 +661,7 @@ static int refresh_adv(struct btd_adv_client *client, mgmt_request_func_t func)

cp->flags = htobl(flags);
cp->instance = client->instance;
+ cp->duration = client->duration;
cp->adv_data_len = adv_data_len;
cp->scan_rsp_len = scan_rsp_len;
memcpy(cp->data, adv_data, adv_data_len);
--
2.13.6