2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <CommonsJavaScript/Converter.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include <Tizen/Common/SecurityExceptions.h>
26 #include <Commons/Exception.h>
27 #include "JSBluetoothAdapter.h"
28 #include <API/Bluetooth/IBluetoothAdapterManager.h>
29 #include <API/Bluetooth/BluetoothFactory.h>
30 #include <API/Bluetooth/BluetoothProperty.h>
31 #include "BluetoothAdapterListener.h"
32 #include "BluetoothMultiCallback.h"
33 #include "BluetoothConverter.h"
35 #include "plugin_config.h"
39 using namespace WrtDeviceApis;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41 using namespace TizenApis::Commons;
47 JSClassRef JSBluetoothAdapter::m_jsClassRef = NULL;
49 JSClassDefinition JSBluetoothAdapter::m_classInfo =
52 kJSClassAttributeNone,
70 JSStaticFunction JSBluetoothAdapter::m_function[] =
72 { "setPowered", JSBluetoothAdapter::setPowered, kJSPropertyAttributeNone },
73 { "setVisible", JSBluetoothAdapter::setVisible, kJSPropertyAttributeNone },
74 { "setName", JSBluetoothAdapter::setName, kJSPropertyAttributeNone },
75 { "discoverDevices", JSBluetoothAdapter::discoveryDevices, kJSPropertyAttributeNone },
76 { "stopDiscovery", JSBluetoothAdapter::stopDiscovery, kJSPropertyAttributeNone },
77 { "getKnownDevices", JSBluetoothAdapter::getKnownDevices, kJSPropertyAttributeNone },
78 { "getDevice", JSBluetoothAdapter::getDevice, kJSPropertyAttributeNone },
79 { "createBonding", JSBluetoothAdapter::createBonding, kJSPropertyAttributeNone },
80 { "destroyBonding", JSBluetoothAdapter::destroyBonding, kJSPropertyAttributeNone },
81 { "registerRFCOMMServiceByUUID", JSBluetoothAdapter::registerRFCOMMServiceByUUID, kJSPropertyAttributeNone },
85 JSStaticValue JSBluetoothAdapter::m_properties[] = {
86 {"address", getAddress, NULL, kJSPropertyAttributeReadOnly},
87 {"name", getName, NULL, kJSPropertyAttributeReadOnly},
88 {"powered", getPowered, NULL, kJSPropertyAttributeReadOnly},
89 {"visible", getVisible, NULL, kJSPropertyAttributeReadOnly},
94 JSValueRef JSBluetoothAdapter::getName(JSContextRef context,
96 JSStringRef propertyName,
97 JSValueRef* exception)
101 Converter converter(context);
102 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
108 Throw(UnknownException);
112 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
113 std::string name = BluetoothAdapterManager->getName();
114 return converter.toJSValueRef(name);
116 Catch (WrtDeviceApis::Commons::ConversionException)
118 LogError("ConversionException");
119 return JSTizenExceptionFactory::postException(context, exception,
120 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
122 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
124 LogError("InvalidArgumentException");
125 return JSTizenExceptionFactory::postException(context, exception,
126 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
128 Catch(WrtDeviceApis::Commons::Exception)
130 LogError("UnkownException");
131 return JSTizenExceptionFactory::postException(context, exception,
132 JSTizenException::UNKNOWN_ERROR, "Unkown error");
134 return JSValueMakeUndefined(context);
137 JSValueRef JSBluetoothAdapter::setName(JSContextRef context, JSObjectRef object,
138 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
139 JSValueRef* exception)
142 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
143 JSValueRef successCallback = NULL;
144 JSValueRef errorCallBack = NULL;
151 Throw(UnknownException);
154 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
156 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_NAME]);
158 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
160 if (argumentCount < 2 || argumentCount > 3)
162 Throw(InvalidArgumentException);
165 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsString(context, arguments[0]) == false)
167 Throw(ConversionException);
170 BluetoothConverter converter(priv->getContext());
171 successCallback = converter.toFunction(arguments[1]);
173 if (argumentCount >= 3)
175 errorCallBack = converter.toFunctionOrNull(arguments[2]);
178 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
182 Throw(UnknownException);
185 cbm->setOnSuccess(successCallback);
186 cbm->setOnError(errorCallBack);
188 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
189 std::string name = converter.toString(arguments[0]);
191 if (name != BluetoothAdapterManager->getName())
193 EventBTSetNamePtr event(new EventBTSetName);
195 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
196 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
197 event->setForAsynchronousCall(&listener);
198 event->setName(name);
199 BluetoothAdapterManager->setAdapterName(event);
203 LogDebug("same name is already set");
204 cbm->callOnSuccess();
206 return JSValueMakeNull(context);
208 Catch (WrtDeviceApis::Commons::ConversionException)
210 LogError("ConversionException");
211 return JSTizenExceptionFactory::postException(context, exception,
212 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
215 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
217 LogError("InvalidArgumentException");
218 return JSTizenExceptionFactory::postException(context, exception,
219 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
222 Catch (WrtDeviceApis::Commons::Exception)
224 LogError("UnkownException");
225 return JSTizenExceptionFactory::postException(context, exception,
226 JSTizenException::UNKNOWN_ERROR, "Unkown error");
228 return JSValueMakeUndefined(context);
232 JSValueRef JSBluetoothAdapter::getAddress(JSContextRef context,
234 JSStringRef propertyName,
235 JSValueRef* exception)
239 Converter converter(context);
240 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
246 Throw(UnknownException);
248 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
249 std::string address = BluetoothAdapterManager->getAddress();
250 return converter.toJSValueRef(address);
252 Catch (WrtDeviceApis::Commons::ConversionException)
254 LogError("ConversionException");
255 return JSTizenExceptionFactory::postException(context, exception,
256 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
259 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
261 LogError("InvalidArgumentException");
262 return JSTizenExceptionFactory::postException(context, exception,
263 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
266 Catch (WrtDeviceApis::Commons::Exception)
268 LogError("UnkownException");
269 return JSTizenExceptionFactory::postException(context, exception,
270 JSTizenException::UNKNOWN_ERROR, "Unkown error");
273 return JSValueMakeUndefined(context);
278 JSValueRef JSBluetoothAdapter::getVisible(JSContextRef context,
280 JSStringRef propertyName,
281 JSValueRef* exception)
285 Converter converter(context);
286 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
291 Throw(UnknownException);
294 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
295 bool visibility = BluetoothAdapterManager->getVisibility();
296 return converter.toJSValueRef(visibility);
298 Catch (WrtDeviceApis::Commons::ConversionException)
300 LogError("ConversionException");
301 return JSTizenExceptionFactory::postException(context, exception,
302 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
305 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
307 LogError("InvalidArgumentException");
308 return JSTizenExceptionFactory::postException(context, exception,
309 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
311 Catch (WrtDeviceApis::Commons::Exception)
313 LogError("UnkownException");
314 return JSTizenExceptionFactory::postException(context, exception,
315 JSTizenException::UNKNOWN_ERROR, "Unkown error");
317 return JSValueMakeUndefined(context);
323 const JSClassRef JSBluetoothAdapter::getClassRef()
327 m_jsClassRef = JSClassCreate(&m_classInfo);
332 const JSClassDefinition* JSBluetoothAdapter::getClassInfo()
338 void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object)
340 JSBluetoothAdapterPriv* priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
344 LogDebug("Already Exist");
349 IBluetoothAdapterManagerPtr BluetoothAdapterManager(BluetoothFactory::getInstance().getBluetoothAdapterManager());
350 priv = new JSBluetoothAdapterPriv( context, BluetoothAdapterManager);
351 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
353 LogError("Object can't store private data.");
356 LogDebug("JSBluetoothAdapter::initialize ");
360 void JSBluetoothAdapter::finalize(JSObjectRef object)
362 LogDebug("JSBluetoothAdapter::Finalrize");
364 JSBluetoothAdapterPriv* priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
368 JSObjectSetPrivate(object, NULL);
369 LogDebug("Deleting BluetoothAdapterManager");
376 bool JSBluetoothAdapter::hasInstance(JSContextRef context, JSObjectRef constructor,
377 JSValueRef possibleInstance, JSValueRef* exception)
379 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
383 JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context)
385 IBluetoothAdapterManagerPtr BluetoothAdapterManager(BluetoothFactory::getInstance().getBluetoothAdapterManager());
386 JSBluetoothAdapterPriv* priv = new JSBluetoothAdapterPriv( context, BluetoothAdapterManager);
388 return JSObjectMake(context, getClassRef(), priv);
392 JSValueRef JSBluetoothAdapter::getPowered(JSContextRef context,
394 JSStringRef propertyName,
395 JSValueRef* exception)
400 Converter converter(context);
401 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
407 Throw(UnknownException);
410 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
411 bool powered = BluetoothAdapterManager->getPowered();
412 return converter.toJSValueRef(powered);
414 Catch (WrtDeviceApis::Commons::ConversionException)
416 LogError("ConversionException");
417 return JSTizenExceptionFactory::postException(context, exception,
418 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
421 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
423 LogError("InvalidArgumentException");
424 return JSTizenExceptionFactory::postException(context, exception,
425 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
428 Catch (WrtDeviceApis::Commons::Exception)
430 LogError("UnkownException");
431 return JSTizenExceptionFactory::postException(context, exception,
432 JSTizenException::UNKNOWN_ERROR, "Unkown error");
435 return JSValueMakeUndefined(context);
439 JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context, JSObjectRef object,
440 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
441 JSValueRef* exception)
444 LogDebug("setPowered");
446 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
447 JSValueRef successCallback = NULL;
448 JSValueRef errorCallBack = NULL;
454 LogError("priv null");
455 Throw(UnknownException);
458 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_POWERED]);
460 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
462 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_POWERED]);
464 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
466 if (argumentCount < 2 || argumentCount > 3)
468 Throw(InvalidArgumentException);
471 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
473 Throw(ConversionException);
476 BluetoothConverter converter(priv->getContext());
477 successCallback = converter.toFunction(arguments[1]);
479 if (argumentCount >= 3)
481 errorCallBack = converter.toFunctionOrNull(arguments[2]);
484 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
488 Throw(UnknownException);
490 cbm->setOnSuccess(successCallback);
491 cbm->setOnError(errorCallBack);
493 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
494 EventBTSetPoweredPtr event(new EventBTSetPowered);
496 if (BluetoothAdapterManager == NULL || event == NULL)
498 LogError("BluetoothAdapterManager or event or listener NULL");
499 Throw(UnknownException);
502 if (converter.toBool(arguments[0]) == true)
511 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
512 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
513 event->setForAsynchronousCall(&listener);
514 BluetoothAdapterManager->setPowered(event);
515 return JSValueMakeNull(context);
517 Catch (WrtDeviceApis::Commons::ConversionException)
519 LogError("ConversionException");
520 return JSTizenExceptionFactory::postException(context, exception,
521 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
523 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
525 LogError("InvalidArgumentException");
526 return JSTizenExceptionFactory::postException(context, exception,
527 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
530 Catch (WrtDeviceApis::Commons::Exception)
532 LogError("Exception");
533 return JSTizenExceptionFactory::postException(context, exception,
534 JSTizenException::UNKNOWN_ERROR, "unknown error");
536 return JSValueMakeUndefined(context);
539 JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context, JSObjectRef object,
540 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
541 JSValueRef* exception)
543 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
544 JSValueRef successCallback = NULL;
545 JSValueRef errorCallBack = NULL;
551 LogError("priv null");
552 Throw(UnknownException);
555 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
557 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_VISIBLE]);
559 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
562 if (argumentCount < 2 || argumentCount > 4)
564 Throw(InvalidArgumentException);
566 BluetoothConverter converter(priv->getContext());
567 successCallback = converter.toFunction(arguments[1]);
569 if (argumentCount >= 3)
571 errorCallBack = converter.toFunctionOrNull(arguments[2]);
574 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
578 Throw(UnknownException);
581 cbm->setOnSuccess(successCallback);
582 cbm->setOnError(errorCallBack);
584 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
586 LogError("Wrong parameter passed, type mismatch");
587 Throw(ConversionException);
590 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
591 EventBTSetVisiblePtr event(new EventBTSetVisible);
593 if (BluetoothAdapterManager == NULL || event == NULL)
595 LogError("BluetoothAdapterManager or event or listener NULL");
596 Throw(UnknownException);
599 if (converter.toBool(arguments[0]) == true)
605 event->setInvisible();
608 if (argumentCount == 4)
610 if (JSValueIsNull(context, arguments[3]) == true)
612 event->setTimeout(0);
616 if (JSValueIsNumber(context, arguments[3]) == false)
618 LogError("Wrong parameter passed");
619 Throw(ConversionException);
622 LogDebug("timeout " << (unsigned short)converter.toInt(arguments[3]));
623 event->setTimeout((unsigned short)converter.toInt(arguments[3]));
627 event->setTimeout(0);
630 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
631 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
632 event->setForAsynchronousCall(&listener);
633 BluetoothAdapterManager->setVisible(event);
635 return JSValueMakeNull(context);
637 Catch (WrtDeviceApis::Commons::ConversionException)
639 LogError("ConversionException");
640 return JSTizenExceptionFactory::postException(context, exception,
641 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
644 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
646 LogError("InvalidArgumentException");
647 return JSTizenExceptionFactory::postException(context, exception,
648 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
651 Catch (WrtDeviceApis::Commons::Exception)
653 LogError("Exception");
654 return JSTizenExceptionFactory::postException(context, exception,
655 JSTizenException::UNKNOWN_ERROR, "unknown error");
658 return JSValueMakeUndefined(context);
661 JSValueRef JSBluetoothAdapter::discoveryDevices(JSContextRef context, JSObjectRef object,
662 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
663 JSValueRef* exception)
665 LogDebug("discoveryDevices");
666 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
672 LogError("priv null");
673 Throw(UnknownException);
676 if (argumentCount < 1 || argumentCount > 2)
678 LogError("InvalidArgumentException");
679 Throw(InvalidArgumentException);
682 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_DISCOVER_DEVICES]);
684 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
686 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_DISCOVER_DEVICES]);
689 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
691 JSValueRef errorArgument = JSValueMakeNull(context);
693 if (argumentCount >= 2)
695 errorArgument = arguments[1];
698 BluetoothConverter converter(priv->getContext());
699 EventBTOnDiscoveryDevicesPrivateDataPtr privData(
700 converter.toEventBTOnDiscoveryDevicesPrivateData(thisObject, arguments[0], errorArgument));
702 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
703 EventBTOnDiscoveryDevicesEmitterPtr emitter(new EventBTOnDiscoveryDevicesEmitter);
705 if (BluetoothAdapterManager == NULL || emitter == NULL || privData == NULL)
707 LogError("BluetoothAdapterManager or event or listener NULL");
708 Throw(UnknownException);
710 emitter->setListener(&BluetoothAdapterListener::getInstance());
711 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
712 BluetoothAdapterManager->discoveryDevicesEmitter(emitter);
714 return JSValueMakeNull(context);
717 Catch (WrtDeviceApis::Commons::ConversionException)
719 LogError("ConversionException");
720 return JSTizenExceptionFactory::postException(context, exception,
721 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
724 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
726 LogError("InvalidArgumentException");
727 return JSTizenExceptionFactory::postException(context, exception,
728 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
731 Catch (WrtDeviceApis::Commons::Exception)
733 LogError("Exception");
734 return JSTizenExceptionFactory::postException(context, exception,
735 JSTizenException::UNKNOWN_ERROR, "unknown error");
737 return JSValueMakeUndefined(context);
741 JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context, JSObjectRef object,
742 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
743 JSValueRef* exception)
745 LogDebug("discoveryDevices");
746 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
747 JSValueRef successCallback = NULL;
748 JSValueRef errorCallBack = NULL;
755 LogError("priv null");
756 Throw(UnknownException);
759 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_STOP_DISCOVERY]);
761 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
763 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_STOP_DISCOVERY]);
765 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
767 if (argumentCount < 1 || argumentCount > 2)
769 LogError("InvalidArgumentException");
770 Throw(InvalidArgumentException);
773 BluetoothConverter converter(priv->getContext());
774 successCallback = converter.toFunction(arguments[0]);
776 if (argumentCount >= 2)
778 errorCallBack = converter.toFunctionOrNull(arguments[1]);
782 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
786 Throw(UnknownException);
789 cbm->setOnSuccess(successCallback);
790 cbm->setOnError(errorCallBack);
792 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
793 EventBTStopDiscoveryPtr event(new EventBTStopDiscovery);
795 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
796 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
797 event->setForAsynchronousCall(&listener);
798 BluetoothAdapterManager->stopDiscovery(event);
800 return JSValueMakeNull(context);
802 Catch (WrtDeviceApis::Commons::ConversionException)
804 LogError("ConversionException");
805 return JSTizenExceptionFactory::postException(context, exception,
806 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
809 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
811 LogError("InvalidArgumentException");
812 return JSTizenExceptionFactory::postException(context, exception,
813 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
816 Catch (WrtDeviceApis::Commons::Exception)
818 LogError("Exception");
819 return JSTizenExceptionFactory::postException(context, exception,
820 JSTizenException::UNKNOWN_ERROR, "unknown error");
824 return JSValueMakeUndefined(context);
829 JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context, JSObjectRef object,
830 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
831 JSValueRef* exception)
833 LogDebug("getKnownDevices");
834 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
835 JSValueRef successCallback = NULL;
836 JSValueRef errorCallBack = NULL;
842 LogError("priv null");
843 Throw(UnknownException);
846 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
848 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_GET_KNOWN_DEVICES]);
851 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
853 if (argumentCount < 1 || argumentCount > 2)
855 LogError("InvalidArgumentException");
856 Throw(InvalidArgumentException);
859 BluetoothConverter converter(priv->getContext());
860 successCallback = converter.toFunction(arguments[0]);
862 if (argumentCount >= 2)
864 errorCallBack = converter.toFunctionOrNull(arguments[1]);
868 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
872 Throw(UnknownException);
875 cbm->setOnSuccess(successCallback);
876 cbm->setOnError(errorCallBack);
878 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
879 EventBTGetKnownDevicesPtr event(new EventBTGetKnownDevices);
882 if (BluetoothAdapterManager == NULL || event == NULL)
884 LogError("BluetoothAdapterManager or event or listener NULL");
885 Throw(UnknownException);
888 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
889 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
890 event->setForAsynchronousCall(&listener);
891 BluetoothAdapterManager->getKownDevices(event);
893 return JSValueMakeNull(context);
895 Catch (WrtDeviceApis::Commons::ConversionException)
897 LogError("ConversionException");
898 return JSTizenExceptionFactory::postException(context, exception,
899 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
902 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
904 LogError("InvalidArgumentException");
905 return JSTizenExceptionFactory::postException(context, exception,
906 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
909 Catch (WrtDeviceApis::Commons::Exception)
911 LogError("Exception");
912 return JSTizenExceptionFactory::postException(context, exception,
913 JSTizenException::UNKNOWN_ERROR, "unknown error");
916 return JSValueMakeUndefined(context);
919 JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context, JSObjectRef object,
920 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
921 JSValueRef* exception)
924 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
925 JSValueRef successCallback = NULL;
926 JSValueRef errorCallBack = NULL;
933 LogError("priv null");
934 Throw(UnknownException);
937 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
939 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_GET_DEVICE]);
941 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
944 if (argumentCount < 2 || argumentCount > 3)
946 LogError("Wrong parameter count passed");
947 Throw(InvalidArgumentException);
950 BluetoothConverter converter(priv->getContext());
951 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
952 successCallback = converter.toFunction(arguments[1]);
954 if (argumentCount >= 3)
956 errorCallBack = converter.toFunctionOrNull(arguments[2]);
959 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
963 Throw(UnknownException);
966 cbm->setOnSuccess(successCallback);
967 cbm->setOnError(errorCallBack);
969 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
970 EventBTGetDevicePtr event(new EventBTGetDevice);
972 if (BluetoothAdapterManager == NULL || event == NULL)
974 LogError("BluetoothAdapterManager or event or listener NULL");
975 Throw(UnknownException);
978 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
979 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
980 event->setForAsynchronousCall(&listener);
981 event->setAddress(address);
982 BluetoothAdapterManager->getDevice(event);
984 return JSValueMakeNull(context);
986 Catch (WrtDeviceApis::Commons::ConversionException)
988 LogError("ConversionException");
989 return JSTizenExceptionFactory::postException(context, exception,
990 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
993 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
995 LogError("InvalidArgumentException");
996 return JSTizenExceptionFactory::postException(context, exception,
997 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1000 Catch (WrtDeviceApis::Commons::Exception)
1002 LogError("Exception");
1003 return JSTizenExceptionFactory::postException(context, exception,
1004 JSTizenException::UNKNOWN_ERROR, "unknown error");
1008 return JSValueMakeUndefined(context);}
1011 JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context, JSObjectRef object,
1012 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1013 JSValueRef* exception)
1015 LogDebug("createBonding");
1017 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1018 JSValueRef successCallback = NULL;
1019 JSValueRef errorCallBack = NULL;
1026 LogError("priv null");
1027 Throw(UnknownException);
1030 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1032 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_CREATE_BONDING]);
1035 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1037 if (argumentCount < 2 || argumentCount > 3)
1039 LogError("Wrong parameter count passed");
1040 Throw(InvalidArgumentException);
1045 BluetoothConverter converter(priv->getContext());
1046 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1047 successCallback = converter.toFunction(arguments[1]);
1049 if (argumentCount >= 3)
1051 errorCallBack = converter.toFunctionOrNull(arguments[2]);
1054 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1058 Throw(UnknownException);
1061 cbm->setOnSuccess(successCallback);
1062 cbm->setOnError(errorCallBack);
1065 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1066 EventBTCreateDestroyBondingPtr event(new EventBTCreateDestroyBonding);
1068 if (BluetoothAdapterManager == NULL || event == NULL)
1070 LogError("BluetoothAdapterManager or event or listener NULL");
1071 Throw(UnknownException);
1074 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1075 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1076 event->setForAsynchronousCall(&listener);
1077 event->setAddress(address);
1078 event->setCreateBonding();
1079 BluetoothAdapterManager->createBonding(event);
1080 return JSValueMakeNull(context);
1082 Catch (WrtDeviceApis::Commons::ConversionException)
1084 LogError("ConversionException");
1085 return JSTizenExceptionFactory::postException(context, exception,
1086 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1089 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1091 LogError("InvalidArgumentException");
1092 return JSTizenExceptionFactory::postException(context, exception,
1093 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1096 Catch (WrtDeviceApis::Commons::Exception)
1098 LogError("Exception");
1099 return JSTizenExceptionFactory::postException(context, exception,
1100 JSTizenException::UNKNOWN_ERROR, "unknown error");
1103 return JSValueMakeUndefined(context);
1107 JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context, JSObjectRef object,
1108 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1109 JSValueRef* exception)
1112 LogDebug("destroyBonding");
1114 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1115 JSValueRef successCallback = NULL;
1116 JSValueRef errorCallBack = NULL;
1122 LogError("priv null");
1123 Throw(UnknownException);
1126 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1128 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_DESTROY_BONDING]);
1130 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1132 if (argumentCount < 2 || argumentCount > 3)
1134 LogError("Wrong parameter count passed");
1135 return JSTizenExceptionFactory::postException(context, exception,
1136 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1139 BluetoothConverter converter(priv->getContext());
1140 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1141 successCallback = converter.toFunction(arguments[1]);
1143 if (argumentCount >= 3)
1145 errorCallBack = converter.toFunctionOrNull(arguments[2]);
1148 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1152 Throw(UnknownException);
1155 cbm->setOnSuccess(successCallback);
1156 cbm->setOnError(errorCallBack);
1158 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1159 EventBTCreateDestroyBondingPtr event(new EventBTCreateDestroyBonding);
1161 if (BluetoothAdapterManager == NULL || event == NULL)
1163 LogError("BluetoothAdapterManager or event or listener NULL");
1164 Throw(UnknownException);
1167 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1168 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1169 event->setForAsynchronousCall(&listener);
1170 event->setDestroyBonding();
1171 event->setAddress(address);
1172 BluetoothAdapterManager->createBonding(event);
1174 return JSValueMakeNull(context);
1176 Catch (WrtDeviceApis::Commons::ConversionException)
1178 LogError("ConversionException");
1179 return JSTizenExceptionFactory::postException(context, exception,
1180 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1183 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1185 LogError("InvalidArgumentException");
1186 return JSTizenExceptionFactory::postException(context, exception,
1187 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1190 Catch (WrtDeviceApis::Commons::Exception)
1192 LogError("Exception");
1193 return JSTizenExceptionFactory::postException(context, exception,
1194 JSTizenException::UNKNOWN_ERROR, "unknown error");
1198 return JSValueMakeUndefined(context);
1202 JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context, JSObjectRef object,
1203 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1204 JSValueRef* exception)
1206 LogDebug("registerRFCOMMServiceByUUID");
1208 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1209 JSValueRef successCallback = NULL;
1210 JSValueRef errorCallBack = NULL;
1214 std::string serviceName, uuid;
1215 unsigned short securityLevel= SECURITY_LEVEL_HIGH_VALUE_INT;
1216 Validator validator(context);
1218 LogDebug("context address" << context << "priv-context" << priv->getContext());
1222 LogError("priv null");
1223 Throw(UnknownException);
1226 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1228 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_REGISTER_RFCOMM]);
1230 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1233 if (argumentCount < 3 || argumentCount > 5)
1235 LogError("Wrong parameter count passed");
1236 Throw(InvalidArgumentException);
1239 BluetoothConverter converter(priv->getContext());
1240 uuid = converter.toBluetoothUUIDString(arguments[0]);
1242 if (!JSValueIsString(context, arguments[1]) || JSValueIsNull(context, arguments[1]))
1244 ThrowMsg(ConversionException, "Not a string");
1246 serviceName = converter.toString(arguments[1]);
1247 successCallback = converter.toFunction(arguments[2]);
1249 if (argumentCount >= 4)
1251 errorCallBack = converter.toFunctionOrNull(arguments[3]);
1254 if (argumentCount >= 5)
1256 securityLevel = converter.toBluetoothSecurityLevelInt(arguments[4]);
1259 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1263 Throw(UnknownException);
1266 cbm->setOnSuccess(successCallback);
1267 cbm->setOnError(errorCallBack);
1269 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1270 EventBTRegisterRFCOMMPtr event(new EventBTRegisterRFCOMM);
1272 if (BluetoothAdapterManager == NULL || event == NULL)
1274 LogError("BluetoothAdapterManager or event or listener NULL");
1275 Throw(UnknownException);
1277 LogDebug("UUid:" << uuid << " serviceName:" << serviceName);
1279 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1280 event->setForAsynchronousCall(&listener);
1281 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1282 event->setUuid(uuid);
1283 event->setSecurityLevel(securityLevel);
1284 event->setName(serviceName);
1285 BluetoothAdapterManager->registerRFCOMMServiceByUUID(event);
1287 return JSValueMakeNull(context);
1289 Catch (WrtDeviceApis::Commons::ConversionException)
1291 LogError("ConversionException");
1292 return JSTizenExceptionFactory::postException(context, exception,
1293 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1296 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1298 LogError("InvalidArgumentException");
1299 return JSTizenExceptionFactory::postException(context, exception,
1300 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1303 Catch (WrtDeviceApis::Commons::UnsupportedException)
1305 LogError("NotsupportedException");
1306 return JSTizenExceptionFactory::postException(context, exception,
1307 JSTizenException::NOT_SUPPORTED_ERROR, "not support error");
1310 Catch (WrtDeviceApis::Commons::Exception)
1312 LogError("Exception");
1313 return JSTizenExceptionFactory::postException(context, exception,
1314 JSTizenException::UNKNOWN_ERROR, "unknown error");
1317 return JSValueMakeUndefined(context);