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