M5Unit-NFC 0.1.0 git rev:93745b5
Loading...
Searching...
No Matches
nfcv.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_NFC_V_NFCV_HPP
11#define M5_UNIT_UNIFIED_NFC_NFC_V_NFCV_HPP
12
13#include "nfc/nfc.hpp"
14#include <cstdint>
15#include <string>
16#include <cstring>
17#include <vector>
18
19namespace m5 {
20namespace nfc {
25namespace v {
26
31enum class Type : uint8_t {
32 Unknown,
33
37 NXP,
38
43 TI,
44
45 ST_LRI,
46 ST_ST25V,
47 ST_ST25DV,
48 ST,
49
51 Fujitsu,
52
53 Unclassified,
54};
55
58const uint8_t option_flag{0x40};
59const uint8_t address_flag{0x20};
60const uint8_t select_flag{0x10};
61const uint8_t inventory_flag{0x04};
62const uint8_t data_rate_flag{0x02};
63const uint8_t subcarrier_flag{0x01};
64const uint8_t nb_slots_flag{0x20}; // if inventory_flag is 1
65const uint8_t AFI_flag{0x10}; // // if inventory_flag is 1
66constexpr uint8_t MAX_BLOCK_SIZE{32};
68
73enum class ModulationMode : uint8_t {
74 OneOf4,
75 OneOf256,
76};
77
83inline m5::nfc::NFCForumTag get_nfc_forum_tag_type(const Type t)
84{
85 return (t != Type::Unknown) ? NFCForumTag::Type5 : NFCForumTag::None;
86}
87
92struct PICC {
93 uint8_t uid[8]{};
95 uint8_t dsfID{};
96 uint8_t afi{};
97 uint8_t icRef{};
98 uint8_t block_size{};
99 uint8_t _pad{}; //
100 uint16_t blocks{};
101
106 inline bool valid() const
107 {
108 return (uid[0] == 0xE0) && blocks && block_size;
109 }
114 inline uint8_t manufacturerCode() const
115 {
116 return valid() ? uid[1] : 0xFF;
117 }
122 inline uint8_t icIdentifier() const
123 {
124 return valid() ? uid[2] : 0x00;
125 }
130 inline uint8_t icReference() const
131 {
132 return valid() ? icRef : 0xFF;
133 }
134
139 inline uint16_t totalSize() const
140 {
141 return blocks * block_size;
142 }
147 inline uint16_t userAreaSize() const
148 {
149 return totalSize(); // Same as totalSize
150 }
156 {
157 return get_nfc_forum_tag_type(type);
158 }
159
164 inline uint16_t firstUserBlock() const
165 {
166 return valid() ? 0 : 0xFFFF;
167 }
172 inline uint16_t lastUserBlock() const
173 {
174 return valid() ? (blocks - 1) : 0xFFFF;
175 }
176
178 std::string uidAsString() const;
180 std::string typeAsString() const;
181};
182
189inline bool operator==(const PICC& a, const PICC& b)
190{
191 return std::memcmp(a.uid, b.uid, 8) == 0;
192}
199inline bool operator!=(const PICC& a, const PICC& b)
200{
201 return !(a == b);
202}
203
211Type identify_type(const PICC& picc);
212
217enum class Command : uint8_t {
218 Inventory = 0x01,
219 StayQuiet = 0x02,
220 Select = 0x25,
221 ResetToReady = 0x26,
222 GetSystemInformaion = 0x2B,
223 GetSystemInformation = 0x2B,
224 ExtendedGetSystemInfo = 0x3B,
225 ReadSingleBlock = 0x20,
227 WriteSingleBlock = 0x21,
229};
230
233constexpr uint32_t TIMEOUT_INVENTORY{16};
234constexpr uint32_t TIMEOUT_STAY_QUIET{16};
235constexpr uint32_t TIMEOUT_SELECT{16};
236constexpr uint32_t TIMEOUT_RESET_TO_READY{16};
237constexpr uint32_t TIMEOUT_GET_SYSTEM_INFORMATION{20};
238constexpr uint32_t TIMEOUT_READ_SINGLE_BLOCK{20};
239constexpr uint32_t TIMEOUT_WRITE_SINGLE_BLOCK{30};
241
253uint32_t encode_VCD(std::vector<uint8_t>& out, const ModulationMode mode, const uint8_t* buffer, const uint32_t length,
254 const bool high_rate = true, const bool add_crc = true);
255
264bool decode_VICC(std::vector<uint8_t>& out, const uint8_t* buffer, const uint32_t length,
265 const uint32_t ignore_bits = 16);
266
267} // namespace v
268} // namespace nfc
269} // namespace m5
270#endif
@ NXP
NXP Semiconductors.
NFC-A definitions.
NFC-B definitions.
Top level namespace of M5Stack.
NFC related definitions.
NFC-V definitions.
NFC definitions.
NFCForumTag
NFC Forum Tag Type.
Definition nfc.hpp:39
bool decode_VICC(std::vector< uint8_t > &out, const uint8_t *buffer, const uint32_t length, const uint32_t ignore_bits)
Decode from VICC frame.
Definition nfcv.cpp:270
uint32_t encode_VCD(std::vector< uint8_t > &out, const ModulationMode mode, const uint8_t *buffer, const uint32_t length, const bool high_rate, const bool add_crc)
Encode to VCD frame.
Definition nfcv.cpp:208
Type
Type of the PICC.
Definition nfcv.hpp:31
@ ST_ST25DV
ST25DV.
@ TI_TAGIT_2048
Tag-it 2048.
@ NXP_ICODE_SLI
ICODE SLI.
@ TI_TAGIT_HF_I
Tag-it HF-I Standard.
@ NXP_ICODE_SLIX_2
ICODE SLIX2.
@ TI_TAGIT_HF_I_Pro
Tag-it HF-I Pro.
@ TI_TAGIT_HF_I_Plus
Tag-it HF-I Plus.
@ TI
TI (Unclassified)
@ NXP_ICODE_SLIX
ICODE SLIX.
@ ST
ST (Unclassified)
Command
ISO/IEC 15693 Command.
Definition nfcv.hpp:217
@ GetSystemInformation
GET SYSTEM INFORMATION.
@ ExtendedReadSingleBlock
EXTENDED READ SINGLE BLOCK.
@ ExtendedGetSystemInfo
EXTENDED GET SYSTEM INFORMATION.
@ Inventory
INVENTORY.
@ ExtendedWriteSingleBlock
EXTENDED WRITE SINGLE BLOCK.
@ ResetToReady
RESET TO READY.
@ ReadSingleBlock
READ SINGLE BLOCK.
@ StayQuiet
STAY QUIET.
@ WriteSingleBlock
WRITE SINGLE BLOCK.
ModulationMode
Definition nfcv.hpp:73
@ OneOf256
1 out of 256 pulse-position modulation
@ OneOf4
1 out of 4 pulse-position modulation
PICC for NFC-V.
Definition nfcv.hpp:92
std::string uidAsString() const
Gets the uid string.
Definition nfcv.cpp:375
uint8_t icIdentifier() const
Gets IC identifier.
Definition nfcv.hpp:122
uint16_t userAreaSize() const
Total user area size.
Definition nfcv.hpp:147
Type type
Type.
Definition nfcv.hpp:94
uint8_t icReference() const
Gets IC reference.
Definition nfcv.hpp:130
uint16_t totalSize() const
Total memory size.
Definition nfcv.hpp:139
uint16_t lastUserBlock() const
Gets the last user block.
Definition nfcv.hpp:172
uint8_t afi
Application Family Identifier.
Definition nfcv.hpp:96
bool valid() const
Valid?
Definition nfcv.hpp:106
NFCForumTag nfcForumTagType() const
NFC ForumTag.
Definition nfcv.hpp:155
uint8_t uid[8]
UID (MSB-first)
Definition nfcv.hpp:93
uint16_t blocks
Number of blocks.
Definition nfcv.hpp:100
std::string typeAsString() const
Gets the type string.
Definition nfcv.cpp:385
uint8_t manufacturerCode() const
Gets manufacturer code.
Definition nfcv.hpp:114
uint16_t firstUserBlock() const
Gets the first user block.
Definition nfcv.hpp:164
uint8_t dsfID
Data Storage Format Identifier.
Definition nfcv.hpp:95
uint8_t block_size
Byte size of 1 block.
Definition nfcv.hpp:98
uint8_t icRef
IC Reference.
Definition nfcv.hpp:97