Skip to content

Commit

Permalink
Change SysFsGpio configuration.
Browse files Browse the repository at this point in the history
Instead of using `__REPLACE_ME__` we now use `__PLACEHOLDER__`
as a placeholder for the pin idenfier. The former had
proven confusing for user.
  • Loading branch information
xaqq committed Aug 5, 2015
1 parent 861bf44 commit f2738d0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 72 deletions.
12 changes: 11 additions & 1 deletion cfg/dev-test/rpi-sysfs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<!-- Device we watch as input source -->
<auth_source>MY_WIEGAND_1</auth_source>
<!-- File that contains (1 per line) the textual data that is valid.
<!-- File that contains (1 per line) the textual data that is valid.
With a wiegand reader, it could be something like this "ab:0f:fa:12" -->
<valid_input_file>valid-cards.txt</valid_input_file>
</instance>
Expand All @@ -55,6 +55,16 @@ With a wiegand reader, it could be something like this "ab:0f:fa:12" -->
<level>2</level>

<module_config>

<aliases>
<default>gpio__NO__</default>
</aliases>
<export_path>/sys/class/gpio/export</export_path>
<unexport_path>/sys/class/gpio/unexport</unexport_path>
<edge_path>/sys/class/gpio/__PLACEHOLDER__/edge</edge_path>
<value_path>/sys/class/gpio/__PLACEHOLDER__/value</value_path>
<direction_path>/sys/class/gpio/__PLACEHOLDER__/value</direction_path>

<gpios>
<!-- Prepare GPIO for our Wiegand reader -->
<gpio>
Expand Down
10 changes: 5 additions & 5 deletions cfg/sysfs-rpleth/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

<module>
<name>MONITOR</name>
<file>libpersistent-monitor.so</file>
<file>libmonitor.so</file>
<level>1</level>

<module_config>
Expand All @@ -38,7 +38,7 @@
</module>

<module>
<name>SYSFS-GPIO</name>
<name>SYSFS_GPIO</name>
<file>libsysfsgpio.so</file>
<level>2</level>

Expand All @@ -51,9 +51,9 @@
<export_path>/sys/class/gpio/export</export_path>
<unexport_path>/sys/class/gpio/unexport</unexport_path>

<edge_path>/sys/class/gpio/__REPLACE_ME__/edge</edge_path>
<value_path>/sys/class/gpio/__REPLACE_ME__/value</value_path>
<direction_path>/sys/class/gpio/__REPLACE_ME__/direction</direction_path>
<edge_path>/sys/class/gpio/__PLACEHOLDER__/edge</edge_path>
<value_path>/sys/class/gpio/__PLACEHOLDER__/value</value_path>
<direction_path>/sys/class/gpio/__PLACEHOLDER__/direction</direction_path>

<gpios>
<!-- Prepare GPIO for our Wiegand reader -->
Expand Down
85 changes: 49 additions & 36 deletions src/modules/sysfsgpio/SysFsGpioConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,64 +26,77 @@ using namespace Leosac::Module::SysFsGpio;

SysFsGpioConfig::SysFsGpioConfig(const boost::property_tree::ptree &cfg)
{
Tools::PropertyTreeExtractor extractor(cfg, "SysFsGpio");

cfg_export_path_ = extractor.get<std::string>("export_path");
cfg_unexport_path_ = extractor.get<std::string>("unexport_path");
cfg_value_path_ = extractor.get<std::string>("value_path");
cfg_edge_path_ = extractor.get<std::string>("edge_path");
cfg_direction_path_ = extractor.get<std::string>("direction_path");

auto aliases_cfg = cfg.get_child("aliases");
for (auto alias : aliases_cfg)
Tools::PropertyTreeExtractor extractor(cfg, "SysFsGpio");

cfg_export_path_ = extractor.get<std::string>("export_path");
cfg_unexport_path_ = extractor.get<std::string>("unexport_path");
cfg_value_path_ = extractor.get<std::string>("value_path");
cfg_edge_path_ = extractor.get<std::string>("edge_path");
cfg_direction_path_ = extractor.get<std::string>("direction_path");

auto aliases_cfg = cfg.get_child("aliases");
for (auto alias : aliases_cfg)
{
if (alias.first == "default")
{
if (alias.first == "default")
{
default_aliases_ = alias.second.data();
continue;
}

int pin_no = std::stoi(alias.first);
pin_aliases_[pin_no] = alias.second.data();
default_aliases_ = alias.second.data();
continue;
}

INFO("SysFsGpio Path Configuration:" << std::endl
<< '\t' << "Export path: " << cfg_export_path_ << std::endl
<< '\t' << "Unexport path: " << cfg_unexport_path_ << std::endl
<< '\t' << "Value path: " << cfg_value_path_ << std::endl
<< '\t' << "Edge path: " << cfg_edge_path_ << std::endl
<< '\t' << "Direction path: " << cfg_direction_path_ << std::endl
<< '\t' << "Default aliases rule: " << default_aliases_ << std::endl);
int pin_no = std::stoi(alias.first);
pin_aliases_[pin_no] = alias.second.data();
}

INFO("SysFsGpio Path Configuration:"
<< std::endl
<< '\t' << "Export path: " << cfg_export_path_ << std::endl
<< '\t' << "Unexport path: " << cfg_unexport_path_ << std::endl
<< '\t' << "Value path: " << cfg_value_path_ << std::endl
<< '\t' << "Edge path: " << cfg_edge_path_ << std::endl
<< '\t' << "Direction path: " << cfg_direction_path_ << std::endl
<< '\t' << "Default aliases rule: " << default_aliases_ << std::endl);
}


const std::string &SysFsGpioConfig::export_path() const
{
return cfg_export_path_;
return cfg_export_path_;
}

const std::string &SysFsGpioConfig::unexport_path() const
{
return cfg_unexport_path_;
return cfg_unexport_path_;
}

std::string SysFsGpioConfig::value_path(int pin_no) const
{
if (pin_aliases_.count(pin_no))
return boost::replace_all_copy(cfg_value_path_, "__REPLACE_ME__", pin_aliases_.find(pin_no)->second);
return boost::replace_all_copy(cfg_value_path_, "__REPLACE_ME__", boost::replace_all_copy(default_aliases_, "__NO__", std::to_string(pin_no)));
if (pin_aliases_.count(pin_no))
return boost::replace_all_copy(cfg_value_path_, "__PLACEHOLDER__",
pin_aliases_.find(pin_no)->second);
return boost::replace_all_copy(
cfg_value_path_, "__PLACEHOLDER__",
boost::replace_all_copy(default_aliases_, "__NO__",
std::to_string(pin_no)));
}

std::string SysFsGpioConfig::edge_path(int pin_no) const
{
if (pin_aliases_.count(pin_no))
return boost::replace_all_copy(cfg_edge_path_, "__REPLACE_ME__", pin_aliases_.find(pin_no)->second);
return boost::replace_all_copy(cfg_edge_path_, "__REPLACE_ME__", boost::replace_all_copy(default_aliases_, "__NO__", std::to_string(pin_no)));
if (pin_aliases_.count(pin_no))
return boost::replace_all_copy(cfg_edge_path_, "__PLACEHOLDER__",
pin_aliases_.find(pin_no)->second);
return boost::replace_all_copy(
cfg_edge_path_, "__PLACEHOLDER__",
boost::replace_all_copy(default_aliases_, "__NO__",
std::to_string(pin_no)));
}

std::string SysFsGpioConfig::direction_path(int pin_no) const
{
if (pin_aliases_.count(pin_no))
return boost::replace_all_copy(cfg_direction_path_, "__REPLACE_ME__", pin_aliases_.find(pin_no)->second);
return boost::replace_all_copy(cfg_direction_path_, "__REPLACE_ME__", boost::replace_all_copy(default_aliases_, "__NO__", std::to_string(pin_no)));
if (pin_aliases_.count(pin_no))
return boost::replace_all_copy(cfg_direction_path_, "__PLACEHOLDER__",
pin_aliases_.find(pin_no)->second);
return boost::replace_all_copy(
cfg_direction_path_, "__PLACEHOLDER__",
boost::replace_all_copy(default_aliases_, "__NO__",
std::to_string(pin_no)));
}
4 changes: 2 additions & 2 deletions src/modules/sysfsgpio/SysFsGpioConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ namespace Leosac
std::string cfg_unexport_path_;

/**
* Absolute path of the "value file" sysfs file: use `__REPLACE_ME__` as a placeholder
* for the PIN identifier (likely its number).
* Absolute path of the "value file" sysfs file: use `__PLACEHOLDER__` as a placeholder
* for the PIN identifier (likely its number, potentially -- prefixed by "gpio" on raspberry pi).
*/
std::string cfg_value_path_;

Expand Down
44 changes: 25 additions & 19 deletions src/modules/sysfsgpio/sysfsgpio.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
SysFsGpio Module Documentation {#mod_sysfsgpio_main}
====================================================

@brief Driving GPIO pin through Linux's sysfs.

[TOC]

Introduction {#mod_sysfsgpio_intro}
Expand All @@ -21,33 +23,37 @@ Below are the configuration options available.

Options | Options | Options | Description | Mandatory
--------|---------|----------------|-----------------------------------------------------------------------------------------|-----------
aliases | | | Define GPIO aliases. This is useful to support multiple platform | YES
aliases | | | Define GPIO aliases. This is useful to support multiple platform | **YES**
---> | default | | Default name resolution for pin. `__NO__` will be replace by the `no` field | NO
---> | PIN_ID | | Option name shall be the pin number, **not** textual `PIN_ID`. Value is the identifier for the pin. | NO
export_path | | | Absolute path to "export" sysfs file | YES
unexport_path | | | Absolute path to "unexport" sysfs file | YES
value_path | | | Absolute path to "value" file. `__REPLACE_ME__` shall act as a placeholder for pin id | YES
edge_path | | | Absolute path to "edge" file. `__REPLACE_ME__` shall act as a placeholder for pin id | YES
direction_path | | | Absolute path to "direction" file. `__REPLACE_ME__` shall act as a placeholder | YES
gpios | | | List of GPIOs pins we configure | YES
---> | gpio | | Configuration informations for one GPIO pin. | YES
---> | ---> | name | Name of the GPIO pin | YES
---> | ---> | no | Number of the GPIO pin. | YES
---> | ---> | direction | Direction of the pin. This in either `in` or `out` | YES
export_path | | | Absolute path to "export" sysfs file | **YES**
unexport_path | | | Absolute path to "unexport" sysfs file | **YES**
value_path | | | Absolute path to "value" file. `__PLACEHOLDER__` shall act as a placeholder for pin identifier. | **YES**
edge_path | | | Absolute path to "edge" file. `__PLACEHOLDER__` shall act as a placeholder for pin identifier. | **YES**
direction_path | | | Absolute path to "direction" file. `__PLACEHOLDER__` shall act as a placeholder for pin identifier | **YES**
gpios | | | List of GPIOs pins we configure | **YES**
---> | gpio | | Configuration informations for one GPIO pin. | **YES**
---> | ---> | name | Name of the GPIO pin | **YES**
---> | ---> | no | Number of the GPIO pin. | **YES**
---> | ---> | direction | Direction of the pin. This in either `in` or `out` | **YES**
---> | ---> | interrupt_mode | What interrupt do we care about? See below for details | NO
---> | ---> | value | Default value of the PIN. Either `1` or `0` | NO

Path information
----------------
Path configuration allows the user the use the same `sysfsgpio` module on multiple platform even when
the path-to-gpio / naming-convention of GPIO pins varies.
The `__REPLACE__ME__` placeholder will be replaced by the *identifier* of the pin. This identifier is the GPIO
pin number. This replacement involve the `aliases` configuration options.
The `__PLACEHOLDER__` placeholder will be replaced by the *identifier* of the pin.
The *identifier* is computed through aliases resolution.

A simple example:
+ We have pin `14` and `value_path` = `/sys/class/gpio/__REPLACE_ME__/value`.
+ `aliases/default` = `gpio__NO__`.
+ The module will resolve the value path of the PIN to `/sys/class/gpio/gpio14/value`
+ Pin id / ping number is `14`.
+ `value_path` = `/sys/class/gpio/__PLACEHOLDER__/value`.
+ `aliases.default` = `gpio__NO__`.
+ The pin *identifier* is resolved as `gpio14` (since `__NO__` from the default alias is replace
by `14`).
+ `__PLACEHOLDER__` is replaced by the pin identifier: The module will resolve
the value path of the PIN to `/sys/class/gpio/gpio14/value`

@see SysFsGpioConfigTest for more example.

Expand Down Expand Up @@ -82,9 +88,9 @@ This is a example of SysFsGpio possible configuration for SysFsGpio module into
</aliases>
<export_path>/sys/class/gpio/export</export_path>
<unexport_path>/sys/class/gpio/unexport</unexport_path>
<edge_path>/sys/class/gpio/__REPLACE_ME__/edge</edge_path>
<value_path>/sys/class/gpio/__REPLACE_ME__/value</value_path>
<direction_path>/sys/class/gpio/__REPLACE_ME__/value</direction_path>
<edge_path>/sys/class/gpio/__PLACEHOLDER__/edge</edge_path>
<value_path>/sys/class/gpio/__PLACEHOLDER__/value</value_path>
<direction_path>/sys/class/gpio/__PLACEHOLDER__/value</direction_path>
<gpios>
<gpio>
<name>wiegand_data_high</name>
Expand Down
18 changes: 9 additions & 9 deletions test/SysFsGpioConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ namespace Leosac

module_cfg.add("export_path", "/path/to/export");
module_cfg.add("unexport_path", "/path/to/unexport");
module_cfg.add("value_path", "/path/to/gpios/__REPLACE_ME__/value");
module_cfg.add("edge_path", "/path/to/gpios/__REPLACE_ME__/edge");
module_cfg.add("direction_path", "/path/to/gpios/__REPLACE_ME__/direction");
module_cfg.add("value_path", "/path/to/gpios/__PLACEHOLDER__/value");
module_cfg.add("edge_path", "/path/to/gpios/__PLACEHOLDER__/edge");
module_cfg.add("direction_path", "/path/to/gpios/__PLACEHOLDER__/direction");

module_cfg.add_child("aliases", aliases_cfg);
cfg_case_1_ = module_cfg;
Expand All @@ -71,9 +71,9 @@ namespace Leosac

module_cfg.add("export_path", "/path/to/export");
module_cfg.add("unexport_path", "/path/to/unexport");
module_cfg.add("value_path", "/random/path/to/__REPLACE_ME__/value");
module_cfg.add("edge_path", "/random/path/to/__REPLACE_ME__/edge");
module_cfg.add("direction_path", "/random/path/to/__REPLACE_ME__/direction");
module_cfg.add("value_path", "/random/path/to/__PLACEHOLDER__/value");
module_cfg.add("edge_path", "/random/path/to/__PLACEHOLDER__/edge");
module_cfg.add("direction_path", "/random/path/to/__PLACEHOLDER__/direction");

// we define default aliases rules.
aliases_cfg.add("default", "gpio__NO__");
Expand All @@ -87,9 +87,9 @@ namespace Leosac

module_cfg.add("export_path", "/path/to/export");
module_cfg.add("unexport_path", "/path/to/unexport");
module_cfg.add("value_path", "/path/to/gpios/__REPLACE_ME__/value");
module_cfg.add("edge_path", "/path/to/gpios/__REPLACE_ME__/edge");
module_cfg.add("direction_path", "/path/to/gpios/__REPLACE_ME__/direction");
module_cfg.add("value_path", "/path/to/gpios/__PLACEHOLDER__/value");
module_cfg.add("edge_path", "/path/to/gpios/__PLACEHOLDER__/edge");
module_cfg.add("direction_path", "/path/to/gpios/__PLACEHOLDER__/direction");

// we define default aliases rules.
aliases_cfg.add("default", "gpio__NO__");
Expand Down

0 comments on commit f2738d0

Please sign in to comment.