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 <JSTizenException.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;
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_ndef_message_destroy(static_cast<nfc_ndef_message_h>(m_EventTagActionWritePtr->getNdefForWriting())) != NFC_ERROR_NONE)
247 LoggerE("Can't destroy NdefMessage");
249 if ((nfc_error_e)result == NFC_ERROR_NONE) {
250 m_EventTagActionWritePtr->setResult(TRUE);
253 m_EventTagActionWritePtr->setResult(FALSE);
254 m_EventTagActionWritePtr->setError(util.getNFCErrorString(result));
255 m_EventTagActionWritePtr->setErrorMessage(util.getNFCErrorMessage(result));
257 EventRequestReceiver<EventTagActionWrite>::ManualAnswer(m_EventTagActionWritePtr);
259 m_EventTagActionWritePtr.Reset();
260 if (EventTagActionWrites.size() > 0) {
261 EventTagActionWritePtr event = EventTagActionWrites.front();
262 EventTagActionWrites.erase(EventTagActionWrites.begin());
263 LoggerD("EventTagActionWrites is removed. (" << EventTagActionWrites.size() << ")");
264 EventRequestReceiver<EventTagActionWrite>::PostRequest(event);
269 void NFCTag::transceiveManualAnswer(int result , std::vector<unsigned char> &data)
272 if (m_EventTagActionTransceivePtr.Get() != NULL) {
273 if ((nfc_error_e)result == NFC_ERROR_NONE) {
274 m_EventTagActionTransceivePtr->setResult(TRUE);
275 m_EventTagActionTransceivePtr->setTransceiveBuffer(data);
278 m_EventTagActionTransceivePtr->setResult(FALSE);
279 m_EventTagActionTransceivePtr->setError(util.getNFCErrorString(result));
280 m_EventTagActionTransceivePtr->setErrorMessage(util.getNFCErrorMessage(result));
282 EventRequestReceiver<EventTagActionTransceive>::ManualAnswer(m_EventTagActionTransceivePtr);
284 m_EventTagActionTransceivePtr.Reset();
285 if (EventTagActionTransceives.size() > 0) {
286 EventTagActionTransceivePtr event = EventTagActionTransceives.front();
287 EventTagActionTransceives.erase(EventTagActionTransceives.begin());
288 LoggerD("EventTagActionWrites is removed. (" << EventTagActionTransceives.size() << ")");
289 EventRequestReceiver<EventTagActionTransceive>::PostRequest(event);
294 void NFCTag::formatManualAnswer(int result)
297 if (m_EventTagActionFormatPtr.Get() != NULL) {
298 if ((nfc_error_e)result == NFC_ERROR_NONE) {
299 m_EventTagActionFormatPtr->setResult(TRUE);
302 m_EventTagActionFormatPtr->setResult(FALSE);
303 m_EventTagActionFormatPtr->setError(util.getNFCErrorString(result));
304 m_EventTagActionFormatPtr->setErrorMessage(util.getNFCErrorMessage(result));
306 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(m_EventTagActionFormatPtr);
307 m_EventTagActionFormatPtr.Reset();
311 void NFCTag::OnRequestReceived(const EventTagActionReadPtr& event) {
315 event->switchToManualAnswer();
317 TagReadDataPtr data( new EventNFCPtrs<EventTagActionRead>(event, this));
318 EventTagActionsReads.push_back(data);
319 int result = nfc_tag_read_ndef(handle, TagReadCallback, data.Get());
322 std::string error = util.getNFCErrorString(result);
324 event->setResult(false);
325 event->setError(error);
326 event->setErrorMessage(util.getNFCErrorMessage(result));
327 EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
329 std::vector<TagReadDataPtr>::iterator it;
330 for (it = EventTagActionsReads.begin(); it != EventTagActionsReads.end(); ++it) {
331 if ((*it)->getEventPtrs() == event) {
332 EventTagActionsReads.erase(it);
333 LoggerD("event is removed. (" << EventTagActionsReads.size() << ")");
339 catch (const WrtDeviceApis::Commons::Exception& ex) {
340 LoggerE("Exception: " << ex.GetMessage());
341 event->setResult(false);
343 EventRequestReceiver<EventTagActionRead>::ManualAnswer(event);
348 void NFCTag::OnRequestReceived(const EventTagActionWritePtr& event) {
350 event->switchToManualAnswer();
352 m_EventTagActionWritePtr = event;
354 int result = nfc_tag_write_ndef(handle, (nfc_ndef_message_h)event->getNdefForWriting(),TagWriteCallback, this);
357 std::string error = util.getNFCErrorString(result);
359 writeNdefManualAnswer(result);
362 catch (const WrtDeviceApis::Commons::Exception& ex) {
363 LoggerE("Exception: " << ex.GetMessage());
365 if (event != m_EventTagActionWritePtr)
366 m_EventTagActionWritePtr = event;
367 writeNdefManualAnswer(NFC_ERROR_OPERATION_FAILED);
371 void NFCTag::OnRequestReceived(const EventTagActionTransceivePtr& event) {
374 event->switchToManualAnswer();
376 m_EventTagActionTransceivePtr = event;
378 unsigned char *buffer = util.toCharPtr(event->getTransceiveBuffer());
379 int result = nfc_tag_transceive(handle, buffer, event->getTransceiveBufferSize(), TagTransceiveCallback, this);
384 std::string error = util.getNFCErrorString(result);
386 std::vector<unsigned char> emptyData;
387 transceiveManualAnswer(result, emptyData);
390 catch (const WrtDeviceApis::Commons::Exception& ex) {
391 LoggerE("Exception: " << ex.GetMessage());
393 std::vector<unsigned char> emptyData;
394 transceiveManualAnswer(NFC_ERROR_OPERATION_FAILED, emptyData);
399 void NFCTag::OnRequestReceived(const EventTagActionFormatPtr& event) {
402 event->switchToManualAnswer();
404 if (m_EventTagActionFormatPtr.Get() != NULL) {
405 event->setResult(false);
406 event->setError(DeviceAPI::Common::JSTizenException::SERVICE_NOT_AVAILABLE);
407 event->setErrorMessage("Progressing Identical Operation");
408 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
411 m_EventTagActionFormatPtr = event;
414 LoggerD("key size :" << event->getKeySize());
416 unsigned char *key = util.toCharPtr(event->getKey());
417 int result = nfc_tag_format_ndef(handle, key, event->getKeySize(), TagFormatCallback, this);
422 std::string error = util.getNFCErrorString(result);
424 event->setResult(false);
425 event->setError(error);
426 event->setErrorMessage(util.getNFCErrorMessage(result));
427 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
428 m_EventTagActionFormatPtr.Reset();
431 catch (const WrtDeviceApis::Commons::Exception& ex) {
432 LoggerE("Exception: " << ex.GetMessage());
433 event->setResult(false);
435 EventRequestReceiver<EventTagActionFormat>::ManualAnswer(event);
436 m_EventTagActionFormatPtr.Reset();