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.
17 #include <CommonsJavaScript/Converter.h>
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <Tizen/Common/SecurityExceptions.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include "JSBluetoothDevice.h"
26 #include <API/Bluetooth/IBluetoothDeviceManager.h>
27 #include <API/Bluetooth/BluetoothFactory.h>
28 #include "BluetoothDeviceManagerListener.h"
29 #include <API/Bluetooth/BluetoothProperty.h>
30 #include "JSBluetoothClass.h"
31 #include "BluetoothConverter.h"
32 #include "plugin_config.h"
36 using namespace WrtDeviceApis;
37 using namespace TizenApis::Commons;
43 JSClassDefinition JSBluetoothDevice::m_classInfo =
46 kJSClassAttributeNone,
64 JSStaticFunction JSBluetoothDevice::m_function[] =
66 { "connectToServiceByUUID", JSBluetoothDevice::connectToServiceByUUID, kJSPropertyAttributeNone },
70 JSStaticValue JSBluetoothDevice::m_properties[] = {
71 {"name", getName, NULL, kJSPropertyAttributeReadOnly},
72 {"address", getAddress, NULL, kJSPropertyAttributeReadOnly},
73 {"deviceClass", getDeviceClass, NULL, kJSPropertyAttributeReadOnly},
74 {"isBonded", getBondProperty, NULL, kJSPropertyAttributeReadOnly},
75 {"isTrusted", getTrustProperty, NULL, kJSPropertyAttributeReadOnly},
76 {"isConnected", getConnectProperty, NULL, kJSPropertyAttributeReadOnly},
77 {"uuids", getUuids, NULL, kJSPropertyAttributeReadOnly},
81 const JSClassRef JSBluetoothDevice::getClassRef()
84 m_jsClassRef = JSClassCreate(&m_classInfo);
89 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
94 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
96 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
98 LogDebug("initialize ");
100 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
104 IBluetoothDeviceManagerPtr BluetoothManager(BluetoothFactory::getInstance().getBluetoothDeviceManager());
105 priv = new JSBluetoothDevicePriv( context, BluetoothManager);
107 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
109 LogError("Object can't store private data.");
115 LogDebug("already exist");
121 void JSBluetoothDevice::finalize(JSObjectRef object)
123 LogDebug("Finalrize");
125 JSBluetoothDevicePriv* priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
129 JSObjectSetPrivate(object, NULL);
130 LogDebug("Deleting BluetoothManager");
136 bool JSBluetoothDevice::hasInstance(JSContextRef context, JSObjectRef constructor,
137 JSValueRef possibleInstance, JSValueRef* exception)
139 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
143 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceData device)
145 IBluetoothDeviceManagerPtr BluetoothManager(BluetoothFactory::getInstance().getBluetoothDeviceManager());
146 BluetoothManager->setDevice(device);
148 JSBluetoothDevicePriv* priv = new JSBluetoothDevicePriv( context, BluetoothManager);
149 return JSObjectMake(context, getClassRef(), priv);
154 JSValueRef JSBluetoothDevice::getName(JSContextRef context,
156 JSStringRef propertyName,
157 JSValueRef* exception)
161 Converter converter(context);
162 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
168 Throw(UnknownException);
171 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
172 std::string name = BluetoothDeviceManager->getNameProperty();
173 LogDebug("name" << name);
174 return converter.toJSValueRef(name);
176 Catch (WrtDeviceApis::Commons::ConversionException)
178 LogError("WrtDeviceApis::Commons::ConversionException");
179 return JSTizenExceptionFactory::postException(context,
180 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
182 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
184 LogError("InvalidArgumentException");
185 return JSTizenExceptionFactory::postException(context,
186 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
188 Catch (WrtDeviceApis::Commons::Exception)
190 LogError("hasInstance Exception");
191 return JSTizenExceptionFactory::postException(context, exception,
192 JSTizenException::UNKNOWN_ERROR, "Unkown error");
194 return JSValueMakeUndefined(context);
197 JSValueRef JSBluetoothDevice::getAddress(JSContextRef context,
199 JSStringRef propertyName,
200 JSValueRef* exception)
204 Converter converter(context);
205 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
211 Throw(UnknownException);
214 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
215 std::string address = BluetoothDeviceManager->getAddressProperty();
217 LogDebug("address" << address);
218 return converter.toJSValueRef(address);
220 Catch (WrtDeviceApis::Commons::ConversionException)
222 LogError("ConversationException");
223 return JSTizenExceptionFactory::postException(context, exception,
224 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
226 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
228 LogError("InvalidArgumentException");
229 return JSTizenExceptionFactory::postException(context, exception,
230 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
232 Catch(WrtDeviceApis::Commons::Exception)
234 LogError("hasInstance Exception");
235 return JSTizenExceptionFactory::postException(context, exception,
236 JSTizenException::UNKNOWN_ERROR, "Unkown error");
238 return JSValueMakeUndefined(context);
240 JSValueRef JSBluetoothDevice::getDeviceClass(JSContextRef context,
242 JSStringRef propertyName,
243 JSValueRef* exception)
247 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
253 Throw(UnknownException);
255 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
256 BluetoothDeviceDataClass devClass = BluetoothDeviceManager->getClass();
258 LogDebug("Major:" << std::hex << devClass.major << "Minor:" << std::hex << devClass.minor << "Service Mask:" << std::hex << devClass.majorServiceMask);
260 JSObjectRef adapterObject = JSBluetoothClass::createJSObject(priv->getContext(), devClass);
261 return adapterObject;
263 Catch (WrtDeviceApis::Commons::ConversionException)
265 LogError("ConversationException");
266 return JSTizenExceptionFactory::postException(context,
267 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
269 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
271 LogError("InvalidArgumentException");
272 return JSTizenExceptionFactory::postException(context,
273 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
275 Catch(WrtDeviceApis::Commons::Exception)
277 LogError("hasInstance Exception");
278 return JSTizenExceptionFactory::postException(context, exception,
279 JSTizenException::UNKNOWN_ERROR, "Unkown error");
281 return JSValueMakeUndefined(context);
284 JSValueRef JSBluetoothDevice::getBondProperty(JSContextRef context,
286 JSStringRef propertyName,
287 JSValueRef* exception)
291 Converter converter(context);
292 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
298 Throw(UnknownException);
301 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
302 bool isBonded = BluetoothDeviceManager->getBondProperty();
304 LogDebug("isBonded " << isBonded);
306 return converter.toJSValueRef(isBonded);
308 Catch (WrtDeviceApis::Commons::ConversionException)
310 LogError("ConversationException");
311 return JSTizenExceptionFactory::postException(context,
312 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");;
314 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
316 LogError("InvalidArgumentException");
317 return JSTizenExceptionFactory::postException(context,
318 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
320 Catch(WrtDeviceApis::Commons::Exception)
322 LogError("hasInstance Exception");
323 return JSTizenExceptionFactory::postException(context, exception,
324 JSTizenException::UNKNOWN_ERROR, "Unkown error");
326 return JSValueMakeUndefined(context);
328 JSValueRef JSBluetoothDevice::getTrustProperty(JSContextRef context,
330 JSStringRef propertyName,
331 JSValueRef* exception)
335 Converter converter(context);
336 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
342 Throw(UnknownException);
345 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
346 bool isTrusted = BluetoothDeviceManager->getTrustProperty();
348 LogDebug("isTrusted " << isTrusted);
349 return converter.toJSValueRef(isTrusted);
351 Catch (WrtDeviceApis::Commons::ConversionException)
353 LogError("ConversationException");
354 return JSTizenExceptionFactory::postException(context,
355 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");;
357 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
359 LogError("InvalidArgumentException");
360 return JSTizenExceptionFactory::postException(context,
361 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
363 Catch(WrtDeviceApis::Commons::Exception)
365 LogError("hasInstance Exception");
366 return JSTizenExceptionFactory::postException(context, exception,
367 JSTizenException::UNKNOWN_ERROR, "Unkown error");
369 return JSValueMakeUndefined(context);
371 JSValueRef JSBluetoothDevice::getConnectProperty(JSContextRef context,
373 JSStringRef propertyName,
374 JSValueRef* exception)
378 Converter converter(context);
379 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
385 Throw(UnknownException);
387 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
388 bool isConnected = BluetoothDeviceManager->getConnectProperty();
390 LogDebug("isConnected " << isConnected);
391 return converter.toJSValueRef(isConnected);
393 Catch (WrtDeviceApis::Commons::ConversionException)
395 LogError("ConversationException");
396 return JSTizenExceptionFactory::postException(context,
397 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");;
399 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
401 LogError("InvalidArgumentException");
402 return JSTizenExceptionFactory::postException(context,
403 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
405 Catch(WrtDeviceApis::Commons::Exception)
407 LogError("hasInstance Exception");
408 return JSTizenExceptionFactory::postException(context, exception,
409 JSTizenException::UNKNOWN_ERROR, "Unkown error");
411 return JSValueMakeUndefined(context);
415 JSValueRef JSBluetoothDevice::getUuids(JSContextRef context,
417 JSStringRef propertyName,
418 JSValueRef* exception)
422 Converter converter(context);
423 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
429 Throw(UnknownException);
432 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
433 std::vector<std::string> uuids = BluetoothDeviceManager->getUuidsProperty();
434 JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
435 int length = uuids.size();
439 return converter.toJSValueRef("");
442 for (int i = 0; i < length; ++i)
444 JSValueRef value = converter.toJSValueRef(uuids[i]);
446 if (!JSSetArrayElement(context, result, i, value))
448 return converter.toJSValueRef("");
454 Catch (WrtDeviceApis::Commons::ConversionException)
456 LogError("ConversationException");
457 return JSTizenExceptionFactory::postException(context,
458 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");;
460 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
462 LogError("InvalidArgumentException");
463 return JSTizenExceptionFactory::postException(context,
464 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
466 Catch(WrtDeviceApis::Commons::Exception)
468 LogError("hasInstance Exception");
469 return JSTizenExceptionFactory::postException(context, exception,
470 JSTizenException::UNKNOWN_ERROR, "Unkown error");
472 return JSValueMakeUndefined(context);
479 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context, JSObjectRef object,
480 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
481 JSValueRef* exception) {
483 LogDebug("connectToServiceByUUID");
485 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(thisObject));
486 JSValueRef successCallback = NULL;
487 JSValueRef errorCallBack = NULL;
495 LogError("priv null");
496 Throw(UnknownException);
499 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_DEVICE_CONNECT_TO_SERVICE]);
501 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
503 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_DEVICE_CONNECT_TO_SERVICE]);
505 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
507 if (argumentCount < 2 || argumentCount > 4)
509 LogError("InvalidArgumentException");
510 return JSTizenExceptionFactory::postException(context,
511 exception, JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
514 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsString(context, arguments[0]) == false)
516 LogError("hasInstance TypeMismatchException");
517 return JSTizenExceptionFactory::postException(context,
518 exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
521 BluetoothConverter converter(context);
522 successCallback = converter.toFunction(arguments[1]);
524 if (argumentCount >= 3)
526 errorCallBack = converter.toFunctionOrNull(arguments[2]);
529 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
533 return JSValueMakeUndefined(context);
535 cbm->setOnSuccess(successCallback);
536 cbm->setOnError(errorCallBack);
538 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
539 EventBTConnectToServiceByUUIDPtr event(new EventBTConnectToServiceByUUID);
540 BluetoothSocketData socketData;
541 socketData.uuid = converter.toString(arguments[0]);
542 socketData.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
544 if (argumentCount >= 4)
546 if (JSValueIsNull(context, arguments[3]) == true)
548 socketData.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
552 if (JSValueIsString(context, arguments[3]) == false)
555 LogError("InvalidArgumentException");
556 return JSTizenExceptionFactory::postException(context,
557 exception, JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
560 std::string protocol = converter.toString(arguments[3]);
561 LogDebug(protocol.c_str());
563 if (strcmp(protocol.c_str(), PROTOCOL_TYPE_RFCOMM_VALUE) == 0)
565 socketData.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
569 LogError("BluetoothDeviceManager or event or listener NULL");
570 Throw(WrtDeviceApis::Commons::UnsupportedException);
576 if (BluetoothDeviceManager == NULL || event == NULL)
578 LogError("BluetoothDeviceManager or event or listener NULL");
579 Throw(WrtDeviceApis::Commons::UnknownException);
582 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
584 event->setForAsynchronousCall(&BluetoothDeviceManagerListener::getInstance());
585 event->setSocketData(socketData);
586 BluetoothDeviceManager->connectToServiceByUUID(event);
588 return JSValueMakeNull(context);
591 Catch (WrtDeviceApis::Commons::UnsupportedException)
593 LogError("UnsupportedException");
594 return JSTizenExceptionFactory::postException(context, exception,
595 JSTizenException::NOT_SUPPORTED_ERROR, "not support error");
598 Catch (WrtDeviceApis::Commons::ConversionException)
600 LogError("ConversionException");
601 return JSTizenExceptionFactory::postException(context, exception,
602 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
605 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
607 LogError("InvalidArgumentException");
608 return JSTizenExceptionFactory::postException(context, exception,
609 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
612 Catch (WrtDeviceApis::Commons::Exception)
614 LogError("Exception");
615 return JSTizenExceptionFactory::postException(context, exception,
616 JSTizenException::UNKNOWN_ERROR, "unknown error");
620 return JSValueMakeUndefined(context);