Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MessageParser.h Source File

MessageParser.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_NDEF_MESSAGEPARSER_H_
00018 #define NFC_NDEF_MESSAGEPARSER_H_
00019 
00020 #include <stdlib.h>
00021 #include "platform/Span.h"
00022 
00023 namespace mbed {
00024 namespace nfc {
00025 namespace ndef {
00026 
00027 /** @addtogroup nfc
00028  * @{
00029  */
00030 
00031 // Forward declaration
00032 class Record;
00033 
00034 /**
00035  * Event driven NDEF Message parser
00036  */
00037 class MessageParser {
00038 public:
00039     /**
00040      * Error that can be reported by the parsing operation.
00041      */
00042     enum error_t {
00043         /**
00044          * The message doesn't start with a message start tag.
00045          */
00046         INVALID_MESSAGE_START,
00047 
00048         /**
00049          * There is not enough data left to pursue parsing of the message.
00050          */
00051         INSUFICIENT_DATA,
00052 
00053         /**
00054          * The type name of a record is invalid.
00055          */
00056         INVALID_TYPE_NAME_FORMAT,
00057 
00058         /**
00059          * An empty record is malformed.
00060          */
00061         INVALID_EMPTY_RECORD,
00062 
00063         /**
00064          * Record of unknown type embed a type length different than 0.
00065          */
00066         INVALID_UNKNOWN_TYPE_LENGTH,
00067 
00068         /**
00069          * Record of unchanged type contains a type.
00070          */
00071         INVALID_UNCHANGED_TYPE,
00072 
00073         /**
00074          * Chunk record encountered.
00075          */
00076         CHUNK_RECORD_NOT_SUPPORTED,
00077 
00078         /**
00079          * Message is not properly closed.
00080          */
00081         MISSING_MESSAGE_END,
00082 
00083         /**
00084          * Type is missing in a record expecting a type (well known type, media
00085          * type, absolute uri or external type).
00086          */
00087         MISSING_TYPE_VALUE
00088     };
00089 
00090     /**
00091      * Report parsing event to the application.
00092      */
00093     struct Delegate {
00094         /**
00095          * Invoked when parsing as started.
00096          */
00097         virtual void on_parsing_started() { }
00098 
00099         /**
00100          * Invoked when a record has been parsed.
00101          *
00102          * @param record The record obtained from parsing.
00103          */
00104         virtual void on_record_parsed(const Record &record) { }
00105 
00106         /**
00107          * Invoked when parsing is over.
00108          */
00109         virtual void on_parsing_terminated() { }
00110 
00111         /**
00112          * Invoked when an error is present in the message.
00113          * @param error The error present in the message.
00114          */
00115         virtual void on_parsing_error(error_t error) { }
00116 
00117     protected:
00118         /**
00119          * Protected non virtual destructor.
00120          * Delegate is not meant to be destroyed in a polymorphic manner.
00121          */
00122         ~Delegate() { }
00123     };
00124 
00125     /**
00126      * Construct a message parser.
00127      */
00128     MessageParser();
00129 
00130     /**
00131      * Set the handler that processes parsing events.
00132      * @param delegate The parsing event handler.
00133      */
00134     void set_delegate(Delegate *delegate);
00135 
00136     /**
00137      * Parse an NDEF Message.
00138      *
00139      * Records and errors are reported to the handler registered with
00140      * set_event_handler.
00141      *
00142      * @param data_buffer The data buffer that contains the NDEF message.
00143      */
00144     void parse(const Span<const uint8_t>  &data_buffer);
00145 
00146 private:
00147     struct parsing_state_t;
00148 
00149     // parser
00150     bool parse_record(parsing_state_t &it);
00151 
00152     static uint8_t compute_lengths_size(uint8_t header);
00153     static uint8_t extract_type_length(parsing_state_t &s);
00154     static uint32_t extract_payload_length(parsing_state_t &s, uint8_t header);
00155     static uint8_t extract_id_length(parsing_state_t &s, uint8_t header);
00156 
00157     // reporting
00158     void report_parsing_started();
00159     void report_record_parsed(const Record &record);
00160     void report_parsing_terminated();
00161     void report_parsing_error(error_t error, parsing_state_t &parsing_state);
00162 
00163     Delegate *_delegate;
00164 };
00165 /** @}*/
00166 } // namespace ndef
00167 } // namespace nfc
00168 } // namespace mbed
00169 
00170 
00171 #endif /* NFC_NDEF_MESSAGEPARSER_H_ */