103e99491942722b6588b3d6ed1626b682c88f1a
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / NFC / JSNFCTagMifareUltra.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 "JSNFCTagMifareUltra.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/EventTagMifareUltraAction.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_NFCTTAGMIFAREULTRA_ATTRIBUTENAME "NFCTagMifareUltra"
46
47 namespace TizenApis {
48 namespace Tizen1_0 {
49
50  JSClassDefinition JSNFCTagMifareUltra::m_classInfo =
51 {
52     0,
53     kJSClassAttributeNone,
54     TIZEN10_NFCTTAGMIFAREULTRA_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     NULL, //HasInstance,
68     NULL  //ConvertToType
69 };
70
71
72 JSStaticFunction JSNFCTagMifareUltra::m_function[] = {
73     {"readPage", JSNFCTagMifareUltra::readPage, kJSPropertyAttributeNone},
74     {"writePage", JSNFCTagMifareUltra::writePage, kJSPropertyAttributeNone},
75     { 0, 0, 0}
76 };
77
78 JSClassRef JSNFCTagMifareUltra::m_jsClassRef = JSClassCreate(JSNFCTagMifareUltra::getClassInfo());
79
80 JSObjectRef JSNFCTagMifareUltra::createJSObject(JSContextRef context, void *tagHandle, void* privManager) {
81         LogDebug("entered");
82
83         INFCTagMifareUltraPtr NFCTagMifareUltra = NFCFactory::getInstance().createNFCTagMifareUltraObject(tagHandle);
84         NFCTagMifareUltra->setPrivateNFCManagerPtr(privManager);
85         
86         NFCTagMifareUltraPrivObject *priv = new NFCTagMifareUltraPrivObject(context, NFCTagMifareUltra);
87
88         if (!priv) {
89                 ThrowMsg(NullPointerException, "Can not new a NFCTagMifareUltra object");
90         }
91         
92         return JSObjectMake(context, getClassRef(), priv);
93 }
94
95 void JSNFCTagMifareUltra::initialize(JSContextRef context, JSObjectRef object)
96 {
97 }
98
99 void JSNFCTagMifareUltra::finalize(JSObjectRef object)
100 {
101         LogDebug( "entered" );
102         NFCTagMifareUltraPrivObject *priv = static_cast<NFCTagMifareUltraPrivObject*>( JSObjectGetPrivate( object ) ) ;
103         JSObjectSetPrivate(object, NULL);
104         LogDebug("Deleting timezone object");
105         delete priv;
106 }
107
108
109 const JSClassRef JSNFCTagMifareUltra::getClassRef()
110 {
111         if (!m_jsClassRef) {
112                 m_jsClassRef = JSClassCreate(&m_classInfo);
113         }
114         return m_jsClassRef;
115 }
116
117 const JSClassDefinition* JSNFCTagMifareUltra::getClassInfo()
118 {
119         return &m_classInfo;
120 }
121
122 JSValueRef JSNFCTagMifareUltra::readPage(JSContextRef context,
123         JSObjectRef object,
124         JSObjectRef thisObject,
125         size_t argumentCount,
126         const JSValueRef arguments[],
127         JSValueRef* exception)
128 {
129         LogDebug("Entered ");
130
131         NFCTagMifareUltraPrivObject* privateObject = static_cast<NFCTagMifareUltraPrivObject*>(JSObjectGetPrivate(thisObject));
132         if (NULL == privateObject) {
133                 LogError("private object is null");
134                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
135         }
136
137         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
138         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
139
140         NFCConverter convert(context);
141         Validator validator(context, exception);
142
143         if (argumentCount < 2) {
144                 LogError("JSNFCTagMifareUltra::readPage TypeMismatchException");
145                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
146         }
147
148         if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) {
149                 LogError("JSNFCTagMifareUltra::readPage TypeMismatchException!");
150                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
151         }
152
153         if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !validator.isCallback(arguments[1])) {
154                 /* 1st argument is mandatory. And 1st argument must be Callback. */
155                 LogError("JSNFCTagMifareUltra::readPage TypeMismatchException!");
156                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
157         }
158         if ((argumentCount > 2) && !JSValueIsNull(context, arguments[2]) && (JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2]))) {
159                 /* 2nd argument must be Callback. */
160                 LogError("JSNFCTagMifareUltra::readPage TypeMismatchException!");
161                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
162         }
163         
164         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
165         if (validator.isCallback(arguments[1])) {
166                 onSuccessForCbm = arguments[1];
167         }
168         if ((argumentCount > 2) && (validator.isCallback(arguments[2]))) {
169                 onErrorForCbm = arguments[2];
170         }
171
172         
173         INFCTagMifareUltraPtr NFCTagMifareUltra(privateObject->getObject());
174         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
175         Try {
176                 EventTagMifareUltraReadPagePtr event(new EventTagMifareUltraReadPage(convert.toInt(arguments[0])));
177                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
178                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
179                 NFCTagMifareUltra->readPage(event);
180                 
181                 return JSValueMakeNull(context);;
182         } Catch (ConversionException) {
183                 LogError("JSNFCManager::readPage : ConversionException");
184                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"Invalid Parameter"));
185         } Catch (InvalidArgumentException) {
186                 LogError("JSNFCManager::readPage InvalidArgumentException");
187                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"Invalid Parameter"));
188         } Catch (WrtDeviceApis::Commons::Exception) {
189                 LogError("Exception: " << _rethrown_exception.GetMessage());
190                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
191         }
192
193         return JSValueMakeNull(context);
194 }
195
196 JSValueRef JSNFCTagMifareUltra::writePage(JSContextRef context,
197         JSObjectRef object,
198         JSObjectRef thisObject,
199         size_t argumentCount,
200         const JSValueRef arguments[],
201         JSValueRef* exception)
202 {
203         LogDebug("Entered ");
204
205         NFCTagMifareUltraPrivObject* privateObject = static_cast<NFCTagMifareUltraPrivObject*>(JSObjectGetPrivate(thisObject));
206         if (NULL == privateObject) {
207                 LogError("private object is null");
208                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
209         }
210
211         AceSecurityStatus status = NFC_CHECK_ACCESS(privateObject->getContext(),NFC_FUNCTION_API_TAG_FUNCS);
212         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
213
214         NFCConverter convert(context);
215         Validator validator(context, exception);
216
217         if (argumentCount < 3) {
218                 LogError("JSNFCTagMifareUltra::writePage TypeMismatchException");
219                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
220         }
221
222         if ((JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]) || !JSValueIsNumber(context, arguments[0])) 
223                 || (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1]) || !JSIsArrayValue(context, arguments[1]))){
224                 LogError("JSNFCTagMifareUltra::writePage TypeMismatchException!");
225                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
226         }
227
228         if (JSValueIsNull(context, arguments[2]) || JSValueIsUndefined(context, arguments[2]) || !validator.isCallback(arguments[2])) {
229                 /* 1st argument is mandatory. And 1st argument must be Callback. */
230                 LogError("JSNFCTagMifareUltra::writePage TypeMismatchException!");
231                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
232         }
233         if ((argumentCount > 3) && !JSValueIsNull(context, arguments[3]) && (JSValueIsUndefined(context, arguments[3]) || !validator.isCallback(arguments[3]))) {
234                 /* 2nd argument must be Callback. */
235                 LogError("JSNFCTagMifareUltra::writePage TypeMismatchException!");
236                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
237         }
238         
239         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
240         if (validator.isCallback(arguments[2])) {
241                 onSuccessForCbm = arguments[2];
242         }
243         if ((argumentCount > 3) && (validator.isCallback(arguments[3]))) {
244                 onErrorForCbm = arguments[3];
245         }
246
247         
248         INFCTagMifareUltraPtr NFCTagMifareUltra(privateObject->getObject());
249         JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(privateObject->getContext(), onSuccessForCbm, onErrorForCbm, true, true);
250         Try {
251                 EventTagMifareUltraWritePagePtr event(new EventTagMifareUltraWritePage(convert.toInt(arguments[0]), convert.toVectorOfUChars(arguments[1])));
252                 event->setPrivateData( StaticPointerCast<IEventPrivateData>(callbackManager) );
253                 event->setForAsynchronousCall(&NFCResponseDispatcher::getInstance());
254                 NFCTagMifareUltra->writePage(event);
255                 
256                 return JSValueMakeNull(context);
257         } Catch (ConversionException) {
258                 LogError("JSNFCManager::writePage : ConversionException");
259                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"Invalid Parameter"));
260         } Catch (InvalidArgumentException) {
261                 LogError("JSNFCManager::writePage InvalidArgumentException");
262                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR,"Invalid Parameter"));
263         } Catch (WrtDeviceApis::Commons::Exception) {
264                 LogError("Exception: " << _rethrown_exception.GetMessage());
265                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR,"Unknown Error"));
266         }
267
268         return JSValueMakeNull(context);
269 }
270
271 }
272 }