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 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
102 void JSBluetoothDevice::finalize(JSObjectRef object)
104 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
106 JSObjectSetPrivate(object, NULL);
111 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
113 JSStringRef propertyName,
114 JSValueRef* exception)
117 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
119 throw TypeMismatchException("Private object is NULL");
122 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
123 return JSUtil::toJSValueRef(context, priv->mDevice->getName());
125 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
126 return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
128 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_DEVICE_CLASS)) {
129 return priv->mDevice->getDeviceClass(context);
131 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_BONDED)) {
132 return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
134 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_TRUSTED)) {
135 return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
137 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_IS_CONNECTED)) {
138 return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
140 else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_UUIDS)) {
141 return priv->mDevice->getUUIDs(context);
143 } catch (const BasePlatformException &err) {
144 LoggerW("Getting property is failed: " << err.getMessage().c_str());
150 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
152 JSObjectRef thisObject,
153 size_t argumentCount,
154 const JSValueRef arguments[],
155 JSValueRef* exception)
157 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
160 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
161 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
165 BluetoothDeviceHolderPtr priv = static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(thisObject));
167 throw TypeMismatchException("Private object is NULL.");
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
177 MultiCallbackUserDataPtr callback(
178 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
180 LoggerW("Can't create MultiCallbackUserData");
183 callback->setCallback("success", successCallback);
184 callback->setCallback("error", errorCallback);
187 BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress, uuid, callback);
188 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
190 return JSValueMakeUndefined(context);
191 } catch (const BasePlatformException &err) {
192 return JSWebAPIErrorFactory::postException(context, exception, err);
194 DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothDevice.connectToServiceByUUID().");
195 return JSWebAPIErrorFactory::postException(context, exception, err);