merge with master
[platform/framework/web/wrt-plugins-tizen.git] / src / NFC / JSNFCAdapter.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 <dpl/log/log.h>
19
20 #include <string>
21 #include <CommonsJavaScript/Validator.h>
22 #include <Commons/Exception.h>
23 #include <CommonsJavaScript/PrivateObject.h>
24 #include <CommonsJavaScript/JSUtils.h>
25 #include <CommonsJavaScript/JSCallbackManager.h>
26 #include <CommonsJavaScript/Utils.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include <SecurityExceptions.h>
30
31 #include "JSNFCManager.h"
32 #include "JSNFCAdapter.h"
33 #include "JSNdefMessage.h"
34 #include "JSNdefRecord.h"
35 #include "NFCConverter.h"
36 #include "NFCStaticController.h"
37 #include "EventNFCChangedPrivateData.h"
38 #include "NFCAsyncCallbackManager.h"
39 #include "NFCListenerManager.h"
40 #include "NFCFactory.h"
41 #include "EventNFCChanged.h"
42 #include "plugin_config.h"
43
44 using namespace std;
45 using namespace DPL;
46 using namespace DeviceAPI::Common;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49
50 #define TIZEN_NFCADAPTER_ATTRIBUTENAME "NFCAdapter"
51 #define TIZEN_NFCADAPTER_POWERED "powered"
52 #define TIZEN_NFCADAPTER_SETYPE "seType"
53 namespace DeviceAPI {
54 namespace NFC {
55
56  JSClassDefinition JSNFCAdapter::m_classInfo =
57 {
58         0,
59         kJSClassAttributeNone,
60         TIZEN_NFCADAPTER_ATTRIBUTENAME,
61         0,
62         m_property,
63         m_function,
64         initialize,
65         finalize,
66         NULL, //hasProperty,
67         NULL,
68         NULL, //setProperty,
69         NULL, //DeleteProperty,
70         NULL, //GetPropertyNames,
71         NULL, //CallAsFunction,
72         NULL, //CallAsConstructor,
73         NULL, //HasInstance,
74         NULL  //ConvertToType
75 };
76
77 JSStaticValue JSNFCAdapter::m_property[] =
78 {
79     //NFCAdapterProperties
80         {TIZEN_NFCADAPTER_POWERED,  getProperty, NULL, kJSPropertyAttributeReadOnly},
81         {TIZEN_NFCADAPTER_SETYPE,  getProperty, NULL, kJSPropertyAttributeReadOnly},
82         { 0, 0, 0, 0 }
83 };
84
85 JSStaticFunction JSNFCAdapter::m_function[] = {
86         {"setTagListener",      JSNFCAdapter::setTagListener, kJSPropertyAttributeNone },
87         {"unsetTagListener",       JSNFCAdapter::unsetTagListener, kJSPropertyAttributeNone},
88         {"setPeerListener",      JSNFCAdapter::setPeerListener, kJSPropertyAttributeNone },
89         {"unsetPeerListener",       JSNFCAdapter::unsetPeerListener, kJSPropertyAttributeNone},
90         {"getCachedMessage", JSNFCAdapter::getCachedMessage, kJSPropertyAttributeNone},
91         {"setPowered",      JSNFCAdapter::setPowered, kJSPropertyAttributeNone },
92         {"setCardEmulation",      JSNFCAdapter::setCardEmulation, kJSPropertyAttributeNone },
93         {"setCardEmulationChangeListener",      JSNFCAdapter::setCardEmulationChangeListener, kJSPropertyAttributeNone },
94         {"unsetCardEmulationChangeListener",      JSNFCAdapter::unsetCardEmulationChangeListener, kJSPropertyAttributeNone },
95         { 0, 0, 0}
96 };
97
98 JSClassRef JSNFCAdapter::m_jsClassRef = JSClassCreate(JSNFCAdapter::getClassInfo());
99
100 void JSNFCAdapter::initialize(JSContextRef context, JSObjectRef object)
101 {
102         LogDebug("entered. Nothing to do");
103 }
104
105 void JSNFCAdapter::finalize(JSObjectRef object)
106 {
107         NFCAdapterPrivObject* priv =
108                 static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
109         delete priv;
110         JSObjectSetPrivate(object, NULL);
111 }
112
113 JSObjectRef JSNFCAdapter::createJSObject(JSContextRef context) {
114         LogDebug("entered");
115
116         INFCAdapterPtr nfcAdapter(NFCFactory::getInstance().createNFCAdapterObject());
117         NFCAdapterPrivObject *priv = new NFCAdapterPrivObject(context, nfcAdapter);
118
119         if (!priv) {
120                 ThrowMsg(NullPointerException, "Can not new a NFCTag object");
121         }
122
123         return JSObjectMake(context, getClassRef(), priv);
124 }
125
126 const JSClassRef JSNFCAdapter::getClassRef()
127 {
128         if (!m_jsClassRef) {
129                 m_jsClassRef = JSClassCreate(&m_classInfo);
130         }
131         return m_jsClassRef;
132 }
133
134 const JSClassDefinition* JSNFCAdapter::getClassInfo()
135 {
136         return &m_classInfo;
137 }
138
139 JSValueRef JSNFCAdapter::getProperty(JSContextRef context, JSObjectRef object,
140         JSStringRef propertyName, JSValueRef* exception)
141 {
142         LogDebug("Enter");
143
144         Try     {
145                 NFCConverter convert(context);
146
147                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCADAPTER_POWERED)) {
148                         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
149                         if (!privateObject) {
150                                 LogError("private object is null");
151                                 ThrowMsg(NullPointerException, "Private object not initialized");
152                         }
153                         INFCAdapterPtr nfcAdapter(privateObject->getObject() );
154                         return convert.toJSValueRef(nfcAdapter->getPowerState());
155                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_NFCADAPTER_SETYPE)) {
156                         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
157                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
158
159                         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
160                         if (!privateObject) {
161                                 LogError("private object is null");
162                                 ThrowMsg(NullPointerException, "Private object not initialized");
163                         }
164                         INFCAdapterPtr nfcAdapter(privateObject->getObject() );
165                         return convert.toJSValueRefSEType(nfcAdapter->getSEType());
166                 }
167         } Catch (NullPointerException) {
168                 LogError("NullPointerException: " << _rethrown_exception.GetMessage());
169         } Catch (ConversionException) {
170                 LogError("ConversionException: " << _rethrown_exception.GetMessage());
171         } Catch (UnsupportedException) {
172                 LogError("UnsupportedException: " << _rethrown_exception.GetMessage());
173         } Catch (UnknownException) {
174                 LogError("UnknownExceptionException: " << _rethrown_exception.GetMessage());
175         } Catch (PlatformException) {
176                 LogError("PlatformExceptionException: " << _rethrown_exception.GetMessage());
177         } Catch (WrtDeviceApis::Commons::Exception) {
178                 LogError("Exception: " << _rethrown_exception.GetMessage());
179         }
180
181     return JSValueMakeUndefined(context);
182 }
183
184 JSValueRef JSNFCAdapter::setTagListener (JSContextRef context, JSObjectRef object,
185                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
186                 JSValueRef* exception) {
187         LogDebug("JSNFCAdapter::setTagListener Enter");
188
189         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
190         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
191
192         NFCConverter nfcConverter(context);
193         Validator validator(context, exception);
194
195         if (argumentCount == 0) {
196                 LogError("JSNFCAdapter::setTagListener TypeMismatchException");
197                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
198         }
199         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) ) {
200                 /* 1st argument is mandatory. And 1st argument must be Callback. */
201                 LogError("JSNFCAdapter::setTagListener TypeMismatchException!");
202                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
203         }
204
205         NFCChangedCallback callbacks;
206
207         if (JSValueIsObject(context, arguments[0]) &&
208             !validator.isCallback(arguments[0])) {
209             callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
210         } else {
211                 LogError("DetectedCB must has onattach and ondetach");
212                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
213         }
214
215         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
216         if (!privateObject) {
217                 LogError("private object is null");
218                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
219         }
220
221         JSContextRef global_context = privateObject->getContext();
222
223         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
224         JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
225     
226         Try {
227                 INFCAdapterPtr nfcAdapter(privateObject->getObject());
228
229                 //process the filter object
230                 TagFilterPtr tagFilter;
231                 if ((argumentCount > 1) &&  !JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1])) {
232                         if (JSIsArrayValue(context, arguments[1])) {
233                                         tagFilter = nfcConverter.toNFCTagFilter(arguments[1]);
234
235                         } else
236                                 Throw(ConversionException);
237                 }
238                 else
239                 {
240                         TagFilterPtr _filter (new TagFilter());
241                         tagFilter = _filter;
242                 }
243
244                 EventNFCChangedPrivateDataPtr privateData(
245                     new EventNFCChangedPrivateData(callbackManager,
246                                                     detachedCallbackManager)
247                     );
248                 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
249                 emitter->setListener(&NFCStaticController::getInstance());
250                 emitter->setEventPrivateData(StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
251
252                 if (nfcAdapter->setTagListener(emitter, tagFilter) != 0)
253                         Throw(UnknownException);
254
255                 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_TAG_LISTENER)));
256                 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
257                 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
258
259                 return JSValueMakeUndefined(context);
260         } Catch (ConversionException) {
261                 LogError("JSNFCAdapter::setTagListener : ConversionException");
262                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
263     } Catch (PlatformException) {
264                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
265                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
266         } Catch (UnknownException) {
267                 LogError("UnknownException: " << _rethrown_exception.GetMessage());
268         } Catch (WrtDeviceApis::Commons::Exception) {
269                 LogError("Exception: " << _rethrown_exception.GetMessage());
270         }
271         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
272 }
273
274 JSValueRef JSNFCAdapter::setPeerListener (JSContextRef context, JSObjectRef object,
275                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
276                 JSValueRef* exception) {
277         LogDebug("JSNFCAdapter::setPeerListener Enter");
278
279         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
280         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
281
282         NFCConverter nfcConverter(context);
283         Validator validator(context, exception);
284
285
286         if (argumentCount == 0) {
287                 LogError("JSNFCAdapter::setPeerListener TypeMismatchException");
288                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
289         }
290         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) ) {
291                 /* 1st argument is mandatory. And 1st argument must be Callback. */
292                 LogError("JSNFCAdapter::setPeerListener TypeMismatchException!");
293                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
294         }
295
296         NFCChangedCallback callbacks;
297
298         if (JSValueIsObject(context, arguments[0]) &&
299             !validator.isCallback(arguments[0])) {
300             callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
301         } else {
302                 LogError("DetectedCB must has onattach and ondetach");
303                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
304         }
305
306         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
307         if (!privateObject) {
308                 LogError("private object is null");
309                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
310         }
311
312         JSContextRef global_context = privateObject->getContext();
313
314         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
315         JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
316
317         Try {
318                 INFCAdapterPtr nfcAdapter(privateObject->getObject());
319
320                 EventNFCChangedPrivateDataPtr privateData(
321                     new EventNFCChangedPrivateData(callbackManager,
322                                                     detachedCallbackManager)
323                     );
324
325                 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
326                 emitter->setListener(&NFCStaticController::getInstance());
327                 emitter->setEventPrivateData(StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
328
329                 if (nfcAdapter->setPeerListener(emitter) != 0)
330                         Throw(UnknownException);
331
332                 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_PEER_LISTENER)));
333                 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
334                 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
335
336                 return JSValueMakeUndefined(context);
337         } Catch (ConversionException) {
338                 LogError("JSNFCAdapter::setPeerListener : ConversionException");
339                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
340         } Catch (PlatformException) {
341                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
342                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
343         } Catch (UnknownException) {
344                 LogError("UnknownException: " << _rethrown_exception.GetMessage());
345         } Catch (WrtDeviceApis::Commons::Exception) {
346                 LogError("Exception: " << _rethrown_exception.GetMessage());
347         }
348         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
349 }
350 JSValueRef JSNFCAdapter::unsetTagListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
351                 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
352         LogDebug("JSNFCAdapter::unsetNFCTagListener Enter");
353
354         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
355         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
356
357         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
358         if (!privateObject) {
359                 LogError("private object is null");
360                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
361         }
362
363         Try     {
364                 INFCAdapterPtr nfcAdapter( privateObject->getObject() );
365
366                 nfcAdapter->unsetTagListener();
367
368                 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject,  static_cast<long>(ID_NFCADAPTER_TAG_LISTENER)));
369                 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
370                 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
371
372                 return JSValueMakeUndefined(context);
373         } Catch (NullPointerException) {
374                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
375         } Catch (PlatformException) {
376                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
377                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
378         } Catch (WrtDeviceApis::Commons::Exception) {
379                 LogError("Exception: " << _rethrown_exception.GetMessage());
380         }
381         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
382 }
383
384 JSValueRef JSNFCAdapter::unsetPeerListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
385                 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
386         LogDebug("JSNFCAdapter::unsetPeerListener Enter");
387
388         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
389         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
390
391         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
392         if (!privateObject) {
393                 LogError("private object is null");
394                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
395         }
396
397         Try {
398                 INFCAdapterPtr nfcAdapter( privateObject->getObject() );
399
400                 nfcAdapter->unsetPeerListener();
401
402                 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject,  static_cast<long>(ID_NFCADAPTER_PEER_LISTENER)));
403                 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
404                 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
405
406                 return JSValueMakeUndefined(context);
407         } Catch (NullPointerException) {
408                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
409         } Catch (PlatformException) {
410                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
411                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
412         } Catch (WrtDeviceApis::Commons::Exception) {
413                 LogError("Exception: " << _rethrown_exception.GetMessage());
414         }
415         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
416 }
417
418 JSValueRef JSNFCAdapter::getCachedMessage (JSContextRef context, JSObjectRef object,
419                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
420                 JSValueRef* exception) {
421         LogDebug("JSNFCAdapter::getCachedMessage Enter");
422
423         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_COMMON_FUNCS);
424         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
425
426         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
427         if (!privateObject) {
428                 LogError("private object is null");
429                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
430         }
431
432         Try {
433                 INFCAdapterPtr nfcAdapter(privateObject->getObject());
434                 void *cachedMessage = nfcAdapter->getCachedMessage();
435                 if (cachedMessage != NULL)
436                         return JSNdefMessage::createJSObject(context, nfcAdapter->getCachedMessage());
437                 return JSValueMakeNull(context);
438         } Catch (UnknownException) {
439                 LogError("Exception: " << _rethrown_exception.GetMessage());
440         } Catch (PlatformException) {
441                 LogError("Exception: " << _rethrown_exception.GetMessage());
442                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
443         } Catch(NullPointerException) {
444                 LogError("Exception: " << _rethrown_exception.GetMessage());
445                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
446         } Catch (WrtDeviceApis::Commons::Exception) {
447                 LogError("Exception: " << _rethrown_exception.GetMessage());
448         }
449         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
450 }
451
452 JSValueRef JSNFCAdapter::setPowered (JSContextRef context, JSObjectRef object,
453                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
454                 JSValueRef* exception) {
455         LogDebug("Enter");
456
457         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_ADMIN_FUNCS);
458         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
459
460         Validator validator(context, exception);
461     if(argumentCount == 0) {
462         /* 1st argument is mandatory. */
463                 LogError("TypeMismatchException!");
464                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
465     }
466         if ((argumentCount > 1) && !JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !validator.isCallback(arguments[1])) {
467                 /* 1st argument is mandatory. And 1st argument must be Callback. */
468                 LogError("TypeMismatchException!");
469                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
470         }
471         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !validator.isCallback(arguments[2])) {
472                 /* 2nd argument must be Callback. */
473                 LogError("TypeMismatchException!");
474                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
475         }
476
477         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
478         if ((argumentCount > 1) && validator.isCallback(arguments[1])) {
479                 onSuccessForCbm = arguments[1];
480         }
481         if ((argumentCount > 2) && validator.isCallback(arguments[2])) {
482                 onErrorForCbm = arguments[2];
483         }
484
485         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
486         if (!privateObject) {
487                 LogError("private object is null");
488                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
489         }
490
491         JSContextRef global_context = privateObject->getContext();
492
493         INFCAdapterPtr nfcAdapter(privateObject->getObject() );
494         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, onErrorForCbm, true, true);
495
496         Try {
497                 NFCConverter nfcConverter(context);
498                 bool state = false;
499                 state = nfcConverter.toBool(arguments[0]);
500
501                 EventNFCChangedSetPoweredPtr event(new EventNFCChangedSetPowered(state));
502                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
503                 event->setForAsynchronousCall(&NFCStaticController::getInstance());
504                 nfcAdapter->setPowered(event);
505                 NFCAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, global_context);
506                 return JSValueMakeUndefined(context);
507         } Catch (ConversionException) {
508                 LogError("ConversionException");
509                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
510         } Catch (PlatformException) {
511                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
512                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available"));
513                 return JSValueMakeUndefined(context);
514         } Catch (UnknownException) {
515                 LogError("UnknownException: " << _rethrown_exception.GetMessage());
516         } Catch(NullPointerException) {
517                 LogError("Exception: " << _rethrown_exception.GetMessage());
518                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
519         } Catch (WrtDeviceApis::Commons::Exception) {
520                 LogError("Exception: " << _rethrown_exception.GetMessage());
521         }
522         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
523         return JSValueMakeUndefined(context);
524 }
525
526 JSValueRef JSNFCAdapter::setCardEmulation (JSContextRef context, JSObjectRef object,
527                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
528                 JSValueRef* exception) {
529         LogDebug("Enter");
530
531         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
532         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
533
534         Validator validator(context, exception);
535         if(argumentCount == 0) {
536             /* 1st argument is mandatory. */
537                 LogError("TypeMismatchException!");
538                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
539         }
540         if ((argumentCount > 1) && !JSValueIsNull(context, arguments[1]) && !JSValueIsUndefined(context, arguments[1]) && !validator.isCallback(arguments[1])) {
541                 /* And 2st argument must be Callback. */
542                 LogError("TypeMismatchException!");
543                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
544         }
545         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && !JSValueIsUndefined(context, arguments[2]) && !validator.isCallback(arguments[2])) {
546                 /* 3nd argument must be Callback. */
547                 LogError("TypeMismatchException!");
548                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
549         }
550
551         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
552         if ((argumentCount > 1) && validator.isCallback(arguments[1])) {
553                 onSuccessForCbm = arguments[1];
554         }
555         if ((argumentCount > 2) && validator.isCallback(arguments[2])) {
556                 onErrorForCbm = arguments[2];
557         }
558
559         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
560         if (!privateObject) {
561                 LogError("private object is null");
562                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
563         }
564
565         JSContextRef global_context = privateObject->getContext();
566
567         INFCAdapterPtr nfcAdapter(privateObject->getObject() );
568         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, onErrorForCbm, true, true);
569
570         Try {
571                 NFCConverter nfcConverter(context);
572                 EventNFCSEType seType = NFC_SE_INVALID;
573                 seType = nfcConverter.toSEType(arguments[0]);
574
575                 EventNFCSetCardEmulationPtr event(new EventNFCSetCardEmulation(seType));
576                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
577                 event->setForAsynchronousCall(&NFCStaticController::getInstance());
578                 nfcAdapter->setCardEmulation(event);
579                 NFCAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, global_context);
580                 return JSValueMakeUndefined(context);
581         } Catch (ConversionException) {
582                 LogError("ConversionException");
583                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
584         } Catch (PlatformException) {
585                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
586                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available"));
587                 return JSValueMakeUndefined(context);
588         } Catch (UnknownException) {
589                 LogError("UnknownException: " << _rethrown_exception.GetMessage());
590         } Catch(NullPointerException) {
591                 LogError("Exception: " << _rethrown_exception.GetMessage());
592                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
593         } Catch (WrtDeviceApis::Commons::Exception) {
594                 LogError("Exception: " << _rethrown_exception.GetMessage());
595         }
596         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
597         return JSValueMakeUndefined(context);
598 }
599
600 JSValueRef JSNFCAdapter::setCardEmulationChangeListener (JSContextRef context, JSObjectRef object,
601                 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
602                 JSValueRef* exception) {
603         LogDebug("Entered ");
604
605         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
606         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
607
608         Validator validator(context, exception);
609
610         if ((argumentCount < 1) || JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !validator.isCallback(arguments[0])) {
611                 /* 1st argument is mandatory. And 1st argument must be Callback. */
612                 LogError("TypeMismatchException!");
613                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
614         }
615
616         JSValueRef onSuccessForCbm = NULL;
617         if (validator.isCallback(arguments[0])) {
618                 onSuccessForCbm = arguments[0];
619         }
620
621         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
622         if (!privateObject) {
623                 LogError("private object is null");
624                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
625         }
626
627         JSContextRef global_context = privateObject->getContext();
628
629         INFCAdapterPtr nfcAdapter(privateObject->getObject() );
630         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, NULL, true, true);
631
632         Try {
633                 EventNFCChangedCardEmulationEmitterPtr emitter(new EventNFCChangedCardEmulationEmitter);
634                 emitter->setListener(&NFCStaticController::getInstance());
635                 emitter->setEventPrivateData(StaticPointerCast<EventNFCChangedCardEmulation::PrivateDataType>(callbackManager));
636                 nfcAdapter->setCardEmulationChangeListener(emitter);
637
638                 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(global_context, thisObject, static_cast<long>(ID_NFCADAPTER_CARDEMULATION_LISTENER)));
639                 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
640                 NFCListenerManagerSingleton::Instance().registerListener(listenerItem, global_context);
641
642                 return JSValueMakeUndefined(context);
643         } Catch (ConversionException) {
644                 LogError("ConversionException");
645                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
646         } Catch (PlatformException) {
647                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
648                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
649         } Catch (UnknownException) {
650                 LogError("UnknownException: " << _rethrown_exception.GetMessage());
651         } Catch (WrtDeviceApis::Commons::Exception) {
652                 LogError("Exception: " << _rethrown_exception.GetMessage());
653         }
654
655         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
656 }
657
658 JSValueRef JSNFCAdapter::unsetCardEmulationChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
659                 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
660         LogDebug("JSNFCAdapter::unsetPeerListener Enter");
661
662         AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
663         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
664
665         NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(thisObject));
666         if (!privateObject) {
667                 LogError("private object is null");
668                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
669         }
670
671         Try {
672                 INFCAdapterPtr nfcAdapter(privateObject->getObject());
673                 nfcAdapter->unsetCardEmulationChangeListener();
674
675                 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject,  ID_NFCADAPTER_CARDEMULATION_LISTENER));
676                 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
677                 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
678
679                 return JSValueMakeUndefined(context);
680         } Catch (ConversionException) {
681                 LogError("ConversionException");
682                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
683         } Catch (NullPointerException) {
684                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
685         } Catch (PlatformException) {
686                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
687                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::SERVICE_NOT_AVAILABLE, "Service Not Available");
688         } Catch (WrtDeviceApis::Commons::Exception) {
689                 LogError("Exception: " << _rethrown_exception.GetMessage());
690         }
691         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
692 }
693
694
695 }
696 }