Leosac  0.8.0
Open Source Access Control
SimpleWiegandStrategy.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 <iomanip>
23 #include <tools/log.hpp>
24 
25 using namespace Leosac::Module::Wiegand;
26 using namespace Leosac::Module::Wiegand::Strategy;
27 
28 
30  : CardReading(reader)
31  , ready_(false)
32  , nb_bits_(0)
33 {
34 }
35 
37 {
38  if (!reader_->counter())
39  return;
40 
41  DEBUG("timeout, buffer size = " << reader_->counter());
42  std::size_t size = ((reader_->counter() - 1) / 8) + 1;
43 
44  std::stringstream card_hex;
45 
46  for (std::size_t i = 0; i < size; ++i)
47  {
48  card_hex << std::hex << std::setfill('0') << std::setw(2)
49  << static_cast<int>(reader_->buffer()[i]);
50  if (i + 1 < size)
51  card_hex << ":";
52  }
53 
54  ready_ = true;
56  card_id_ = card_hex.str();
57 }
58 
60 {
61  return ready_;
62 }
63 
64 void SimpleWiegandStrategy::signal(zmqpp::socket &sock)
65 {
66  assert(ready_);
67  assert(card_id_.length());
68 
69  zmqpp::message msg;
71  << card_id_ << nb_bits_;
72  sock.send(msg);
73 }
74 
75 const std::string &SimpleWiegandStrategy::get_card_id() const
76 {
77  return card_id_;
78 }
79 
81 {
82  return nb_bits_;
83 }
84 
86 {
87  ready_ = false;
88  card_id_ = "";
89  nb_bits_ = 0;
91 }
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::nb_bits_
int nb_bits_
Definition: SimpleWiegandStrategy.hpp:54
Leosac::Module::Wiegand::WiegandReaderImpl
An implementation class that represents a Wiegand Reader.
Definition: WiegandReaderImpl.hpp:41
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::signal
virtual void signal(zmqpp::socket &sock) override
Tells the strategy implementation to send a message to the application containing the received creden...
Definition: SimpleWiegandStrategy.cpp:64
DEBUG
@ DEBUG
Definition: log.hpp:35
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::timeout
virtual void timeout() override
This is called when the module detect a timeout.
Definition: SimpleWiegandStrategy.cpp:36
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::get_nb_bits
virtual int get_nb_bits() const override
Returns the number of bits in the card.
Definition: SimpleWiegandStrategy.cpp:80
WiegandReaderImpl.hpp
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::reset
virtual void reset() override
Reset the strategy, meaning that the next time timeout() is called the behavior should be the same th...
Definition: SimpleWiegandStrategy.cpp:85
Leosac::Module::Wiegand::WiegandReaderImpl::read_reset
void read_reset()
Reset the "read state" of the reader, effectively cleaning the wiegand-bit-buffer and resetting the c...
Definition: WiegandReaderImpl.cpp:192
Leosac::Module::Wiegand::WiegandReaderImpl::counter
int counter() const
Returns the number of bits read.
Definition: WiegandReaderImpl.cpp:203
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::completed
virtual bool completed() const override
Did the strategy gather needed data? If this function returns true, that means that the strategy impl...
Definition: SimpleWiegandStrategy.cpp:59
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::card_id_
std::string card_id_
Definition: SimpleWiegandStrategy.hpp:55
Leosac::Module::Wiegand::Strategy
Definition: Autodetect.hpp:31
Leosac::Module::Wiegand::WiegandReaderImpl::buffer
const unsigned char * buffer() const
Return a pointer to internal buffer memory.
Definition: WiegandReaderImpl.cpp:198
Leosac::Module::Wiegand::Strategy::CardReading
Interface for a strategy that read a card number.
Definition: CardReading.hpp:39
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::get_card_id
virtual const std::string & get_card_id() const override
Returns the card id that was read.
Definition: SimpleWiegandStrategy.cpp:75
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::SimpleWiegandStrategy
SimpleWiegandStrategy(WiegandReaderImpl *reader)
Definition: SimpleWiegandStrategy.cpp:29
log.hpp
Leosac::Module::Wiegand
Provide support for Wiegand devices.
Definition: Autodetect.hpp:29
Leosac::Module::Wiegand::Strategy::SimpleWiegandStrategy::ready_
bool ready_
Definition: SimpleWiegandStrategy.hpp:53
Leosac::Module::Wiegand::Strategy::WiegandStrategy::reader_
WiegandReaderImpl * reader_
Definition: WiegandStrategy.hpp:110
Leosac::Module::Wiegand::WiegandReaderImpl::name
const std::string & name() const
Returns the name of this reader.
Definition: WiegandReaderImpl.cpp:208
Leosac::Auth::SourceType::SIMPLE_WIEGAND
@ SIMPLE_WIEGAND
This define message formatting for data source SIMPLE_WIEGAND.
SimpleWiegandStrategy.hpp