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[] = {
62 BLUETOOTH_DEVICE_NAME,
65 kJSPropertyAttributeNone |
66 kJSPropertyAttributeReadOnly |
67 kJSPropertyAttributeDontDelete
70 BLUETOOTH_DEVICE_ADDRESS,
73 kJSPropertyAttributeNone |
74 kJSPropertyAttributeReadOnly |
75 kJSPropertyAttributeDontDelete
78 BLUETOOTH_DEVICE_DEVICE_CLASS,
81 kJSPropertyAttributeNone |
82 kJSPropertyAttributeReadOnly |
83 kJSPropertyAttributeDontDelete
86 BLUETOOTH_DEVICE_IS_BONDED,
89 kJSPropertyAttributeNone |
90 kJSPropertyAttributeReadOnly |
91 kJSPropertyAttributeDontDelete
94 BLUETOOTH_DEVICE_IS_TRUSTED,
97 kJSPropertyAttributeNone |
98 kJSPropertyAttributeReadOnly |
99 kJSPropertyAttributeDontDelete
102 BLUETOOTH_DEVICE_IS_CONNECTED,
105 kJSPropertyAttributeNone |
106 kJSPropertyAttributeReadOnly |
107 kJSPropertyAttributeDontDelete
110 BLUETOOTH_DEVICE_UUIDS,
113 kJSPropertyAttributeNone |
114 kJSPropertyAttributeReadOnly |
115 kJSPropertyAttributeDontDelete
120 JSStaticFunction JSBluetoothDevice::m_function[] = {
122 BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID,
123 connectToServiceByUUID,
124 kJSPropertyAttributeNone
129 JSClassRef JSBluetoothDevice::m_jsClassRef =
130 JSClassCreate(JSBluetoothDevice::getClassInfo());
132 const JSClassRef JSBluetoothDevice::getClassRef()
135 m_jsClassRef = JSClassCreate(&m_classInfo);
140 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
145 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context,
146 BluetoothDeviceSharedPtr device)
148 BluetoothDeviceHolderPtr holder = new BluetoothDeviceHolder(device);
149 return JSObjectMake(context, getClassRef(), static_cast<void*>(holder));
152 BluetoothDeviceSharedPtr JSBluetoothDevice::toBluetoothDevice(
153 JSObjectRef deviceObj)
155 BluetoothDeviceHolderPtr priv =
156 static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(deviceObj));
157 return priv->mDevice;
160 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
165 void JSBluetoothDevice::finalize(JSObjectRef object)
167 BluetoothDeviceHolderPtr priv =
168 static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
170 JSObjectSetPrivate(object, NULL);
175 JSValueRef JSBluetoothDevice::getProperty(JSContextRef context,
177 JSStringRef propertyName,
178 JSValueRef* exception)
181 BluetoothDeviceHolderPtr priv =
182 static_cast<BluetoothDeviceHolderPtr>(JSObjectGetPrivate(object));
184 throw TypeMismatchException("Private object is NULL");
187 if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_NAME)) {
188 return JSUtil::toJSValueRef(context, priv->mDevice->getName());
190 else if (JSStringIsEqualToUTF8CString(propertyName,
191 BLUETOOTH_DEVICE_ADDRESS)) {
192 return JSUtil::toJSValueRef(context, priv->mDevice->getAddress());
194 else if (JSStringIsEqualToUTF8CString(propertyName,
195 BLUETOOTH_DEVICE_DEVICE_CLASS)) {
196 return priv->mDevice->getDeviceClass(context);
198 else if (JSStringIsEqualToUTF8CString(propertyName,
199 BLUETOOTH_DEVICE_IS_BONDED)) {
200 return JSUtil::toJSValueRef(context, priv->mDevice->isBonded());
202 else if (JSStringIsEqualToUTF8CString(propertyName,
203 BLUETOOTH_DEVICE_IS_TRUSTED)) {
204 return JSUtil::toJSValueRef(context, priv->mDevice->isTrusted());
206 else if (JSStringIsEqualToUTF8CString(propertyName,
207 BLUETOOTH_DEVICE_IS_CONNECTED)) {
208 return JSUtil::toJSValueRef(context, priv->mDevice->isConnected());
210 else if (JSStringIsEqualToUTF8CString(propertyName,
211 BLUETOOTH_DEVICE_UUIDS)) {
212 return priv->mDevice->getUUIDs(context);
214 } catch (const BasePlatformException &err) {
215 LOGW("Getting property is failed: %s", err.getMessage().c_str());
221 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context,
223 JSObjectRef thisObject,
224 size_t argumentCount,
225 const JSValueRef arguments[],
226 JSValueRef* exception)
228 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 1);
232 BluetoothDeviceHolderPtr priv =
233 static_cast<BluetoothDeviceHolderPtr>(
234 JSObjectGetPrivate(thisObject));
236 throw TypeMismatchException("Private object is NULL.");
239 TIME_TRACER_ITEM_BEGIN("connectToServiceByUUID::ACE", 1);
240 TIZEN_CHECK_ACCESS(context, exception, priv->mDevice.get(),
241 BLUETOOTH_DEVICE_API_CONNECT_TO_SERVICE_BY_UUID);
242 TIME_TRACER_ITEM_END("connectToServiceByUUID::ACE", 1);
244 ArgumentValidator validator(context, argumentCount, arguments);
247 std::string uuid = validator.toString(0);
250 JSObjectRef successCallback = validator.toFunction(1);
253 JSObjectRef errorCallback = validator.toFunction(2, true);
256 std::string remoteAddress = priv->mDevice->getAddress();
259 MultiCallbackUserDataPtr callback(
260 new MultiCallbackUserData(GlobalContextManager::getInstance()->
261 getGlobalContext(context)));
263 LOGW("Can't create MultiCallbackUserData");
266 callback->setCallback("success", successCallback);
267 callback->setCallback("error", errorCallback);
270 BluetoothAdapter::getInstance()->connectToServiceByUUID(remoteAddress,
272 TIME_TRACER_ITEM_END(__FUNCTION__, 1);
274 return JSValueMakeUndefined(context);
275 } catch (const BasePlatformException &err) {
276 return JSWebAPIErrorFactory::postException(context, exception, err);
278 Common::UnknownException err(
279 "Unknown Error in BluetoothDevice.connectToServiceByUUID().");
280 return JSWebAPIErrorFactory::postException(context, exception, err);