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 "NFCTarget.h"
26 using namespace WrtDeviceApis;
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 void TargetReceivedCallback(nfc_p2p_target_h target, nfc_ndef_message_h message, void *user_data) {
37 LoggerD("Callback TargetReceivedCallback.");
39 unsigned char *rawdata = NULL;
41 if (nfc_ndef_message_get_rawdata(message, &rawdata, &size) != NFC_ERROR_NONE) {
42 LoggerE("Can't get rawdata of message");
49 std::vector<unsigned char> receiveData = util.toVector(rawdata, size);
52 ((NFCTarget *)user_data)->receiveNdef(receiveData);
54 LoggerD("Error! TargetReceivedCallback is Null.");
58 static void TargetSentCallback(nfc_error_e result, void *user_data) {
59 LoggerD("Callback TargetSentCallback.");
61 ((NFCTarget *)user_data)->sendNDEFManualAnswer((unsigned int)result);
64 LoggerD("Error! TargetSentCallback is Null.");
69 NFCTarget::NFCTarget(void *targetHandle)
73 if (targetHandle == NULL)
74 ThrowMsg(Commons::UnknownException, "Target Handler is Null Pointer.");
76 handle = (nfc_p2p_target_h)targetHandle;
77 LoggerD("handle:"<<targetHandle);
78 m_isSetReceiveListenter = false;
79 m_EventTargetActionSendPtr.Reset();
82 NFCTarget::~NFCTarget()
88 bool NFCTarget::isConnected() {
89 nfc_p2p_target_h curHandle = NULL;
90 if (nfc_manager_get_connected_target(&curHandle) == NFC_ERROR_NONE) {
91 if (curHandle == handle)
97 void NFCTarget::setReceiveNDEFListener(const EventTargetActionReceiveEmitterPtr& event) {
99 if (m_isSetReceiveListenter == true)
100 ThrowMsg(Commons::AlreadyInUseException, "Already set Listener");
103 m_isSetReceiveListenter = true;
105 int result = nfc_p2p_set_data_received_cb(handle, TargetReceivedCallback, this);
108 if (result != NFC_ERROR_NONE) {
109 m_isSetReceiveListenter = false;
110 util.throwNFCException(result, "Can't set Receive Ndef Listener");
114 void NFCTarget::receiveNdef(std::vector<unsigned char>&messageData) {
116 if ((m_isSetReceiveListenter == true) && (m_emitter.Get() != NULL)) {
117 EventTargetActionReceivePtr event(new EventTargetActionReceive());
118 event->setReadNdefResult(messageData);
119 m_emitter->emit(event);
123 void NFCTarget::unsetReceiveNDEFListener() {
125 if (m_isSetReceiveListenter == true) {
126 nfc_p2p_unset_data_received_cb(handle);
127 LoggerD("Success to unset Listener");
129 LoggerD("No set Listener");
131 m_isSetReceiveListenter = false;
134 void NFCTarget::sendNDEF(const EventTargetActionSendPtr& event) {
137 if (m_EventTargetActionSendPtr.Get() != NULL)
138 m_EventTargetActionSends.push_back(event);
140 EventRequestReceiver<EventTargetActionSend>::PostRequest(event);
145 void NFCTarget::sendNDEFManualAnswer(unsigned int result) {
147 if (m_EventTargetActionSendPtr.Get() != NULL) {
148 if ((nfc_error_e)result == NFC_ERROR_NONE) {
149 m_EventTargetActionSendPtr->setResult(TRUE);
152 m_EventTargetActionSendPtr->setResult(FALSE);
153 m_EventTargetActionSendPtr->setError(util.getNFCErrorString(result));
154 m_EventTargetActionSendPtr->setErrorMessage(util.getNFCErrorMessage(result));
156 EventRequestReceiver<EventTargetActionSend>::ManualAnswer(m_EventTargetActionSendPtr);
158 m_EventTargetActionSendPtr.Reset();
160 if (m_EventTargetActionSends.size() > 0) {
161 EventTargetActionSendPtr event = m_EventTargetActionSends.front();
162 m_EventTargetActionSends.erase(m_EventTargetActionSends.begin());
163 LoggerD("m_EventTargetActionSends is removed. (" << m_EventTargetActionSends.size() << ")");
164 EventRequestReceiver<EventTargetActionSend>::PostRequest(event);
169 void NFCTarget::OnRequestReceived(const EventTargetActionSendPtr& event) {
172 event->switchToManualAnswer();
173 m_EventTargetActionSendPtr = event;
175 nfc_ndef_message_h message = (nfc_ndef_message_h)event->getMessageForSending();
176 int result = nfc_p2p_send(handle, message, TargetSentCallback, this);
179 std::string error = util.getNFCErrorString(result);
181 sendNDEFManualAnswer(result);
183 } catch (const Commons::Exception& ex) {
184 LoggerE("Exception: " << ex.GetMessage());
185 if (event != m_EventTargetActionSendPtr)
186 m_EventTargetActionSendPtr = event;
187 sendNDEFManualAnswer(NFC_ERROR_OPERATION_FAILED);