M5Unit-NFC 0.1.2 git rev:63c4caa
Loading...
Searching...
No Matches
emulation_layer_a.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
17#ifndef M5_UNIT_NFC_NFC_LAYER_A_EMULATION_LAYER_A_HPP
18#define M5_UNIT_NFC_NFC_LAYER_A_EMULATION_LAYER_A_HPP
19
20#include "nfc/a/nfca.hpp"
21#include <m5_utility/compatibility_feature.hpp>
22#include <vector>
23#include <memory>
24
25namespace m5 {
26
27namespace unit {
28class UnitST25R3916;
29class CapST25R3916;
30} // namespace unit
31
32namespace nfc {
33
41public:
46 enum class State { None, Off, Idle, Ready, Active, Halt };
47
48 struct Adapter;
53 virtual ~EmulationLayerA();
54
56 inline State state() const
57 {
58 return _state;
59 }
61 inline const m5::nfc::a::PICC& emulatePICC() const
62 {
63 return _picc;
64 }
66 inline uint32_t expiredTime() const
67 {
68 return _expired_ms;
69 }
80 void setExpiredTime(const uint32_t ms)
81 {
82 _expired_ms = ms;
83 }
84
86 bool begin(const m5::nfc::a::PICC& picc, uint8_t* ptr, const uint32_t size);
88 bool end();
90 void update();
91
101 virtual State receive_callback(const uint8_t* rx, const uint32_t rx_len);
102
103protected:
104 /*
105 Return to State::Off when the current state has lasted longer than the expiration time
106 @note Called at the top of update(). Does nothing while the time is zero, or in None/Off
107 */
108 void update_expired();
109 /*
110 Send a response to the reader
111 @param tx Transmit buffer
112 @param tx_len Transmit length
113 @param timeout_ms Timeout in milliseconds
114 @return True if successful
115 */
116 bool transmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms);
117
118private:
119 void update_off();
120 void update_idle();
121 void update_ready();
122 void update_active();
123 void update_halt();
124
125protected:
126 uint8_t* _memory{};
127 uint32_t _memory_size{};
128
129private:
130 State _state{}, _prev{};
131 uint32_t _expired_ms{10 * 1000u};
132 m5::utility::elapsed_time_t _activity_at{}; // When RF traffic or a state change was last seen
133 std::unique_ptr<Adapter> _impl;
134 m5::nfc::a::PICC _picc{};
135};
136
138// Impl for units
139struct EmulationLayerA::Adapter {
140 virtual ~Adapter() = default;
141
142 virtual bool start_emulation(const m5::nfc::a::PICC& picc) = 0;
143 virtual bool stop_emulation() = 0;
144 virtual bool transmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms) = 0;
145
146 virtual EmulationLayerA::State update_off() = 0;
147 virtual EmulationLayerA::State update_idle() = 0;
148 virtual EmulationLayerA::State update_ready() = 0;
149 virtual EmulationLayerA::State update_active() = 0;
150 virtual EmulationLayerA::State update_halt() = 0;
151
152 // Put the chip back to where it waits for a reader, from whatever state it is in. The default
153 // does nothing, so an adapter that has no such step keeps working
154 virtual EmulationLayerA::State reset_to_off()
155 {
156 return EmulationLayerA::State::Off;
157 }
158
159 // Whether the chip saw RF traffic since this was last asked, which also clears it. Answers the
160 // chip sends by itself are included, since they never reach receive_callback. The default says
161 // no, so an adapter that cannot tell falls back to timing state changes alone
162 virtual bool consume_rf_activity()
163 {
164 return false;
165 }
166};
168
169} // namespace nfc
170} // namespace m5
171#endif
Common interface layer for each chip of the NFC-A emulation.
Definition emulation_layer_a.hpp:40
bool end()
End NFC-A emulation.
Definition emulation_layer_a.cpp:62
const m5::nfc::a::PICC & emulatePICC() const
Gets the emulated PICC information.
Definition emulation_layer_a.hpp:61
State
Emulation state for NFC-A.
Definition emulation_layer_a.hpp:46
bool begin(const m5::nfc::a::PICC &picc, uint8_t *ptr, const uint32_t size)
Begin NFC-A emulation.
Definition emulation_layer_a.cpp:33
void setExpiredTime(const uint32_t ms)
Sets the expiration time (ms)
Definition emulation_layer_a.hpp:80
uint32_t expiredTime() const
Gets the expiration time (ms)
Definition emulation_layer_a.hpp:66
void update()
Update emulation state machine.
Definition emulation_layer_a.cpp:87
State state() const
Gets the current emulation state.
Definition emulation_layer_a.hpp:56
virtual State receive_callback(const uint8_t *rx, const uint32_t rx_len)
Handles a command received from the reader.
Definition emulation_layer_a.cpp:158
ST25R3916 unit in CapCC1101 (SPI)
Definition unit_ST25R3916.hpp:2150
ST25R3916 Unit.
Definition unit_ST25R3916.hpp:39
Top level namespace of M5Stack.
NFC related definitions.
Unit-related namespace.
NFC-A definitions.
PICC for NFC-A.
Definition nfca.hpp:481