Leosac  0.7.0
OpenSourceAccessControl
SysFSGPIOPin.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 "SysFSGPIOPin.hpp"
21 #include "tools/unixfs.hpp"
22 #include <fcntl.h>
23 #include <tools/log.hpp>
24 #include <unistd.h>
25 
26 using namespace Leosac::Module::SysFsGpio;
28 
29 SysFsGpioPin::SysFsGpioPin(zmqpp::context &ctx, const std::string &name, int gpio_no,
30  Direction direction, InterruptMode interrupt_mode,
31  bool initial_value, SysFsGpioModule &module)
32  : gpio_no_(gpio_no)
33  , sock_(ctx, zmqpp::socket_type::rep)
34  , name_(name)
35  , direction_(direction)
36  , initial_value_(initial_value)
37  , module_(module)
38  , path_cfg_(module.general_config())
39  , next_update_time_(std::chrono::system_clock::time_point::max())
40 {
41  sock_.bind("inproc://" + name);
42 
43  set_direction(direction);
44  set_interrupt(interrupt_mode);
45  std::string full_path = path_cfg_.value_path(gpio_no);
46 
47  if (direction == Direction::Out)
48  {
49  if (initial_value_)
50  turn_on();
51  else
52  turn_off();
53  }
54 
55  file_fd_ = open(full_path.c_str(), O_RDONLY | O_NONBLOCK);
56  assert(file_fd_ != -1);
57 }
58 
60 {
62  {
63  if (initial_value_)
64  turn_on();
65  else
66  turn_off();
67  }
68 
69  if (file_fd_ != -1 && ::close(file_fd_) != 0)
70  {
71  ERROR("fail to close fd " << file_fd_);
72  }
73  try
74  {
75  UnixFs::writeSysFsValue(module_.general_config().unexport_path(), gpio_no_);
76  }
77  catch (FsException &e)
78  {
79  ERROR("Error while unexporting GPIO: " << e.what());
80  }
81 }
82 
84 {
85  std::string direction = dir == Direction::In ? "in" : "out";
86  UnixFs::writeSysFsValue(path_cfg_.direction_path(gpio_no_), direction);
87 }
88 
90 {
91  std::string value;
93  value = "none";
94  else if (mode == SysFsGpioPin::InterruptMode::Both)
95  value = "both";
96  else if (mode == SysFsGpioPin::InterruptMode::Falling)
97  value = "falling";
98  else if (mode == SysFsGpioPin::InterruptMode::Rising)
99  value = "rising";
100  else
101  assert(0);
102  UnixFs::writeSysFsValue(path_cfg_.edge_path(gpio_no_), value);
103 }
104 
106 {
107  zmqpp::message_t msg;
108  std::string frame1;
109  sock_.receive(msg);
110 
111  msg >> frame1;
112  bool ok = false;
113  if (frame1 == "ON")
114  ok = turn_on(&msg);
115  else if (frame1 == "OFF")
116  ok = turn_off();
117  else if (frame1 == "TOGGLE")
118  ok = toggle();
119  sock_.send(ok ? "OK" : "KO");
120 
121  // publish new state.
122  module_.publish_on_bus(zmqpp::message() << ("S_" + name_)
123  << (read_value() ? "ON" : "OFF"));
124 }
125 
126 bool SysFsGpioPin::turn_on(zmqpp::message *msg /* = nullptr */)
127 {
128  DEBUG("Remaining = " << msg->remaining());
129  if (msg && msg->remaining() == 1)
130  {
131  // ASSERT_LOG(msg->parts() == 2 && msg->remaining() == 1, "Invalid internal
132  // message.");
133  // optional parameter is present
134  int64_t duration;
135  *msg >> duration;
137  std::chrono::system_clock::now() + std::chrono::milliseconds(duration);
138  }
139  UnixFs::writeSysFsValue(path_cfg_.value_path(gpio_no_), 1);
140  return true;
141 }
142 
144 {
145  UnixFs::writeSysFsValue(path_cfg_.value_path(gpio_no_), 0);
146  return true;
147 }
148 
150 {
151  int v = UnixFs::readSysFsValue<int>(path_cfg_.value_path(gpio_no_));
152  UnixFs::writeSysFsValue(path_cfg_.value_path(gpio_no_), v == 1 ? 0 : 1);
153  return true;
154 }
155 
157 {
158  return UnixFs::readSysFsValue<bool>(path_cfg_.value_path(gpio_no_));
159 }
160 
162 {
163  std::array<char, 64> buffer;
164  ssize_t ret;
165 
166  // flush interrupt by reading.
167  // if we fail we cant recover, this means hardware failure.
168  ret = ::read(file_fd_, &buffer[0], buffer.size());
169  ASSERT_LOG(ret >= 0, "Read failed on GPIO pin.");
170  ret = ::lseek(file_fd_, 0, SEEK_SET);
171  ASSERT_LOG(ret >= 0, "Lseek failed on GPIO pin.");
172 
173  module_.publish_on_bus(zmqpp::message() << "S_INT:" + name_);
174 }
175 
176 void SysFsGpioPin::register_sockets(zmqpp::reactor *reactor)
177 {
178  reactor->add(sock_, std::bind(&SysFsGpioPin::handle_message, this));
179  if (direction_ == Direction::In)
180  reactor->add(file_fd_, std::bind(&SysFsGpioPin::handle_interrupt, this),
181  zmqpp::poller::poll_pri);
182 }
183 
184 std::chrono::system_clock::time_point SysFsGpioPin::next_update() const
185 {
186  return next_update_time_;
187 }
188 
190 {
191  DEBUG("Turning off SysFsGPIO pin.");
192  turn_off();
193  next_update_time_ = std::chrono::system_clock::time_point::max();
194 }
std::string edge_path(int pin_no) const
Compute the absolute path the "edge" file for pin_no.
void handle_interrupt()
Interrupt happened for this GPIO ping.
zmqpp::socket sock_
listen to command from other component.
void register_sockets(zmqpp::reactor *reactor)
Register own socket to the module&#39;s reactor.
SysFsGpioPin(zmqpp::context &ctx, const std::string &name, int gpio_no, Direction direction, InterruptMode interrupt_mode, bool initial_value, SysFsGpioModule &module)
STL namespace.
void set_direction(Direction dir)
Write direction to the direction file.
void set_interrupt(InterruptMode mode)
Write interrupt mode to the edge file.
bool read_value()
Read value from filesystem.
void handle_message()
The SysFsGpioModule will register this method so its called when a message is ready on the pin socket...
virtual const char * what() const noexcept final
std::string value_path(int pin_no) const
Compute the absolute path the "value" file for pin_no.
bool toggle()
Read to sysfs and then write the opposite value.
void publish_on_bus(zmqpp::message &msg)
Write the message eon the bus.
Handle GPIO management over sysfs.
unix filesystem helper functions
const Direction direction_
Direction of the PIN.
std::chrono::system_clock::time_point next_update_time_
Time point of next wished update.
bool turn_off()
Write to sysfs to turn the gpio on.
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
Namespace for the module that implements GPIO support using the Linux Kernel sysfs interface...
Definition: log.hpp:34
Definition: log.hpp:38
std::chrono::system_clock::time_point next_update() const
This method shall returns the time point at which we want to be updated.
int file_fd_
File descriptor of the GPIO in sysfs.
const std::string & unexport_path() const
Returns the absolute path to the "unexport" sysfs file.
std::string direction_path(int pin_no) const
Compute the absolute path the "direction" file for pin_no.
SysFsGpioModule & module_
Reference to the module.
const SysFsGpioConfig & general_config() const
Retrieve a reference to the config object.
bool turn_on(zmqpp::message *msg=nullptr)
Write to sysfs to turn the gpio on.
const bool initial_value_
Initial value of the PIN.