Mistake on this page?
Report an issue in GitHub or email us
MessageParser.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NFC_NDEF_MESSAGEPARSER_H_
18 #define NFC_NDEF_MESSAGEPARSER_H_
19 
20 #include <stdlib.h>
21 #include "platform/Span.h"
22 
23 namespace mbed {
24 namespace nfc {
25 namespace ndef {
26 
27 /** @addtogroup nfc
28  * @{
29  */
30 
31 // Forward declaration
32 class Record;
33 
34 /**
35  * Event driven NDEF Message parser
36  */
38 public:
39  /**
40  * Error that can be reported by the parsing operation.
41  */
42  enum error_t {
43  /**
44  * The message doesn't start with a message start tag.
45  */
47 
48  /**
49  * There is not enough data left to pursue parsing of the message.
50  */
52 
53  /**
54  * The type name of a record is invalid.
55  */
57 
58  /**
59  * An empty record is malformed.
60  */
62 
63  /**
64  * Record of unknown type embed a type length different than 0.
65  */
67 
68  /**
69  * Record of unchanged type contains a type.
70  */
72 
73  /**
74  * Chunk record encountered.
75  */
77 
78  /**
79  * Message is not properly closed.
80  */
82 
83  /**
84  * Type is missing in a record expecting a type (well known type, media
85  * type, absolute uri or external type).
86  */
88  };
89 
90  /**
91  * Report parsing event to the application.
92  */
93  struct Delegate {
94  /**
95  * Invoked when parsing as started.
96  */
97  virtual void on_parsing_started() { }
98 
99  /**
100  * Invoked when a record has been parsed.
101  *
102  * @param record The record obtained from parsing.
103  */
104  virtual void on_record_parsed(const Record &record) { }
105 
106  /**
107  * Invoked when parsing is over.
108  */
109  virtual void on_parsing_terminated() { }
110 
111  /**
112  * Invoked when an error is present in the message.
113  * @param error The error present in the message.
114  */
115  virtual void on_parsing_error(error_t error) { }
116 
117  protected:
118  /**
119  * Protected non virtual destructor.
120  * Delegate is not meant to be destroyed in a polymorphic manner.
121  */
122  ~Delegate() { }
123  };
124 
125  /**
126  * Construct a message parser.
127  */
128  MessageParser();
129 
130  /**
131  * Set the handler that processes parsing events.
132  * @param delegate The parsing event handler.
133  */
134  void set_delegate(Delegate *delegate);
135 
136  /**
137  * Parse an NDEF Message.
138  *
139  * Records and errors are reported to the handler registered with
140  * set_event_handler.
141  *
142  * @param data_buffer The data buffer that contains the NDEF message.
143  */
144  void parse(const Span<const uint8_t> &data_buffer);
145 
146 private:
147  struct parsing_state_t;
148 
149  // parser
150  bool parse_record(parsing_state_t &it);
151 
152  static uint8_t compute_lengths_size(uint8_t header);
153  static uint8_t extract_type_length(parsing_state_t &s);
154  static uint32_t extract_payload_length(parsing_state_t &s, uint8_t header);
155  static uint8_t extract_id_length(parsing_state_t &s, uint8_t header);
156 
157  // reporting
158  void report_parsing_started();
159  void report_record_parsed(const Record &record);
160  void report_parsing_terminated();
161  void report_parsing_error(error_t error, parsing_state_t &parsing_state);
162 
163  Delegate *_delegate;
164 };
165 /** @}*/
166 } // namespace ndef
167 } // namespace nfc
168 } // namespace mbed
169 
170 
171 #endif /* NFC_NDEF_MESSAGEPARSER_H_ */
The message doesn&#39;t start with a message start tag.
Definition: MessageParser.h:46
The type name of a record is invalid.
Definition: MessageParser.h:56
MBED_NORETURN void error(const char *format,...) MBED_PRINTF(1
To generate a fatal compile-time error, you can use the pre-processor error directive.
MessageParser()
Construct a message parser.
void parse(const Span< const uint8_t > &data_buffer)
Parse an NDEF Message.
Report parsing event to the application.
Definition: MessageParser.h:93
error_t
Error that can be reported by the parsing operation.
Definition: MessageParser.h:42
Record of unchanged type contains a type.
Definition: MessageParser.h:71
There is not enough data left to pursue parsing of the message.
Definition: MessageParser.h:51
Event driven NDEF Message parser.
Definition: MessageParser.h:37
Represent a record.
Definition: Record.h:148
Type is missing in a record expecting a type (well known type, media type, absolute uri or external t...
Definition: MessageParser.h:87
void set_delegate(Delegate *delegate)
Set the handler that processes parsing events.
virtual void on_parsing_error(error_t error)
Invoked when an error is present in the message.
virtual void on_record_parsed(const Record &record)
Invoked when a record has been parsed.
virtual void on_parsing_terminated()
Invoked when parsing is over.
virtual void on_parsing_started()
Invoked when parsing as started.
Definition: MessageParser.h:97
~Delegate()
Protected non virtual destructor.
Record of unknown type embed a type length different than 0.
Definition: MessageParser.h:66
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.