Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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