2020-11-30 23:03:22

by Sonny Sasaka

[permalink] [raw]
Subject: [PATCH BlueZ v5 6/7] adapter: Add a public function to find a device by path

The public function is motivated by the Battery Provider API code which
needs to probe whether a device exists.

Reviewed-by: Miao-chen Chou <[email protected]>

---
src/adapter.c | 33 ++++++++++++++++++++++++---------
src/adapter.h | 2 ++
2 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 56d0c6eaa..03d9d29e9 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -872,6 +872,30 @@ struct btd_device *btd_adapter_find_device(struct btd_adapter *adapter,
return device;
}

+static int device_path_cmp(gconstpointer a, gconstpointer b)
+{
+ const struct btd_device *device = a;
+ const char *path = b;
+ const char *dev_path = device_get_path(device);
+
+ return strcasecmp(dev_path, path);
+}
+
+struct btd_device *btd_adapter_find_device_by_path(struct btd_adapter *adapter,
+ const char *path)
+{
+ GSList *list;
+
+ if (!adapter)
+ return NULL;
+
+ list = g_slist_find_custom(adapter->devices, path, device_path_cmp);
+ if (!list)
+ return NULL;
+
+ return list->data;
+}
+
static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
{
if (uuid->type == SDP_UUID16)
@@ -3192,15 +3216,6 @@ static gboolean property_get_roles(const GDBusPropertyTable *property,
return TRUE;
}

-static int device_path_cmp(gconstpointer a, gconstpointer b)
-{
- const struct btd_device *device = a;
- const char *path = b;
- const char *dev_path = device_get_path(device);
-
- return strcasecmp(dev_path, path);
-}
-
static DBusMessage *remove_device(DBusConnection *conn,
DBusMessage *msg, void *user_data)
{
diff --git a/src/adapter.h b/src/adapter.h
index e5750a37b..60b5e3bcc 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -83,6 +83,8 @@ sdp_list_t *btd_adapter_get_services(struct btd_adapter *adapter);
struct btd_device *btd_adapter_find_device(struct btd_adapter *adapter,
const bdaddr_t *dst,
uint8_t dst_type);
+struct btd_device *btd_adapter_find_device_by_path(struct btd_adapter *adapter,
+ const char *path);

const char *adapter_get_path(struct btd_adapter *adapter);
const bdaddr_t *btd_adapter_get_address(struct btd_adapter *adapter);
--
2.26.2


2020-12-01 16:29:29

by Bastien Nocera

[permalink] [raw]
Subject: Re: [PATCH BlueZ v5 6/7] adapter: Add a public function to find a device by path

On Mon, 2020-11-30 at 13:56 -0800, Sonny Sasaka wrote:
> The public function is motivated by the Battery Provider API code
> which
> needs to probe whether a device exists.
>
> Reviewed-by: Miao-chen Chou <[email protected]>
>
> ---
>  src/adapter.c | 33 ++++++++++++++++++++++++---------
>  src/adapter.h |  2 ++
>  2 files changed, 26 insertions(+), 9 deletions(-)
>
> diff --git a/src/adapter.c b/src/adapter.c
> index 56d0c6eaa..03d9d29e9 100644
> --- a/src/adapter.c
> +++ b/src/adapter.c
> @@ -872,6 +872,30 @@ struct btd_device
> *btd_adapter_find_device(struct btd_adapter *adapter,
>         return device;
>  }
>  
> +static int device_path_cmp(gconstpointer a, gconstpointer b)
> +{
> +       const struct btd_device *device = a;
> +       const char *path = b;
> +       const char *dev_path = device_get_path(device);
> +
> +       return strcasecmp(dev_path, path);
> +}
> +
> +struct btd_device *btd_adapter_find_device_by_path(struct
> btd_adapter *adapter,
> +                                                  const char *path)
> +{
> +       GSList *list;
> +
> +       if (!adapter)
> +               return NULL;
> +
> +       list = g_slist_find_custom(adapter->devices, path,
> device_path_cmp);
> +       if (!list)
> +               return NULL;
> +
> +       return list->data;
> +}
> +
>  static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
>  {
>         if (uuid->type == SDP_UUID16)
> @@ -3192,15 +3216,6 @@ static gboolean property_get_roles(const
> GDBusPropertyTable *property,
>         return TRUE;
>  }
>  
> -static int device_path_cmp(gconstpointer a, gconstpointer b)
> -{
> -       const struct btd_device *device = a;
> -       const char *path = b;
> -       const char *dev_path = device_get_path(device);
> -
> -       return strcasecmp(dev_path, path);
> -}

You should move the function in a separate patch, ideally. This is
helpful to show that the function was not modified in any way.

>  static DBusMessage *remove_device(DBusConnection *conn,
>                                         DBusMessage *msg, void
> *user_data)
>  {
> diff --git a/src/adapter.h b/src/adapter.h
> index e5750a37b..60b5e3bcc 100644
> --- a/src/adapter.h
> +++ b/src/adapter.h
> @@ -83,6 +83,8 @@ sdp_list_t *btd_adapter_get_services(struct
> btd_adapter *adapter);
>  struct btd_device *btd_adapter_find_device(struct btd_adapter
> *adapter,
>                                                         const
> bdaddr_t *dst,
>                                                         uint8_t
> dst_type);
> +struct btd_device *btd_adapter_find_device_by_path(struct
> btd_adapter *adapter,
> +                                                  const char *path);
>  
>  const char *adapter_get_path(struct btd_adapter *adapter);
>  const bdaddr_t *btd_adapter_get_address(struct btd_adapter
> *adapter);