M5Unit-NFC 0.0.3 git rev:59f5362
Loading...
Searching...
No Matches
file_system.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_ISODEP_FILE_SYSTEM_HPP
11#define M5_UNIT_UNIFIED_NFC_NFC_ISODEP_FILE_SYSTEM_HPP
12#include "isoDEP.hpp"
13#include "nfc/apdu/apdu.hpp"
14
15namespace m5 {
16namespace nfc {
17namespace isodep {
18class IsoDEP;
19}
20
26struct FCP {
27 uint16_t fid{};
28 uint16_t file_size{};
29 uint8_t file_descriptor{0x01};
30 uint8_t file_size_tag{0x80};
31
36 std::vector<uint8_t> to_tlv() const
37 {
38 std::vector<uint8_t> fcp;
39 fcp.reserve(16);
40
41 // File Descriptor (0x82)
42 fcp.push_back(0x82);
43 fcp.push_back(1);
44 fcp.push_back(file_descriptor);
45
46 // File ID (0x83)
47 fcp.push_back(0x83);
48 fcp.push_back(2);
49 fcp.push_back(static_cast<uint8_t>((fid >> 8) & 0xFF));
50 fcp.push_back(static_cast<uint8_t>(fid & 0xFF));
51
52 // File Size (tag: file_size_tag)
53 fcp.push_back(file_size_tag);
54 fcp.push_back(2);
55 fcp.push_back(static_cast<uint8_t>((file_size >> 8) & 0xFF));
56 fcp.push_back(static_cast<uint8_t>(file_size & 0xFF));
57
58 //
59 std::vector<uint8_t> out;
60 out.reserve(fcp.size() + 2);
61 out.push_back(0x62);
62 out.push_back(static_cast<uint8_t>(fcp.size()));
63 out.insert(out.end(), fcp.begin(), fcp.end());
64 return out;
65 }
66};
67
75bool parseFCI(FCP& fcp, const uint8_t* data, const uint32_t len);
76
82public:
83 explicit FileSystem(m5::nfc::isodep::IsoDEP& isoDEP) : _isoDEP{isoDEP}
84 {
85 }
86 virtual ~FileSystem() = default;
87
90
97 bool createFile(const uint8_t* fcp, const uint16_t fcp_len);
103 bool createFile(const FCP& fcp);
110 bool createFile(const uint16_t fid, const uint16_t file_size);
111
122 const m5::nfc::apdu::SelectResponse res, const uint8_t* param, const uint8_t param_len);
123
131 bool selectByFileId(const uint16_t fid,
132 const m5::nfc::apdu::SelectResponse res = m5::nfc::apdu::SelectResponse::FCI,
133 const m5::nfc::apdu::SelectOccurrence occ = m5::nfc::apdu::SelectOccurrence::FirstOrOnly);
140 bool selectFileIdAuto(const uint16_t fid,
141 const m5::nfc::apdu::SelectOccurrence occ = m5::nfc::apdu::SelectOccurrence::FirstOrOnly);
150 bool selectByDfName(const uint8_t* aid, const uint8_t aid_len,
151 const m5::nfc::apdu::SelectResponse res = m5::nfc::apdu::SelectResponse::FCI,
152 const m5::nfc::apdu::SelectOccurrence occ = m5::nfc::apdu::SelectOccurrence::FirstOrOnly);
160 bool selectDfNameAuto(const uint8_t* aid, const uint8_t aid_len,
161 const m5::nfc::apdu::SelectOccurrence occ = m5::nfc::apdu::SelectOccurrence::FirstOrOnly);
171 bool selectByPath(const uint8_t* path, const uint8_t path_len, const bool from_mf = true,
172 const m5::nfc::apdu::SelectResponse res = m5::nfc::apdu::SelectResponse::FCI,
173 const m5::nfc::apdu::SelectOccurrence occ = m5::nfc::apdu::SelectOccurrence::FirstOrOnly);
180 bool selectParent(const m5::nfc::apdu::SelectResponse res = m5::nfc::apdu::SelectResponse::FCI,
181 const m5::nfc::apdu::SelectOccurrence occ = m5::nfc::apdu::SelectOccurrence::FirstOrOnly);
186 inline bool selectMasterFile()
187 {
188 return selectByFileId(m5::nfc::apdu::master_file_id);
189 }
190
198 bool verify(const uint8_t* password, const uint16_t pass_len, const uint8_t param2);
205 inline bool verifyGlobal(const uint8_t* password, const uint16_t pass_len)
206 {
207 return verify(password, pass_len, 0x80);
208 }
215 inline bool verifySpecific(const uint8_t* password, const uint16_t pass_len)
216 {
217 return verify(password, pass_len, 0x00);
218 }
226 bool readBinary(std::vector<uint8_t>& out, const uint16_t offset, const uint16_t le /* 1..256 recommended */);
234 bool updateBinary(const uint16_t offset, const uint8_t* data, const uint16_t data_len);
236
241 inline const std::vector<uint8_t>& lastSelectData() const
242 {
243 return _last_select_fci;
244 }
245
246protected:
248 std::vector<uint8_t> _last_select_fci{};
249};
250
251} // namespace nfc
252} // namespace m5
253#endif
Application Protocol Data Unit (ISO/IEC 7816-4)
SelectBy
Select control for SELECT_FILE.
Definition apdu.hpp:122
SelectResponse
Response for SELECT_FILE.
Definition apdu.hpp:148
SelectOccurrence
Select occurrence for SELECT_FILE.
Definition apdu.hpp:137
ISO/IEC 7816-4 file system.
Definition file_system.hpp:81
bool selectFileIdAuto(const uint16_t fid, const m5::nfc::apdu::SelectOccurrence occ=m5::nfc::apdu::SelectOccurrence::FirstOrOnly)
SELECT by File ID with auto response fallback (None->FCI->FCP)
Definition file_system.cpp:180
bool verifySpecific(const uint8_t *password, const uint16_t pass_len)
VERIFY specific reference data.
Definition file_system.hpp:215
bool selectDfNameAuto(const uint8_t *aid, const uint8_t aid_len, const m5::nfc::apdu::SelectOccurrence occ=m5::nfc::apdu::SelectOccurrence::FirstOrOnly)
SELECT by DF Name with auto response fallback (FCI->None->FCP)
Definition file_system.cpp:194
bool selectMasterFile()
SELECT MF (Master File)
Definition file_system.hpp:186
const std::vector< uint8_t > & lastSelectData() const
Last SELECT response data (SW excluded)
Definition file_system.hpp:241
bool updateBinary(const uint16_t offset, const uint8_t *data, const uint16_t data_len)
UPDATE BINARY.
Definition file_system.cpp:296
bool verify(const uint8_t *password, const uint16_t pass_len, const uint8_t param2)
VERIFY (generic)
Definition file_system.cpp:238
bool selectByFileId(const uint16_t fid, const m5::nfc::apdu::SelectResponse res=m5::nfc::apdu::SelectResponse::FCI, const m5::nfc::apdu::SelectOccurrence occ=m5::nfc::apdu::SelectOccurrence::FirstOrOnly)
SELECT by File ID with explicit response type.
Definition file_system.cpp:170
bool selectParent(const m5::nfc::apdu::SelectResponse res=m5::nfc::apdu::SelectResponse::FCI, const m5::nfc::apdu::SelectOccurrence occ=m5::nfc::apdu::SelectOccurrence::FirstOrOnly)
SELECT parent DF.
Definition file_system.cpp:209
bool createFile(const uint8_t *fcp, const uint16_t fcp_len)
CREATE FILE.
Definition file_system.cpp:87
bool verifyGlobal(const uint8_t *password, const uint16_t pass_len)
VERIFY global reference data.
Definition file_system.hpp:205
bool selectByPath(const uint8_t *path, const uint8_t path_len, const bool from_mf=true, const m5::nfc::apdu::SelectResponse res=m5::nfc::apdu::SelectResponse::FCI, const m5::nfc::apdu::SelectOccurrence occ=m5::nfc::apdu::SelectOccurrence::FirstOrOnly)
SELECT by path (MF or current DF)
Definition file_system.cpp:202
bool selectFile(const m5::nfc::apdu::SelectBy by, const m5::nfc::apdu::SelectOccurrence occ, const m5::nfc::apdu::SelectResponse res, const uint8_t *param, const uint8_t param_len)
SELECT FILE (generic)
Definition file_system.cpp:121
bool selectByDfName(const uint8_t *aid, const uint8_t aid_len, const m5::nfc::apdu::SelectResponse res=m5::nfc::apdu::SelectResponse::FCI, const m5::nfc::apdu::SelectOccurrence occ=m5::nfc::apdu::SelectOccurrence::FirstOrOnly)
SELECT by DF Name with explicit response type.
Definition file_system.cpp:188
bool readBinary(std::vector< uint8_t > &out, const uint16_t offset, const uint16_t le)
READ BINARY.
Definition file_system.cpp:260
ISO Data Exchange Protocol.
Definition isoDEP.hpp:190
ISO Data Exchange Protocol.
For ISO-DEP.
Top level namespace of M5stack.
NFC related definitions.
File Control Parameters.
Definition file_system.hpp:26
std::vector< uint8_t > to_tlv() const
Build FCP TLV (tag 0x62)
Definition file_system.hpp:36
uint16_t fid
File ID (FID)
Definition file_system.hpp:27
uint16_t file_size
File size in bytes.
Definition file_system.hpp:28
uint8_t file_descriptor
File descriptor (default: transparent EF)
Definition file_system.hpp:29
uint8_t file_size_tag
File size TLV tag (default: 0x80)
Definition file_system.hpp:30