M5Unit-RFID 0.2.0 git rev:79c1939
Loading...
Searching...
No Matches
uhf_layer.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_RFID_UHF_UHF_LAYER_HPP
11#define M5_UNIT_RFID_UHF_UHF_LAYER_HPP
12
13#include "uhf.hpp"
14#include "unit/unit_UHFRFID.hpp"
15
16namespace m5 {
17namespace uhf {
18
26using Result = m5::stl::expected<void, Reason>;
27
39class UHFLayer {
40public:
41 explicit UHFLayer(m5::unit::UHFRFIDComponent& u) : _u{u}
42 {
43 }
44 UHFLayer(const UHFLayer&) = delete;
45 UHFLayer& operator=(const UHFLayer&) = delete;
46
49
59 bool detect(std::vector<Tag>& tags, const uint32_t timeout_ms = 1000U);
75 bool detect(Tag& tag, const uint32_t timeout_ms = 500U);
77
80
96 bool select(const Tag& tag, const uint32_t access_password = 0, const bool verify = true);
98 bool select(const Epc& epc, const uint32_t access_password = 0, const bool verify = true);
115 bool select(const Tid& tid, const uint32_t access_password = 0, const bool verify = true);
117 bool deselect();
119 inline bool isSelected() const
120 {
121 return _has_selection;
122 }
124 inline const Tag& selectedTag() const
125 {
126 return _selected;
127 }
136 bool identify(Tag& tag);
138
141
149 bool dump();
156 bool dump(const Bank bank);
165 bool dump(const Bank bank, const uint16_t word_address, const uint16_t words);
167
170
180 Result readBank(std::vector<uint8_t>& out, const Bank bank, const uint16_t word_address, const uint16_t word_count);
192 Result readBank(uint8_t* out, uint16_t& out_len, const Bank bank, const uint16_t word_address,
193 const uint16_t word_count);
211 Result writeBank(const Bank bank, const uint16_t word_address, const uint8_t* data, const uint16_t data_len);
223 Result lock(const std::vector<LockSetting>& settings, const bool allow_permanent = false);
234 Result kill(const Tag& tag, const uint32_t kill_password);
246 Result readBlockPermalock(std::vector<uint8_t>& mask, const Bank bank, const uint16_t block_pointer = 0,
247 const uint8_t block_range = 1);
261 Result blockPermalock(const Bank bank, const uint8_t* mask, const size_t mask_len, const bool allow_permanent,
262 const uint16_t block_pointer = 0, const uint8_t block_range = 1);
284 Result writeQTParameters(const QTParameters& qt, const bool persistent = false);
292 Result readNxpConfigWord(uint16_t& config);
306 Result toggleNxpConfigWord(uint16_t& config, const uint16_t toggle);
315 Result writeNxpEAS(const bool enable);
324 bool nxpEASAlarm(std::vector<uint8_t>& alarm);
337 Result nxpReadProtect(const bool protect);
339
340private:
341 // Store a mask, switch the module over to it, and optionally prove a tag answers
342 bool apply_selection(const Bank bank, const uint32_t pointer_bits, const uint8_t* mask, const size_t mask_len,
343 const uint32_t access_password, const bool verify);
344 // Read one word of the EPC bank to prove the selected tag is there and answering
345 bool verify_selection();
346 // Drop the selection when a write has replaced the bytes its mask matches on
347 void drop_selection_if_mask_rewritten(const Bank bank);
348 // Store the mask of the selected tag again, so a lost singulation can be regained
349 bool reapply_selection();
350 // How many words of a bank there are to read, 0 when that is not known
351 uint16_t bank_words(const Bank bank);
352 // Print a stretch of one bank, sixteen bytes to a line
353 bool dump_words(const char* what, const Bank bank, const uint16_t word_address, const uint16_t words);
354 /*
355 Pass on a reader's failure in terms the caller can act on
356 @param result What the reader answered
357 @return The same success, or what its error code means
358 */
359 Result relay(const m5::unit::TagResult& result) const;
360 /*
361 Stop polling for as long as a tag is being addressed
362 @details An inventory round leaves every tag it saw flagged as already counted, and a tag
363 in that state answers nothing until the flag decays. Letting polling run between two tag
364 operations therefore breaks the second one, so it stays stopped from select() until
365 deselect() rather than being stopped and restarted around each operation
366 @return False when polling was running and would not stop. The module is still running
367 rounds, and a tag operation attempted now would be answering into them
368 */
369 bool pause_polling();
370 // Put polling back the way select() found it
371 void resume_polling();
372
374 Tag _selected{};
375 uint32_t _access_password{};
376 // Bank the stored mask matches against, which decides whether an EPC rewrite invalidates it
377 Bank _mask_bank{Bank::Epc};
378 bool _has_selection{};
379 // Was polling running when the tag currently being addressed was selected?
380 bool _resume_polling{};
381};
382
383} // namespace uhf
384} // namespace m5
385#endif
EPC Gen2 semantics layer for UHF-RFID reader units.
Definition uhf_layer.hpp:39
bool nxpEASAlarm(std::vector< uint8_t > &alarm)
Ask the field whether any tag has its Product Status Flag asserted.
Definition uhf_layer.cpp:755
Result readBlockPermalock(std::vector< uint8_t > &mask, const Bank bank, const uint16_t block_pointer=0, const uint8_t block_range=1)
Read which blocks of a bank the selected tag has permanently locked.
Definition uhf_layer.cpp:567
Result toggleNxpConfigWord(uint16_t &config, const uint16_t toggle)
Invert bits of the Config-Word of an NXP UCODE G2X.
Definition uhf_layer.cpp:718
Result kill(const Tag &tag, const uint32_t kill_password)
Kill a tag permanently.
Definition uhf_layer.cpp:674
Result lock(const std::vector< LockSetting > &settings, const bool allow_permanent=false)
Change the lock state of memory banks and passwords.
Definition uhf_layer.cpp:522
Result nxpReadProtect(const bool protect)
Turn the read protection of an NXP UCODE G2X on or off.
Definition uhf_layer.cpp:765
Result writeNxpEAS(const bool enable)
Set or clear the Product Status Flag of an NXP UCODE G2X.
Definition uhf_layer.cpp:737
const Tag & selectedTag() const
The tag currently being addressed.
Definition uhf_layer.hpp:124
Result blockPermalock(const Bank bank, const uint8_t *mask, const size_t mask_len, const bool allow_permanent, const uint16_t block_pointer=0, const uint8_t block_range=1)
Permanently lock blocks of a bank on the selected tag.
Definition uhf_layer.cpp:581
Result readNxpConfigWord(uint16_t &config)
Read the Config-Word of an NXP UCODE G2X.
Definition uhf_layer.cpp:713
bool isSelected() const
Is a tag being addressed?
Definition uhf_layer.hpp:119
Result readQTParameters(QTParameters &qt)
Read the QT control word of the selected tag.
Definition uhf_layer.cpp:604
Result readBank(std::vector< uint8_t > &out, const Bank bank, const uint16_t word_address, const uint16_t word_count)
Read a memory bank.
Definition uhf_layer.cpp:415
bool select(const Tag &tag, const uint32_t access_password=0, const bool verify=true)
Point subsequent tag operations at one tag.
Definition uhf_layer.cpp:197
bool detect(std::vector< Tag > &tags, const uint32_t timeout_ms=1000U)
Detect tags.
Definition uhf_layer.cpp:43
bool deselect()
Stop addressing a single tag, and put polling back the way select() found it.
Definition uhf_layer.cpp:254
bool identify(Tag &tag)
Read the selected tag's TID and fill in what it says about the tag.
Definition uhf_layer.cpp:264
Result writeBank(const Bank bank, const uint16_t word_address, const uint8_t *data, const uint16_t data_len)
Write a memory bank.
Definition uhf_layer.cpp:455
Result writeQTParameters(const QTParameters &qt, const bool persistent=false)
Write the QT control word of the selected tag.
Definition uhf_layer.cpp:630
bool dump()
Dump every memory bank of the selected tag.
Definition uhf_layer.cpp:380
Non-instantiable base class for UHF-RFID reader units.
Definition unit_UHFRFID.hpp:64
Top level namespace of M5Stack.
UHF-RFID (EPC Gen2) related namespace.
EPC of a tag.
Definition uhf.hpp:173
QT control word of an Impinj Monza 4QT.
Definition uhf.hpp:1410
A tag, as the EPC Gen2 standard calls it.
Definition uhf.hpp:366
TID of a tag.
Definition uhf.hpp:213
Common vocabulary for UHF-RFID (EPCglobal UHF Class 1 Gen 2 / ISO 18000-6C)
Bank
EPC Gen2 memory bank.
Definition uhf.hpp:32
m5::stl::expected< void, Reason > Result
What became of an operation that was meant to change a tag.
Definition uhf_layer.hpp:26
Base class for UHF-RFID reader units.
m5::stl::expected< void, uint8_t > TagResult
What became of an operation on a tag.
Definition unit_UHFRFID.hpp:35