From d5d6a76ecdbc0157a52fc3b9b5602cf72e0471a7 Mon Sep 17 00:00:00 2001 From: Leandro Dorileo Date: Wed, 14 Aug 2013 17:10:36 -0300 Subject: [PATCH] [bluetooth] implement destroyBonding API --- bluetooth/bluetooth_api.js | 47 +++++++++++++++++++++++++++- bluetooth/bluetooth_context.cc | 2 ++ bluetooth/bluetooth_context.h | 3 ++ bluetooth/bluetooth_context_bluez4.cc | 58 +++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) diff --git a/bluetooth/bluetooth_api.js b/bluetooth/bluetooth_api.js index 61bf62b..9549475 100644 --- a/bluetooth/bluetooth_api.js +++ b/bluetooth/bluetooth_api.js @@ -544,7 +544,52 @@ BluetoothAdapter.prototype.createBonding = function(address, successCallback, er }); }; -BluetoothAdapter.prototype.destroyBonding = function(address, deviceSuccessCallback, errorCallback) {}; +BluetoothAdapter.prototype.destroyBonding = function(address, successCallback, errorCallback) { + if (adapter.serviceNotAvailable(errorCallback)) + return; + + if (typeof address !== 'string' + || (successCallback && typeof successCallback !== 'function') + || (errorCallback && typeof errorCallback !== 'function')) { + if (errorCallback) { + var error = new tizen.WebAPIError(tizen.WebAPIException.TYPE_MISMATCH_ERR); + errorCallback(error); + } + } + + var msg = { + 'cmd': 'DestroyBonding', + 'address': address + }; + + postMessage(msg, function(result) { + var cb_device; + + if (result.error != 0) { + if (errorCallback) { + var error_type = (result.error == 1) ? tizen.WebAPIException.NOT_FOUND_ERR : tizen.WebAPIException.UNKNOWN_ERR; + var error = new tizen.WebAPIError(error_type); + errorCallback(error); + } + + throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR); + return; + } + + if (successCallback) { + var known_devices = adapter.known_devices; + for (var i = 0; i < known_devices.length; i++) { + if (known_devices[i].address === address) { + cb_device = known_devices[i]; + break; + } + } + + successCallback(cb_device); + } + }); +}; + BluetoothAdapter.prototype.registerRFCOMMServiceByUUID = function(uuid, name, serviceSuccessCallback, errorCallback) {}; var _deviceClassMask = { diff --git a/bluetooth/bluetooth_context.cc b/bluetooth/bluetooth_context.cc index dd27db5..eb3a15e 100644 --- a/bluetooth/bluetooth_context.cc +++ b/bluetooth/bluetooth_context.cc @@ -63,6 +63,8 @@ void BluetoothContext::HandleMessage(const char* message) { HandleSetAdapterProperty(v); else if (cmd == "CreateBonding") HandleCreateBonding(v); + else if (cmd == "DestroyBonding") + HandleDestroyBonding(v); } void BluetoothContext::HandleSyncMessage(const char* message) { diff --git a/bluetooth/bluetooth_context.h b/bluetooth/bluetooth_context.h index 8eb0568..e2f3a8a 100644 --- a/bluetooth/bluetooth_context.h +++ b/bluetooth/bluetooth_context.h @@ -67,6 +67,7 @@ class BluetoothContext { picojson::value HandleGetDefaultAdapter(const picojson::value& msg); void HandleSetAdapterProperty(const picojson::value& msg); void HandleCreateBonding(const picojson::value& msg); + void HandleDestroyBonding(const picojson::value& msg); void PostMessage(picojson::value v); void SetSyncReply(picojson::value v); @@ -103,6 +104,8 @@ class BluetoothContext { G_CALLBACK_1(OnGotDefaultAdapterPath, GObject*, GAsyncResult*); G_CALLBACK_1(OnGotAdapterProperties, GObject*, GAsyncResult*); G_CALLBACK_1(OnAdapterCreateBonding, GObject*, GAsyncResult*); + G_CALLBACK_1(OnAdapterDestroyBonding, GObject*, GAsyncResult*); + G_CALLBACK_1(OnFoundDevice, GObject*, GAsyncResult*); G_CALLBACK_2(OnAdapterPropertySet, GObject*, GAsyncResult*); G_CALLBACK_1(OnDeviceProxyCreated, GObject*, GAsyncResult*); G_CALLBACK_1(OnGotDeviceProperties, GObject*, GAsyncResult*); diff --git a/bluetooth/bluetooth_context_bluez4.cc b/bluetooth/bluetooth_context_bluez4.cc index 37980ac..b8c537c 100644 --- a/bluetooth/bluetooth_context_bluez4.cc +++ b/bluetooth/bluetooth_context_bluez4.cc @@ -279,6 +279,54 @@ void BluetoothContext::OnAdapterCreateBonding(GObject*, GAsyncResult* res) { g_variant_unref(result); } +void BluetoothContext::OnAdapterDestroyBonding(GObject*, GAsyncResult* res) { + GError* error = 0; + GVariant* result = g_dbus_proxy_call_finish(adapter_proxy_, res, &error); + + picojson::value::object o; + o["cmd"] = picojson::value(""); + o["reply_id"] = picojson::value(callbacks_map_["DestroyBonding"]); + o["error"] = picojson::value(static_cast(0)); + + if (!result) { + g_printerr("\n\nError on destroying adapter bonding: %s\n", error->message); + g_error_free(error); + + o["error"] = picojson::value(static_cast(2)); + } + + PostMessage(picojson::value(o)); + callbacks_map_.erase("DestroyBonding"); + g_variant_unref(result); +} + +void BluetoothContext::OnFoundDevice(GObject*, GAsyncResult* res) { + picojson::value::object o; + char* object_path; + GError* error = 0; + GVariant* result = g_dbus_proxy_call_finish(adapter_proxy_, res, &error); + + if (!result) { + g_printerr("\n\nError on destroying adapter bonding: %s\n", error->message); + g_error_free(error); + + o["cmd"] = picojson::value(""); + o["reply_id"] = picojson::value(callbacks_map_["DestroyBonding"]); + o["error"] = picojson::value(static_cast(1)); + + PostMessage(picojson::value(o)); + callbacks_map_.erase("DestroyBonding"); + return; + } + + g_variant_get(result, "(o)", &object_path); + g_dbus_proxy_call(adapter_proxy_, "RemoveDevice", + g_variant_new("(o)", object_path), + G_DBUS_CALL_FLAGS_NONE, -1, NULL, OnAdapterDestroyBondingThunk, this); + + g_variant_unref(result); +} + BluetoothContext::~BluetoothContext() { delete api_; @@ -412,6 +460,16 @@ void BluetoothContext::HandleCreateBonding(const picojson::value& msg) { G_DBUS_CALL_FLAGS_NONE, -1, NULL, OnAdapterCreateBondingThunk, this); } +void BluetoothContext::HandleDestroyBonding(const picojson::value& msg) { + GError* error = 0; + std::string address = msg.get("address").to_str(); + callbacks_map_["DestroyBonding"] = msg.get("reply_id").to_str(); + + g_dbus_proxy_call(adapter_proxy_, "FindDevice", + g_variant_new("(s)", address.c_str()), + G_DBUS_CALL_FLAGS_NONE, -1, NULL, OnFoundDeviceThunk, this); +} + void BluetoothContext::OnDeviceProxyCreated(GObject* object, GAsyncResult* res) { GDBusProxy* device_proxy; GError* error = 0; -- 2.7.4