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/JSTizenExceptionFactory.h>
23 #include <Tizen/Common/JSTizenException.h>
24 #include <Tizen/Common/SecurityExceptions.h>
25 #include <API/Bluetooth/BluetoothFactory.h>
26 #include <API/Bluetooth/BluetoothProperty.h>
27 #include <dpl/scoped_array.h>
28 #include <dpl/scoped_free.h>
29 #include "BluetoothSocketListener.h"
30 #include "JSBluetoothSocket.h"
31 #include "JSBluetoothDevice.h"
32 #include "BluetoothMultiCallback.h"
33 #include "BluetoothConverter.h"
34 #include <bluetooth.h>
35 #include "plugin_config.h"
39 using namespace TizenApis::Api;
40 using namespace TizenApis::Api::Bluetooth;
41 using namespace TizenApis::Commons;
48 JSClassDefinition JSBluetoothSocket::m_classInfo =
51 kJSClassAttributeNone,
69 JSStaticValue JSBluetoothSocket::m_properties[] =
71 {"uuid", getProperty, NULL, kJSPropertyAttributeReadOnly},
72 {"protocol", getProperty, NULL, kJSPropertyAttributeReadOnly},
73 {"state", getProperty, NULL, kJSPropertyAttributeReadOnly},
74 {"peer", getProperty, NULL, kJSPropertyAttributeReadOnly},
75 {"onError", NULL, setProperty, kJSPropertyAttributeNone},
76 {"onMessage", NULL, setProperty, kJSPropertyAttributeNone},
77 {"onClose", NULL, setProperty, kJSPropertyAttributeNone},
82 JSStaticFunction JSBluetoothSocket::m_function[] =
84 { "writeData", JSBluetoothSocket::writeData, kJSPropertyAttributeNone },
85 { "readData", JSBluetoothSocket::readData, kJSPropertyAttributeNone },
86 { "close", JSBluetoothSocket::close, kJSPropertyAttributeNone },
90 const JSClassRef JSBluetoothSocket::getClassRef()
94 m_jsClassRef = JSClassCreate(&m_classInfo);
99 const JSClassDefinition* JSBluetoothSocket::getClassInfo()
104 JSClassRef JSBluetoothSocket::m_jsClassRef = JSClassCreate(JSBluetoothSocket::getClassInfo());
106 bool JSBluetoothSocket::setProperty(JSContextRef context,
108 JSStringRef propertyName,
110 JSValueRef* exception)
116 JSBluetoothSocketPriv *priv = static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(object));
117 IBluetoothSocketManagerPtr BluetoothSocketManager(priv->getObject());
118 BluetoothConverter converter(priv->getContext());
122 ThrowMsg(ConversionException, "private object null");
125 EventBTSocketNotificationEmitterPtr emitter = BluetoothSocketManager->getSocketNotifier();
127 if (BluetoothSocketManager == NULL || emitter == NULL)
129 LogError("BluetoothManager or event or listener NULL");
130 Throw(WrtDeviceApis::Commons::UnknownException);
133 BluetoothSocketNotifierPrivateDataPtr privData =
134 DPL::DynamicPointerCast<BluetoothSocketNotifierPrivateData>(emitter->getEventPrivateData());
136 if (privData == NULL)
138 LogError("privData NULL");
139 Throw(WrtDeviceApis::Commons::UnknownException);
142 converter.setBluetoothSocketNotifierCallback(propertyName, value, object, privData);
143 emitter->setListener(&BluetoothSocketListener::getInstance());
144 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
148 catch (WrtDeviceApis::Commons::Exception& ex)
150 LogError("Exception: " << ex.GetMessage());
152 switch (ex.getCode())
154 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
155 JSTizenExceptionFactory::postException(context, exception,
156 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
158 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
159 JSTizenExceptionFactory::postException(context, exception,
160 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
162 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
164 JSTizenExceptionFactory::postException(context, exception,
165 JSTizenException::UNKNOWN_ERROR, "Unkown error");
172 void JSBluetoothSocket::initialize(JSContextRef context, JSObjectRef object)
174 LogDebug("JSBluetoothSocket::initialize ");
178 void JSBluetoothSocket::finalize(JSObjectRef object)
180 LogDebug("JSBluetoothSocket::Finalrize");
182 JSBluetoothSocketPriv *priv = static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(object));
187 JSObjectSetPrivate(object, NULL);
191 bool JSBluetoothSocket::hasInstance(JSContextRef context, JSObjectRef constructor,
192 JSValueRef possibleInstance, JSValueRef* exception)
194 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
197 JSObjectRef JSBluetoothSocket::createJSObject(JSContextRef context, BluetoothSocketData socketData)
199 IBluetoothSocketManagerPtr BluetoothSocketManager(BluetoothFactory::getInstance().getBluetoothSocketManager());
200 JSBluetoothSocketPriv* priv = new JSBluetoothSocketPriv( context, BluetoothSocketManager);
201 BluetoothSocketManager->setSocketData(socketData);
205 if (BluetoothSocketManager == NULL || priv == NULL)
207 LogError("BluetoothManager or event or listener NULL");
208 Throw(WrtDeviceApis::Commons::UnknownException);
211 BluetoothConverter converter(priv->getContext());
212 EventBTSocketNotificationEmitterPtr emitter(new EventBTSocketNotificationEmitter);
214 BluetoothSocketNotifierPrivateDataPtr privData(converter.getInitalBluetoothSocketNotifierPrivateData());
215 emitter->setListener(&BluetoothSocketListener::getInstance());
216 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
218 if (BluetoothSocketManager->setSocketNotifier(emitter) != BT_ERROR_NONE)
220 LogError("callback set error");
221 Throw(WrtDeviceApis::Commons::UnknownException);
224 return JSObjectMake(context, getClassRef(), priv);
226 catch (WrtDeviceApis::Commons::Exception& ex)
228 LogError("Exception: " << ex.GetMessage());
230 switch (ex.getCode())
232 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
233 return JSTizenExceptionFactory::makeErrorObject(context,
234 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
235 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
236 return JSTizenExceptionFactory::makeErrorObject(context,
237 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
238 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
240 return JSTizenExceptionFactory::makeErrorObject(context,
241 JSTizenException::UNKNOWN_ERROR, "Unkown error");
248 JSObjectRef JSBluetoothSocket::createJSObject(JSContextRef context, BluetoothSocketData socketData, EventBTReadDataType readData)
250 IBluetoothSocketManagerPtr BluetoothSocketManager(BluetoothFactory::getInstance().getBluetoothSocketManager());
251 JSBluetoothSocketPriv* priv = new JSBluetoothSocketPriv( context, BluetoothSocketManager);
252 BluetoothSocketManager->setSocketData(socketData);
253 BluetoothSocketManager->setReadData(readData);
254 return JSObjectMake(context, getClassRef(), priv);
256 JSValueRef JSBluetoothSocket::getProperty(JSContextRef context,
258 JSStringRef propertyName,
259 JSValueRef* exception)
266 JSBluetoothSocketPriv *priv = static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(object));
267 IBluetoothSocketManagerPtr BluetoothSocketManager(priv->getObject());
268 BluetoothConverter converter(priv->getContext());
272 LogError("priv null");
273 Throw(WrtDeviceApis::Commons::UnknownException);
276 if(JSStringIsEqualToUTF8CString(propertyName, "uuid"))
278 std::string uuid = BluetoothSocketManager->getUUID();
279 return converter.toJSValueRef(uuid);
281 else if(JSStringIsEqualToUTF8CString(propertyName, "protocol"))
283 unsigned short int protocol = BluetoothSocketManager->getProtocol();
284 return converter.toBluetoothProtocolJSValue(protocol);
287 else if(JSStringIsEqualToUTF8CString(propertyName, "state"))
289 unsigned short int state = BluetoothSocketManager->getState();
290 return converter.toBluetoothStateJSValue(state);
292 else if(JSStringIsEqualToUTF8CString(propertyName, "peer"))
294 BluetoothDeviceData device = BluetoothSocketManager->getPeer();
295 JSObjectRef adapterObject = JSBluetoothDevice::createJSObject(priv->getContext(), device);
296 return adapterObject;
300 Catch (WrtDeviceApis::Commons::SecurityException)
302 LogError("permission denied error");
303 return JSTizenExceptionFactory::postException(context, exception,
304 JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");
307 Catch (WrtDeviceApis::Commons::ConversionException)
309 LogError("ConversionException");
310 return JSTizenExceptionFactory::postException(context, exception,
311 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
313 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
315 LogError("InvalidArgumentException");
316 return JSTizenExceptionFactory::postException(context, exception,
317 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
319 Catch (WrtDeviceApis::Commons::UnknownException)
321 return JSTizenExceptionFactory::postException(context, exception,
322 JSTizenException::UNKNOWN_ERROR, "Unkown error");
324 Catch(WrtDeviceApis::Commons::Exception)
326 LogError("UnkownException");
327 return JSTizenExceptionFactory::postException(context, exception,
328 JSTizenException::UNKNOWN_ERROR, "Unkown error");
331 return JSValueMakeUndefined(context);
335 JSValueRef JSBluetoothSocket::writeData(JSContextRef context, JSObjectRef object,
336 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
337 JSValueRef* exception)
339 LogDebug("JSBluetoothSocket");
340 Converter converter(context);
343 JSBluetoothSocketPriv *priv = static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(thisObject));
347 LogError("priv null");
348 Throw(WrtDeviceApis::Commons::UnknownException);
351 if (argumentCount < 1 || JSValueIsNull(context, arguments[0]) == true)
353 LogError("argument wrong");
354 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
357 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
359 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_SOCKET_WRITE_DATA]);
361 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
363 IBluetoothSocketManagerPtr BluetoothSocketManager(priv->getObject());
364 std::vector<char> data = converter.toVectorOfChars(arguments[0]);
365 unsigned int index = 0;
367 if (data.size() == 0)
369 LogError("size 0 array passed");
370 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
373 DPL::ScopedArray<char> buffer(new char[data.size()]);
375 for (index = 0; index < data.size(); index++)
377 buffer[index] = data[index];
380 if (BluetoothSocketManager->writeData(buffer.Get(), data.size()) != BT_ERROR_NONE)
382 LogError("BluetoothSocket Write Error");
383 Throw(WrtDeviceApis::Commons::UnknownException);
386 return converter.toJSValueRef(data.size());
388 Catch (WrtDeviceApis::Commons::SecurityException)
390 LogError("permission denied error");
391 return JSTizenExceptionFactory::postException(context, exception,
392 JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");
395 Catch (WrtDeviceApis::Commons::ConversionException)
397 LogError("ConversionException");
398 return JSTizenExceptionFactory::postException(context, exception,
399 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
401 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
403 LogError("InvalidArgumentException");
404 return JSTizenExceptionFactory::postException(context, exception,
405 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
407 Catch (WrtDeviceApis::Commons::UnknownException)
409 return JSTizenExceptionFactory::postException(context, exception,
410 JSTizenException::UNKNOWN_ERROR, "Unkown error");
412 Catch(WrtDeviceApis::Commons::Exception)
414 LogError("UnkownException");
415 return JSTizenExceptionFactory::postException(context, exception,
416 JSTizenException::UNKNOWN_ERROR, "Unkown error");
419 return converter.toJSValueRef(0);
423 JSValueRef JSBluetoothSocket::readData(JSContextRef context, JSObjectRef object,
424 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
425 JSValueRef* exception)
429 LogDebug("JSBluetoothSocket");
433 JSBluetoothSocketPriv *priv = static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(thisObject));
434 IBluetoothSocketManagerPtr BluetoothSocketManager(priv->getObject());
436 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
438 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_SOCKET_READ_DATA]);
440 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
443 EventBTReadDataType data = BluetoothSocketManager->readData();
444 int length = data.size();
445 LogDebug("size" << length);
446 BluetoothConverter converter(priv->getContext());
450 LogError("BluetoothSocket Read Error");
451 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
455 JSObjectRef result = converter.toBluetoothByteArray(data);
458 Catch (WrtDeviceApis::Commons::SecurityException)
460 LogError("permission denied error");
461 return JSTizenExceptionFactory::postException(context, exception,
462 JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");
465 Catch (WrtDeviceApis::Commons::ConversionException)
467 LogError("ConversionException");
468 return JSTizenExceptionFactory::postException(context, exception,
469 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
471 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
473 LogError("InvalidArgumentException");
474 return JSTizenExceptionFactory::postException(context, exception,
475 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
477 Catch(WrtDeviceApis::Commons::Exception)
479 LogError("UnkownException");
480 return JSTizenExceptionFactory::postException(context, exception,
481 JSTizenException::UNKNOWN_ERROR, "Unkown error");
484 return JSValueMakeUndefined(context);
488 JSValueRef JSBluetoothSocket::close(JSContextRef context, JSObjectRef object,
489 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
490 JSValueRef* exception)
492 LogDebug("JSBluetoothSocket");
496 JSBluetoothSocketPriv *priv = static_cast<JSBluetoothSocketPriv*>(JSObjectGetPrivate(thisObject));
498 IBluetoothSocketManagerPtr BluetoothSocketManager(priv->getObject());
500 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_SOCKET_CLOSE]);
502 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
504 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_SOCKET_CLOSE]);
507 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
510 Catch (WrtDeviceApis::Commons::SecurityException)
512 LogError("permission denied error");
513 return JSTizenExceptionFactory::postException(context, exception,
514 JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");
517 Catch (WrtDeviceApis::Commons::ConversionException)
519 LogError("ConversionException");
520 return JSTizenExceptionFactory::postException(context, exception,
521 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
523 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
525 LogError("InvalidArgumentException");
526 return JSTizenExceptionFactory::postException(context, exception,
527 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
529 Catch(WrtDeviceApis::Commons::Exception)
531 LogError("UnkownException");
532 return JSTizenExceptionFactory::postException(context, exception,
533 JSTizenException::UNKNOWN_ERROR, "Unkown error");
537 return JSValueMakeUndefined(context);