ESPHome  2022.9.2
nfc.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/log.h"
4 #include "esphome/core/helpers.h"
5 #include "ndef_record.h"
6 #include "ndef_message.h"
7 #include "nfc_tag.h"
8 
9 namespace esphome {
10 namespace nfc {
11 
12 static const uint8_t MIFARE_CLASSIC_BLOCK_SIZE = 16;
13 static const uint8_t MIFARE_CLASSIC_LONG_TLV_SIZE = 4;
14 static const uint8_t MIFARE_CLASSIC_SHORT_TLV_SIZE = 2;
15 
16 static const uint8_t MIFARE_ULTRALIGHT_PAGE_SIZE = 4;
17 static const uint8_t MIFARE_ULTRALIGHT_READ_SIZE = 4;
18 static const uint8_t MIFARE_ULTRALIGHT_DATA_START_PAGE = 4;
19 static const uint8_t MIFARE_ULTRALIGHT_MAX_PAGE = 63;
20 
21 static const uint8_t TAG_TYPE_MIFARE_CLASSIC = 0;
22 static const uint8_t TAG_TYPE_1 = 1;
23 static const uint8_t TAG_TYPE_2 = 2;
24 static const uint8_t TAG_TYPE_3 = 3;
25 static const uint8_t TAG_TYPE_4 = 4;
26 static const uint8_t TAG_TYPE_UNKNOWN = 99;
27 
28 // Mifare Commands
29 static const uint8_t MIFARE_CMD_AUTH_A = 0x60;
30 static const uint8_t MIFARE_CMD_AUTH_B = 0x61;
31 static const uint8_t MIFARE_CMD_READ = 0x30;
32 static const uint8_t MIFARE_CMD_WRITE = 0xA0;
33 static const uint8_t MIFARE_CMD_WRITE_ULTRALIGHT = 0xA2;
34 
35 static const char *const MIFARE_CLASSIC = "Mifare Classic";
36 static const char *const NFC_FORUM_TYPE_2 = "NFC Forum Type 2";
37 static const char *const ERROR = "Error";
38 
39 static const uint8_t DEFAULT_KEY[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
40 static const uint8_t NDEF_KEY[6] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7};
41 static const uint8_t MAD_KEY[6] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
42 
43 std::string format_uid(std::vector<uint8_t> &uid);
44 std::string format_bytes(std::vector<uint8_t> &bytes);
45 
46 uint8_t guess_tag_type(uint8_t uid_length);
47 uint8_t get_mifare_classic_ndef_start_index(std::vector<uint8_t> &data);
48 bool decode_mifare_classic_tlv(std::vector<uint8_t> &data, uint32_t &message_length, uint8_t &message_start_index);
49 uint32_t get_mifare_classic_buffer_size(uint32_t message_length);
50 
51 bool mifare_classic_is_first_block(uint8_t block_num);
52 bool mifare_classic_is_trailer_block(uint8_t block_num);
53 
54 uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length);
55 
56 } // namespace nfc
57 } // namespace esphome
uint32_t get_mifare_classic_buffer_size(uint32_t message_length)
Definition: nfc.cpp:78
bool mifare_classic_is_trailer_block(uint8_t block_num)
Definition: nfc.cpp:99
bool decode_mifare_classic_tlv(std::vector< uint8_t > &data, uint32_t &message_length, uint8_t &message_start_index)
Definition: nfc.cpp:55
uint8_t get_mifare_classic_ndef_start_index(std::vector< uint8_t > &data)
Definition: nfc.cpp:42
std::string format_uid(std::vector< uint8_t > &uid)
Definition: nfc.cpp:10
uint8_t guess_tag_type(uint8_t uid_length)
Definition: nfc.cpp:34
bool mifare_classic_is_first_block(uint8_t block_num)
Definition: nfc.cpp:91
Definition: a4988.cpp:4
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
uint32_t get_mifare_ultralight_buffer_size(uint32_t message_length)
Definition: nfc.cpp:71
std::string format_bytes(std::vector< uint8_t > &bytes)
Definition: nfc.cpp:22