Leosac  0.8.0
Open Source Access Control
WiegandCardAndPin.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 "WiegandCardAndPin.hpp"
22 #include <tools/log.hpp>
23 
24 using namespace Leosac::Module::Wiegand;
25 using namespace Leosac::Module::Wiegand::Strategy;
26 
28  CardReadingUPtr read_card,
29  PinReadingUPtr read_pin,
30  std::chrono::milliseconds delay)
31  : WiegandStrategy(reader)
32  , read_card_strategy_(std::move(read_card))
33  , read_pin_strategy_(std::move(read_pin))
34  , delay_(delay)
35  , reading_card_(true)
36  , ready_(false)
37 {
38  time_card_read_ = std::chrono::system_clock::now();
39 }
40 
42 {
43  using namespace std::chrono;
44  auto elapsed_ms =
45  duration_cast<milliseconds>(system_clock::now() - time_card_read_);
46 
47  if (reading_card_)
48  {
49  read_card_strategy_->timeout();
50  if (read_card_strategy_->completed())
51  {
52  DEBUG("Switch to PIN. Current card id = "
53  << read_card_strategy_->get_card_id());
54  reading_card_ = false;
55  time_card_read_ = std::chrono::system_clock::now();
57  }
58  }
59  else
60  {
61  if (elapsed_ms > delay_)
62  {
63  DEBUG("Too slow to enter pin code. Aborting.");
64  reset();
65  return;
66  }
67  // if we received anything, update the last activity time to give
68  // more time to user to eventually finish to type its code.
69  if (reader_->counter())
70  time_card_read_ = std::chrono::system_clock::now();
71  read_pin_strategy_->timeout();
72  if (read_pin_strategy_->completed())
73  ready_ = true;
74  }
75 }
76 
78 {
79  return ready_;
80 }
81 
82 void WiegandCardAndPin::signal(zmqpp::socket &sock)
83 {
84  DEBUG("Card = " << read_card_strategy_->get_card_id());
85  DEBUG("Pin = " << read_pin_strategy_->get_pin());
86 
87  zmqpp::message msg;
88  msg << ("S_" + reader_->name()) << Auth::SourceType::WIEGAND_CARD_PIN
89  << read_card_strategy_->get_card_id() << read_card_strategy_->get_nb_bits()
90  << read_pin_strategy_->get_pin();
91  sock.send(msg);
92  reset();
93 }
94 
96 {
98  read_card_strategy_->set_reader(new_ptr);
99  read_pin_strategy_->set_reader(new_ptr);
100 }
101 
103 {
104  ready_ = false;
105  reading_card_ = true;
106  reader_->read_reset();
107 
108  read_card_strategy_->reset();
109  read_pin_strategy_->reset();
110 }
Leosac::Module::Wiegand::Strategy::CardReadingUPtr
std::unique_ptr< CardReading > CardReadingUPtr
Definition: CardReading.hpp:34
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::completed
virtual bool completed() const override
Did the strategy gather needed data? If this function returns true, that means that the strategy impl...
Definition: WiegandCardAndPin.cpp:77
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::reset
void reset() override
Reset self.
Definition: WiegandCardAndPin.cpp:102
Leosac::Module::Wiegand::WiegandReaderImpl
An implementation class that represents a Wiegand Reader.
Definition: WiegandReaderImpl.hpp:41
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::reading_card_
bool reading_card_
Definition: WiegandCardAndPin.hpp:76
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::ready_
bool ready_
Definition: WiegandCardAndPin.hpp:77
DEBUG
@ DEBUG
Definition: log.hpp:35
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::signal
virtual void signal(zmqpp::socket &sock) override
Tells the strategy implementation to send a message to the application containing the received creden...
Definition: WiegandCardAndPin.cpp:82
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::read_pin_strategy_
PinReadingUPtr read_pin_strategy_
Definition: WiegandCardAndPin.hpp:70
Leosac::Auth::SourceType::WIEGAND_CARD_PIN
@ WIEGAND_CARD_PIN
When reading both a card an a PIN code.
WiegandReaderImpl.hpp
Leosac::Module::Wiegand::Strategy::WiegandStrategy::set_reader
virtual void set_reader(WiegandReaderImpl *new_ptr)
Update the pointer that points back to the associated reader.
Definition: WiegandStrategy.hpp:99
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::read_card_strategy_
CardReadingUPtr read_card_strategy_
Definition: WiegandCardAndPin.hpp:69
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::Strategy::WiegandCardAndPin::WiegandCardAndPin
WiegandCardAndPin(WiegandReaderImpl *reader, CardReadingUPtr read_card, PinReadingUPtr read_pin, std::chrono::milliseconds delay)
Create a strategy that read card and PIN code.
Definition: WiegandCardAndPin.cpp:27
Leosac::Module::Wiegand::WiegandReaderImpl::counter
int counter() const
Returns the number of bits read.
Definition: WiegandReaderImpl.cpp:203
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::set_reader
virtual void set_reader(WiegandReaderImpl *new_ptr) override
Update the pointer that points back to the associated reader.
Definition: WiegandCardAndPin.cpp:95
Leosac::Module::Wiegand::Strategy::WiegandStrategy
The multiple modes available to wiegand reader are implemented through the strategy pattern.
Definition: WiegandStrategy.hpp:50
Leosac::Module::Wiegand::Strategy
Definition: Autodetect.hpp:31
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::delay_
std::chrono::milliseconds delay_
Definition: WiegandCardAndPin.hpp:72
Leosac::Module::Wiegand::Strategy::PinReadingUPtr
std::unique_ptr< PinReading > PinReadingUPtr
Definition: StrategiesFwd.hpp:33
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::timeout
virtual void timeout() override
This is called when the module detect a timeout.
Definition: WiegandCardAndPin.cpp:41
Leosac::Module::Wiegand::Strategy::WiegandCardAndPin::time_card_read_
TimePoint time_card_read_
Definition: WiegandCardAndPin.hpp:74
WiegandCardAndPin.hpp
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