wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Bluetooth / JSBluetoothAdapter.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <SecurityExceptions.h>
19
20 #include <JSUtil.h>
21 #include <JSWebAPIException.h>
22 #include <ArgumentValidator.h>
23 #include <GlobalContextManager.h>
24
25 #include "plugin_config.h"
26 #include "JSBluetoothAdapter.h"
27 #include "BluetoothAdapter.h"
28
29 #include <TimeTracer.h>
30 #include <Logger.h>
31
32 using namespace WrtDeviceApis::Commons;
33 using namespace DeviceAPI::Common;
34
35 namespace DeviceAPI {
36 namespace Bluetooth {
37
38 JSClassDefinition JSBluetoothAdapter::m_classInfo = {
39     0,
40     kJSClassAttributeNone,
41     "BluetoothAdapter",
42     NULL, //ParentClass
43     m_property, //StaticValues
44     m_function, //StaticFunctions
45     initialize, //Initialize
46     finalize, //Finalize
47     NULL, //HasProperty,
48     NULL, //GetProperty,
49     NULL, //SetProperty,
50     NULL, //DeleteProperty,
51     NULL, //GetPropertyNames,
52     NULL, //CallAsFunction,
53     NULL, //CallAsConstructor,
54     NULL, //HasInstance,
55     NULL //ConvertToType
56 };
57
58 JSStaticValue JSBluetoothAdapter::m_property[] = {
59     { BLUETOOTH_ADAPTER_NAME, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
60     { BLUETOOTH_ADAPTER_POWERED, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
61     { BLUETOOTH_ADAPTER_VISIBLE, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
62     { BLUETOOTH_ADAPTER_ADDRESS, getProperty, NULL, kJSPropertyAttributeNone|kJSPropertyAttributeReadOnly|kJSPropertyAttributeDontDelete },
63     { 0, 0, 0, 0 }
64 };
65
66 JSStaticFunction JSBluetoothAdapter::m_function[] = {
67     { BLUETOOTH_ADAPTER_API_SET_NAME, setName, kJSPropertyAttributeNone },
68     { BLUETOOTH_ADAPTER_API_SET_POWERED, setPowered, kJSPropertyAttributeNone },
69     { BLUETOOTH_ADAPTER_API_SET_VISIBLE, setVisible, kJSPropertyAttributeNone },
70     { BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES, discoverDevices, kJSPropertyAttributeNone },
71     { BLUETOOTH_ADAPTER_API_STOP_DISCOVERY, stopDiscovery, kJSPropertyAttributeNone },
72     { BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES, getKnownDevices, kJSPropertyAttributeNone },
73     { BLUETOOTH_ADAPTER_API_GET_DEVICE, getDevice, kJSPropertyAttributeNone },
74     { BLUETOOTH_ADAPTER_API_CREATE_BONDING, createBonding, kJSPropertyAttributeNone },
75     { BLUETOOTH_ADAPTER_API_DESTROY_BONDING, destroyBonding, kJSPropertyAttributeNone },
76     { BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID, registerRFCOMMServiceByUUID, kJSPropertyAttributeNone },
77     { 0, 0, 0 }
78 };
79
80 JSClassRef JSBluetoothAdapter::m_jsClassRef = JSClassCreate(JSBluetoothAdapter::getClassInfo());
81
82 const JSClassRef JSBluetoothAdapter::getClassRef()
83 {
84     if (!m_jsClassRef) {
85         m_jsClassRef = JSClassCreate(&m_classInfo);
86     }
87     return m_jsClassRef;
88 }
89
90 const JSClassDefinition* JSBluetoothAdapter::getClassInfo()
91 {
92     return &m_classInfo;
93 }
94
95 JSObjectRef JSBluetoothAdapter::createJSObject(JSContextRef context)
96 {
97     return JSObjectMake(context, getClassRef(), NULL);
98 }
99
100 void JSBluetoothAdapter::initialize(JSContextRef context, JSObjectRef object)
101 {
102     // do nothing
103     LoggerD("Enter");
104 }
105
106
107 void JSBluetoothAdapter::finalize(JSObjectRef object)
108 {
109     // do nothing
110     LoggerD("Enter");
111 }
112
113 JSValueRef JSBluetoothAdapter::getProperty(JSContextRef context,
114         JSObjectRef object,
115         JSStringRef propertyName,
116         JSValueRef* exception)
117 {
118     LoggerD("Enter");
119     try {
120         if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_NAME)) {
121             std::string name = BluetoothAdapter::getInstance()->getName();
122             LoggerD("name: " << name);
123             return JSUtil::toJSValueRef(context, name);
124         }
125         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_POWERED)) {
126             return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getPowered());
127         }
128         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_ADAPTER_VISIBLE)) {
129             return JSUtil::toJSValueRef(context, BluetoothAdapter::getInstance()->getVisible());
130         }
131         else if (JSStringIsEqualToUTF8CString(propertyName, BLUETOOTH_DEVICE_ADDRESS)) {
132             std::string address = BluetoothAdapter::getInstance()->getAddress();
133             LoggerD("address: " << address);
134             return JSUtil::toJSValueRef(context, address);
135         }
136     } catch (const BasePlatformException &err) {
137         LoggerW("Getting property is failed" << err.getMessage().c_str());
138     }
139
140     return NULL;
141 }
142
143 JSValueRef JSBluetoothAdapter::setName(JSContextRef context,
144         JSObjectRef object,
145         JSObjectRef thisObject,
146         size_t argumentCount,
147         const JSValueRef arguments[],
148         JSValueRef* exception)
149 {
150     LoggerD("Entered");
151     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
152     
153     // Access Check
154     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_NAME);
155     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
156
157     try {
158         // Check whether Bluetooth is supported or not
159         if(!BluetoothAdapter::isBluetoothSupported()) {
160             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
161         }
162     
163         ArgumentValidator validator(context, argumentCount, arguments);        
164         std::string name = validator.toString(0);  // name
165         JSObjectRef successCallback = validator.toFunction(1, true);  // successCallback  
166         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
167
168         // perform
169         MultiCallbackUserDataPtr callback(
170                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
171         if(!callback){
172             LoggerW("Can't create MultiCallbackUserData");
173         }
174         else {
175             callback->setCallback("success", successCallback);
176             callback->setCallback("error", errorCallback);
177         }      
178         
179         BluetoothAdapter::getInstance()->setName(name, callback);        
180         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
181         
182         return JSValueMakeUndefined(context);
183     } catch (const BasePlatformException &err) {
184         return JSWebAPIException::throwException(context, exception, err);
185     } catch (...) {
186         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setName().");
187         return JSWebAPIException::throwException(context, exception, err);
188     }
189 }
190
191 JSValueRef JSBluetoothAdapter::setPowered(JSContextRef context,
192         JSObjectRef object,
193         JSObjectRef thisObject,
194         size_t argumentCount,
195         const JSValueRef arguments[],
196         JSValueRef* exception)
197 {
198     LoggerD("Entered");
199     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
200     
201     // Access Check
202     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_POWERED);
203     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
204
205     try {
206         // Check whether Bluetooth is supported or not
207         if(!BluetoothAdapter::isBluetoothSupported()) {
208             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
209         }
210     
211         // Validate arguments
212         ArgumentValidator validator(context, argumentCount, arguments);
213         bool state = validator.toBool(0);  // state
214         JSObjectRef successCallback = validator.toFunction(1, true);  // successCallback  
215         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
216
217         // perform
218         MultiCallbackUserDataPtr callback(
219                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
220         if(!callback){
221             LoggerW("Can't create MultiCallbackUserData");
222         }
223         else {
224             callback->setCallback("success", successCallback);
225             callback->setCallback("error", errorCallback);        
226         }
227         
228         BluetoothAdapter::getInstance()->setPowered(state, callback);
229         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
230         
231         return JSValueMakeUndefined(context);
232     } catch (const BasePlatformException &err) {
233         return JSWebAPIException::throwException(context, exception, err);
234     } catch (...) {
235         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setPowered().");
236         return JSWebAPIException::throwException(context, exception, err);
237     }
238 }
239
240 JSValueRef JSBluetoothAdapter::setVisible(JSContextRef context,
241         JSObjectRef object,
242         JSObjectRef thisObject,
243         size_t argumentCount,
244         const JSValueRef arguments[],
245         JSValueRef* exception)
246 {
247     LoggerD("Entered");
248     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
249
250     // Access Check
251     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_SET_VISIBLE);
252     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
253
254     try {    
255         // Check whether Bluetooth is supported or not
256         if(!BluetoothAdapter::isBluetoothSupported()) {
257             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
258         }
259         
260         // Validate arguments
261         ArgumentValidator validator(context, argumentCount, arguments);
262         bool mode = validator.toBool(0);  // mode
263         JSObjectRef successCallback = validator.toFunction(1, true);  // successCallback  
264         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
265         unsigned long timeout = validator.toULong(3, true);  // timeout 
266         if(timeout > 65535)
267             timeout = 180;
268
269         // perform
270         MultiCallbackUserDataPtr callback(
271                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
272         if(!callback){
273             LoggerW("Can't create MultiCallbackUserData");
274         }
275         else {
276             callback->setCallback("success", successCallback);
277             callback->setCallback("error", errorCallback);            
278         }
279         
280         BluetoothAdapter::getInstance()->setVisible(mode, timeout, callback);
281         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
282         
283         return JSValueMakeUndefined(context);
284     } catch (const BasePlatformException &err) {
285         return JSWebAPIException::throwException(context, exception, err);
286     } catch (...) {
287         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.setVisible().");
288         return JSWebAPIException::throwException(context, exception, err);
289     }
290 }
291
292 JSValueRef JSBluetoothAdapter::discoverDevices(JSContextRef context,
293         JSObjectRef object,
294         JSObjectRef thisObject,
295         size_t argumentCount,
296         const JSValueRef arguments[],
297         JSValueRef* exception)
298 {
299     LoggerD("Entered");
300     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
301
302     // Access Check
303     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_DISCOVER_DEVICES);
304     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
305
306     try {    
307         // Check whether Bluetooth is supported or not
308         if(!BluetoothAdapter::isBluetoothSupported()) {
309             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
310         }
311
312         // Validate arguments
313         ArgumentValidator validator(context, argumentCount, arguments);
314
315         // successCallback
316         JSObjectRef successCallback = validator.toCallbackObject(0, false, "onstarted", "ondevicefound", "ondevicedisappeared", "onfinished", NULL);
317
318         // errorCallback
319         JSObjectRef errorCallback = validator.toFunction(1, true);
320
321         MultiCallbackUserDataPtr callback(
322                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
323         if(!callback){
324             LoggerW("Can't create MultiMultiCallbackUserData");
325         }
326         else {
327             // onstarted
328             JSValueRef onstartedValue = JSUtil::getProperty(context , successCallback, "onstarted");
329             if(!JSValueIsUndefined(context, onstartedValue)) {
330                 LoggerD("There is a onstarted()");
331                 callback->setCallback("onstarted", JSUtil::JSValueToObject(context, onstartedValue));
332             }
333             
334             // ondevicefound
335             JSValueRef ondevicefoundValue = JSUtil::getProperty(context , successCallback, "ondevicefound");
336             if(!JSValueIsUndefined(context, ondevicefoundValue)) {
337                 LoggerD("There is a ondevicefound()");
338                 callback->setCallback("ondevicefound", JSUtil::JSValueToObject(context, ondevicefoundValue));
339             }
340             
341             // ondevicedisappeared
342             JSValueRef ondevicedisappearedValue = JSUtil::getProperty(context , successCallback, "ondevicedisappeared");
343             if(!JSValueIsUndefined(context, ondevicedisappearedValue)) {
344                 LoggerD("There is a ondevicedisappeared()");
345                 callback->setCallback("ondevicedisappeared", JSUtil::JSValueToObject(context, ondevicedisappearedValue));
346             }
347             
348             // onfinished
349             JSValueRef onfinishedValue = JSUtil::getProperty(context , successCallback, "onfinished");
350             if(!JSValueIsUndefined(context, onfinishedValue)) {
351                 LoggerD("There is a onfinished()");
352                 callback->setCallback("onfinished", JSUtil::JSValueToObject(context, onfinishedValue));
353             }      
354             
355             callback->setCallback("error", errorCallback);        
356         }
357         
358         // perform       
359         BluetoothAdapter::getInstance()->discoverDevices(callback);
360         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
361         
362         return JSValueMakeUndefined(context);
363     } catch (const BasePlatformException &err) {
364         return JSWebAPIException::throwException(context, exception, err);
365     } catch (...) {
366         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.discoverDevices().");
367         return JSWebAPIException::throwException(context, exception, err);
368     }
369 }
370
371 JSValueRef JSBluetoothAdapter::stopDiscovery(JSContextRef context,
372         JSObjectRef object,
373         JSObjectRef thisObject,
374         size_t argumentCount,
375         const JSValueRef arguments[],
376         JSValueRef* exception)
377 {
378     LoggerD("Entered");
379     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
380
381     // Access Check
382     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_STOP_DISCOVERY);
383     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
384
385     try {
386         // Check whether Bluetooth is supported or not
387         if(!BluetoothAdapter::isBluetoothSupported()) {
388             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
389         }
390         
391         // Validate arguments
392         ArgumentValidator validator(context, argumentCount, arguments);
393         JSObjectRef successCallback = validator.toFunction(0, true);  // successCallback  
394         JSObjectRef errorCallback = validator.toFunction(1, true);  // errorCallback
395
396         // perform
397         MultiCallbackUserDataPtr callback(
398                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
399         if(!callback){
400             LoggerW("Can't create MultiCallbackUserData");
401         }
402         else {
403             callback->setCallback("success", successCallback);
404             callback->setCallback("error", errorCallback);        
405         }
406         
407         BluetoothAdapter::getInstance()->stopDiscovery(callback);
408         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
409
410         return JSValueMakeUndefined(context);
411     } catch (const BasePlatformException &err) {
412         return JSWebAPIException::throwException(context, exception, err);
413     } catch (...) {
414         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.stopDiscovery().");
415         return JSWebAPIException::throwException(context, exception, err);
416     }
417 }
418
419 JSValueRef JSBluetoothAdapter::getKnownDevices(JSContextRef context,
420         JSObjectRef object,
421         JSObjectRef thisObject,
422         size_t argumentCount,
423         const JSValueRef arguments[],
424         JSValueRef* exception)
425 {
426     LoggerD("Entered");
427     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
428
429     // Access Check
430     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_GET_KNOWN_DEVICES);
431     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
432
433     try {
434         // Check whether Bluetooth is supported or not
435         if(!BluetoothAdapter::isBluetoothSupported()) {
436             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
437         }
438         
439         // Validate arguments
440         ArgumentValidator validator(context, argumentCount, arguments);
441         JSObjectRef successCallback = validator.toFunction(0);  // successCallback  
442         JSObjectRef errorCallback = validator.toFunction(1, true);  // errorCallback
443
444         // perform
445         MultiCallbackUserDataPtr callback(
446                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
447         if(!callback){
448             LoggerW("Can't create MultiCallbackUserData");
449         }
450         else {
451             callback->setCallback("success", successCallback);
452             callback->setCallback("error", errorCallback);        
453         }
454         
455         BluetoothAdapter::getInstance()->getKnownDevices(callback);
456         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
457         
458         return JSValueMakeUndefined(context);
459     } catch (const BasePlatformException &err) {
460         return JSWebAPIException::throwException(context, exception, err);
461     } catch (...) {
462         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getKnownDevices().");
463         return JSWebAPIException::throwException(context, exception, err);
464     }
465 }
466
467 JSValueRef JSBluetoothAdapter::getDevice(JSContextRef context,
468         JSObjectRef object,
469         JSObjectRef thisObject,
470         size_t argumentCount,
471         const JSValueRef arguments[],
472         JSValueRef* exception)
473 {
474     LoggerD("Entered");
475     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
476
477     // Access Check
478     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_GET_DEVICE);
479     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
480
481     try {
482         // Check whether Bluetooth is supported or not
483         if(!BluetoothAdapter::isBluetoothSupported()) {
484             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
485         }
486         
487         // Validate arguments
488         ArgumentValidator validator(context, argumentCount, arguments);
489         std::string address = validator.toString(0);  // address
490         JSObjectRef successCallback = validator.toFunction(1);  // successCallback  
491         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
492
493         // perform
494         MultiCallbackUserDataPtr callback(
495                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
496         if(!callback){
497             LoggerW("Can't create MultiCallbackUserData");
498         }
499         else {
500             callback->setCallback("success", successCallback);
501             callback->setCallback("error", errorCallback);        
502         }
503         
504         BluetoothAdapter::getInstance()->getDevice(address, callback);
505         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
506         
507         return JSValueMakeUndefined(context);
508     } catch (const BasePlatformException &err) {
509         return JSWebAPIException::throwException(context, exception, err);
510     } catch (...) {
511         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.getDevice().");
512         return JSWebAPIException::throwException(context, exception, err);
513     }
514 }
515
516 JSValueRef JSBluetoothAdapter::createBonding(JSContextRef context,
517         JSObjectRef object,
518         JSObjectRef thisObject,
519         size_t argumentCount,
520         const JSValueRef arguments[],
521         JSValueRef* exception)
522 {
523     LoggerD("Entered");
524     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
525
526     // Access Check
527     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_CREATE_BONDING);
528     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
529
530     try {
531         // Check whether Bluetooth is supported or not
532         if(!BluetoothAdapter::isBluetoothSupported()) {
533             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
534         }
535         
536         // Validate arguments
537         ArgumentValidator validator(context, argumentCount, arguments);
538         std::string address = validator.toString(0);  // address
539         JSObjectRef successCallback = validator.toFunction(1);  // successCallback  
540         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
541
542         // perform
543         MultiCallbackUserDataPtr callback(
544                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
545         if(!callback){
546             LoggerW("Can't create MultiCallbackUserData");
547         }
548         else {
549             callback->setCallback("success", successCallback);
550             callback->setCallback("error", errorCallback);        
551         }
552         
553         BluetoothAdapter::getInstance()->createBonding(address, callback);
554         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
555         
556         return JSValueMakeUndefined(context);
557     } catch (const BasePlatformException &err) {
558         return JSWebAPIException::throwException(context, exception, err);
559     } catch (...) {
560         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.createBonding().");
561         return JSWebAPIException::throwException(context, exception, err);
562     }
563 }
564
565 JSValueRef JSBluetoothAdapter::destroyBonding(JSContextRef context,
566         JSObjectRef object,
567         JSObjectRef thisObject,
568         size_t argumentCount,
569         const JSValueRef arguments[],
570         JSValueRef* exception)
571 {
572     LoggerD("Entered");
573     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
574
575     // Access Check
576     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_DESTROY_BONDING);
577     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
578
579     try {
580         // Check whether Bluetooth is supported or not
581         if(!BluetoothAdapter::isBluetoothSupported()) {
582             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
583         }
584         
585         // Validate arguments
586         ArgumentValidator validator(context, argumentCount, arguments);
587         std::string address = validator.toString(0);  // address
588         JSObjectRef successCallback = validator.toFunction(1, true);  // successCallback  
589         JSObjectRef errorCallback = validator.toFunction(2, true);  // errorCallback
590
591         // perform
592         MultiCallbackUserDataPtr callback(
593                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
594         if(!callback){
595             LoggerW("Can't create MultiCallbackUserData");
596         }
597         else {
598             callback->setCallback("success", successCallback);
599             callback->setCallback("error", errorCallback);        
600         }
601         
602         BluetoothAdapter::getInstance()->destroyBonding(address, callback);
603         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
604         
605         return JSValueMakeUndefined(context);
606     } catch (const BasePlatformException &err) {
607         return JSWebAPIException::throwException(context, exception, err);
608     } catch (...) {
609         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.destroyBonding().");
610         return JSWebAPIException::throwException(context, exception, err);
611     }
612 }
613
614 JSValueRef JSBluetoothAdapter::registerRFCOMMServiceByUUID(JSContextRef context,
615         JSObjectRef object,
616         JSObjectRef thisObject,
617         size_t argumentCount,
618         const JSValueRef arguments[],
619         JSValueRef* exception)
620 {
621     LoggerD("Entered");
622     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
623
624     // Access Check
625     AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(BLUETOOTH_ADAPTER_API_REGISTER_RFCOMMSERVICE_BY_UUID);
626     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
627
628     try {
629         // Check whether Bluetooth is supported or not
630         if(!BluetoothAdapter::isBluetoothSupported()) {
631             throw DeviceAPI::Common::NotSupportedException("Bluetooth is not supported");
632         }
633         
634         // Validate arguments
635         ArgumentValidator validator(context, argumentCount, arguments);
636         std::string uuid = validator.toString(0);  // uuid
637         std::string name = validator.toString(1);  // name
638         JSObjectRef successCallback = validator.toFunction(2);  // successCallback  
639         JSObjectRef errorCallback = validator.toFunction(3, true);  // errorCallback
640
641         // perform
642         MultiCallbackUserDataPtr callback(
643                 new MultiCallbackUserData(GlobalContextManager::getInstance()->getGlobalContext(context)));
644         if(!callback){
645             LoggerW("Can't create MultiCallbackUserData");
646         }
647         else {
648             callback->setCallback("success", successCallback);
649             callback->setCallback("error", errorCallback);        
650         }
651         
652         BluetoothAdapter::getInstance()->registerRFCOMMServiceByUUID(uuid, name, callback);
653         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
654         
655         return JSValueMakeUndefined(context);
656     } catch (const BasePlatformException &err) {
657         return JSWebAPIException::throwException(context, exception, err);
658     } catch (...) {
659         DeviceAPI::Common::UnknownException err("Unknown Error in BluetoothAdapter.registerRFCOMMServiceByUUID().");
660         return JSWebAPIException::throwException(context, exception, err);
661     }
662 }
663
664
665 } // Bluetooth
666 } // DeviceAPI