Leosac  0.8.0
Open Source Access Control
RplethModule.hpp
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 #pragma once
21 
23 #include "modules/BaseModule.hpp"
25 #include "rplethpacket.hpp"
26 #include <memory>
27 #include <vector>
28 
29 namespace Leosac
30 {
31 namespace Module
32 {
37 namespace Rpleth
38 {
43 class RplethModule : public BaseModule
44 {
45  public:
46  RplethModule(zmqpp::context &ctx, zmqpp::socket *pipe,
47  const boost::property_tree::ptree &cfg, CoreUtilsPtr utils);
48 
49  ~RplethModule();
50 
51  RplethModule(const RplethModule &) = delete;
52 
53  RplethModule &operator=(const RplethModule &) = delete;
54 
64  static std::vector<uint8_t>
65  card_convert_from_text(const std::pair<std::string, int> &card_info);
66 
67  private:
68  void process_config();
69 
73  bool client_connected(const std::string &identity) const;
74 
79  bool client_failed(const std::string &identity) const;
80 
81  static constexpr int buffer_size = 8192;
82 
98  bool handle_client_msg(const std::string &client_identity, CircularBuffer &buf);
99 
104 
108  void handle_socket();
109 
115  void rpleth_send_cards(const RplethPacket &packet);
116 
123 
127  void rpleth_beep(const RplethPacket &p);
128 
132  void rpleth_greenled(const RplethPacket &p);
133 
138 
143 
148 
153 
158 
159  void restart_reader();
160 
164  boost::property_tree::ptree get_network_config();
165 
169  bool push_network_config(const boost::property_tree::ptree &tree);
170 
175  void rpleth_publish_card();
176 
182  void handle_wiegand_event();
183 
187  std::list<std::string> cards_pushed_;
188 
193  std::list<std::string> cards_read_;
194 
199  std::list<std::pair<std::string, int>> cards_read_stream_;
200 
201  std::map<std::string, CircularBuffer> clients_;
202 
203  zmqpp::context &ctx_;
204 
208  zmqpp::socket server_;
209 
214  zmqpp::socket bus_sub_;
215 
219  zmqpp::socket core_;
220 
224  std::unique_ptr<Hardware::FWiegandReader> reader_;
225 
232  std::vector<std::string> failed_clients_;
233 
235 };
236 }
237 }
238 }
Leosac::Module::Rpleth::RplethModule::cards_read_stream_
std::list< std::pair< std::string, int > > cards_read_stream_
If stream mode is on, all cards read are stored here.
Definition: RplethModule.hpp:199
Leosac::Module::Rpleth::RplethModule::rpleth_beep
void rpleth_beep(const RplethPacket &p)
Handle Rpleth Beep command.
Definition: RplethModule.cpp:302
Leosac::Module::BaseModule
Base class for module implementation.
Definition: BaseModule.hpp:110
Leosac::Module::Rpleth::RplethModule::push_network_config
bool push_network_config(const boost::property_tree::ptree &tree)
Push a configuration to the core as the new network config.
Definition: RplethModule.cpp:553
Leosac::Module::Rpleth::RplethModule::rpleth_greenled
void rpleth_greenled(const RplethPacket &p)
Handle rpleth greenled command.
Definition: RplethModule.cpp:317
Leosac::Module::Rpleth::RplethModule::rpleth_receive_cards
RplethPacket rpleth_receive_cards(const RplethPacket &packet)
Handle Rpleth ReceiveUnpresentedCards command.
Definition: RplethModule.cpp:254
circularbuffer.hpp
simple circular buffer class
Leosac::Module::Rpleth::RplethModule::rpleth_publish_card
void rpleth_publish_card()
Flush the cards_read_stream_ list to clients.
Definition: RplethModule.cpp:343
Leosac::Module::Rpleth::RplethModule::reader_
std::unique_ptr< Hardware::FWiegandReader > reader_
Interface to the reader.
Definition: RplethModule.hpp:224
Leosac::Module::Rpleth::RplethModule::set_dhcp_state
RplethPacket set_dhcp_state(const RplethPacket &p)
Enable or disable DHCP.
Definition: RplethModule.cpp:431
Leosac::Module::Rpleth::RplethPacket
Definition: rplethpacket.hpp:39
BaseModule.hpp
Leosac::Module::Rpleth::RplethModule::restart_reader
void restart_reader()
Definition: RplethModule.cpp:584
Leosac::Module::Rpleth::RplethModule::clients_
std::map< std::string, CircularBuffer > clients_
Definition: RplethModule.hpp:201
Leosac::Module::Rpleth::RplethModule::server_
zmqpp::socket server_
Stream socket to receive Rpleth connection.
Definition: RplethModule.hpp:208
Leosac::Module::Rpleth::RplethModule::~RplethModule
~RplethModule()
Definition: RplethModule.cpp:52
Leosac::Module::Rpleth::RplethModule::RplethModule
RplethModule(zmqpp::context &ctx, zmqpp::socket *pipe, const boost::property_tree::ptree &cfg, CoreUtilsPtr utils)
Definition: RplethModule.cpp:33
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::Rpleth::RplethModule::handle_client_packet
RplethPacket handle_client_packet(RplethPacket packet)
If we successfully built a packet, lets handle it.
Definition: RplethModule.cpp:140
Leosac::Module::Rpleth::RplethModule::buffer_size
static constexpr int buffer_size
Definition: RplethModule.hpp:81
Leosac::Module::Rpleth::RplethModule::process_config
void process_config()
Definition: RplethModule.cpp:56
Leosac::Module::Rpleth::RplethModule::bus_sub_
zmqpp::socket bus_sub_
Subscribe to the message bus and listen for event sent by the wiegand reader we watch.
Definition: RplethModule.hpp:214
Leosac::Module::Rpleth::RplethModule::set_reader_ip
RplethPacket set_reader_ip(const RplethPacket &p)
Update the IP of this Leosac unit.
Definition: RplethModule.cpp:456
Leosac::Module::Rpleth::RplethModule::handle_client_msg
bool handle_client_msg(const std::string &client_identity, CircularBuffer &buf)
Try to handle a client message.
Definition: RplethModule.cpp:110
Leosac::Module::Rpleth::RplethModule::ctx_
zmqpp::context & ctx_
Definition: RplethModule.hpp:203
rplethpacket.hpp
Rpleth packet class.
Leosac::Module::Rpleth::RplethModule::stream_mode_
bool stream_mode_
Definition: RplethModule.hpp:234
Leosac::Module::Rpleth::RplethModule::get_dhcp_state
RplethPacket get_dhcp_state()
Handle Rpleth GetDHCP command.
Definition: RplethModule.cpp:402
Leosac::Module::Rpleth::RplethModule::operator=
RplethModule & operator=(const RplethModule &)=delete
Leosac::Module::Rpleth::RplethModule::core_
zmqpp::socket core_
REQ socket to core.
Definition: RplethModule.hpp:219
Leosac::Module::Rpleth::RplethModule
Main class for the Rpleth module.
Definition: RplethModule.hpp:43
Leosac::Module::Rpleth::RplethModule::card_convert_from_text
static std::vector< uint8_t > card_convert_from_text(const std::pair< std::string, int > &card_info)
Convert a card number from textual hexadecimal representation to a 8 bytes byte-vector in Network Byt...
Definition: RplethModule.cpp:386
Leosac::Module::Rpleth::RplethModule::rpleth_send_cards
void rpleth_send_cards(const RplethPacket &packet)
Handle Rpleth SendCards command: we will store the list of received card somewhere safe.
Definition: RplethModule.cpp:225
Leosac::Module::Rpleth::RplethModule::handle_socket
void handle_socket()
Handle data available on server socket.
Definition: RplethModule.cpp:72
Leosac::Module::Rpleth::RplethModule::set_reader_netmask
RplethPacket set_reader_netmask(const RplethPacket &p)
Update netmask of this Leosac unit.
Definition: RplethModule.cpp:489
Leosac::Module::Rpleth::RplethModule::client_connected
bool client_connected(const std::string &identity) const
Do we already know this client ?
Definition: RplethModule.cpp:332
Leosac::Module::Rpleth::RplethModule::cards_read_
std::list< std::string > cards_read_
Valid cards our Wiegand reader read: cards that were not pushed are not stored here.
Definition: RplethModule.hpp:193
Leosac::Module::Rpleth::RplethModule::handle_wiegand_event
void handle_wiegand_event()
We received a message (on the BUS, from the wiegand reader we watch), that means a card was inserted.
Definition: RplethModule.cpp:196
Leosac::Module::Rpleth::CircularBuffer
Implementation of a ring buffer.
Definition: circularbuffer.hpp:43
Leosac::CoreUtilsPtr
std::shared_ptr< CoreUtils > CoreUtilsPtr
Definition: LeosacFwd.hpp:35
Leosac::Module::Rpleth::RplethModule::failed_clients_
std::vector< std::string > failed_clients_
Client that are "failed".
Definition: RplethModule.hpp:232
Leosac::Module::Rpleth::RplethModule::client_failed
bool client_failed(const std::string &identity) const
Is the client in an invalid state ?
Definition: RplethModule.cpp:337
FWiegandReader.hpp
Leosac::Module::Rpleth::RplethModule::get_network_config
boost::property_tree::ptree get_network_config()
Retrieve the network configuration from the core.
Definition: RplethModule.cpp:565
Leosac::Module::Rpleth::RplethModule::cards_pushed_
std::list< std::string > cards_pushed_
List of cards pushed by SendCards Rpleth command.
Definition: RplethModule.hpp:187
Leosac::Module::Rpleth::RplethModule::set_reader_gw
RplethPacket set_reader_gw(const RplethPacket &p)
Update the reader gateway.
Definition: RplethModule.cpp:521