From 368207af89c86964df48cfa5b682183b04f6f0cb Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Wed, 12 May 2021 17:18:13 -0700 Subject: [PATCH] client/advertising: Add support for setting min/max intervals This adds support for setting MinInterval/MaxInterval. Signed-off-by: Anuj Jain Signed-off-by: Ayush Garg --- client/advertising.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ client/advertising.h | 1 + client/main.c | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/client/advertising.c b/client/advertising.c index d63e48e..921e8da 100644 --- a/client/advertising.c +++ b/client/advertising.c @@ -51,6 +51,8 @@ static struct ad { char *type; char *local_name; char *secondary; + uint32_t min_interval; + uint32_t max_interval; uint16_t local_appearance; uint16_t duration; uint16_t timeout; @@ -181,6 +183,10 @@ static void print_ad(void) if (ad.timeout) bt_shell_printf("Timeout: %u sec\n", ad.timeout); + + if (ad.min_interval) + bt_shell_printf("Interval: %u-%u msec\n", ad.min_interval, + ad.max_interval); } static void register_reply(DBusMessage *message, void *user_data) @@ -444,6 +450,36 @@ static gboolean get_secondary(const GDBusPropertyTable *property, return TRUE; } +static gboolean min_interval_exits(const GDBusPropertyTable *property, + void *data) +{ + return ad.min_interval; +} + +static gboolean get_min_interval(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, + &ad.min_interval); + + return TRUE; +} + +static gboolean max_interval_exits(const GDBusPropertyTable *property, + void *data) +{ + return ad.max_interval; +} + +static gboolean get_max_interval(const GDBusPropertyTable *property, + DBusMessageIter *iter, void *user_data) +{ + dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, + &ad.max_interval); + + return TRUE; +} + static const GDBusPropertyTable ad_props[] = { { "Type", "s", get_type }, { "ServiceUUIDs", "as", get_uuids, NULL, uuids_exists }, @@ -459,6 +495,8 @@ static const GDBusPropertyTable ad_props[] = { { "Appearance", "q", get_appearance, NULL, appearance_exits }, { "Duration", "q", get_duration, NULL, duration_exits }, { "Timeout", "q", get_timeout, NULL, timeout_exits }, + { "MinInterval", "u", get_min_interval, NULL, min_interval_exits }, + { "MaxInterval", "u", get_max_interval, NULL, max_interval_exits }, { "SecondaryChannel", "s", get_secondary, NULL, secondary_exits }, { } }; @@ -950,3 +988,27 @@ void ad_advertise_secondary(DBusConnection *conn, const char *value) return bt_shell_noninteractive_quit(EXIT_SUCCESS); } + +void ad_advertise_interval(DBusConnection *conn, uint32_t *min, uint32_t *max) +{ + if (!min && !max) { + if (ad.min_interval && ad.max_interval) + bt_shell_printf("Interval: %u-%u msec\n", + ad.min_interval, ad.max_interval); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } + + if (ad.min_interval != *min) { + ad.min_interval = *min; + g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, + "MinInterval"); + } + + if (ad.max_interval != *max) { + ad.max_interval = *max; + g_dbus_emit_property_changed(conn, AD_PATH, AD_IFACE, + "MaxInterval"); + } + + return bt_shell_noninteractive_quit(EXIT_SUCCESS); +} diff --git a/client/advertising.h b/client/advertising.h index 25e6aee..472396e 100755 --- a/client/advertising.h +++ b/client/advertising.h @@ -29,3 +29,4 @@ 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); void ad_advertise_secondary(DBusConnection *conn, const char *value); +void ad_advertise_interval(DBusConnection *conn, uint32_t *min, uint32_t *max); diff --git a/client/main.c b/client/main.c index 39619aa..65acccf 100644 --- a/client/main.c +++ b/client/main.c @@ -2631,6 +2631,40 @@ static void cmd_advertise_secondary(int argc, char *argv[]) ad_advertise_secondary(dbus_conn, argv[1]); } +static void cmd_advertise_interval(int argc, char *argv[]) +{ + uint32_t min, max; + char *endptr = NULL; + + if (argc < 2) { + ad_advertise_interval(dbus_conn, NULL, NULL); + return; + } + + min = strtol(argv[1], &endptr, 0); + if (!endptr || *endptr != '\0' || min < 20 || min > 10485) { + bt_shell_printf("Invalid argument\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + max = min; + + if (argc > 2) { + max = strtol(argv[1], &endptr, 0); + if (!endptr || *endptr != '\0' || max < 20 || max > 10485) { + bt_shell_printf("Invalid argument\n"); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + } + + if (min > max) { + bt_shell_printf("Invalid argument: %u > %u\n", min, max); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + ad_advertise_interval(dbus_conn, &min, &max); +} + static void ad_clear_uuids(void) { ad_disable_uuids(dbus_conn); @@ -2689,6 +2723,14 @@ static void ad_clear_secondary(void) ad_advertise_secondary(dbus_conn, value); } +static void ad_clear_interval(void) +{ + uint32_t min = 0; + uint32_t max = 0; + + ad_advertise_interval(dbus_conn, &min, &max); +} + static const struct clear_entry ad_clear[] = { { "uuids", ad_clear_uuids }, { "service", ad_clear_service }, @@ -2700,6 +2742,7 @@ static const struct clear_entry ad_clear[] = { { "duration", ad_clear_duration }, { "timeout", ad_clear_timeout }, { "secondary", ad_clear_secondary }, + { "interval", ad_clear_interval }, {} }; @@ -2818,6 +2861,8 @@ static const struct bt_shell_menu advertise_menu = { "Set/Get advertise timeout" }, { "secondary", "[1M/2M/Coded]", cmd_advertise_secondary, "Set/Get advertise secondary channel" }, + { "interval", "[min] [max] ", cmd_advertise_interval, + "Set/Get advertise interval range" }, { "clear", "[uuids/service/manufacturer/config-name...]", cmd_ad_clear, "Clear advertise config" }, { } }, -- 2.7.4