M5Unit-NFC 0.0.3 git rev:59f5362
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 inline bool messageBegin() const
56 {
57 return value & MB;
58 }
59 inline bool messageEnd() const
60 {
61 return value & ME;
62 }
63 inline bool chunk() const
64 {
65 return value & CF;
66 }
67 inline bool shortRecord() const
68 {
69 return value & SR;
70 }
71 inline bool idLength() const
72 {
73 return value & IL;
74 }
75 inline TNF tnf() const
76 {
77 return static_cast<TNF>(value & TNF_MASK);
78 }
80
83 inline void messageBegin(const bool b)
84 {
85 value = (value & ~MB) | (b ? MB : 0);
86 }
87 inline void messageEnd(const bool b)
88 {
89 value = (value & ~ME) | (b ? ME : 0);
90 }
91 inline void chunk(const bool b)
92 {
93 value = (value & ~CF) | (b ? CF : 0);
94 }
95 inline void shortRecord(const bool b)
96 {
97 value = (value & ~SR) | (b ? SR : 0);
98 }
99 inline void idLength(const bool b)
100 {
101 value = (value & ~IL) | (b ? IL : 0);
102 }
103 inline void tnf(const TNF t)
104 {
105 value = (value & ~TNF_MASK) | (m5::stl::to_underlying(t) & TNF_MASK);
106 }
108
109 uint8_t value{};
110};
111
117class Record {
118public:
119 Record() noexcept : Record(TNF::Wellknown)
120 {
121 }
122
123 explicit Record(const TNF tnf) noexcept
124 {
125 _attr.tnf(tnf);
126 }
127 ~Record()
128 {
129 }
130
133 inline Attribute& attribute()
134 {
135 return _attr;
136 }
137 inline const Attribute& attribute() const
138 {
139 return _attr;
140 }
141 inline TNF tnf() const
142 {
143 return _attr.tnf();
144 }
146
178 inline const char* type() const
179 {
180 return _type.c_str();
181 }
182 inline void setType(const char* s)
183 {
184 _type = s;
185 }
187
191 inline uint32_t identifierSize() const
192 {
193 return _id.size();
194 }
196 inline const uint8_t* identifier() const
197 {
198 return !_id.empty() ? _id.data() : nullptr;
199 }
205 inline void setIdentifier(const uint8_t* id, const uint32_t len)
206 {
207 if (_attr.tnf() != TNF::Empty && id && len) {
208 _id = std::vector<uint8_t>(id, id + len);
209 _attr.idLength(true);
210 } else {
211 _id.clear();
212 _attr.idLength(false);
213 }
214 }
216 inline void clearIdentifier()
217 {
218 setIdentifier(nullptr, 0);
219 }
221
225 std::string payloadAsString() const;
226
228 inline uint32_t payloadSize() const
229 {
230 return _payload.size();
231 }
233 inline const uint8_t* payload() const
234 {
235 return !_payload.empty() ? _payload.data() : nullptr;
236 }
242 inline void setPayload(const uint8_t* data, const uint32_t len)
243 {
244 if (_attr.tnf() != TNF::Empty && data && len) {
245 _payload = std::vector<uint8_t>(data, data + len);
246 _attr.shortRecord(_payload.size() < 256);
247 }
248 }
250
253
260 bool setTextPayload(const char* str, const char* lang);
268 bool setURIPayload(const char* uri, URIProtocol protocol);
270
272 uint32_t required() const;
273
281 uint32_t encode(uint8_t* buf, const uint32_t blen) const;
289 uint32_t decode(const uint8_t* buf, const uint32_t blen);
290
292 void clear();
293
295 void dump() const;
296
297protected:
298 bool apply_nested_message();
299 void set_text_payload(const char* str, const char* lang);
300 void set_uri_payload(const char* uri, URIProtocol protocol);
301
302private:
303 Attribute _attr{};
304 std::string _type{};
305 std::vector<uint8_t> _payload{};
306 std::vector<uint8_t> _id{};
307
308 friend bool operator==(const Record& a, const Record& b);
309};
310
312inline bool operator==(const Record& a, const Record& b)
313{
314 return a._attr.value == b._attr.value && a._type == b._type && a._payload == b._payload && a._id == b._id;
315}
317inline bool operator!=(const Record& a, const Record& b)
318{
319 return !(a == b);
320}
321
322} // namespace ndef
323} // namespace nfc
324} // namespace m5
325#endif
NDEF Record.
Definition ndef_record.hpp:117
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:228
const uint8_t * payload() const
Gets the payload pointer.
Definition ndef_record.hpp:233
void clearIdentifier()
Clear the identifier.
Definition ndef_record.hpp:216
friend bool operator==(const Record &a, const Record &b)
Equal?
Definition ndef_record.hpp:312
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:286
void dump() const
Dump record for debug.
Definition ndef_record.cpp:294
std::string payloadAsString() const
Obtain the payload as a string.
Definition ndef_record.cpp:216
const uint8_t * identifier() const
Gets the identifier pointer.
Definition ndef_record.hpp:196
uint32_t decode(const uint8_t *buf, const uint32_t blen)
Decode.
Definition ndef_record.cpp:146
void setIdentifier(const uint8_t *id, const uint32_t len)
Set the identifier.
Definition ndef_record.hpp:205
void setPayload(const uint8_t *data, const uint32_t len)
Set the payload data.
Definition ndef_record.hpp:242
uint32_t identifierSize() const
Gets the identifier size.
Definition ndef_record.hpp:191
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
static constexpr uint8_t CF
Chunked Flag.
Definition ndef_record.hpp:47
static constexpr uint8_t ME
Message End.
Definition ndef_record.hpp:46
static constexpr uint8_t MB
Message Begin.
Definition ndef_record.hpp:45
static constexpr uint8_t IL
ID Length (If disabled (specified as 0), ID LENGTH and ID can be omitted)
Definition ndef_record.hpp:49
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