wrt-plugins-tizen_0.4.23
[framework/web/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 <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
25 #include <MultiCallbackUserData.h>
26
27 #include "plugin_config.h"
28 #include "JSBluetoothDevice.h"
29 #include "BluetoothDevice.h"
30 #include "BluetoothAdapter.h"
31
32 #include <TimeTracer.h>
33 #include <Logger.h>
34
35 using namespace WrtDeviceApis::Commons;
36 using namespace DeviceAPI::Common;
37
38 namespace DeviceAPI {
39 namespace Bluetooth {
40
41 JSClassDefinition JSBluetoothDevice::m_classInfo = {
42     0,
43     kJSClassAttributeNone,
44     "BluetoothDevice",
45     NULL, //ParentClass
46     m_property, //StaticValues
47     m_function, //StaticFunctions
48     initialize, //Initialize
49     finalize, //Finalize
50     NULL, //HasProperty,
51     NULL, //GetProperty,
52     NULL, //SetProperty,
53     NULL, //DeleteProperty,
54     NULL, //GetPropertyNames,
55     NULL, //CallAsFunction,
56     NULL, //CallAsConstructor,
57     NULL, //HasInstance,
58     NULL //ConvertToType
59 };
60
61 JSStaticValue JSBluetoothDevice::m_property[] = {
62     { BLUETOOTH_DEVICE_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63     { BLUETOOTH_DEVICE_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
64     { BLUETOOTH_DEVICE_DEVICE_CLASS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
65     { BLUETOOTH_DEVICE_IS_BONDED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
66     { BLUETOOTH_DEVICE_IS_TRUSTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
67     { BLUETOOTH_DEVICE_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
68     { BLUETOOTH_DEVICE_UUIDS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
69     { 0, 0, 0, 0 }
70 };
71
72 JSStaticFunction JSBluetoothDevice::m_function[] = {
73     { BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, connectToServiceByUUID, kJSPropertyAttributeNone },
74     { 0, 0, 0 }
75 };
76
77 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
78
79 const JSClassRef JSBluetoothDevice::getClassRef()
80 {
81     if (!m_jsClassRef) {
82         m_jsClassRef = JSClassCreate(&m_classInfo);
83     }
84     return m_jsClassRef;
85 }
86
87 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
88 {
89     return &m_classInfo;
90 }
91
92 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceSharedPtr device)
93 {
94     BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device);
95     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
96 }
97
98 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
99 {
100     LoggerD("Enter");
101     // Do nothing
102 }
103
104 void JSBluetoothDevice::finalize(JSObjectRef object)
105 {
106     BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
107     if (priv) {
108         JSObjectSetPrivate(object, NULL);
109         delete priv;
110     }
111 }
112
113 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
114         JSObjectRef object,
115         JSStringRef propertyName,
116         JSValueRef* exception)
117 {
118     try {
119         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
120         if (!priv) {
121             throw TypeMismatchException("Private object is NULL");
122         }
123
124         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
125             return JSUtil::toJSValueRef(context, priv->mDevice->getName());
126         }
127         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
128             return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
129         }
130         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
131             return priv->mDevice->getDeviceClass(context);
132         }
133         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
134             return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
135         }
136         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
137             return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
138         }
139         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
140             return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
141         }
142         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
143             return priv->mDevice->getUUIDs(context);
144         }
145     } catch (const BasePlatformException &err) {
146         LoggerW("Getting property is failed: " << err.getMessage().c_str());
147     }
148
149     return NULL;
150 }
151
152 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
153         JSObjectRef object,
154         JSObjectRef thisObject,
155         size_t argumentCount,
156         const JSValueRef arguments[],
157         JSValueRef* exception)
158 {
159     LoggerD("Enter");
160     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
161     
162     // Access Check
163     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
164     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165
166     try {
167         // Check whether Bluetooth is supported or not
168         if(!BluetoothAdapter::isBluetoothSupported()) {
169             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
170         }
171     
172         // Private Object
173         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject));
174         if (!priv) {
175             throw TypeMismatchException("Private object is NULL.");
176         }
177     
178         ArgumentValidator validator(context, argumentCount, arguments);        
179         std::string uuid = validator.toString(0);  // uuid
180         JSObjectRef successCallback = validator.toFunction(1);  // successCallback  
181         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
182         std::string remoteAddress = priv->mDevice->getAddress();    // remote address
183
184         // perform
185         MultiCallbackUserDataPtr callback(
186                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
187         if(!callback){
188             LoggerW("Can't create MultiCallbackUserData");
189         }
190         else {
191             callback->setCallback("success", successCallback);
192             callback->setCallback("error", errorCallback);        
193         }
194         
195         BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback);
196         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
197         
198         return JSValueMakeUndefined(context);
199     } catch (const BasePlatformException &err) {
200         return JSWebAPIException::throwException(context, exception, err);
201     } catch (...) {
202         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
203         return JSWebAPIException::throwException(context, exception, err);
204     }
205 }
206
207
208
209 } // Bluetooth
210 } // DeviceAPI