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