Leosac  0.7.0
OpenSourceAccessControl
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 }
An implementation class that represents a Wiegand Reader.
virtual void timeout() override
This is called when the module detect a timeout.
STL namespace.
std::unique_ptr< CardReading > CardReadingUPtr
Definition: CardReading.hpp:34
virtual void signal(zmqpp::socket &sock) override
Tells the strategy implementation to send a message to the application containing the received creden...
std::unique_ptr< PinReading > PinReadingUPtr
int counter() const
Returns the number of bits read.
virtual void set_reader(WiegandReaderImpl *new_ptr) override
Update the pointer that points back to the associated reader.
The multiple modes available to wiegand reader are implemented through the strategy pattern...
virtual bool completed() const override
Did the strategy gather needed data? If this function returns true, that means that the strategy impl...
void read_reset()
Reset the "read state" of the reader, effectively cleaning the wiegand-bit-buffer and resetting the c...
When reading both a card an a PIN code.
Definition: log.hpp:38
virtual void set_reader(WiegandReaderImpl *new_ptr)
Update the pointer that points back to the associated reader.
Provide support for Wiegand devices.
Definition: Autodetect.hpp:29
const std::string & name() const
Returns the name of this reader.
WiegandCardAndPin(WiegandReaderImpl *reader, CardReadingUPtr read_card, PinReadingUPtr read_pin, std::chrono::milliseconds delay)
Create a strategy that read card and PIN code.