Mbed OS Reference
Loading...
Searching...
No Matches
SimpleMessageParser.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_COMMON_SIMPLEMESSAGEPARSER_H_
19#define NFC_COMMON_SIMPLEMESSAGEPARSER_H_
20
21#include "platform/Span.h"
22
23#include "nfc/ndef/MessageParser.h"
24#include "nfc/ndef/RecordParser.h"
25#include "nfc/ndef/common/URI.h"
26#include "nfc/ndef/common/Text.h"
27#include "nfc/ndef/common/Mime.h"
28
29namespace mbed {
30namespace nfc {
31namespace ndef {
32namespace common {
33
34/** @addtogroup nfc
35 * @{
36 */
37
38/**
39 * Basic message parser that aggregates URIParser, TextParser and MimeParser.
40 *
41 * Custom parsers can be added at runtime as well.
42 */
45 URIParser::Delegate,
46 TextParser::Delegate,
47 MimeParser::Delegate {
48public:
49 /**
50 * Delegate invoked when the parser raise an event.
51 */
52 struct Delegate {
53 /**
54 * Invoked when an error is present in the message.
55 * @param error The error present in the message.
56 */
58
59 /**
60 * Invoked when parsing as started.
61 */
62 virtual void on_parsing_started() { }
63
64 /**
65 * Invoked when a text element has been parsed.
66 *
67 * @param text The text parsed.
68 * @param id The RecordId of the text object.
69 */
70 virtual void on_text_parsed(const Text &text, const RecordID &id) { }
71
72 /**
73 * Invoked when a text element has been parsed.
74 *
75 * @param uri The uri parsed.
76 * @param id The RecordId of the uri object.
77 */
78 virtual void on_uri_parsed(const URI &uri, const RecordID &id) { }
79
80 /**
81 * Invoked when a mime element has been parsed.
82 *
83 * @param mime The mime object parsed.
84 * @param id The RecordId of the mime object.
85 */
86 virtual void on_mime_parsed(const Mime &mime, const RecordID &id) { }
87
88 /**
89 * Invoked when an unknown record has been parsed.
90 * @param record The record freshly parsed.
91 */
92 virtual void on_unknown_record_parsed(const Record &record) { }
93
94 /**
95 * Invoked when parsing is over.
96 */
97 virtual void on_parsing_terminated() { }
98
99 protected:
100 ~Delegate() { }
101 };
102
103 /**
104 * Construct a new CommonMessageParser.
105 */
107
108 /**
109 * Set the handler that processes parsing events.
110 * @param delegate The parsing event handler.
111 */
112 void set_delegate(Delegate *delegate);
113
114 /**
115 * Parse an NDEF Message.
116 *
117 * Records and errors are reported to the handler registered with
118 * set_event_handler.
119 *
120 * @param data_buffer The data buffer that contains the NDEF message.
121 */
122 void parse(const Span<const uint8_t> &data_buffer);
123
124 /**
125 * Insert a new parser in the parser chain.
126 * @param parser The parser to add in the parsing chain.
127 */
129
130private:
131 ////////////////////////////////////////////////////////////////////////////
132 /// Implementation of MessageParser::EventHandler
133
134 virtual void on_parsing_error(MessageParser::error_t error);
135
136 virtual void on_parsing_started();
137
138 virtual void on_record_parsed(const Record &record);
139
140 virtual void on_parsing_terminated();
141
142 ////////////////////////////////////////////////////////////////////////////
143 /// Implementation of URIParser::EventHandler
144
145 virtual void on_record_parsed(const URI &uri, const RecordID &id);
146
147 ////////////////////////////////////////////////////////////////////////////
148 /// Implementation of TextParser::EventHandler
149
150 virtual void on_record_parsed(const Text &text, const RecordID &id);
151
152 ////////////////////////////////////////////////////////////////////////////
153 /// Implementation of MimeParser::EventHandler
154
155 virtual void on_record_parsed(const Mime &mime, const RecordID &id);
156
157 MessageParser _message_parser;
158 RecordParserChain _record_parser_chain;
159 URIParser _uri_parser;
160 TextParser _text_parser;
161 MimeParser _mime_parser;
162 Delegate *_delegate;
163};
164/** @}*/
165} // namespace common
166} // namespace ndef
167} // namespace nfc
168} // namespace mbed
169
170#endif /* NFC_COMMON_SIMPLEMESSAGEPARSER_H_ */
171
Event driven NDEF Message parser.
Definition: MessageParser.h:38
error_t
Error that can be reported by the parsing operation.
Definition: MessageParser.h:43
Represent a mime object.
Definition: Mime.h:41
Parse a Mime payload.
Definition: Mime.h:142
Basic message parser that aggregates URIParser, TextParser and MimeParser.
SimpleMessageParser()
Construct a new CommonMessageParser.
void add_record_parser(RecordParser *parser)
Insert a new parser in the parser chain.
void parse(const Span< const uint8_t > &data_buffer)
Parse an NDEF Message.
void set_delegate(Delegate *delegate)
Set the handler that processes parsing events.
Represent the well known type text.
Definition: Text.h:41
Model the well known type URI.
Definition: URI.h:42
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.
Report parsing event to the application.
Definition: MessageParser.h:94
Represent a record.
Definition: Record.h:149
Delegate invoked when the parser raise an event.
virtual void on_uri_parsed(const URI &uri, const RecordID &id)
Invoked when a text element has been parsed.
virtual void on_unknown_record_parsed(const Record &record)
Invoked when an unknown record has been parsed.
virtual void on_parsing_terminated()
Invoked when parsing is over.
virtual void on_mime_parsed(const Mime &mime, const RecordID &id)
Invoked when a mime element has been parsed.
virtual void on_parsing_error(MessageParser::error_t error)
Invoked when an error is present in the message.
virtual void on_parsing_started()
Invoked when parsing as started.
virtual void on_text_parsed(const Text &text, const RecordID &id)
Invoked when a text element has been parsed.