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::TYPE_MISMATCH_ERROR, "type mismatch error");
123 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
125 LogError("InvalidArgumentException");
126 return JSTizenExceptionFactory::postException(context, exception,
127 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
129 Catch(WrtDeviceApis::Commons::UnsupportedException)
131 LogError("UnsupportException");
132 return JSTizenExceptionFactory::postException(context, exception,
133 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
135 Catch(WrtDeviceApis::Commons::Exception)
137 LogError("UnkownException");
138 return JSTizenExceptionFactory::postException(context, exception,
139 JSTizenException::UNKNOWN_ERROR, "Unkown error");
141 return JSValueMakeUndefined(context);
144 JSValueRef JSBluetoothAdapter::setName(JSContextRef context, JSObjectRef object,
145 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
146 JSValueRef* exception)
149 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
150 JSValueRef successCallback = NULL;
151 JSValueRef errorCallBack = NULL;
158 Throw(UnknownException);
161 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
163 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_NAME]);
165 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
167 if (argumentCount < 1 || argumentCount > 3)
169 Throw(InvalidArgumentException);
172 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsString(context, arguments[0]) == false)
174 Throw(ConversionException);
177 BluetoothConverter converter(priv->getContext());
179 if (argumentCount >= 2)
181 successCallback = converter.toFunctionOrNull(arguments[1]);
184 if (argumentCount >= 3)
186 errorCallBack = converter.toFunctionOrNull(arguments[2]);
189 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
193 Throw(UnknownException);
196 cbm->setOnSuccess(successCallback);
197 cbm->setOnError(errorCallBack);
199 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
200 std::string name = converter.toString(arguments[0]);
202 if (name != BluetoothAdapterManager->getName())
204 EventBTSetNamePtr event(new EventBTSetName);
206 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
207 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
208 event->setForAsynchronousCall(&listener);
209 event->setName(name);
210 BluetoothAdapterManager->setAdapterName(event);
214 LogDebug("same name is already set");
215 cbm->callOnSuccess();
217 return JSValueMakeNull(context);
219 Catch (WrtDeviceApis::Commons::ConversionException)
221 LogError("ConversionException");
222 return JSTizenExceptionFactory::postException(context, exception,
223 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
226 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
228 LogError("InvalidArgumentException");
229 return JSTizenExceptionFactory::postException(context, exception,
230 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
232 Catch(WrtDeviceApis::Commons::UnsupportedException)
234 LogError("UnsupportException");
235 return JSTizenExceptionFactory::postException(context, exception,
236 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
238 Catch (WrtDeviceApis::Commons::Exception)
240 LogError("UnkownException");
241 return JSTizenExceptionFactory::postException(context, exception,
242 JSTizenException::UNKNOWN_ERROR, "Unkown error");
244 return JSValueMakeUndefined(context);
248 JSValueRef JSBluetoothAdapter::getAddress(JSContextRef context,
250 JSStringRef propertyName,
251 JSValueRef* exception)
255 Converter converter(context);
256 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
262 Throw(UnknownException);
264 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
265 std::string address = BluetoothAdapterManager->getAddress();
266 return converter.toJSValueRef(address);
268 Catch (WrtDeviceApis::Commons::ConversionException)
270 LogError("ConversionException");
271 return JSTizenExceptionFactory::postException(context, exception,
272 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
275 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
277 LogError("InvalidArgumentException");
278 return JSTizenExceptionFactory::postException(context, exception,
279 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
281 Catch(WrtDeviceApis::Commons::UnsupportedException)
283 LogError("UnsupportException");
284 return JSTizenExceptionFactory::postException(context, exception,
285 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
287 Catch (WrtDeviceApis::Commons::Exception)
289 LogError("UnkownException");
290 return JSTizenExceptionFactory::postException(context, exception,
291 JSTizenException::UNKNOWN_ERROR, "Unkown error");
294 return JSValueMakeUndefined(context);
299 JSValueRef JSBluetoothAdapter::getVisible(JSContextRef context,
301 JSStringRef propertyName,
302 JSValueRef* exception)
306 Converter converter(context);
307 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
312 Throw(UnknownException);
315 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
316 bool visibility = BluetoothAdapterManager->getVisibility();
317 return converter.toJSValueRef(visibility);
319 Catch (WrtDeviceApis::Commons::ConversionException)
321 LogError("ConversionException");
322 return JSTizenExceptionFactory::postException(context, exception,
323 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
326 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
328 LogError("InvalidArgumentException");
329 return JSTizenExceptionFactory::postException(context, exception,
330 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
332 Catch(WrtDeviceApis::Commons::UnsupportedException)
334 LogError("UnsupportException");
335 return JSTizenExceptionFactory::postException(context, exception,
336 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
338 Catch (WrtDeviceApis::Commons::Exception)
340 LogError("UnkownException");
341 return JSTizenExceptionFactory::postException(context, exception,
342 JSTizenException::UNKNOWN_ERROR, "Unkown error");
344 return JSValueMakeUndefined(context);
350 const JSClassRef JSBluetoothAdapter::getClassRef()
354 m_jsClassRef = JSClassCreate(&m_classInfo);
359 const JSClassDefinition* JSBluetoothAdapter::getClassInfo()
365 void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object)
367 JSBluetoothAdapterPriv* priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
371 LogDebug("Already Exist");
376 IBluetoothAdapterManagerPtr BluetoothAdapterManager(BluetoothFactory::getInstance().getBluetoothAdapterManager());
377 priv = new JSBluetoothAdapterPriv( context, BluetoothAdapterManager);
378 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
380 LogError("Object can't store private data.");
383 LogDebug("JSBluetoothAdapter::initialize ");
387 void JSBluetoothAdapter::finalize(JSObjectRef object)
389 LogDebug("JSBluetoothAdapter::Finalrize");
391 JSBluetoothAdapterPriv* priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
395 JSObjectSetPrivate(object, NULL);
396 LogDebug("Deleting BluetoothAdapterManager");
403 bool JSBluetoothAdapter::hasInstance(JSContextRef context, JSObjectRef constructor,
404 JSValueRef possibleInstance, JSValueRef* exception)
406 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
410 JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context)
412 IBluetoothAdapterManagerPtr BluetoothAdapterManager(BluetoothFactory::getInstance().getBluetoothAdapterManager());
413 JSBluetoothAdapterPriv* priv = new JSBluetoothAdapterPriv( context, BluetoothAdapterManager);
415 return JSObjectMake(context, getClassRef(), priv);
419 JSValueRef JSBluetoothAdapter::getPowered(JSContextRef context,
421 JSStringRef propertyName,
422 JSValueRef* exception)
427 Converter converter(context);
428 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
434 Throw(UnknownException);
437 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
438 bool powered = BluetoothAdapterManager->getPowered();
439 return converter.toJSValueRef(powered);
441 Catch (WrtDeviceApis::Commons::ConversionException)
443 LogError("ConversionException");
444 return JSTizenExceptionFactory::postException(context, exception,
445 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
448 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
450 LogError("InvalidArgumentException");
451 return JSTizenExceptionFactory::postException(context, exception,
452 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
454 Catch(WrtDeviceApis::Commons::UnsupportedException)
456 LogError("UnsupportException");
457 return JSTizenExceptionFactory::postException(context, exception,
458 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
460 Catch (WrtDeviceApis::Commons::Exception)
462 LogError("UnkownException");
463 return JSTizenExceptionFactory::postException(context, exception,
464 JSTizenException::UNKNOWN_ERROR, "Unkown error");
467 return JSValueMakeUndefined(context);
471 JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context, JSObjectRef object,
472 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
473 JSValueRef* exception)
476 LogDebug("setPowered");
478 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
479 JSValueRef successCallback = NULL;
480 JSValueRef errorCallBack = NULL;
486 LogError("priv null");
487 Throw(UnknownException);
490 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_POWERED]);
492 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
494 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_POWERED]);
496 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
498 if (argumentCount < 1 || argumentCount > 3)
500 Throw(InvalidArgumentException);
503 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
505 Throw(ConversionException);
508 BluetoothConverter converter(priv->getContext());
510 if (argumentCount >= 2)
512 successCallback = converter.toFunctionOrNull(arguments[1]);
515 if (argumentCount >= 3)
517 errorCallBack = converter.toFunctionOrNull(arguments[2]);
520 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
524 Throw(UnknownException);
526 cbm->setOnSuccess(successCallback);
527 cbm->setOnError(errorCallBack);
529 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
530 EventBTSetPoweredPtr event(new EventBTSetPowered);
532 if (BluetoothAdapterManager == NULL || event == NULL)
534 LogError("BluetoothAdapterManager or event or listener NULL");
535 Throw(UnknownException);
538 if (converter.toBool(arguments[0]) == true)
547 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
548 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
549 event->setForAsynchronousCall(&listener);
550 BluetoothAdapterManager->setPowered(event);
551 return JSValueMakeNull(context);
553 Catch (WrtDeviceApis::Commons::ConversionException)
555 LogError("ConversionException");
556 return JSTizenExceptionFactory::postException(context, exception,
557 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
560 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
562 LogError("InvalidArgumentException");
563 return JSTizenExceptionFactory::postException(context, exception,
564 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
566 Catch(WrtDeviceApis::Commons::UnsupportedException)
568 LogError("UnsupportException");
569 return JSTizenExceptionFactory::postException(context, exception,
570 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
572 Catch (WrtDeviceApis::Commons::Exception)
574 LogError("Exception");
575 return JSTizenExceptionFactory::postException(context, exception,
576 JSTizenException::UNKNOWN_ERROR, "unknown error");
578 return JSValueMakeUndefined(context);
581 JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context, JSObjectRef object,
582 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
583 JSValueRef* exception)
585 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
586 JSValueRef successCallback = NULL;
587 JSValueRef errorCallBack = NULL;
593 LogError("priv null");
594 Throw(UnknownException);
597 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
599 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_VISIBLE]);
601 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
604 if (argumentCount < 1 || argumentCount > 4)
606 Throw(InvalidArgumentException);
609 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
611 Throw(ConversionException);
614 BluetoothConverter converter(priv->getContext());
616 if (argumentCount >= 2)
618 successCallback = converter.toFunctionOrNull(arguments[1]);
621 if (argumentCount >= 3)
623 errorCallBack = converter.toFunctionOrNull(arguments[2]);
626 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
630 Throw(UnknownException);
633 cbm->setOnSuccess(successCallback);
634 cbm->setOnError(errorCallBack);
636 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
638 LogError("Wrong parameter passed, type mismatch");
639 Throw(ConversionException);
642 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
643 EventBTSetVisiblePtr event(new EventBTSetVisible);
645 if (BluetoothAdapterManager == NULL || event == NULL)
647 LogError("BluetoothAdapterManager or event or listener NULL");
648 Throw(UnknownException);
651 if (converter.toBool(arguments[0]) == true)
657 event->setInvisible();
660 if (argumentCount == 4)
662 if (JSValueIsNull(context, arguments[3]) == true)
664 event->setTimeout(0);
668 if (JSValueIsNumber(context, arguments[3]) == false)
670 LogError("Wrong parameter passed");
671 Throw(ConversionException);
674 LogDebug("timeout " << (unsigned short)converter.toInt(arguments[3]));
675 event->setTimeout((unsigned short)converter.toInt(arguments[3]));
679 event->setTimeout(0);
682 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
683 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
684 event->setForAsynchronousCall(&listener);
685 BluetoothAdapterManager->setVisible(event);
687 return JSValueMakeNull(context);
689 Catch (WrtDeviceApis::Commons::ConversionException)
691 LogError("ConversionException");
692 return JSTizenExceptionFactory::postException(context, exception,
693 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
696 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
698 LogError("InvalidArgumentException");
699 return JSTizenExceptionFactory::postException(context, exception,
700 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
702 Catch(WrtDeviceApis::Commons::UnsupportedException)
704 LogError("UnsupportException");
705 return JSTizenExceptionFactory::postException(context, exception,
706 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
708 Catch (WrtDeviceApis::Commons::Exception)
710 LogError("Exception");
711 return JSTizenExceptionFactory::postException(context, exception,
712 JSTizenException::UNKNOWN_ERROR, "unknown error");
715 return JSValueMakeUndefined(context);
718 JSValueRef JSBluetoothAdapter::discoveryDevices(JSContextRef context, JSObjectRef object,
719 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
720 JSValueRef* exception)
722 LogDebug("discoveryDevices");
723 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
729 LogError("priv null");
730 Throw(UnknownException);
733 if (argumentCount < 1 || argumentCount > 2)
735 LogError("InvalidArgumentException");
736 Throw(InvalidArgumentException);
739 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_DISCOVER_DEVICES]);
741 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
743 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_DISCOVER_DEVICES]);
746 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
748 JSValueRef errorArgument = JSValueMakeNull(context);
750 if (argumentCount >= 2)
752 errorArgument = arguments[1];
755 BluetoothConverter converter(priv->getContext());
756 EventBTOnDiscoveryDevicesPrivateDataPtr privData(
757 converter.toEventBTOnDiscoveryDevicesPrivateData(thisObject, arguments[0], errorArgument));
759 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
760 EventBTOnDiscoveryDevicesEmitterPtr emitter(new EventBTOnDiscoveryDevicesEmitter);
762 if (BluetoothAdapterManager == NULL || emitter == NULL || privData == NULL)
764 LogError("BluetoothAdapterManager or event or listener NULL");
765 Throw(UnknownException);
767 emitter->setListener(&BluetoothAdapterListener::getInstance());
768 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
769 BluetoothAdapterManager->discoveryDevicesEmitter(emitter);
771 return JSValueMakeNull(context);
774 Catch (WrtDeviceApis::Commons::ConversionException)
776 LogError("ConversionException");
777 return JSTizenExceptionFactory::postException(context, exception,
778 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
781 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
783 LogError("InvalidArgumentException");
784 return JSTizenExceptionFactory::postException(context, exception,
785 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
787 Catch(WrtDeviceApis::Commons::UnsupportedException)
789 LogError("UnsupportException");
790 return JSTizenExceptionFactory::postException(context, exception,
791 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
793 Catch (WrtDeviceApis::Commons::Exception)
795 LogError("Exception");
796 return JSTizenExceptionFactory::postException(context, exception,
797 JSTizenException::UNKNOWN_ERROR, "unknown error");
799 return JSValueMakeUndefined(context);
803 JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context, JSObjectRef object,
804 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
805 JSValueRef* exception)
807 LogDebug("discoveryDevices");
808 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
809 JSValueRef successCallback = NULL;
810 JSValueRef errorCallBack = NULL;
817 LogError("priv null");
818 Throw(UnknownException);
821 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_STOP_DISCOVERY]);
823 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
825 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_STOP_DISCOVERY]);
827 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
829 if (argumentCount > 2)
831 LogError("InvalidArgumentException");
832 Throw(InvalidArgumentException);
835 BluetoothConverter converter(priv->getContext());
837 if (argumentCount >= 1)
839 successCallback = converter.toFunctionOrNull(arguments[0]);
842 if (argumentCount >= 2)
844 errorCallBack = converter.toFunctionOrNull(arguments[1]);
848 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
852 Throw(UnknownException);
855 cbm->setOnSuccess(successCallback);
856 cbm->setOnError(errorCallBack);
858 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
859 EventBTStopDiscoveryPtr event(new EventBTStopDiscovery);
861 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
862 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
863 event->setForAsynchronousCall(&listener);
864 BluetoothAdapterManager->stopDiscovery(event);
866 return JSValueMakeNull(context);
868 Catch (WrtDeviceApis::Commons::ConversionException)
870 LogError("ConversionException");
871 return JSTizenExceptionFactory::postException(context, exception,
872 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
875 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
877 LogError("InvalidArgumentException");
878 return JSTizenExceptionFactory::postException(context, exception,
879 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
881 Catch(WrtDeviceApis::Commons::UnsupportedException)
883 LogError("UnsupportException");
884 return JSTizenExceptionFactory::postException(context, exception,
885 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
887 Catch (WrtDeviceApis::Commons::Exception)
889 LogError("Exception");
890 return JSTizenExceptionFactory::postException(context, exception,
891 JSTizenException::UNKNOWN_ERROR, "unknown error");
895 return JSValueMakeUndefined(context);
900 JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context, JSObjectRef object,
901 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
902 JSValueRef* exception)
904 LogDebug("getKnownDevices");
905 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
906 JSValueRef successCallback = NULL;
907 JSValueRef errorCallBack = NULL;
913 LogError("priv null");
914 Throw(UnknownException);
917 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
919 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_GET_KNOWN_DEVICES]);
922 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
924 if (argumentCount < 1 || argumentCount > 2)
926 LogError("InvalidArgumentException");
927 Throw(InvalidArgumentException);
930 BluetoothConverter converter(priv->getContext());
931 successCallback = converter.toFunction(arguments[0]);
933 if (argumentCount >= 2)
935 errorCallBack = converter.toFunctionOrNull(arguments[1]);
939 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
943 Throw(UnknownException);
946 cbm->setOnSuccess(successCallback);
947 cbm->setOnError(errorCallBack);
949 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
950 EventBTGetKnownDevicesPtr event(new EventBTGetKnownDevices);
953 if (BluetoothAdapterManager == NULL || event == NULL)
955 LogError("BluetoothAdapterManager or event or listener NULL");
956 Throw(UnknownException);
959 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
960 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
961 event->setForAsynchronousCall(&listener);
962 BluetoothAdapterManager->getKownDevices(event);
964 return JSValueMakeNull(context);
966 Catch (WrtDeviceApis::Commons::ConversionException)
968 LogError("ConversionException");
969 return JSTizenExceptionFactory::postException(context, exception,
970 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
973 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
975 LogError("InvalidArgumentException");
976 return JSTizenExceptionFactory::postException(context, exception,
977 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
979 Catch(WrtDeviceApis::Commons::UnsupportedException)
981 LogError("UnsupportException");
982 return JSTizenExceptionFactory::postException(context, exception,
983 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
985 Catch (WrtDeviceApis::Commons::Exception)
987 LogError("Exception");
988 return JSTizenExceptionFactory::postException(context, exception,
989 JSTizenException::UNKNOWN_ERROR, "unknown error");
992 return JSValueMakeUndefined(context);
995 JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context, JSObjectRef object,
996 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
997 JSValueRef* exception)
1000 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1001 JSValueRef successCallback = NULL;
1002 JSValueRef errorCallBack = NULL;
1009 LogError("priv null");
1010 Throw(UnknownException);
1013 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1015 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_GET_DEVICE]);
1017 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1020 if (argumentCount < 2 || argumentCount > 3)
1022 LogError("Wrong parameter count passed");
1023 Throw(InvalidArgumentException);
1026 BluetoothConverter converter(priv->getContext());
1027 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1028 successCallback = converter.toFunction(arguments[1]);
1030 if (argumentCount >= 3)
1032 errorCallBack = converter.toFunctionOrNull(arguments[2]);
1035 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1039 Throw(UnknownException);
1042 cbm->setOnSuccess(successCallback);
1043 cbm->setOnError(errorCallBack);
1045 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1046 EventBTGetDevicePtr event(new EventBTGetDevice);
1048 if (BluetoothAdapterManager == NULL || event == NULL)
1050 LogError("BluetoothAdapterManager or event or listener NULL");
1051 Throw(UnknownException);
1054 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1055 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1056 event->setForAsynchronousCall(&listener);
1057 event->setAddress(address);
1058 BluetoothAdapterManager->getDevice(event);
1060 return JSValueMakeNull(context);
1062 Catch (WrtDeviceApis::Commons::ConversionException)
1064 LogError("ConversionException");
1065 return JSTizenExceptionFactory::postException(context, exception,
1066 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1069 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1071 LogError("InvalidArgumentException");
1072 return JSTizenExceptionFactory::postException(context, exception,
1073 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
1075 Catch(WrtDeviceApis::Commons::UnsupportedException)
1077 LogError("UnsupportException");
1078 return JSTizenExceptionFactory::postException(context, exception,
1079 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
1081 Catch (WrtDeviceApis::Commons::Exception)
1083 LogError("Exception");
1084 return JSTizenExceptionFactory::postException(context, exception,
1085 JSTizenException::UNKNOWN_ERROR, "unknown error");
1089 return JSValueMakeUndefined(context);}
1092 JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context, JSObjectRef object,
1093 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1094 JSValueRef* exception)
1096 LogDebug("createBonding");
1098 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1099 JSValueRef successCallback = NULL;
1100 JSValueRef errorCallBack = NULL;
1107 LogError("priv null");
1108 Throw(UnknownException);
1111 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1113 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_CREATE_BONDING]);
1116 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1118 if (argumentCount < 2 || argumentCount > 3)
1120 LogError("Wrong parameter count passed");
1121 Throw(InvalidArgumentException);
1126 BluetoothConverter converter(priv->getContext());
1127 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1128 successCallback = converter.toFunction(arguments[1]);
1130 if (argumentCount >= 3)
1132 errorCallBack = converter.toFunctionOrNull(arguments[2]);
1135 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1139 Throw(UnknownException);
1142 cbm->setOnSuccess(successCallback);
1143 cbm->setOnError(errorCallBack);
1146 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1147 EventBTCreateDestroyBondingPtr event(new EventBTCreateDestroyBonding);
1149 if (BluetoothAdapterManager == NULL || event == NULL)
1151 LogError("BluetoothAdapterManager or event or listener NULL");
1152 Throw(UnknownException);
1155 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1156 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1157 event->setForAsynchronousCall(&listener);
1158 event->setAddress(address);
1159 event->setCreateBonding();
1160 BluetoothAdapterManager->createBonding(event);
1161 return JSValueMakeNull(context);
1163 Catch (WrtDeviceApis::Commons::ConversionException)
1165 LogError("ConversionException");
1166 return JSTizenExceptionFactory::postException(context, exception,
1167 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1170 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1172 LogError("InvalidArgumentException");
1173 return JSTizenExceptionFactory::postException(context, exception,
1174 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
1176 Catch(WrtDeviceApis::Commons::UnsupportedException)
1178 LogError("UnsupportException");
1179 return JSTizenExceptionFactory::postException(context, exception,
1180 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
1182 Catch (WrtDeviceApis::Commons::Exception)
1184 LogError("Exception");
1185 return JSTizenExceptionFactory::postException(context, exception,
1186 JSTizenException::UNKNOWN_ERROR, "unknown error");
1189 return JSValueMakeUndefined(context);
1193 JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context, JSObjectRef object,
1194 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1195 JSValueRef* exception)
1198 LogDebug("destroyBonding");
1200 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1201 JSValueRef successCallback = NULL;
1202 JSValueRef errorCallBack = NULL;
1208 LogError("priv null");
1209 Throw(UnknownException);
1212 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1214 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_DESTROY_BONDING]);
1216 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1218 if (argumentCount < 1 || argumentCount > 3)
1220 LogError("Wrong parameter count passed");
1221 return JSTizenExceptionFactory::postException(context, exception,
1222 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1225 BluetoothConverter converter(priv->getContext());
1226 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1228 if (argumentCount >= 2)
1230 successCallback = converter.toFunctionOrNull(arguments[1]);
1233 if (argumentCount >= 3)
1235 errorCallBack = converter.toFunctionOrNull(arguments[2]);
1238 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1242 Throw(UnknownException);
1245 cbm->setOnSuccess(successCallback);
1246 cbm->setOnError(errorCallBack);
1248 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1249 EventBTCreateDestroyBondingPtr event(new EventBTCreateDestroyBonding);
1251 if (BluetoothAdapterManager == NULL || event == NULL)
1253 LogError("BluetoothAdapterManager or event or listener NULL");
1254 Throw(UnknownException);
1257 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1258 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1259 event->setForAsynchronousCall(&listener);
1260 event->setDestroyBonding();
1261 event->setAddress(address);
1262 BluetoothAdapterManager->createBonding(event);
1264 return JSValueMakeNull(context);
1266 Catch (WrtDeviceApis::Commons::ConversionException)
1268 LogError("ConversionException");
1269 return JSTizenExceptionFactory::postException(context, exception,
1270 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1273 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1275 LogError("InvalidArgumentException");
1276 return JSTizenExceptionFactory::postException(context, exception,
1277 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
1279 Catch(WrtDeviceApis::Commons::UnsupportedException)
1281 LogError("UnsupportException");
1282 return JSTizenExceptionFactory::postException(context, exception,
1283 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
1285 Catch (WrtDeviceApis::Commons::Exception)
1287 LogError("Exception");
1288 return JSTizenExceptionFactory::postException(context, exception,
1289 JSTizenException::UNKNOWN_ERROR, "unknown error");
1293 return JSValueMakeUndefined(context);
1297 JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context, JSObjectRef object,
1298 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1299 JSValueRef* exception)
1301 LogDebug("registerRFCOMMServiceByUUID");
1303 JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1304 JSValueRef successCallback = NULL;
1305 JSValueRef errorCallBack = NULL;
1309 std::string serviceName, uuid;
1310 unsigned short securityLevel= SECURITY_LEVEL_HIGH_VALUE_INT;
1311 Validator validator(context);
1313 LogDebug("context address" << context << "priv-context" << priv->getContext());
1317 LogError("priv null");
1318 Throw(UnknownException);
1321 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1323 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_REGISTER_RFCOMM]);
1325 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1328 if (argumentCount < 3 || argumentCount > 5)
1330 LogError("Wrong parameter count passed");
1331 Throw(InvalidArgumentException);
1334 BluetoothConverter converter(priv->getContext());
1335 uuid = converter.toBluetoothUUIDString(arguments[0]);
1337 if (!JSValueIsString(context, arguments[1]) || JSValueIsNull(context, arguments[1]))
1339 ThrowMsg(ConversionException, "Not a string");
1341 serviceName = converter.toString(arguments[1]);
1342 successCallback = converter.toFunction(arguments[2]);
1344 if (argumentCount >= 4)
1346 errorCallBack = converter.toFunctionOrNull(arguments[3]);
1349 if (argumentCount >= 5)
1351 securityLevel = converter.toBluetoothSecurityLevelInt(arguments[4]);
1354 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1358 Throw(UnknownException);
1361 cbm->setOnSuccess(successCallback);
1362 cbm->setOnError(errorCallBack);
1364 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1365 EventBTRegisterRFCOMMPtr event(new EventBTRegisterRFCOMM);
1367 if (BluetoothAdapterManager == NULL || event == NULL)
1369 LogError("BluetoothAdapterManager or event or listener NULL");
1370 Throw(UnknownException);
1372 LogDebug("UUid:" << uuid << " serviceName:" << serviceName);
1374 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1375 event->setForAsynchronousCall(&listener);
1376 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1377 event->setUuid(uuid);
1378 event->setSecurityLevel(securityLevel);
1379 event->setName(serviceName);
1380 BluetoothAdapterManager->registerRFCOMMServiceByUUID(event);
1382 return JSValueMakeNull(context);
1384 Catch (WrtDeviceApis::Commons::ConversionException)
1386 LogError("ConversionException");
1387 return JSTizenExceptionFactory::postException(context, exception,
1388 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1391 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
1393 LogError("InvalidArgumentException");
1394 return JSTizenExceptionFactory::postException(context, exception,
1395 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
1397 Catch(WrtDeviceApis::Commons::UnsupportedException)
1399 LogError("UnsupportException");
1400 return JSTizenExceptionFactory::postException(context, exception,
1401 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
1403 Catch (WrtDeviceApis::Commons::Exception)
1405 LogError("Exception");
1406 return JSTizenExceptionFactory::postException(context, exception,
1407 JSTizenException::UNKNOWN_ERROR, "unknown error");
1410 return JSValueMakeUndefined(context);