tizen 2.3.1 release
[framework/web/wearable/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 "BluetoothUtil.h"
25 #include "JSBluetoothClass.h"
26
27 #include <Logger.h>
28
29 using namespace DeviceAPI::Common;
30
31 namespace DeviceAPI {
32 namespace Bluetooth {
33
34 BluetoothDevice::BluetoothDevice(
35     bt_adapter_device_discovery_info_s *discoveryInfo)
36 {
37     Common::SecurityAccessor();
38     mName = std::string(discoveryInfo->remote_name);
39     mAddress = std::string(discoveryInfo->remote_address);
40     mDeviceClass = BluetoothClassSharedPtr(
41         new BluetoothClass(discoveryInfo->bt_class));
42
43     for(int i = 0; i < discoveryInfo->service_count; i++) {
44         mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
45     }
46     isUpdated = true;
47 }
48
49 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo)
50 {
51     Common::SecurityAccessor();
52     mName = std::string(deviceInfo->remote_name);
53     mAddress = std::string(deviceInfo->remote_address);
54     mDeviceClass = BluetoothClassSharedPtr(
55         new BluetoothClass(deviceInfo->bt_class));
56
57     for(int i = 0; i < deviceInfo->service_count; i++) {
58         mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
59     }
60     isUpdated = true;
61 }
62
63 BluetoothDevice::~BluetoothDevice()
64 {
65     BluetoothAdapter::getInstance()->removeConnReq(mAddress);
66 }
67
68 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
69 {
70     mName = std::string(deviceInfo->remote_name);
71     mUUIDs.clear();
72     for(int i = 0; i < deviceInfo->service_count; i++) {
73         mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
74     }
75     isUpdated = true;
76 }
77
78 std::string BluetoothDevice::getName() const
79 {
80     return mName;
81 }
82
83 std::string BluetoothDevice::getAddress() const
84 {
85     return mAddress;
86 }
87
88 void BluetoothDevice::copyAceCheckAccessFunction(
89     const Common::SecurityAccessor *securityAccessor)
90 {
91     Common::SecurityAccessor::copyAceCheckAccessFunction(securityAccessor);
92     mDeviceClass->copyAceCheckAccessFunction(securityAccessor);
93 }
94
95 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
96 {
97     return JSBluetoothClass::createJSObject(context, mDeviceClass);
98 }
99
100 bool BluetoothDevice::isBonded() const
101 {
102     bool isBonded = false;
103     int ret = 0;
104     bt_device_info_s *deviceInfo = NULL;
105
106     try {
107         ret = bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo);
108         if (ret != BT_ERROR_NONE) {
109             LOGE("%d", ret);
110             BluetoothUtil::throwBluetoothException(ret,
111                 "bt_adapter_get_bonded_device_info failed");
112         }
113         if (!deviceInfo) {
114             LOGE("deviceInfo is null");
115             throw UnknownException("deviceInfo is null");
116         }
117
118         isBonded = deviceInfo->is_bonded;
119         ret = bt_adapter_free_device_info(deviceInfo);
120         if (ret != BT_ERROR_NONE) {
121             LOGE("%d", ret);
122             BluetoothUtil::throwBluetoothException(ret,
123                 "bt_adapter_free_device_info failed");
124         }
125     } catch (BasePlatformException &e) {
126         LOGE("%s: %s", e.getName().c_str(), e.getMessage().c_str());
127     }
128     return isBonded;
129 }
130
131 bool BluetoothDevice::isTrusted() const
132 {
133     bool isTrusted = false;
134     int ret = 0;
135     bt_device_info_s *deviceInfo = NULL;
136
137     try {
138         ret = bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo);
139         if (ret != BT_ERROR_NONE) {
140             LOGE("%d", ret);
141             BluetoothUtil::throwBluetoothException(ret,
142                 "bt_adapter_get_bonded_device_info failed");
143         }
144         if (!deviceInfo) {
145             LOGE("deviceInfo is null");
146             throw UnknownException("deviceInfo is null");
147         }
148
149         isTrusted = deviceInfo->is_authorized;
150         ret = bt_adapter_free_device_info(deviceInfo);
151         if (ret != BT_ERROR_NONE) {
152             LOGE("%d", ret);
153             BluetoothUtil::throwBluetoothException(ret,
154                 "bt_adapter_free_device_info failed");
155         }
156     } catch (BasePlatformException &e) {
157         LOGE("%s: %s", e.getName().c_str(), e.getMessage().c_str());
158     }
159     return isTrusted;
160 }
161
162 bool BluetoothDevice::isConnected() const
163 {
164     bool isConnected = false;
165     int ret = 0;
166     bt_device_info_s *deviceInfo = NULL;
167
168     try {
169         ret = bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo);
170         if (ret != BT_ERROR_NONE) {
171             LOGE("%d", ret);
172             BluetoothUtil::throwBluetoothException(ret,
173                 "bt_adapter_get_bonded_device_info failed");
174         }
175         if (!deviceInfo) {
176             LOGE("deviceInfo is null");
177             throw UnknownException("deviceInfo is null");
178         }
179
180         isConnected = deviceInfo->is_connected;
181         ret = bt_adapter_free_device_info(deviceInfo);
182         if (ret != BT_ERROR_NONE) {
183             LOGE("%d", ret);
184             BluetoothUtil::throwBluetoothException(ret,
185                 "bt_adapter_free_device_info failed");
186         }
187     } catch (BasePlatformException &e) {
188         LOGE("%s: %s", e.getName().c_str(), e.getMessage().c_str());
189     }
190     return isConnected;
191 }
192
193 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
194 {
195     return JSUtil::toJSValueRef(context, mUUIDs);
196 }
197
198 } // Bluetooth
199 } // DeviceAPI