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)
103 void JSBluetoothDevice::finalize(JSObjectRef object)
105 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
107 JSObjectSetPrivate(object, NULL);
112 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
114 JSStringRef propertyName,
115 JSValueRef* exception)
118 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
120 throw TypeMismatchException("Private object is NULL");
123 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
124 return JSUtil::toJSValueRef(context, priv->mDevice->getName());
126 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
127 return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
129 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
130 return priv->mDevice->getDeviceClass(context);
132 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
133 return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
135 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
136 return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
138 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
139 return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
141 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
142 return priv->mDevice->getUUIDs(context);
144 } catch (const BasePlatformException &err) {
145 LoggerW("Getting property is failed: " << err.getMessage().c_str());
151 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
153 JSObjectRef thisObject,
154 size_t argumentCount,
155 const JSValueRef arguments[],
156 JSValueRef* exception)
158 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
161 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
162 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165 // Check whether Bluetooth is supported or not
166 if(!BluetoothAdapter::isBluetoothSupported()) {
167 throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
171 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject));
173 throw TypeMismatchException("Private object is NULL.");
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
183 MultiCallbackUserDataPtr callback(
184 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
186 LoggerW("Can't create MultiCallbackUserData");
189 callback->setCallback("success", successCallback);
190 callback->setCallback("error", errorCallback);
193 BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback);
194 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
196 return JSValueMakeUndefined(context);
197 } catch (const BasePlatformException &err) {
198 return JSWebAPIException::throwException(context, exception, err);
200 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
201 return JSWebAPIException::throwException(context, exception, err);