From cffc0221cca537a2364ce4836756442cd3a7a267 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Tue, 28 Aug 2018 17:43:43 +0100 Subject: [PATCH] NFC: Add get_record_size in common types. --- features/nfc/nfc/ndef/common/Mime.h | 7 +++++++ features/nfc/nfc/ndef/common/Text.h | 7 +++++++ features/nfc/nfc/ndef/common/URI.h | 7 +++++++ features/nfc/source/nfc/ndef/common/Mime.cpp | 15 +++++++++++++++ features/nfc/source/nfc/ndef/common/Text.cpp | 20 ++++++++++++++++++++ features/nfc/source/nfc/ndef/common/URI.cpp | 20 ++++++++++++++++++++ 6 files changed, 76 insertions(+) diff --git a/features/nfc/nfc/ndef/common/Mime.h b/features/nfc/nfc/ndef/common/Mime.h index 37e47ee7d7..0128e573de 100644 --- a/features/nfc/nfc/ndef/common/Mime.h +++ b/features/nfc/nfc/ndef/common/Mime.h @@ -112,6 +112,13 @@ public: bool is_last_record = false ); + /** + * Compute the size of this Mime object in a ndef record. + * + * @return The size of the ndef record required to store this object. + */ + size_t get_record_size() const; + private: friend class MimeParser; diff --git a/features/nfc/nfc/ndef/common/Text.h b/features/nfc/nfc/ndef/common/Text.h index aa6421eef8..99bd968563 100644 --- a/features/nfc/nfc/ndef/common/Text.h +++ b/features/nfc/nfc/ndef/common/Text.h @@ -128,6 +128,13 @@ public: bool is_last_record = false ); + /** + * Compute the size of this object in a ndef record. + * + * @return The size of the ndef record required to store this object. + */ + size_t get_record_size() const; + private: friend class TextParser; diff --git a/features/nfc/nfc/ndef/common/URI.h b/features/nfc/nfc/ndef/common/URI.h index 73d4f64119..8570de60e6 100644 --- a/features/nfc/nfc/ndef/common/URI.h +++ b/features/nfc/nfc/ndef/common/URI.h @@ -150,6 +150,13 @@ public: bool is_last_record = false ); + /** + * Compute the size of this object in a ndef record. + * + * @return The size of the ndef record required to store this object. + */ + size_t get_record_size() const; + /** * Equal operator between two URIs * @param lhs The URI on the left hand side diff --git a/features/nfc/source/nfc/ndef/common/Mime.cpp b/features/nfc/source/nfc/ndef/common/Mime.cpp index 77074a8234..06a969e97e 100644 --- a/features/nfc/source/nfc/ndef/common/Mime.cpp +++ b/features/nfc/source/nfc/ndef/common/Mime.cpp @@ -106,6 +106,21 @@ bool Mime::append_as_record( get_mime_content(), is_last_record ); + +size_t Mime::get_record_size() const +{ + return MessageBuilder::compute_record_size( + Record( + RecordType( + RecordType::media_type, + get_mime_type() + ), + get_mime_content(), + RecordID(), + /* chunk */ false, + /* last record */ false + ) + ); } void Mime::move_data( diff --git a/features/nfc/source/nfc/ndef/common/Text.cpp b/features/nfc/source/nfc/ndef/common/Text.cpp index 478bdd89d9..f8870291c2 100644 --- a/features/nfc/source/nfc/ndef/common/Text.cpp +++ b/features/nfc/source/nfc/ndef/common/Text.cpp @@ -149,6 +149,26 @@ bool Text::append_as_record( return message_builder.append_record(type, payload, is_last_record); } +size_t Text::get_record_size() const +{ + if (!_text_record) { + return 0; + } + + return MessageBuilder::compute_record_size( + Record( + RecordType( + RecordType::well_known_type, + text_record_type_value + ), + RecordPayload(_text_record, _text_record_size), + RecordID(), + /* chunk */ false, + /* last record */ false + ) + ); +} + bool TextParser::do_parse(const Record &record, Text &text) { if (record.type.tnf != RecordType::well_known_type) { diff --git a/features/nfc/source/nfc/ndef/common/URI.cpp b/features/nfc/source/nfc/ndef/common/URI.cpp index f1ecf984e7..adf8489c4f 100644 --- a/features/nfc/source/nfc/ndef/common/URI.cpp +++ b/features/nfc/source/nfc/ndef/common/URI.cpp @@ -129,6 +129,26 @@ bool URI::append_as_record(MessageBuilder &message_builder, bool is_last_record) return message_builder.append_record(type, payload, is_last_record); } +size_t URI::get_record_size() const +{ + if (!_uri) { + return 0; + } + + return MessageBuilder::compute_record_size( + Record( + RecordType( + RecordType::well_known_type, + uri_record_type_value + ), + RecordPayload(_uri, _uri_size), + RecordID(), + /* chunk */ false, + /* last record */ false + ) + ); +} + void URI::move_data(uint8_t *new_uri, size_t new_uri_size) { delete[] _uri;