Leosac  0.7.0
OpenSourceAccessControl
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 }
virtual void signal(zmqpp::socket &sock) override
Tells the strategy implementation to send a message to the application containing the received creden...
virtual void reset() override
Reset the strategy, meaning that the next time timeout() is called the behavior should be the same th...
An implementation class that represents a Wiegand Reader.
This define message formatting for data source SIMPLE_WIEGAND.
virtual int get_nb_bits() const override
Returns the number of bits in the card.
int counter() const
Returns the number of bits read.
virtual const std::string & get_card_id() const override
Returns the card id that was read.
Interface for a strategy that read a card number.
Definition: CardReading.hpp:39
void read_reset()
Reset the "read state" of the reader, effectively cleaning the wiegand-bit-buffer and resetting the c...
Definition: log.hpp:38
virtual bool completed() const override
Did the strategy gather needed data? If this function returns true, that means that the strategy impl...
Provide support for Wiegand devices.
Definition: Autodetect.hpp:29
const unsigned char * buffer() const
Return a pointer to internal buffer memory.
virtual void timeout() override
This is called when the module detect a timeout.
const std::string & name() const
Returns the name of this reader.