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