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