Leosac  0.8.0
Open Source Access Control
EventPublish.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 "EventPublish.h"
21 #include <core/auth/Auth.hpp>
22 
23 using namespace Leosac::Module::EventPublish;
24 
25 EventPublish::EventPublish(zmqpp::context &ctx, zmqpp::socket *pipe,
26  const boost::property_tree::ptree &cfg,
27  CoreUtilsPtr utils)
28  : BaseModule(ctx, pipe, cfg, utils)
29  , bus_sub_(ctx, zmqpp::socket_type::sub)
30  , network_pub_(ctx, zmqpp::socket_type::pub)
31 {
32  bus_sub_.connect("inproc://zmq-bus-pub");
34  reactor_.add(bus_sub_, std::bind(&EventPublish::handle_msg_bus, this));
35 }
36 
37 void EventPublish::handle_msg_bus()
38 {
39  zmqpp::message msg;
40  std::string src;
42  std::string card;
43  int bits;
44 
45  bus_sub_.receive(msg);
46 
47  if (msg.parts() < 4)
48  return;
49  msg >> src >> type >> card >> bits;
50  INFO("Will publish card id {" << card << "}");
51 
53  {
54  INFO("EventPublish cannot handle this type of credential yet.");
55  return;
56  }
57 
58  // Hack Alert! Hack Alert!
59  // For this use case, we read 35bits wiegand frame CP1000 encoded.
60  // We want to extract the identifier: the 20 second-to-last bits (14 first bit
61  // ignored).
62  // In case this is not a 35 bits tram, do nothing
63  if (bits == 35)
64  {
65  std::stringstream ss;
66  ss << std::hex << "0x";
67  for (auto c : card)
68  {
69  if (c == ':')
70  continue;
71  ss << std::hex << c;
72  }
73  INFO("Bla: {" << ss.str() << "}");
74  // n has 40 "meaningful" bits because the hex string was 5 bytes.
75  uint64_t n;
76  ss >> n;
77  INFO(" n = " << n);
78  n = (n >> 6); // remove last 6bits. parity bit + 5bits;
79  n &= 0xFFFFF; // keep 20 bits.
80  INFO(" n2 = " << n);
81  std::stringstream ss2;
82  ss2 << n;
83  ss2 >> card;
84  }
85  else
86  return;
87 
88  if (publish_source_)
89  network_pub_.send(zmqpp::message() << card << src);
90  else
91  network_pub_.send(card);
92 }
93 
94 void EventPublish::process_config()
95 {
96  auto port = config_.get<int>("module_config.port");
97  network_pub_.bind("tcp://*:" + std::to_string(port));
98  publish_source_ = config_.get<bool>("module_config.publish_source", false);
99 
100  for (auto &&itr : config_.get_child("module_config.sources"))
101  {
102  auto name = itr.second.get<std::string>("");
103  bus_sub_.subscribe("S_" + name);
104  }
105 }
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Module::EventPublish::EventPublish::process_config
void process_config()
Processing the configuration tree, spawning AuthFileInstance object as described in the configuration...
Definition: EventPublish.cpp:94
zmqpp
Definition: CoreUtils.hpp:27
Leosac::Module::EventPublish::EventPublish::bus_sub_
zmqpp::socket bus_sub_
Read internal message bus.
Definition: EventPublish.h:74
Leosac::Module::EventPublish::EventPublish::publish_source_
bool publish_source_
Definition: EventPublish.h:81
Auth.hpp
INFO
@ INFO
Definition: log.hpp:34
Leosac::Module::EventPublish
Module that publish wiegand auth event on a ZMQ publisher.
Definition: EventPublish.h:40
Leosac::Module::BaseModule::config_
boost::property_tree::ptree config_
The configuration tree passed to the start_module function.
Definition: BaseModule.hpp:193
Leosac::Module::BaseModule::reactor_
zmqpp::reactor reactor_
The reactor object we poll() on in the main loop.
Definition: BaseModule.hpp:214
Leosac::Module::EventPublish::EventPublish::handle_msg_bus
void handle_msg_bus()
Definition: EventPublish.cpp:37
Leosac::Module::EventPublish::EventPublish::network_pub_
zmqpp::socket network_pub_
Publish to the internet.
Definition: EventPublish.h:79
Leosac::Module::EventPublish::EventPublish
Run the Event publication module.
Definition: EventPublish.h:48
Leosac::Auth::SourceType
SourceType
Definition: Auth.hpp:29
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Auth::SourceType::SIMPLE_WIEGAND
@ SIMPLE_WIEGAND
This define message formatting for data source SIMPLE_WIEGAND.
EventPublish.h