4283313491a9d115acd4f6c922e44312290e50d0
[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 <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     { BLUETOOTH_DEVICE_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62     { BLUETOOTH_DEVICE_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63     { BLUETOOTH_DEVICE_DEVICE_CLASS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
64     { BLUETOOTH_DEVICE_IS_BONDED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
65     { BLUETOOTH_DEVICE_IS_TRUSTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
66     { BLUETOOTH_DEVICE_IS_CONNECTED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
67     { BLUETOOTH_DEVICE_UUIDS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
68     { 0, 0, 0, 0 }
69 };
70
71 JSStaticFunction JSBluetoothDevice::m_function[] = {
72     { BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, connectToServiceByUUID, kJSPropertyAttributeNone },
73     { 0, 0, 0 }
74 };
75
76 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
77
78 const JSClassRef JSBluetoothDevice::getClassRef()
79 {
80     if (!m_jsClassRef) {
81         m_jsClassRef = JSClassCreate(&m_classInfo);
82     }
83     return m_jsClassRef;
84 }
85
86 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
87 {
88     return &m_classInfo;
89 }
90
91 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceSharedPtr device)
92 {
93     BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device);
94     return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
95 }
96
97 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
98 {
99     // Do nothing
100 }
101
102 void JSBluetoothDevice::finalize(JSObjectRef object)
103 {
104     BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
105     if (priv) {
106         JSObjectSetPrivate(object, NULL);
107         delete priv;
108     }
109 }
110
111 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
112         JSObjectRef object,
113         JSStringRef propertyName,
114         JSValueRef* exception)
115 {
116     try {
117         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
118         if (!priv) {
119             throw TypeMismatchException("Private object is NULL");
120         }
121
122         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
123             return JSUtil::toJSValueRef(context, priv->mDevice->getName());
124         }
125         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
126             return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
127         }
128         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
129             return priv->mDevice->getDeviceClass(context);
130         }
131         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
132             return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
133         }
134         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
135             return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
136         }
137         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
138             return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
139         }
140         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
141             return priv->mDevice->getUUIDs(context);
142         }
143     } catch (const BasePlatformException &err) {
144         LoggerW("Getting property is failed: " << err.getMessage().c_str());
145     }
146
147     return NULL;
148 }
149
150 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
151         JSObjectRef object,
152         JSObjectRef thisObject,
153         size_t argumentCount,
154         const JSValueRef arguments[],
155         JSValueRef* exception)
156 {
157     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
158     
159     // Access Check
160     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
161     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
162
163     try {
164         // Private Object
165         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject));
166         if (!priv) {
167             throw TypeMismatchException("Private object is NULL.");
168         }
169     
170         ArgumentValidator validator(context, argumentCount, arguments);        
171         std::string uuid = validator.toString(0);  // uuid
172         JSObjectRef successCallback = validator.toFunction(1);  // successCallback  
173         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
174         std::string remoteAddress = priv->mDevice->getAddress();    // remote address
175
176         // perform
177         MultiCallbackUserDataPtr callback(
178                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
179         if(!callback){
180             LoggerW("Can't create MultiCallbackUserData");
181         }
182         else {
183             callback->setCallback("success", successCallback);
184             callback->setCallback("error", errorCallback);        
185         }
186         
187         BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback);
188         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
189         
190         return JSValueMakeUndefined(context);
191     } catch (const BasePlatformException &err) {
192         return JSWebAPIErrorFactory::postException(context, exception, err);
193     } catch (...) {
194         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
195         return JSWebAPIErrorFactory::postException(context, exception, err);
196     }
197 }
198
199
200
201 } // Bluetooth
202 } // DeviceAPI