[Verification] Code compiles without error.
Change-Id: I8cd2134cfe0b1ce87bc2c3f5e21a38e2a0230e65
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
// NFCTag related functions
-std::string NFCAdapter::TagTypeGetter(int tag_id) {
+PlatformResult NFCAdapter::TagTypeGetter(int tag_id, std::string* type) {
LoggerD("Entered");
- nfc_tag_type_e type = NFC_UNKNOWN_TARGET;
+ nfc_tag_type_e nfc_type = NFC_UNKNOWN_TARGET;
- int err = nfc_tag_get_type(m_last_tag_handle, &type);
+ int err = nfc_tag_get_type(m_last_tag_handle, &nfc_type);
if(NFC_ERROR_NONE != err) {
LoggerE("Failed to get tag type: %d", err);
- NFCUtil::throwNFCException(err, "Failed to get tag type");
+ return NFCUtil::CodeToResult(err, "Failed to get tag type");
}
- return NFCUtil::toStringNFCTag(type);
+ *type = NFCUtil::ToStringNFCTag(nfc_type);
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-bool NFCAdapter::TagIsSupportedNDEFGetter(int tag_id) {
+PlatformResult NFCAdapter::TagIsSupportedNDEFGetter(int tag_id, bool *is_supported) {
LoggerD("Entered");
- bool result = false;
-
- int err = nfc_tag_is_support_ndef(m_last_tag_handle, &result);
+ int err = nfc_tag_is_support_ndef(m_last_tag_handle, is_supported);
if(NFC_ERROR_NONE != err) {
LoggerE("Failed to check if NDEF is supported %d", err);
- NFCUtil::throwNFCException(err,
+ return NFCUtil::CodeToResult(err,
"Failed to check if NDEF is supported");
}
- return result;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-unsigned int NFCAdapter::TagNDEFSizeGetter(int tag_id) {
+PlatformResult NFCAdapter::TagNDEFSizeGetter(int tag_id, unsigned int *size) {
LoggerD("Entered");
- unsigned int result = 0;
-
- int err = nfc_tag_get_ndef_size(m_last_tag_handle, &result);
+ int err = nfc_tag_get_ndef_size(m_last_tag_handle, size);
if(NFC_ERROR_NONE != err) {
LoggerE("Failed to get tag NDEF size: %d, %s", err);
- NFCUtil::throwNFCException(err,
+ return NFCUtil::CodeToResult(err,
"Failed to get tag NDEF size");
}
- return result;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
static bool tagPropertiesGetterCb(const char *key,
return false;
}
-NFCTagPropertiesT NFCAdapter::TagPropertiesGetter(int tag_id) {
+PlatformResult NFCAdapter::TagPropertiesGetter(int tag_id, NFCTagPropertiesT *properties) {
LoggerD("Entered");
- NFCTagPropertiesT result;
-
int err = nfc_tag_foreach_information(m_last_tag_handle,
- tagPropertiesGetterCb, (void*)&result);
+ tagPropertiesGetterCb, (void*)properties);
if(NFC_ERROR_NONE != err) {
LoggerE("Error occured while getting NFC properties: %d", err);
- NFCUtil::throwNFCException(err,
+ return NFCUtil::CodeToResult(err,
"Error occured while getting NFC properties");
}
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult NFCAdapter::TagIsConnectedGetter(int tag_id, bool *state) {
+
+ LoggerD("Entered");
+
+ PlatformResult result = PlatformResult(ErrorCode::NO_ERROR);
+
+ if(tag_id != m_latest_tag_id || NULL == m_last_tag_handle) {
+ // internaly stored tag id changed -> new tag has been already connected
+ // internaly stored tag handle NULL -> tag has been disconnected
+ LoggerD("NFCTag () not connected (id differs or invalid handle)");
+ *state = false;
+ return result;
+ }
+
+ nfc_tag_h handle = NULL;
+ int ret = nfc_manager_get_connected_tag(&handle);
+ if(NFC_ERROR_NONE != ret) {
+ LoggerE("Failed to get connected tag: %s",
+ NFCUtil::getNFCErrorMessage(ret).c_str());
+ // exception is thrown here to return undefined in JS layer
+ // instead of false
+ return NFCUtil::CodeToResult(ret, "Failed to get connected tag");
+ }
+
+ if(m_last_tag_handle != handle) {
+ LoggerD("Last known handle and current handle differs");
+ *state = false;
+ } else {
+ *state = true;
+ }
+
return result;
}
+// TODO remove after clean code from try/catch
bool NFCAdapter::TagIsConnectedGetter(int tag_id) {
LoggerD("Entered");
-
if(tag_id != m_latest_tag_id || NULL == m_last_tag_handle) {
// internaly stored tag id changed -> new tag has been already connected
// internaly stored tag handle NULL -> tag has been disconnected
return true;
}
-
int NFCAdapter::GetNextTagId() {
LoggerD("Entered");
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)));
+ obj.insert(make_pair("type", NFCUtil::ToStringNFCTag(tag_type)));
NFCInstance::getInstance().PostMessage(event.serialize().c_str());
}
free(raw_data);
}
-void NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) {
+PlatformResult NFCAdapter::TagReadNDEF(int tag_id, const picojson::value& args) {
LoggerD("Entered");
+
+ bool is_connected = false;
+ PlatformResult result = TagIsConnectedGetter(tag_id, &is_connected);
+ if (result.IsError()) {
+ return result;
+ }
+
double callbackId = args.get(CALLBACK_ID).get<double>();
LoggerD("Received callback id: %f", callbackId);
- if(!TagIsConnectedGetter(tag_id)) {
- UnknownException ex("Tag is no more connected");
-
- picojson::value event = createEventError(callbackId, ex);
+ if(!is_connected) {
+ picojson::value event = CreateEventError(callbackId,
+ PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Tag is no more connected."));
NFCInstance::getInstance().PostMessage(event.serialize().c_str());
- return;
+ return PlatformResult(ErrorCode::NO_ERROR);;
}
double* callbackIdPointer = new double(callbackId);
- int result = nfc_tag_read_ndef(m_last_tag_handle, tagReadNDEFCb,
+ int ret = nfc_tag_read_ndef(m_last_tag_handle, tagReadNDEFCb,
(void*)(callbackIdPointer));
- if(NFC_ERROR_NONE != result) {
- LoggerE("Failed to read NDEF message from tag: %d", result);
+ if(NFC_ERROR_NONE != ret) {
+ LoggerE("Failed to read NDEF message from tag: %d", ret);
delete callbackIdPointer;
callbackIdPointer = NULL;
// for permission related error throw exception ...
- if(NFC_ERROR_SECURITY_RESTRICTED == result ||
- NFC_ERROR_PERMISSION_DENIED == result) {
- throw SecurityException("Failed to read NDEF - permission denied");
+ if(NFC_ERROR_SECURITY_RESTRICTED == ret ||
+ NFC_ERROR_PERMISSION_DENIED == ret) {
+ return PlatformResult(ErrorCode::SECURITY_ERR, "Failed to read NDEF - permission denied");
}
LoggerE("Preparing error callback to call");
// ... otherwise call error callback
- std::string errName = NFCUtil::getNFCErrorString(result);
- std::string errMessage = NFCUtil::getNFCErrorMessage(result);
- PlatformException ex(errName, errMessage);
+ std::string errMessage = NFCUtil::getNFCErrorMessage(ret);
- picojson::value event = createEventError(callbackId, ex);
+ result = NFCUtil::CodeToResult(ret, errMessage.c_str());
+
+ picojson::value event = CreateEventError(callbackId, result);
NFCInstance::getInstance().PostMessage(event.serialize().c_str());
}
+ return PlatformResult(ErrorCode::NO_ERROR);
}
void NFCAdapter::TagWriteNDEF(int tag_id, const picojson::value& args) {
// NFCTag related methods
// attributes
- std::string TagTypeGetter(int tag_id);
- bool TagIsSupportedNDEFGetter(int tag_id);
- unsigned int TagNDEFSizeGetter(int tag_id);
- NFCTagPropertiesT TagPropertiesGetter(int tag_id);
+ common::PlatformResult TagTypeGetter(int tag_id, std::string *type);
+ common::PlatformResult TagIsSupportedNDEFGetter(int tag_id, bool *is_supported);
+ common::PlatformResult TagNDEFSizeGetter(int tag_id, unsigned int *size);
+ common::PlatformResult TagPropertiesGetter(int tag_id, NFCTagPropertiesT *properties);
+ common::PlatformResult TagIsConnectedGetter(int tag_id, bool *state);
+ // TODO remove after clean code from try/catch
bool TagIsConnectedGetter(int tag_id);
// methods
- void TagReadNDEF(int tag_id, const picojson::value& args);
+ common::PlatformResult TagReadNDEF(int tag_id, const picojson::value& args);
void TagWriteNDEF(int tag_id, const picojson::value& args);
void TagTransceive(int tag_id, const picojson::value& args);
// listeners
void NFCInstance::ReadNDEF(
const picojson::value& args, picojson::object& out) {
+ CHECK_EXIST(args, "id", out);
+
int tag_id = static_cast<int>(args.get("id").get<double>());
LoggerD("Tag id: %d", tag_id);
- try {
- NFCAdapter::GetInstance()->TagReadNDEF(tag_id, args);
+ PlatformResult result =NFCAdapter::GetInstance()->TagReadNDEF(tag_id, args);
+ if (result.IsSuccess()) {
ReportSuccess(out);
- }
- catch(const common::PlatformException& ex) {
- ReportError(ex, out);
+ } else {
+ ReportError(result, &out);
}
}
LoggerD("Entered");
+ CHECK_EXIST(args, "id", out);
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- try {
- // Function below throws exception if core API call fails
- if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(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;
- }
+ // Function below throws exception if core API call fails
+ bool is_connected = false;
+ PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
- std::string tag_type =
- NFCAdapter::GetInstance()->TagTypeGetter(tag_id);
+ if (result.IsError()) {
+ ReportError(result, &out);
+ return;
+ }
- ReportSuccess(picojson::value(tag_type), out);
+ if (!is_connected) {
+ 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;
}
- catch(const PlatformException& ex) {
- LoggerE("Failed to check tag connection");
- ReportError(ex, out);
+
+ std::string tag_type = "";
+ result = NFCAdapter::GetInstance()->TagTypeGetter(tag_id, &tag_type);
+
+ if (result.IsSuccess()) {
+ ReportSuccess(picojson::value(tag_type), out);
+ } else {
+ ReportError(result, &out);
}
}
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- try {
- // Function below throws exception if core API call fails
- if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(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;
- }
-
- bool is_supported =
- NFCAdapter::GetInstance()->TagIsSupportedNDEFGetter(tag_id);
+ // Function below throws exception if core API call fails
+ bool is_connected = false;
+ PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
- ReportSuccess(picojson::value(is_supported), out);
+ if (result.IsError()) {
+ ReportError(result, &out);
+ return;
}
- catch(const PlatformException& ex) {
- LoggerE("Failed to check is NDEF supported");
- ReportError(ex, out);
+
+ if (!is_connected) {
+ 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;
}
+ bool is_supported = false;
+ result = NFCAdapter::GetInstance()->TagIsSupportedNDEFGetter(tag_id, &is_supported);
+ if (result.IsSuccess()) {
+ ReportSuccess(picojson::value(is_supported), out);
+ } else {
+ ReportError(result, &out);
+ }
}
void NFCInstance::TagNDEFSizeGetter(
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- try {
- // Function below throws exception if core API call fails
- if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(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;
- }
-
- unsigned int ndef_size =
- NFCAdapter::GetInstance()->TagNDEFSizeGetter(tag_id);
+ // Function below throws exception if core API call fails
+ bool is_connected = false;
+ PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
- ReportSuccess(picojson::value((double)ndef_size), out);
+ if (result.IsError()) {
+ ReportError(result, &out);
+ return;
}
- catch(const PlatformException& ex) {
- LoggerE("Failed to get tag NDEF size");
- ReportError(ex, out);
+
+ if (!is_connected) {
+ 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;
}
+ unsigned int ndef_size;
+ result = NFCAdapter::GetInstance()->TagNDEFSizeGetter(tag_id, &ndef_size);
+
+ if (result.IsSuccess()) {
+ ReportSuccess(picojson::value((double)ndef_size), out);
+ } else {
+ ReportError(result, &out);
+ }
}
void NFCInstance::TagPropertiesGetter(
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- try {
- // Function below throws exception if core API call fails
- if (!NFCAdapter::GetInstance()->TagIsConnectedGetter(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;
- }
- NFCTagPropertiesT result =
- NFCAdapter::GetInstance()->TagPropertiesGetter(tag_id);
+ // Function below throws exception if core API call fails
+ bool is_connected = false;
+ PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &is_connected);
+
+ if (result.IsError()) {
+ ReportError(result, &out);
+ return;
+ }
+
+ if (!is_connected) {
+ 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;
+ }
+
+ NFCTagPropertiesT prop;
+ result = NFCAdapter::GetInstance()->TagPropertiesGetter(tag_id, &prop);
+ if (result.IsSuccess()) {
picojson::value properties = picojson::value(picojson::array());
picojson::array& properties_array = properties.get<picojson::array>();
- for (auto it = result.begin() ; it != result.end(); it++) {
+ for (auto it = prop.begin() ; it != prop.end(); it++) {
picojson::value val = picojson::value(picojson::object());
picojson::object& obj = val.get<picojson::object>();
properties_array.push_back(val);
}
ReportSuccess(properties, out);
+ } else {
+ ReportError(result, &out);
}
- catch(const PlatformException& ex) {
- LoggerE("Failed to tag properties");
- ReportError(ex, out);
- }
+
}
void NFCInstance::TagIsConnectedGetter(
LoggerD("Entered");
+ CHECK_EXIST(args, "id", out);
int tag_id = (int)args.get("id").get<double>();
LoggerD("Tag id: %d", tag_id);
- try {
- bool connected = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id);
- ReportSuccess(picojson::value(connected), out);
- }
- catch(const PlatformException& ex) {
- LoggerE("Failed to check tag connection");
- ReportError(ex, out);
+
+ bool connected = false;
+ PlatformResult result = NFCAdapter::GetInstance()->TagIsConnectedGetter(tag_id, &connected);
+
+ if (result.IsSuccess()) {
+ ReportSuccess(out);
+ } else {
+ ReportError(result, &out);
}
}
-
} // namespace nfc
} // namespace extension
return "UnknownError";
}
-std::string NFCUtil::toStringNFCTag(nfc_tag_type_e tag_type)
+std::string NFCUtil::ToStringNFCTag(nfc_tag_type_e tag_type)
{
switch (tag_type) {
case NFC_GENERIC_PICC:
static void throwNFCException(const int errorCode, const char * message);
static std::string getNFCErrorString(const int error_code);
static std::string getNFCErrorMessage(const int error_code);
- static std::string toStringNFCTag(const nfc_tag_type_e tag_type);
+ static std::string ToStringNFCTag(const nfc_tag_type_e tag_type);
static nfc_tag_type_e toNfcTagString(const std::string& type_string);
static common::PlatformResult ToStringCardEmulationMode(
const nfc_se_card_emulation_mode_type_e card_mode, std::string *mode);