Update change log and spec for wrt-plugins-tizen_0.4.44
[platform/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 BluetoothDeviceSharedPtr JSBluetoothDevice::toBluetoothDevice(JSObjectRef deviceObj)
98 {
99     BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(deviceObj));
100     return priv->mDevice;
101 }
102
103 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
104 {
105     // Do nothing
106 }
107
108 void JSBluetoothDevice::finalize(JSObjectRef object)
109 {
110     BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
111     if (priv) {
112         JSObjectSetPrivate(object, NULL);
113         delete priv;
114     }
115 }
116
117 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
118         JSObjectRef object,
119         JSStringRef propertyName,
120         JSValueRef* exception)
121 {
122     try {
123         BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
124         if (!priv) {
125             throw TypeMismatchException("Private object is NULL");
126         }
127
128         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
129             return JSUtil::toJSValueRef(context, priv->mDevice->getName());
130         }
131         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
132             return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
133         }
134         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
135             return priv->mDevice->getDeviceClass(context);
136         }
137         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
138             return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
139         }
140         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
141             return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
142         }
143         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
144             return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
145         }
146         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
147             return priv->mDevice->getUUIDs(context);
148         }
149     } catch (const BasePlatformException &err) {
150         LoggerW("Getting property is failed: " << err.getMessage().c_str());
151     }
152
153     return NULL;
154 }
155
156 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
157         JSObjectRef object,
158         JSObjectRef thisObject,
159         size_t argumentCount,
160         const JSValueRef arguments[],
161         JSValueRef* exception)
162 {
163     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
164     
165     // Access Check
166     TIME_TRACER_ITEM_BEGIN("connectToServiceByUUID::ACE", 1);
167     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
168     TIME_TRACER_ITEM_END("connectToServiceByUUID::ACE", 1);
169     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
170
171     try {
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__, 1);
197         
198         return JSValueMakeUndefined(context);
199     } catch (const BasePlatformException &err) {
200         return JSWebAPIErrorFactory::postException(context, exception, err);
201     } catch (...) {
202         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
203         return JSWebAPIErrorFactory::postException(context, exception, err);
204     }
205 }
206
207
208
209 } // Bluetooth
210 } // DeviceAPI