From cfd9944c77a61215250c3b0f81978a1236d4c590 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Mon, 24 Sep 2018 14:40:32 +0800 Subject: binding: bluetooth: return adapter/device versus bluez path To allow this binding API to be bluetooth stack agnostic remove pure bluez paths, and replace with generic fields. Bug-AGL: SPEC-1630 Change-Id: Iadba73782b6339df2ca5937e16a654e7ef3e477b Signed-off-by: Matt Ranostay --- README.md | 21 +++++++++++++-------- binding/bluetooth-agent.c | 3 ++- binding/bluetooth-api.c | 11 +++++------ binding/bluetooth-api.h | 32 +++++++++++++++++++++++++++++--- binding/bluetooth-bluez.c | 14 ++++++++++---- binding/bluetooth-common.h | 2 ++ binding/bluetooth-util.c | 12 ++++++++++++ 7 files changed, 73 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ed9deeb..872daa8 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ This verb allows an client to get initial paired devices, and discovered unpaire "response": { "adapters": [ { - "path": "/org/bluez/hci0", + "name": "hci0", "properties": { "address": "00:1A:7D:DA:71:0F", "powered": true, @@ -56,7 +56,8 @@ This verb allows an client to get initial paired devices, and discovered unpaire ], "devices": [ { - "path": "/org/bluez/hci0/dev_F8_34_41_DA_BA_46", + "adapter": "hci0", + "device": "dev_F8_34_41_DA_BA_46", "properties": { "address": "F8:34:41:DA:BA:46", "name": "roguebox", @@ -91,7 +92,8 @@ This verb allows an client to get initial paired devices, and discovered unpaire } }, { - "path": "/org/bluez/hci0/dev_67_13_E2_57_29_0F", + "adapter": "hci0", + "device": "dev_67_13_E2_57_29_0F", "properties": { "address": "68:13:E2:57:29:0F", "alias": "67-13-E2-57-29-0F", @@ -116,7 +118,7 @@ adapter_state verb allows setting and retrieving of requested adapter settings: | Name | Description | |-----------------|------------------------------------------------------------------| -| adapter | Must be the bluez path to the adapter (i.e. /org/bluez/hci0) | +| adapter | Must be the name of the adapter (i.e. hci0) | | discovery | Discover nearby broadcasting devices | | discoverable | Allow other devices to detect this device | | powered | Adapter power state (optional, rfkill should be disabled already | @@ -152,7 +154,8 @@ Sample of discovering a new device event:
 {
-  "device": "/org/bluez/hci0/dev_88_0F_10_96_D3_20",
+  "adapter": "hci0",
+  "device": "dev_88_0F_10_96_D3_20",
   "action": "added",
   "properties": {
     "address": "88:0F:10:96:D3:20",
@@ -184,11 +187,12 @@ Sample of discovering a new device event:
 }
 
-Changed status events for a deivce: +Changed status events for a device:
 {
-  "path": "/org/bluez/hci0/dev_88_0F_10_96_D3_20",
+  "adapter": "hci0",
+  "device": "dev_88_0F_10_96_D3_20",
   "action": "changed",
   "properties": {
     "connected": true
@@ -202,8 +206,9 @@ After pairing request agent will send event for a pincode that must be confirmed
 
 
 {
+  "adapter": "hci0",
+  "device": "dev_88_OF_10_96_D3_20",
   "action": "request_confirmation",
-  "device": "/org/bluez/hci0/dev_88_OF_10_96_D3_20",
   "pincode": 327142
 }
 
diff --git a/binding/bluetooth-agent.c b/binding/bluetooth-agent.c index b6ae5fe..a942baa 100644 --- a/binding/bluetooth-agent.c +++ b/binding/bluetooth-agent.c @@ -101,7 +101,8 @@ static void handle_method_call( jev = json_object_new_object(); json_object_object_add(jev, "action", json_object_new_string("request_confirmation")); - json_object_object_add(jev, "device", json_object_new_string(path)); + + json_process_path(jev, path); json_object_object_add(jev, "pincode", json_object_new_int(pin)); cw->agent_data.pin_code = pin; diff --git a/binding/bluetooth-api.c b/binding/bluetooth-api.c index 28ac6fb..4649067 100644 --- a/binding/bluetooth-api.c +++ b/binding/bluetooth-api.c @@ -262,8 +262,7 @@ static void bluez_devices_signal_callback( jresp = json_object_new_object(); - json_object_object_add(jresp, "device", - json_object_new_string(path)); + json_process_path(jresp, path); json_object_object_add(jresp, "action", json_object_new_string("added")); @@ -307,8 +306,7 @@ static void bluez_devices_signal_callback( jresp = json_object_new_object(); - json_object_object_add(jresp, "path", - json_object_new_string(path)); + json_process_path(jresp, path); json_object_object_add(jresp, "action", json_object_new_string("removed")); @@ -321,8 +319,7 @@ static void bluez_devices_signal_callback( jresp = json_object_new_object(); - json_object_object_add(jresp, "path", - json_object_new_string(object_path)); + json_process_path(jresp, object_path); json_object_object_add(jresp, "action", json_object_new_string("changed")); @@ -609,6 +606,7 @@ static void bluetooth_state(afb_req_t request) afb_req_fail(request, "failed", "No adapter give to return state"); return; } + adapter = BLUEZ_ROOT_PATH(adapter); jresp = adapter_properties(ns, &error, adapter); if (!jresp) { @@ -631,6 +629,7 @@ static void bluetooth_adapter(afb_req_t request) afb_req_fail(request, "failed", "No adapter given to configure"); return; } + adapter = BLUEZ_ROOT_PATH(adapter); scan = afb_req_value(request, "discovery"); if (scan) { diff --git a/binding/bluetooth-api.h b/binding/bluetooth-api.h index 50bf501..d8df0af 100644 --- a/binding/bluetooth-api.h +++ b/binding/bluetooth-api.h @@ -37,6 +37,18 @@ #define BLUEZ_OBJECT_PATH "/" #define BLUEZ_PATH "/org/bluez" +#define BLUEZ_ROOT_PATH(_t) \ + ({ \ + const char *__t = (_t); \ + size_t __len = strlen(BLUEZ_PATH) + 1 + \ + strlen(__t) + 1; \ + char *__tpath; \ + __tpath = alloca(__len + 1 + 1); \ + snprintf(__tpath, __len + 1, \ + BLUEZ_PATH "/%s", __t); \ + __tpath; \ + }) + #define FREEDESKTOP_INTROSPECT "org.freedesktop.DBus.Introspectable" #define FREEDESKTOP_PROPERTIES "org.freedesktop.DBus.Properties" #define FREEDESKTOP_OBJECTMANAGER "org.freedesktop.DBus.ObjectManager" @@ -52,7 +64,22 @@ struct bluetooth_state; -static inline const char *bluez_strip_path(const char *path) +static inline gchar *bluez_return_adapter(const char *path) +{ + gchar **strings = g_strsplit(path, "/", -1); + gchar *adapter; + + if (g_strv_length(strings) < 3) { + g_strfreev(strings); + return NULL; + } + adapter = g_strdup(strings[3]); + g_strfreev(strings); + + return adapter; +} + +static inline gchar *bluez_return_device(const char *path) { const char *basename; @@ -61,10 +88,9 @@ static inline const char *bluez_strip_path(const char *path) return NULL; basename++; /* at least one character */ - return *basename ? basename : NULL; + return *basename ? g_strdup(basename) : NULL; } - struct call_work *call_work_create_unlocked(struct bluetooth_state *ns, const char *access_type, const char *type_arg, const char *method, const char *bluez_method, diff --git a/binding/bluetooth-bluez.c b/binding/bluetooth-bluez.c index 990a244..7640046 100644 --- a/binding/bluetooth-bluez.c +++ b/binding/bluetooth-bluez.c @@ -372,22 +372,28 @@ json_object *bluez_get_properties(struct bluetooth_state *ns, while (g_variant_iter_loop(array2, "{&sa{sv}}", &interface, &array3)) { json_object *array = NULL; + gchar *tmp = NULL; + + jtype = json_object_new_object(); if (!strcmp(interface, BLUEZ_ADAPTER_INTERFACE)) { access_type = BLUEZ_AT_ADAPTER; array = jarray; + + tmp = bluez_return_adapter(path2); + json_object_object_add(jtype, "name", json_object_new_string(tmp)); + g_free(tmp); } else if (!strcmp(interface, BLUEZ_DEVICE_INTERFACE)) { access_type = BLUEZ_AT_DEVICE; array = jarray2; + + json_process_path(jtype, path2); } else { + json_object_put(jtype); continue; /* TODO: Maybe display other interfaces */ } pi = bluez_get_property_info(access_type, error); - jtype = json_object_new_object(); - - json_object_object_add(jtype, "path", - json_object_new_string(path2)); while (g_variant_iter_loop(array3, "{sv}", &key, &var)) { if (!jprop) diff --git a/binding/bluetooth-common.h b/binding/bluetooth-common.h index 159c5f4..faa9558 100644 --- a/binding/bluetooth-common.h +++ b/binding/bluetooth-common.h @@ -110,6 +110,8 @@ gchar *key_dbus_to_json(const gchar *key, gboolean auto_lower); json_object *simple_gvariant_to_json(GVariant *var, json_object *parent, gboolean recurse); +void json_process_path(json_object *jresp, const char *path); + /** * Structure for converting from dbus properties to json * and vice-versa. diff --git a/binding/bluetooth-util.c b/binding/bluetooth-util.c index 0af4898..e826aaf 100644 --- a/binding/bluetooth-util.c +++ b/binding/bluetooth-util.c @@ -1024,3 +1024,15 @@ json_object *get_named_property(const struct property_info *pi, return jret; } + +void json_process_path(json_object *jresp, const char *path) { + gchar *tmp; + + tmp = bluez_return_adapter(path); + json_object_object_add(jresp, "adapter", json_object_new_string(tmp)); + g_free(tmp); + + tmp = bluez_return_device(path); + json_object_object_add(jresp, "device", json_object_new_string(tmp)); + g_free(tmp); +} -- cgit 1.2.3-korg