if (m_is_ndef_listener_set) {
nfc_p2p_unset_data_received_cb(m_peer_handle);
}
+ if (m_is_tag_listener_set) {
+ nfc_manager_unset_tag_discovered_cb();
+ }
}
static picojson::value createEventError(double callbackId, PlatformException ex) {
LoggerD("Entered");
- if(tag_id != m_latest_tag_id) {
+ if(tag_id != m_latest_tag_id || NULL == m_last_tag_handle) {
// internaly stored tag id changed -> new tag has been already connected
- LoggerD("NFCTag () not connected. Latest tag id: %d",
- tag_id, m_latest_tag_id);
+ // internaly stored tag handle NULL -> tag has been disconnected
+ LoggerD("NFCTag () not connected (id differs or invalid handle)");
+ return false;
+ }
+
+ nfc_tag_h handle = NULL;
+ int result = nfc_manager_get_connected_tag(&handle);
+ if(NFC_ERROR_NONE != result) {
+ LOGE("Failed to get connected tag: %s",
+ NFCUtil::getNFCErrorMessage(result).c_str());
+ // exception is thrown here to return undefined in JS layer
+ // instead of false
+ NFCUtil::throwNFCException(result, "Failed to get connected tag");
+ }
+
+ if(m_last_tag_handle != handle) {
+ LoggerD("Last known handle and current handle differs");
return false;
}
- // TODO: implement checking if stored handle is still connected
- LoggerW("This function is only a stub!");
return true;
}
+
+int NFCAdapter::GetNextTagId() {
+
+ LoggerD("Entered");
+ return ++m_latest_tag_id;
+}
+
+
+static void tagEventCallback(nfc_discovered_type_e type, nfc_tag_h tag, void *data)
+{
+ LoggerD("Entered");
+
+ picojson::value event = picojson::value(picojson::object());
+ picojson::object& obj = event.get<picojson::object>();
+ obj.insert(make_pair("listenerId", "TagListener"));
+
+ NFCAdapter* adapter = NFCAdapter::GetInstance();
+ // Tag detected event
+ if (NFC_DISCOVERED_TYPE_ATTACHED == type) {
+ nfc_tag_type_e tag_type;
+
+ int result;
+ result = nfc_tag_get_type(tag, &tag_type);
+ if(NFC_ERROR_NONE != result) {
+ LoggerE("setTagListener failed %d",result);
+ return;
+ }
+ // Fetch new id and set detected tag handle in NFCAdapter
+ int generated_id = adapter->GetNextTagId();
+ adapter->SetTagHandle(tag);
+
+ obj.insert(make_pair("action", "onattach"));
+ obj.insert(make_pair("id", static_cast<double>(generated_id)));
+ obj.insert(make_pair("type", NFCUtil::toStringNFCTag(tag_type)));
+
+ NFCInstance::getInstance().PostMessage(event.serialize().c_str());
+ }
+ // Tag disconnected event
+ else if (NFC_DISCOVERED_TYPE_DETACHED == type) {
+ // Set stored tag handle to NULL
+ adapter->SetTagHandle(NULL);
+
+ obj.insert(make_pair("action", "ondetach"));
+ NFCInstance::getInstance().PostMessage(event.serialize().c_str());
+ }
+ // ERROR - should never happen
+ else {
+ LoggerE("Invalid NFC discovered type: %d (%x)", type, type);
+ }
+
+}
+
+void NFCAdapter::SetTagListener(){
+
+ LoggerD("Entered");
+
+ if(!m_is_tag_listener_set) {
+ nfc_manager_set_tag_filter(NFC_TAG_FILTER_ALL_ENABLE);
+ int result = nfc_manager_set_tag_discovered_cb (tagEventCallback, NULL);
+ if (NFC_ERROR_NONE != result) {
+ LoggerE("Failed to register tag listener: %d", result);
+ NFCUtil::throwNFCException(result, "Failed to register tag listene");
+ }
+ m_is_tag_listener_set = true;
+ }
+}
+
+void NFCAdapter::UnsetTagListener(){
+
+ LoggerD("Entered");
+
+ if(m_is_tag_listener_set) {
+ nfc_manager_unset_tag_discovered_cb();
+ m_is_tag_listener_set = false;
+ }
+}
+
+void NFCAdapter::SetTagHandle(nfc_tag_h tag) {
+
+ LoggerD("Entered");
+
+ m_last_tag_handle = tag;
+}
+
+
}// nfc
}// extension
// NFCTag related methods
bool IsTagConnected(int tag_id);
+ void SetTagListener();
+ void UnsetTagListener();
+ int GetNextTagId();
+ void SetTagHandle(nfc_tag_h tag);
static NFCAdapter* GetInstance();
private:
virtual ~NFCAdapter();
nfc_tag_h m_last_tag_handle;
+ bool m_is_tag_listener_set;
int m_latest_tag_id;
bool m_is_listener_set;
bool m_is_transaction_ese_listener_set;
var ACTIVE_SECURE_ELEMENT_LISTENER = 'ActiveSecureElementChanged';
var TRANSACTION_EVENT_ESE_LISTENER = 'TransactionEventListener_ESE';
var TRANSACTION_EVENT_UICC_LISTENER = 'TransactionEventListener_UICC';
+var TAG_LISTENER = 'TagListener';
var cardEmulationModeListener = new ListenerManager(native_, CARD_EMULATION_MODE_LISTENER);
var activeSecureElementChangeListener = new ListenerManager(native_, ACTIVE_SECURE_ELEMENT_LISTENER);
var transactionEventListenerEse = new ListenerManager(native_, TRANSACTION_EVENT_ESE_LISTENER);
NFCAdapter.prototype.setTagListener = function() {
+ var args = validator_.validateArgs(arguments, [
+ {
+ name: 'listener',
+ type: types_.LISTENER,
+ values: ['onattach', 'ondetach']
+ },
+ {
+ name : 'tagType',
+ type : types_.STRING,
+ optional : true,
+ nullable : true
+ }
+ ]);
+
+ // TODO: NFCTag type value validation needed here
+
+ // Listener object creation
+ var listenerCallback = function(message) {
+ var tagObject = undefined;
+
+ if('onattach' === message.action) {
+ tagObject = new NFCTag(message.id);
+
+ if(!types_.isNullOrUndefined(args.tagType)) {
+ // If filter set for listener then check tag type
+ if(tagObject.type !== args.tagType) {
+ return;
+ }
+ }
+ }
+ args.listener[message.action](tagObject);
+ }
+
+ // Register (acivate) core listener if not done yet
+ if(!native_.isListenerSet(TAG_LISTENER)) {
+ var result = native_.callSync('NFCAdapter_setTagListener');
+ if (native_.isFailure(result)) {
+ throw new tizen.WebAPIException(0, result.error.message, result.error.name);
+ }
+ }
+
+ native_.addListener(TAG_LISTENER, listenerCallback);
+ return;
};
NFCAdapter.prototype.setPeerListener = function() {
NFCAdapter.prototype.unsetTagListener = function() {
+ native_.removeListener(TAG_LISTENER);
+
+ var result = native_.callSync('NFCAdapter_unsetTagListener');
+ if (native_.isFailure(result)) {
+ throw new tizen.WebAPIException(0, result.error.message, result.error.name);
+ }
+
+ return;
};
NFCAdapter.prototype.unsetPeerListener = function() {
REGISTER_SYNC("NFCAdapter_activeSecureElementGetter", ActiveSecureElementGetter);
REGISTER_SYNC("NFCAdapter_setPeerListener", SetPeerListener);
REGISTER_SYNC("NFCAdapter_setTagListener", SetTagListener);
- REGISTER_SYNC("NFCAdapter_setPeerListener", SetPeerListener);
REGISTER_SYNC("NFCAdapter_PeerIsConnectedGetter", PeerIsConnectedGetter);
REGISTER_SYNC("NFCAdapter_unsetTagListener", UnsetTagListener);
REGISTER_SYNC("NFCAdapter_unsetPeerListener", UnsetPeerListener);
void NFCInstance::SetTagListener(
const picojson::value& args, picojson::object& out) {
+ try {
+ NFCAdapter::GetInstance()->SetTagListener();
+ ReportSuccess(out);
+ }
+ catch(const common::PlatformException& ex) {
+ ReportError(ex, out);
+ }
}
void NFCInstance::PeerIsConnectedGetter(
void NFCInstance::UnsetTagListener(
const picojson::value& args, picojson::object& out) {
+ NFCAdapter::GetInstance()->UnsetTagListener();
+ ReportSuccess(out);
}
void NFCInstance::UnsetPeerListener(
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- // TODO: implement this stub
- LoggerW("Stub function used!");
- std::string tag_type = NFCUtil::toStringNFCTag(NFC_UNKNOWN_TARGET);
- ReportSuccess(picojson::value(tag_type), out);
+ try {
+ // Function below throws exception if core API call fails
+ if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+ LoggerE("Tag with id %d is not connected anymore", tag_id);
+ // If tag is not connected then attribute's value
+ // should be undefined
+ ReportError(out);
+ return;
+ }
+
+ // TODO: implement this stub
+ LoggerW("Stub function used!");
+ std::string tag_type = NFCUtil::toStringNFCTag(NFC_UNKNOWN_TARGET);
+
+ ReportSuccess(picojson::value(tag_type), out);
+ }
+ catch(const PlatformException& ex) {
+ LoggerE("Failed to check tag connection");
+ ReportError(ex, out);
+ }
}
void NFCInstance::TagIsSupportedNDEFGetter(
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- // TODO: implement this stub
- LoggerW("Stub function used!");
- bool is_supported = true;
- ReportSuccess(picojson::value(is_supported), out);
+ try {
+ // Function below throws exception if core API call fails
+ if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+ LoggerE("Tag with id %d is not connected anymore", tag_id);
+ // If tag is not connected then attribute's value
+ // should be undefined
+ ReportError(out);
+ return;
+ }
+
+ // TODO: implement this stub
+ LoggerW("Stub function used!");
+ bool is_supported = true;
+
+ ReportSuccess(picojson::value(is_supported), out);
+ }
+ catch(const PlatformException& ex) {
+ LoggerE("Failed to check tag connection");
+ ReportError(ex, out);
+ }
+
}
void NFCInstance::TagNDEFSizeGetter(
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- // TODO: implement this stub
- LoggerW("Stub function used!");
- int ndef_size = 1234;
- ReportSuccess(picojson::value((double)ndef_size), out);
+ try {
+ // Function below throws exception if core API call fails
+ if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+ LoggerE("Tag with id %d is not connected anymore", tag_id);
+ // If tag is not connected then attribute's value
+ // should be undefined
+ ReportError(out);
+ return;
+ }
+
+ // TODO: implement this stub
+ LoggerW("Stub function used!");
+ int ndef_size = 1234;
+
+ ReportSuccess(picojson::value((double)ndef_size), out);
+ }
+ catch(const PlatformException& ex) {
+ LoggerE("Failed to check tag connection");
+ ReportError(ex, out);
+ }
+
}
void NFCInstance::TagPropertiesGetter(
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
-
- // TODO: implement this stub
- LoggerW("Stub function used!");
- ReportSuccess(out);
+ try {
+ // Function below throws exception if core API call fails
+ if (!NFCAdapter::GetInstance()->IsTagConnected(tag_id)) {
+ LoggerE("Tag with id %d is not connected anymore", tag_id);
+ // If tag is not connected then attribute's value
+ // should be undefined
+ ReportError(out);
+ return;
+ }
+
+ // TODO: implement this stub
+ LoggerW("Stub function used!");
+ ReportSuccess(out);
+ }
+ catch(const PlatformException& ex) {
+ LoggerE("Failed to check tag connection");
+ ReportError(ex, out);
+ }
}
void NFCInstance::TagIsConnectedGetter(