From 77ed9e1efabe83efed9548f2bd263eb74816bcdf Mon Sep 17 00:00:00 2001 From: Grzegorz Rynkowski Date: Mon, 30 Mar 2015 12:42:40 +0200 Subject: [PATCH] [NFC] Small fixes Verification: 1. Build the source. 2. TCT tests result should be unchanged (basic tests package: 91/91, plus unchanged results of extended tests package). Change-Id: I3a4d3dc548bed82304f66db8ad3f1fcb88ab4129 Signed-off-by: Grzegorz Rynkowski --- src/nfc/nfc_adapter.cc | 44 +++++++++++------------- src/nfc/nfc_adapter.h | 2 +- src/nfc/nfc_message_utils.cc | 6 ++-- src/nfc/nfc_util.cc | 65 +++++++++++++++++++----------------- src/nfc/nfc_util.h | 13 +++++--- 5 files changed, 67 insertions(+), 63 deletions(-) diff --git a/src/nfc/nfc_adapter.cc b/src/nfc/nfc_adapter.cc index 7339788f..6f7fbefc 100644 --- a/src/nfc/nfc_adapter.cc +++ b/src/nfc/nfc_adapter.cc @@ -401,15 +401,18 @@ PlatformResult NFCAdapter::GetCardEmulationMode(std::string* mode) { return NFCUtil::ToStringCardEmulationMode(card_mode, mode); } -PlatformResult NFCAdapter::SetCardEmulationMode(std::string mode) { +PlatformResult NFCAdapter::SetCardEmulationMode(const std::string& mode) { LoggerD("Entered"); - nfc_se_card_emulation_mode_type_e new_mode = - NFCUtil::toCardEmulationMode(mode); + nfc_se_card_emulation_mode_type_e new_mode; + PlatformResult result = NFCUtil::ToCardEmulationMode(mode, &new_mode); + if (result.IsError()) { + return result; + } LoggerD("Card emulation mode value: %x", (int)new_mode); std::string current_mode = ""; - PlatformResult result = GetCardEmulationMode(¤t_mode); + result = GetCardEmulationMode(¤t_mode); if (result.IsError()) { return result; @@ -826,7 +829,7 @@ static bool tagPropertiesGetterCb(const char* key, int value_size, void* user_data) { if (user_data) { - UCharVector tag_info = NFCUtil::toVector(value, value_size); + UCharVector tag_info = NFCUtil::ToVector(value, value_size); (static_cast(user_data))->push_back( std::make_pair(key, tag_info)); return true; @@ -1131,16 +1134,17 @@ static void tagTransceiveCb(nfc_error_e err, int buffer_size, void* data) { LoggerD("Entered"); + std::unique_ptr buffer_ptr(buffer); + buffer = nullptr; if (!data) { // no callback id - unable to report success, neither error LoggerE("Unable to launch transceive callback"); return; } - - double callback_id = *((double*)data); - delete (double*)data; - data = NULL; + double callback_id = *(reinterpret_cast(data)); + delete reinterpret_cast(data); + data = nullptr; if (NFC_ERROR_NONE != err) { @@ -1159,13 +1163,8 @@ static void tagTransceiveCb(nfc_error_e err, picojson::value response = createEventSuccess(callback_id); picojson::object& response_obj = response.get(); tools::ReportSuccess(response_obj); - picojson::array& callback_data_array = response_obj.insert(std::make_pair(JSON_DATA, - picojson::value(picojson::array()))).first->second.get(); - - for (unsigned int i = 0; i < buffer_size; i++) { - callback_data_array.push_back( - picojson::value(static_cast(buffer[i]))); - } + response_obj[JSON_DATA] = + picojson::value(NFCUtil::FromUCharArray(buffer_ptr.get(), buffer_size)); NFCAdapter::GetInstance()->RespondAsync(response.serialize().c_str()); } @@ -1192,20 +1191,17 @@ PlatformResult NFCAdapter::TagTransceive(int tag_id, const picojson::value& args const picojson::array& data_array = FromJson( args.get(), JSON_DATA); - std::unique_ptr buffer_ptr(new unsigned char[data_array.size()]); - - for(std::size_t i = 0; i < data_array.size(); ++i) { - buffer_ptr.get()[i] = static_cast(data_array[i].get()); - } - + unsigned char* buffer = NFCUtil::DoubleArrayToUCharArray(data_array); double* callback_id_pointer = new double(callback_id); - int ret = nfc_tag_transceive(m_last_tag_handle, buffer_ptr.get(), + int ret = nfc_tag_transceive(m_last_tag_handle, buffer, data_array.size(), tagTransceiveCb, (void*) callback_id_pointer); if (NFC_ERROR_NONE != ret) { delete callback_id_pointer; - callback_id_pointer = NULL; + callback_id_pointer = nullptr; + delete[] buffer; + buffer = nullptr; // for permission related error throw exception if(NFC_ERROR_SECURITY_RESTRICTED == ret || diff --git a/src/nfc/nfc_adapter.h b/src/nfc/nfc_adapter.h index 9f988eef..f09117d5 100644 --- a/src/nfc/nfc_adapter.h +++ b/src/nfc/nfc_adapter.h @@ -41,7 +41,7 @@ class NFCAdapter { // cardEmulationMode getter and setter common::PlatformResult GetCardEmulationMode(std::string* mode); - common::PlatformResult SetCardEmulationMode(std::string mode); + common::PlatformResult SetCardEmulationMode(const std::string& mode); // activeSecureElement getter and setter common::PlatformResult GetActiveSecureElement(std::string* type); common::PlatformResult SetActiveSecureElement(std::string element); diff --git a/src/nfc/nfc_message_utils.cc b/src/nfc/nfc_message_utils.cc index 1ddd8b8c..be56d51c 100644 --- a/src/nfc/nfc_message_utils.cc +++ b/src/nfc/nfc_message_utils.cc @@ -108,7 +108,7 @@ static PlatformResult getTypeNameFromHandle(nfc_ndef_record_h handle, } return NFCUtil::CodeToResult(result, "Can't get record's type"); } - *type = NFCUtil::toVector(type_name, type_size); + *type = NFCUtil::ToVector(type_name, type_size); return PlatformResult(ErrorCode::NO_ERROR); } @@ -134,7 +134,7 @@ static PlatformResult getIdFromHandle(nfc_ndef_record_h handle, return NFCUtil::CodeToResult(result, "Can't get record's id"); } - *id = NFCUtil::toVector(tmp_id, id_size); + *id = NFCUtil::ToVector(tmp_id, id_size); return PlatformResult(ErrorCode::NO_ERROR); } @@ -161,7 +161,7 @@ static PlatformResult getPayloadFromHandle(nfc_ndef_record_h handle, return NFCUtil::CodeToResult(result, "Can't get record's payload"); } - *payload = NFCUtil::toVector(tmp_payload, payload_size); + *payload = NFCUtil::ToVector(tmp_payload, payload_size); return PlatformResult(ErrorCode::NO_ERROR); } diff --git a/src/nfc/nfc_util.cc b/src/nfc/nfc_util.cc index 91a702dd..89f3f4d8 100644 --- a/src/nfc/nfc_util.cc +++ b/src/nfc/nfc_util.cc @@ -14,7 +14,7 @@ using namespace common; namespace extension { namespace nfc { -UCharVector NFCUtil::toVector(const unsigned char* ch, const int size) +UCharVector NFCUtil::ToVector(const unsigned char* ch, const int size) { UCharVector vec(ch, ch + size / sizeof(char)); return vec; @@ -151,58 +151,58 @@ std::string NFCUtil::ToStringNFCTag(nfc_tag_type_e tag_type) } } -nfc_tag_type_e NFCUtil::toNfcTagString(const std::string& type_string) +PlatformResult NFCUtil::ToNfcTagString(const std::string& type_string, nfc_tag_type_e* tag_type) { if (GENERIC_TARGET == type_string) { - return NFC_GENERIC_PICC; + *tag_type = NFC_GENERIC_PICC; } else if (ISO14443_A == type_string) { - return NFC_ISO14443_A_PICC; + *tag_type = NFC_ISO14443_A_PICC; } else if (ISO14443_4A == type_string) { - return NFC_ISO14443_4A_PICC; + *tag_type = NFC_ISO14443_4A_PICC; } else if (ISO14443_3A == type_string) { - return NFC_ISO14443_3A_PICC; + *tag_type = NFC_ISO14443_3A_PICC; } else if (MIFARE_MINI == type_string) { - return NFC_MIFARE_MINI_PICC; + *tag_type = NFC_MIFARE_MINI_PICC; } else if (MIFARE_1K == type_string) { - return NFC_MIFARE_1K_PICC; + *tag_type = NFC_MIFARE_1K_PICC; } else if (MIFARE_4K == type_string) { - return NFC_MIFARE_4K_PICC; + *tag_type = NFC_MIFARE_4K_PICC; } else if (MIFARE_ULTRA == type_string) { - return NFC_MIFARE_ULTRA_PICC; + *tag_type = NFC_MIFARE_ULTRA_PICC; } else if (MIFARE_DESFIRE == type_string) { - return NFC_MIFARE_DESFIRE_PICC; + *tag_type = NFC_MIFARE_DESFIRE_PICC; } else if (ISO14443_B == type_string) { - return NFC_ISO14443_B_PICC; + *tag_type = NFC_ISO14443_B_PICC; } else if (ISO14443_4B == type_string) { - return NFC_ISO14443_4B_PICC; + *tag_type = NFC_ISO14443_4B_PICC; } else if (ISO14443_BPRIME == type_string) { - return NFC_ISO14443_BPRIME_PICC; + *tag_type = NFC_ISO14443_BPRIME_PICC; } else if (FELICA == type_string) { - return NFC_FELICA_PICC; + *tag_type = NFC_FELICA_PICC; } else if (JEWEL == type_string) { - return NFC_JEWEL_PICC; + *tag_type = NFC_JEWEL_PICC; } else if (ISO15693 == type_string) { - return NFC_ISO15693_PICC; + *tag_type = NFC_ISO15693_PICC; } else if (UNKNOWN_TARGET == type_string) { - return NFC_UNKNOWN_TARGET; + *tag_type = NFC_UNKNOWN_TARGET; } else { - throw TypeMismatchException("No Match Tag Type"); + return PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "No Match Tag Type"); } } @@ -224,17 +224,18 @@ PlatformResult NFCUtil::ToStringCardEmulationMode( return PlatformResult(ErrorCode::NO_ERROR); } -nfc_se_card_emulation_mode_type_e NFCUtil::toCardEmulationMode( - const std::string &mode_string) -{ +PlatformResult NFCUtil::ToCardEmulationMode( + const std::string& mode_string, + nfc_se_card_emulation_mode_type_e* mode) { if (mode_string == ALWAYS_ON) { - return NFC_SE_CARD_EMULATION_MODE_ON; + *mode = NFC_SE_CARD_EMULATION_MODE_ON; } else if (mode_string == OFF) { - return NFC_SE_CARD_EMULATION_MODE_OFF; + *mode = NFC_SE_CARD_EMULATION_MODE_OFF; } else { LOGE("No Match Card Emulation mode: %s", mode_string.c_str()); - throw TypeMismatchException("No Match Card Emulation mode"); + return PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "No Match Card Emulation mode"); } + return PlatformResult(ErrorCode::NO_ERROR); } PlatformResult NFCUtil::ToStringSecureElementType(const nfc_se_type_e se_type, @@ -333,12 +334,16 @@ nfc_card_emulation_category_type_e NFCUtil::StringToCategory(const std::string& AssertMsg(false, "That category type is incorrect."); } -UCharVector NFCUtil::DoubleArrayToUCharVector(const picojson::array& apdu_array) { - unsigned char apdu[apdu_array.size()]; - for(std::size_t i = 0; i < apdu_array.size(); ++i) { - apdu[i] = static_cast(apdu_array.at(i).get()); +unsigned char* NFCUtil::DoubleArrayToUCharArray(const picojson::array& array_in) { + unsigned char* result_array = new unsigned char[array_in.size()]; + for(std::size_t i = 0; i < array_in.size(); ++i) { + result_array[i] = static_cast(array_in.at(i).get()); } - return toVector(apdu, apdu_array.size()); + return result_array; +} + +UCharVector NFCUtil::DoubleArrayToUCharVector(const picojson::array& array_in) { + return ToVector(NFCUtil::DoubleArrayToUCharArray(array_in), array_in.size()); } picojson::array NFCUtil::FromUCharArray(unsigned char* array, diff --git a/src/nfc/nfc_util.h b/src/nfc/nfc_util.h index f702e418..8ba36c33 100644 --- a/src/nfc/nfc_util.h +++ b/src/nfc/nfc_util.h @@ -49,24 +49,27 @@ typedef std::vector UCharVector; class NFCUtil { public: - static UCharVector toVector(const unsigned char *ch, const int size); + static UCharVector ToVector(const unsigned char* ch, const int size); static common::PlatformResult CodeToResult(const int errorCode, const std::string& message); static std::string getNFCErrorString(const int error_code); static const std::string getNFCErrorMessage(const int error_code); 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 ToNfcTagString(const std::string& type_string, + nfc_tag_type_e* tag_type); static common::PlatformResult ToStringCardEmulationMode( const nfc_se_card_emulation_mode_type_e card_mode, std::string *mode); - static nfc_se_card_emulation_mode_type_e toCardEmulationMode( - const std::string& mode_string); + static common::PlatformResult ToCardEmulationMode( + const std::string& mode_string, + nfc_se_card_emulation_mode_type_e* mode); static common::PlatformResult ToStringSecureElementType(const nfc_se_type_e se_type, std::string *type); static common::PlatformResult ToSecureElementType(const std::string& type_string, nfc_se_type_e *type); static void setDefaultFilterValues(std::vector& filter); static const char* ToStr(nfc_hce_event_type_e event_type); static const char* ToStr(nfc_se_type_e se_type); static nfc_card_emulation_category_type_e StringToCategory(const std::string& category_type); - static UCharVector DoubleArrayToUCharVector(const picojson::array& apdu_array); + static unsigned char* DoubleArrayToUCharArray(const picojson::array& array_in); + static UCharVector DoubleArrayToUCharVector(const picojson::array& array_in); static picojson::array FromUCharArray(unsigned char* array, unsigned int apdu_len); }; -- 2.34.1