tests: simplify and reduce the strictness of version string regex patterns
It seems like every release there's some new problem with parsing the
version strings in tests. Let's reduce the strictness of the patterns
to avoid future test failures and accept the following version schemes:
The square bracket is in the wrong place which makes the regex not match
version strings of the form: x.y.z. Fix the pattern in core and bindings
tests.
tests: provide and use gpiod_test_chip_watch_line_info_or_fail()
We can shrink the code a bit by wrapping the call to
gpiod_chip_watch_line_info() and the subsequent checks in a helper macro.
This also fixes a potential null-pointer dereference in one of the info
event test cases.
Some of the helpers' names don't refer to the objects they're acting
upon. Make the naming consistent. While at it: fix line breaking in the
helpers header.
Sorting chip names with alphasort() results in gpiochip2 coming after
gpiochip19 as it only find the lexicographic order of strings. Switching
to using versionsort() makes it look better and the order is more logical
with the numbering being taken into account.
Reviewed-by: Kent Gibson <warthog618@gmail.com> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
tools: tests: update the kernel version required to run tests
The v5.16 kernel requirement is a leftover from when the tests were
developed to work with gpio-sim before the module was actually released
in v5.17. Update the kernel requirement to v5.17.4 which includes fixes
to set/get_multiple() callbacks in gpio-sim.
build: use AM_V_GEN when calling external programs
In order to output a status line in silent mode and the whole command in
default mode, use the AM_V_GEN predefined automake variable when executing
external programs like doxygen and help2man.
bindings: cxx: tests: fix the test case for chip::unwatch_line_info()
We need to actually trigger a line-info event before making sure the watch
has been disabled.
Fixes: f2245fa32c1b ("bindings: cxx: tests: add a test case for chip::unwatch_line_info()") Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
tests: fix the test case for gpiod_line_request_reconfigure_lines()
We need to actually pass the line config with different offsets to
gpiod_line_request_reconfigure_lines() for the test to make sense.
Fixes: 8ef0ca0fae8f ("tests: add more test cases for gpiod_line_request_reconfigure_lines()") Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
tests: add more test cases for gpiod_line_request_reconfigure_lines()
Add two new test cases for gpiod_line_request_reconfigure_lines(). One
verifies that NULL line config is handled correctly, the other checks
that reconfigured offsets must be the same as the ones requested.
After discussing this a while back we removed the call to fcntl() that
would make the request file descriptor non-blocking so that a call to
gpiod_line_request_read_edge_events() would block if no events are
pending. We agreed that the same behavior should apply to info events and
made the docs state that. Unfortunately we still pass the O_NONBLOCK flag
to open() making read() called on the chip file descriptor return
immediately if no info events are pending. Fix it by removing this flag.
Benjamin Li [Mon, 6 Mar 2023 18:45:45 +0000 (10:45 -0800)]
contrib: add sample Android.bp to build within an Android tree
Add an Android.bp file for Soong, the Android build system, to build
the library including C++ bindings along with all the CLI tools.
This reference Android build file will live in a contrib/ folder to
indiciate it is a less-maintained part of libgpiod. It will need to
be moved to the root directory in order to use it, though, as Soong
doesn't let Blueprint files reference sources in a parent directory.
Signed-off-by: Benjamin Li <benl@squareup.com>
[Bartosz: add the build file to the release tarball generated by 'make dist'] Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Benjamin Li [Mon, 6 Mar 2023 18:45:44 +0000 (10:45 -0800)]
tools: remove dependency on glibc program_invocation_[short_]name
Platforms like Bionic libc don't have program_invocation_[short_]name,
which is a GNU extension. It does have getprogname(), a BSD extension,
but rather than supporting multiple extensions let's just introduce
our own local helpers.
We derive the short name ourselves rather than calling basename(),
as the POSIX version takes char *, not const char *, and is thus
not guaranteed to avoid modifying its input. (The GNU version does
take const char * but we are avoiding extensions here.)
Signed-off-by: Benjamin Li <benl@squareup.com> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
core: sanitize the output values in gpiod_line_config_set_output_values()
We check the output values for invalid ones in
gpiod_line_settings_set_output_value() but in
gpiod_line_config_set_output_values() we just accept all indiscriminately.
Factor out the common checking code into a helper function, use it in both
functions and add a relevant test case.
bindings: cxx: make edge_event's impl struct implementations final
The implementations of the abstract edge_event's base impl private struct
can be made final and their destructors non-virtual. As these types are
entirely private, it does not affect the ABI.
tests: add a test-case for looking up non-standard GPIO line names
Make sure line names with whitespaces or some less common characters (that
can be found in real-life names in mainline .dts files) can be correctly
mapped to offsets.
GPIOD_VERSION_STRING environment variable is no longer used by setup.py.
It's been replaced by reading the version from the variable defined in
version.py.
libgpiod C++ types are not meant to be inherited from. They already don't
even provide virtual destructors so mark them explicitly as final. With
that the destructors of throwable types can also be made non-virtual.
If info is NULL in one of the line-info test cases, we'll still try to
read its property leading to an abort() triggered from assert(info). Just
bail out of the test function if gpiod_chip_get_line_info() fails.
tests: add line value enums for gpiosim GLib wrapper
libgpiosim has a dedicated enum type for line values but the GLib wrapper
still uses magic numbers (0 and 1). Add an enum type for the wrapper and
use it across all test cases.
bindings: cxx: drop the re-export guard of visible symbols
The ifdef guard that changes the definition of GPIOD_CXX_API between
"default" and "hidden" is there to prevent libraries that would include
gpiod.hpp from re-exporting libgpiodcxx symbols.
Unfortunately the hidden linkage only works for translation units within
the same ELF object. At static linking time we must always mark the
throwable types as visible or we'll get the following errors:
/usr/bin/ld: tests-chip.o:(.data+0x0): undefined reference to `typeinfo for gpiod::chip_closed'
/usr/bin/ld: tests-line-request.o:(.data+0x0): undefined reference to `typeinfo for gpiod::request_released'
/usr/bin/ld: .libs/gpiod-cxx-test: hidden symbol `_ZTIN5gpiod11chip_closedE' isn't defined
/usr/bin/ld: final link failed: bad value
On top of that we actually WANT to re-export typeinfo for throwable types
as they can be propagated to external callers when an exception is thrown.
The above error can only be triggered on clang. GCC seems to be
incorrectly omitting the "hidden" attribute in this case.
Fix it by moving to a later version of bindgen, which updates the types
of few of the arguments to the FFI helpers and so required changes to
few of explicit type conversions.
twalk_r() is a GNU extension for the binary search tree API. Musl libc
doesn't provide it and after inquiring with the maintainer it turned out
it won't provide it anytime soon as musl strives to only implement well
standardized functions.
In order to not limit building libgpiosim and tests to glibc only, let's
replace twalk_r() with twalk() and use global variables for the state.
It's not a big deal as we're already using a global root node and a mutex
to protect it.
The DESTDIR variable can be used to perform a staged installation. The
package should be configured as if it was going to be installed in its
final location (e.g., --prefix /usr), but when running make install, the
DESTDIR should be set to the absolute name of a directory into which the
installation will be diverted. From this directory it is easy to review
which files are being installed where, and finally copy them to their
final location by some means.
Prefix $(prefix) with $(DESTDIR) when calling setup.py.
Reported-by: Peter Robinson <pbrobinson@gmail.com> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> Reviewed-by: Peter Robinson <pbrobinson@gmail.com> Tested-by: Peter Robinson <pbrobinson@gmail.com>
bindings: cxx: fix building gpiod::timestamp with clang
Clang's system_clock implementation uses microseconds resolution by
default and we fail to construct a time_point out of chrono::nanoseconds
as the library prohibits us from accidentally losing information when
casting the latter to the former. Use nanoseconds explicitly as the
resolution of the realtime timestamp.
uint is an old compatibility name that GCC still provides but clang
doesn't. Use unsigned int instead.
Fixes: 8ffb6489286f ("tools: line name focussed rework") Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org> Reviewed-by: Kent Gibson <warthog618@gmail.com>
tests: rework error handling and stop overusing g_error()
GLib recommends using g_error() only for programming errors and
unrecoverable situations. It calls abort() and triggers a core dump. This
means that if we encounter an error when running a test case, we'll
exit immediately and leave a "leaked" GPIO simulator in configfs.
Rework the error handling: use g_error() only when a programming bug is
encountered (e.g. invalid enum value) while everywhere else use the
regular GError-based error handling. This way, in case of an error in
libgpiosim, we'll simply fail the current test case and release all
resources as usual.
In order not to pollute the test cases with error handling, let's hide the
actual error checking behind macros for g_gpiosim_chip_new() and
g_gpiosim_chip_get_value(). For g_gpiosim_chip_set_pull() let's just emit
a log and do nothing as the test case in question will inevitably fail if
the expected value is not correctly read back.
In order for the chip to get a failing constructor, we need to link the
test-suite against the entire libgio, not only libgobject (for the
GInitable interface).
Revert "tests: consistently use GLib types in tests"
GLib recommends to use standard library's int as the return type for
main() and g_test_run() also returns int, not gint. Revert the commit
that made main() return gint.
tests: drop the .dispose() method from gpiosim-glib's chip class
The .dispose() method should be used to drop references to any other
GObjects while .finalize() should free any remaining memory. While we are
dropping references in .dispose(), these are references to the libgpiosim
objects that are also guaranteed to always be at most equal 1. Move the
reference drops to .finalize() and remove .dispose() entirely.
tests: add a missing call to parent's implementation of .constructed()
In GObject inheriting classes must call the parent's implementation of
"virtual" methods manually. Add a missing call to parent's .constructed()
method in the GLib wrapper of libgpiosim.
gpioset: use #ifdef instead of #if with GPIOSET_INTERACTIVE
We're not setting GPIOSET_INTERACTIVE to any specific value that we'd
want to check so be consistent and use #ifdef everywhere to simply check
if it's defined or not.
g_variant_new() returns a floating reference that must be converted to
a full reference before returning or else we may get weird reference
counting errors.
We have two helpers that do the same thing: given a key and a map, they
return the associated value or throw an exception if the map doesn't
contain it. The difference is just in the type of the exception. Remove
one and reuse the other. While at it: modify the names a bit for better
readability - especially the mapping function which is not limited to
just mapping enum types.
There are two leftover instances where we return raw C pointers from
local functions. For safety and consistency with the rest of the code
convert those functions to return unique_ptr.
bindings: rust: provide line_config.set_output_values()
Add a new function to line config allowing to set a list of output values
for requested lines. This works very similarily to the C++ version of the
new C interface.
In C++ bindings we can chain the mutators as they all return a reference
to the object they modify. It's a common practice to allow that in Rust
too so make all mutators that don't already do it return a mutable
reference to self.
It's also logically incorrect to make mutators borrow an immutable
reference to self. Even if that builds - as we're fiddling with C
pointers - it could change in the future. It's fine for getters but
setters should all use mutable references.
bindings: python: add the output_values argument to Chip.request_lines()
Add a new optional argument to Chip.request_lines() called output_values.
It accepts a dictionary of mappings between line names or offsets to the
output values the requested lines should be set to at request-time.
core: provide gpiod_line_config_set_output_values()
Currently if user wants to use the same settings for a set of requested
lines with the exception of the output value - they need to go through
hoops by updating the line settings object and adding it one by one to
the line config. Provide a helper function that allows to set a global
list of output values that override the settings. For details on the
interface: see documentation in this commit.
We have two functions in the C API that allow users to retrieve a list
of offsets from objects: gpiod_line_request_get_offsets() and
gpiod_line_config_get_offsets(). Even though they serve pretty much the
same purpose, they have different signatures and one of them also
requires the user to free the memory allocated within the libgpiod
library with a non-libgpiod free() function.
Unify them: make them take the array in which to store offsets and the
size of this array. Make them return the number of offsets actually
stored in the array and make them impossible to fail. Change their names
to be more descriptive and in the case of line_config: add a new function
that allows users to get the number of configured offsets.
Update the entire tree to use the new interfaces.
For rust bindings: also unify the line config interface to return a map
of line settings like C++ bindings do instead of having a function to
get settings by offset. A map returned from a single call is easier to
iterate over with a for loop than using an integer and calling the
previous line_settings() method.
The rust crates builds fine when built with the 'make' command, as
static linking works fine. But when referenced from a remote rust crate,
it gives following error:
error: could not find native static library `gpiod`, perhaps an -L flag is missing?
This happens since we only support 'static' LIB-KIND currently. Remove
the same to allow others to work too.
bindings: rust: make request_config optional in Chip.request_lines()
Request config is not necessary to request lines. In C API we accept
a NULL pointer, in C++ it's not necessary to assign a request_config
to the request builder, in Python the consumer and event buffer size
arguments are optional. Let's make rust bindings consistent and not
require the request config to be always present. Convert the argument
in request_lines to Option and update the rest of the code.
We're using testing wrong variables in the reset_config test case. We
should be checking the retrieved0 settings which are read back from
the line config object.
Implement the copy operator for line_settings. We have a copy() method
for line settings in C API while in C++ it's useful to copy line_settings
returned in an std::map from line_config.get_line_settings().