Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / JSBluetoothServiceHandler.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 #include "JSBluetoothServiceHandler.h"
18 #include <CommonsJavaScript/Converter.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include <Tizen/Common/SecurityExceptions.h>
26 #include <API/Bluetooth/BluetoothProperty.h>
27 #include "BluetoothConverter.h"
28 #include "BluetoothAdapterListener.h"
29 #include <API/Bluetooth/BluetoothFactory.h>
30 #include <bluetooth.h>
31 #include "plugin_config.h"
32
33 using namespace std;
34 using namespace DPL;
35 using namespace WrtDeviceApis;
36 using namespace TizenApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38
39
40 namespace TizenApis {
41 namespace Tizen1_0 {
42
43 JSClassRef JSBluetoothServiceHandler::m_jsClassRef = NULL;
44
45 JSClassDefinition JSBluetoothServiceHandler::m_classInfo =
46 {
47         0,
48         kJSClassAttributeNone,
49         "BluetoothServiceHandler",
50         NULL,
51         m_properties,
52         m_function,
53         initialize,
54         finalize,
55         NULL, 
56         NULL, 
57         NULL, 
58         NULL, 
59         NULL, 
60         NULL,
61         NULL,
62         NULL,
63         NULL
64 };
65
66
67
68
69 JSStaticValue JSBluetoothServiceHandler::m_properties[] = 
70 {
71         {"uuid", getUuid, NULL, kJSPropertyAttributeReadOnly},
72         {"onConnect", NULL, setOnConnect, kJSPropertyAttributeNone},
73         {"name", getName, NULL, kJSPropertyAttributeReadOnly},                  
74         {"isConnected", getIsConnected, NULL, kJSPropertyAttributeReadOnly},                    
75         {0, 0, 0, 0}
76 };
77
78 JSStaticFunction JSBluetoothServiceHandler::m_function[] =
79 {
80         {"unregister", JSBluetoothServiceHandler::unregisterRFCOMMService, kJSPropertyAttributeNone},
81         { 0, 0, 0 }
82 };
83
84 void JSBluetoothServiceHandler::initialize(JSContextRef context, JSObjectRef object) 
85 {
86         LogDebug("JSBluetoothServiceHandler::initialize");
87 }
88
89 JSObjectRef JSBluetoothServiceHandler::createJSObject(JSContextRef context, std::string name, std::string uuid, int registerSock)
90 {
91         IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(BluetoothFactory::getInstance().getBluetoothServiceHandlerManager());
92         BluetoothServiceHandlerManager->setRegisterSocket(registerSock);
93         BluetoothServiceHandlerManager->setUUID(uuid);
94         BluetoothServiceHandlerManager->setIsConnected(false);
95         BluetoothServiceHandlerManager->setName(name);
96
97         JSBluetoothServiceHandlerPriv* priv = new JSBluetoothServiceHandlerPriv( context, BluetoothServiceHandlerManager);
98
99         return JSObjectMake(context, getClassRef(), priv);
100
101 }
102
103
104
105 const JSClassRef JSBluetoothServiceHandler::getClassRef() 
106 {
107         if (!m_jsClassRef) 
108         {
109                 m_jsClassRef = JSClassCreate(&m_classInfo);
110         }
111         return m_jsClassRef;
112 }
113
114
115 void JSBluetoothServiceHandler::getPropertyNames(JSContextRef context,
116                                         JSObjectRef object,
117                                         JSPropertyNameAccumulatorRef propertyNames)
118 {
119 }
120
121
122 bool JSBluetoothServiceHandler::setOnConnect(JSContextRef context,
123                                         JSObjectRef object,
124                                         JSStringRef propertyName,
125                                         JSValueRef value,
126                                         JSValueRef* exception)
127
128 {
129         LogDebug("OK");
130         
131         try 
132         {
133                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
134                 BluetoothConverter converter(priv->getContext());
135                 JSValueRef successCallback = NULL;
136                 JSValueRef errorCallback = NULL;
137
138                 if (priv == NULL)
139                 {
140                         ThrowMsg(UnknownException, "private object null");
141                 }
142
143                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
144
145                 if (cbm == NULL)
146                 {
147                         ThrowMsg(ConversionException, "callback manager creation fail");
148                 }
149
150                 successCallback = converter.toFunction(value);
151                 
152                 cbm->setOnSuccess(successCallback);
153                 cbm->setOnError(errorCallback);
154                 //cbm->setObject(object);
155
156                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
157                 EventBTServiceOnConnectEmitterPtr emitter(new EventBTServiceOnConnectEmitter);
158
159                 emitter->setListener(&BluetoothAdapterListener::getInstance());
160                 emitter->setEventPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(cbm));
161                 if (BluetoothServiceHandlerManager->setServiceHandlerListener(emitter) != BT_ERROR_NONE)
162                 {
163                         ThrowMsg(UnknownException, "private object null");
164                 }
165                                 
166                 return true;
167         }
168         catch (const WrtDeviceApis::Commons::Exception& ex) 
169         {
170                 LogError("Exception: " << ex.GetMessage());
171
172                 switch (ex.getCode())
173                 {
174                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
175                         JSTizenExceptionFactory::postException(context, exception,
176                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
177                         break;
178                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
179                         JSTizenExceptionFactory::postException(context, exception,
180                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
181                         break;
182                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
183                 default:
184                         JSTizenExceptionFactory::postException(context, exception,
185                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
186                 }
187         }       
188         return false;
189
190 }
191
192
193         
194
195 JSValueRef JSBluetoothServiceHandler::getUuid(JSContextRef context,
196                                         JSObjectRef object,
197                                         JSStringRef propertyName,
198                                         JSValueRef* exception)
199 {
200         LogDebug("OK"); 
201         
202         try 
203         {
204                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
205                 BluetoothConverter converter(priv->getContext());
206
207                 if (priv == NULL)
208                 {
209                         ThrowMsg(UnknownException, "private object null");
210                 }
211
212                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
213                 std::string uuid = BluetoothServiceHandlerManager->getUUID();
214                 return converter.toJSValueRef(uuid);
215
216         }
217         
218         catch (const WrtDeviceApis::Commons::Exception& ex) 
219         {
220                 LogError("Exception: " << ex.GetMessage());
221
222                 switch (ex.getCode())
223                 {
224                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
225                         return JSTizenExceptionFactory::postException(context, exception,
226                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
227                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
228                         return JSTizenExceptionFactory::postException(context, exception,
229                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
230                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
231                 default:
232                         return JSTizenExceptionFactory::postException(context, exception,
233                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
234                 }
235         }       
236         
237         return JSValueMakeUndefined(context);
238 }
239
240
241 JSValueRef JSBluetoothServiceHandler::getName(JSContextRef context,
242                                         JSObjectRef object,
243                                         JSStringRef propertyName,
244                                         JSValueRef* exception)
245 {
246         LogDebug("OK"); 
247         
248         try 
249         {
250                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
251                 BluetoothConverter converter(priv->getContext());
252
253                 if (priv == NULL)
254                 {
255                         ThrowMsg(UnknownException, "private object null");
256                 }
257
258                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
259                 std::string name = BluetoothServiceHandlerManager->getName();
260                 return converter.toJSValueRef(name);
261
262         }
263         
264         catch (const WrtDeviceApis::Commons::Exception& ex) 
265         {
266                 LogError("Exception: " << ex.GetMessage());
267
268                 switch (ex.getCode())
269                 {
270                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
271                         return JSTizenExceptionFactory::postException(context, exception,
272                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
273                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
274                         return JSTizenExceptionFactory::postException(context, exception,
275                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
276                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
277                 default:
278                         return JSTizenExceptionFactory::postException(context, exception,
279                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
280                 }
281         }       
282         
283         return JSValueMakeUndefined(context);
284 }
285
286 JSValueRef JSBluetoothServiceHandler::getIsConnected(JSContextRef context,
287                                         JSObjectRef object,
288                                         JSStringRef propertyName,
289                                         JSValueRef* exception)
290 {
291         LogDebug("OK"); 
292         
293         try 
294         {
295                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
296                 BluetoothConverter converter(priv->getContext());
297
298                 if (priv == NULL)
299                 {
300                         ThrowMsg(UnknownException, "private object null");
301                 }
302
303                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
304                 bool connected = BluetoothServiceHandlerManager->getIsConnected();
305                 return converter.toJSValueRef(connected);
306
307         }
308         
309         catch (const WrtDeviceApis::Commons::Exception& ex) 
310         {
311                 LogError("Exception: " << ex.GetMessage());
312
313                 switch (ex.getCode())
314                 {
315                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
316                         return JSTizenExceptionFactory::postException(context, exception,
317                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
318                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
319                         return JSTizenExceptionFactory::postException(context, exception,
320                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
321                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
322                 default:
323                         return JSTizenExceptionFactory::postException(context, exception,
324                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
325                 }
326         }       
327         
328         return JSValueMakeUndefined(context);
329 }
330
331 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo() 
332 {
333         return &m_classInfo;
334 }
335
336
337
338
339
340 void JSBluetoothServiceHandler::finalize(JSObjectRef object) 
341 {
342         JSBluetoothServiceHandlerPriv* priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
343         LogDebug("JSBluetoothServiceHandler::Finalrize");
344
345         if (priv != NULL)
346         {
347                 JSObjectSetPrivate(object, NULL);
348                 LogDebug("Deleting BluetoothManager");
349                 delete priv;
350         }
351 }
352
353 bool JSBluetoothServiceHandler::hasInstance(JSContextRef context, JSObjectRef constructor,
354                 JSValueRef possibleInstance, JSValueRef* exception) 
355 {
356         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
357 }
358
359 JSValueRef JSBluetoothServiceHandler::unregisterRFCOMMService(JSContextRef context, JSObjectRef object,
360                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
361                 JSValueRef* exception) 
362 {
363
364         LogDebug("unregisterRFCOMMService");
365         
366         JSValueRef successCallback = NULL;
367         JSValueRef errorCallBack = NULL;
368         
369         Try     
370         {
371                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(thisObject));
372
373                 if (priv == NULL)
374                 {
375                         LogError("priv null");
376                         Throw(UnknownException);        
377                 }
378
379                 if (argumentCount < 1 || argumentCount > 2)
380                 {
381                         LogError("Wrong parameter count passed");
382                         Throw(InvalidArgumentException);
383                 }
384
385                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
386                                 priv->getContext(),
387                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_UNREGISTER_RFCOMM]);
388                 
389                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);  
390         
391
392                 BluetoothConverter converter(priv->getContext());
393
394                 successCallback = converter.toFunction(arguments[0]);
395                 
396                 if (argumentCount >= 2)
397                 {
398                         errorCallBack = converter.toFunctionOrNull(arguments[1]);
399                 }
400
401                 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
402                 
403                 if (cbm == NULL)
404                 {
405                         Throw(UnknownException);        
406                 }
407                         
408                 cbm->setOnSuccess(successCallback);
409                 cbm->setOnError(errorCallBack);
410 //              cbm->setObject(thisObject);
411                 
412                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
413                 EventBTUnregisterRFCOMMServicePtr event(new EventBTUnregisterRFCOMMService);
414
415                 if (BluetoothServiceHandlerManager == NULL || event == NULL)
416                 {
417                         LogError("BluetoothAdapterManager or event or listener NULL");
418                         Throw(UnknownException);        
419                 }
420
421                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
422                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
423                 event->setForAsynchronousCall(&listener);
424                 BluetoothServiceHandlerManager->unRegister(event);
425                 
426                 return JSValueMakeNull(context);
427         }
428         Catch (WrtDeviceApis::Commons::ConversionException)
429         {
430                 LogError("ConversionException");
431                 return JSTizenExceptionFactory::postException(context, exception, 
432                                         JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
433
434         }               
435         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
436         {
437                 LogError("InvalidArgumentException");
438                 return JSTizenExceptionFactory::postException(context, exception, 
439                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
440
441         }
442         Catch (WrtDeviceApis::Commons::Exception) 
443         {
444                 LogError("Exception");
445                 return JSTizenExceptionFactory::postException(context, exception, 
446                         JSTizenException::UNKNOWN_ERROR, "unknown error");
447
448         }       
449         return JSValueMakeUndefined(context);
450 }
451
452
453 }
454 }
455