2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include <PlatformException.h>
21 #include "plugin_config.h"
22 #include "BluetoothDevice.h"
23 #include "BluetoothAdapter.h"
24 #include "JSBluetoothClass.h"
29 #define BLUEZ_PREFIX "org.bluez"
30 #define BLUEZ_SERVICE BLUEZ_PREFIX
31 #define BLUEZ_ADAPTER_IFACE BLUEZ_PREFIX ".Adapter1"
32 #define BLUEZ_DEVICE_IFACE BLUEZ_PREFIX ".Device1"
35 using namespace DeviceAPI::Common;
42 BluetoothDevice::BluetoothDevice(bt_adapter_device_discovery_info_s *discoveryInfo, char *devicePath)
44 mName = std::string(discoveryInfo->remote_name);
45 mAddress = std::string(discoveryInfo->remote_address);
46 mDevicePath = devicePath?strdup(devicePath):NULL;
47 mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(discoveryInfo->bt_class));
48 for(int i = 0; i < discoveryInfo->service_count; i++)
50 mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
55 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo, char *devicePath)
57 mName = std::string(deviceInfo->remote_name);
58 mAddress = std::string(deviceInfo->remote_address);
59 mDevicePath = devicePath?strdup(devicePath):NULL;
60 mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(deviceInfo->bt_class));
61 for(int i = 0; i < deviceInfo->service_count; i++)
63 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
68 BluetoothDevice::~BluetoothDevice()
70 BluetoothAdapter::getInstance()->removeConnReq(mAddress);
74 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
76 mName = std::string(deviceInfo->remote_name);
78 for(int i = 0; i < deviceInfo->service_count; i++)
80 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
85 std::string BluetoothDevice::getName() const
90 std::string BluetoothDevice::getAddress() const
95 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
97 return JSBluetoothClass::createJSObject(context, mDeviceClass);
100 gchar* BluetoothDevice::getDefaultAdapter() const
105 GError * error = nullptr;
106 GDBusProxy * managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
109 "org.freedesktop.DBus.ObjectManager",
113 LoggerE("could not create ObjManager proxy");
114 // DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<<error->message<<endl;
119 GVariant * objectMap = g_dbus_proxy_call_sync(managerProxy, "GetManagedObjects",nullptr, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
123 LoggerE("failed to get GetManagedObj");
124 //DebugOut(DebugOut::Error)<<"Failed call to GetManagedObjects: "<<error->message<<endl;
125 g_object_unref(managerProxy);
132 GVariantIter* level2Dict;
134 g_variant_get(objectMap, "(a{oa{sa{sv}}})",&iter);
136 while(g_variant_iter_next(iter, "{oa{sa{sv}}}",&objPath, &level2Dict))
138 char * interfaceName;
139 GVariantIter* innerDict;
140 while(g_variant_iter_next(level2Dict, "{sa{sv}}", &interfaceName, &innerDict))
142 if(!strcmp(interfaceName, "org.bluez.Adapter1"))
147 while(done == false && g_variant_iter_next(innerDict,"{sv}", &propertyName, &value))
149 if(!strcmp(propertyName, "Address"))
152 g_variant_get(value,"s",&addr);
154 result = objPath?strdup(objPath):NULL;
159 g_free(propertyName);
160 g_variant_unref(value);
163 g_free(interfaceName);
164 g_variant_iter_free(innerDict);
167 g_variant_iter_free(level2Dict);
169 g_variant_iter_free(iter);
174 char* BluetoothDevice::deviceFromAddress() const
179 GError * error = nullptr;
180 GDBusProxy * managerProxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
183 "org.freedesktop.DBus.ObjectManager",
187 LoggerE("could not create ObjManager proxy");
188 // DebugOut(DebugOut::Error)<<"Could not create ObjectManager proxy for Bluez: "<<error->message<<endl;
193 GVariant * objectMap = g_dbus_proxy_call_sync(managerProxy, "GetManagedObjects",nullptr, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
197 LoggerE("failed to get GetManagedObj");
198 //DebugOut(DebugOut::Error)<<"Failed call to GetManagedObjects: "<<error->message<<endl;
199 g_object_unref(managerProxy);
206 GVariantIter* level2Dict;
208 g_variant_get(objectMap, "(a{oa{sa{sv}}})",&iter);
210 while(g_variant_iter_next(iter, "{oa{sa{sv}}}",&objPath, &level2Dict))
212 char * interfaceName;
213 GVariantIter* innerDict;
214 while(g_variant_iter_next(level2Dict, "{sa{sv}}", &interfaceName, &innerDict))
216 if(!strcmp(interfaceName, "org.bluez.Device1"))
221 while(done == false && g_variant_iter_next(innerDict,"{sv}", &propertyName, &value))
223 if(!strcmp(propertyName, "Address"))
226 g_variant_get(value,"s",&addr);
228 if(addr && std::string(addr) == mAddress)
230 result = objPath?strdup(objPath):NULL;
236 g_free(propertyName);
237 g_variant_unref(value);
240 g_free(interfaceName);
241 g_variant_iter_free(innerDict);
244 g_variant_iter_free(level2Dict);
246 g_variant_iter_free(iter);
252 char *BluetoothDevice::getDevice() const
254 if(!strcmp(mAddress.c_str(), ""))
256 LoggerE("Invalid address");
260 char *device = deviceFromAddress();
261 char *result = device?strdup(device):NULL;
263 char *adapter = getDefaultAdapter();
265 LoggerE("Failed to get default adapter");
270 GVariant *reply = NULL;
271 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
276 g_variant_new("(s)", mAddress.c_str()),
278 G_DBUS_CALL_FLAGS_NONE,
284 //LoggerE("Failed to find " << mAddress << " device: " << (err?err->message:"Invalid reply"));
292 g_variant_get(reply, "(o)", &device);
294 // make a copy of adapter, 'cause it will be destroyed when 'reply' is un-refed
295 char *result = device?strdup(device):NULL;
297 g_variant_unref(reply);
303 bool BluetoothDevice::isBonded() const
305 char *device = getDevice();
312 GVariant *reply = NULL;
313 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
316 "org.freedesktop.DBus.Properties",
318 g_variant_new("(ss)", "org.bluez.Device1", "Paired"),
320 G_DBUS_CALL_FLAGS_NONE,
334 g_variant_get(reply, "(v)", &value);
335 paired = g_variant_get_boolean(value);
337 g_variant_unref(reply);
342 bool BluetoothDevice::isTrusted() const
344 char *device = getDevice();
347 //LoggerE("Failed to get device object");
352 GVariant *reply = NULL;
353 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
356 "org.freedesktop.DBus.Properties",
358 g_variant_new("(ss)", "org.bluez.Device1", "Trusted"),
360 G_DBUS_CALL_FLAGS_NONE,
372 bool trusted = false;
374 g_variant_get(reply, "(v)", &value);
375 trusted = g_variant_get_boolean(value);
377 g_variant_unref(reply);
384 bool BluetoothDevice::isConnected() const
386 char *device = getDevice();
393 GVariant *reply = NULL;
394 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
397 "org.freedesktop.DBus.Properties",
399 g_variant_new("(ss)", "org.bluez.Device1", "Connected"),
401 G_DBUS_CALL_FLAGS_NONE,
413 bool connected = false;
415 g_variant_get(reply, "(v)", &value);
416 connected = g_variant_get_boolean(value);
418 g_variant_unref(reply);
424 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
426 return JSUtil::toJSValueRef(context, mUUIDs);