From 2c6e3c0af904e953dbb96e8825e0512fa78af003 Mon Sep 17 00:00:00 2001 From: Pascal Brunot Date: Thu, 25 Apr 2024 10:17:55 +0200 Subject: [PATCH] Linter fixes --- include/Mrfc522Driver.hpp | 7 ++++--- include/RFIDWrapper.hpp | 2 +- include/Tasks.hpp | 3 --- include/WhiteList.hpp | 4 ++-- include/card.hpp | 15 ++++++++++++++- include/mock/MockMrfc522.hpp | 8 +++++--- src/Mrfc522Driver.cpp | 6 +++--- src/RFIDWrapper.tpp | 9 ++++----- src/Tasks.cpp | 2 +- src/mock/MockMrfc522.cpp | 8 +++++--- 10 files changed, 39 insertions(+), 25 deletions(-) diff --git a/include/Mrfc522Driver.hpp b/include/Mrfc522Driver.hpp index 5c9cb602..04f45ec2 100644 --- a/include/Mrfc522Driver.hpp +++ b/include/Mrfc522Driver.hpp @@ -3,6 +3,7 @@ #include "Arduino.h" #include +#include #include "FabUser.hpp" #include "MFRC522Debug.h" @@ -24,9 +25,9 @@ namespace fablabbg public: struct UidDriver { - byte size; // Number of bytes in the UID. 4, 7 or 10. - byte uidByte[10]; - byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. + byte size; // Number of bytes in the UID. 4, 7 or 10. + std::array uidByte{0}; // The UID (Unique Identifier) of the PICC. + byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. }; Mrfc522Driver(); diff --git a/include/RFIDWrapper.hpp b/include/RFIDWrapper.hpp index a90a8063..77fa22a2 100644 --- a/include/RFIDWrapper.hpp +++ b/include/RFIDWrapper.hpp @@ -51,7 +51,7 @@ namespace fablabbg RFIDWrapper &operator=(const RFIDWrapper &x) = delete; // copy assignment RFIDWrapper(RFIDWrapper &&) = delete; // move constructor RFIDWrapper &operator=(RFIDWrapper &&) = delete; // move assignment - ~RFIDWrapper() = default; // Default destructor + ~RFIDWrapper() override{}; // Default destructor }; } // namespace fablabbg diff --git a/include/Tasks.hpp b/include/Tasks.hpp index 21c604ac..edbecee0 100644 --- a/include/Tasks.hpp +++ b/include/Tasks.hpp @@ -105,9 +105,6 @@ namespace fablabbg::Tasks class Scheduler { public: - Scheduler() : tasks{} {}; - ~Scheduler() = default; - auto addTask(Task &task) -> void; auto removeTask(const Task &task) -> void; diff --git a/include/WhiteList.hpp b/include/WhiteList.hpp index 72453bc9..e858970b 100644 --- a/include/WhiteList.hpp +++ b/include/WhiteList.hpp @@ -14,7 +14,7 @@ namespace fablabbg namespace conf::cards { static constexpr auto LEN = 10U; /* Number of whitelisted cards */ - } + } // namespace conf::cards using WhiteListEntry = std::tuple; using WhiteList = std::array; @@ -47,6 +47,6 @@ namespace fablabbg return sorted1 == sorted2; } -} +} // namespace fablabbg #endif // WHITELIST_HPP diff --git a/include/card.hpp b/include/card.hpp index f39b7042..d0135b38 100644 --- a/include/card.hpp +++ b/include/card.hpp @@ -12,7 +12,7 @@ namespace fablabbg::card { using uid_t = u_int64_t; static constexpr uid_t INVALID = 0ULL; - + /// @brief Returns a string representation of the UID /// @param uid number to convert /// @return an hex string representation of the UID (e.g. "123456ADCD") @@ -42,6 +42,19 @@ namespace fablabbg::card return result; } + /// @brief Converts a UID from an array of bytes to a number + /// @param uid array of bytes + /// @return the UID as a number + [[nodiscard]] constexpr inline auto to_array(const uid_t uid) -> std::array + { + std::array retVal{0}; + for (auto i = (conf::rfid_tags::UID_BYTE_LEN - 1); i >= 0; i--) + { + retVal[i] = (uid >> (i * 8)) & 0xFF; + } + return retVal; + } + inline void print(uint64_t uid) { ESP_LOGI(TAG, "Card UID = %s", card::uid_str(uid).c_str()); diff --git a/include/mock/MockMrfc522.hpp b/include/mock/MockMrfc522.hpp index 2d1570e7..566ab24c 100644 --- a/include/mock/MockMrfc522.hpp +++ b/include/mock/MockMrfc522.hpp @@ -2,6 +2,8 @@ #define MOCK_MOCKMRFC522_HPP_ #include "Arduino.h" + +#include #include #include @@ -22,9 +24,9 @@ namespace fablabbg public: struct UidDriver { - byte size; // Number of bytes in the UID. 4, 7 or 10. - byte uidByte[10]; - byte sak; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. + byte size{4}; // Number of bytes in the UID. 4, 7 or 10. + std::array uidByte{0}; + byte sak{0}; // The SAK (Select acknowledge) byte returned from the PICC after successful selection. }; constexpr MockMrfc522(){}; diff --git a/src/Mrfc522Driver.cpp b/src/Mrfc522Driver.cpp index 3cbf413c..f4a30aab 100644 --- a/src/Mrfc522Driver.cpp +++ b/src/Mrfc522Driver.cpp @@ -1,4 +1,5 @@ #include "Arduino.h" +#include #include #include "MFRC522DriverPinSimple.h" @@ -57,11 +58,10 @@ namespace fablabbg bool Mrfc522Driver::PCD_PerformSelfTest() { return mfrc522->PCD_PerformSelfTest(); } - Mrfc522Driver::UidDriver Mrfc522Driver::getDriverUid() const + auto Mrfc522Driver::getDriverUid() const -> UidDriver { UidDriver retVal{}; - memset(&retVal, 0, sizeof(retVal)); - memcpy(retVal.uidByte, mfrc522->uid.uidByte, sizeof(retVal.uidByte)); + std::copy(mfrc522->uid.uidByte, mfrc522->uid.uidByte + 10, retVal.uidByte.begin()); retVal.size = mfrc522->uid.size; retVal.sak = mfrc522->uid.sak; return retVal; diff --git a/src/RFIDWrapper.tpp b/src/RFIDWrapper.tpp index 0da383a2..3a98e568 100644 --- a/src/RFIDWrapper.tpp +++ b/src/RFIDWrapper.tpp @@ -1,3 +1,4 @@ +#include #include #include "Logging.hpp" @@ -94,11 +95,9 @@ namespace fablabbg { std::array arr{0}; auto const &uid = driver->getDriverUid(); - memcpy(arr.data(), uid.uidByte, std::min(conf::rfid_tags::UID_BYTE_LEN, uid.size)); - - auto c = card::from_array(arr); - - return c; + size_t size = std::min(uid.size, conf::rfid_tags::UID_BYTE_LEN); + std::copy(uid.uidByte.cbegin(), uid.uidByte.cbegin() + size, arr.begin()); + return card::from_array(arr); } /// @brief Initializes RFID chip including self test diff --git a/src/Tasks.cpp b/src/Tasks.cpp index c1a0ba0b..a80ce9f9 100644 --- a/src/Tasks.cpp +++ b/src/Tasks.cpp @@ -81,7 +81,7 @@ namespace fablabbg::Tasks auto Scheduler::execute() const -> void { // Tasks shall be run in order of expiration (the most expired task shall run first) - std::vector iters{0}; + std::vector iters; for (auto it = tasks.begin(); it != tasks.end(); ++it) { iters.push_back(it); // Vector of iterators diff --git a/src/mock/MockMrfc522.cpp b/src/mock/MockMrfc522.cpp index 438bae79..e33b944d 100644 --- a/src/mock/MockMrfc522.cpp +++ b/src/mock/MockMrfc522.cpp @@ -1,15 +1,17 @@ #include "mock/MockMrfc522.hpp" +#include + namespace fablabbg { auto MockMrfc522::getDriverUid() const -> MockMrfc522::UidDriver { UidDriver retVal{}; - memset(&retVal, 0, sizeof(retVal)); - if (getSimulatedUid().has_value()) + if (auto sim = getSimulatedUid(); sim.has_value()) { - memcpy(retVal.uidByte, &uid.value(), sizeof(uid.value())); retVal.size = sizeof(uid.value()); + auto uid = card::to_array(sim.value()); + std::copy(uid.begin(), uid.end(), retVal.uidByte.begin()); retVal.sak = 1; } return retVal;