NDEF: Move common types into the namespace ndef::common

pull/7822/head
Vincent Coubard 2018-08-24 17:35:55 +01:00 committed by Donatien Garnier
parent 3770cfb963
commit 684b3cad08
10 changed files with 67 additions and 48 deletions

View File

@ -24,6 +24,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
/** /**
@ -103,7 +104,7 @@ public:
* Append into a message builder * Append into a message builder
*/ */
bool append_as_record( bool append_as_record(
ndef::MessageBuilder &message_builder, MessageBuilder &message_builder,
bool is_last_record = false bool is_last_record = false
); );
@ -126,9 +127,9 @@ private:
/** /**
* Parse a Mime payload. * Parse a Mime payload.
*/ */
class MimeParser : public ndef::GenericRecordParser<MimeParser, Mime> { class MimeParser : public GenericRecordParser<MimeParser, Mime> {
public: public:
bool do_parse(const ndef::Record &record, Mime &mime); bool do_parse(const Record &record, Mime &mime);
}; };
/** /**
@ -136,6 +137,7 @@ public:
*/ */
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -26,6 +26,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
/** /**
@ -39,7 +40,7 @@ namespace common {
* Custom parsers can be added at runtime as well. * Custom parsers can be added at runtime as well.
*/ */
class SimpleMessageParser : class SimpleMessageParser :
ndef::MessageParser::Delegate, MessageParser::Delegate,
URIParser::Delegate, URIParser::Delegate,
TextParser::Delegate, TextParser::Delegate,
MimeParser::Delegate MimeParser::Delegate
@ -53,7 +54,7 @@ public:
* Invoked when an error is present in the message. * Invoked when an error is present in the message.
* @param error The error present in the message. * @param error The error present in the message.
*/ */
virtual void on_parsing_error(ndef::MessageParser::error_t error) { } virtual void on_parsing_error(MessageParser::error_t error) { }
/** /**
* Invoked when parsing as started. * Invoked when parsing as started.
@ -64,25 +65,25 @@ public:
* Invoked when a text element has been parsed. * Invoked when a text element has been parsed.
* @param text The text parsed. * @param text The text parsed.
*/ */
virtual void on_text_parsed(const Text &text, const ndef::RecordID &id) { } virtual void on_text_parsed(const Text &text, const RecordID &id) { }
/** /**
* Invoked when a text element has been parsed. * Invoked when a text element has been parsed.
* @param text The text parsed. * @param text The text parsed.
*/ */
virtual void on_uri_parsed(const URI &uri, const ndef::RecordID &id) { } virtual void on_uri_parsed(const URI &uri, const RecordID &id) { }
/** /**
* Invoked when a mime element has been parsed. * Invoked when a mime element has been parsed.
* @param mime The mime object parsed. * @param mime The mime object parsed.
*/ */
virtual void on_mime_parsed(const Mime &mime, const ndef::RecordID &id) { } virtual void on_mime_parsed(const Mime &mime, const RecordID &id) { }
/** /**
* Invoked when an unknown record has been parsed. * Invoked when an unknown record has been parsed.
* @param The record freshly parsed. * @param The record freshly parsed.
*/ */
virtual void on_unknown_record_parsed(const ndef::Record &record) { } virtual void on_unknown_record_parsed(const Record &record) { }
/** /**
* Invoked when parsing is over. * Invoked when parsing is over.
@ -118,49 +119,49 @@ public:
* Insert a new parser in the parser chain. * Insert a new parser in the parser chain.
* @param parser The parser to add in the parsing chain. * @param parser The parser to add in the parsing chain.
*/ */
void add_record_parser(ndef::RecordParser *parser); void add_record_parser(RecordParser *parser);
private: private:
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
/// Implementation of MessageParser::EventHandler /// Implementation of MessageParser::EventHandler
virtual void on_parsing_error(ndef::MessageParser::error_t error); virtual void on_parsing_error(MessageParser::error_t error);
virtual void on_parsing_started(); virtual void on_parsing_started();
virtual void on_record_parsed(const ndef::Record &record); virtual void on_record_parsed(const Record &record);
virtual void on_parsing_terminated(); virtual void on_parsing_terminated();
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
/// Implementation of URIParser::EventHandler /// Implementation of URIParser::EventHandler
virtual void on_record_parsed(const URI &uri, const ndef::RecordID &id); virtual void on_record_parsed(const URI &uri, const RecordID &id);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
/// Implementation of TextParser::EventHandler /// Implementation of TextParser::EventHandler
virtual void on_record_parsed(const Text &text, const ndef::RecordID &id); virtual void on_record_parsed(const Text &text, const RecordID &id);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
/// Implementation of MimeParser::EventHandler /// Implementation of MimeParser::EventHandler
virtual void on_record_parsed(const Mime &mime, const ndef::RecordID &id); virtual void on_record_parsed(const Mime &mime, const RecordID &id);
ndef::MessageParser _message_parser; MessageParser _message_parser;
ndef::RecordParserChain _record_parser_chain; RecordParserChain _record_parser_chain;
URIParser _uri_parser; URIParser _uri_parser;
TextParser _text_parser; TextParser _text_parser;
MimeParser _mime_parser; MimeParser _mime_parser;
Delegate *_delegate; Delegate *_delegate;
}; };
/** /**
* @} * @}
*/ */
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -24,6 +24,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
/** /**
@ -120,7 +121,7 @@ public:
* Append into a message builder * Append into a message builder
*/ */
bool append_as_record( bool append_as_record(
ndef::MessageBuilder &message_builder, MessageBuilder &message_builder,
bool is_last_record = false bool is_last_record = false
); );
@ -136,9 +137,9 @@ private:
/** /**
* Parse a Text. * Parse a Text.
*/ */
class TextParser : public ndef::GenericRecordParser<TextParser, Text> { class TextParser : public GenericRecordParser<TextParser, Text> {
public: public:
virtual bool do_parse(const ndef::Record &record, Text &text); virtual bool do_parse(const Record &record, Text &text);
}; };
/** /**
@ -146,6 +147,7 @@ public:
*/ */
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -26,6 +26,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
/** /**
@ -145,7 +146,7 @@ public:
* Append into a message builder * Append into a message builder
*/ */
bool append_as_record( bool append_as_record(
ndef::MessageBuilder &message_builder, MessageBuilder &message_builder,
bool is_last_record = false bool is_last_record = false
); );
@ -181,9 +182,9 @@ private:
/** /**
* Parser of a URI. * Parser of a URI.
*/ */
class URIParser : public ndef::GenericRecordParser<URIParser, URI> { class URIParser : public GenericRecordParser<URIParser, URI> {
public: public:
bool do_parse(const ndef::Record &record, URI &uri); bool do_parse(const Record &record, URI &uri);
}; };
/** /**
@ -191,6 +192,7 @@ public:
*/ */
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -21,6 +21,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
/** /**
@ -61,6 +62,7 @@ Span<const uint8_t> span_from_cstr(const char *cstr);
*/ */
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -20,6 +20,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
Mime::Mime() : Mime::Mime() :
@ -92,12 +93,12 @@ Span<const uint8_t> Mime::get_mime_content() const
} }
bool Mime::append_as_record( bool Mime::append_as_record(
ndef::MessageBuilder &message_builder, MessageBuilder &message_builder,
bool is_last_record bool is_last_record
) { ) {
return message_builder.append_record( return message_builder.append_record(
ndef::RecordType( RecordType(
ndef::RecordType::media_type, RecordType::media_type,
get_mime_type() get_mime_type()
), ),
get_mime_content(), get_mime_content(),
@ -121,9 +122,9 @@ size_t Mime::mime_size() const
return _type_size + _content_size; return _type_size + _content_size;
} }
bool MimeParser::do_parse(const ndef::Record &record, Mime &mime) bool MimeParser::do_parse(const Record &record, Mime &mime)
{ {
if (record.type.tnf != ndef::RecordType::media_type) { if (record.type.tnf != RecordType::media_type) {
return false; return false;
} }
@ -149,5 +150,6 @@ bool MimeParser::do_parse(const ndef::Record &record, Mime &mime)
} }
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -18,6 +18,7 @@
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
SimpleMessageParser::SimpleMessageParser() : SimpleMessageParser::SimpleMessageParser() :
@ -50,12 +51,12 @@ void SimpleMessageParser::parse(const Span<const uint8_t> &data_buffer)
_message_parser.parse(data_buffer); _message_parser.parse(data_buffer);
} }
void SimpleMessageParser::add_record_parser(ndef::RecordParser *parser) void SimpleMessageParser::add_record_parser(RecordParser *parser)
{ {
_record_parser_chain.set_next_parser(parser); _record_parser_chain.set_next_parser(parser);
} }
void SimpleMessageParser::on_parsing_error(ndef::MessageParser::error_t error) void SimpleMessageParser::on_parsing_error(MessageParser::error_t error)
{ {
if (_delegate) { if (_delegate) {
_delegate->on_parsing_error(error); _delegate->on_parsing_error(error);
@ -69,7 +70,7 @@ void SimpleMessageParser::on_parsing_started()
} }
} }
void SimpleMessageParser::on_record_parsed(const ndef::Record &record) void SimpleMessageParser::on_record_parsed(const Record &record)
{ {
bool parsed = _record_parser_chain.parse(record); bool parsed = _record_parser_chain.parse(record);
@ -87,7 +88,7 @@ void SimpleMessageParser::on_parsing_terminated()
void SimpleMessageParser::on_record_parsed( void SimpleMessageParser::on_record_parsed(
const URI &uri, const URI &uri,
const ndef::RecordID &id const RecordID &id
) { ) {
if (_delegate) { if (_delegate) {
_delegate->on_uri_parsed(uri, id); _delegate->on_uri_parsed(uri, id);
@ -96,7 +97,7 @@ void SimpleMessageParser::on_record_parsed(
void SimpleMessageParser::on_record_parsed( void SimpleMessageParser::on_record_parsed(
const Text &text, const Text &text,
const ndef::RecordID &id const RecordID &id
) { ) {
if (_delegate) { if (_delegate) {
_delegate->on_text_parsed(text, id); _delegate->on_text_parsed(text, id);
@ -105,7 +106,7 @@ void SimpleMessageParser::on_record_parsed(
void SimpleMessageParser::on_record_parsed( void SimpleMessageParser::on_record_parsed(
const Mime &mime, const Mime &mime,
const ndef::RecordID &id const RecordID &id
) { ) {
if (_delegate) { if (_delegate) {
_delegate->on_mime_parsed(mime, id); _delegate->on_mime_parsed(mime, id);
@ -113,6 +114,7 @@ void SimpleMessageParser::on_record_parsed(
} }
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -29,6 +29,7 @@ static const uint8_t text_record_type_value[] = { 'T' };
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
Text::Text() : Text::Text() :
@ -129,23 +130,23 @@ void Text::move_data(uint8_t *text, size_t size) {
} }
bool Text::append_as_record( bool Text::append_as_record(
ndef::MessageBuilder &message_builder, MessageBuilder &message_builder,
bool is_last_record bool is_last_record
) { ) {
// Build the record type // Build the record type
ndef::RecordType type( RecordType type(
ndef::RecordType::well_known_type, RecordType::well_known_type,
text_record_type_value text_record_type_value
); );
// build the record payload // build the record payload
ndef::RecordPayload payload(_text_record, _text_record_size); RecordPayload payload(_text_record, _text_record_size);
return message_builder.append_record(type, payload, is_last_record); return message_builder.append_record(type, payload, is_last_record);
} }
bool TextParser::do_parse(const ndef::Record &record, Text &text) bool TextParser::do_parse(const Record &record, Text &text)
{ {
if (record.type.tnf != ndef::RecordType::well_known_type) { if (record.type.tnf != RecordType::well_known_type) {
return false; return false;
} }
@ -167,5 +168,6 @@ bool TextParser::do_parse(const ndef::Record &record, Text &text)
} }
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -26,6 +26,7 @@ static const uint8_t uri_record_type_value[] = { 'U' } ;
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
URI::URI() : URI::URI() :
@ -107,20 +108,20 @@ Span<const uint8_t> URI::get_uri_field() const
); );
} }
bool URI::append_as_record(ndef::MessageBuilder &message_builder, bool is_last_record) bool URI::append_as_record(MessageBuilder &message_builder, bool is_last_record)
{ {
if (!_uri) { if (!_uri) {
return false; return false;
} }
// Build the record type // Build the record type
ndef::RecordType type( RecordType type(
ndef::RecordType::well_known_type, RecordType::well_known_type,
uri_record_type_value uri_record_type_value
); );
// build the record payload // build the record payload
ndef::RecordPayload payload(_uri, _uri_size); RecordPayload payload(_uri, _uri_size);
return message_builder.append_record(type, payload, is_last_record); return message_builder.append_record(type, payload, is_last_record);
} }
@ -132,9 +133,9 @@ void URI::move_data(uint8_t *new_uri, size_t new_uri_size)
_uri_size = new_uri_size; _uri_size = new_uri_size;
} }
bool URIParser::do_parse(const ndef::Record &record, URI &uri) bool URIParser::do_parse(const Record &record, URI &uri)
{ {
if (record.type.tnf != ndef::RecordType::well_known_type) { if (record.type.tnf != RecordType::well_known_type) {
return false; return false;
} }
@ -156,6 +157,7 @@ bool URIParser::do_parse(const ndef::Record &record, URI &uri)
} }
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed

View File

@ -14,12 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
#include "nfc/common/util.h" #include "nfc/ndef/common/util.h"
#include <cstring> #include <cstring>
namespace mbed { namespace mbed {
namespace nfc { namespace nfc {
namespace ndef {
namespace common { namespace common {
Span<const uint8_t> span_from_cstr(const char *cstr) Span<const uint8_t> span_from_cstr(const char *cstr)
@ -32,6 +33,7 @@ Span<const uint8_t> span_from_cstr(const char *cstr)
*/ */
} // namespace common } // namespace common
} // namespace ndef
} // namespace nfc } // namespace nfc
} // namespace mbed } // namespace mbed