M5Unit-NFC 0.1.0 git rev:93745b5
Loading...
Searching...
No Matches
ndef_record.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_UNIFIED_NFC_NDEF_NDEF_RECORD_HPP
11#define M5_UNIT_UNIFIED_NFC_NDEF_NDEF_RECORD_HPP
12
13#include "ndef.hpp"
14#include <vector>
15#include <string>
16
17namespace m5 {
18namespace nfc {
19namespace ndef {
20
21class TLV;
22
27enum class TNF : uint8_t {
28 Empty,
29 Wellknown,
30 MIMEMedia,
31 URI,
32 External,
33 Unknown,
34 Unchanged,
35 Reserved,
36};
37
42struct Attribute {
45 static constexpr uint8_t MB{0x80};
46 static constexpr uint8_t ME{0x40};
47 static constexpr uint8_t CF{0x20};
48 static constexpr uint8_t SR{0x10};
49 static constexpr uint8_t IL{0x08};
50 static constexpr uint8_t TNF_MASK{0x07};
52
55
59 inline bool messageBegin() const
60 {
61 return value & MB;
62 }
67 inline bool messageEnd() const
68 {
69 return value & ME;
70 }
75 inline bool chunk() const
76 {
77 return value & CF;
78 }
83 inline bool shortRecord() const
84 {
85 return value & SR;
86 }
91 inline bool idLength() const
92 {
93 return value & IL;
94 }
99 inline TNF tnf() const
100 {
101 // TNF_MASK (0x07) ensures result is always a valid TNF (0..7)
102 return static_cast<TNF>(value & TNF_MASK);
103 }
105
108
112 inline void messageBegin(const bool b)
113 {
114 value = (value & ~MB) | (b ? MB : 0);
115 }
120 inline void messageEnd(const bool b)
121 {
122 value = (value & ~ME) | (b ? ME : 0);
123 }
128 inline void chunk(const bool b)
129 {
130 value = (value & ~CF) | (b ? CF : 0);
131 }
136 inline void shortRecord(const bool b)
137 {
138 value = (value & ~SR) | (b ? SR : 0);
139 }
144 inline void idLength(const bool b)
145 {
146 value = (value & ~IL) | (b ? IL : 0);
147 }
152 inline void tnf(const TNF t)
153 {
154 value = (value & ~TNF_MASK) | (m5::stl::to_underlying(t) & TNF_MASK);
155 }
157
158 uint8_t value{};
159};
160
166class Record {
167public:
168 Record() noexcept : Record(TNF::Wellknown)
169 {
170 }
171
172 explicit Record(const TNF tnf) noexcept
173 {
174 _attr.tnf(tnf);
175 }
176 ~Record()
177 {
178 }
179
182
187 {
188 return _attr;
189 }
194 inline const Attribute& attribute() const
195 {
196 return _attr;
197 }
202 inline TNF tnf() const
203 {
204 return _attr.tnf();
205 }
207
239
243 inline const char* type() const
244 {
245 return _type.c_str();
246 }
251 inline void setType(const char* s)
252 {
253 _type = s;
254 }
256
259
263 inline uint32_t identifierSize() const
264 {
265 return _id.size();
266 }
271 inline const uint8_t* identifier() const
272 {
273 return !_id.empty() ? _id.data() : nullptr;
274 }
280 inline void setIdentifier(const uint8_t* id, const uint32_t len)
281 {
282 if (_attr.tnf() != TNF::Empty && id && len) {
283 _id = std::vector<uint8_t>(id, id + len);
284 _attr.idLength(true);
285 } else {
286 _id.clear();
287 _attr.idLength(false);
288 }
289 }
291 inline void clearIdentifier()
292 {
293 setIdentifier(nullptr, 0);
294 }
296
299
303 std::string payloadAsString() const;
304
309 inline uint32_t payloadSize() const
310 {
311 return _payload.size();
312 }
317 inline const uint8_t* payload() const
318 {
319 return !_payload.empty() ? _payload.data() : nullptr;
320 }
326 inline void setPayload(const uint8_t* data, const uint32_t len)
327 {
328 if (_attr.tnf() == TNF::Empty || !data || !len) {
329 _payload.clear();
330 _attr.shortRecord(true);
331 return;
332 }
333 _payload = std::vector<uint8_t>(data, data + len);
334 _attr.shortRecord(_payload.size() < 256);
335 }
337
340
348 bool setTextPayload(const char* str, const char* lang);
357 bool setURIPayload(const char* uri, URIProtocol protocol);
359
364 uint32_t required() const;
365
373 uint32_t encode(uint8_t* buf, const uint32_t blen) const;
381 uint32_t decode(const uint8_t* buf, const uint32_t blen);
382
384 void clear();
385
387 void dump() const;
388
389protected:
390 bool apply_nested_message();
391 void set_text_payload(const char* str, const char* lang);
392 void set_uri_payload(const char* uri, URIProtocol protocol);
393
394private:
395 Attribute _attr{};
396 std::string _type{};
397 std::vector<uint8_t> _payload{};
398 std::vector<uint8_t> _id{};
399
400 friend bool operator==(const Record& a, const Record& b);
401};
402
404inline bool operator==(const Record& a, const Record& b)
405{
406 return a._attr.value == b._attr.value && a._type == b._type && a._payload == b._payload && a._id == b._id;
407}
409inline bool operator!=(const Record& a, const Record& b)
410{
411 return !(a == b);
412}
413
414} // namespace ndef
415} // namespace nfc
416} // namespace m5
417#endif
NDEF Record.
Definition ndef_record.hpp:166
bool setURIPayload(const char *uri, URIProtocol protocol)
Set URI to the payload.
Definition ndef_record.cpp:67
uint32_t payloadSize() const
Gets the payload size.
Definition ndef_record.hpp:309
const uint8_t * payload() const
Gets the payload pointer.
Definition ndef_record.hpp:317
void clearIdentifier()
Clear the identifier.
Definition ndef_record.hpp:291
friend bool operator==(const Record &a, const Record &b)
Equal?
Definition ndef_record.hpp:404
TNF tnf() const
Gets Type Name Format.
Definition ndef_record.hpp:202
uint32_t encode(uint8_t *buf, const uint32_t blen) const
Encode.
Definition ndef_record.cpp:79
uint32_t required() const
Size required for encoding.
Definition ndef_record.cpp:42
bool setTextPayload(const char *str, const char *lang)
Set text to the payload.
Definition ndef_record.cpp:55
void clear()
Clear.
Definition ndef_record.cpp:289
Attribute & attribute()
Gets mutable attribute.
Definition ndef_record.hpp:186
void setType(const char *s)
Set the type string.
Definition ndef_record.hpp:251
void dump() const
Dump record for debug.
Definition ndef_record.cpp:297
std::string payloadAsString() const
Obtain the payload as a string.
Definition ndef_record.cpp:220
const char * type() const
Gets the type string.
Definition ndef_record.hpp:243
const uint8_t * identifier() const
Gets the identifier pointer.
Definition ndef_record.hpp:271
const Attribute & attribute() const
Gets attribute.
Definition ndef_record.hpp:194
uint32_t decode(const uint8_t *buf, const uint32_t blen)
Decode.
Definition ndef_record.cpp:150
void setIdentifier(const uint8_t *id, const uint32_t len)
Set the identifier.
Definition ndef_record.hpp:280
void setPayload(const uint8_t *data, const uint32_t len)
Set the payload data.
Definition ndef_record.hpp:326
uint32_t identifierSize() const
Gets the identifier size.
Definition ndef_record.hpp:263
NFC-A definitions.
NFC-B definitions.
Top level namespace of M5Stack.
For NDEF.
NFC related definitions.
NDEF related.
URIProtocol
URI Identifier Code.
Definition ndef.hpp:132
TNF
Type Name Field for NDEF Record.
Definition ndef_record.hpp:27
@ Wellknown
NFC Forum well-known-type.
@ MIMEMedia
Media-type as define in RFC2046.
@ Unchanged
Unchanged.
@ URI
Absolute URI as define in RFC3986.
@ Reserved
Reserved.
@ External
NFC Forum external type.
NDEF Record attribute (1st byte)
Definition ndef_record.hpp:42
TNF tnf() const
Type Name Format.
Definition ndef_record.hpp:99
bool messageBegin() const
Message Begin flag.
Definition ndef_record.hpp:59
static constexpr uint8_t CF
Chunked Flag.
Definition ndef_record.hpp:47
void messageEnd(const bool b)
Set Message End flag.
Definition ndef_record.hpp:120
void messageBegin(const bool b)
Set Message Begin flag.
Definition ndef_record.hpp:112
bool chunk() const
Chunked flag.
Definition ndef_record.hpp:75
static constexpr uint8_t ME
Message End.
Definition ndef_record.hpp:46
static constexpr uint8_t MB
Message Begin.
Definition ndef_record.hpp:45
bool idLength() const
ID Length flag.
Definition ndef_record.hpp:91
bool shortRecord() const
Short Record flag.
Definition ndef_record.hpp:83
static constexpr uint8_t IL
ID Length (If disabled (specified as 0), ID LENGTH and ID can be omitted)
Definition ndef_record.hpp:49
void idLength(const bool b)
Set ID Length flag.
Definition ndef_record.hpp:144
void tnf(const TNF t)
Set Type Name Format.
Definition ndef_record.hpp:152
static constexpr uint8_t TNF_MASK
Type Name Format.
Definition ndef_record.hpp:50
static constexpr uint8_t SR
Short Flag.
Definition ndef_record.hpp:48
void shortRecord(const bool b)
Set Short Record flag.
Definition ndef_record.hpp:136
void chunk(const bool b)
Set Chunked flag.
Definition ndef_record.hpp:128
bool messageEnd() const
Message End flag.
Definition ndef_record.hpp:67