Leosac  0.7.0
OpenSourceAccessControl
WiegandPinBuffered.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 
20 #include "WiegandPinBuffered.hpp"
22 #include <tools/log.hpp>
23 
24 using namespace Leosac::Module::Wiegand;
25 using namespace Leosac::Module::Wiegand::Strategy;
26 
28  : PinReading(reader)
29  , ready_(false)
30 {
31 }
32 
34 {
35  if (reader_->counter() == 0)
36  {
37  // fail silently since its a normal timeout.
38  return;
39  }
40  if (reader_->counter() != 26)
41  {
42  WARN("Expected number of bits invalid. (" << reader_->counter()
43  << " but we expected 26)");
44  reset();
45  return;
46  }
47 
48  // bits 10 to 25 are relevant.
49  // in MSB
50  unsigned int n = 0;
51  for (int i = 9; i < 26; ++i)
52  {
53  unsigned int v = ((reader_->buffer()[i / 8] >> (7 - i % 8)) & 0x01);
54  n |= v << (15 - (i - 9));
55  }
56  if (n == 65535)
57  {
58  // per HID documentation.
59  WARN("Invalid Pin Code");
60  return;
61  }
62  pin_ = std::to_string(n);
63  ready_ = true;
64 }
65 
67 {
68  return ready_;
69 }
70 
71 void WiegandPinBuffered::signal(zmqpp::socket &sock)
72 {
73  assert(ready_);
74  assert(pin_.length());
75 
76  DEBUG("Sending PIN Code: " << pin_);
77  zmqpp::message msg;
78  msg << ("S_" + reader_->name()) << Leosac::Auth::SourceType::WIEGAND_PIN << pin_;
79  sock.send(msg);
80 }
81 
82 const std::string &WiegandPinBuffered::get_pin() const
83 {
84  return pin_;
85 }
86 
88 {
90  ready_ = false;
91  pin_ = "";
92 }
virtual void signal(zmqpp::socket &sock) override
Tells the strategy implementation to send a message to the application containing the received creden...
An implementation class that represents a Wiegand Reader.
Interface for a strategy that read a PIN code.
Definition: PinReading.hpp:37
virtual const std::string & get_pin() const override
Retrieve the pin code that was read from the reader.
int counter() const
Returns the number of bits read.
Definition: log.hpp:35
virtual void timeout() override
This is called when the module detect a timeout.
Message formatting when using a simple PIN code.
WiegandPinBuffered(WiegandReaderImpl *reader)
Create a strategy that read 4bits-per-key PIN code.
void read_reset()
Reset the "read state" of the reader, effectively cleaning the wiegand-bit-buffer and resetting the c...
Definition: log.hpp:38
Provide support for Wiegand devices.
Definition: Autodetect.hpp:29
const unsigned char * buffer() const
Return a pointer to internal buffer memory.
const std::string & name() const
Returns the name of this reader.
virtual bool completed() const override
Did the strategy gather needed data? If this function returns true, that means that the strategy impl...
virtual void reset() override
Reset the strategy, meaning that the next time timeout() is called the behavior should be the same th...