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>
42enum class Tag : uint8_t {
60 return (t == Tag::Null) ? (1u << 0)
61 : (t == Tag::LockControl) ? (1u << 1)
62 : (t == Tag::MemoryControl) ? (1u << 2)
63 : (t == Tag::Message) ? (1u << 3)
64 : (t == Tag::Proprietary) ? (1u << 4)
65 : (t == Tag::Terminator) ? (1u << 5)
70template <
typename... Ts>
71struct are_all_tag : std::true_type {
73template <
typename T,
typename... Ts>
74struct are_all_tag<T, Ts...> : std::integral_constant<bool, std::is_same<Tag, T>::value && are_all_tag<Ts...>::value> {
77constexpr TagBits make_tag_bits_impl(TagBits acc)
82template <
typename... Rest>
83constexpr TagBits make_tag_bits_impl(TagBits acc, Tag head, Rest... rest)
85 return make_tag_bits_impl(acc |
tag_to_tagbit(head), rest...);
94template <
typename... T>
97 static_assert(
sizeof...(tags) > 0,
"At least one Tag is required");
98 static_assert(are_all_tag<T...>::value,
"Arguments must be Tag");
99 return make_tag_bits_impl(0u, tags...);
110 make_tag_bits(m5::nfc::ndef::Tag::LockControl, m5::nfc::ndef::Tag::MemoryControl, m5::nfc::ndef::Tag::Message,
111 m5::nfc::ndef::Tag::Proprietary, m5::nfc::ndef::Tag::Terminator);
118 return t <= m5::stl::to_underlying(Tag::Message) || t == m5::stl::to_underlying(Tag::Proprietary) ||
119 t == m5::stl::to_underlying(Tag::Terminator);
125 return t == m5::stl::to_underlying(Tag::Terminator);
172const char* get_uri_idc_string(
const URIProtocol protocol);
186 inline bool valid()
const
191 inline bool can_read()
const
195 inline bool can_write()
const
201 inline uint8_t major_version()
const
203 return (block[1] >> 4) & 0x0F;
205 inline uint8_t minor_version()
const
207 return block[1] & 0x0F;
209 inline uint16_t ndef_size()
const
211 return (uint16_t)block[2] << 3;
213 inline uint8_t read_access()
const
215 return (block[3] >> 4) & 0x0F;
217 inline uint8_t write_access()
const
219 return block[3] & 0x0F;
222 inline void major_version(
const uint8_t
v)
224 block[1] = (block[1] & 0x0F) | ((
v & 0x0F) << 4);
226 inline void minor_version(
const uint8_t
v)
228 block[1] = (block[1] & 0xF0) | (
v & 0x0F);
230 inline void ndef_size(
const uint16_t sz)
232 block[2] = (sz > 2040) ? 0 : (sz >> 3);
234 inline void read_access(
const uint8_t
a)
236 block[3] = (block[3] & 0x0F) | ((
a & 0x03) << 4);
238 inline void write_access(
const uint8_t
a)
240 block[3] = (block[3] & 0xF0) | (
a & 0x03);
257 static constexpr uint8_t DEFAULT_VERSION{0x10};
281 inline uint8_t version()
const
285 inline uint8_t max_block_to_read()
const
289 inline uint8_t max_block_to_write()
const
293 inline uint16_t blocks_for_ndef_storage()
const
295 return ((uint16_t)block[3] << 8) | block[4];
305 inline uint32_t current_ndef_message_length()
const
307 return ((uint32_t)block[11] << 16) | ((uint32_t)block[12] << 8) | block[13];
309 inline uint16_t check_sum()
const
311 return ((uint16_t)block[14] << 8) | block[15];
315 inline void version(
const uint8_t ver)
319 inline void max_block_to_read(
const uint8_t
b)
323 inline void max_block_to_write(
const uint8_t
b)
327 inline void blocks_for_ndef_storage(
const uint16_t s)
334 block[9] = m5::stl::to_underlying(
f);
338 block[10] = m5::stl::to_underlying(
f);
340 inline void current_ndef_message_length(
const uint32_t len)
342 block[11] = len >> 16;
343 block[12] = len >> 8;
344 block[13] = len & 0xFF;
349 uint16_t calculate_check_sum()
const;
350 uint16_t update_check_sum();
362constexpr uint8_t
NDEF_AID[] = {0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01};
380using FileControlTagBits = uint8_t;
385 return (t == FileControlTag::Message) ? (1u << 0) : ((t == FileControlTag::Proprietary) ? (1u << 1) : 0u);
389template <
typename... Ts>
390struct are_all_fc : std::true_type {
392template <
typename T,
typename... Ts>
393struct are_all_fc<T, Ts...>
394 : std::integral_constant<bool, std::is_same<FileControlTag, T>::value && are_all_fc<Ts...>::value> {
397constexpr FileControlTagBits make_fc_bits_impl(FileControlTagBits acc)
402template <
typename... Rest>
403constexpr FileControlTagBits make_fc_bits_impl(FileControlTagBits acc, FileControlTag head, Rest... rest)
405 return make_fc_bits_impl(acc | fc_to_fcbit(head), rest...);
414template <
typename... T>
417 static_assert(
sizeof...(fcs) > 0,
"At least one Fc is required");
418 static_assert(are_all_fc<T...>::value,
"Arguments must be Fc");
419 return make_fc_bits_impl(0u, fcs...);
460 std::vector<FileControlTLV> fctlvs{};
462 inline uint8_t major_version()
const
466 inline uint8_t minor_version()
const
470 inline bool valid()
const
472 return cclen > 7 && !this->fctlvs.empty();
475 inline FileControlTLV fctlv(
const uint8_t index)
const
477 return index < this->fctlvs.size() ? this->fctlvs[index] : FileControlTLV{};
479 FileControlTLV fctlv(
const FileControlTag fc = FileControlTag::Message)
const;
481 bool parse(
const uint8_t* buf,
const uint16_t len);
498 inline bool valid()
const
503 inline bool can_read()
const
507 inline bool can_write()
const
511 inline uint8_t size()
const
516 inline uint8_t major_version()
const
518 return (block[1] >> 6) & 0x03;
520 inline uint8_t minor_version()
const
522 return (block[1] >> 4) & 0x03;
524 inline uint16_t ndef_size()
const
526 return (block[0] ==
MAGIC_NO_CC4) ? (((uint16_t)block[2]) << 3)
527 : (block[0] ==
MAGIC_NO_CC8) ? (((uint16_t)block[6] << 8) | block[7])
530 inline uint8_t read_access()
const
532 return (block[1] >> 2) & 0x03;
534 inline uint8_t write_access()
const
536 return block[1] & 0x03;
538 inline uint8_t additional_feature()
const
543 inline void major_version(
const uint8_t
v)
545 block[1] = (block[1] & 0x3F) | ((
v & 0x03) << 6);
547 inline void minor_version(
const uint8_t
v)
549 block[1] = (block[1] & 0xCF) | ((
v & 0x03) << 4);
551 inline void ndef_size(
const uint16_t sz)
554 block[2] = (sz >> 3);
556 block[6] = (sz >> 8);
557 block[7] = sz & 0xFF;
560 inline void read_access(
const uint8_t
a)
562 block[1] = (block[1] & 0xF3) | ((
a & 0x03) << 2);
564 inline void write_access(
const uint8_t
a)
566 block[1] = (block[1] & 0xFC) | (
a & 0x03);
568 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:415
constexpr uint16_t NDEF_APP_FID
ISO DF FID for NDEF app.
Definition ndef.hpp:363
constexpr uint8_t TYPE2_CC_BLOCK
Block for CC type2.
Definition ndef.hpp:34
constexpr uint8_t NDEF_MINOR_VERSION
Support minor version.
Definition ndef.hpp:28
constexpr uint8_t NDEF_AID[]
AID for NDEF.
Definition ndef.hpp:362
constexpr uint8_t MAGIC_NO_CC4
4 byte CC
Definition ndef.hpp:29
FileControlTag
File control for File Control TLV for type4.
Definition ndef.hpp:370
constexpr uint8_t ACCESS_PROPRIETARY
Access condition (proprietary)
Definition ndef.hpp:32
uint8_t TagBits
TLV(Tag,Length,Value) tag bit group for type2/5.
Definition ndef.hpp:55
constexpr TagBits make_tag_bits(T... tags)
Make TagBit from tag.
Definition ndef.hpp:95
constexpr FileControlTagBits fcBitsAll
All fcs.
Definition ndef.hpp:429
constexpr TagBits tag_to_tagbit(const Tag t)
Tag to TagBit.
Definition ndef.hpp:58
URIProtocol
URI Identifier Code.
Definition ndef.hpp:132
@ 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:109
constexpr TagBits tagBitsMessage
Message only.
Definition ndef.hpp:113
constexpr FileControlTagBits fcBitsMessage
Message only.
Definition ndef.hpp:431
constexpr FileControlTagBits fc_to_fcbit(const FileControlTag t)
Tag to TagBit.
Definition ndef.hpp:383
constexpr bool contains_file_control_tag(const FileControlTagBits tb, const FileControlTag t)
Check whether FileControlTagBits contains given FileControl.
Definition ndef.hpp:423
bool is_terminator_tag(const uint8_t t)
Is terminator?
Definition ndef.hpp:123
constexpr uint8_t ACCESS_FREE
Access condition (Free access)
Definition ndef.hpp:31
constexpr uint16_t CC_FILE_ID
CC file id.
Definition ndef.hpp:361
constexpr uint8_t MAGIC_NO_CC8
8 byte CC (Type5)
Definition ndef.hpp:30
Tag
TLV(Tag,Length,Value) tag for type2/5.
Definition ndef.hpp:42
@ Proprietary
Proprietary.
@ LockControl
Lock control.
@ MemoryControl
Memory control.
constexpr uint16_t NDEF_FILE_ID
ISO EF FID for NDEF file.
Definition ndef.hpp:364
bool is_valid_tag(const uint8_t t)
Is valid tag?
Definition ndef.hpp:116
constexpr uint8_t NDEF_MAJOR_VERSION
Support major version.
Definition ndef.hpp:27
constexpr bool contains_tag(const TagBits tb, const Tag t)
Check whether TagBits contains given Tag.
Definition ndef.hpp:103
constexpr uint16_t CC4_MAX_NDEF_LENGTH
Maximum ndef length for 4 byte CC.
Definition ndef.hpp:35
NFC
NFC type.
Definition nfc.hpp:27
Capability container for Type2.
Definition ndef.hpp:183
For Type 3 tag (T3T)
Definition ndef.hpp:254
AccessFlag
Permission to read and write.
Definition ndef.hpp:272
@ ReadOnly
Allow read only.
@ ReadWrite
Allow read and write.
WriteFlag
Flag for fault tolerance.
Definition ndef.hpp:263
@ InProgress
Write in progress.
Capability container for Type4.
Definition ndef.hpp:455
uint16_t cclen
CC length.
Definition ndef.hpp:456
uint16_t mlc
Maximum Lc.
Definition ndef.hpp:459
uint16_t mle
Maximum Le.
Definition ndef.hpp:458
uint8_t mapping_version
Mapping version.
Definition ndef.hpp:457
File control TLV.
Definition ndef.hpp:437
uint8_t tag
File control tag.
Definition ndef.hpp:438
uint8_t read_access
Read access.
Definition ndef.hpp:442
uint8_t len
Length.
Definition ndef.hpp:439
uint8_t write_access
Write access.
Definition ndef.hpp:443
uint16_t ndef_file_size
NDEF file size.
Definition ndef.hpp:441
uint16_t ndef_file_id
NDEF file ID.
Definition ndef.hpp:440
Capability container for Type5.
Definition ndef.hpp:495