1bce7ca19d38ecb030b4aacc6b64995df23f1820
[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     // Do nothing
101 }
102
103 void JSBluetoothDevice::finalize(JSObjectRef object)
104 {
105     BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
106     if (priv) {
107         JSObjectSetPrivate(object, NULL);
108         delete priv;
109     }
110 }
111
112 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
113         JSObjectRef object,
114         JSStringRef propertyName,
115         JSValueRef* exception)
116 {
117     try {
118         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
119         if (!priv) {
120             throw TypeMismatchException("Private object is NULL");
121         }
122
123         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
124             return JSUtil::toJSValueRef(context, priv->mDevice->getName());
125         }
126         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
127             return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
128         }
129         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
130             return priv->mDevice->getDeviceClass(context);
131         }
132         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
133             return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
134         }
135         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
136             return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
137         }
138         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
139             return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
140         }
141         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
142             return priv->mDevice->getUUIDs(context);
143         }
144     } catch (const BasePlatformException &err) {
145         LoggerW("Getting property is failed: " << err.getMessage().c_str());
146     }
147
148     return NULL;
149 }
150
151 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
152         JSObjectRef object,
153         JSObjectRef thisObject,
154         size_t argumentCount,
155         const JSValueRef arguments[],
156         JSValueRef* exception)
157 {
158     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
159     
160     // Access Check
161     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
162     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
163
164     try {
165         // Check whether Bluetooth is supported or not
166         if(!BluetoothAdapter::isBluetoothSupported()) {
167             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
168         }
169     
170         // Private Object
171         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject));
172         if (!priv) {
173             throw TypeMismatchException("Private object is NULL.");
174         }
175     
176         ArgumentValidator validator(context, argumentCount, arguments);        
177         std::string uuid = validator.toString(0);  // uuid
178         JSObjectRef successCallback = validator.toFunction(1);  // successCallback  
179         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
180         std::string remoteAddress = priv->mDevice->getAddress();    // remote address
181
182         // perform
183         MultiCallbackUserDataPtr callback(
184                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
185         if(!callback){
186             LoggerW("Can't create MultiCallbackUserData");
187         }
188         else {
189             callback->setCallback("success", successCallback);
190             callback->setCallback("error", errorCallback);        
191         }
192         
193         BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback);
194         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
195         
196         return JSValueMakeUndefined(context);
197     } catch (const BasePlatformException &err) {
198         return JSWebAPIException::throwException(context, exception, err);
199     } catch (...) {
200         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
201         return JSWebAPIException::throwException(context, exception, err);
202     }
203 }
204
205
206
207 } // Bluetooth
208 } // DeviceAPI