Leosac  0.8.0
Open Source Access Control
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 }
WiegandPinBuffered.hpp
WARN
@ WARN
Definition: log.hpp:33
Leosac::Module::Wiegand::WiegandReaderImpl
An implementation class that represents a Wiegand Reader.
Definition: WiegandReaderImpl.hpp:41
DEBUG
@ DEBUG
Definition: log.hpp:35
Leosac::Auth::SourceType::WIEGAND_PIN
@ WIEGAND_PIN
Message formatting when using a simple PIN code.
Leosac::Module::Wiegand::Strategy::WiegandPinBuffered::get_pin
virtual const std::string & get_pin() const override
Retrieve the pin code that was read from the reader.
Definition: WiegandPinBuffered.cpp:82
Leosac::Module::Wiegand::Strategy::PinReading
Interface for a strategy that read a PIN code.
Definition: PinReading.hpp:37
Leosac::Module::Wiegand::Strategy::WiegandPinBuffered::completed
virtual bool completed() const override
Did the strategy gather needed data? If this function returns true, that means that the strategy impl...
Definition: WiegandPinBuffered.cpp:66
WiegandReaderImpl.hpp
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
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::WiegandPinBuffered::WiegandPinBuffered
WiegandPinBuffered(WiegandReaderImpl *reader)
Create a strategy that read 4bits-per-key PIN code.
Definition: WiegandPinBuffered.cpp:27
Leosac::Module::Wiegand::Strategy::WiegandPinBuffered::signal
virtual void signal(zmqpp::socket &sock) override
Tells the strategy implementation to send a message to the application containing the received creden...
Definition: WiegandPinBuffered.cpp:71
Leosac::Module::Wiegand::Strategy::WiegandPinBuffered::reset
virtual void reset() override
Reset the strategy, meaning that the next time timeout() is called the behavior should be the same th...
Definition: WiegandPinBuffered.cpp:87
Leosac::Module::Wiegand::Strategy::WiegandPinBuffered::ready_
bool ready_
Definition: WiegandPinBuffered.hpp:57
Leosac::Module::Wiegand::Strategy::WiegandPinBuffered::timeout
virtual void timeout() override
This is called when the module detect a timeout.
Definition: WiegandPinBuffered.cpp:33
log.hpp
Leosac::Module::Wiegand
Provide support for Wiegand devices.
Definition: Autodetect.hpp:29
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::Module::Wiegand::Strategy::WiegandPinBuffered::pin_
std::string pin_
Definition: WiegandPinBuffered.hpp:58