client: Add advertise.discoverable-timeout command 09/205009/1
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 21 May 2018 13:21:05 +0000 (16:21 +0300)
committerAmit Purwar <amit.purwar@samsung.com>
Mon, 29 Apr 2019 04:11:17 +0000 (09:41 +0530)
This adds advertise.discoverable-timeout command which can be used to
limit the amount of time the advertisement is discoverable:

[bluetooth]# advertise.discoverable on
[bluetooth]# advertise.discoverable-timeout 10
[bluetooth]# advertise on

@ MGMT Command: Add Advertising (0x003e) plen 14
        Instance: 1
        Flags: 0x00000001
          Switch into Connectable mode
        Duration: 0
        Timeout: 0
        Advertising data length: 3
        Flags: 0x02
          LE General Discoverable Mode
        Scan response length: 0
@ MGMT Event: Advertising Added (0x0023) plen 1
        Instance: 1
@ MGMT Event: Command Complete (0x0001) plen 4
      Add Advertising (0x003e) plen 1
        Status: Success (0x00)
        Instance: 1
@ MGMT Command: Add Advertising (0x003e) plen 14
        Instance: 1
        Flags: 0x00000001
          Switch into Connectable mode
        Duration: 0
        Timeout: 0
        Advertising data length: 3
        Flags: 0x00
        Scan response length: 0

Change-Id: I4d4a936d529355d5ae0092b84e7e62d0c174bf61
Signed-off-by: Amit Purwar <amit.purwar@samsung.com>
client/advertising.c
client/advertising.h
client/main.c

index 0546c89..65b8ffb 100644 (file)
@@ -66,6 +66,7 @@ static struct ad {
        uint16_t local_appearance;
        uint16_t duration;
        uint16_t timeout;
+       uint16_t discoverable_to;
        char **uuids;
        size_t uuids_len;
        struct service_data service;
@@ -424,6 +425,21 @@ static gboolean get_discoverable(const GDBusPropertyTable *property,
        return TRUE;
 }
 
+static gboolean discoverable_timeout_exits(const GDBusPropertyTable *property,
+                                                       void *data)
+{
+       return ad.discoverable_to;
+}
+
+static gboolean get_discoverable_timeout(const GDBusPropertyTable *property,
+                                       DBusMessageIter *iter, void *user_data)
+{
+       dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16,
+                                                       &ad.discoverable_to);
+
+       return TRUE;
+}
+
 static const GDBusPropertyTable ad_props[] = {
        { "Type", "s", get_type },
        { "ServiceUUIDs", "as", get_uuids, NULL, uuids_exists },
@@ -432,6 +448,8 @@ static const GDBusPropertyTable ad_props[] = {
                                                manufacturer_data_exists },
        { "Data", "a{yv}", get_data, NULL, data_exists },
        { "Discoverable", "b", get_discoverable, NULL, discoverable_exists },
+       { "DiscoverableTimeout", "q", get_discoverable_timeout, NULL,
+                                               discoverable_timeout_exits },
        { "Includes", "as", get_includes, NULL, includes_exists },
        { "LocalName", "s", get_local_name, NULL, local_name_exits },
        { "Appearance", "q", get_appearance, NULL, appearance_exits },
@@ -750,6 +768,26 @@ void ad_advertise_discoverable(DBusConnection *conn, dbus_bool_t *value)
        return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
+void ad_advertise_discoverable_timeout(DBusConnection *conn, long int *value)
+{
+       if (!value) {
+               if (ad.discoverable_to)
+                       bt_shell_printf("Timeout: %u sec\n",
+                                       ad.discoverable_to);
+               return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+       }
+
+       if (ad.discoverable_to == *value)
+               return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+
+       ad.discoverable_to = *value;
+
+       g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE,
+                                       "DiscoverableTimeout");
+
+       return bt_shell_noninteractive_quit(EXIT_SUCCESS);
+}
+
 void ad_advertise_tx_power(DBusConnection *conn, dbus_bool_t *value)
 {
        if (!value) {
index 5991908..fe3a7c8 100755 (executable)
@@ -40,3 +40,4 @@ void ad_advertise_timeout(DBusConnection *conn, long int *value);
 void ad_advertise_data(DBusConnection *conn, int argc, char *argv[]);
 void ad_disable_data(DBusConnection *conn);
 void ad_advertise_discoverable(DBusConnection *conn, dbus_bool_t *value);
+void ad_advertise_discoverable_timeout(DBusConnection *conn, long int *value);
index 228ceb6..e25a487 100644 (file)
@@ -2234,6 +2234,25 @@ static void cmd_advertise_discoverable(int argc, char *argv[])
        ad_advertise_discoverable(dbus_conn, &discoverable);
 }
 
+static void cmd_advertise_discoverable_timeout(int argc, char *argv[])
+{
+       long int value;
+       char *endptr = NULL;
+
+       if (argc < 2) {
+               ad_advertise_discoverable_timeout(dbus_conn, NULL);
+               return;
+       }
+
+       value = strtol(argv[1], &endptr, 0);
+       if (!endptr || *endptr != '\0' || value > UINT16_MAX) {
+               bt_shell_printf("Invalid argument\n");
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+       }
+
+       ad_advertise_discoverable_timeout(dbus_conn, &value);
+}
+
 static void cmd_advertise_tx_power(int argc, char *argv[])
 {
        dbus_bool_t powered;
@@ -2426,6 +2445,9 @@ static const struct bt_shell_menu advertise_menu = {
                        "Set/Get advertise data" },
        { "discoverable", "[on/off]", cmd_advertise_discoverable,
                        "Set/Get advertise discoverable" },
+       { "discoverable-timeout", "[seconds]",
+                       cmd_advertise_discoverable_timeout,
+                       "Set/Get advertise discoverable timeout" },
        { "tx-power", "[on/off]", cmd_advertise_tx_power,
                        "Show/Enable/Disable TX power to be advertised",
                                                        NULL },