Leosac  0.8.0
Open Source Access Control
FGPIO.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 "FGPIO.hpp"
21 #include "tools/log.hpp"
22 #include <zmqpp/message.hpp>
23 
24 using namespace Leosac::Hardware;
25 
26 FGPIO::FGPIO(zmqpp::context &ctx, const std::string &gpio_name)
27  : gpio_name_(gpio_name)
28  , backend_(ctx, zmqpp::socket_type::req)
29 {
30  backend_.connect("inproc://" + gpio_name);
31  poller_.add(backend_);
32 }
33 
34 bool FGPIO::turnOn(std::chrono::milliseconds duration)
35 {
36  std::string rep;
37  zmqpp::message msg;
38 
39  msg << "ON" << duration.count();
40 
41  backend_.send(msg);
42  poller_.poll(5000);
43  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked");
44  backend_.receive(rep);
45 
46  if (rep == "OK")
47  return true;
48  return false;
49 }
50 
52 {
53  std::string rep;
54 
55  backend_.send("ON");
56  poller_.poll(5000);
57  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked");
58  backend_.receive(rep);
59 
60  if (rep == "OK")
61  return true;
62  return false;
63 }
64 
66 {
67  std::string rep;
68 
69  backend_.send("OFF");
70  poller_.poll(5000);
71  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked");
72  backend_.receive(rep);
73 
74  if (rep == "OK")
75  return true;
76  return false;
77 }
78 
80 {
81  std::string rep;
82 
83  backend_.send("TOGGLE");
84  poller_.poll(5000);
85  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked");
86  backend_.receive(rep);
87 
88  if (rep == "OK")
89  return true;
90  return false;
91 }
92 
94 {
95  std::string rep;
96 
97  backend_.send("STATE");
98  poller_.poll(5000);
99  ASSERT_LOG(poller_.has_input(backend_), "Operation was blocked");
100  backend_.receive(rep);
101 
102  if (rep == "ON")
103  return true;
104  assert(rep == "OFF");
105  return false;
106 }
107 
109 {
110  return !isOn();
111 }
112 
113 std::string const &FGPIO::name() const
114 {
115  return gpio_name_;
116 }
FGPIO.hpp
Leosac::Hardware::FGPIO::gpio_name_
std::string gpio_name_
Definition: FGPIO.hpp:98
zmqpp
Definition: CoreUtils.hpp:27
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Hardware::FGPIO::toggle
bool toggle()
Toggle the GPIO value by sending a message to the backend GPIO impl.
Definition: FGPIO.cpp:79
Leosac::Hardware::FGPIO::poller_
zmqpp::poller poller_
A poller to not wait for infinity in case something went wrong.
Definition: FGPIO.hpp:108
Leosac::Hardware::FGPIO::name
const std::string & name() const
Name of the GPIO pin as defined in the configuration file.
Definition: FGPIO.cpp:113
Leosac::Hardware::FGPIO::isOn
bool isOn()
Query the value of the GPIO and returns true if the LED is ON.
Definition: FGPIO.cpp:93
Leosac::Hardware::FGPIO::isOff
bool isOff()
Similar to isOn().
Definition: FGPIO.cpp:108
log.hpp
Leosac::Hardware::FGPIO::FGPIO
FGPIO(zmqpp::context &ctx, const std::string &gpio_name)
Definition: FGPIO.cpp:26
Leosac::Hardware::FGPIO::turnOff
bool turnOff()
Turn the GPIO OFF by sending a message to the backend GPIO impl.
Definition: FGPIO.cpp:65
Leosac::Hardware
Provides facade classes to hardware device implementation.
Definition: Buzzer.cpp:25
Leosac::Hardware::FGPIO::backend_
zmqpp::socket backend_
A socket to talk to the backend GPIO.
Definition: FGPIO.hpp:103
Leosac::Hardware::FGPIO::turnOn
bool turnOn()
Turn the GPIO ON by sending a message to the backend GPIO impl.
Definition: FGPIO.cpp:51