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.
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <SecurityExceptions.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <JSTizenException.h>
27 #include "JSBluetoothDevice.h"
28 #include "IBluetoothDeviceManager.h"
29 #include "BluetoothFactory.h"
30 #include "BluetoothDeviceManagerListener.h"
31 #include "BluetoothProperty.h"
32 #include "JSBluetoothClass.h"
33 #include "BluetoothConverter.h"
34 #include "plugin_config.h"
38 using namespace WrtDeviceApis;
39 using namespace DeviceAPI::Common;
44 JSClassDefinition JSBluetoothDevice::m_classInfo =
47 kJSClassAttributeNone,
65 JSStaticFunction JSBluetoothDevice::m_function[] =
67 { "connectToServiceByUUID", JSBluetoothDevice::connectToServiceByUUID, kJSPropertyAttributeNone },
71 JSStaticValue JSBluetoothDevice::m_properties[] = {
72 {"name", getName, NULL, kJSPropertyAttributeReadOnly},
73 {"address", getAddress, NULL, kJSPropertyAttributeReadOnly},
74 {"deviceClass", getDeviceClass, NULL, kJSPropertyAttributeReadOnly},
75 {"isBonded", getBondProperty, NULL, kJSPropertyAttributeReadOnly},
76 {"isTrusted", getTrustProperty, NULL, kJSPropertyAttributeReadOnly},
77 {"isConnected", getConnectProperty, NULL, kJSPropertyAttributeReadOnly},
78 {"uuids", getUuids, NULL, kJSPropertyAttributeReadOnly},
82 const JSClassRef JSBluetoothDevice::getClassRef()
85 m_jsClassRef = JSClassCreate(&m_classInfo);
90 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
95 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
97 JSObjectRef JSBluetoothDevice::m_deviceClass = NULL;
99 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
101 LogDebug("initialize ");
103 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
107 IBluetoothDeviceManagerPtr BluetoothManager(BluetoothFactory::getInstance().getBluetoothDeviceManager());
108 priv = new JSBluetoothDevicePriv( context, BluetoothManager);
110 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
112 LogError("Object can't store private data.");
118 LogDebug("already exist");
124 void JSBluetoothDevice::finalize(JSObjectRef object)
126 LogDebug("Finalrize");
128 JSBluetoothDevicePriv* priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
132 JSObjectSetPrivate(object, NULL);
133 LogDebug("Deleting BluetoothManager");
139 bool JSBluetoothDevice::hasInstance(JSContextRef context, JSObjectRef constructor,
140 JSValueRef possibleInstance, JSValueRef* exception)
142 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
146 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceData device)
148 IBluetoothDeviceManagerPtr BluetoothManager(BluetoothFactory::getInstance().getBluetoothDeviceManager());
149 BluetoothManager->setDevice(device);
151 JSBluetoothDevicePriv* priv = new JSBluetoothDevicePriv( context, BluetoothManager);
152 return JSObjectMake(context, getClassRef(), priv);
157 JSValueRef JSBluetoothDevice::getName(JSContextRef context,
159 JSStringRef propertyName,
160 JSValueRef* exception)
164 Converter converter(context);
165 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
171 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
174 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
175 std::string name = BluetoothDeviceManager->getNameProperty();
176 LogDebug("name" << name);
177 return converter.toJSValueRef(name);
179 Catch (WrtDeviceApis::Commons::Exception)
181 LogWarning("Exception"<<_rethrown_exception.GetMessage());
187 JSValueRef JSBluetoothDevice::getAddress(JSContextRef context,
189 JSStringRef propertyName,
190 JSValueRef* exception)
194 Converter converter(context);
195 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
201 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
204 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
205 std::string address = BluetoothDeviceManager->getAddressProperty();
207 LogDebug("address" << address);
208 return converter.toJSValueRef(address);
210 Catch(WrtDeviceApis::Commons::Exception)
212 LogWarning("Exception"<<_rethrown_exception.GetMessage());
217 JSValueRef JSBluetoothDevice::getDeviceClass(JSContextRef context,
219 JSStringRef propertyName,
220 JSValueRef* exception)
224 if(m_deviceClass == NULL)
226 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
232 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
234 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
235 BluetoothDeviceDataClass devClass = BluetoothDeviceManager->getClass();
237 LogDebug("Major:" << std::hex << devClass.major << "Minor:" << std::hex << devClass.minor << "Service Mask:" << std::hex << devClass.majorServiceMask);
239 m_deviceClass = JSBluetoothClass::createJSObject(priv->getContext(), devClass);
241 Catch(WrtDeviceApis::Commons::Exception)
243 LogWarning("Exception"<<_rethrown_exception.GetMessage());
244 return JSValueMakeUndefined(context);
248 return m_deviceClass;
251 JSValueRef JSBluetoothDevice::getBondProperty(JSContextRef context,
253 JSStringRef propertyName,
254 JSValueRef* exception)
258 Converter converter(context);
259 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
265 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
268 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
269 bool isBonded = BluetoothDeviceManager->getBondProperty();
271 LogDebug("isBonded " << isBonded);
273 return converter.toJSValueRef(isBonded);
275 Catch(WrtDeviceApis::Commons::Exception)
277 LogWarning("Exception"<<_rethrown_exception.GetMessage());
280 return JSValueMakeUndefined(context);
282 JSValueRef JSBluetoothDevice::getTrustProperty(JSContextRef context,
284 JSStringRef propertyName,
285 JSValueRef* exception)
289 Converter converter(context);
290 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
296 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
299 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
300 bool isTrusted = BluetoothDeviceManager->getTrustProperty();
302 LogDebug("isTrusted " << isTrusted);
303 return converter.toJSValueRef(isTrusted);
305 Catch(WrtDeviceApis::Commons::Exception)
307 LogWarning("Exception"<<_rethrown_exception.GetMessage());
310 return JSValueMakeUndefined(context);
312 JSValueRef JSBluetoothDevice::getConnectProperty(JSContextRef context,
314 JSStringRef propertyName,
315 JSValueRef* exception)
319 Converter converter(context);
320 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
326 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
328 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
329 bool isConnected = BluetoothDeviceManager->getConnectProperty();
331 LogDebug("isConnected " << isConnected);
332 return converter.toJSValueRef(isConnected);
334 Catch(WrtDeviceApis::Commons::Exception)
336 LogWarning("Exception"<<_rethrown_exception.GetMessage());
339 return JSValueMakeUndefined(context);
342 JSValueRef JSBluetoothDevice::getUuids(JSContextRef context,
344 JSStringRef propertyName,
345 JSValueRef* exception)
349 Converter converter(context);
350 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
356 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
359 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
360 std::vector<std::string> uuids = BluetoothDeviceManager->getUuidsProperty();
361 JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
362 int length = uuids.size();
366 return converter.toJSValueRef("");
369 for (int i = 0; i < length; ++i)
371 JSValueRef value = converter.toJSValueRef(uuids[i]);
373 if (!JSSetArrayElement(context, result, i, value))
375 return converter.toJSValueRef("");
381 Catch(WrtDeviceApis::Commons::Exception)
383 LogWarning("Exception"<<_rethrown_exception.GetMessage());
386 return JSValueMakeUndefined(context);
392 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context, JSObjectRef object,
393 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
394 JSValueRef* exception) {
396 LogDebug("connectToServiceByUUID");
398 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(thisObject));
399 JSValueRef successCallback = NULL;
400 JSValueRef errorCallBack = NULL;
402 JSValueRef reserveArguments[3];
406 return JSTizenExceptionFactory::postException(context, exception,
407 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
410 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
414 for (index = 0; index < 3; index++)
416 if (index < argumentCount)
417 reserveArguments[index] = arguments[index];
419 reserveArguments[index] = JSValueMakeNull(context);
423 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_DEVICE_CONNECT_TO_SERVICE]);
425 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
426 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_DEVICE_CONNECT_TO_SERVICE]);
428 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
431 BluetoothConverter converter(context);
432 successCallback = converter.toFunction(reserveArguments[1]);
433 errorCallBack = converter.toFunctionOrNull(reserveArguments[2]);
438 return JSValueMakeUndefined(context);
440 cbm->setOnSuccess(successCallback);
441 cbm->setOnError(errorCallBack);
443 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
444 EventBTConnectToServiceByUUIDPtr event(new EventBTConnectToServiceByUUID);
445 BluetoothSocketData socketData;
446 socketData.uuid = converter.toBluetoothUUIDString(reserveArguments[0]);
447 socketData.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
449 /* if (argumentCount >= 4)
451 socketData.protocol = converter.toBluetoothProtocolInt(reserveArguments[3]);
455 if (BluetoothDeviceManager == NULL || event == NULL)
457 LogError("BluetoothDeviceManager or event or listener NULL");
458 Throw(WrtDeviceApis::Commons::UnknownException);
461 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
463 event->setForAsynchronousCall(&BluetoothDeviceManagerListener::getInstance());
464 event->setSocketData(socketData);
465 BluetoothDeviceManager->connectToServiceByUUID(event);
467 Catch (WrtDeviceApis::Commons::ConversionException)
469 LogError("ConversionException");
470 return JSTizenExceptionFactory::postException(context, exception,
471 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
474 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
476 LogError("InvalidArgumentException");
477 return JSTizenExceptionFactory::postException(context, exception,
478 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
480 Catch(WrtDeviceApis::Commons::UnsupportedException)
482 LogError("UnsupportException");
483 return JSTizenExceptionFactory::postException(context, exception,
484 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
487 Catch (WrtDeviceApis::Commons::AlreadyInUseException)
489 LogError("AlreadyInUseException");
490 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(priv->getContext(),
491 JSTizenException::SERVICE_NOT_AVAILABLE, "already connected"));
493 Catch (WrtDeviceApis::Commons::Exception)
495 LogError("Exception");
496 return JSTizenExceptionFactory::postException(context, exception,
497 JSTizenException::UNKNOWN_ERROR, "unknown error");
500 return JSValueMakeUndefined(context);