M5Unit-NFC 0.0.3 git rev:59f5362
Loading...
Searching...
No Matches
nfcb.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_B_NFCB_HPP
11#define M5_UNIT_UNIFIED_NFC_NFC_B_NFCB_HPP
12
13#include "nfc/nfc.hpp"
14#include <cstdint>
15#include <string>
16#include <cstring>
17
18namespace m5 {
19namespace nfc {
24namespace b {
25
30enum class Type : uint8_t {
31 Unknown,
33};
34
39enum class Require : uint8_t {
40 Slot1,
41 Slot2,
42 Slot4,
43 Slot8,
44 Slot16,
45};
46
47constexpr uint8_t ATQB_LENGTH{11};
48
51constexpr uint8_t COMMUNICATION_SAME_SPEED{0x80};
52constexpr uint8_t COMMUNICATION_SPPED_106K{0X00};
53constexpr uint8_t COMMUNICATION_SPPED_212K_FROM_PICC{0X10};
54constexpr uint8_t COMMUNICATION_SPPED_424K_FROM_PICC{0X20};
55constexpr uint8_t COMMUNICATION_SPPED_847K_FROM_PICC{0X40};
56constexpr uint8_t COMMUNICATION_SPPED_212K_TO_PICC{0X01};
57constexpr uint8_t COMMUNICATION_SPPED_424K_TO_PICC{0X02};
58constexpr uint8_t COMMUNICATION_SPPED_847K_TO_PICC{0X04};
60
63const uint8_t FRAME_OPTION_NAD{0x02};
64const uint8_t FRAME_OPTION_CID{0x01};
66
70uint16_t maximum_frame_length(const uint8_t protocol[3]);
71
72inline uint8_t maximum_frame_length_bits(const uint8_t protocol[3])
73{
74 return protocol ? protocol[1] >> 4 : 0x0F;
75}
76
78inline bool supports_iso14443_4(const uint8_t protocol[3])
79{
80 return protocol ? ((protocol[1] & 0x0F) & 0x01) : false;
81}
82
84inline uint8_t get_frame_option(const uint8_t protocol[3])
85{
86 return protocol ? (protocol[2] & 0x03) : 0x00;
87}
89inline uint8_t get_fwi(const uint8_t protocol[3])
90{
91 return protocol ? ((protocol[2] >> 4) & 0x0F) : 0x0F /*RFU*/;
92}
94
99struct PICC {
100 uint8_t uid[8]{};
101 union {
102 uint8_t atqb[ATQB_LENGTH]{};
103 struct {
104 uint8_t pupi[4];
105 uint8_t application[4];
106 uint8_t protocol[3];
107 // uint8_t cid[1];
108 } __attribute__((packed));
109 } __attribute__((packed));
110 Type type{Type::Unknown};
111 uint8_t cid{};
112 uint8_t option{};
113
115 inline bool valid() const
116 {
117 return isISO14443_4();
118 }
119
120 std::string pupiAsString() const;
121 std::string typeAsString() const;
122
126 inline bool isISO14443_4() const
127 {
129 }
131
134 inline bool supportsNAD() const
135 {
136 return get_frame_option(protocol) & FRAME_OPTION_NAD;
137 }
138 inline bool supportsCID() const
139 {
140 return get_frame_option(protocol) & FRAME_OPTION_CID;
141 }
142 inline uint16_t maximumFrameLength() const
143 {
144 return maximum_frame_length(protocol);
145 }
146 inline uint8_t maximumFrameLengthBits() const
147 {
148 return maximum_frame_length_bits(protocol);
149 }
150 inline uint8_t communicationSpeed() const
151 {
152 return protocol[0];
153 }
154 inline uint8_t fwi() const
155 {
156 return get_fwi(protocol);
157 }
159};
160
162inline bool operator==(const PICC& a, const PICC& b)
163{
164 return std::memcmp(a.atqb, b.atqb, sizeof(a.atqb)) == 0;
165}
167inline bool operator!=(const PICC& a, const PICC& b)
168{
169 return !(a == b);
170}
171
176enum class Command : uint8_t {
177 // ISO/IEC 14443B
178 REQ_WUPB = 0x05,
179 ATTRIB = 0x1D,
180 HLTB = 0x50,
181 // ISO/IEC 14443-4
182 DESELECT = 0xC2,
183 DESELECT_WITH_CID = 0xCA,
184};
185
188constexpr uint32_t TIMEOUT_REQ_WUP_B{5};
189constexpr uint32_t TIMEOUT_ATTRIB{50};
190constexpr uint32_t TIMEOUT_HLTB{5};
191constexpr uint32_t TIMEOUT_DESELECT{5};
193
194} // namespace b
195} // namespace nfc
196} // namespace m5
197#endif
NFC-A definitions.
NFC-B definitions.
Top level namespace of M5stack.
NFC related definitions.
NFC definitions.
uint16_t maximum_frame_length(const uint8_t protocol[3])
Get maximum frame length from protocol bytes.
Definition nfcb.cpp:36
uint8_t get_frame_option(const uint8_t protocol[3])
Gets the frame option bits.
Definition nfcb.hpp:84
Type
Type of the PICC.
Definition nfcb.hpp:30
@ Unclassified
Unclassified.
bool supports_iso14443_4(const uint8_t protocol[3])
Supports ISO/IEC 14443-4?
Definition nfcb.hpp:78
constexpr uint8_t ATQB_LENGTH
ATQB length pupi(4) + application(4) + protocol(3)
Definition nfcb.hpp:47
Require
Number of slots required in the request/wakeup.
Definition nfcb.hpp:39
uint8_t get_fwi(const uint8_t protocol[3])
Gets the FWI.
Definition nfcb.hpp:89
uint8_t maximum_frame_length_bits(const uint8_t protocol[3])
Get maximum frame length from protocol bytes.
Definition nfcb.hpp:72
Command
ISO/IEC 14443 Type B command (Layer 3 / activation)
Definition nfcb.hpp:176
@ HLTB
Halt Type B (rarely used)
@ ATTRIB
Attribute (activate ISO-DEP)
@ REQ_WUPB
Request/Wakeup Type B.
@ DESELECT_WITH_CID
DESELECT with CID.
PICC for NFC-B.
Definition nfcb.hpp:99
uint8_t atqb[ATQB_LENGTH]
ATQB.
Definition nfcb.hpp:102
std::string typeAsString() const
Gets the type string.
Definition nfcb.cpp:55
uint8_t cid
CID;.
Definition nfcb.hpp:111
std::string pupiAsString() const
Gets the pupi string.
Definition nfcb.cpp:45
uint8_t application[4]
Application Data.
Definition nfcb.hpp:105
bool valid() const
Valid?
Definition nfcb.hpp:115
bool isISO14443_4() const
ISO14443-4?
Definition nfcb.hpp:126
uint8_t protocol[3]
Protocol information.
Definition nfcb.hpp:106
Type type
Type.
Definition nfcb.hpp:110
uint8_t pupi[4]
Pseudo-Unique PICC Identifier.
Definition nfcb.hpp:104