Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SimpleMessageParser.h Source File

SimpleMessageParser.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef NFC_COMMON_SIMPLEMESSAGEPARSER_H_
00018 #define NFC_COMMON_SIMPLEMESSAGEPARSER_H_
00019 
00020 #include "platform/Span.h"
00021 
00022 #include "nfc/ndef/MessageParser.h"
00023 #include "nfc/ndef/RecordParser.h"
00024 #include "nfc/ndef/common/URI.h"
00025 #include "nfc/ndef/common/Text.h"
00026 #include "nfc/ndef/common/Mime.h"
00027 
00028 namespace mbed {
00029 namespace nfc {
00030 namespace ndef {
00031 namespace common {
00032 
00033 /** @addtogroup nfc
00034  * @{
00035  */
00036 
00037 /**
00038  * Basic message parser that aggregates URIParser, TextParser and MimeParser.
00039  *
00040  * Custom parsers can be added at runtime as well.
00041  */
00042 class SimpleMessageParser :
00043     MessageParser::Delegate,
00044     URIParser::Delegate,
00045     TextParser::Delegate,
00046     MimeParser::Delegate {
00047 public:
00048     /**
00049      * Delegate invoked when the parser raise an event.
00050      */
00051     struct Delegate {
00052         /**
00053          * Invoked when an error is present in the message.
00054          * @param error The error present in the message.
00055          */
00056         virtual void on_parsing_error(MessageParser::error_t error) { }
00057 
00058         /**
00059          * Invoked when parsing as started.
00060          */
00061         virtual void on_parsing_started() { }
00062 
00063         /**
00064          * Invoked when a text element has been parsed.
00065          *
00066          * @param text The text parsed.
00067          * @param id The RecordId of the text object.
00068          */
00069         virtual void on_text_parsed(const Text &text, const RecordID  &id) { }
00070 
00071         /**
00072          * Invoked when a text element has been parsed.
00073          *
00074          * @param uri The uri parsed.
00075          * @param id The RecordId of the uri object.
00076          */
00077         virtual void on_uri_parsed(const URI &uri, const RecordID  &id) { }
00078 
00079         /**
00080          * Invoked when a mime element has been parsed.
00081          *
00082          * @param mime The mime object parsed.
00083          * @param id The RecordId of the mime object.
00084          */
00085         virtual void on_mime_parsed(const Mime &mime, const RecordID  &id) { }
00086 
00087         /**
00088          * Invoked when an unknown record has been parsed.
00089          * @param record The record freshly parsed.
00090          */
00091         virtual void on_unknown_record_parsed(const Record &record) { }
00092 
00093         /**
00094          * Invoked when parsing is over.
00095          */
00096         virtual void on_parsing_terminated() { }
00097 
00098     protected:
00099         ~Delegate() { }
00100     };
00101 
00102     /**
00103      * Construct a new CommonMessageParser.
00104      */
00105     SimpleMessageParser();
00106 
00107     /**
00108      * Set the handler that processes parsing events.
00109      * @param delegate The parsing event handler.
00110      */
00111     void set_delegate(Delegate *delegate);
00112 
00113     /**
00114      * Parse an NDEF Message.
00115      *
00116      * Records and errors are reported to the handler registered with
00117      * set_event_handler.
00118      *
00119      * @param data_buffer The data buffer that contains the NDEF message.
00120      */
00121     void parse(const Span<const uint8_t>  &data_buffer);
00122 
00123     /**
00124      * Insert a new parser in the parser chain.
00125      * @param parser The parser to add in the parsing chain.
00126      */
00127     void add_record_parser(RecordParser *parser);
00128 
00129 private:
00130     ////////////////////////////////////////////////////////////////////////////
00131     /// Implementation of MessageParser::EventHandler
00132 
00133     virtual void on_parsing_error(MessageParser::error_t error);
00134 
00135     virtual void on_parsing_started();
00136 
00137     virtual void on_record_parsed(const Record &record);
00138 
00139     virtual void on_parsing_terminated();
00140 
00141     ////////////////////////////////////////////////////////////////////////////
00142     /// Implementation of URIParser::EventHandler
00143 
00144     virtual void on_record_parsed(const URI &uri, const RecordID  &id);
00145 
00146     ////////////////////////////////////////////////////////////////////////////
00147     /// Implementation of TextParser::EventHandler
00148 
00149     virtual void on_record_parsed(const Text &text, const RecordID  &id);
00150 
00151     ////////////////////////////////////////////////////////////////////////////
00152     /// Implementation of MimeParser::EventHandler
00153 
00154     virtual void on_record_parsed(const Mime &mime, const RecordID  &id);
00155 
00156     MessageParser _message_parser;
00157     RecordParserChain _record_parser_chain;
00158     URIParser _uri_parser;
00159     TextParser _text_parser;
00160     MimeParser _mime_parser;
00161     Delegate *_delegate;
00162 };
00163 /** @}*/
00164 } // namespace common
00165 } // namespace ndef
00166 } // namespace nfc
00167 } // namespace mbed
00168 
00169 #endif /* NFC_COMMON_SIMPLEMESSAGEPARSER_H_ */
00170