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 "JSBluetoothServiceHandler.h"
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 <API/Bluetooth/BluetoothProperty.h>
27 #include "BluetoothConverter.h"
28 #include "BluetoothAdapterListener.h"
29 #include <API/Bluetooth/BluetoothFactory.h>
30 #include <bluetooth.h>
31 #include "plugin_config.h"
35 using namespace WrtDeviceApis;
36 using namespace TizenApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
43 JSClassRef JSBluetoothServiceHandler::m_jsClassRef = NULL;
45 JSClassDefinition JSBluetoothServiceHandler::m_classInfo =
48 kJSClassAttributeNone,
49 "BluetoothServiceHandler",
69 JSStaticValue JSBluetoothServiceHandler::m_properties[] =
71 {"uuid", getUuid, NULL, kJSPropertyAttributeReadOnly},
72 {"onconnect", NULL, setOnConnect, kJSPropertyAttributeNone},
73 {"name", getName, NULL, kJSPropertyAttributeReadOnly},
74 {"isConnected", getIsConnected, NULL, kJSPropertyAttributeReadOnly},
78 JSStaticFunction JSBluetoothServiceHandler::m_function[] =
80 {"unregister", JSBluetoothServiceHandler::unregisterRFCOMMService, kJSPropertyAttributeNone},
84 void JSBluetoothServiceHandler::initialize(JSContextRef context, JSObjectRef object)
86 LogDebug("JSBluetoothServiceHandler::initialize");
89 JSObjectRef JSBluetoothServiceHandler::createJSObject(JSContextRef context, std::string name, std::string uuid, int registerSock)
91 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(BluetoothFactory::getInstance().getBluetoothServiceHandlerManager());
92 BluetoothServiceHandlerManager->setRegisterSocket(registerSock);
93 BluetoothServiceHandlerManager->setUUID(uuid);
94 BluetoothServiceHandlerManager->setIsConnected(false);
95 BluetoothServiceHandlerManager->setName(name);
97 JSBluetoothServiceHandlerPriv* priv = new JSBluetoothServiceHandlerPriv( context, BluetoothServiceHandlerManager);
99 return JSObjectMake(context, getClassRef(), priv);
105 const JSClassRef JSBluetoothServiceHandler::getClassRef()
109 m_jsClassRef = JSClassCreate(&m_classInfo);
115 void JSBluetoothServiceHandler::getPropertyNames(JSContextRef context,
117 JSPropertyNameAccumulatorRef propertyNames)
122 bool JSBluetoothServiceHandler::setOnConnect(JSContextRef context,
124 JSStringRef propertyName,
126 JSValueRef* exception)
133 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
134 BluetoothConverter converter(priv->getContext());
135 JSValueRef successCallback = NULL;
136 JSValueRef errorCallback = NULL;
140 ThrowMsg(UnknownException, "private object null");
143 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
147 ThrowMsg(ConversionException, "callback manager creation fail");
150 successCallback = converter.toFunction(value);
152 cbm->setOnSuccess(successCallback);
153 cbm->setOnError(errorCallback);
154 //cbm->setObject(object);
156 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
157 EventBTServiceOnConnectEmitterPtr emitter(new EventBTServiceOnConnectEmitter);
159 emitter->setListener(&BluetoothAdapterListener::getInstance());
160 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(cbm));
161 BluetoothServiceHandlerManager->setServiceHandlerListener(emitter);
165 catch (const WrtDeviceApis::Commons::Exception& ex)
167 LogError("Exception: " << ex.GetMessage());
169 switch (ex.getCode())
171 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
172 JSTizenExceptionFactory::postException(context, exception,
173 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
175 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
176 JSTizenExceptionFactory::postException(context, exception,
177 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
179 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
181 JSTizenExceptionFactory::postException(context, exception,
182 JSTizenException::UNKNOWN_ERROR, "Unkown error");
192 JSValueRef JSBluetoothServiceHandler::getUuid(JSContextRef context,
194 JSStringRef propertyName,
195 JSValueRef* exception)
201 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
202 BluetoothConverter converter(priv->getContext());
206 ThrowMsg(UnknownException, "private object null");
209 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
210 std::string uuid = BluetoothServiceHandlerManager->getUUID();
211 return converter.toJSValueRef(uuid);
215 catch (const WrtDeviceApis::Commons::Exception& ex)
217 LogError("Exception: " << ex.GetMessage());
219 switch (ex.getCode())
221 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
222 return JSTizenExceptionFactory::postException(context, exception,
223 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
224 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
225 return JSTizenExceptionFactory::postException(context, exception,
226 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
227 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
229 return JSTizenExceptionFactory::postException(context, exception,
230 JSTizenException::UNKNOWN_ERROR, "Unkown error");
234 return JSValueMakeUndefined(context);
238 JSValueRef JSBluetoothServiceHandler::getName(JSContextRef context,
240 JSStringRef propertyName,
241 JSValueRef* exception)
247 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
248 BluetoothConverter converter(priv->getContext());
252 ThrowMsg(UnknownException, "private object null");
255 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
256 std::string name = BluetoothServiceHandlerManager->getName();
257 return converter.toJSValueRef(name);
261 catch (const WrtDeviceApis::Commons::Exception& ex)
263 LogError("Exception: " << ex.GetMessage());
265 switch (ex.getCode())
267 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
268 return JSTizenExceptionFactory::postException(context, exception,
269 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
270 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
271 return JSTizenExceptionFactory::postException(context, exception,
272 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
273 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
275 return JSTizenExceptionFactory::postException(context, exception,
276 JSTizenException::UNKNOWN_ERROR, "Unkown error");
280 return JSValueMakeUndefined(context);
283 JSValueRef JSBluetoothServiceHandler::getIsConnected(JSContextRef context,
285 JSStringRef propertyName,
286 JSValueRef* exception)
292 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
293 BluetoothConverter converter(priv->getContext());
297 ThrowMsg(UnknownException, "private object null");
300 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
301 bool connected = BluetoothServiceHandlerManager->getIsConnected();
302 return converter.toJSValueRef(connected);
306 catch (const WrtDeviceApis::Commons::Exception& ex)
308 LogError("Exception: " << ex.GetMessage());
310 switch (ex.getCode())
312 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
313 return JSTizenExceptionFactory::postException(context, exception,
314 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
315 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
316 return JSTizenExceptionFactory::postException(context, exception,
317 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
318 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
320 return JSTizenExceptionFactory::postException(context, exception,
321 JSTizenException::UNKNOWN_ERROR, "Unkown error");
325 return JSValueMakeUndefined(context);
328 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo()
337 void JSBluetoothServiceHandler::finalize(JSObjectRef object)
339 JSBluetoothServiceHandlerPriv* priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
340 LogDebug("JSBluetoothServiceHandler::Finalrize");
344 JSObjectSetPrivate(object, NULL);
345 LogDebug("Deleting BluetoothManager");
350 bool JSBluetoothServiceHandler::hasInstance(JSContextRef context, JSObjectRef constructor,
351 JSValueRef possibleInstance, JSValueRef* exception)
353 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
356 JSValueRef JSBluetoothServiceHandler::unregisterRFCOMMService(JSContextRef context, JSObjectRef object,
357 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
358 JSValueRef* exception)
361 LogDebug("unregisterRFCOMMService");
363 JSValueRef successCallback = NULL;
364 JSValueRef errorCallBack = NULL;
365 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(thisObject));
366 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
368 JSValueRef reserveArguments[2];
371 for (index = 0; index < 2; index++)
373 if (index < argumentCount)
374 reserveArguments[index] = arguments[index];
376 reserveArguments[index] = JSValueMakeUndefined(context);
382 LogError("priv null");
383 Throw(ConversionException);
386 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
387 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_UNREGISTER]);
389 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
392 BluetoothConverter converter(priv->getContext());
394 if (argumentCount >= 1 && JSValueIsUndefined(context, reserveArguments[0]) == false)
396 successCallback = converter.toFunctionOrNull(reserveArguments[0]);
399 if (argumentCount >= 2 && JSValueIsUndefined(context, reserveArguments[1]) == false)
401 errorCallBack = converter.toFunctionOrNull(reserveArguments[1]);
407 Throw(UnknownException);
410 cbm->setOnSuccess(successCallback);
411 cbm->setOnError(errorCallBack);
413 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
414 EventBTUnregisterRFCOMMServicePtr event(new EventBTUnregisterRFCOMMService);
416 if (BluetoothServiceHandlerManager == NULL || event == NULL)
418 LogError("BluetoothAdapterManager or event or listener NULL");
419 Throw(UnknownException);
422 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
423 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
424 event->setForAsynchronousCall(&listener);
425 BluetoothServiceHandlerManager->unRegister(event);
428 Catch (WrtDeviceApis::Commons::ConversionException)
430 LogError("ConversionException");
431 return JSTizenExceptionFactory::postException(context, exception,
432 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
435 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
437 LogError("InvalidArgumentException");
438 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(priv->getContext(),
439 JSTizenException::INVALID_VALUES_ERROR, "invalid values error"));
441 Catch (WrtDeviceApis::Commons::Exception)
443 LogError("Exception");
444 return JSTizenExceptionFactory::postException(context, exception,
445 JSTizenException::UNKNOWN_ERROR, "unknown error");
447 return JSValueMakeUndefined(context);