Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / JSBluetoothAdapter.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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. 
15  */
16
17
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 <Commons/Exception.h>
27 #include "JSBluetoothAdapter.h"
28 #include <API/Bluetooth/IBluetoothAdapterManager.h>
29 #include <API/Bluetooth/BluetoothFactory.h>
30 #include <API/Bluetooth/BluetoothProperty.h>
31 #include "BluetoothAdapterListener.h"
32 #include "BluetoothMultiCallback.h"
33 #include "BluetoothConverter.h"
34
35 #include "plugin_config.h"
36
37 using namespace std;
38 using namespace DPL;
39 using namespace WrtDeviceApis;
40 using namespace WrtDeviceApis::CommonsJavaScript;
41 using namespace TizenApis::Commons;
42
43
44 namespace TizenApis {
45 namespace Tizen1_0 {
46
47 JSClassRef JSBluetoothAdapter::m_jsClassRef = NULL;
48
49 JSClassDefinition JSBluetoothAdapter::m_classInfo =
50 {
51         0,
52         kJSClassAttributeNone,
53         "BluetoothAdapter",
54         NULL,
55         m_properties,
56         m_function,
57         initialize,
58         finalize,
59         NULL,
60         NULL,
61         NULL,
62         NULL,
63         NULL,
64         NULL,
65         NULL,
66         hasInstance,
67         NULL
68 };
69
70 JSStaticFunction JSBluetoothAdapter::m_function[] =
71 {
72         { "setPowered", JSBluetoothAdapter::setPowered, kJSPropertyAttributeNone },
73         { "setVisible", JSBluetoothAdapter::setVisible, kJSPropertyAttributeNone },
74         { "setName", JSBluetoothAdapter::setName, kJSPropertyAttributeNone },   
75         { "discoverDevices", JSBluetoothAdapter::discoveryDevices, kJSPropertyAttributeNone },
76         { "stopDiscovery", JSBluetoothAdapter::stopDiscovery, kJSPropertyAttributeNone },
77         { "getKnownDevices", JSBluetoothAdapter::getKnownDevices, kJSPropertyAttributeNone },
78         { "getDevice", JSBluetoothAdapter::getDevice, kJSPropertyAttributeNone },
79         { "createBonding", JSBluetoothAdapter::createBonding, kJSPropertyAttributeNone },
80         { "destroyBonding", JSBluetoothAdapter::destroyBonding, kJSPropertyAttributeNone },
81         { "registerRFCOMMServiceByUUID", JSBluetoothAdapter::registerRFCOMMServiceByUUID, kJSPropertyAttributeNone },   
82         { 0, 0, 0 }
83 };
84
85 JSStaticValue JSBluetoothAdapter::m_properties[] = {
86         {"address", getAddress, NULL, kJSPropertyAttributeReadOnly},
87         {"name", getName, NULL, kJSPropertyAttributeReadOnly},
88         {"powered", getPowered, NULL, kJSPropertyAttributeReadOnly},            
89         {"visible", getVisible, NULL, kJSPropertyAttributeReadOnly},            
90
91         {0, 0, 0, 0}
92 };
93
94 JSValueRef JSBluetoothAdapter::getName(JSContextRef context,
95                                                                         JSObjectRef object,
96                                                                         JSStringRef propertyName,
97                                                                         JSValueRef* exception)
98 {
99         LogDebug("Enter");      
100
101         Converter converter(context);
102         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
103
104         Try 
105         {
106                 if (priv == NULL)
107                 {
108                         Throw(UnknownException);
109                 }
110
111                         
112                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
113                 std::string name = BluetoothAdapterManager->getName();
114                 return converter.toJSValueRef(name);
115         }
116         Catch (WrtDeviceApis::Commons::ConversionException)
117         {
118                 LogError("ConversionException");
119                 return JSTizenExceptionFactory::postException(context, exception,
120                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
121         }
122         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
123         {
124                 LogError("InvalidArgumentException");
125                 return JSTizenExceptionFactory::postException(context, exception,
126                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
127         }
128         Catch(WrtDeviceApis::Commons::Exception) 
129         {
130                 LogError("UnkownException");
131                 return JSTizenExceptionFactory::postException(context, exception,
132                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
133         }
134         return JSValueMakeUndefined(context);
135 }
136
137 JSValueRef JSBluetoothAdapter::setName(JSContextRef context, JSObjectRef object,
138                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
139                 JSValueRef* exception)
140 {
141         LogDebug("Enter");      
142         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
143         JSValueRef successCallback = NULL;
144         JSValueRef errorCallBack = NULL;
145         
146                 
147         Try 
148         {
149                 if (priv == NULL)
150                 {
151                         Throw(UnknownException);
152                 }
153
154                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
155                                 priv->getContext(),
156                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_NAME]);
157
158                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
159                 
160                 if (argumentCount < 2 || argumentCount > 3)
161                 {
162                         Throw(InvalidArgumentException);        
163                 }
164
165                 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsString(context, arguments[0]) == false)
166                 {
167                         Throw(ConversionException);     
168                 }
169
170                 BluetoothConverter converter(priv->getContext());
171                 successCallback = converter.toFunction(arguments[1]);
172
173                 if (argumentCount >= 3)
174                 {
175                         errorCallBack = converter.toFunctionOrNull(arguments[2]);
176                 }
177
178                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
179                 
180                 if (cbm == NULL)
181                 {
182                         Throw(UnknownException);
183                 }
184                 
185                 cbm->setOnSuccess(successCallback);
186                 cbm->setOnError(errorCallBack);
187                 
188                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
189                 std::string name = converter.toString(arguments[0]);
190
191                 if (name != BluetoothAdapterManager->getName())
192                 {
193                         EventBTSetNamePtr event(new EventBTSetName);
194                         
195                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
196                         BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
197                         event->setForAsynchronousCall(&listener);
198                         event->setName(name);
199                         BluetoothAdapterManager->setAdapterName(event);
200                 }
201                 else
202                 {
203                         LogDebug("same name is already set");
204                         cbm->callOnSuccess();
205                 }
206                 return JSValueMakeNull(context);
207         }       
208         Catch (WrtDeviceApis::Commons::ConversionException)
209         {
210                 LogError("ConversionException");
211                 return JSTizenExceptionFactory::postException(context, exception, 
212                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
213
214         }
215         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
216         {
217                 LogError("InvalidArgumentException");
218                 return JSTizenExceptionFactory::postException(context, exception,
219                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
220
221         }
222         Catch (WrtDeviceApis::Commons::Exception) 
223         {
224                 LogError("UnkownException");
225                 return JSTizenExceptionFactory::postException(context, exception, 
226                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
227         }
228         return JSValueMakeUndefined(context);
229 }
230
231
232 JSValueRef JSBluetoothAdapter::getAddress(JSContextRef context,
233                                                                 JSObjectRef object,
234                                                                 JSStringRef propertyName,
235                                                                 JSValueRef* exception)
236 {
237         LogDebug("Enter");      
238
239         Converter converter(context);
240         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
241
242         Try 
243         {
244                 if (priv == NULL)
245                 {
246                         Throw(UnknownException);
247                 }
248                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
249                 std::string address = BluetoothAdapterManager->getAddress();
250                 return converter.toJSValueRef(address);
251         }
252         Catch (WrtDeviceApis::Commons::ConversionException)
253         {
254                 LogError("ConversionException");
255                 return JSTizenExceptionFactory::postException(context, exception,
256                                 JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
257
258         }
259         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
260         {
261                 LogError("InvalidArgumentException");
262                 return JSTizenExceptionFactory::postException(context, exception,
263                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
264
265         }
266         Catch (WrtDeviceApis::Commons::Exception) 
267         {
268                 LogError("UnkownException");
269                 return JSTizenExceptionFactory::postException(context, exception,
270                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
271
272         }
273         return JSValueMakeUndefined(context);
274
275 }
276
277
278 JSValueRef JSBluetoothAdapter::getVisible(JSContextRef context,
279                                                                         JSObjectRef object,
280                                                                         JSStringRef propertyName,
281                                                                         JSValueRef* exception)
282 {
283         LogDebug("Enter");      
284
285         Converter converter(context);
286         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
287         Try 
288         {
289                 if (priv == NULL)
290                 {
291                         Throw(UnknownException);
292                 }
293
294                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
295                 bool visibility = BluetoothAdapterManager->getVisibility();
296                 return converter.toJSValueRef(visibility);
297         }
298         Catch (WrtDeviceApis::Commons::ConversionException)
299         {
300                 LogError("ConversionException");
301                 return JSTizenExceptionFactory::postException(context, exception, 
302                         JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
303
304         }       
305         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
306         {
307                 LogError("InvalidArgumentException");
308                 return JSTizenExceptionFactory::postException(context, exception, 
309                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
310         }
311         Catch (WrtDeviceApis::Commons::Exception) 
312         {
313                 LogError("UnkownException");
314                 return JSTizenExceptionFactory::postException(context, exception, 
315                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
316         }
317         return JSValueMakeUndefined(context);
318
319 }
320
321
322
323 const JSClassRef JSBluetoothAdapter::getClassRef() 
324 {
325         if (!m_jsClassRef) 
326         {
327                 m_jsClassRef = JSClassCreate(&m_classInfo);
328         }
329         return m_jsClassRef;
330 }
331
332 const JSClassDefinition* JSBluetoothAdapter::getClassInfo() 
333 {
334         return &m_classInfo;
335 }
336
337
338 void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object) 
339 {
340         JSBluetoothAdapterPriv* priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
341
342         if (priv != NULL)
343         {
344                 LogDebug("Already Exist");
345         }
346         else
347         {
348
349                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(BluetoothFactory::getInstance().getBluetoothAdapterManager());
350                 priv = new JSBluetoothAdapterPriv( context, BluetoothAdapterManager);
351                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
352                 {
353                         LogError("Object can't store private data.");
354                         delete priv;
355                 }
356                 LogDebug("JSBluetoothAdapter::initialize ");
357         }
358 }
359
360 void JSBluetoothAdapter::finalize(JSObjectRef object) 
361 {
362         LogDebug("JSBluetoothAdapter::Finalrize");
363
364         JSBluetoothAdapterPriv* priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
365
366         if (priv != NULL)
367         {
368                 JSObjectSetPrivate(object, NULL);
369                 LogDebug("Deleting BluetoothAdapterManager");
370                 delete priv;
371         }
372
373
374 }
375
376 bool JSBluetoothAdapter::hasInstance(JSContextRef context, JSObjectRef constructor,
377                 JSValueRef possibleInstance, JSValueRef* exception) 
378 {
379         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
380 }
381
382
383 JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context)
384 {
385         IBluetoothAdapterManagerPtr BluetoothAdapterManager(BluetoothFactory::getInstance().getBluetoothAdapterManager());
386         JSBluetoothAdapterPriv* priv = new JSBluetoothAdapterPriv( context, BluetoothAdapterManager);
387
388         return JSObjectMake(context, getClassRef(), priv);
389
390 }
391
392 JSValueRef JSBluetoothAdapter::getPowered(JSContextRef context,
393                                                                         JSObjectRef object,
394                                                                         JSStringRef propertyName,
395                                                                         JSValueRef* exception)
396
397 {
398         LogDebug("Enter");      
399
400         Converter converter(context);
401         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(object));
402
403         Try 
404         {
405                 if (priv == NULL)
406                 {
407                         Throw(UnknownException);
408                 }
409
410                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
411                 bool powered = BluetoothAdapterManager->getPowered();
412                 return converter.toJSValueRef(powered);
413         }       
414         Catch (WrtDeviceApis::Commons::ConversionException)
415         {
416                 LogError("ConversionException");
417                 return JSTizenExceptionFactory::postException(context, exception, 
418                         JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
419
420         }       
421         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
422         {
423                 LogError("InvalidArgumentException");
424                 return JSTizenExceptionFactory::postException(context, exception, 
425                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
426
427         }
428         Catch (WrtDeviceApis::Commons::Exception) 
429         {
430                 LogError("UnkownException");
431                 return JSTizenExceptionFactory::postException(context, exception, 
432                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
433
434         }
435         return JSValueMakeUndefined(context);
436 }
437
438
439 JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context, JSObjectRef object,
440                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
441                 JSValueRef* exception) 
442 {
443
444         LogDebug("setPowered");
445
446         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
447         JSValueRef successCallback = NULL;
448         JSValueRef errorCallBack = NULL;
449
450         Try     
451         {
452                 if (priv == NULL)
453                 {
454                         LogError("priv null");
455                         Throw(UnknownException);        
456                 }
457                 
458                 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_POWERED]);
459
460                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
461                                 priv->getContext(),
462                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_POWERED]);
463                 
464                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
465
466                 if (argumentCount < 2 || argumentCount > 3)
467                 {
468                         Throw(InvalidArgumentException);        
469                 }
470                 
471                 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
472                 {
473                         Throw(ConversionException);     
474                 }
475                 
476                 BluetoothConverter converter(priv->getContext());
477                 successCallback = converter.toFunction(arguments[1]);
478
479                 if (argumentCount >= 3)
480                 {
481                         errorCallBack = converter.toFunctionOrNull(arguments[2]);
482                 }
483
484                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
485
486                 if (cbm == NULL)
487                 {
488                         Throw(UnknownException);        
489                 }
490                 cbm->setOnSuccess(successCallback);
491                 cbm->setOnError(errorCallBack);
492         
493                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
494                 EventBTSetPoweredPtr event(new EventBTSetPowered);
495
496                 if (BluetoothAdapterManager == NULL || event == NULL)
497                 {
498                         LogError("BluetoothAdapterManager or event or listener NULL");
499                         Throw(UnknownException);        
500                 }
501
502                 if (converter.toBool(arguments[0]) == true)
503                 {
504                         event->setEnable();
505                 }
506                 else
507                 {
508                         event->setDisable();
509                 }
510                 
511                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
512                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
513                 event->setForAsynchronousCall(&listener);
514                 BluetoothAdapterManager->setPowered(event);
515                 return JSValueMakeNull(context);
516         }
517         Catch (WrtDeviceApis::Commons::ConversionException)
518         {
519                 LogError("ConversionException");
520                 return JSTizenExceptionFactory::postException(context, exception, 
521                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
522         }               
523         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
524         {
525                 LogError("InvalidArgumentException");
526                 return JSTizenExceptionFactory::postException(context, exception, 
527                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
528
529         }
530         Catch (WrtDeviceApis::Commons::Exception) 
531         {
532                 LogError("Exception");
533                 return JSTizenExceptionFactory::postException(context, exception, 
534                         JSTizenException::UNKNOWN_ERROR, "unknown error");
535         }
536         return JSValueMakeUndefined(context);
537 }
538
539 JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context, JSObjectRef object,
540                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
541                 JSValueRef* exception) 
542 {
543         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
544         JSValueRef successCallback = NULL;
545         JSValueRef errorCallBack = NULL;
546
547         Try     
548         {
549                 if (priv == NULL)
550                 {
551                         LogError("priv null");
552                         Throw(UnknownException);        
553                 }
554
555                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
556                                 priv->getContext(),
557                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_SET_VISIBLE]);
558                 
559                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
560
561                 
562                 if (argumentCount < 2 || argumentCount > 4)
563                 {
564                         Throw(InvalidArgumentException);        
565                 }
566                 BluetoothConverter converter(priv->getContext());
567                 successCallback = converter.toFunction(arguments[1]);
568
569                 if (argumentCount >= 3)
570                 {
571                         errorCallBack = converter.toFunctionOrNull(arguments[2]);
572                 }
573                 
574                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
575
576                 if (cbm == NULL)
577                 {
578                         Throw(UnknownException);        
579                 }
580
581                 cbm->setOnSuccess(successCallback);
582                 cbm->setOnError(errorCallBack);
583
584                 if (JSValueIsNull(context, arguments[0]) == true || JSValueIsBoolean(context, arguments[0]) == false)
585                 {
586                         LogError("Wrong parameter passed, type mismatch");
587                         Throw(ConversionException);     
588                 }
589
590                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
591                 EventBTSetVisiblePtr event(new EventBTSetVisible);
592
593                 if (BluetoothAdapterManager == NULL || event == NULL)
594                 {
595                         LogError("BluetoothAdapterManager or event or listener NULL");
596                         Throw(UnknownException);        
597                 }
598
599                 if (converter.toBool(arguments[0]) == true)
600                 {
601                         event->setVisible();
602                 }
603                 else
604                 {
605                         event->setInvisible();
606                 }
607
608                 if (argumentCount == 4)
609                 {
610                         if (JSValueIsNull(context, arguments[3]) == true)
611                         {
612                                 event->setTimeout(0);
613                         }
614                         else
615                         {
616                                 if (JSValueIsNumber(context, arguments[3]) == false) 
617                                 {
618                                         LogError("Wrong parameter passed");
619                                         Throw(ConversionException);
620                                 }
621                         }
622                         LogDebug("timeout " << (unsigned short)converter.toInt(arguments[3]));
623                         event->setTimeout((unsigned short)converter.toInt(arguments[3]));
624                 }
625                 else
626                 {
627                         event->setTimeout(0);
628                 }
629         
630                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
631                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
632                 event->setForAsynchronousCall(&listener);
633                 BluetoothAdapterManager->setVisible(event);
634                 
635                 return JSValueMakeNull(context);
636         }
637         Catch (WrtDeviceApis::Commons::ConversionException)
638         {
639                 LogError("ConversionException");
640                 return JSTizenExceptionFactory::postException(context, exception, 
641                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
642
643         }               
644         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
645         {
646                 LogError("InvalidArgumentException");
647                 return JSTizenExceptionFactory::postException(context, exception, 
648                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
649
650         }
651         Catch (WrtDeviceApis::Commons::Exception) 
652         {
653                 LogError("Exception");
654                 return JSTizenExceptionFactory::postException(context, exception, 
655                         JSTizenException::UNKNOWN_ERROR, "unknown error");
656
657         }
658         return JSValueMakeUndefined(context);
659 }
660
661 JSValueRef JSBluetoothAdapter::discoveryDevices(JSContextRef context, JSObjectRef object,
662                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
663                 JSValueRef* exception) 
664 {
665         LogDebug("discoveryDevices");
666         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
667
668         Try     
669         {
670                 if (priv == NULL)
671                 {
672                         LogError("priv null");
673                         Throw(UnknownException);        
674                 }
675                 
676                 if (argumentCount < 1 || argumentCount > 2)
677                 {
678                         LogError("InvalidArgumentException");
679                         Throw(InvalidArgumentException);        
680                 }
681
682                 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_DISCOVER_DEVICES]);
683
684                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
685                                 priv->getContext(),
686                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_DISCOVER_DEVICES]);
687                 
688
689                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
690                 
691                 JSValueRef errorArgument = JSValueMakeNull(context);
692
693                 if (argumentCount >= 2)
694                 {
695                         errorArgument = arguments[1];
696                 }
697
698                 BluetoothConverter converter(priv->getContext());
699                 EventBTOnDiscoveryDevicesPrivateDataPtr privData(
700                         converter.toEventBTOnDiscoveryDevicesPrivateData(thisObject, arguments[0], errorArgument));
701                 
702                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
703                 EventBTOnDiscoveryDevicesEmitterPtr emitter(new EventBTOnDiscoveryDevicesEmitter);
704
705                 if (BluetoothAdapterManager == NULL || emitter == NULL || privData == NULL)
706                 {
707                         LogError("BluetoothAdapterManager or event or listener NULL");
708                         Throw(UnknownException);        
709                 }
710                 emitter->setListener(&BluetoothAdapterListener::getInstance());
711                 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privData));
712                 BluetoothAdapterManager->discoveryDevicesEmitter(emitter);
713
714                 return JSValueMakeNull(context);
715
716         }
717         Catch (WrtDeviceApis::Commons::ConversionException)
718         {
719                 LogError("ConversionException");
720                 return JSTizenExceptionFactory::postException(context, exception, 
721                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
722
723         }                       
724         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
725         {
726                 LogError("InvalidArgumentException");
727                 return JSTizenExceptionFactory::postException(context, exception, 
728                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
729
730         }
731         Catch (WrtDeviceApis::Commons::Exception) 
732         {
733                 LogError("Exception");
734                 return JSTizenExceptionFactory::postException(context, exception, 
735                         JSTizenException::UNKNOWN_ERROR, "unknown error");
736         }
737         return JSValueMakeUndefined(context);
738
739 }
740
741 JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context, JSObjectRef object,
742                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
743                 JSValueRef* exception) 
744 {
745         LogDebug("discoveryDevices");
746         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
747         JSValueRef successCallback = NULL;
748         JSValueRef errorCallBack = NULL;
749         
750
751         Try     
752         {
753                 if (priv == NULL)
754                 {
755                         LogError("priv null");
756                         Throw(UnknownException);        
757                 }
758
759                 LogDebug(bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_STOP_DISCOVERY]);
760
761                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
762                                 priv->getContext(),
763                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_STOP_DISCOVERY]);
764
765                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
766                 
767                 if (argumentCount < 1 || argumentCount > 2)
768                 {
769                         LogError("InvalidArgumentException");
770                         Throw(InvalidArgumentException);        
771                 }
772
773                 BluetoothConverter converter(priv->getContext());
774                 successCallback = converter.toFunction(arguments[0]);
775                 
776                 if (argumentCount >= 2)
777                 {
778                         errorCallBack = converter.toFunctionOrNull(arguments[1]);
779                 }
780
781
782                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
783                 
784                 if (cbm == NULL)
785                 {
786                         Throw(UnknownException);        
787                 }
788                         
789                 cbm->setOnSuccess(successCallback);
790                 cbm->setOnError(errorCallBack);
791
792                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
793                 EventBTStopDiscoveryPtr  event(new EventBTStopDiscovery);
794                 
795                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
796                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
797                 event->setForAsynchronousCall(&listener);
798                 BluetoothAdapterManager->stopDiscovery(event);
799
800                 return JSValueMakeNull(context);
801         }
802         Catch (WrtDeviceApis::Commons::ConversionException)
803         {
804                 LogError("ConversionException");
805                 return JSTizenExceptionFactory::postException(context, exception, 
806                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
807
808         }               
809         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
810         {
811                 LogError("InvalidArgumentException");
812                 return JSTizenExceptionFactory::postException(context, exception, 
813                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
814
815         }
816         Catch (WrtDeviceApis::Commons::Exception) 
817         {
818                 LogError("Exception");
819                 return JSTizenExceptionFactory::postException(context, exception, 
820                         JSTizenException::UNKNOWN_ERROR, "unknown error");
821
822         }
823         
824         return JSValueMakeUndefined(context);
825
826 }
827
828
829 JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context, JSObjectRef object,
830                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
831                 JSValueRef* exception) 
832 {
833         LogDebug("getKnownDevices");
834         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
835         JSValueRef successCallback = NULL;
836         JSValueRef errorCallBack = NULL;
837
838         Try     
839         {
840                 if (priv == NULL)
841                 {
842                         LogError("priv null");
843                         Throw(UnknownException);        
844                 }
845
846                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
847                                 priv->getContext(),
848                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_GET_KNOWN_DEVICES]);
849                 
850
851                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
852                 
853                 if (argumentCount < 1 || argumentCount > 2)
854                 {
855                         LogError("InvalidArgumentException");
856                         Throw(InvalidArgumentException);
857                 }
858
859                 BluetoothConverter converter(priv->getContext());
860                 successCallback = converter.toFunction(arguments[0]);
861                 
862                 if (argumentCount >= 2)
863                 {
864                         errorCallBack = converter.toFunctionOrNull(arguments[1]);
865                 }
866
867
868                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
869                 
870                 if (cbm == NULL)
871                 {
872                         Throw(UnknownException);        
873                 }
874                         
875                 cbm->setOnSuccess(successCallback);
876                 cbm->setOnError(errorCallBack);
877
878                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
879                 EventBTGetKnownDevicesPtr event(new EventBTGetKnownDevices);
880
881
882                 if (BluetoothAdapterManager == NULL || event == NULL)
883                 {
884                         LogError("BluetoothAdapterManager or event or listener NULL");
885                         Throw(UnknownException);        
886                 }
887
888                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
889                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
890                 event->setForAsynchronousCall(&listener);
891                 BluetoothAdapterManager->getKownDevices(event);
892                 
893                 return JSValueMakeNull(context);
894         }
895         Catch (WrtDeviceApis::Commons::ConversionException)
896         {
897                 LogError("ConversionException");
898                 return JSTizenExceptionFactory::postException(context, exception, 
899                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
900
901         }               
902         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
903         {
904                 LogError("InvalidArgumentException");
905                 return JSTizenExceptionFactory::postException(context, exception, 
906                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
907
908         }
909         Catch (WrtDeviceApis::Commons::Exception) 
910         {
911                 LogError("Exception");
912                 return JSTizenExceptionFactory::postException(context, exception, 
913                         JSTizenException::UNKNOWN_ERROR, "unknown error");
914
915         }
916         return JSValueMakeUndefined(context);
917 }
918
919 JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context, JSObjectRef object,
920                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
921                 JSValueRef* exception) 
922 {
923         LogDebug("Enter");
924         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
925         JSValueRef successCallback = NULL;
926         JSValueRef errorCallBack = NULL;
927
928
929         Try     
930         {
931                 if (priv == NULL)
932                 {
933                         LogError("priv null");
934                         Throw(UnknownException);        
935                 }
936
937                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
938                                 priv->getContext(),
939                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADAPTER_GET_DEVICE]);
940
941                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
942
943                 
944                 if (argumentCount < 2 || argumentCount > 3)
945                 {
946                         LogError("Wrong parameter count passed");
947                         Throw(InvalidArgumentException);
948                 }
949
950                 BluetoothConverter converter(priv->getContext());
951                 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
952                 successCallback = converter.toFunction(arguments[1]);
953                 
954                 if (argumentCount >= 3)
955                 {
956                         errorCallBack = converter.toFunctionOrNull(arguments[2]);
957                 }
958
959                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
960                 
961                 if (cbm == NULL)
962                 {
963                         Throw(UnknownException);        
964                 }
965                         
966                 cbm->setOnSuccess(successCallback);
967                 cbm->setOnError(errorCallBack);
968
969                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
970                 EventBTGetDevicePtr event(new EventBTGetDevice);
971
972                 if (BluetoothAdapterManager == NULL || event == NULL)
973                 {
974                         LogError("BluetoothAdapterManager or event or listener NULL");
975                         Throw(UnknownException);        
976                 }
977                 
978                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
979                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
980                 event->setForAsynchronousCall(&listener);
981                 event->setAddress(address);
982                 BluetoothAdapterManager->getDevice(event);
983
984                 return JSValueMakeNull(context);
985         }
986         Catch (WrtDeviceApis::Commons::ConversionException)
987         {
988                 LogError("ConversionException");
989                 return JSTizenExceptionFactory::postException(context, exception, 
990                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
991
992         }               
993         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
994         {
995                 LogError("InvalidArgumentException");
996                 return JSTizenExceptionFactory::postException(context, exception, 
997                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
998
999         }
1000         Catch (WrtDeviceApis::Commons::Exception) 
1001         {
1002                 LogError("Exception");
1003                 return JSTizenExceptionFactory::postException(context, exception, 
1004                         JSTizenException::UNKNOWN_ERROR, "unknown error");
1005
1006         }
1007
1008         return JSValueMakeUndefined(context);}
1009
1010
1011 JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context, JSObjectRef object,
1012                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1013                 JSValueRef* exception) 
1014 {
1015         LogDebug("createBonding");
1016
1017         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1018         JSValueRef successCallback = NULL;
1019         JSValueRef errorCallBack = NULL;
1020
1021         Try     
1022         {
1023
1024                 if (priv == NULL)
1025                 {
1026                         LogError("priv null");
1027                         Throw(UnknownException);        
1028                 }
1029
1030                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1031                                 priv->getContext(),
1032                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_CREATE_BONDING]);
1033
1034
1035                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1036                 
1037                 if (argumentCount < 2 || argumentCount > 3)
1038                 {
1039                         LogError("Wrong parameter count passed");
1040                         Throw(InvalidArgumentException);
1041
1042                 }
1043
1044
1045                 BluetoothConverter converter(priv->getContext());
1046                 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1047                 successCallback = converter.toFunction(arguments[1]);
1048                 
1049                 if (argumentCount >= 3)
1050                 {
1051                         errorCallBack = converter.toFunctionOrNull(arguments[2]);
1052                 }
1053
1054                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1055                 
1056                 if (cbm == NULL)
1057                 {
1058                         Throw(UnknownException);        
1059                 }
1060                         
1061                 cbm->setOnSuccess(successCallback);
1062                 cbm->setOnError(errorCallBack);
1063
1064
1065                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1066                 EventBTCreateDestroyBondingPtr event(new EventBTCreateDestroyBonding);
1067
1068                 if (BluetoothAdapterManager == NULL || event == NULL)
1069                 {
1070                         LogError("BluetoothAdapterManager or event or listener NULL");
1071                         Throw(UnknownException);        
1072                 }
1073
1074                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1075                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
1076                 event->setForAsynchronousCall(&listener);
1077                 event->setAddress(address);
1078                 event->setCreateBonding();
1079                 BluetoothAdapterManager->createBonding(event);
1080                 return JSValueMakeNull(context);
1081         }
1082         Catch (WrtDeviceApis::Commons::ConversionException)
1083         {
1084                 LogError("ConversionException");
1085                 return JSTizenExceptionFactory::postException(context, exception, 
1086                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1087
1088         }               
1089         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
1090         {
1091                 LogError("InvalidArgumentException");
1092                 return JSTizenExceptionFactory::postException(context, exception, 
1093                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1094
1095         }
1096         Catch (WrtDeviceApis::Commons::Exception) 
1097         {
1098                 LogError("Exception");
1099                 return JSTizenExceptionFactory::postException(context, exception, 
1100                         JSTizenException::UNKNOWN_ERROR, "unknown error");
1101
1102         }       
1103         return JSValueMakeUndefined(context);
1104
1105 }
1106
1107 JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context, JSObjectRef object,
1108                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1109                 JSValueRef* exception) 
1110 {
1111
1112         LogDebug("destroyBonding");
1113
1114         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));
1115         JSValueRef successCallback = NULL;
1116         JSValueRef errorCallBack = NULL;
1117
1118         Try     
1119         {
1120                 if (priv == NULL)
1121                 {
1122                         LogError("priv null");
1123                         Throw(UnknownException);        
1124                 }
1125
1126                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1127                                 priv->getContext(),
1128                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_DESTROY_BONDING]);
1129
1130                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1131                 
1132                 if (argumentCount < 2 || argumentCount > 3)
1133                 {
1134                         LogError("Wrong parameter count passed");
1135                         return JSTizenExceptionFactory::postException(context, exception, 
1136                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1137                 }
1138
1139                 BluetoothConverter converter(priv->getContext());
1140                 std::string address = converter.toBluetoothDeviceAddress(arguments[0]);
1141                 successCallback = converter.toFunction(arguments[1]);
1142                 
1143                 if (argumentCount >= 3)
1144                 {
1145                         errorCallBack = converter.toFunctionOrNull(arguments[2]);
1146                 }
1147
1148                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1149                 
1150                 if (cbm == NULL)
1151                 {
1152                         Throw(UnknownException);        
1153                 }
1154                         
1155                 cbm->setOnSuccess(successCallback);
1156                 cbm->setOnError(errorCallBack);
1157                 
1158                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1159                 EventBTCreateDestroyBondingPtr event(new EventBTCreateDestroyBonding);
1160                 
1161                 if (BluetoothAdapterManager == NULL || event == NULL)
1162                 {
1163                         LogError("BluetoothAdapterManager or event or listener NULL");
1164                         Throw(UnknownException);        
1165                 }
1166
1167                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();           
1168                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1169                 event->setForAsynchronousCall(&listener);
1170                 event->setDestroyBonding();
1171                 event->setAddress(address);
1172                 BluetoothAdapterManager->createBonding(event);
1173                 
1174                 return JSValueMakeNull(context);
1175         }
1176         Catch (WrtDeviceApis::Commons::ConversionException)
1177         {
1178                 LogError("ConversionException");
1179                 return JSTizenExceptionFactory::postException(context, exception, 
1180                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1181
1182         }               
1183         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
1184         {
1185                 LogError("InvalidArgumentException");
1186                 return JSTizenExceptionFactory::postException(context, exception, 
1187                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1188
1189         }
1190         Catch (WrtDeviceApis::Commons::Exception) 
1191         {
1192                 LogError("Exception");
1193                 return JSTizenExceptionFactory::postException(context, exception, 
1194                         JSTizenException::UNKNOWN_ERROR, "unknown error");
1195
1196         }       
1197         
1198         return JSValueMakeUndefined(context);
1199 }
1200
1201
1202 JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context, JSObjectRef object,
1203                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
1204                 JSValueRef* exception) 
1205 {
1206         LogDebug("registerRFCOMMServiceByUUID");
1207
1208         JSBluetoothAdapterPriv *priv = static_cast<JSBluetoothAdapterPriv*>(JSObjectGetPrivate(thisObject));    
1209         JSValueRef successCallback = NULL;
1210         JSValueRef errorCallBack = NULL;
1211
1212         Try     
1213         {
1214                 std::string serviceName, uuid;
1215                 unsigned short securityLevel= SECURITY_LEVEL_HIGH_VALUE_INT;
1216                 Validator validator(context);           
1217                 
1218                 LogDebug("context address" << context << "priv-context" << priv->getContext());
1219                                 
1220                 if (priv == NULL)
1221                 {
1222                         LogError("priv null");
1223                         Throw(UnknownException);        
1224                 }
1225
1226                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
1227                                 priv->getContext(),
1228                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_REGISTER_RFCOMM]);
1229
1230                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
1231
1232                 
1233                 if (argumentCount < 3 || argumentCount > 5)
1234                 {
1235                         LogError("Wrong parameter count passed");       
1236                         Throw(InvalidArgumentException);
1237                 }
1238
1239                 BluetoothConverter converter(priv->getContext());
1240                 uuid = converter.toBluetoothUUIDString(arguments[0]);
1241
1242                 if (!JSValueIsString(context, arguments[1]) || JSValueIsNull(context, arguments[1]))
1243                 {
1244                         ThrowMsg(ConversionException, "Not a string");
1245                 }
1246                 serviceName = converter.toString(arguments[1]);
1247                 successCallback = converter.toFunction(arguments[2]);
1248                 
1249                 if (argumentCount >= 4)
1250                 {
1251                         errorCallBack = converter.toFunctionOrNull(arguments[3]);
1252                 }
1253                 
1254                 if (argumentCount >= 5)
1255                 {
1256                         securityLevel = converter.toBluetoothSecurityLevelInt(arguments[4]);
1257                 }
1258         
1259                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
1260                 
1261                 if (cbm == NULL)
1262                 {
1263                         Throw(UnknownException);        
1264                 }
1265
1266                 cbm->setOnSuccess(successCallback);
1267                 cbm->setOnError(errorCallBack);
1268                 
1269                 IBluetoothAdapterManagerPtr BluetoothAdapterManager(priv->getObject());
1270                 EventBTRegisterRFCOMMPtr event(new EventBTRegisterRFCOMM);
1271
1272                 if (BluetoothAdapterManager == NULL || event == NULL)
1273                 {
1274                         LogError("BluetoothAdapterManager or event or listener NULL");
1275                         Throw(UnknownException);        
1276                 }
1277                 LogDebug("UUid:" << uuid << " serviceName:" << serviceName);
1278
1279                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();           
1280                 event->setForAsynchronousCall(&listener);
1281                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
1282                 event->setUuid(uuid);
1283                 event->setSecurityLevel(securityLevel); 
1284                 event->setName(serviceName);
1285                 BluetoothAdapterManager->registerRFCOMMServiceByUUID(event);
1286
1287                 return JSValueMakeNull(context);
1288         }
1289         Catch (WrtDeviceApis::Commons::ConversionException)
1290         {
1291                 LogError("ConversionException");
1292                 return JSTizenExceptionFactory::postException(context, exception, 
1293                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
1294
1295         }               
1296         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
1297         {
1298                 LogError("InvalidArgumentException");
1299                 return JSTizenExceptionFactory::postException(context, exception, 
1300                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
1301
1302         }
1303         Catch (WrtDeviceApis::Commons::UnsupportedException)
1304         {
1305                 LogError("NotsupportedException");
1306                 return JSTizenExceptionFactory::postException(context, exception, 
1307                         JSTizenException::NOT_SUPPORTED_ERROR, "not support error");
1308
1309         }
1310         Catch (WrtDeviceApis::Commons::Exception) 
1311         {
1312                 LogError("Exception");
1313                 return JSTizenExceptionFactory::postException(context, exception, 
1314                         JSTizenException::UNKNOWN_ERROR, "unknown error");
1315
1316         }       
1317         return JSValueMakeUndefined(context);
1318
1319 }
1320
1321
1322 }
1323 }