10#ifndef M5_UNIT_UNIFIED_NFC_NDEF_NDEF_HPP
11#define M5_UNIT_UNIFIED_NFC_NDEF_NDEF_HPP
15#include <m5_utility/stl/extension.hpp>
16#include <m5_utility/log/library_log.hpp>
43enum class Tag : uint8_t {
61 return (t == Tag::Null) ? (1u << 0)
62 : (t == Tag::LockControl) ? (1u << 1)
63 : (t == Tag::MemoryControl) ? (1u << 2)
64 : (t == Tag::Message) ? (1u << 3)
65 : (t == Tag::Proprietary) ? (1u << 4)
66 : (t == Tag::Terminator) ? (1u << 5)
71template <
typename... Ts>
72struct are_all_tag : std::true_type {
74template <
typename T,
typename... Ts>
75struct are_all_tag<T, Ts...> : std::integral_constant<bool, std::is_same<Tag, T>::value && are_all_tag<Ts...>::value> {
78constexpr TagBits make_tag_bits_impl(TagBits acc)
83template <
typename... Rest>
84constexpr TagBits make_tag_bits_impl(TagBits acc, Tag head, Rest... rest)
86 return make_tag_bits_impl(acc |
tag_to_tagbit(head), rest...);
95template <
typename... T>
98 static_assert(
sizeof...(tags) > 0,
"At least one Tag is required");
99 static_assert(are_all_tag<T...>::value,
"Arguments must be Tag");
100 return make_tag_bits_impl(0u, tags...);
111 make_tag_bits(m5::nfc::ndef::Tag::LockControl, m5::nfc::ndef::Tag::MemoryControl, m5::nfc::ndef::Tag::Message,
112 m5::nfc::ndef::Tag::Proprietary, m5::nfc::ndef::Tag::Terminator);
119 return t <= m5::stl::to_underlying(Tag::Message) || t == m5::stl::to_underlying(Tag::Proprietary) ||
120 t == m5::stl::to_underlying(Tag::Terminator);
126 return t == m5::stl::to_underlying(Tag::Terminator);
173const char* get_uri_idc_string(
const URIProtocol protocol);
187 inline bool valid()
const
192 inline bool can_read()
const
196 inline bool can_write()
const
202 inline uint8_t major_version()
const
204 return (block[1] >> 4) & 0x0F;
206 inline uint8_t minor_version()
const
208 return block[1] & 0x0F;
210 inline uint16_t ndef_size()
const
212 return (uint16_t)block[2] << 3;
214 inline uint8_t read_access()
const
216 return (block[3] >> 4) & 0x0F;
218 inline uint8_t write_access()
const
220 return block[3] & 0x0F;
223 inline void major_version(
const uint8_t
v)
225 block[1] = (block[1] & 0x0F) | ((
v & 0x0F) << 4);
227 inline void minor_version(
const uint8_t
v)
229 block[1] = (block[1] & 0xF0) | (
v & 0x0F);
231 inline void ndef_size(
const uint16_t sz)
233 block[2] = (sz > 2040) ? 0 : (sz >> 3);
235 inline void read_access(
const uint8_t
a)
237 block[3] = (block[3] & 0x0F) | ((
a & 0x03) << 4);
239 inline void write_access(
const uint8_t
a)
241 block[3] = (block[3] & 0xF0) | (
a & 0x03);
258 static constexpr uint8_t DEFAULT_VERSION{0x10};
282 inline uint8_t version()
const
286 inline uint8_t max_block_to_read()
const
290 inline uint8_t max_block_to_write()
const
294 inline uint16_t blocks_for_ndef_storage()
const
296 return ((uint16_t)block[3] << 8) | block[4];
306 inline uint32_t current_ndef_message_length()
const
308 return ((uint32_t)block[11] << 16) | ((uint32_t)block[12] << 8) | block[13];
310 inline uint16_t check_sum()
const
312 return ((uint16_t)block[14] << 8) | block[15];
316 inline void version(
const uint8_t ver)
320 inline void max_block_to_read(
const uint8_t
b)
324 inline void max_block_to_write(
const uint8_t
b)
328 inline void blocks_for_ndef_storage(
const uint16_t s)
335 block[9] = m5::stl::to_underlying(
f);
339 block[10] = m5::stl::to_underlying(
f);
341 inline void current_ndef_message_length(
const uint32_t len)
343 block[11] = len >> 16;
344 block[12] = len >> 8;
345 block[13] = len & 0xFF;
350 uint16_t calculate_check_sum()
const;
351 uint16_t update_check_sum();
363constexpr uint8_t
NDEF_AID[] = {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
382using FileControlTagBits = uint8_t;
387 return (t == FileControlTag::Message) ? (1u << 0) : ((t == FileControlTag::Proprietary) ? (1u << 1) : 0u);
391template <
typename... Ts>
392struct are_all_fc : std::true_type {
394template <
typename T,
typename... Ts>
395struct are_all_fc<T, Ts...>
396 : std::integral_constant<bool, std::is_same<FileControlTag, T>::value && are_all_fc<Ts...>::value> {
399constexpr FileControlTagBits make_fc_bits_impl(FileControlTagBits acc)
404template <
typename... Rest>
405constexpr FileControlTagBits make_fc_bits_impl(FileControlTagBits acc, FileControlTag head, Rest... rest)
407 return make_fc_bits_impl(acc | fc_to_fcbit(head), rest...);
416template <
typename... T>
419 static_assert(
sizeof...(fcs) > 0,
"At least one Fc is required");
420 static_assert(are_all_fc<T...>::value,
"Arguments must be Fc");
421 return make_fc_bits_impl(0u, fcs...);
449 if (this->
tag == 0x04 || this->
tag == 0x05) {
452 return FileControlTag::Invalid;
465 std::vector<FileControlTLV> fctlvs{};
467 inline uint8_t major_version()
const
471 inline uint8_t minor_version()
const
475 inline bool valid()
const
477 return cclen > 7 && !this->fctlvs.empty();
480 inline FileControlTLV fctlv(
const uint8_t index)
const
482 return index < this->fctlvs.size() ? this->fctlvs[index] : FileControlTLV{};
484 FileControlTLV fctlv(
const FileControlTag fc = FileControlTag::Message)
const;
486 bool parse(
const uint8_t* buf,
const uint16_t len);
503 inline bool valid()
const
508 inline bool can_read()
const
512 inline bool can_write()
const
516 inline uint8_t size()
const
521 inline uint8_t major_version()
const
523 return (block[1] >> 6) & 0x03;
525 inline uint8_t minor_version()
const
527 return (block[1] >> 4) & 0x03;
529 inline uint16_t ndef_size()
const
532 return (block[0] ==
MAGIC_NO_CC4) ? ((uint16_t)block[2] << 3)
533 : (block[0] ==
MAGIC_NO_CC8) ? ((uint16_t)(((uint16_t)block[6] << 8) | block[7]) << 3)
536 inline uint8_t read_access()
const
538 return (block[1] >> 2) & 0x03;
540 inline uint8_t write_access()
const
542 return block[1] & 0x03;
544 inline uint8_t additional_feature()
const
549 inline void major_version(
const uint8_t
v)
551 block[1] = (block[1] & 0x3F) | ((
v & 0x03) << 6);
553 inline void minor_version(
const uint8_t
v)
555 block[1] = (block[1] & 0xCF) | ((
v & 0x03) << 4);
557 inline void ndef_size(
const uint16_t sz)
560 block[2] = (sz >> 3);
562 const uint16_t mlen = sz >> 3;
563 block[6] = (mlen >> 8);
564 block[7] = mlen & 0xFF;
568 M5_LIB_LOGW(
"Cannot store %u bytes in a %02X capability container", sz, block[0]);
571 inline void read_access(
const uint8_t
a)
573 block[1] = (block[1] & 0xF3) | ((
a & 0x03) << 2);
575 inline void write_access(
const uint8_t
a)
577 block[1] = (block[1] & 0xFC) | (
a & 0x03);
579 inline void additional_feature(
const uint8_t af)
Top level namespace of M5Stack.
constexpr FileControlTagBits make_fc_bits(T... fcs)
Make FileControlBit from FileControlTag.
Definition ndef.hpp:417
constexpr uint16_t NDEF_APP_FID
ISO DF FID for NDEF app.
Definition ndef.hpp:364
constexpr uint8_t TYPE2_CC_BLOCK
Block for CC type2.
Definition ndef.hpp:35
constexpr uint8_t NDEF_MINOR_VERSION
Support minor version.
Definition ndef.hpp:29
constexpr uint8_t NDEF_AID[]
AID for NDEF.
Definition ndef.hpp:363
constexpr uint8_t MAGIC_NO_CC4
4 byte CC
Definition ndef.hpp:30
FileControlTag
File control for File Control TLV for type4.
Definition ndef.hpp:371
@ Invalid
Unknown / not yet decoded.
constexpr uint8_t ACCESS_PROPRIETARY
Access condition (proprietary)
Definition ndef.hpp:33
uint8_t TagBits
TLV(Tag,Length,Value) tag bit group for type2/5.
Definition ndef.hpp:56
constexpr TagBits make_tag_bits(T... tags)
Make TagBit from tag.
Definition ndef.hpp:96
constexpr FileControlTagBits fcBitsAll
All fcs.
Definition ndef.hpp:431
constexpr TagBits tag_to_tagbit(const Tag t)
Tag to TagBit.
Definition ndef.hpp:59
URIProtocol
URI Identifier Code.
Definition ndef.hpp:133
@ URN_EPC_PAT
urn:epc:pat:
@ FTP_AA
ftp://anonymous:anonymous@
@ URN_EPC_TAG
urn:epc:tag:
@ URN_EPC_RAW
urn:epc:raw:
constexpr TagBits tagBitsAll
All tags.
Definition ndef.hpp:110
constexpr TagBits tagBitsMessage
Message only.
Definition ndef.hpp:114
constexpr FileControlTagBits fcBitsMessage
Message only.
Definition ndef.hpp:433
constexpr FileControlTagBits fc_to_fcbit(const FileControlTag t)
Tag to TagBit.
Definition ndef.hpp:385
constexpr bool contains_file_control_tag(const FileControlTagBits tb, const FileControlTag t)
Check whether FileControlTagBits contains given FileControl.
Definition ndef.hpp:425
bool is_terminator_tag(const uint8_t t)
Is terminator?
Definition ndef.hpp:124
constexpr uint8_t ACCESS_FREE
Access condition (Free access)
Definition ndef.hpp:32
constexpr uint16_t CC_FILE_ID
CC file id.
Definition ndef.hpp:362
constexpr uint8_t MAGIC_NO_CC8
8 byte CC (Type5)
Definition ndef.hpp:31
Tag
TLV(Tag,Length,Value) tag for type2/5.
Definition ndef.hpp:43
@ Proprietary
Proprietary.
@ LockControl
Lock control.
@ MemoryControl
Memory control.
constexpr uint16_t NDEF_FILE_ID
ISO EF FID for NDEF file.
Definition ndef.hpp:365
bool is_valid_tag(const uint8_t t)
Is valid tag?
Definition ndef.hpp:117
constexpr uint8_t NDEF_MAJOR_VERSION
Support major version.
Definition ndef.hpp:28
constexpr bool contains_tag(const TagBits tb, const Tag t)
Check whether TagBits contains given Tag.
Definition ndef.hpp:104
constexpr uint16_t CC4_MAX_NDEF_LENGTH
Maximum ndef length for 4 byte CC.
Definition ndef.hpp:36
NFC
NFC type.
Definition nfc.hpp:27
Capability container for Type2.
Definition ndef.hpp:184
For Type 3 tag (T3T)
Definition ndef.hpp:255
AccessFlag
Permission to read and write.
Definition ndef.hpp:273
@ ReadOnly
Allow read only.
@ ReadWrite
Allow read and write.
WriteFlag
Flag for fault tolerance.
Definition ndef.hpp:264
@ InProgress
Write in progress.
Capability container for Type4.
Definition ndef.hpp:460
uint16_t cclen
CC length.
Definition ndef.hpp:461
uint16_t mlc
Maximum Lc.
Definition ndef.hpp:464
uint16_t mle
Maximum Le.
Definition ndef.hpp:463
uint8_t mapping_version
Mapping version.
Definition ndef.hpp:462
File control TLV.
Definition ndef.hpp:439
uint8_t tag
File control tag.
Definition ndef.hpp:440
uint8_t read_access
Read access.
Definition ndef.hpp:444
uint8_t len
Length.
Definition ndef.hpp:441
uint8_t write_access
Write access.
Definition ndef.hpp:445
uint16_t ndef_file_size
NDEF file size.
Definition ndef.hpp:443
uint16_t ndef_file_id
NDEF file ID.
Definition ndef.hpp:442
Capability container for Type5.
Definition ndef.hpp:500