2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dpl/log/log.h>
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>
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"
46 using namespace DeviceAPI::Common;
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
50 #define TIZEN_NFCADAPTER_ATTRIBUTENAME "NFCAdapter"
51 #define TIZEN_NFCADAPTER_POWERED "powered"
52 #define TIZEN_NFCADAPTER_SETYPE "seType"
56 JSClassDefinition JSNFCAdapter::m_classInfo =
59 kJSClassAttributeNone,
60 TIZEN_NFCADAPTER_ATTRIBUTENAME,
69 NULL, //DeleteProperty,
70 NULL, //GetPropertyNames,
71 NULL, //CallAsFunction,
72 NULL, //CallAsConstructor,
77 JSStaticValue JSNFCAdapter::m_property[] =
79 //NFCAdapterProperties
80 {TIZEN_NFCADAPTER_POWERED, getProperty, NULL, kJSPropertyAttributeReadOnly},
81 {TIZEN_NFCADAPTER_SETYPE, getProperty, NULL, kJSPropertyAttributeReadOnly},
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 },
98 JSClassRef JSNFCAdapter::m_jsClassRef = JSClassCreate(JSNFCAdapter::getClassInfo());
100 void JSNFCAdapter::initialize(JSContextRef context, JSObjectRef object)
102 LogDebug("entered. Nothing to do");
105 void JSNFCAdapter::finalize(JSObjectRef object)
107 NFCAdapterPrivObject* priv =
108 static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
110 JSObjectSetPrivate(object, NULL);
113 JSObjectRef JSNFCAdapter::createJSObject(JSContextRef context) {
116 INFCAdapterPtr nfcAdapter(NFCFactory::getInstance().createNFCAdapterObject());
117 NFCAdapterPrivObject *priv = new NFCAdapterPrivObject(context, nfcAdapter);
120 ThrowMsg(NullPointerException, "Can not new a NFCTag object");
123 return JSObjectMake(context, getClassRef(), priv);
126 const JSClassRef JSNFCAdapter::getClassRef()
129 m_jsClassRef = JSClassCreate(&m_classInfo);
134 const JSClassDefinition* JSNFCAdapter::getClassInfo()
139 JSValueRef JSNFCAdapter::getProperty(JSContextRef context, JSObjectRef object,
140 JSStringRef propertyName, JSValueRef* exception)
145 NFCConverter convert(context);
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");
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);
159 NFCAdapterPrivObject* privateObject = static_cast<NFCAdapterPrivObject*>(JSObjectGetPrivate(object));
160 if (!privateObject) {
161 LogError("private object is null");
162 ThrowMsg(NullPointerException, "Private object not initialized");
164 INFCAdapterPtr nfcAdapter(privateObject->getObject() );
165 return convert.toJSValueRefSEType(nfcAdapter->getSEType());
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());
181 return JSValueMakeUndefined(context);
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");
189 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
190 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
192 NFCConverter nfcConverter(context);
193 Validator validator(context, exception);
195 if (argumentCount == 0) {
196 LogError("JSNFCAdapter::setTagListener TypeMismatchException");
197 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
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");
205 NFCChangedCallback callbacks;
207 if (JSValueIsObject(context, arguments[0]) &&
208 !validator.isCallback(arguments[0])) {
209 callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
211 LogError("DetectedCB must has onattach and ondetach");
212 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
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");
221 JSContextRef global_context = privateObject->getContext();
223 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
224 JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
227 INFCAdapterPtr nfcAdapter(privateObject->getObject());
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]);
236 Throw(ConversionException);
240 TagFilterPtr _filter (new TagFilter());
244 EventNFCChangedPrivateDataPtr privateData(
245 new EventNFCChangedPrivateData(callbackManager,
246 detachedCallbackManager)
248 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
249 emitter->setListener(&NFCStaticController::getInstance());
250 emitter->setEventPrivateData(StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
252 if (nfcAdapter->setTagListener(emitter, tagFilter) != 0)
253 Throw(UnknownException);
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);
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());
271 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
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");
279 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
280 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
282 NFCConverter nfcConverter(context);
283 Validator validator(context, exception);
286 if (argumentCount == 0) {
287 LogError("JSNFCAdapter::setPeerListener TypeMismatchException");
288 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
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");
296 NFCChangedCallback callbacks;
298 if (JSValueIsObject(context, arguments[0]) &&
299 !validator.isCallback(arguments[0])) {
300 callbacks = nfcConverter.toNFCChangedCallback(arguments[0]);
302 LogError("DetectedCB must has onattach and ondetach");
303 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
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");
312 JSContextRef global_context = privateObject->getContext();
314 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, callbacks.onattach, NULL, true, true);
315 JSCallbackManagerPtr detachedCallbackManager = JSCallbackManager::createObject(global_context, callbacks.ondetach, NULL);
318 INFCAdapterPtr nfcAdapter(privateObject->getObject());
320 EventNFCChangedPrivateDataPtr privateData(
321 new EventNFCChangedPrivateData(callbackManager,
322 detachedCallbackManager)
325 EventNFCChangedEmitterPtr emitter(new EventNFCChangedEmitter);
326 emitter->setListener(&NFCStaticController::getInstance());
327 emitter->setEventPrivateData(StaticPointerCast<EventNFCChanged::PrivateDataType>(privateData));
329 if (nfcAdapter->setPeerListener(emitter) != 0)
330 Throw(UnknownException);
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);
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());
348 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
350 JSValueRef JSNFCAdapter::unsetTagListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
351 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
352 LogDebug("JSNFCAdapter::unsetNFCTagListener Enter");
354 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_TAG_FUNCS);
355 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
364 INFCAdapterPtr nfcAdapter( privateObject->getObject() );
366 nfcAdapter->unsetTagListener();
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);
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());
381 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
384 JSValueRef JSNFCAdapter::unsetPeerListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
385 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
386 LogDebug("JSNFCAdapter::unsetPeerListener Enter");
388 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_P2P_FUNCS);
389 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
398 INFCAdapterPtr nfcAdapter( privateObject->getObject() );
400 nfcAdapter->unsetPeerListener();
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);
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());
415 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
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");
423 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_COMMON_FUNCS);
424 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
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());
449 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
452 JSValueRef JSNFCAdapter::setPowered (JSContextRef context, JSObjectRef object,
453 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
454 JSValueRef* exception) {
457 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_ADMIN_FUNCS);
458 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
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");
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");
477 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
478 if ((argumentCount > 1) && validator.isCallback(arguments[1])) {
479 onSuccessForCbm = arguments[1];
481 if ((argumentCount > 2) && validator.isCallback(arguments[2])) {
482 onErrorForCbm = arguments[2];
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");
491 JSContextRef global_context = privateObject->getContext();
493 INFCAdapterPtr nfcAdapter(privateObject->getObject() );
494 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, onErrorForCbm, true, true);
497 NFCConverter nfcConverter(context);
499 state = nfcConverter.toBool(arguments[0]);
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());
522 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
523 return JSValueMakeUndefined(context);
526 JSValueRef JSNFCAdapter::setCardEmulation (JSContextRef context, JSObjectRef object,
527 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
528 JSValueRef* exception) {
531 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
532 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
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");
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");
551 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
552 if ((argumentCount > 1) && validator.isCallback(arguments[1])) {
553 onSuccessForCbm = arguments[1];
555 if ((argumentCount > 2) && validator.isCallback(arguments[2])) {
556 onErrorForCbm = arguments[2];
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");
565 JSContextRef global_context = privateObject->getContext();
567 INFCAdapterPtr nfcAdapter(privateObject->getObject() );
568 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, onErrorForCbm, true, true);
571 NFCConverter nfcConverter(context);
572 EventNFCSEType seType = NFC_SE_INVALID;
573 seType = nfcConverter.toSEType(arguments[0]);
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());
596 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
597 return JSValueMakeUndefined(context);
600 JSValueRef JSNFCAdapter::setCardEmulationChangeListener (JSContextRef context, JSObjectRef object,
601 JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[],
602 JSValueRef* exception) {
603 LogDebug("Entered ");
605 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
606 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
608 Validator validator(context, exception);
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");
616 JSValueRef onSuccessForCbm = NULL;
617 if (validator.isCallback(arguments[0])) {
618 onSuccessForCbm = arguments[0];
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");
627 JSContextRef global_context = privateObject->getContext();
629 INFCAdapterPtr nfcAdapter(privateObject->getObject() );
630 JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(global_context, onSuccessForCbm, NULL, true, true);
633 EventNFCChangedCardEmulationEmitterPtr emitter(new EventNFCChangedCardEmulationEmitter);
634 emitter->setListener(&NFCStaticController::getInstance());
635 emitter->setEventPrivateData(StaticPointerCast<EventNFCChangedCardEmulation::PrivateDataType>(callbackManager));
636 nfcAdapter->setCardEmulationChangeListener(emitter);
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);
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());
655 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
658 JSValueRef JSNFCAdapter::unsetCardEmulationChangeListener(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
659 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) {
660 LogDebug("JSNFCAdapter::unsetPeerListener Enter");
662 AceSecurityStatus status = NFC_CHECK_ACCESS(NFC_FUNCTION_API_CARDEMULATION_FUNCS);
663 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
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");
672 INFCAdapterPtr nfcAdapter(privateObject->getObject());
673 nfcAdapter->unsetCardEmulationChangeListener();
675 NFCListenerCancellerPtr canceller = NFCListenerCancellerPtr(new NFCListenerCanceller(privateObject->getContext(), thisObject, ID_NFCADAPTER_CARDEMULATION_LISTENER));
676 IListenerItemPtr listenerItem = StaticPointerCast<IListenerItem>(canceller);
677 NFCListenerManagerSingleton::Instance().unregisterListener(listenerItem);
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());
691 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");