The C++ bindings are missing the functionality provided by the core C
library's gpiod_line_update(). Extend the line class with the update()
method that allows to re-read the line information from the kernel.
core: rename up_to_date to needs_update in struct gpiod_line
This variable is called up_to_date which can lead readers to the wrong
conclusion that libgpiod can somehow know when external actors (such as
a different process or a kernel driver) change the state of a GPIO line.
This is not the case and libgpiod only ever knows if a line needs info
update when a previous internal call to gpiod_line_update() fails.
The kernel does not provide any notification mechanism currently.
For that reason: rename the variable to needs_update.
While at it: clarify the documentation for gpiod_line_needs_update().
bindings: python: tests: fix checking the kernel version
Kernel release candidate version strings seem to not be working
correctly with python's version.parse() function. Split the string
retrieved with os.uname().release using '-' as delimiter and pass
only the first part - 'major.minor.release' - to version.parse().
core: fix the major:minor number comparison between the device and sysfs
Current code will incorrectly conclude that a device "1:1" matches
a sysfs device "1:10" as the length it reads from sysfsp is based on
the devstr length, which in this example is 3.
Always read the whole sysfs attribute and compare the string generated
from actual major:minor numbers against it (minus the trailing newline).
treewide: tests: uncomment previously failing test assertions
Mainline commit 2c60e6b5c924 ("gpiolib: never report open-drain/source
lines as 'input' to user-space") in the linux kernel has now been
backported to v5.2 stable branch and released in linux v5.2.11. We can
now enable back the assertions that were previously failed in test
suites (core, C++ and python). The required kernel versions must be
updated as well.
bindings: cxx: tests: simplify the event fd polling test case
The mapping of file descriptors to lines was copied over from the
pre-catch2 set of C++ examples. It's not needed here as we know which
line the event will be generated on.
bindings: python: examples: wait for ENTER press in gpioset.py
After setting the line values in gpioset.py: don't exit until the user
presses ENTER. This makes the example work the same as the 'wait' mode
in the original gpioset utility.
bindings: python: use unittest to implement a proper test-suite
Currently the only tests we have for python bindings are in a simple
script that makes a lot of assumptions about the environment and
doesn't even work anymore with recent kernels.
Remove it and replace it with a proper test-suite implemented using
libgpiod and the standard python unittest package.
bindings: python: fix the default_val argument name in the doc string
The argument in Line.request() is called default_val, not default_vals.
For backward compatibility with older versions default_vals would still
work if it was passed a tuple or list, but let's not confuse users:
default_val is the preferred way for single lines.
Alexander Stein [Wed, 7 Aug 2019 19:51:32 +0000 (21:51 +0200)]
bindings: cxx: add a workaround for --success run
If run with --success, all expressions are evaluated and printed out.
But REQUIRE_FALSE(chip) tries to iterate over the chip resulting in this
backtrace:
[...]
Work around this by forcing catch2 to call gpiod::chip::operator bool().
Signed-off-by: Alexander Stein <alexander.stein@mailbox.org> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Alexander Stein [Wed, 7 Aug 2019 19:51:31 +0000 (21:51 +0200)]
bindings: cxx: vix compile errors
This fixes the following compile errors:
tests-event.cpp:152:3: error: cannot declare reference to
'class std::system_error&', which is not a typedef or a template type
argument
152 | REQUIRE_THROWS_AS(line.event_get_fd(), ::std::system_error&);
This occurs on catch2 but not on catch.
Signed-off-by: Alexander Stein <alexander.stein@mailbox.org> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Alexander Stein [Wed, 7 Aug 2019 19:51:30 +0000 (21:51 +0200)]
bindings: cxx: split out catch's main()
Compiling the source using CATCH_CONFIG_MAIN to provide main() takes
several seconds, so split it out from any library testing code, so it
really needs to be built once only.
Signed-off-by: Alexander Stein <alexander.stein@mailbox.org>
[Bartosz: added the copyright notice for consistency with other files] Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Alexander Stein [Wed, 7 Aug 2019 19:51:28 +0000 (21:51 +0200)]
bindings: cxx: use 'upstream' include path
According to https://github.com/catchorg/Catch2/issues/1202 the
regular include is 'catch2/catch.hpp'. Also CMake and pkg-config
provide include paths for this include name.
Signed-off-by: Alexander Stein <alexander.stein@mailbox.org> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
tools: tests: restore the bats shebang in the bats source file
In commit f52e12eafd17 ("tools: tests: run the bats script indirectly
from a shell script") we removed the bats shebang and dropped the
execute permission from the .bats test-suite file.
While the preferred method of running the gpio-tools tests is using
the helper bash script, the .bats file should still be executable on
its own. Restore the execute permissions and the shebang.
bindings: cxx: use catch2 to implement a proper test-suite
Currently the only tests we have for C++ bindings are in a simple
program that makes a lot of assumptions about the environment and
doesn't even work anymore with recent kernels.
Remove it and replace it with a proper test-suite implemented using
libgpiomockup and Catch2.
bindings: cxx: add a missing throw to line::event_get_fd()
If the call to gpiod_line_event_get_fd() fails, we're not actually
throwing the exception - we're only creating it on stack. Add the
missing throw keyword.
Fixes: 8078a4a2ad90 ("bindings: implement C++ bindings") Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Kernel commit 223ecaf140b1 ("gpiolib: fix incorrect IRQ requesting of
an active-low lineevent") fixed an issue with line events not being
inverted with the GPIOHANDLE_REQUEST_ACTIVE_LOW flag. This change has
now been released in linux v5.2.7 and the relevant test case must be
adjusted.
Convert the test case to expect a falling edge event. Since the first
pull change will be 0->1, it will be interpreted as such when the
active-low flag is used.
The minimum kernel version to run the core test-suite is now set to
v5.2.7.
tests: mockup: don't fail at init-time if gpio-mockup is already loaded
The comment in gpio_mockup_new() says we can deal with gpio-mockup
already being loaded, but it's wrong - we'd actually fail in the call
to kmod_module_probe_insert_module().
Pass the KMOD_PROBE_IGNORE_LOADED flag to the insert function to fix
that.
tools: tests: fix the gpiomon test case with --active-low switch
Kernel commit 223ecaf140b1 ("gpiolib: fix incorrect IRQ requesting of
an active-low lineevent") fixed an issue with line events not being
inverted with the GPIOHANDLE_REQUEST_ACTIVE_LOW flag. This change has
now been released in linux v5.2.7 and the relevant test case for
gpiomon needs to be adjusted.
Since the line going down will now be interpreted as a rising edge
event, set the pull to 1 before running gpiomon and then set it to 0
after starting it to trigger a rising edge event.
The minimum kernel version to run tests must be set to v5.2.7.
tools: tests: pass command-line arguments to the underlying bats script
Command-line arguments need to be forwarded to the bats test-suite if
we want to control bats' execution.
Fixes: f52e12eafd17 ("tools: tests: run the bats script indirectly from a shell script") Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
tools: tests: run the bats script indirectly from a shell script
Bats makes it very difficult to run some initial code once, perform
checks for requirements and optionally exit if they are not satisfied.
Instead of working around it in the .bats script, just create
a separate shell script that does all the initial checks and then
execs the actual bats test-suite.
tests: add a test case for using misc line flags together
This adds a test case that verifies that the ACTIVE_LOW flag works
correctly when used together with OPEN_SOURCE and OPEN_DRAIN flags.
Note: a bug has been discovered in the kernel that makes the LINEINFO
ioctl() incorrectly report the direction of lines requested with
open-drain or open-source flags as 'input' if the underlying hardware
doesn't support these config options. A patch fixing it has been sent
and once it's released, the FIXME's added by this patch will be
removed.
While at it: extend the previous misc flags test with additional checks
of the reported direction, but disable the one that fails due to the
bug mentioned above.
bindings: cxx: examples: use seconds for timeout in line.event_wait()
While the event_wait() method takes ::std::chrono::nanoseconds as the
timeout argument, we don't actually need to use the same type - we can
use less fine-grained duration types. Switch to using seconds in the
gpiomoncxx example to showcase this implicit conversion.
tests: mockup: make sure the gpio-mockup debugfs directory is writable
The gpio-mockup library needs write permissions to make full use of
the debugfs interface. Add the W_OK flag to the call to access() when
preparing the context structure.
tests: mockup: add a comment explaining the kernel version definition
The library only works on newer kernels. The specific features we're
using have only available since linux v5.1.0. Add a comment explaining
this limitation to the library code.
tests: mockup: validate the chip index where applicable
In functions that take the chip index as argument: let's verify that
its value is valid i.e. it refers to an existing chip to avoid potential
segfaults.
While we're at it: delegate the validation of arguments in mockup getter
helpers to a separate routine.
tests: use GLib for library test cases and bats for gpio-tools
This replaces the custom hand-crafted testing suite with well-known
open-source tools while keeping the same coverage. The core library
is now tested using the GLib unit testing framework. The gpio-tools
are now tested in bash using the bats testing system instead of being
invoked from a C program.
This simplifies and shrinks the testing code and makes it less prone
to bugs.
Ramon Fried [Wed, 31 Jul 2019 17:38:14 +0000 (20:38 +0300)]
gpioinfo: mark kernel claimed lines as used
In case where the GPIOLINE_FLAG_KERNEL flag was set and no consumer
string is provided by the kernel, the used column was still showing
the pin as "unused" Fix that by writing "kernel".
Signed-off-by: Ramon Fried <rfried.dev@gmail.com>
[Bartosz:
- fixed indentation and alignment
- tweaked the commit message formatting] Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
tests: setup libgpiomockup before checking the kernel version
The gpio-mockup helper library checks the kernel version itself when
creating the context object. We then check the version again (as the
minimum versions for libgpiomockup and the test-suite may differ) in
the test-suite's main function. Make sure the library runs the check
first as we don't want to tell the user the version is OK and then
fail because it isn't...
There's no need to reimplement checking the kernel version. An
appropriate macro - KERNEL_VERSION() - is already provided in
linux/version.h. Use it in libgpiomockup.
When using gpiod_foreach_chip_noclose() we need to close any non-mockup
GPIO chip that may be present in the system. Otherwise we'll be leaking
memory.
bindings: python: fix GCC8 warnings due to function pointer casting
GCC8 has added a new warning heuristic to detect invalid function
casts. This causes a lot of warnings when building libgpiod python
bindings.
Let's use Py_UNUSED where applicable to make function prototypes match
that of PyCFunction. For keyword functions, let's cast them to a
function pointer taking no arguments before casting them again to
PyCFunction. This may not be the best solution but it has to do for
the moment.
When exiting normally we free all allocated resources (mostly to keep
valgrind quiet). The toolpath string is not being freed and pops up in
valgrind's "still reachable" category of leaks. Free this memory too
in the atexit() cleanup callback.
tests: gpio-mockup: store the number of chips in gpio_mockup_probe()
We're currently not storing the number of chips in the gpio-mockup
context structure after probing the module. The value is left
initialized to 0. This leads to both a memory leak and to chip ordering
errors when running the tests at high CPU loads. This patch fixes both
issues.
Fixes: e43e46d4f79c ("tests: mockup: add a library for controlling the gpio-mockup module") Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
tests: gpio-mockup: don't allocate the chip structure twice
We incorrectly allocate the chip structure twice: once in
gpio_mockup_probe() and then again in make_chip() which leads to a
memory leak. Remove the first instance as it's the one that's
unnecessary.
Fixes: e43e46d4f79c ("tests: mockup: add a library for controlling the gpio-mockup module") Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
tests: gpiomon: use test_trigger_event() to fix timing errors
Since we don't need to trigger events from a separate thread when
testing tools and we've experienced timing issues with these two
test cases, let's use the new helper for triggering of single events.
We use a separate thread to trigger line events for library test cases
but if we're testing tools which are separate processes, we can generate
the events from the main thread. Add a simple helper for that.
tests: gpiomon: tweak the timing for single event type tests
Since switching to using libgpiomockup there are some timing problems
with two gpiomon test cases. I'm not sure what the reason is and we'll
be switching to glib unit tests soon, so let's not waste time on that
and just increase the sleep time.
tests: mockup: add a library for controlling the gpio-mockup module
In order to both switch to glib unit testing framework for core
libgpiod tests as well as to cover the bindings in other languages
with proper testing while avoiding unnecessary code duplication, let's
factor out the code responsible for interaction with the gpio-mockup
module into a separate shared library.
Having both --enable-tests configure option and the check make target
seems redundant and confusing. Especially since the tests won't even
run without appropriate permissions for /dev/gpiochipX anyway.
Remove the check target and the --enable-install-tests option. From now
on the tests - if enabled - are built as a regular program installed to
${prefix}/bin.
After recent changes the tests stopped working when other (non mockup)
GPIO chips are present in the system. The reason for that is broken
mapping of chip numbers to chip indexes. This patch fixes it.
tests: gpiomon: increase the time waiting for events
We no longer have influence over the type of generated events. We
always start with a rising edge-event and then get alternating edges.
We need to wait slightly longer to collect the events we're expecting
in one of the test cases.
tests: provide test_debugfs_get/set_value() helpers
Since linux v5.1 gpio-mockup simulates pull-up/down resistors for dummy
lines. The current pull can be set using debugfs. Since we can no longer
verify if the character device interface properly sets line values, the
debugfs interface now also allows to read line values. This way we can
verify if the ABI works correctly using different code paths.
Provide functions for reading and setting values of mockup GPIO lines.
Since linux v5.1 the debugfs path for gpio-mockup changed. Use the new
path both for the main directory as well as for the chip subdirectories
which no longer use the chip's label.
tests: don't allow to generate events of the same type subsequently
Since linux v5.1 the state of simulated lines is tracked by the kernel
and it's no longer possible to generate two subsequent events of the
same type (e.g. two rising edge events one after another). Drop the
event_type parameter from test_set_event() and adjust all users.
TODO: add a list of things to implement for feature-complete
Add a document listing major features I'd like to see in libgpiod to
make it more or less complete (not including of course the support for
any potential new features that the kernel may expose in the future).
David Kozub [Thu, 28 Mar 2019 22:49:23 +0000 (23:49 +0100)]
bindings: cxx: make operator bool() explicit
A non-explicit operator bool() is dangerous: It allows unwanted
conversion to integral types:
gpiod::chip c;
int i = c;
This is a trivial example, but more insidious effects are possible,
e.g. with std::set<gpiod::chip>, which would use the operator bool()
to implement element comparison, thus turning such set into a set that
has at most 2 elements: An invalid (false) element and just 1 valid
element:
std::set<gpiod::chip> s;
s.emplace();
s.emplace("/dev/gpiochip0");
s.emplace("/dev/gpiochip1");
// s.size() is still 2 even if both chips were opened!
Making the operator explicit disables this.
See e.g.
https://en.cppreference.com/w/cpp/language/implicit_conversion#The_safe_bool_problem
for more info on this.
Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
David Kozub [Thu, 28 Mar 2019 23:27:28 +0000 (00:27 +0100)]
bindings: cxx: remove superfluous std::move
There is no need to use std::move on an r-value (e.g.
std::move(gpiod::chip())). Similarly, there is no need to use std::move
for returning a local variable as in:
T foo()
{
U local;
return std::move(local);
}
Not only it doesn't help, it actually inhibits named return value
optimization.
See e.g. https://isocpp.org/blog/2013/02/no-really-moving-a-return-value-is-easy-stackoverflow
Signed-off-by: David Kozub <zub@linux.fjfi.cvut.cz> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Currently we only support running the test cases for gpio-tools from
the top-level source directory. Some users want to install the test
executable and run the tests from other locations (/bin, /usr/bin or
custom path).
This patch makes the test suite look in the source tree path first,
then check the directory in which the program resides and last iterate
over all directories in $PATH.
We only do that once at the beginning and then reuse the path later.
Cc: Anders Roxell <anders.roxell@linaro.org> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com> Reviewed-by: Anders Roxell <anders.roxell@linaro.org> Tested-by: Anders Roxell <anders.roxell@linaro.org>
Anders Roxell [Fri, 15 Mar 2019 10:01:44 +0000 (11:01 +0100)]
tests: enable installing tests to bindir
Add an enable option to configure that allows to install the testing
binary to <prefix>/bindir.
Signed-off-by: Anders Roxell <anders.roxell@linaro.org>
[Bartosz: trimmed the patch to fit the new approach] Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
The src directory containing lib and tools is not necessary and will
actually cause problems if we tried to add a new component to src/
which would depend on libraries from the bindings directory because it
(bindings) already depends on src/lib.
Simplify the directory structure. Move lib and tools to the root source
directory and update all relevant files. That way we can specify the
exact build order of components from the top source Makefile.am.
Currently the man page template used by help2man is not included in the
distribution tarball. This causes build failures on systems that
actually do have help2man in which case we rebuild the man pages
locally. Add template to EXTRA_DIST.
Return -1 and set errno to EINVAL if the number of lines specified in
any context-less helper is 0. This is done in addition to bailing out
when the number is higher than the maximum supported number of GPIO
lines. This fixes the segfaults that currently ocurr for num_lines == 0.