Leosac  0.8.0
Open Source Access Control
TestAndResetModule.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 "TestAndResetModule.hpp"
21 #include "core/auth/Auth.hpp"
22 #include "tools/log.hpp"
23 
24 using namespace Leosac::Module::TestAndReset;
25 using namespace Leosac::Hardware;
26 
28 {
29 }
30 
31 TestAndResetModule::TestAndResetModule(zmqpp::context &ctx, zmqpp::socket *pipe,
32  const boost::property_tree::ptree &cfg,
33  CoreUtilsPtr utils)
34  : BaseModule(ctx, pipe, cfg, utils)
35  , kernel_sock_(ctx, zmqpp::socket_type::req)
36  , sub_(ctx, zmqpp::socket_type::sub)
37  , test_led_(nullptr)
38  , test_buzzer_(nullptr)
39  , run_on_start_(true)
40  , promisc_(false)
41 {
42  sub_.connect("inproc://zmq-bus-pub");
43  kernel_sock_.connect("inproc://leosac-kernel");
44 
46  reactor_.add(sub_, std::bind(&TestAndResetModule::handle_bus_msg, this));
47  if (run_on_start_)
49 }
50 
52 {
53  boost::property_tree::ptree module_config = config_.get_child("module_config");
54  std::string test_device_led = module_config.get<std::string>("test_led", "");
55  std::string test_device_buzzer =
56  module_config.get<std::string>("test_buzzer", "");
57  run_on_start_ = module_config.get<bool>("run_on_start", true);
58  promisc_ = module_config.get<bool>("promisc", false);
59  const auto &devices = module_config.get_child_optional("devices");
60 
61  if (!test_device_led.empty())
62  test_led_ = std::unique_ptr<FLED>(new FLED(ctx_, test_device_led));
63  if (!test_device_buzzer.empty())
64  test_buzzer_ = std::unique_ptr<FLED>(new FLED(ctx_, test_device_buzzer));
65 
66  if (devices)
67  {
68  for (auto &node : module_config.get_child("devices"))
69  {
70  boost::property_tree::ptree device_cfg = node.second;
71 
72  std::string device_name = device_cfg.get_child("name").data();
73  config_check(device_name);
74  std::string reset_card = device_cfg.get<std::string>("reset_card", "");
75  std::string test_card = device_cfg.get<std::string>("test_card", "");
76 
77  sub_.subscribe("S_" + device_name);
78  if (!reset_card.empty())
79  device_reset_card_[device_name] = reset_card;
80  if (!test_card.empty())
81  device_test_card_[device_name] = test_card;
82  }
83  }
84 
85  if (promisc_)
86  {
87  std::string reset_card = module_config.get<std::string>("reset_card");
88  device_reset_card_["__promisc"] = reset_card;
89  sub_.subscribe("");
90  }
91 }
92 
94 {
95  zmqpp::message msg;
96  std::string src;
98  std::string card;
99 
100  sub_.receive(msg);
101 
102  if (msg.parts() < 4)
103  return;
104  msg >> src >> type >> card;
105 
107  {
108  ERROR("Invalid auth source type ! Doing nothing.");
109  return;
110  }
111 
112  if (promisc_)
113  {
114  if (has_reset_card(card))
115  kernel_sock_.send("RESET");
116  }
117  else
118  {
119  // remove "S_" from topic string
120  src = src.substr(2, src.size());
121  if (device_reset_card_.count(src) && device_reset_card_[src] == card)
122  {
123  kernel_sock_.send("RESET");
124  }
125  }
126 
127  if (device_test_card_.count(src) && device_test_card_[src] == card)
128  {
129  INFO("Test card read.");
131  }
132 }
133 
135 {
136  DEBUG("Running test sequence...");
137  if (test_buzzer_)
138  {
139  test_buzzer_->blink(4000, 500);
140  }
141  if (test_led_)
142  {
143  test_led_->blink(4000, 500);
144  }
145  if (!test_led_ && !test_buzzer_)
146  {
147  INFO("Test sequence doing nothing...");
148  }
149 }
150 
151 bool TestAndResetModule::has_reset_card(const std::string &card_id) const
152 {
153  return std::find_if(device_reset_card_.begin(), device_reset_card_.end(),
154  [&](std::pair<std::string, std::string> p) -> bool {
155  return p.second == card_id;
156  }) != device_reset_card_.end();
157 }
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Module::TestAndReset::TestAndResetModule::run_test_sequence
void run_test_sequence()
Do some stuff to let the user known something happened.
Definition: TestAndResetModule.cpp:134
zmqpp
Definition: CoreUtils.hpp:27
Leosac::Module::BaseModule::config_check
void config_check(const std::string &obj_name, Leosac::Hardware::DeviceClass type)
An helper that checks configuration the existence of some objects.
Definition: BaseModule.cpp:143
ERROR
@ ERROR
Definition: log.hpp:32
DEBUG
@ DEBUG
Definition: log.hpp:35
Auth.hpp
INFO
@ INFO
Definition: log.hpp:34
Leosac::Module::TestAndReset::TestAndResetModule::run_on_start_
bool run_on_start_
Play the sequence on module startup.
Definition: TestAndResetModule.hpp:109
Leosac::Hardware::FLED
A Facade to a LED object.
Definition: FLED.hpp:44
Leosac::Module::TestAndReset::TestAndResetModule::TestAndResetModule
TestAndResetModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: TestAndResetModule.cpp:31
Leosac::Module::TestAndReset::TestAndResetModule::device_test_card_
std::map< std::string, std::string > device_test_card_
Map a device name to the test card.
Definition: TestAndResetModule.hpp:94
TestAndResetModule.hpp
Leosac::Module::TestAndReset::TestAndResetModule::kernel_sock_
zmqpp::socket kernel_sock_
REQ socket to kernel.
Definition: TestAndResetModule.hpp:78
Leosac::Module::TestAndReset::TestAndResetModule::has_reset_card
bool has_reset_card(const std::string &card_id) const
Search the device_reset_card_ map for an entry whose value is card_id;.
Definition: TestAndResetModule.cpp:151
Leosac::Module::TestAndReset::TestAndResetModule::process_config
void process_config()
Definition: TestAndResetModule.cpp:51
Leosac::Module::BaseModule::config_
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
Definition: BaseModule.hpp:193
Leosac::Module::TestAndReset
This module provide a way to have feedback when presenting a test card.
Definition: TestAndResetModule.hpp:35
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::TestAndReset::TestAndResetModule::~TestAndResetModule
virtual ~TestAndResetModule()
Definition: TestAndResetModule.cpp:27
Leosac::Module::TestAndReset::TestAndResetModule::promisc_
bool promisc_
Definition: TestAndResetModule.hpp:111
Leosac::Module::TestAndReset::TestAndResetModule::test_led_
std::unique_ptr< Hardware::FLED > test_led_
Led device for test card.
Definition: TestAndResetModule.hpp:99
Leosac::Module::TestAndReset::TestAndResetModule::device_reset_card_
std::map< std::string, std::string > device_reset_card_
Map a device name to the reset card, since one device can support one reset card currently.
Definition: TestAndResetModule.hpp:89
Leosac::Module::TestAndReset::TestAndResetModule::test_buzzer_
std::unique_ptr< Hardware::FLED > test_buzzer_
Buzzer device for test card.
Definition: TestAndResetModule.hpp:104
Leosac::Module::TestAndReset::TestAndResetModule::handle_bus_msg
void handle_bus_msg()
Definition: TestAndResetModule.cpp:93
log.hpp
Leosac::Module::TestAndReset::TestAndResetModule::sub_
zmqpp::socket sub_
Sub socket on the BUS.
Definition: TestAndResetModule.hpp:83
Leosac::Auth::SourceType
SourceType
Definition: Auth.hpp:29
Leosac::Module::BaseModule::ctx_
zmqpp::context & ctx_
A reference to the ZeroMQ context in case you need it to create additional socket.
Definition: BaseModule.hpp:183
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Hardware
Provides facade classes to hardware device implementation.
Definition: Buzzer.cpp:25
Leosac::Auth::SourceType::SIMPLE_WIEGAND
@ SIMPLE_WIEGAND
This define message formatting for data source SIMPLE_WIEGAND.