From 66b4fabac6b13dae96c4088739c4c3fe203523ad Mon Sep 17 00:00:00 2001 From: xaqq Date: Wed, 16 Sep 2015 18:19:19 +0200 Subject: [PATCH] Problem: Getting card number was not smart enough. When getting the card ID from a WiegandCard object, if the format is Wiegand 26, we will extract the 16bits corresponding to the card ID instead of generating a card number from all the bits. --- src/core/auth/WiegandCard.cpp | 29 ++++++++++++++++++++++++++++- src/core/auth/WiegandCard.hpp | 5 +++++ test/WiegandCard.cpp | 23 ++++++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/core/auth/WiegandCard.cpp b/src/core/auth/WiegandCard.cpp index efa36d650..fcd24dbb9 100644 --- a/src/core/auth/WiegandCard.cpp +++ b/src/core/auth/WiegandCard.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "tools/IVisitor.hpp" #include "WiegandCard.hpp" @@ -60,6 +61,32 @@ std::string WiegandCard::to_string() const uint64_t WiegandCard::to_int() const { + switch (nb_bits_) + { + case 26: + return to_wiegand_26(); + default: + { + auto card_num_hex = boost::replace_all_copy(card_id_, ":", ""); + return std::stoul(card_num_hex, nullptr, 16); + } + } +} + +uint64_t WiegandCard::to_wiegand_26() const +{ + assert(nb_bits_ == 26); + assert(card_id_.size() == 2*4 + 3); + auto card_num_hex = boost::replace_all_copy(card_id_, ":", ""); - return std::stoul(card_num_hex, nullptr, 16); + uint64_t tmp = std::stoul(card_num_hex, nullptr, 16); + + // we have 32 bits of data (8 hex character) + + // we want to drop the last bit from wiegand 26 frame. + // so drop 7 bits (6 useless (32-26) + last one) + tmp = tmp >> 7; + // keep 16 bits + tmp &= 0xFFFF; + return tmp; } diff --git a/src/core/auth/WiegandCard.hpp b/src/core/auth/WiegandCard.hpp index d7c9eca4c..7f38499c1 100644 --- a/src/core/auth/WiegandCard.hpp +++ b/src/core/auth/WiegandCard.hpp @@ -65,6 +65,11 @@ namespace Leosac uint64_t to_int() const; protected: + /** + * Extract the card ID. + */ + uint64_t to_wiegand_26() const; + /** * Card id */ diff --git a/test/WiegandCard.cpp b/test/WiegandCard.cpp index 80f6b3981..d55f31e39 100644 --- a/test/WiegandCard.cpp +++ b/test/WiegandCard.cpp @@ -35,7 +35,7 @@ namespace Leosac WiegandCard c2("aa:bb:cc:dd", 32); ASSERT_EQ(2864434397, c2.to_int()); - WiegandCard c3("39:4B:C5:8", 26); + WiegandCard c3("39:4B:C5:8", 32); ASSERT_EQ(60079192, c3.to_int()); WiegandCard c4("00:00:00:01", 32); @@ -44,5 +44,26 @@ namespace Leosac WiegandCard c5("00:00:00:10", 32); ASSERT_EQ(16, c5.to_int()); } + + TEST(TestWiegandCard, wiegand_26) + { + WiegandCard c1("80:80:33:80", 26); + ASSERT_EQ(103, c1.to_int()); + + WiegandCard c2("80:80:33:40", 26); + ASSERT_EQ(102, c2.to_int()); + + WiegandCard c3("80:80:40:00", 26); + ASSERT_EQ(128, c3.to_int()); + + WiegandCard c4("80:80:41:40", 26); + ASSERT_EQ(130, c4.to_int()); + + WiegandCard c5("80:80:41:80", 26); + ASSERT_EQ(131, c5.to_int()); + + WiegandCard c6("80:80:43:00", 26); + ASSERT_EQ(134, c6.to_int()); + } } }