2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <SecurityExceptions.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <JSTizenException.h>
27 #include "JSBluetoothDevice.h"
28 #include "IBluetoothDeviceManager.h"
29 #include "BluetoothFactory.h"
30 #include "BluetoothDeviceManagerListener.h"
31 #include "BluetoothProperty.h"
32 #include "JSBluetoothClass.h"
33 #include "BluetoothConverter.h"
34 #include "plugin_config.h"
39 using namespace WrtDeviceApis;
40 using namespace DeviceAPI::Common;
45 JSClassDefinition JSBluetoothDevice::m_classInfo =
48 kJSClassAttributeNone,
66 JSStaticFunction JSBluetoothDevice::m_function[] =
68 { "connectToServiceByUUID", JSBluetoothDevice::connectToServiceByUUID, kJSPropertyAttributeNone },
72 JSStaticValue JSBluetoothDevice::m_properties[] = {
73 {"name", getName, NULL, kJSPropertyAttributeReadOnly},
74 {"address", getAddress, NULL, kJSPropertyAttributeReadOnly},
75 {"isBonded", getBondProperty, NULL, kJSPropertyAttributeReadOnly},
76 {"isTrusted", getTrustProperty, NULL, kJSPropertyAttributeReadOnly},
77 {"isConnected", getConnectProperty, NULL, kJSPropertyAttributeReadOnly},
78 {"uuids", getUuids, NULL, kJSPropertyAttributeReadOnly},
82 const JSClassRef JSBluetoothDevice::getClassRef()
85 m_jsClassRef = JSClassCreate(&m_classInfo);
90 const JSClassDefinition* JSBluetoothDevice::getClassInfo()
95 JSClassRef JSBluetoothDevice::m_jsClassRef = JSClassCreate(JSBluetoothDevice::getClassInfo());
97 void JSBluetoothDevice::initialize(JSContextRef context, JSObjectRef object)
99 LogDebug("initialize ");
101 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
105 IBluetoothDeviceManagerPtr BluetoothManager(BluetoothFactory::getInstance().getBluetoothDeviceManager());
106 priv = new JSBluetoothDevicePriv( context, BluetoothManager);
108 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
110 LogError("Object can't store private data.");
116 LogDebug("already exist");
119 setAttributesIntoJSObject(context, object);
122 void JSBluetoothDevice::finalize(JSObjectRef object)
124 LogDebug("Finalrize");
126 JSBluetoothDevicePriv* priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
130 JSObjectSetPrivate(object, NULL);
131 LogDebug("Deleting BluetoothManager");
137 bool JSBluetoothDevice::hasInstance(JSContextRef context, JSObjectRef constructor,
138 JSValueRef possibleInstance, JSValueRef* exception)
140 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
144 JSObjectRef JSBluetoothDevice::createJSObject(JSContextRef context, BluetoothDeviceData device)
146 IBluetoothDeviceManagerPtr BluetoothManager(BluetoothFactory::getInstance().getBluetoothDeviceManager());
147 BluetoothManager->setDevice(device);
149 JSBluetoothDevicePriv* priv = new JSBluetoothDevicePriv( context, BluetoothManager);
150 return JSObjectMake(context, getClassRef(), priv);
155 JSValueRef JSBluetoothDevice::getName(JSContextRef context,
157 JSStringRef propertyName,
158 JSValueRef* exception)
162 Converter converter(context);
163 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
169 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
172 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
173 std::string name = BluetoothDeviceManager->getNameProperty();
174 LogDebug("name" << name);
175 return converter.toJSValueRef(name);
177 Catch (WrtDeviceApis::Commons::Exception)
179 LogWarning("Exception"<<_rethrown_exception.GetMessage());
185 JSValueRef JSBluetoothDevice::getAddress(JSContextRef context,
187 JSStringRef propertyName,
188 JSValueRef* exception)
192 Converter converter(context);
193 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
199 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
202 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
203 std::string address = BluetoothDeviceManager->getAddressProperty();
205 LogDebug("address" << address);
206 return converter.toJSValueRef(address);
208 Catch(WrtDeviceApis::Commons::Exception)
210 LogWarning("Exception"<<_rethrown_exception.GetMessage());
215 JSValueRef JSBluetoothDevice::getBondProperty(JSContextRef context,
217 JSStringRef propertyName,
218 JSValueRef* exception)
222 Converter converter(context);
223 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
229 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
232 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
233 bool isBonded = BluetoothDeviceManager->getBondProperty();
235 LogDebug("isBonded " << isBonded);
237 return converter.toJSValueRef(isBonded);
239 Catch(WrtDeviceApis::Commons::Exception)
241 LogWarning("Exception"<<_rethrown_exception.GetMessage());
244 return JSValueMakeUndefined(context);
246 JSValueRef JSBluetoothDevice::getTrustProperty(JSContextRef context,
248 JSStringRef propertyName,
249 JSValueRef* exception)
253 Converter converter(context);
254 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
260 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
263 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
264 bool isTrusted = BluetoothDeviceManager->getTrustProperty();
266 LogDebug("isTrusted " << isTrusted);
267 return converter.toJSValueRef(isTrusted);
269 Catch(WrtDeviceApis::Commons::Exception)
271 LogWarning("Exception"<<_rethrown_exception.GetMessage());
274 return JSValueMakeUndefined(context);
276 JSValueRef JSBluetoothDevice::getConnectProperty(JSContextRef context,
278 JSStringRef propertyName,
279 JSValueRef* exception)
283 Converter converter(context);
284 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
290 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
292 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
293 bool isConnected = BluetoothDeviceManager->getConnectProperty();
295 LogDebug("isConnected " << isConnected);
296 return converter.toJSValueRef(isConnected);
298 Catch(WrtDeviceApis::Commons::Exception)
300 LogWarning("Exception"<<_rethrown_exception.GetMessage());
303 return JSValueMakeUndefined(context);
306 JSValueRef JSBluetoothDevice::getUuids(JSContextRef context,
308 JSStringRef propertyName,
309 JSValueRef* exception)
313 Converter converter(context);
314 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
320 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
323 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
324 std::vector<std::string> uuids = BluetoothDeviceManager->getUuidsProperty();
325 JSObjectRef result = JSCreateArrayObject(context, 0, NULL);
326 int length = uuids.size();
330 return converter.toJSValueRef("");
333 for (int i = 0; i < length; ++i)
335 JSValueRef value = converter.toJSValueRef(uuids[i]);
337 if (!JSSetArrayElement(context, result, i, value))
339 return converter.toJSValueRef("");
345 Catch(WrtDeviceApis::Commons::Exception)
347 LogWarning("Exception"<<_rethrown_exception.GetMessage());
350 return JSValueMakeUndefined(context);
356 JSValueRef JSBluetoothDevice::connectToServiceByUUID(JSContextRef context, JSObjectRef object,
357 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
358 JSValueRef* exception) {
360 LogDebug("connectToServiceByUUID");
362 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(thisObject));
363 JSValueRef successCallback = NULL;
364 JSValueRef errorCallBack = NULL;
366 JSValueRef reserveArguments[3];
370 return JSTizenExceptionFactory::postException(context, exception,
371 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
374 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
378 for (index = 0; index < 3; index++)
380 if (index < argumentCount)
381 reserveArguments[index] = arguments[index];
383 reserveArguments[index] = JSValueMakeNull(context);
387 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_DEVICE_CONNECT_TO_SERVICE]);
389 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
390 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_DEVICE_CONNECT_TO_SERVICE]);
392 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
395 BluetoothConverter converter(context);
396 successCallback = converter.toFunction(reserveArguments[1]);
397 errorCallBack = converter.toFunctionOrNull(reserveArguments[2]);
402 return JSValueMakeUndefined(context);
404 cbm->setOnSuccess(successCallback);
405 cbm->setOnError(errorCallBack);
407 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
408 EventBTConnectToServiceByUUIDPtr event(new EventBTConnectToServiceByUUID);
409 BluetoothSocketData socketData;
410 socketData.uuid = converter.toBluetoothUUIDString(reserveArguments[0]);
411 socketData.protocol = PROTOCOL_TYPE_RFCOMM_VALUE_INT;
413 /* if (argumentCount >= 4)
415 socketData.protocol = converter.toBluetoothProtocolInt(reserveArguments[3]);
419 if (BluetoothDeviceManager == NULL || event == NULL)
421 LogError("BluetoothDeviceManager or event or listener NULL");
422 Throw(WrtDeviceApis::Commons::UnknownException);
425 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
427 event->setForAsynchronousCall(&BluetoothDeviceManagerListener::getInstance());
428 event->setSocketData(socketData);
429 BluetoothDeviceManager->connectToServiceByUUID(event);
431 Catch (WrtDeviceApis::Commons::ConversionException)
433 LogError("ConversionException");
434 return JSTizenExceptionFactory::postException(context, exception,
435 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
438 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
440 LogError("InvalidArgumentException");
441 return JSTizenExceptionFactory::postException(context, exception,
442 JSTizenException::TYPE_MISMATCH_ERROR, "invalid value error");
444 Catch(WrtDeviceApis::Commons::UnsupportedException)
446 LogError("UnsupportException");
447 return JSTizenExceptionFactory::postException(context, exception,
448 JSTizenException::NOT_SUPPORTED_ERROR, "Unsupport Exception");
451 Catch (WrtDeviceApis::Commons::AlreadyInUseException)
453 LogError("AlreadyInUseException");
454 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(priv->getContext(),
455 JSTizenException::SERVICE_NOT_AVAILABLE, "already connected"));
457 Catch (WrtDeviceApis::Commons::Exception)
459 LogError("Exception");
460 return JSTizenExceptionFactory::postException(context, exception,
461 JSTizenException::UNKNOWN_ERROR, "unknown error");
464 return JSValueMakeUndefined(context);
467 void JSBluetoothDevice::setAttributesIntoJSObject(JSContextRef context, JSObjectRef object)
472 JSBluetoothDevicePriv *priv = static_cast<JSBluetoothDevicePriv*>(JSObjectGetPrivate(object));
475 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is NULL.");
477 IBluetoothDeviceManagerPtr BluetoothDeviceManager(priv->getObject());
478 BluetoothDeviceDataClass devClass = BluetoothDeviceManager->getClass();
480 LogDebug("Major:" << std::hex << devClass.major << "Minor:" << std::hex << devClass.minor << "Service Mask:" << std::hex << devClass.majorServiceMask);
482 JSUtil::setProperty(context, object, "deviceClass",
483 JSBluetoothClass::createJSObject(priv->getContext(), devClass), kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete);
485 Catch(WrtDeviceApis::Commons::Exception)
487 LogWarning("Exception"<<_rethrown_exception.GetMessage());