Mistake on this page?
Report an issue in GitHub or email us
MessageBuilder.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_MESSAGEBUILDER_H_
18 #define NFC_NDEF_MESSAGEBUILDER_H_
19 
20 #include <stdint.h>
21 
22 #include "platform/Span.h"
23 
24 #include "nfc/ndef/Record.h"
25 
26 namespace mbed {
27 namespace nfc {
28 namespace ndef {
29 
30 /** @addtogroup nfc
31  * @{
32  */
33 
34 /**
35  * Construct a NDEF Message.
36  */
38 
39 public:
40  /**
41  * Build a record payload.
42  */
43  struct PayloadBuilder {
44  /**
45  * Return the size of the payload built by this object.
46  *
47  * @return The size of the payload.
48  */
49  virtual size_t size() const = 0;
50 
51  /**
52  * Build the payload in a buffer that has the required size.
53  *
54  * @param buffer The buffer used to construct the payload.
55  */
56  virtual void build(const Span<uint8_t> &buffer) const = 0;
57 
58  protected:
59  /**
60  * Non virtual destructor.
61  */
63  };
64 
65  /**
66  * Create a new MessageBuilder that can be used to construct valid NDEF
67  * messages.
68  *
69  * @param buffer The data buffer that will contain the NDEF message.
70  */
71  MessageBuilder(const Span<uint8_t> &buffer);
72 
73  /**
74  * Append a new record to the message being built.
75  *
76  * @param type The type of the record to insert.
77  * @param payload The payload of the record (optional).
78  * @param is_last_record true if the record to insert is the last record of
79  * the payload or false otherwise.
80  *
81  * @return true if the record has been successfully inserted or false
82  * otherwise.
83  *
84  * @note insertion can fail if the message is already complete or if the
85  * size remaining in the message buffer is not large enough to makes the
86  * record inserted fit.
87  */
88  bool append_record(
89  const RecordType &type,
90  const RecordPayload &payload = RecordPayload(),
91  bool is_last_record = false
92  );
93 
94  /**
95  * Append a new record to the message being built.
96  *
97  * @param type The type of the record to insert.
98  * @param builder The builder of the payload.
99  * @param is_last_record true if the record to insert is the last record of
100  * the payload or false otherwise.
101  *
102  * @return true if the record has been successfully inserted or false
103  * otherwise.
104  *
105  * @note insertion can fail if the message is already complete or if the
106  * size remaining in the message buffer is not large enough to makes the
107  * record inserted fit.
108  */
109  bool append_record(
110  const RecordType &type,
111  const PayloadBuilder &builder,
112  bool is_last_record = false
113  );
114 
115  /**
116  * Append a new record to the message being built.
117  *
118  * @param record The record to insert.
119  * @param builder The builder that will construct the payload.
120  *
121  * @return true if the record has been successfully inserted or false otherwise.
122  *
123  * @note insertion can fail if the message is already complete or if the
124  * size remaining in the message buffer is not large enough to makes the
125  * record inserted fit.
126  */
127  bool append_record(
128  const Record &record,
129  const PayloadBuilder *builder = NULL
130  );
131 
132  /**
133  * Compute the size of a record.
134  *
135  * @param record The record used to compute the size.
136  * @param builder The payload builder if any.
137  *
138  * @return The size of the payload for the record in input.
139  */
140  static size_t compute_record_size(
141  const Record &record,
142  const PayloadBuilder *builder = NULL
143  );
144 
145  /**
146  * Reset the builder state.
147  */
148  void reset();
149 
150  /**
151  * Reset the builder state and assign a new buffer to it.
152  */
153  void reset(const Span<uint8_t> &buffer);
154 
155  /**
156  * Return true if the message stored is complete and false otherwise.
157  *
158  * @return true if the message is complete or false.
159  */
160  bool is_message_complete() const;
161 
162  /**
163  * Return the buffer storing the data if the message is complete or an empty
164  * buffer if the message is not complete.
165  *
166  * @return The message built.
167  */
169 
170 private:
171  // append fields
172  void append_header(const Record &record, const PayloadBuilder *);
173  void append_type_length(const Record &record);
174  void append_payload_length(const Record &, const PayloadBuilder *);
175  void append_id_length(const Record &);
176  void append_type(const Record &);
177  void append_id(const Record &);
178  void append_payload(const Record &, const PayloadBuilder *);
179 
180  // helpers
181  static bool is_short_payload(const Record &record, const PayloadBuilder *);
182  static size_t get_payload_size(const Record &, const PayloadBuilder *);
183 
184  // builder state.
185  Span<uint8_t> _message_buffer;
186  size_t _position;
187  bool _message_started;
188  bool _message_ended;
189  bool _in_chunk;
190 };
191 /** @}*/
192 } // namespace ndef
193 } // namespace nfc
194 } // namespace mbed
195 
196 #endif /* NFC_NDEF_MESSAGEBUILDER_H_ */
Construct a NDEF Message.
MessageBuilder(const Span< uint8_t > &buffer)
Create a new MessageBuilder that can be used to construct valid NDEF messages.
bool append_record(const RecordType &type, const RecordPayload &payload=RecordPayload(), bool is_last_record=false)
Append a new record to the message being built.
virtual void build(const Span< uint8_t > &buffer) const =0
Build the payload in a buffer that has the required size.
void reset()
Reset the builder state.
static size_t compute_record_size(const Record &record, const PayloadBuilder *builder=NULL)
Compute the size of a record.
Span< const uint8_t > get_message() const
Return the buffer storing the data if the message is complete or an empty buffer if the message is no...
Represent a record.
Definition: Record.h:148
virtual size_t size() const =0
Return the size of the payload built by this object.
Span< const uint8_t > RecordPayload
Definition of a Record payload.
Definition: Record.h:136
Encode a record type.
Definition: Record.h:52
bool is_message_complete() const
Return true if the message stored is complete and false otherwise.
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.