fixed compile errors
[platform/framework/web/wrt-plugins-tizen.git] / src / NFC / NFCTag.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <Commons/Exception.h>
19 #include <JSWebAPIErrorFactory.h>
20
21 #include "NFCTag.h"
22 #include "NFCUtil.h"
23 #include <Logger.h>
24
25 namespace DeviceAPI {
26 namespace NFC {
27
28 namespace
29 {
30         /**
31         * Callback method called by platform as interval time.
32         * @param event_type Platform magnetic evet type
33         * @param event Platform sensor event data
34         * @param this_ User data pointer.
35         */
36         static bool TagInformationCallback(const char *key, const unsigned char *value, int value_size, void *user_data) {
37                 LoggerD("Callback TagInformationCallback.");
38                 if (user_data) {
39                         NFCUtil util;
40                         std::vector<unsigned char> tagInfo = util.toVector(value, value_size);
41                         (static_cast<NFCTag*>(user_data))->makeTagProperties(key, tagInfo);
42                         return TRUE;
43                 }
44                 LoggerD("Error! TagInformationCallback is Null.");
45                 return false;
46         }
47
48         static void TagReadCallback(nfc_error_e result , nfc_ndef_message_h message , void * data) {
49                 LoggerD("Callback TagReadCallback.");
50                 if (data) {
51                         NFCTag *tagPtr = (NFCTag *)((EventNFCPtrs<EventTagActionRead> *)data)->getThisPtr();
52                         if (tagPtr) {
53                                 EventTagActionReadPtr event = ((EventNFCPtrs<EventTagActionRead> *)data)->getEventPtrs();
54                                 LoggerD("message handler :" << message);
55
56                                 unsigned char *rawdata = NULL;
57                                 unsigned int size;
58                                 if (nfc_ndef_message_get_rawdata(message, &rawdata, &size) != NFC_ERROR_NONE) {
59                                         LoggerE("Can't get record's rawdata");
60                                         if (rawdata)
61                                                 free(rawdata);
62                                         return;
63                                 }
64
65                                 NFCUtil util;
66                                 std::vector<unsigned char> readData = util.toVector(rawdata, size);
67                                 if (rawdata)
68                                         free(rawdata);
69                                 tagPtr->readNdefManualAnswer((int)result, readData, event);
70                         }
71                 
72                 } else {
73                         LoggerD("Error! TagReadCallback is Null.");
74                 }
75         }
76
77         static void TagWriteCallback(nfc_error_e result, void *data) {
78                 LoggerD("Callback TagWriteCallback.");
79                 if (data) {
80                         ((NFCTag *)data)->writeNdefManualAnswer((int)result);
81                 } else {
82                         LoggerD("Error! TagWriteCallback is Null.");
83                 }               
84         }
85
86         static void TagTransceiveCallback(nfc_error_e result, unsigned char *buffer, int buffer_size,  void *data) {
87                 LoggerD("Callback TagTransceiveCallback.");
88                 if (data) {
89                         NFCUtil util;
90                         std::vector<unsigned char> responseData = util.toVector(buffer, buffer_size)    ;
91                         ((NFCTag *)data)->transceiveManualAnswer((int)result, responseData);
92                 } else {
93                         LoggerD("Error! TagTransceiveCallback is Null.");
94                 }               
95         }
96
97         static void TagFormatCallback(nfc_error_e result, void *data) {
98                 LoggerD("Callback TagFormatCallback.");
99                 if (data) {
100                         ((NFCTag *)data)->formatManualAnswer((int)result);              
101                 } else {
102                         LoggerD("Error! TagFormatCallback is Null.");
103                 }               
104         }
105 }//private namespace
106
107 NFCTag::NFCTag(void *tagHandle)
108 {
109         LoggerD("entered");
110
111         handle = (nfc_tag_h)tagHandle;
112
113         m_EventTagActionWritePtr.Reset();
114         m_EventTagActionTransceivePtr.Reset();
115         m_EventTagActionFormatPtr.Reset();
116 }
117
118 NFCTag::~NFCTag()
119 {
120         LoggerD("entered");
121         handle = NULL;
122 }
123
124 bool NFCTag::isConnected() {
125         nfc_tag_h curHandle = NULL;
126         if (nfc_manager_get_connected_tag(&curHandle) == NFC_ERROR_NONE) {
127                 if (curHandle == handle)
128                         return true;
129         }
130         return false;
131 }
132
133 nfcTagType NFCTag::getTagType() {
134         nfc_tag_type_e type = NFC_UNKNOWN_TARGET;
135         int result = nfc_tag_get_type(handle, &type);
136
137         NFCUtil util;
138         if (result != NFC_ERROR_NONE)
139                 util.throwNFCException(result, "Can't get tag Type");
140
141         return util.convertTonfcTagType(type);
142 }
143
144 bool NFCTag::isNDEFSupport() {
145         bool isNDEF = FALSE;
146         int result = nfc_tag_is_support_ndef(handle, &isNDEF);
147
148         NFCUtil util;
149         if (result != NFC_ERROR_NONE)
150                 util.throwNFCException(result, "Can't get if Ndef is supported");
151
152         return isNDEF;
153 }
154 long NFCTag::getNdefSize(){
155         unsigned int ndefSize = 0;
156         int result = nfc_tag_get_ndef_size(handle, &ndefSize);
157
158         NFCUtil util;
159         if (result != NFC_ERROR_NONE)
160                 util.throwNFCException(result, "Can't get Ndef size");
161
162         return static_cast<long>(ndefSize);
163 }
164 std::vector<NFCTagProperties> NFCTag::getProperties() {
165         LoggerD("Enter");
166         props.clear();
167         int result = nfc_tag_foreach_information(handle, TagInformationCallback, this);
168
169         NFCUtil util;
170         if (result != NFC_ERROR_NONE)
171                 util.throwNFCException(result, "Can't get Ndef Properties");
172
173         return props;
174 }
175
176 void NFCTag::makeTagProperties(const char *key, std::vector<unsigned char> &value) {
177         NFCTagProperties newProp;
178         newProp.key = key;
179         newProp.value = value;
180         props.push_back(newProp);
181 }
182
183 void NFCTag::readNdef(const EventTagActionReadPtr& event) {
184         LoggerD("Enter");
185         if (!isNDEFSupport())
186                 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Not Support NDEF");
187
188         EventRequestReceiver<EventTagActionRead>::PostRequest(event);
189 }
190
191 void NFCTag::writeNdef(const EventTagActionWritePtr& event) {
192         LoggerD("Enter");
193         if (!isNDEFSupport())
194                 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Not Support NDEF");
195
196         if (m_EventTagActionWritePtr.Get() != NULL)
197                 EventTagActionWrites.push_back(event);
198         else
199                 EventRequestReceiver<EventTagActionWrite>::PostRequest(event);
200
201         return;
202 }
203
204 void NFCTag::transceive(const EventTagActionTransceivePtr& event) {
205         LoggerD("Enter");
206
207         if (m_EventTagActionTransceivePtr.Get() != NULL)
208                 EventTagActionTransceives.push_back(event);
209         else
210                 EventRequestReceiver<EventTagActionTransceive>::PostRequest(event);
211 }
212
213 void NFCTag::format(const EventTagActionFormatPtr& event) {
214         LoggerD("Enter");
215         EventRequestReceiver<EventTagActionFormat>::PostRequest(event);
216 }
217
218 void NFCTag::readNdefManualAnswer(int result, std::vector<unsigned char> &data, const EventTagActionReadPtr &event)
219 {
220         LoggerD("Enter");
221         if ((nfc_error_e)result == NFC_ERROR_NONE) {
222                 event->setResult(TRUE);
223                 event->setReadNdefResult(data);
224         } else {
225                 NFCUtil util;
226                 event->setResult(FALSE);
227                 event->setError(util.getNFCErrorString(result));
228                 event->setErrorMessage(util.getNFCErrorMessage(result));
229         }
230         EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
231
232         std::vector<TagReadDataPtr>::iterator it;
233         for (it = EventTagActionsReads.begin(); it != EventTagActionsReads.end(); ++it) {
234                 if ((*it)->getEventPtrs() == event) {
235                         EventTagActionsReads.erase(it);
236                         LoggerD("event is removed. (" << EventTagActionsReads.size() << ")");
237                         break;
238                 }
239         }
240 }
241
242 void NFCTag::writeNdefManualAnswer(int result)
243 {
244         LoggerD("Enter");
245         if (m_EventTagActionWritePtr.Get() != NULL) {
246                 if ((nfc_error_e)result == NFC_ERROR_NONE) {
247                         m_EventTagActionWritePtr->setResult(TRUE);
248                 } else {
249                         NFCUtil util;
250                         m_EventTagActionWritePtr->setResult(FALSE);
251                         m_EventTagActionWritePtr->setError(util.getNFCErrorString(result));
252                         m_EventTagActionWritePtr->setErrorMessage(util.getNFCErrorMessage(result));
253                 }
254                 EventRequestReceiver<EventTagActionWrite>::ManualAnswer(m_EventTagActionWritePtr);
255
256                 m_EventTagActionWritePtr.Reset();
257                 if (EventTagActionWrites.size() > 0) {
258                         EventTagActionWritePtr event = EventTagActionWrites.front();
259                         EventTagActionWrites.erase(EventTagActionWrites.begin());
260                         LoggerD("EventTagActionWrites is removed. (" << EventTagActionWrites.size() << ")");
261                         EventRequestReceiver<EventTagActionWrite>::PostRequest(event);
262                 }
263         }
264 }
265
266 void NFCTag::transceiveManualAnswer(int result , std::vector<unsigned char> &data)
267 {
268         LoggerD("Enter");
269         if (m_EventTagActionTransceivePtr.Get() != NULL) {
270                 if ((nfc_error_e)result == NFC_ERROR_NONE) {
271                         m_EventTagActionTransceivePtr->setResult(TRUE);
272                         m_EventTagActionTransceivePtr->setTransceiveBuffer(data);
273                 } else {
274                         NFCUtil util;
275                         m_EventTagActionTransceivePtr->setResult(FALSE);
276                         m_EventTagActionTransceivePtr->setError(util.getNFCErrorString(result));
277                         m_EventTagActionTransceivePtr->setErrorMessage(util.getNFCErrorMessage(result));
278                 }
279                 EventRequestReceiver<EventTagActionTransceive>::ManualAnswer(m_EventTagActionTransceivePtr);
280
281                 m_EventTagActionTransceivePtr.Reset();
282                 if (EventTagActionTransceives.size() > 0) {
283                         EventTagActionTransceivePtr event = EventTagActionTransceives.front();
284                         EventTagActionTransceives.erase(EventTagActionTransceives.begin());
285                         LoggerD("EventTagActionWrites is removed. (" << EventTagActionTransceives.size() << ")");
286                         EventRequestReceiver<EventTagActionTransceive>::PostRequest(event);
287                 }
288         }
289 }
290
291 void NFCTag::formatManualAnswer(int result)
292 {
293         LoggerD("Enter");
294         if (m_EventTagActionFormatPtr.Get() != NULL) {
295                 if ((nfc_error_e)result == NFC_ERROR_NONE) {
296                         m_EventTagActionFormatPtr->setResult(TRUE);
297                 } else {
298                         NFCUtil util;
299                         m_EventTagActionFormatPtr->setResult(FALSE);
300                         m_EventTagActionFormatPtr->setError(util.getNFCErrorString(result));
301                         m_EventTagActionFormatPtr->setErrorMessage(util.getNFCErrorMessage(result));
302                 }
303                 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(m_EventTagActionFormatPtr);
304                 m_EventTagActionFormatPtr.Reset();
305         }
306 }
307
308 void NFCTag::OnRequestReceived(const EventTagActionReadPtr& event) {
309         LoggerD("Enter");
310         
311         try {
312                 event->switchToManualAnswer();
313
314                 TagReadDataPtr data( new EventNFCPtrs<EventTagActionRead>(event, this));
315                 EventTagActionsReads.push_back(data);
316                 int result = nfc_tag_read_ndef(handle, TagReadCallback, data.Get());
317
318                 NFCUtil util;
319                 std::string error = util.getNFCErrorString(result);
320                 if (error != "") {
321                         event->setResult(false);
322                         event->setError(error);
323                         event->setErrorMessage(util.getNFCErrorMessage(result));
324                         EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
325
326                         std::vector<TagReadDataPtr>::iterator it;
327                         for (it = EventTagActionsReads.begin(); it != EventTagActionsReads.end(); ++it) {
328                                 if ((*it)->getEventPtrs() == event) {
329                                         EventTagActionsReads.erase(it);
330                                         LoggerD("event is removed. (" << EventTagActionsReads.size() << ")");
331                                         break;
332                                 }
333                         }
334                 }
335         }
336         catch (const WrtDeviceApis::Commons::Exception& ex) {
337                 LoggerE("Exception: " << ex.GetMessage());
338                 event->setResult(false);
339         
340                 EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
341         }
342         
343 }
344
345 void NFCTag::OnRequestReceived(const EventTagActionWritePtr& event) {
346         try {
347                 event->switchToManualAnswer();
348
349                 m_EventTagActionWritePtr = event;
350
351                 int result = nfc_tag_write_ndef(handle, (nfc_ndef_message_h)event->getNdefForWriting(),TagWriteCallback, this);
352
353                 NFCUtil util;
354                 std::string error = util.getNFCErrorString(result);
355                 if (error != "") {
356                         writeNdefManualAnswer(result);
357                 }
358         }
359         catch (const WrtDeviceApis::Commons::Exception& ex) {
360                 LoggerE("Exception: " << ex.GetMessage());
361
362                  if (event != m_EventTagActionWritePtr)
363                         m_EventTagActionWritePtr = event;
364                 writeNdefManualAnswer(NFC_ERROR_OPERATION_FAILED);
365         }
366 }
367
368 void NFCTag::OnRequestReceived(const EventTagActionTransceivePtr& event) {
369         NFCUtil util;
370         try {
371                 event->switchToManualAnswer();
372                 
373                 m_EventTagActionTransceivePtr = event;
374
375                 unsigned char *buffer = util.toCharPtr(event->getTransceiveBuffer());
376                 int result = nfc_tag_transceive(handle, buffer, event->getTransceiveBufferSize(), TagTransceiveCallback, this);
377
378                 if (buffer)
379                         free(buffer);
380
381                 std::string error = util.getNFCErrorString(result);
382                 if (error != "") {
383                         std::vector<unsigned char> emptyData;
384                         transceiveManualAnswer(result, emptyData);
385                 }
386         }
387         catch (const WrtDeviceApis::Commons::Exception& ex) {
388                 LoggerE("Exception: " << ex.GetMessage());
389
390                 std::vector<unsigned char> emptyData;
391                 transceiveManualAnswer(NFC_ERROR_OPERATION_FAILED, emptyData);
392         }
393         
394 }
395
396 void NFCTag::OnRequestReceived(const EventTagActionFormatPtr& event) {
397         
398         try {
399                 event->switchToManualAnswer();
400
401                 if (m_EventTagActionFormatPtr.Get() != NULL) {
402                         event->setResult(false);
403                         event->setError(DeviceAPI::Common::JSWebAPIErrorFactory::SERVICE_NOT_AVAILABLE_ERROR);
404                         event->setErrorMessage("Progressing Identical Operation");
405                         EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
406                         return;
407                 }
408                 m_EventTagActionFormatPtr = event;
409                 
410                 NFCUtil util;
411                 LoggerD("key size :" <<  event->getKeySize());
412
413                 unsigned char *key = util.toCharPtr(event->getKey());
414                 int result = nfc_tag_format_ndef(handle, key, event->getKeySize(), TagFormatCallback, this);
415
416                 if (key)
417                         free(key);
418
419                 std::string error = util.getNFCErrorString(result);
420                 if (error != "") {
421                         event->setResult(false);
422                         event->setError(error);
423                         event->setErrorMessage(util.getNFCErrorMessage(result));
424                         EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
425                         m_EventTagActionFormatPtr.Reset();
426                 }
427         }
428         catch (const WrtDeviceApis::Commons::Exception& ex) {
429                 LoggerE("Exception: " << ex.GetMessage());
430                 event->setResult(false);
431                 
432                 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
433                 m_EventTagActionFormatPtr.Reset();
434         }
435         
436 }
437
438
439 }
440 }