tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothDevice.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 <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24 #include <MultiCallbackUserData.h>
25
26 #include "plugin_config.h"
27 #include "JSBluetoothDevice.h"
28 #include "BluetoothDevice.h"
29 #include "BluetoothAdapter.h"
30
31 #include <TimeTracer.h>
32 #include <Logger.h>
33
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
36
37 namespace DeviceAPI {
38 namespace Bluetooth {
39
40 JSClassDefinition JSBluetoothDevice::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     "BluetoothDevice",
44     NULL, //ParentClass
45     m_property, //StaticValues
46     m_function, //StaticFunctions
47     initialize, //Initialize
48     finalize, //Finalize
49     NULL, //HasProperty,
50     NULL, //GetProperty,
51     NULL, //SetProperty,
52     NULL, //DeleteProperty,
53     NULL, //GetPropertyNames,
54     NULL, //CallAsFunction,
55     NULL, //CallAsConstructor,
56     NULL, //HasInstance,
57     NULL //ConvertToType
58 };
59
60 JSStaticValue JSBluetoothDevice::m_property[] = {
61     {
62         BLUETOOTH_DEVICE_NAME,
63         getProperty,
64         NULL,
65         kJSPropertyAttributeNone |
66         kJSPropertyAttributeReadOnly |
67         kJSPropertyAttributeDontDelete
68     },
69     {
70         BLUETOOTH_DEVICE_ADDRESS,
71         getProperty,
72         NULL,
73         kJSPropertyAttributeNone |
74         kJSPropertyAttributeReadOnly |
75         kJSPropertyAttributeDontDelete
76     },
77     {
78         BLUETOOTH_DEVICE_DEVICE_CLASS,
79         getProperty,
80         NULL,
81         kJSPropertyAttributeNone |
82         kJSPropertyAttributeReadOnly |
83         kJSPropertyAttributeDontDelete
84     },
85     {
86         BLUETOOTH_DEVICE_IS_BONDED,
87         getProperty,
88         NULL,
89         kJSPropertyAttributeNone |
90         kJSPropertyAttributeReadOnly |
91         kJSPropertyAttributeDontDelete
92     },
93     {
94         BLUETOOTH_DEVICE_IS_TRUSTED,
95         getProperty,
96         NULL,
97         kJSPropertyAttributeNone |
98         kJSPropertyAttributeReadOnly |
99         kJSPropertyAttributeDontDelete
100     },
101     {
102         BLUETOOTH_DEVICE_IS_CONNECTED,
103         getProperty,
104         NULL,
105         kJSPropertyAttributeNone |
106         kJSPropertyAttributeReadOnly |
107         kJSPropertyAttributeDontDelete
108     },
109     {
110         BLUETOOTH_DEVICE_UUIDS,
111         getProperty,
112         NULL,
113         kJSPropertyAttributeNone |
114         kJSPropertyAttributeReadOnly |
115         kJSPropertyAttributeDontDelete
116     },
117     { 0, 0, 0, 0 }
118 };
119
120 JSStaticFunction JSBluetoothDevice::m_function[] = {
121     {
122         BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID,
123         connectToServiceByUUID,
124         kJSPropertyAttributeNone
125     },
126     { 0, 0, 0 }
127 };
128
129 JSClassRef JSBluetoothDevice::m_jsClassRef =
130     JSClassCreate(JSBluetoothDevice::getClassInfo());
131
132 const JSClassRef JSBluetoothDevice::getClassRef()
133 {
134     if (!m_jsClassRef) {
135         m_jsClassRef = JSClassCreate(&m_classInfo);
136     }
137     return m_jsClassRef;
138 }
139
140 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
141 {
142     return &m_classInfo;
143 }
144
145 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context,
146     BluetoothDeviceSharedPtr device)
147 {
148     BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device);
149     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
150 }
151
152 BluetoothDeviceSharedPtr JSBluetoothDevice::toBluetoothDevice(
153     JSObjectRef deviceObj)
154 {
155     BluetoothDeviceHolderPtr priv =
156         static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(deviceObj));
157     return priv->mDevice;
158 }
159
160 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
161 {
162     // Do nothing
163 }
164
165 void JSBluetoothDevice::finalize(JSObjectRef object)
166 {
167     BluetoothDeviceHolderPtr priv =
168         static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
169     if (priv) {
170         JSObjectSetPrivate(object, NULL);
171         delete priv;
172     }
173 }
174
175 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
176         JSObjectRef object,
177         JSStringRef propertyName,
178         JSValueRef* exception)
179 {
180     try {
181         BluetoothDeviceHolderPtr priv =
182             static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
183         if (!priv) {
184             throw TypeMismatchException("Private object is NULL");
185         }
186
187         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
188             return JSUtil::toJSValueRef(context, priv->mDevice->getName());
189         }
190         else if (JSStringIsEqualToUTF8CString(propertyName,
191             BLUETOOTH_DEVICE_ADDRESS)) {
192             return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
193         }
194         else if (JSStringIsEqualToUTF8CString(propertyName,
195             BLUETOOTH_DEVICE_DEVICE_CLASS)) {
196             return priv->mDevice->getDeviceClass(context);
197         }
198         else if (JSStringIsEqualToUTF8CString(propertyName,
199             BLUETOOTH_DEVICE_IS_BONDED)) {
200             return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
201         }
202         else if (JSStringIsEqualToUTF8CString(propertyName,
203             BLUETOOTH_DEVICE_IS_TRUSTED)) {
204             return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
205         }
206         else if (JSStringIsEqualToUTF8CString(propertyName,
207             BLUETOOTH_DEVICE_IS_CONNECTED)) {
208             return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
209         }
210         else if (JSStringIsEqualToUTF8CString(propertyName,
211             BLUETOOTH_DEVICE_UUIDS)) {
212             return priv->mDevice->getUUIDs(context);
213         }
214     } catch (const BasePlatformException &err) {
215         LOGW("Getting property is failed: %s", err.getMessage().c_str());
216     }
217
218     return NULL;
219 }
220
221 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
222         JSObjectRef object,
223         JSObjectRef thisObject,
224         size_t argumentCount,
225         const JSValueRef arguments[],
226         JSValueRef* exception)
227 {
228     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
229
230     try {
231         // Private Object
232         BluetoothDeviceHolderPtr priv =
233             static_cast<BluetoothDeviceHolderPtr>(
234                 JSObjectGetPrivate(thisObject));
235         if (!priv) {
236             throw TypeMismatchException("Private object is NULL.");
237         }
238           // Access Check
239         TIME_TRACER_ITEM_BEGIN("connectToServiceByUUID::ACE", 1);
240         TIZEN_CHECK_ACCESS(context, exception, priv->mDevice.get(),
241             BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
242         TIME_TRACER_ITEM_END("connectToServiceByUUID::ACE", 1);
243
244         ArgumentValidator validator(context, argumentCount, arguments);
245
246         // uuid
247         std::string uuid = validator.toString(0);
248
249         // successCallback
250         JSObjectRef successCallback = validator.toFunction(1);
251
252         // errorCallback
253         JSObjectRef errorCallback = validator.toFunction(2, true);
254
255         // remote address
256         std::string remoteAddress = priv->mDevice->getAddress();
257
258         // perform
259         MultiCallbackUserDataPtr callback(
260             new MultiCallbackUserData(GlobalContextManager::getInstance()->
261                 getGlobalContext(context)));
262         if(!callback){
263             LOGW("Can't create MultiCallbackUserData");
264         }
265         else {
266             callback->setCallback("success", successCallback);
267             callback->setCallback("error", errorCallback);
268         }
269
270         BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress,
271             uuid, callback);
272         TIME_TRACER_ITEM_END(__FUNCTION__, 1);
273
274         return JSValueMakeUndefined(context);
275     } catch (const BasePlatformException &err) {
276         return JSWebAPIErrorFactory::postException(context, exception, err);
277     } catch (...) {
278         Common::UnknownException err(
279             "Unknown Error in BluetoothDevice.connectToServiceByUUID().");
280         return JSWebAPIErrorFactory::postException(context, exception, err);
281     }
282 }
283
284 } // Bluetooth
285 } // DeviceAPI