wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / BluetoothDevice.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 #include <PlatformException.h>
19 #include <JSUtil.h>
20
21 #include "plugin_config.h"
22 #include "BluetoothDevice.h"
23 #include "BluetoothAdapter.h"
24 #include "JSBluetoothClass.h"
25
26 #include <Logger.h>
27
28 using namespace DeviceAPI::Common;
29
30 namespace DeviceAPI {
31 namespace Bluetooth {
32
33 BluetoothDevice::BluetoothDevice(bt_adapter_device_discovery_info_s *discoveryInfo)
34 {
35     LoggerD("Enter");
36
37     mName = std::string(discoveryInfo->remote_name);
38     mAddress = std::string(discoveryInfo->remote_address);
39     mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(discoveryInfo->bt_class));
40     for(int i = 0; i < discoveryInfo->service_count; i++) {
41         mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
42     }
43     isUpdated = true;
44 }
45
46 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo)
47 {
48     LoggerD("Enter");
49
50     mName = std::string(deviceInfo->remote_name);
51     mAddress = std::string(deviceInfo->remote_address);
52     mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(deviceInfo->bt_class));
53     for(int i = 0; i < deviceInfo->service_count; i++) {
54         mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
55     }
56     isUpdated = true;    
57 }
58
59 BluetoothDevice::~BluetoothDevice()
60 {
61     LoggerD("Enter");
62     BluetoothAdapter::getInstance()->removeConnReq(mAddress);
63 }
64
65 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
66 {
67     LoggerD("Enter");
68
69     mName = std::string(deviceInfo->remote_name);
70     mUUIDs.clear();
71     for(int i = 0; i < deviceInfo->service_count; i++) {
72         mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
73     }
74     isUpdated = true;
75 }
76
77 std::string BluetoothDevice::getName() const
78 {
79     LoggerD("Enter");
80     return mName;
81 }
82
83 std::string BluetoothDevice::getAddress() const
84 {
85     LoggerD("Enter");
86     return mAddress;
87 }
88
89 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
90 {
91     LoggerD("Enter");
92     /*
93     JSValueRef deviceClass = mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS);
94     if(deviceClass == NULL) {
95         deviceClass = JSBluetoothClass::createJSObject(context, mDeviceClass);
96         mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, deviceClass);
97     }
98     
99     return deviceClass;
100     */
101     return JSBluetoothClass::createJSObject(context, mDeviceClass);
102 }
103
104 bool BluetoothDevice::isBonded() const
105 {
106     LoggerD("Enter");
107
108     bool ret = false;
109     bt_device_info_s *deviceInfo = NULL;
110     if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
111         ret = deviceInfo->is_bonded;
112         bt_adapter_free_device_info(deviceInfo);
113     }
114     
115     return ret;
116 }
117
118 bool BluetoothDevice::isTrusted() const
119 {
120     LoggerD("Enter");
121
122     bool ret = false;
123     bt_device_info_s *deviceInfo = NULL;
124     if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
125         ret = deviceInfo->is_authorized;
126         bt_adapter_free_device_info(deviceInfo);
127     }
128     
129     return ret;
130 }
131
132 bool BluetoothDevice::isConnected() const
133 {
134     LoggerD("Enter");
135
136     bool ret = false;
137     bt_device_info_s *deviceInfo = NULL;
138     if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
139         ret = deviceInfo->is_connected;
140         bt_adapter_free_device_info(deviceInfo);
141     }
142     
143     return ret;
144 }
145
146 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
147 {
148     LoggerD("Enter");
149 /*
150     if(isUpdated == true) {
151         mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_UUIDS, JSUtil::toJSValueRef(context, mUUIDs));
152         isUpdated = false;
153     }
154     
155     return mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_UUIDS);
156 */
157     return JSUtil::toJSValueRef(context, mUUIDs);
158 }
159
160
161 } // Bluetooth
162 } // DeviceAPI