Mbed OS Reference
Loading...
Searching...
No Matches
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
24namespace mbed {
25namespace nfc {
26namespace ndef {
27
28/** @addtogroup nfc
29 * @{
30 */
31
32// Forward declaration
33class Record;
34
35/**
36 * Event driven NDEF Message parser
37 */
39public:
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 */
117
118 protected:
119 /**
120 * Protected non virtual destructor.
121 * Delegate is not meant to be destroyed in a polymorphic manner.
122 */
124 };
125
126 /**
127 * Construct a message parser.
128 */
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
147private:
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_ */
Event driven NDEF Message parser.
Definition: MessageParser.h:38
void parse(const Span< const uint8_t > &data_buffer)
Parse an NDEF Message.
MessageParser()
Construct a message parser.
error_t
Error that can be reported by the parsing operation.
Definition: MessageParser.h:43
@ MISSING_MESSAGE_END
Message is not properly closed.
Definition: MessageParser.h:82
@ INSUFICIENT_DATA
There is not enough data left to pursue parsing of the message.
Definition: MessageParser.h:52
@ INVALID_EMPTY_RECORD
An empty record is malformed.
Definition: MessageParser.h:62
@ CHUNK_RECORD_NOT_SUPPORTED
Chunk record encountered.
Definition: MessageParser.h:77
@ MISSING_TYPE_VALUE
Type is missing in a record expecting a type (well known type, media type, absolute uri or external t...
Definition: MessageParser.h:88
@ INVALID_UNCHANGED_TYPE
Record of unchanged type contains a type.
Definition: MessageParser.h:72
@ INVALID_TYPE_NAME_FORMAT
The type name of a record is invalid.
Definition: MessageParser.h:57
@ INVALID_UNKNOWN_TYPE_LENGTH
Record of unknown type embed a type length different than 0.
Definition: MessageParser.h:67
@ INVALID_MESSAGE_START
The message doesn't start with a message start tag.
Definition: MessageParser.h:47
void set_delegate(Delegate *delegate)
Set the handler that processes parsing events.
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.
Nonowning view to a sequence of contiguous elements.
Definition: Span.h:215
Report parsing event to the application.
Definition: MessageParser.h:94
virtual void on_parsing_terminated()
Invoked when parsing is over.
virtual void on_parsing_started()
Invoked when parsing as started.
Definition: MessageParser.h:98
virtual void on_record_parsed(const Record &record)
Invoked when a record has been parsed.
~Delegate()
Protected non virtual destructor.
virtual void on_parsing_error(error_t error)
Invoked when an error is present in the message.
Represent a record.
Definition: Record.h:149