Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNFCTagMifareClassic.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
18 #include "JSNFCTagMifareClassic.h"
19 #include "NFCConverter.h"
20 #include "ResponseDispatcher.h"
21 #include "JSNFCTag.h"
22 #include <dpl/log/log.h>
23
24 #include <CommonsJavaScript/Validator.h>
25 #include <Commons/Exception.h>
26 #include <CommonsJavaScript/PrivateObject.h>
27 #include <CommonsJavaScript/JSUtils.h>
28 #include <CommonsJavaScript/JSCallbackManager.h>
29 #include <CommonsJavaScript/Utils.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/SecurityExceptions.h>
33 #include <API/NFC/NFCFactory.h>
34 #include <API/NFC/EventTagMifareClassicAction.h>
35
36 #include "plugin_config.h"
37
38
39 using namespace TizenApis::Api::NFC;
40 using namespace TizenApis::Commons;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace DPL;
44
45 #define TIZEN10_NFCTTAGMIFARECLASSIC_ATTRIBUTENAME "NFCTagMifareClassic"
46
47 namespace TizenApis {
48 namespace Tizen1_0 {
49
50  JSClassDefinition JSNFCTagMifareClassic::m_classInfo =
51 {
52     0,
53     kJSClassAttributeNone,
54     TIZEN10_NFCTTAGMIFARECLASSIC_ATTRIBUTENAME,
55     JSNFCTag::getClassRef(),
56     NULL,
57     m_function,
58     initialize,
59     finalize,
60     NULL, //hasProperty,
61     NULL,
62     NULL, //setProperty,
63     NULL, //DeleteProperty,
64     NULL, //GetPropertyNames,
65     NULL, //CallAsFunction,
66     NULL, //CallAsConstructor,
67     hasInstance, //HasInstance,
68     NULL  //ConvertToType
69 };
70
71 JSStaticFunction JSNFCTagMifareClassic::m_function[] = {
72     {"authenticateWithKeyA", JSNFCTagMifareClassic::authenticateWithKeyA, kJSPropertyAttributeNone},
73     {"authenticateWithKeyB", JSNFCTagMifareClassic::authenticateWithKeyB, kJSPropertyAttributeNone},
74     {"readBlock", JSNFCTagMifareClassic::readBlock, kJSPropertyAttributeNone},
75     {"writeBlock", JSNFCTagMifareClassic::writeBlock, kJSPropertyAttributeNone},
76     {"incrementBlockValue", JSNFCTagMifareClassic::incrementBlockValue, kJSPropertyAttributeNone},
77     {"decrementBlockValue", JSNFCTagMifareClassic::decrementBlockValue, kJSPropertyAttributeNone},
78     {"transfer", JSNFCTagMifareClassic::transfer, kJSPropertyAttributeNone},
79     {"restore", JSNFCTagMifareClassic::restore, kJSPropertyAttributeNone},
80     { 0, 0, 0}
81 };
82
83 JSClassRef JSNFCTagMifareClassic::m_jsClassRef = JSClassCreate(JSNFCTagMifareClassic::getClassInfo());
84
85 JSObjectRef JSNFCTagMifareClassic::createJSObject(JSContextRef context, void *tagHandle, void* privManager) {
86         LogDebug("entered");
87
88         INFCTagMifareClassicPtr NFCTagMifareClassic = NFCFactory::getInstance().createNFCTagMifareClassicObject(tagHandle);
89         NFCTagMifareClassic->setPrivateNFCManagerPtr(privManager);
90         
91         NFCTagMifareClassicPrivObject *priv = new NFCTagMifareClassicPrivObject(context, NFCTagMifareClassic);
92
93         if (!priv) {
94                 ThrowMsg(NullPointerException, "Can not new a NFCTagMifareClassic object");
95         }
96         
97         return JSObjectMake(context, getClassRef(), priv);
98 }
99
100 void JSNFCTagMifareClassic::initialize(JSContextRef context, JSObjectRef object)
101 {
102 }
103
104 void JSNFCTagMifareClassic::finalize(JSObjectRef object)
105 {
106         LogDebug( "entered" );
107         NFCTagMifareClassicPrivObject *priv = static_cast<NFCTagMifareClassicPrivObject*>( JSObjectGetPrivate( object ) ) ;
108         JSObjectSetPrivate(object, NULL);
109         LogDebug("Deleting timezone object");
110         delete priv;
111 }
112
113
114 const JSClassRef JSNFCTagMifareClassic::getClassRef()
115 {
116         if (!m_jsClassRef) {
117                 m_jsClassRef = JSClassCreate(&m_classInfo);
118         }
119         return m_jsClassRef;
120 }
121
122 const JSClassDefinition* JSNFCTagMifareClassic::getClassInfo()
123 {
124         return &m_classInfo;
125 }
126
127 bool JSNFCTagMifareClassic::hasInstance(JSContextRef context,
128         JSObjectRef constructor,
129         JSValueRef possibleInstance,
130         JSValueRef* exception)
131 {
132     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
133 }
134
135 JSValueRef JSNFCTagMifareClassic::authenticateWithKeyA(JSContextRef context,
136         JSObjectRef object,
137         JSObjectRef thisObject,
138         size_t argumentCount,
139         const JSValueRef arguments[],
140         JSValueRef* exception)
141 {
142         LogDebug("Entered ");
143
144         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
145         if (NULL == privateObject) {
146                 LogError("private object is null");
147                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
148         }
149
150         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
151         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
152
153         NFCConverter convert(context);
154         Validator validator(context, exception);
155
156         if ((argumentCount < 2) || (argumentCount > 4)) {
157                 LogError("JSNFCTagMifareClassic::authenticateWithKeyA arguemtcount error");
158                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
159         }
160
161         if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSIsArrayValue(context, arguments[0])) 
162                 || (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSValueIsNumber(context, arguments[1]))){
163                 LogError("JSNFCTagMifareClassic::authenticateWithKeyA key or sectorIndex TypeMismatchException!");
164                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
165         }
166
167         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
168                 /* 1st argument is mandatory. And 1st argument must be Callback. */
169                 LogError("JSNFCTagMifareClassic::authenticateWithKeyA SuccessCallback TypeMismatchException!");
170                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
171         }
172         if((argumentCount > 3) && !JSValueIsNull(context, arguments[3]) && (JSValueIsUndefined(context, arguments[3]) || !validator.isCallback(arguments[3]))) {
173                 /* 2nd argument must be Callback. */
174                 LogError("JSNFCTagMifareClassic::authenticateWithKeyA ErrorCallback TypeMismatchException!");
175                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
176         }
177
178         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
179         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
180                 onSuccessForCbm = arguments[2];
181         }
182         if ((argumentCount > 3) && (validator.isCallback(arguments[3]))) {
183                 onErrorForCbm = arguments[3];
184         }
185
186         
187         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
188         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
189         Try {
190                 EventTagMifareClassicAuthenticateWithKeyAPtr event(new EventTagMifareClassicAuthenticateWithKeyA(convert.toVectorOfUChars(arguments[0]), convert.toInt(arguments[1])));
191                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
192                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
193                 NFCTagMifareClassic->authenticateWithKeyA(event);
194                 
195                 return JSValueMakeNull(context);
196         } Catch (ConversionException) {
197                 LogError("JSNFCManager::authenticateWithKeyA : ConversionException");
198                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
199         } Catch (InvalidArgumentException) {
200                 LogError("JSNFCManager::authenticateWithKeyA InvalidArgumentException");
201                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
202         } Catch (PlatformException) {
203                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
204                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
205                 return JSValueMakeNull(context);
206         } Catch (WrtDeviceApis::Commons::Exception) {
207                 LogError("Exception: " << _rethrown_exception.GetMessage());
208                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
209                 return JSValueMakeNull(context);
210         }
211
212         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
213         return JSValueMakeNull(context);
214 }
215
216 JSValueRef JSNFCTagMifareClassic::authenticateWithKeyB(JSContextRef context,
217         JSObjectRef object,
218         JSObjectRef thisObject,
219         size_t argumentCount,
220         const JSValueRef arguments[],
221         JSValueRef* exception)
222 {
223         LogDebug("Entered ");
224
225         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
226         if (NULL == privateObject) {
227                 LogError("private object is null");
228                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
229         }
230
231         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
232         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
233
234         NFCConverter convert(context);
235         Validator validator(context, exception);
236
237         if ((argumentCount < 2) || (argumentCount > 4)) {
238                 LogError("JSNFCTagMifareClassic::authenticateWithKeyB argumentCount error");
239                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
240         }
241
242         if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSIsArrayValue(context, arguments[0])) 
243                 || (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSValueIsNumber(context, arguments[1]))){
244                 LogError("JSNFCTagMifareClassic::authenticateWithKeyB key or sectorIndex TypeMismatchException!");
245                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
246         }
247
248         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
249                 /* 1st argument is mandatory. And 1st argument must be Callback. */
250                 LogError("JSNFCTagMifareClassic::authenticateWithKeyB SuccessCallback TypeMismatchException!");
251                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
252         }
253         if((argumentCount > 3) && !JSValueIsNull(context, arguments[3]) && (JSValueIsUndefined(context, arguments[3]) || !validator.isCallback(arguments[3]))){
254                 /* 2nd argument must be Callback. */
255                 LogError("JSNFCTagMifareClassic::authenticateWithKeyB ErrorCallback TypeMismatchException!");
256                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
257         }
258
259         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
260         if (validator.isCallback(arguments[2])) {
261                 onSuccessForCbm = arguments[2];
262         }
263         if ((argumentCount > 3) && validator.isCallback(arguments[3])) {
264                 onErrorForCbm = arguments[3];
265         }
266
267         
268         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
269         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
270         Try {
271                 EventTagMifareClassicAuthenticateWithKeyBPtr event(new EventTagMifareClassicAuthenticateWithKeyB(convert.toVectorOfUChars(arguments[0]), convert.toInt(arguments[1])));
272                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
273                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
274                 NFCTagMifareClassic->authenticateWithKeyB(event);
275                 
276                 return JSValueMakeNull(context);
277         } Catch (ConversionException) {
278                 LogError("JSNFCManager::authenticateWithKeyB : ConversionException");
279                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
280         } Catch (InvalidArgumentException) {
281                 LogError("JSNFCManager::authenticateWithKeyB InvalidArgumentException");
282                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
283         } Catch (PlatformException) {
284                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
285                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
286                 return JSValueMakeNull(context);
287         } Catch (WrtDeviceApis::Commons::Exception) {
288                 LogError("Exception: " << _rethrown_exception.GetMessage());
289                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
290                 return JSValueMakeNull(context);
291         }
292
293         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
294         return JSValueMakeNull(context);
295 }
296
297 JSValueRef JSNFCTagMifareClassic::readBlock(JSContextRef context,
298         JSObjectRef object,
299         JSObjectRef thisObject,
300         size_t argumentCount,
301         const JSValueRef arguments[],
302         JSValueRef* exception)
303 {
304         LogDebug("Entered ");
305
306         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
307         if (NULL == privateObject) {
308                 LogError("private object is null");
309                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
310         }
311
312         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
313         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
314
315         NFCConverter convert(context);
316         Validator validator(context, exception);
317
318         if ((argumentCount < 2) || (argumentCount > 3)) {
319                 LogError("JSNFCTagMifareClassic::readBlock argumentCount error");
320                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
321         }
322
323         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) {
324                 LogError("JSNFCTagMifareClassic::readBlock blockIndex TypeMismatchException!");
325                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
326         }
327
328         if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1])) {
329                 /* 1st argument is mandatory. And 1st argument must be Callback. */
330                 LogError("JSNFCTagMifareClassic::readBlock NFCDataReadCB TypeMismatchException!");
331                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
332         }
333         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
334                 /* 2nd argument must be Callback. */
335                 LogError("JSNFCTagMifareClassic::readBlock errorCallback TypeMismatchException!");
336                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
337         }
338         
339         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
340         if (validator.isCallback(arguments[1])) {
341                 onSuccessForCbm = arguments[1];
342         }
343         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
344                 onErrorForCbm = arguments[2];
345         }
346
347         
348         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
349         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
350         Try {
351                 EventTagMifareClassicReadBlockPtr event(new EventTagMifareClassicReadBlock(convert.toInt(arguments[0])));
352                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
353                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
354                 NFCTagMifareClassic->readBlock(event);
355                 
356                 return JSValueMakeNull(context);
357         } Catch (ConversionException) {
358                 LogError("JSNFCManager::readBlock : ConversionException");
359                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
360         } Catch (InvalidArgumentException) {
361                 LogError("JSNFCManager::readBlock InvalidArgumentException");
362                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
363         } Catch (PlatformException) {
364                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
365                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
366                 return JSValueMakeNull(context);
367         } Catch (WrtDeviceApis::Commons::Exception) {
368                 LogError("Exception: " << _rethrown_exception.GetMessage());
369                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
370                 return JSValueMakeNull(context);
371         }
372
373         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
374         return JSValueMakeNull(context);
375 }
376
377 JSValueRef JSNFCTagMifareClassic::writeBlock(JSContextRef context,
378         JSObjectRef object,
379         JSObjectRef thisObject,
380         size_t argumentCount,
381         const JSValueRef arguments[],
382         JSValueRef* exception)
383 {
384         LogDebug("Entered ");
385
386         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
387         if (NULL == privateObject) {
388                 LogError("private object is null");
389                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
390         }
391
392         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
393         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
394
395         NFCConverter convert(context);
396         Validator validator(context, exception);
397
398         if ((argumentCount < 2) || (argumentCount > 4)) {
399                 LogError("JSNFCTagMifareClassic::writeBlock argumentCount error");
400                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
401         }
402
403         if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) 
404                 || (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSIsArrayValue(context, arguments[1]))){
405                 LogError("JSNFCTagMifareClassic::writeBlock blockIndex or block TypeMismatchException!");
406                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
407         }
408
409         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
410                 /* 1st argument is mandatory. And 1st argument must be Callback. */
411                 LogError("JSNFCTagMifareClassic::writeBlock successCallback TypeMismatchException!");
412                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
413         }
414         if ((argumentCount > 3) && !JSValueIsNull(context, arguments[3]) && (JSValueIsUndefined(context, arguments[3]) || !validator.isCallback(arguments[3]))) {
415                 /* 2nd argument must be Callback. */
416                 LogError("JSNFCTagMifareClassic::writeBlock errorCallback TypeMismatchException!");
417                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
418         }
419
420         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
421         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
422                 onSuccessForCbm = arguments[2];
423         }
424         if ((argumentCount > 3) && (validator.isCallback(arguments[3]))) {
425                 onErrorForCbm = arguments[3];
426         }
427
428         
429         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
430         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
431         Try {
432                 EventTagMifareClassicWriteBlockPtr event(new EventTagMifareClassicWriteBlock(convert.toInt(arguments[0]), convert.toVectorOfUChars(arguments[1])));
433                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
434                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
435                 NFCTagMifareClassic->writeBlock(event);
436                 
437                 return JSValueMakeNull(context);
438         } Catch (ConversionException) {
439                 LogError("JSNFCManager::writeBlock : ConversionException");
440                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR,"Invalid Parameter"));
441         } Catch (InvalidArgumentException) {
442                 LogError("JSNFCManager::writeBlock InvalidArgumentException");
443                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
444         } Catch (PlatformException) {
445                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
446                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
447                 return JSValueMakeNull(context);
448         } Catch (WrtDeviceApis::Commons::Exception) {
449                 LogError("Exception: " << _rethrown_exception.GetMessage());
450                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
451                 return JSValueMakeNull(context);
452         }
453
454         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
455         return JSValueMakeNull(context);
456 }
457
458 JSValueRef JSNFCTagMifareClassic::incrementBlockValue(JSContextRef context,
459         JSObjectRef object,
460         JSObjectRef thisObject,
461         size_t argumentCount,
462         const JSValueRef arguments[],
463         JSValueRef* exception)
464 {
465         LogDebug("Entered ");
466
467         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
468         if (NULL == privateObject) {
469                 LogError("private object is null");
470                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
471         }
472   
473         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
474         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
475
476         NFCConverter convert(context);
477         Validator validator(context, exception);
478
479         if ((argumentCount < 2) || (argumentCount > 4)) {
480                 LogError("JSNFCTagMifareClassic::incrementBlockValue argumentCount error");
481                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
482         }
483
484         if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) 
485                 || (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSValueIsNumber(context, arguments[1]))){
486                 LogError("JSNFCTagMifareClassic::incrementBlockValue index or value TypeMismatchException!");
487                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
488         }
489
490         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
491                 /* 1st argument is mandatory. And 1st argument must be Callback. */
492                 LogError("JSNFCTagMifareClassic::incrementBlockValue successCallback TypeMismatchException!");
493                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
494         }
495         if ((argumentCount > 3) && !JSValueIsNull(context, arguments[3]) && (JSValueIsUndefined(context, arguments[3]) || !validator.isCallback(arguments[3]))) {
496                 /* 2nd argument must be Callback. */
497                 LogError("JSNFCTagMifareClassic::incrementBlockValue errorCallbackTypeMismatchException!");
498                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
499         }
500         
501         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
502         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
503                 onSuccessForCbm = arguments[2];
504         }
505         if ((argumentCount > 3) && (validator.isCallback(arguments[3]))) {
506                 onErrorForCbm = arguments[3];
507         }
508
509         
510         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
511         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
512         Try {
513                 EventTagMifareClassicIncrementBlockValuePtr event(new EventTagMifareClassicIncrementBlockValue(convert.toInt(arguments[0]), convert.toInt(arguments[1])));
514                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
515                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
516                 NFCTagMifareClassic->incrementBlockValue(event);
517                 
518                 return JSValueMakeNull(context);
519         } Catch (ConversionException) {
520                 LogError("JSNFCManager::incrementBlockValue : ConversionException");
521                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
522         } Catch (InvalidArgumentException) {
523                 LogError("JSNFCManager::incrementBlockValue InvalidArgumentException");
524                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
525         } Catch (PlatformException) {
526                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
527                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
528                 return JSValueMakeNull(context);
529         } Catch (WrtDeviceApis::Commons::Exception) {
530                 LogError("Exception: " << _rethrown_exception.GetMessage());
531                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
532                 return JSValueMakeNull(context);
533         }
534
535         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
536         return JSValueMakeNull(context);
537 }
538
539 JSValueRef JSNFCTagMifareClassic::decrementBlockValue(JSContextRef context,
540         JSObjectRef object,
541         JSObjectRef thisObject,
542         size_t argumentCount,
543         const JSValueRef arguments[],
544         JSValueRef* exception)
545 {
546         LogDebug("Entered ");
547
548         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
549         if (NULL == privateObject) {
550                 LogError("private object is null");
551                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
552         }
553
554         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
555         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
556
557         NFCConverter convert(context);
558         Validator validator(context, exception);
559
560         if ((argumentCount < 2) || (argumentCount > 4)) {
561                 LogError("JSNFCTagMifareClassic::decrementBlockValue argumentCount error");
562                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
563         }
564
565         if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) 
566                 || (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSValueIsNumber(context, arguments[1]))){
567                 LogError("JSNFCTagMifareClassic::decrementBlockValue index or value TypeMismatchException!");
568                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
569         }
570
571         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
572                 /* 1st argument is mandatory. And 1st argument must be Callback. */
573                 LogError("JSNFCTagMifareClassic::decrementBlockValue successCallback TypeMismatchException!");
574                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
575         }
576         if ((argumentCount > 3) && !JSValueIsNull(context, arguments[3]) && (JSValueIsUndefined(context, arguments[3]) || !validator.isCallback(arguments[3]))) {
577                 /* 2nd argument must be Callback. */
578                 LogError("JSNFCTagMifareClassic::decrementBlockValue errorCallbackTypeMismatchException!");
579                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
580         }
581         
582         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
583         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
584                 onSuccessForCbm = arguments[2];
585         }
586         if ((argumentCount > 3) && (validator.isCallback(arguments[3]))) {
587                 onErrorForCbm = arguments[3];
588         }
589
590         
591         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
592         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
593         Try {
594                 EventTagMifareClassicDecrementBlockValuePtr event(new EventTagMifareClassicDecrementBlockValue(convert.toInt(arguments[0]), convert.toInt(arguments[1])));
595                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
596                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
597                 NFCTagMifareClassic->decrementBlockValue(event);
598                 
599                 return JSValueMakeNull(context);
600         } Catch (ConversionException) {
601                 LogError("JSNFCManager::decrementBlockValue : ConversionException");
602                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
603         } Catch (InvalidArgumentException) {
604                 LogError("JSNFCManager::decrementBlockValue InvalidArgumentException");
605                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
606         } Catch (PlatformException) {
607                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
608                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
609                 return JSValueMakeNull(context);
610         } Catch (WrtDeviceApis::Commons::Exception) {
611                 LogError("Exception: " << _rethrown_exception.GetMessage());
612                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
613                 return JSValueMakeNull(context);
614         }
615
616         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
617         return JSValueMakeNull(context);
618 }
619
620 JSValueRef JSNFCTagMifareClassic::transfer(JSContextRef context,
621         JSObjectRef object,
622         JSObjectRef thisObject,
623         size_t argumentCount,
624         const JSValueRef arguments[],
625         JSValueRef* exception)
626 {
627         LogDebug("Entered ");
628         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
629         if (NULL == privateObject) {
630                 LogError("private object is null");
631                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
632         }
633
634         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
635         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
636
637         NFCConverter convert(context);
638         Validator validator(context, exception);
639
640         if ((argumentCount < 1) || (argumentCount > 3)) {
641                 LogError("JSNFCTagMifareClassic::transfer argumentCount error");
642                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
643         }
644
645         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) {
646                 LogError("JSNFCTagMifareClassic::transfer blockIndex TypeMismatchException!");
647                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
648         }
649
650         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
651                 /* 2nd argument must be Callback. */
652                 LogError("JSNFCTagMifareClassic::transfer successCallback TypeMismatchException!");
653                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
654         }
655         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
656                 /* 2nd argument must be Callback. */
657                 LogError("JSNFCTagMifareClassic::transfer errorCallback TypeMismatchException!");
658                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
659         }
660         
661         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
662         if ((argumentCount > 1) && (validator.isCallback(arguments[1]))) {
663                 onSuccessForCbm = arguments[1];
664         }
665         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
666                 onErrorForCbm = arguments[2];
667         }
668
669         
670         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
671         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
672         Try {
673                 EventTagMifareClassicTransferPtr event(new EventTagMifareClassicTransfer(convert.toInt(arguments[0])));
674                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
675                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
676                 NFCTagMifareClassic->transfer(event);
677                 
678                 return JSValueMakeNull(context);
679         } Catch (ConversionException) {
680                 LogError("JSNFCManager::transfer : ConversionException");
681                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
682         } Catch (InvalidArgumentException) {
683                 LogError("JSNFCManager::transfer InvalidArgumentException");
684                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
685         } Catch (PlatformException) {
686                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
687                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
688                 return JSValueMakeNull(context);
689         } Catch (WrtDeviceApis::Commons::Exception) {
690                 LogError("Exception: " << _rethrown_exception.GetMessage());
691                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
692                 return JSValueMakeNull(context);
693         }
694
695         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
696         return JSValueMakeNull(context);
697 }
698
699 JSValueRef JSNFCTagMifareClassic::restore(JSContextRef context,
700         JSObjectRef object,
701         JSObjectRef thisObject,
702         size_t argumentCount,
703         const JSValueRef arguments[],
704         JSValueRef* exception)
705 {
706         LogDebug("Entered ");
707         NFCTagMifareClassicPrivObject* privateObject = static_cast<NFCTagMifareClassicPrivObject*>(JSObjectGetPrivate(thisObject));
708         if (NULL == privateObject) {
709                 LogError("private object is null");
710                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
711         }
712
713         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
714         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
715
716         NFCConverter convert(context);
717         Validator validator(context, exception);
718
719         if ((argumentCount < 1) || (argumentCount > 3)) {
720                 LogError("JSNFCTagMifareClassic::restore argumentCount error");
721                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
722         }
723
724         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) {
725                 LogError("JSNFCTagMifareClassic::restore blockIndex TypeMismatchException!");
726                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
727         }
728
729         if ((argumentCount > 1) && !JSValueIsNull(context, arguments[1]) && (JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1]))) {
730                 /* 1st argument is mandatory. And 1st argument must be Callback. */
731                 LogError("JSNFCTagMifareClassic::restore successCallback TypeMismatchException!");
732                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
733         }
734         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
735                 /* 2nd argument must be Callback. */
736                 LogError("JSNFCTagMifareClassic::restore errorCallback TypeMismatchException!");
737                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
738         }
739         
740         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
741         if ((argumentCount > 1) && (validator.isCallback(arguments[1]))) {
742                 onSuccessForCbm = arguments[1];
743         }
744         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
745                 onErrorForCbm = arguments[2];
746         }
747
748         
749         INFCTagMifareClassicPtr NFCTagMifareClassic(privateObject->getObject());
750         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
751         Try {
752                 EventTagMifareClassicRestorePtr event(new EventTagMifareClassicRestore(convert.toInt(arguments[0])));
753                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
754                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
755                 NFCTagMifareClassic->restore(event);
756                 
757                 return JSValueMakeNull(context);
758         } Catch (ConversionException) {
759                 LogError("JSNFCManager::restore : ConversionException");
760                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
761         } Catch (InvalidArgumentException) {
762                 LogError("JSNFCManager::restore InvalidArgumentException");
763                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Invalid Values");
764         } Catch (PlatformException) {
765                 LogError("PlatformException: " << _rethrown_exception.GetMessage());
766                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
767                 return JSValueMakeNull(context);
768         } Catch (WrtDeviceApis::Commons::Exception) {
769                 LogError("Exception: " << _rethrown_exception.GetMessage());
770                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
771                 return JSValueMakeNull(context);
772         }
773
774         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
775         return JSValueMakeNull(context);
776 }
777
778 }
779 }