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 if (BluetoothServiceHandlerManager->setServiceHandlerListener(emitter) != BT_ERROR_NONE)
163 ThrowMsg(UnknownException, "private object null");
168 catch (const WrtDeviceApis::Commons::Exception& ex)
170 LogError("Exception: " << ex.GetMessage());
172 switch (ex.getCode())
174 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
175 JSTizenExceptionFactory::postException(context, exception,
176 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
178 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
179 JSTizenExceptionFactory::postException(context, exception,
180 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
182 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
184 JSTizenExceptionFactory::postException(context, exception,
185 JSTizenException::UNKNOWN_ERROR, "Unkown error");
195 JSValueRef JSBluetoothServiceHandler::getUuid(JSContextRef context,
197 JSStringRef propertyName,
198 JSValueRef* exception)
204 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
205 BluetoothConverter converter(priv->getContext());
209 ThrowMsg(UnknownException, "private object null");
212 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
213 std::string uuid = BluetoothServiceHandlerManager->getUUID();
214 return converter.toJSValueRef(uuid);
218 catch (const WrtDeviceApis::Commons::Exception& ex)
220 LogError("Exception: " << ex.GetMessage());
222 switch (ex.getCode())
224 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
225 return JSTizenExceptionFactory::postException(context, exception,
226 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
227 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
228 return JSTizenExceptionFactory::postException(context, exception,
229 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
230 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
232 return JSTizenExceptionFactory::postException(context, exception,
233 JSTizenException::UNKNOWN_ERROR, "Unkown error");
237 return JSValueMakeUndefined(context);
241 JSValueRef JSBluetoothServiceHandler::getName(JSContextRef context,
243 JSStringRef propertyName,
244 JSValueRef* exception)
250 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
251 BluetoothConverter converter(priv->getContext());
255 ThrowMsg(UnknownException, "private object null");
258 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
259 std::string name = BluetoothServiceHandlerManager->getName();
260 return converter.toJSValueRef(name);
264 catch (const WrtDeviceApis::Commons::Exception& ex)
266 LogError("Exception: " << ex.GetMessage());
268 switch (ex.getCode())
270 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
271 return JSTizenExceptionFactory::postException(context, exception,
272 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
273 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
274 return JSTizenExceptionFactory::postException(context, exception,
275 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
276 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
278 return JSTizenExceptionFactory::postException(context, exception,
279 JSTizenException::UNKNOWN_ERROR, "Unkown error");
283 return JSValueMakeUndefined(context);
286 JSValueRef JSBluetoothServiceHandler::getIsConnected(JSContextRef context,
288 JSStringRef propertyName,
289 JSValueRef* exception)
295 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
296 BluetoothConverter converter(priv->getContext());
300 ThrowMsg(UnknownException, "private object null");
303 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
304 bool connected = BluetoothServiceHandlerManager->getIsConnected();
305 return converter.toJSValueRef(connected);
309 catch (const WrtDeviceApis::Commons::Exception& ex)
311 LogError("Exception: " << ex.GetMessage());
313 switch (ex.getCode())
315 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
316 return JSTizenExceptionFactory::postException(context, exception,
317 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
318 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
319 return JSTizenExceptionFactory::postException(context, exception,
320 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
321 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
323 return JSTizenExceptionFactory::postException(context, exception,
324 JSTizenException::UNKNOWN_ERROR, "Unkown error");
328 return JSValueMakeUndefined(context);
331 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo()
340 void JSBluetoothServiceHandler::finalize(JSObjectRef object)
342 JSBluetoothServiceHandlerPriv* priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
343 LogDebug("JSBluetoothServiceHandler::Finalrize");
347 JSObjectSetPrivate(object, NULL);
348 LogDebug("Deleting BluetoothManager");
353 bool JSBluetoothServiceHandler::hasInstance(JSContextRef context, JSObjectRef constructor,
354 JSValueRef possibleInstance, JSValueRef* exception)
356 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
359 JSValueRef JSBluetoothServiceHandler::unregisterRFCOMMService(JSContextRef context, JSObjectRef object,
360 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
361 JSValueRef* exception)
364 LogDebug("unregisterRFCOMMService");
366 JSValueRef successCallback = NULL;
367 JSValueRef errorCallBack = NULL;
371 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(thisObject));
375 LogError("priv null");
376 Throw(UnknownException);
379 if (argumentCount < 1 || argumentCount > 2)
381 LogError("Wrong parameter count passed");
382 Throw(InvalidArgumentException);
385 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
387 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_UNREGISTER_RFCOMM]);
389 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
392 BluetoothConverter converter(priv->getContext());
394 successCallback = converter.toFunction(arguments[0]);
396 if (argumentCount >= 2)
398 errorCallBack = converter.toFunctionOrNull(arguments[1]);
401 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
405 Throw(UnknownException);
408 cbm->setOnSuccess(successCallback);
409 cbm->setOnError(errorCallBack);
410 // cbm->setObject(thisObject);
412 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
413 EventBTUnregisterRFCOMMServicePtr event(new EventBTUnregisterRFCOMMService);
415 if (BluetoothServiceHandlerManager == NULL || event == NULL)
417 LogError("BluetoothAdapterManager or event or listener NULL");
418 Throw(UnknownException);
421 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
422 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
423 event->setForAsynchronousCall(&listener);
424 BluetoothServiceHandlerManager->unRegister(event);
426 return JSValueMakeNull(context);
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 return JSTizenExceptionFactory::postException(context, exception,
439 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
442 Catch (WrtDeviceApis::Commons::Exception)
444 LogError("Exception");
445 return JSTizenExceptionFactory::postException(context, exception,
446 JSTizenException::UNKNOWN_ERROR, "unknown error");
449 return JSValueMakeUndefined(context);