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;
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<NFCTagPropertiesT*>(user_data))->push_back(
std::make_pair(key, tag_info));
return true;
int buffer_size,
void* data) {
LoggerD("Entered");
+ std::unique_ptr<unsigned char> 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<double*>(data));
+ delete reinterpret_cast<double*>(data);
+ data = nullptr;
if (NFC_ERROR_NONE != err) {
picojson::value response = createEventSuccess(callback_id);
picojson::object& response_obj = response.get<picojson::object>();
tools::ReportSuccess(response_obj);
- picojson::array& callback_data_array = response_obj.insert(std::make_pair(JSON_DATA,
- picojson::value(picojson::array()))).first->second.get<picojson::array>();
-
- for (unsigned int i = 0; i < buffer_size; i++) {
- callback_data_array.push_back(
- picojson::value(static_cast<double>(buffer[i])));
- }
+ response_obj[JSON_DATA] =
+ picojson::value(NFCUtil::FromUCharArray(buffer_ptr.get(), buffer_size));
NFCAdapter::GetInstance()->RespondAsync(response.serialize().c_str());
}
const picojson::array& data_array = FromJson<picojson::array>(
args.get<picojson::object>(), JSON_DATA);
- std::unique_ptr<unsigned char> 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<unsigned char>(data_array[i].get<double>());
- }
-
+ 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 ||
// 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);
}
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);
}
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);
}
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);
}
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;
}
}
-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");
}
}
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,
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<unsigned char>(apdu_array.at(i).get<double>());
+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<unsigned char>(array_in.at(i).get<double>());
}
- 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,
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<nfc_tag_type_e>& 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);
};