From f6c81ccfaa36f755aaeef5bdbb220c6af27819d6 Mon Sep 17 00:00:00 2001 From: Tomasz Marciniak Date: Thu, 21 May 2015 15:59:15 +0200 Subject: [PATCH] [NFC] Fix for NFCTag manual test. [Feature] Create NDEFRecord before passing it to NDEFMessage constructor. [Verification] Code compiles without errors. Manual test NFCTag_writeNDEF_with_successCallback passes. Change-Id: I9ac72702a351d1c28e767f470c11d5c761901f49 Signed-off-by: Tomasz Marciniak --- src/nfc/nfc_api.js | 125 +++++++++++++++++++++++++---------- src/nfc/nfc_message_utils.cc | 3 + 2 files changed, 92 insertions(+), 36 deletions(-) diff --git a/src/nfc/nfc_api.js b/src/nfc/nfc_api.js index 5ffd6edd..2522eca7 100644 --- a/src/nfc/nfc_api.js +++ b/src/nfc/nfc_api.js @@ -579,7 +579,7 @@ NFCAdapter.prototype.getCachedMessage = function() { return new tizen.NDEFMessage(); } - return new tizen.NDEFMessage(result.records); + return new tizen.NDEFMessage(toRecordsArray(result.records)); }; NFCAdapter.prototype.setExclusiveModeForTransaction = function() { @@ -861,6 +861,47 @@ NFCAdapter.prototype.getAIDsForCategory = function(type, category, successCallba native_.call('NFCAdapter_getAIDsForCategory', data, callback); }; +function InternalRecordData(tnf, type, payload, id) { + this.tnf = tnf; + this.type = type; + this.payload = payload; + this.id = id; +}; + +var toRecordsArray = function(array) { + var result = []; + if (type_.isNullOrUndefined(array) || !type_.isArray(array)) { + return result; + } + + for (var i = 0; i < array.length; i++) { + var data = new InternalRecordData(array[i].tnf, array[i].type, array[i].payload, array[i].id); + + if (array[i].recordType == 'Record') { + result.push(new tizen.NDEFRecord(data.tnf_, data.type_, data.payload_, data.id_)); + continue; + } + + if (array[i].recordType == 'RecordText') { + result.push(new tizen.NDEFRecordText(array[i].text, array[i].languageCode, + array[i].encoding, data)); + continue; + } + + if (array[i].recordType == 'RecordURI') { + result.push(new tizen.NDEFRecordURI(array[i].uri, data)); + continue; + } + + if (array[i].recordType == 'RecordMedia') { + result.push(new tizen.NDEFRecordMedia(array[i].mimeType, array[i].data, data)); + continue; + } + } + + return result; +}; + //////////////////NFCTag ///////////////// function NFCTag(tagid) { @@ -953,7 +994,7 @@ function NFCTag(tagid) { args.errorCallback(native_.getErrorObject(result)); } } else { - var message = new tizen.NDEFMessage(result.records); + var message = new tizen.NDEFMessage(toRecordsArray(result.records)); args.readCallback(message); } }); @@ -1329,7 +1370,7 @@ tizen.NDEFRecord = function(first, type, payload, id) { }; //////////////////NDEFRecordText ///////////////// -tizen.NDEFRecordText = function(text, languageCode, encoding) { +tizen.NDEFRecordText = function(text, languageCode, encoding, internal_) { var text_ = undefined; var languageCode_ = undefined; var encoding_ = NDEFRecordTextEncoding[encoding] ? @@ -1339,18 +1380,22 @@ tizen.NDEFRecordText = function(text, languageCode, encoding) { text_ = converter_.toString(text); languageCode_ = converter_.toString(languageCode); - var result = native_.callSync( - 'NDEFRecordText_constructor', { - 'text': text_, - 'languageCode' : languageCode_, - 'encoding' : encoding_ - } - ); - if (native_.isFailure(result)) { - throw native_.getErrorObject(result); + if (!type_.isNullOrUndefined(internal_) && (internal_ instanceof InternalRecordData)) { + tizen.NDEFRecord.call(this, internal_.tnf_, internal_.type_, internal_.payload_, internal_.id_); + } else { + var result = native_.callSync( + 'NDEFRecordText_constructor', { + 'text': text_, + 'languageCode' : languageCode_, + 'encoding' : encoding_ + } + ); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + tizen.NDEFRecord.call(this, result.result.tnf, result.result.type, + result.result.payload, result.result.id); } - tizen.NDEFRecord.call(this, result.result.tnf, result.result.type, - result.result.payload, result.result.id); } else { throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR); } @@ -1374,22 +1419,26 @@ tizen.NDEFRecordText.prototype = new tizen.NDEFRecord(new InternalData()); tizen.NDEFRecordText.prototype.constructor = tizen.NDEFRecordText; //////////////////NDEFRecordURI ///////////////// -tizen.NDEFRecordURI = function(uri) { +tizen.NDEFRecordURI = function(uri, internal_) { var uri_ = undefined; try { if (arguments.length >= 1) { uri_ = converter_.toString(uri); - var result = native_.callSync( - 'NDEFRecordURI_constructor', { - 'uri': uri_ - } - ); - if (native_.isFailure(result)) { - throw native_.getErrorObject(result); + if (!type_.isNullOrUndefined(internal_) && (internal_ instanceof InternalRecordData)) { + tizen.NDEFRecord.call(this, internal_.tnf_, internal_.type_, internal_.payload_, internal_.id_); + } else { + var result = native_.callSync( + 'NDEFRecordURI_constructor', { + 'uri': uri_ + } + ); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + tizen.NDEFRecord.call(this, result.result.tnf, result.result.type, + result.result.payload, result.result.id); } - tizen.NDEFRecord.call(this, result.result.tnf, result.result.type, - result.result.payload, result.result.id); } else { throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR); } @@ -1409,7 +1458,7 @@ tizen.NDEFRecordURI.prototype = new tizen.NDEFRecord(new InternalData()); tizen.NDEFRecordURI.prototype.constructor = tizen.NDEFRecordURI; //////////////////NDEFRecordMedia ///////////////// -tizen.NDEFRecordMedia = function(mimeType, data) { +tizen.NDEFRecordMedia = function(mimeType, data, internal_) { var mimeType_ = undefined; var data_ = undefined; try { @@ -1417,18 +1466,22 @@ tizen.NDEFRecordMedia = function(mimeType, data) { mimeType_ = converter_.toString(mimeType); data_ = toByteArray(data, Math.pow(2, 32) - 1); - var result = native_.callSync( - 'NDEFRecordMedia_constructor', { - 'mimeType': mimeType_, - 'data': data_, - 'dataSize': data_.length - } - ); - if (native_.isFailure(result)) { - throw native_.getErrorObject(result); + if (!type_.isNullOrUndefined(internal_) && (internal_ instanceof InternalRecordData)) { + tizen.NDEFRecord.call(this, internal_.tnf_, internal_.type_, internal_.payload_, internal_.id_); + } else { + var result = native_.callSync( + 'NDEFRecordMedia_constructor', { + 'mimeType': mimeType_, + 'data': data_, + 'dataSize': data_.length + } + ); + if (native_.isFailure(result)) { + throw native_.getErrorObject(result); + } + tizen.NDEFRecord.call(this, result.result.tnf, result.result.type, + result.result.payload, result.result.id); } - tizen.NDEFRecord.call(this, result.result.tnf, result.result.type, - result.result.payload, result.result.id); } else { throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR); } diff --git a/src/nfc/nfc_message_utils.cc b/src/nfc/nfc_message_utils.cc index dd3741f5..1b2deaee 100644 --- a/src/nfc/nfc_message_utils.cc +++ b/src/nfc/nfc_message_utils.cc @@ -258,6 +258,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message, LoggerE("Error: %s", ret.message().c_str()); return ret; } + record_obj.insert(std::make_pair("recordType", picojson::value("RecordText"))); continue; } if (RECORD_TYPE_URI == type[0]) { @@ -266,6 +267,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message, LoggerE("Error: %s", ret.message().c_str()); return ret; } + record_obj.insert(std::make_pair("recordType", picojson::value("RecordURI"))); continue; } } @@ -275,6 +277,7 @@ PlatformResult NFCMessageUtils::ToNdefRecords(const nfc_ndef_message_h message, LoggerE("Error: %s", ret.message().c_str()); return ret; } + record_obj.insert(std::make_pair("recordType", picojson::value("Record"))); } } return PlatformResult(ErrorCode::NO_ERROR); -- 2.34.1