2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <SecurityExceptions.h>
21 #include <ArgumentValidator.h>
22 #include <GlobalContextManager.h>
23 #include <PlatformException.h>
24 #include <MultiCallbackUserData.h>
26 #include "plugin_config.h"
27 #include "JSBluetoothDevice.h"
28 #include "BluetoothDevice.h"
29 #include "BluetoothAdapter.h"
31 #include <TimeTracer.h>
34 using namespace WrtDeviceApis::Commons;
35 using namespace DeviceAPI::Common;
40 JSClassDefinition JSBluetoothDevice::m_classInfo = {
42 kJSClassAttributeNone,
45 m_property, //StaticValues
46 m_function, //StaticFunctions
47 initialize, //Initialize
52 NULL, //DeleteProperty,
53 NULL, //GetPropertyNames,
54 NULL, //CallAsFunction,
55 NULL, //CallAsConstructor,
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 },
71 JSStaticFunction JSBluetoothDevice::m_function[] = {
72 { BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, connectToServiceByUUID, kJSPropertyAttributeNone },
76 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
78 const JSClassRef JSBluetoothDevice::getClassRef()
81 m_jsClassRef = JSClassCreate(&m_classInfo);
86 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
91 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceSharedPtr device)
93 BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device);
94 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
97 BluetoothDeviceSharedPtr JSBluetoothDevice::toBluetoothDevice(JSObjectRef deviceObj)
99 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(deviceObj));
100 return priv->mDevice;
103 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
108 void JSBluetoothDevice::finalize(JSObjectRef object)
110 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
112 JSObjectSetPrivate(object, NULL);
117 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
119 JSStringRef propertyName,
120 JSValueRef* exception)
123 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
125 throw TypeMismatchException("Private object is NULL");
128 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
129 return JSUtil::toJSValueRef(context, priv->mDevice->getName());
131 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
132 return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
134 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
135 return priv->mDevice->getDeviceClass(context);
137 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
138 return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
140 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
141 return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
143 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
144 return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
146 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
147 return priv->mDevice->getUUIDs(context);
149 } catch (const BasePlatformException &err) {
150 LoggerW("Getting property is failed: " << err.getMessage().c_str());
156 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
158 JSObjectRef thisObject,
159 size_t argumentCount,
160 const JSValueRef arguments[],
161 JSValueRef* exception)
163 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
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);
173 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject));
175 throw TypeMismatchException("Private object is NULL.");
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
185 MultiCallbackUserDataPtr callback(
186 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
188 LoggerW("Can't create MultiCallbackUserData");
191 callback->setCallback("success", successCallback);
192 callback->setCallback("error", errorCallback);
195 BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback);
196 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
198 return JSValueMakeUndefined(context);
199 } catch (const BasePlatformException &err) {
200 return JSWebAPIErrorFactory::postException(context, exception, err);
202 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
203 return JSWebAPIErrorFactory::postException(context, exception, err);