Skip to content

Commit

Permalink
Problem: Getting card number was not smart enough.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xaqq committed Sep 16, 2015
1 parent 824c2c8 commit 66b4fab
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/core/auth/WiegandCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <boost/algorithm/string.hpp>
#include <sstream>
#include <tools/log.hpp>
#include "tools/IVisitor.hpp"
#include "WiegandCard.hpp"

Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/core/auth/WiegandCard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ namespace Leosac
uint64_t to_int() const;

protected:
/**
* Extract the card ID.
*/
uint64_t to_wiegand_26() const;

/**
* Card id
*/
Expand Down
23 changes: 22 additions & 1 deletion test/WiegandCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
}
}

0 comments on commit 66b4fab

Please sign in to comment.