Add DRAFT stubs for Vehicle plugin
[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                 BluetoothServiceHandlerManager->setServiceHandlerListener(emitter);
162                                 
163                 return true;
164         }
165         catch (const WrtDeviceApis::Commons::Exception& ex) 
166         {
167                 LogError("Exception: " << ex.GetMessage());
168
169                 switch (ex.getCode())
170                 {
171                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
172                         JSTizenExceptionFactory::postException(context, exception,
173                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
174                         break;
175                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
176                         JSTizenExceptionFactory::postException(context, exception,
177                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
178                         break;
179                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
180                 default:
181                         JSTizenExceptionFactory::postException(context, exception,
182                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
183                 }
184         }       
185         return false;
186
187 }
188
189
190         
191
192 JSValueRef JSBluetoothServiceHandler::getUuid(JSContextRef context,
193                                         JSObjectRef object,
194                                         JSStringRef propertyName,
195                                         JSValueRef* exception)
196 {
197         LogDebug("OK"); 
198         
199         try 
200         {
201                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
202                 BluetoothConverter converter(priv->getContext());
203
204                 if (priv == NULL)
205                 {
206                         ThrowMsg(UnknownException, "private object null");
207                 }
208
209                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
210                 std::string uuid = BluetoothServiceHandlerManager->getUUID();
211                 return converter.toJSValueRef(uuid);
212
213         }
214         
215         catch (const WrtDeviceApis::Commons::Exception& ex) 
216         {
217                 LogError("Exception: " << ex.GetMessage());
218
219                 switch (ex.getCode())
220                 {
221                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
222                         return JSTizenExceptionFactory::postException(context, exception,
223                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
224                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
225                         return JSTizenExceptionFactory::postException(context, exception,
226                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
227                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
228                 default:
229                         return JSTizenExceptionFactory::postException(context, exception,
230                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
231                 }
232         }       
233         
234         return JSValueMakeUndefined(context);
235 }
236
237
238 JSValueRef JSBluetoothServiceHandler::getName(JSContextRef context,
239                                         JSObjectRef object,
240                                         JSStringRef propertyName,
241                                         JSValueRef* exception)
242 {
243         LogDebug("OK"); 
244         
245         try 
246         {
247                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
248                 BluetoothConverter converter(priv->getContext());
249
250                 if (priv == NULL)
251                 {
252                         ThrowMsg(UnknownException, "private object null");
253                 }
254
255                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
256                 std::string name = BluetoothServiceHandlerManager->getName();
257                 return converter.toJSValueRef(name);
258
259         }
260         
261         catch (const WrtDeviceApis::Commons::Exception& ex) 
262         {
263                 LogError("Exception: " << ex.GetMessage());
264
265                 switch (ex.getCode())
266                 {
267                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
268                         return JSTizenExceptionFactory::postException(context, exception,
269                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
270                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
271                         return JSTizenExceptionFactory::postException(context, exception,
272                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
273                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
274                 default:
275                         return JSTizenExceptionFactory::postException(context, exception,
276                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
277                 }
278         }       
279         
280         return JSValueMakeUndefined(context);
281 }
282
283 JSValueRef JSBluetoothServiceHandler::getIsConnected(JSContextRef context,
284                                         JSObjectRef object,
285                                         JSStringRef propertyName,
286                                         JSValueRef* exception)
287 {
288         LogDebug("OK"); 
289         
290         try 
291         {
292                 JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
293                 BluetoothConverter converter(priv->getContext());
294
295                 if (priv == NULL)
296                 {
297                         ThrowMsg(UnknownException, "private object null");
298                 }
299
300                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
301                 bool connected = BluetoothServiceHandlerManager->getIsConnected();
302                 return converter.toJSValueRef(connected);
303
304         }
305         
306         catch (const WrtDeviceApis::Commons::Exception& ex) 
307         {
308                 LogError("Exception: " << ex.GetMessage());
309
310                 switch (ex.getCode())
311                 {
312                 case WrtDeviceApis::Commons::ExceptionCodes::ConversionException:
313                         return JSTizenExceptionFactory::postException(context, exception,
314                                 JSTizenException::TYPE_MISMATCH_ERROR, "type mismatch error");
315                 case WrtDeviceApis::Commons::ExceptionCodes::InvalidArgumentException:
316                         return JSTizenExceptionFactory::postException(context, exception,
317                                 JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");
318                 case WrtDeviceApis::Commons::ExceptionCodes::Exception:
319                 default:
320                         return JSTizenExceptionFactory::postException(context, exception,
321                                 JSTizenException::UNKNOWN_ERROR, "Unkown error");
322                 }
323         }       
324         
325         return JSValueMakeUndefined(context);
326 }
327
328 const JSClassDefinition* JSBluetoothServiceHandler::getClassInfo() 
329 {
330         return &m_classInfo;
331 }
332
333
334
335
336
337 void JSBluetoothServiceHandler::finalize(JSObjectRef object) 
338 {
339         JSBluetoothServiceHandlerPriv* priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(object));
340         LogDebug("JSBluetoothServiceHandler::Finalrize");
341
342         if (priv != NULL)
343         {
344                 JSObjectSetPrivate(object, NULL);
345                 LogDebug("Deleting BluetoothManager");
346                 delete priv;
347         }
348 }
349
350 bool JSBluetoothServiceHandler::hasInstance(JSContextRef context, JSObjectRef constructor,
351                 JSValueRef possibleInstance, JSValueRef* exception) 
352 {
353         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
354 }
355
356 JSValueRef JSBluetoothServiceHandler::unregisterRFCOMMService(JSContextRef context, JSObjectRef object,
357                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
358                 JSValueRef* exception) 
359 {
360
361         LogDebug("unregisterRFCOMMService");
362         
363         JSValueRef successCallback = NULL;
364         JSValueRef errorCallBack = NULL;
365         JSBluetoothServiceHandlerPriv *priv = static_cast<JSBluetoothServiceHandlerPriv*>(JSObjectGetPrivate(thisObject));
366         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(priv->getContext(), NULL, NULL);
367         size_t index = 0;
368         JSValueRef reserveArguments[2];
369         Try     
370         {
371                 for (index = 0; index < 2; index++)
372                 {
373                         if (index < argumentCount)
374                                 reserveArguments[index] = arguments[index];
375                         else 
376                                 reserveArguments[index] = JSValueMakeUndefined(context);
377                 }
378                 
379
380                 if (priv == NULL)
381                 {
382                         LogError("priv null");
383                         Throw(ConversionException);     
384                 }
385
386                 AceSecurityStatus status = BLUETOOTH_CHECK_ACCESS(
387                                 bluetoothExportedNames[BLUETOOTH_FUNCTION_API_ADPATER_UNREGISTER]);
388                 
389                 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);  
390         
391
392                 BluetoothConverter converter(priv->getContext());
393
394                 if (argumentCount >= 1 && JSValueIsUndefined(context, reserveArguments[0]) == false)
395                 {
396                         successCallback = converter.toFunctionOrNull(reserveArguments[0]);
397                 }
398                 
399                 if (argumentCount >= 2 && JSValueIsUndefined(context, reserveArguments[1]) == false)
400                 {
401                         errorCallBack = converter.toFunctionOrNull(reserveArguments[1]);
402                 }
403
404                 
405                 if (cbm == NULL)
406                 {
407                         Throw(UnknownException);        
408                 }
409                         
410                 cbm->setOnSuccess(successCallback);
411                 cbm->setOnError(errorCallBack);
412                 
413                 IBluetoothServiceHandlerManagerPtr BluetoothServiceHandlerManager(priv->getObject());
414                 EventBTUnregisterRFCOMMServicePtr event(new EventBTUnregisterRFCOMMService);
415
416                 if (BluetoothServiceHandlerManager == NULL || event == NULL)
417                 {
418                         LogError("BluetoothAdapterManager or event or listener NULL");
419                         Throw(UnknownException);        
420                 }
421
422                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData > (cbm));
423                 BluetoothAdapterListener& listener = BluetoothAdapterListener::getInstance();
424                 event->setForAsynchronousCall(&listener);
425                 BluetoothServiceHandlerManager->unRegister(event);
426                 
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                 cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(priv->getContext(), 
439                                 JSTizenException::INVALID_VALUES_ERROR, "invalid values error"));       
440         }
441         Catch (WrtDeviceApis::Commons::Exception) 
442         {
443                 LogError("Exception");
444                 return JSTizenExceptionFactory::postException(context, exception, 
445                         JSTizenException::UNKNOWN_ERROR, "unknown error");
446         }       
447         return JSValueMakeUndefined(context);
448 }
449
450
451 }
452 }
453