M5Unit-NFC 0.1.2 git rev:63c4caa
Loading...
Searching...
No Matches
emulation_layer_f.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_F_EMULATION_LAYER_F_HPP
18#define M5_UNIT_NFC_NFC_LAYER_F_EMULATION_LAYER_F_HPP
19
20#include "nfc/f/nfcf.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, Communicated, Selected };
47
48 struct Adapter;
53 virtual ~EmulationLayerF();
54
56 inline State state() const
57 {
58 return _state;
59 }
61 inline const m5::nfc::f::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::f::PICC& picc, uint8_t* ptr, const uint32_t size);
88 bool end();
90 void update();
91
101 virtual State receive_callback(const State s, 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_communicated();
121 void update_selected();
122
123protected:
124 uint8_t* _memory{};
125 uint32_t _memory_size{};
126
127private:
128 State _state{}, _prev{};
129 uint32_t _expired_ms{10 * 1000u};
130 m5::utility::elapsed_time_t _activity_at{}; // When RF traffic or a state change was last seen
131 std::unique_ptr<Adapter> _impl;
132 m5::nfc::f::PICC _picc{};
133};
134
136// Impl for units
137struct EmulationLayerF::Adapter {
138 virtual ~Adapter() = default;
139
140 virtual bool start_emulation(const m5::nfc::f::PICC& picc) = 0;
141 virtual bool stop_emulation() = 0;
142 virtual bool transmit(const uint8_t* tx, const uint16_t tx_len, const uint32_t timeout_ms) = 0;
143
144 virtual EmulationLayerF::State update_off() = 0;
145 virtual EmulationLayerF::State update_communicated() = 0;
146 virtual EmulationLayerF::State update_selected() = 0;
147
148 // Put the chip back to where it waits for a reader, from whatever state it is in. The default
149 // does nothing, so an adapter that has no such step keeps working
150 virtual EmulationLayerF::State reset_to_off()
151 {
152 return EmulationLayerF::State::Off;
153 }
154
155 // Whether the chip saw RF traffic since this was last asked, which also clears it. Answers the
156 // chip sends by itself are included, since they never reach receive_callback. The default says
157 // no, so an adapter that cannot tell falls back to timing state changes alone
158 virtual bool consume_rf_activity()
159 {
160 return false;
161 }
162};
164
165} // namespace nfc
166} // namespace m5
167#endif
Common interface layer for each chip of the NFC-F emulation.
Definition emulation_layer_f.hpp:40
void setExpiredTime(const uint32_t ms)
Sets the expiration time (ms)
Definition emulation_layer_f.hpp:80
State state() const
Gets the current emulation state.
Definition emulation_layer_f.hpp:56
const m5::nfc::f::PICC & emulatePICC() const
Gets the emulated PICC information.
Definition emulation_layer_f.hpp:61
State
Emulation state for NFC-F.
Definition emulation_layer_f.hpp:46
virtual State receive_callback(const State s, const uint8_t *rx, const uint32_t rx_len)
Handles a command received from the reader.
Definition emulation_layer_f.cpp:166
bool end()
End NFC-F emulation.
Definition emulation_layer_f.cpp:88
bool begin(const m5::nfc::f::PICC &picc, uint8_t *ptr, const uint32_t size)
Begin NFC-F emulation.
Definition emulation_layer_f.cpp:70
uint32_t expiredTime() const
Gets the expiration time (ms)
Definition emulation_layer_f.hpp:66
void update()
Update emulation state machine.
Definition emulation_layer_f.cpp:113
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-F definitions.
PICC information for NFC-F.
Definition nfcf.hpp:494