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_MANAGER_IFACE BLUEZ_PREFIX ".Manager"
32 #define BLUEZ_ADAPTER_IFACE BLUEZ_PREFIX ".Adapter"
33 #define BLUEZ_DEVICE_IFACE BLUEZ_PREFIX ".Device"
36 using namespace DeviceAPI::Common;
41 BluetoothDevice::BluetoothDevice(bt_adapter_device_discovery_info_s *discoveryInfo)
43 mName = std::string(discoveryInfo->remote_name);
44 mAddress = std::string(discoveryInfo->remote_address);
45 mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(discoveryInfo->bt_class));
46 for(int i = 0; i < discoveryInfo->service_count; i++) {
47 mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
52 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo)
54 mName = std::string(deviceInfo->remote_name);
55 mAddress = std::string(deviceInfo->remote_address);
56 mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(deviceInfo->bt_class));
57 for(int i = 0; i < deviceInfo->service_count; i++) {
58 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
63 BluetoothDevice::~BluetoothDevice()
65 BluetoothAdapter::getInstance()->removeConnReq(mAddress);
68 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
70 mName = std::string(deviceInfo->remote_name);
72 for(int i = 0; i < deviceInfo->service_count; i++) {
73 mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
78 std::string BluetoothDevice::getName() const
83 std::string BluetoothDevice::getAddress() const
88 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
90 return JSBluetoothClass::createJSObject(context, mDeviceClass);
93 gchar* BluetoothDevice::getDefaultAdapter() const
96 GVariant *reply = NULL;
97 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
104 G_DBUS_CALL_FLAGS_NONE,
110 LoggerE("Failed to get default adapter: " << err->message);
114 LoggerE("Reply from 'DefaultAdapter' is null");
118 char *adapter = NULL;
119 g_variant_get(reply, "(o)", &adapter);
121 // make a copy of adapter, 'cause it will be destroyed when 'reply' is un-refed
122 char *result = adapter?strdup(adapter):NULL;
124 g_variant_unref(reply);
129 char *BluetoothDevice::getDevice() const
131 if(!strcmp(mAddress.c_str(), "")) {
132 LoggerE("Invalid address");
136 char *adapter = getDefaultAdapter();
138 LoggerE("Failed to get default adapter");
143 GVariant *reply = NULL;
144 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
149 g_variant_new("(s)", mAddress.c_str()),
151 G_DBUS_CALL_FLAGS_NONE,
157 //LoggerE("Failed to find " << mAddress << " device: " << (err?err->message:"Invalid reply"));
165 g_variant_get(reply, "(o)", &device);
167 // make a copy of adapter, 'cause it will be destroyed when 'reply' is un-refed
168 char *result = device?strdup(device):NULL;
170 g_variant_unref(reply);
176 bool BluetoothDevice::isBonded() const
178 char *device = getDevice();
180 //LoggerE("Failed to get device object");
185 GVariant *reply = NULL;
186 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
193 G_DBUS_CALL_FLAGS_NONE,
206 g_variant_get(reply, "(a{sv})", &iter);
210 while(g_variant_iter_next(iter, "{sv}", &key, &value)) {
211 if(!strcmp(key, "Paired")) {
212 paired = g_variant_get_boolean(value);
217 g_variant_unref(reply);
223 bool BluetoothDevice::isTrusted() const
225 char *device = getDevice();
227 //LoggerE("Failed to get device object");
232 GVariant *reply = NULL;
233 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
240 G_DBUS_CALL_FLAGS_NONE,
253 g_variant_get(reply, "(a{sv})", &iter);
256 bool trusted = false;
257 while(g_variant_iter_next(iter, "{sv}", &key, &value)) {
258 if(!strcmp(key, "Trusted")) {
259 trusted = g_variant_get_boolean(value);
264 g_variant_unref(reply);
270 bool BluetoothDevice::isConnected() const
272 char *device = getDevice();
274 //LoggerE("Failed to get device object");
279 GVariant *reply = NULL;
280 reply = g_dbus_connection_call_sync( g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL),
287 G_DBUS_CALL_FLAGS_NONE,
300 g_variant_get(reply, "(a{sv})", &iter);
303 bool connected = false;
304 while(g_variant_iter_next(iter, "{sv}", &key, &value)) {
305 if(!strcmp(key, "Connected")) {
306 connected = g_variant_get_boolean(value);
311 g_variant_unref(reply);
317 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
319 return JSUtil::toJSValueRef(context, mUUIDs);