Leosac  0.8.0
Open Source Access Control
RFIDCard.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
22 #include "tools/log.hpp"
23 #include <boost/algorithm/string.hpp>
24 
25 using namespace Leosac;
26 using namespace Leosac::Cred;
27 
28 RFIDCard::RFIDCard(const std::string &new_card_id, int new_nb_bits)
29 {
30  card_id(new_card_id);
31  nb_bits(new_nb_bits);
32 }
33 
34 const std::string &RFIDCard::card_id() const
35 {
36  return card_id_;
37 }
38 
39 int RFIDCard::nb_bits() const
40 {
41  return nb_bits_;
42 }
43 
44 uint64_t RFIDCard::to_raw_int() const
45 {
46  auto card_num_hex = boost::replace_all_copy(card_id_, ":", "");
47 
48  static_assert(sizeof(decltype(std::stoull(card_num_hex, nullptr, 16))) ==
49  sizeof(uint64_t),
50  "stoul not big enough");
51 
52  uint64_t tmp = std::stoull(card_num_hex, nullptr, 16);
53  int trailing_zero = (64 - nb_bits_) % 8;
54  tmp >>= trailing_zero;
55  return tmp;
56 }
57 
58 uint64_t RFIDCard::to_int() const
59 {
60  switch (nb_bits_)
61  {
62  case 26:
63  return to_wiegand_26();
64  case 34:
65  return to_wiegand_34();
66  default:
67  INFO("Not using format to convert WiegandCard to integer because no format "
68  "match.");
69  return to_raw_int();
70  }
71 }
72 
73 uint64_t RFIDCard::to_wiegand_26() const
74 {
75  assert(nb_bits_ == 26);
76  assert(card_id_.size() == 2 * 4 + 3);
77 
78  uint64_t tmp = to_raw_int();
79  // Drop the last bit (parity) from the raw frame.
80  tmp >>= 1;
81  // keep 16 bits
82  tmp &= 0xFFFF;
83  return tmp;
84 }
85 
86 uint64_t RFIDCard::to_wiegand_34() const
87 {
88  assert(nb_bits_ == 34);
89  assert(card_id_.size() == 2 * 5 + 4);
90 
91  uint64_t tmp = to_raw_int();
92  // Drop the last bit (parity) from the raw frame.
93  tmp >>= 1;
94  // keep 24 bits
95  tmp &= 0xFFFFFF;
96  return tmp;
97 }
98 
99 void RFIDCard::nb_bits(int i)
100 {
102  nb_bits_ = i;
103 }
104 
105 void RFIDCard::card_id(const std::string &id)
106 {
108  card_id_ = id;
109 }
110 
112 {
113  validate_card_id(card.card_id());
114  validate_nb_bits(card.nb_bits());
115 }
116 
117 void RFIDCardValidator::validate_card_id(const std::string &card_id)
118 {
119  bool fail = false;
120  char c;
121  std::istringstream ss(card_id);
122  while (true)
123  {
124  c = 0;
125  for (int i = 0; i < 2; ++i)
126  {
127  ss >> c;
128  if (!isxdigit(c) || !ss.good())
129  fail = true;
130  }
131  ss >> c;
132  if (!ss.good())
133  break;
134  if (c != ':')
135  fail = true;
136  }
137  if (fail)
138  {
139  throw ModelException("data/attributes/cardId",
140  "Card id must have aa:bb:cc:11 format.");
141  }
142 }
143 
145 {
146  if (nb <= 0)
147  {
148  throw ModelException("data/attributes/nbBits",
149  "The number of bits must be > 0");
150  }
151 }
Leosac::Cred::Credential::id
virtual CredentialId id() const override
Retrieve the identifier of the credential.
Definition: Credential.cpp:48
Leosac::Cred::RFIDCardValidator::validate
static void validate(const IRFIDCard &card)
Definition: RFIDCard.cpp:111
Leosac::Cred::RFIDCard::to_wiegand_34
uint64_t to_wiegand_34() const
Extract the card ID, assuming the format to be Wiegand34.
Definition: RFIDCard.cpp:86
Leosac::Cred::RFIDCard::nb_bits
virtual int nb_bits() const override
Definition: RFIDCard.cpp:39
RFIDCard.hpp
Leosac::Cred::IRFIDCard::card_id
virtual const std::string & card_id() const =0
INFO
@ INFO
Definition: log.hpp:34
Leosac::Cred::RFIDCard::nb_bits_
int nb_bits_
Definition: RFIDCard.hpp:61
Leosac::Cred::RFIDCardValidator::validate_nb_bits
static void validate_nb_bits(int nb_bits)
Definition: RFIDCard.cpp:144
Leosac::Cred::RFIDCard::card_id_
std::string card_id_
Definition: RFIDCard.hpp:62
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Cred::IRFIDCard::nb_bits
virtual int nb_bits() const =0
Leosac::Cred::RFIDCard::RFIDCard
RFIDCard()=default
Leosac::Cred::RFIDCardValidator::validate_card_id
static void validate_card_id(const std::string &card_id)
Definition: RFIDCard.cpp:117
ModelException.hpp
Leosac::Cred::RFIDCard::card_id
virtual const std::string & card_id() const override
Definition: RFIDCard.cpp:34
Leosac::Cred::RFIDCard::to_wiegand_26
uint64_t to_wiegand_26() const
Extract the card ID, assuming the format to be Wiegand26.
Definition: RFIDCard.cpp:73
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::Cred::RFIDCard::to_int
virtual uint64_t to_int() const override
Returns the integer representation of the card ID.
Definition: RFIDCard.cpp:58
Leosac::Cred
Definition: Credential.hpp:31
log.hpp
Leosac::Cred::RFIDCard::to_raw_int
virtual uint64_t to_raw_int() const override
Convert the bits of the card to an integer.
Definition: RFIDCard.cpp:44
Leosac::Cred::IRFIDCard
Interface for RFIDCard credential.
Definition: IRFIDCard.hpp:32