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 <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24 #include <PlatformException.h>
25 #include <MultiCallbackUserData.h>
27 #include "plugin_config.h"
28 #include "JSBluetoothDevice.h"
29 #include "BluetoothDevice.h"
30 #include "BluetoothAdapter.h"
32 #include <TimeTracer.h>
35 using namespace WrtDeviceApis::Commons;
36 using namespace DeviceAPI::Common;
41 JSClassDefinition JSBluetoothDevice::m_classInfo = {
43 kJSClassAttributeNone,
46 m_property, //StaticValues
47 m_function, //StaticFunctions
48 initialize, //Initialize
53 NULL, //DeleteProperty,
54 NULL, //GetPropertyNames,
55 NULL, //CallAsFunction,
56 NULL, //CallAsConstructor,
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 },
72 JSStaticFunction JSBluetoothDevice::m_function[] = {
73 { BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID, connectToServiceByUUID, kJSPropertyAttributeNone },
77 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
79 const JSClassRef JSBluetoothDevice::getClassRef()
82 m_jsClassRef = JSClassCreate(&m_classInfo);
87 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
92 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceSharedPtr device)
94 BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device);
95 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
98 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
104 void JSBluetoothDevice::finalize(JSObjectRef object)
106 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
108 JSObjectSetPrivate(object, NULL);
113 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
115 JSStringRef propertyName,
116 JSValueRef* exception)
119 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
121 throw TypeMismatchException("Private object is NULL");
124 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
125 return JSUtil::toJSValueRef(context, priv->mDevice->getName());
127 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
128 return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
130 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
131 return priv->mDevice->getDeviceClass(context);
133 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
134 return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
136 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
137 return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
139 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
140 return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
142 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
143 return priv->mDevice->getUUIDs(context);
145 } catch (const BasePlatformException &err) {
146 LoggerW("Getting property is failed: " << err.getMessage().c_str());
152 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
154 JSObjectRef thisObject,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
160 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
163 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
164 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
167 // Check whether Bluetooth is supported or not
168 if(!BluetoothAdapter::isBluetoothSupported()) {
169 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
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__, 0);
198 return JSValueMakeUndefined(context);
199 } catch (const BasePlatformException &err) {
200 return JSWebAPIException::throwException(context, exception, err);
202 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
203 return JSWebAPIException::throwException(context, exception, err);