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 <Commons/Exception.h>
19 #include <JSWebAPIErrorFactory.h>
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.
36 static bool TagInformationCallback(const char *key, const unsigned char *value, int value_size, void *user_data) {
37 LoggerD("Callback TagInformationCallback.");
40 std::vector<unsigned char> tagInfo = util.toVector(value, value_size);
41 (static_cast<NFCTag*>(user_data))->makeTagProperties(key, tagInfo);
44 LoggerD("Error! TagInformationCallback is Null.");
48 static void TagReadCallback(nfc_error_e result , nfc_ndef_message_h message , void * data) {
49 LoggerD("Callback TagReadCallback.");
51 NFCTag *tagPtr = (NFCTag *)((EventNFCPtrs<EventTagActionRead> *)data)->getThisPtr();
53 EventTagActionReadPtr event = ((EventNFCPtrs<EventTagActionRead> *)data)->getEventPtrs();
54 LoggerD("message handler :" << message);
56 unsigned char *rawdata = NULL;
58 if (nfc_ndef_message_get_rawdata(message, &rawdata, &size) != NFC_ERROR_NONE) {
59 LoggerE("Can't get record's rawdata");
66 std::vector<unsigned char> readData = util.toVector(rawdata, size);
69 tagPtr->readNdefManualAnswer((int)result, readData, event);
73 LoggerD("Error! TagReadCallback is Null.");
77 static void TagWriteCallback(nfc_error_e result, void *data) {
78 LoggerD("Callback TagWriteCallback.");
80 ((NFCTag *)data)->writeNdefManualAnswer((int)result);
82 LoggerD("Error! TagWriteCallback is Null.");
86 static void TagTransceiveCallback(nfc_error_e result, unsigned char *buffer, int buffer_size, void *data) {
87 LoggerD("Callback TagTransceiveCallback.");
90 std::vector<unsigned char> responseData = util.toVector(buffer, buffer_size) ;
91 ((NFCTag *)data)->transceiveManualAnswer((int)result, responseData);
93 LoggerD("Error! TagTransceiveCallback is Null.");
97 static void TagFormatCallback(nfc_error_e result, void *data) {
98 LoggerD("Callback TagFormatCallback.");
100 ((NFCTag *)data)->formatManualAnswer((int)result);
102 LoggerD("Error! TagFormatCallback is Null.");
107 NFCTag::NFCTag(void *tagHandle)
111 handle = (nfc_tag_h)tagHandle;
113 m_EventTagActionWritePtr.Reset();
114 m_EventTagActionTransceivePtr.Reset();
115 m_EventTagActionFormatPtr.Reset();
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)
133 nfcTagType NFCTag::getTagType() {
134 nfc_tag_type_e type = NFC_UNKNOWN_TARGET;
135 int result = nfc_tag_get_type(handle, &type);
138 if (result != NFC_ERROR_NONE)
139 util.throwNFCException(result, "Can't get tag Type");
141 return util.convertTonfcTagType(type);
144 bool NFCTag::isNDEFSupport() {
146 int result = nfc_tag_is_support_ndef(handle, &isNDEF);
149 if (result != NFC_ERROR_NONE)
150 util.throwNFCException(result, "Can't get if Ndef is supported");
154 long NFCTag::getNdefSize(){
155 unsigned int ndefSize = 0;
156 int result = nfc_tag_get_ndef_size(handle, &ndefSize);
159 if (result != NFC_ERROR_NONE)
160 util.throwNFCException(result, "Can't get Ndef size");
162 return static_cast<long>(ndefSize);
164 std::vector<NFCTagProperties> NFCTag::getProperties() {
167 int result = nfc_tag_foreach_information(handle, TagInformationCallback, this);
170 if (result != NFC_ERROR_NONE)
171 util.throwNFCException(result, "Can't get Ndef Properties");
176 void NFCTag::makeTagProperties(const char *key, std::vector<unsigned char> &value) {
177 NFCTagProperties newProp;
179 newProp.value = value;
180 props.push_back(newProp);
183 void NFCTag::readNdef(const EventTagActionReadPtr& event) {
185 if (!isNDEFSupport())
186 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Not Support NDEF");
188 EventRequestReceiver<EventTagActionRead>::PostRequest(event);
191 void NFCTag::writeNdef(const EventTagActionWritePtr& event) {
193 if (!isNDEFSupport())
194 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Not Support NDEF");
196 if (m_EventTagActionWritePtr.Get() != NULL)
197 EventTagActionWrites.push_back(event);
199 EventRequestReceiver<EventTagActionWrite>::PostRequest(event);
204 void NFCTag::transceive(const EventTagActionTransceivePtr& event) {
207 if (m_EventTagActionTransceivePtr.Get() != NULL)
208 EventTagActionTransceives.push_back(event);
210 EventRequestReceiver<EventTagActionTransceive>::PostRequest(event);
213 void NFCTag::format(const EventTagActionFormatPtr& event) {
215 EventRequestReceiver<EventTagActionFormat>::PostRequest(event);
218 void NFCTag::readNdefManualAnswer(int result, std::vector<unsigned char> &data, const EventTagActionReadPtr &event)
221 if ((nfc_error_e)result == NFC_ERROR_NONE) {
222 event->setResult(TRUE);
223 event->setReadNdefResult(data);
226 event->setResult(FALSE);
227 event->setError(util.getNFCErrorString(result));
228 event->setErrorMessage(util.getNFCErrorMessage(result));
230 EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
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() << ")");
242 void NFCTag::writeNdefManualAnswer(int result)
245 if (m_EventTagActionWritePtr.Get() != NULL) {
246 if ((nfc_error_e)result == NFC_ERROR_NONE) {
247 m_EventTagActionWritePtr->setResult(TRUE);
250 m_EventTagActionWritePtr->setResult(FALSE);
251 m_EventTagActionWritePtr->setError(util.getNFCErrorString(result));
252 m_EventTagActionWritePtr->setErrorMessage(util.getNFCErrorMessage(result));
254 EventRequestReceiver<EventTagActionWrite>::ManualAnswer(m_EventTagActionWritePtr);
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);
266 void NFCTag::transceiveManualAnswer(int result , std::vector<unsigned char> &data)
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);
275 m_EventTagActionTransceivePtr->setResult(FALSE);
276 m_EventTagActionTransceivePtr->setError(util.getNFCErrorString(result));
277 m_EventTagActionTransceivePtr->setErrorMessage(util.getNFCErrorMessage(result));
279 EventRequestReceiver<EventTagActionTransceive>::ManualAnswer(m_EventTagActionTransceivePtr);
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);
291 void NFCTag::formatManualAnswer(int result)
294 if (m_EventTagActionFormatPtr.Get() != NULL) {
295 if ((nfc_error_e)result == NFC_ERROR_NONE) {
296 m_EventTagActionFormatPtr->setResult(TRUE);
299 m_EventTagActionFormatPtr->setResult(FALSE);
300 m_EventTagActionFormatPtr->setError(util.getNFCErrorString(result));
301 m_EventTagActionFormatPtr->setErrorMessage(util.getNFCErrorMessage(result));
303 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(m_EventTagActionFormatPtr);
304 m_EventTagActionFormatPtr.Reset();
308 void NFCTag::OnRequestReceived(const EventTagActionReadPtr& event) {
312 event->switchToManualAnswer();
314 TagReadDataPtr data( new EventNFCPtrs<EventTagActionRead>(event, this));
315 EventTagActionsReads.push_back(data);
316 int result = nfc_tag_read_ndef(handle, TagReadCallback, data.Get());
319 std::string error = util.getNFCErrorString(result);
321 event->setResult(false);
322 event->setError(error);
323 event->setErrorMessage(util.getNFCErrorMessage(result));
324 EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
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() << ")");
336 catch (const WrtDeviceApis::Commons::Exception& ex) {
337 LoggerE("Exception: " << ex.GetMessage());
338 event->setResult(false);
340 EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
345 void NFCTag::OnRequestReceived(const EventTagActionWritePtr& event) {
347 event->switchToManualAnswer();
349 m_EventTagActionWritePtr = event;
351 int result = nfc_tag_write_ndef(handle, (nfc_ndef_message_h)event->getNdefForWriting(),TagWriteCallback, this);
354 std::string error = util.getNFCErrorString(result);
356 writeNdefManualAnswer(result);
359 catch (const WrtDeviceApis::Commons::Exception& ex) {
360 LoggerE("Exception: " << ex.GetMessage());
362 if (event != m_EventTagActionWritePtr)
363 m_EventTagActionWritePtr = event;
364 writeNdefManualAnswer(NFC_ERROR_OPERATION_FAILED);
368 void NFCTag::OnRequestReceived(const EventTagActionTransceivePtr& event) {
371 event->switchToManualAnswer();
373 m_EventTagActionTransceivePtr = event;
375 unsigned char *buffer = util.toCharPtr(event->getTransceiveBuffer());
376 int result = nfc_tag_transceive(handle, buffer, event->getTransceiveBufferSize(), TagTransceiveCallback, this);
381 std::string error = util.getNFCErrorString(result);
383 std::vector<unsigned char> emptyData;
384 transceiveManualAnswer(result, emptyData);
387 catch (const WrtDeviceApis::Commons::Exception& ex) {
388 LoggerE("Exception: " << ex.GetMessage());
390 std::vector<unsigned char> emptyData;
391 transceiveManualAnswer(NFC_ERROR_OPERATION_FAILED, emptyData);
396 void NFCTag::OnRequestReceived(const EventTagActionFormatPtr& event) {
399 event->switchToManualAnswer();
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);
408 m_EventTagActionFormatPtr = event;
411 LoggerD("key size :" << event->getKeySize());
413 unsigned char *key = util.toCharPtr(event->getKey());
414 int result = nfc_tag_format_ndef(handle, key, event->getKeySize(), TagFormatCallback, this);
419 std::string error = util.getNFCErrorString(result);
421 event->setResult(false);
422 event->setError(error);
423 event->setErrorMessage(util.getNFCErrorMessage(result));
424 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
425 m_EventTagActionFormatPtr.Reset();
428 catch (const WrtDeviceApis::Commons::Exception& ex) {
429 LoggerE("Exception: " << ex.GetMessage());
430 event->setResult(false);
432 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
433 m_EventTagActionFormatPtr.Reset();