Leosac  0.8.0
Open Source Access Control
SysFsGpioConfig.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 "SysFsGpioConfig.hpp"
21 #include "tools/Colorize.hpp"
23 #include "tools/log.hpp"
24 #include <boost/algorithm/string.hpp>
25 
26 using namespace Leosac::Module::SysFsGpio;
27 
28 SysFsGpioConfig::SysFsGpioConfig(const boost::property_tree::ptree &cfg)
29 {
30  Tools::PropertyTreeExtractor extractor(cfg, "SysFsGpio");
31 
32  cfg_export_path_ = extractor.get<std::string>("export_path");
33  cfg_unexport_path_ = extractor.get<std::string>("unexport_path");
34  cfg_value_path_ = extractor.get<std::string>("value_path");
35  cfg_edge_path_ = extractor.get<std::string>("edge_path");
36  cfg_direction_path_ = extractor.get<std::string>("direction_path");
37 
38  auto aliases_cfg = cfg.get_child("aliases");
39  for (auto alias : aliases_cfg)
40  {
41  if (alias.first == "default")
42  {
43  default_aliases_ = alias.second.data();
44  continue;
45  }
46 
47  int pin_no = std::stoi(alias.first);
48  pin_aliases_[pin_no] = alias.second.data();
49  }
50 
51  using namespace Colorize;
52  INFO("SysFsGpio Path Configuration:"
53  << std::endl
54  << '\t' << "Export path: " << green(cfg_export_path_) << std::endl
55  << '\t' << "Unexport path: " << green(cfg_unexport_path_) << std::endl
56  << '\t' << "Value path: " << green(cfg_value_path_) << std::endl
57  << '\t' << "Edge path: " << green(cfg_edge_path_) << std::endl
58  << '\t' << "Direction path: " << green(cfg_direction_path_) << std::endl
59  << '\t' << "Default aliases rule: " << green(default_aliases_)
60  << std::endl);
61 }
62 
63 const std::string &SysFsGpioConfig::export_path() const
64 {
65  return cfg_export_path_;
66 }
67 
68 const std::string &SysFsGpioConfig::unexport_path() const
69 {
70  return cfg_unexport_path_;
71 }
72 
73 std::string SysFsGpioConfig::value_path(int pin_no) const
74 {
75  if (pin_aliases_.count(pin_no))
76  return boost::replace_all_copy(cfg_value_path_, "__PLACEHOLDER__",
77  pin_aliases_.find(pin_no)->second);
78  return boost::replace_all_copy(
79  cfg_value_path_, "__PLACEHOLDER__",
80  boost::replace_all_copy(default_aliases_, "__NO__", std::to_string(pin_no)));
81 }
82 
83 std::string SysFsGpioConfig::edge_path(int pin_no) const
84 {
85  if (pin_aliases_.count(pin_no))
86  return boost::replace_all_copy(cfg_edge_path_, "__PLACEHOLDER__",
87  pin_aliases_.find(pin_no)->second);
88  return boost::replace_all_copy(
89  cfg_edge_path_, "__PLACEHOLDER__",
90  boost::replace_all_copy(default_aliases_, "__NO__", std::to_string(pin_no)));
91 }
92 
93 std::string SysFsGpioConfig::direction_path(int pin_no) const
94 {
95  if (pin_aliases_.count(pin_no))
96  return boost::replace_all_copy(cfg_direction_path_, "__PLACEHOLDER__",
97  pin_aliases_.find(pin_no)->second);
98  return boost::replace_all_copy(
99  cfg_direction_path_, "__PLACEHOLDER__",
100  boost::replace_all_copy(default_aliases_, "__NO__", std::to_string(pin_no)));
101 }
Leosac::Module::SysFsGpio::SysFsGpioConfig::direction_path
std::string direction_path(int pin_no) const
Compute the absolute path the "direction" file for pin_no.
Definition: SysFsGpioConfig.cpp:93
Leosac::Tools::PropertyTreeExtractor::get
T get(const std::string &node_name)
Extract a mandatory property from the tree.
Definition: PropertyTreeExtractor.cpp:36
Leosac::Module::SysFsGpio::SysFsGpioConfig::default_aliases_
std::string default_aliases_
Default aliases rule, as defined in configuration.
Definition: SysFsGpioConfig.hpp:83
Leosac::Module::SysFsGpio::SysFsGpioConfig::cfg_edge_path_
std::string cfg_edge_path_
Absolute path to the "edge" file.
Definition: SysFsGpioConfig.hpp:106
INFO
@ INFO
Definition: log.hpp:34
SysFsGpioConfig.hpp
Colorize.hpp
Leosac::Module::SysFsGpio::SysFsGpioConfig::pin_aliases_
std::map< int, std::string > pin_aliases_
Maps pin number to file identifier.
Definition: SysFsGpioConfig.hpp:78
Leosac::Module::SysFsGpio::SysFsGpioConfig::cfg_export_path_
std::string cfg_export_path_
Absolute path of the "export" sysfs file.
Definition: SysFsGpioConfig.hpp:88
Leosac::Module::SysFsGpio::SysFsGpioConfig::value_path
std::string value_path(int pin_no) const
Compute the absolute path the "value" file for pin_no.
Definition: SysFsGpioConfig.cpp:73
PropertyTreeExtractor.hpp
Leosac::Module::SysFsGpio::SysFsGpioConfig::SysFsGpioConfig
SysFsGpioConfig(const boost::property_tree::ptree &cfg)
Construct the general config object from the configuration tree.
Definition: SysFsGpioConfig.cpp:28
Leosac::Module::SysFsGpio
Namespace for the module that implements GPIO support using the Linux Kernel sysfs interface.
Definition: SysFsGpioConfig.hpp:32
Leosac::Module::SysFsGpio::SysFsGpioConfig::edge_path
std::string edge_path(int pin_no) const
Compute the absolute path the "edge" file for pin_no.
Definition: SysFsGpioConfig.cpp:83
Leosac::Colorize::green
std::string green(const T &in)
Definition: Colorize.hpp:82
Leosac::Module::SysFsGpio::SysFsGpioConfig::cfg_direction_path_
std::string cfg_direction_path_
Absolute path to the "direction" file.
Definition: SysFsGpioConfig.hpp:111
Leosac::Tools::PropertyTreeExtractor
This class is a simpler helper to extract value from a property tree.
Definition: PropertyTreeExtractor.hpp:38
log.hpp
Leosac::Module::SysFsGpio::SysFsGpioConfig::cfg_value_path_
std::string cfg_value_path_
Absolute path of the "value file" sysfs file: use __PLACEHOLDER__ as a placeholder for the PIN identi...
Definition: SysFsGpioConfig.hpp:101
Leosac::Module::SysFsGpio::SysFsGpioConfig::cfg_unexport_path_
std::string cfg_unexport_path_
Absolute path of the "unexport" sysfs file.
Definition: SysFsGpioConfig.hpp:93
Leosac::Module::SysFsGpio::SysFsGpioConfig::export_path
const std::string & export_path() const
Returns the absolute path to the "export" sysfs file.
Definition: SysFsGpioConfig.cpp:63
Leosac::Module::SysFsGpio::SysFsGpioConfig::unexport_path
const std::string & unexport_path() const
Returns the absolute path to the "unexport" sysfs file.
Definition: SysFsGpioConfig.cpp:68