diff options
author | Matt Ranostay <matt.ranostay@konsulko.com> | 2018-09-24 14:40:32 +0800 |
---|---|---|
committer | Matt Ranostay <matt.ranostay@konsulko.com> | 2018-11-23 08:49:14 -0800 |
commit | cfd9944c77a61215250c3b0f81978a1236d4c590 (patch) | |
tree | b345083c1061d156b2bf1168d7474c419a18864a /binding | |
parent | dfff48e48f646e021e6d2b8326cd7fdc3d706d9c (diff) |
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 <matt.ranostay@konsulko.com>
Diffstat (limited to 'binding')
-rw-r--r-- | binding/bluetooth-agent.c | 3 | ||||
-rw-r--r-- | binding/bluetooth-api.c | 11 | ||||
-rw-r--r-- | binding/bluetooth-api.h | 32 | ||||
-rw-r--r-- | binding/bluetooth-bluez.c | 14 | ||||
-rw-r--r-- | binding/bluetooth-common.h | 2 | ||||
-rw-r--r-- | binding/bluetooth-util.c | 12 |
6 files changed, 60 insertions, 14 deletions
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); +} |