Update change log and spec for wrt-plugins-tizen_0.4.70
[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         Common::SecurityAccessor();
36     mName = std::string(discoveryInfo->remote_name);
37     mAddress = std::string(discoveryInfo->remote_address);
38     mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(discoveryInfo->bt_class));
39     for(int i = 0; i < discoveryInfo->service_count; i++) {
40         mUUIDs.push_back(std::string(discoveryInfo->service_uuid[i]));
41     }
42     isUpdated = true;
43 }
44
45 BluetoothDevice::BluetoothDevice(bt_device_info_s *deviceInfo)
46 {
47         Common::SecurityAccessor();
48     mName = std::string(deviceInfo->remote_name);
49     mAddress = std::string(deviceInfo->remote_address);
50     mDeviceClass = BluetoothClassSharedPtr(new BluetoothClass(deviceInfo->bt_class));
51         
52     for(int i = 0; i < deviceInfo->service_count; i++) {
53         mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
54     }
55     isUpdated = true;    
56 }
57
58 BluetoothDevice::~BluetoothDevice()
59 {
60     BluetoothAdapter::getInstance()->removeConnReq(mAddress);
61 }
62
63 void BluetoothDevice::updateInfo(bt_device_info_s *deviceInfo)
64 {
65     mName = std::string(deviceInfo->remote_name);
66     mUUIDs.clear();
67     for(int i = 0; i < deviceInfo->service_count; i++) {
68         mUUIDs.push_back(std::string(deviceInfo->service_uuid[i]));
69     }
70     isUpdated = true;
71 }
72
73 std::string BluetoothDevice::getName() const
74 {
75     return mName;
76 }
77
78 std::string BluetoothDevice::getAddress() const
79 {
80     return mAddress;
81 }
82
83 void BluetoothDevice::copyAceCheckAccessFunction(const Common::SecurityAccessor *securityAccessor)
84 {
85         Common::SecurityAccessor::copyAceCheckAccessFunction(securityAccessor);
86         mDeviceClass->copyAceCheckAccessFunction(securityAccessor);
87 }
88
89 JSValueRef BluetoothDevice::getDeviceClass(JSContextRef context)
90 {
91     /*
92     JSValueRef deviceClass = mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS);
93     if(deviceClass == NULL) {
94         deviceClass = JSBluetoothClass::createJSObject(context, mDeviceClass);
95         mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_DEVICE_CLASS, deviceClass);
96     }
97     
98     return deviceClass;
99     */
100     return JSBluetoothClass::createJSObject(context, mDeviceClass);
101 }
102
103 bool BluetoothDevice::isBonded() const
104 {
105     bool ret = false;
106     bt_device_info_s *deviceInfo = NULL;
107     if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
108         ret = deviceInfo->is_bonded;
109         bt_adapter_free_device_info(deviceInfo);
110     }
111     
112     return ret;
113 }
114
115 bool BluetoothDevice::isTrusted() const
116 {
117     bool ret = false;
118     bt_device_info_s *deviceInfo = NULL;
119     if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
120         ret = deviceInfo->is_authorized;
121         bt_adapter_free_device_info(deviceInfo);
122     }
123     
124     return ret;
125 }
126
127 bool BluetoothDevice::isConnected() const
128 {
129     bool ret = false;
130     bt_device_info_s *deviceInfo = NULL;
131     if(bt_adapter_get_bonded_device_info(mAddress.c_str(), &deviceInfo) == BT_ERROR_NONE && deviceInfo != NULL) {
132         ret = deviceInfo->is_connected;
133         bt_adapter_free_device_info(deviceInfo);
134     }
135     
136     return ret;
137 }
138
139 JSValueRef BluetoothDevice::getUUIDs(JSContextRef context)
140 {
141 /*
142     if(isUpdated == true) {
143         mLocalProperty.setProperty(context, BLUETOOTH_DEVICE_UUIDS, JSUtil::toJSValueRef(context, mUUIDs));
144         isUpdated = false;
145     }
146     
147     return mLocalProperty.getProperty(context, BLUETOOTH_DEVICE_UUIDS);
148 */
149     return JSUtil::toJSValueRef(context, mUUIDs);
150 }
151
152
153 } // Bluetooth
154 } // DeviceAPI