1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Commit Graph

342 Commits

Author SHA1 Message Date
Bartosz Golaszewski
e8e38dc67d bindings: python: gpiod v2.2.1
Minor gpiod package release.

Changelog:
- fix requesting lines by name with multiple entries
- fix a use-after-free bug in python bindings
- fix passing the event clock property to line requests in python bindings

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-08-01 14:32:54 +02:00
Benjamin Cabé
c3be23d6d1 bindings: python: properly pass event clock settings
Python binding was ignoring event_clock line setting.

Signed-off-by: Benjamin Cabé <kartben@gmail.com>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240731104658.93117-1-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-08-01 10:33:50 +02:00
Iker Pedrosa
cdda65e3d7 bindings: python: fix a use-after-free bug
`req_cfg` variable is freed and then used, which would generate an
error. Avoid this problem by freeing when the variable will no longer be
used.

Signed-off-by: Iker Pedrosa <ikerpedrosam@gmail.com>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/3d8b12dd60eec59d4184c0bcc7d575b4eccbc22c.1722250385.git.ikerpedrosam@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-08-01 10:33:44 +02:00
Chuang Zhu
a1617c2f61 bindings: python: fix line request by name with multiple entries
When multiple entries are requested using line names in
Chip.request_lines(), only the the last entry is added to
LineRequest._name_map, causing a ValueError when trying to use functions
like LineRequest.set_value() on any former entries.

Move the required variables to the correct scope.

Signed-off-by: Chuang Zhu <git@chuang.cz>
[Bartosz: tweak the commit message]
Co-developed-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240710125719.33655-2-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-08-01 10:33:35 +02:00
Bartosz Golaszewski
db68634b73 bindings: python: gpiod v2.2.0
Minor gpiod package release.

Changelog:
- add a script for generating sdist and wheels
- fix make build
- support casting of line.Value to bool
- test fixes
- fix __repr__() implementations

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-06-18 22:23:31 +02:00
Vincent Fazio
72d2fa01a2 bindings: python: add script to generate sdist and wheels
Introduce a shell script that generates an sdist tarball and PEP 600/656
conformant wheels.

The wheels are generated via cibuildwheel, a tool provided by the Python
Packaging Authority (PyPA) [0]. The tool leverages toolchains within
containers maintained by PyPA [1] to generate wheels that are runnable
on hosts that meet the platform compatibility tag [2] requirements.

By default, the script creates X86_64 and AArch64 CPython 3.9-3.12
wheels for glibc and musl libc based systems.

These defaults can be overridden via CIBW_* environment variables [3].

[0]: https://cibuildwheel.pypa.io/en/stable/
[1]: https://github.com/pypa/manylinux/
[2]: https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/
[3]: https://cibuildwheel.pypa.io/en/stable/options/#options-summary

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
Link: https://lore.kernel.org/r/20240611205041.1448276-1-vfazio@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-06-12 20:15:21 +02:00
Khem Raj
95a2eb76bf bindings: cxx: Migrate C++ tests to use Catch2 v3
Catch2 v3.x has API changes which needs to be addressed
in the tests themselves, hence this changeset is to fix
those.

Signed-off-by: Khem Raj <raj.khem@gmail.com>
Link: https://lore.kernel.org/r/20240531184223.3949069-1-raj.khem@gmail.com
[Bartosz: added a version requirement (>= 3.0) for catch2 to configure.ac]
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-06-03 11:36:22 +02:00
Kent Gibson
dee10f0807 bindings: python: fix python-tests-run make target
The python-tests-run target is broken as it does not correctly split
lines.  It also calls Python directly rather then through the $PYTHON
variable.

Fix the line splitting and call Python using the $PYTHON variable.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240526113234.253859-3-warthog618@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-05-28 11:13:15 +02:00
Kent Gibson
8635780c1c bindings: python: add Testing section to README
Add a section describing how to run the test suite.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240526113234.253859-2-warthog618@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-05-28 11:13:15 +02:00
Bartosz Golaszewski
27fe10150f bindings: python: tests: run the Value cast test with the rest of the suite
Make the new test file for line definitions part of the entire test-suite
for python bindings.

Fixes: c8e3ae0499 ("bindings: python: tests: add test for casting line.Value to bool")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-05-23 10:08:31 +02:00
Kent Gibson
c8e3ae0499 bindings: python: tests: add test for casting line.Value to bool
The line.Value represents the logical line state, so intuitively you
would expect it to be able to be cast to bool, with ACTIVE
corresponding to True, and INACTIVE to False.

Add a test that line.Value can be cast to bool.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240522004643.96863-2-warthog618@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-05-23 09:51:53 +02:00
Kent Gibson
5f9152b0ca bindings: python: support casting line.Value to bool
Python types default to being truthy when cast to bool, so casting
line.Value to bool always returns True.

Add a line.Value.__bool__() operator to map the line value to bool as
one would intuitively expect, so ACTIVE is True and INACTIVE is False.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/20240522004643.96863-3-warthog618@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-05-23 09:51:53 +02:00
Bartosz Golaszewski
3ece522b8d bindings: python: tests: fix invalid syntax warnings
Use double '/' for escaping regex special characters for patterns in
python strings to silence the following warnings:

bindings/python/tests/tests_edge_event.py:211: SyntaxWarning: invalid escape sequence '\.'
  "<EdgeEvent type=Type\.RISING_EDGE timestamp_ns=[0-9]+ line_offset=0 global_seqno=1 line_seqno=1>",
bindings/python/tests/tests_info_event.py:188: SyntaxWarning: invalid escape sequence '\.'
  '<InfoEvent type=Type\.LINE_REQUESTED timestamp_ns=[0-9]+ line_info=<LineInfo offset=0 name="None" used=True consumer="\?" direction=Direction\.INPUT active_low=False bias=Bias\.UNKNOWN drive=Drive\.PUSH_PULL edge_detection=Edge\.NONE event_clock=Clock\.MONOTONIC debounced=False debounce_period=0:00:00>>',

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-05-22 20:07:34 +02:00
Bartosz Golaszewski
76ecc337c5 licensing: relicense C++ bindings under LGPL-2.1-or-later
Commit ea84f882d5 ("licensing: relicense C++ library code under
LGPL-3.0-or-later") changed the license of C++ bindings in order to
solve potential issues with code generated from templates[1], default
implementations, etc. However this change makes the bindings less
attractive to projects that have strict licensing restrictions and avoid
GPL-3.0 code[2].

After talking to Grant Likely I decided that the best approach is to
make the bindings available under LGPL-v2.1-or-later and simply let the
end user decide which version's text to apply.

While at it: tweak the README to also mention that examples are provided
under GPL-2.0-or-later.

Link: [1] https://www.spinics.net/lists/linux-gpio/msg46605.html
Link: [2] https://github.com/brgl/libgpiod/issues/72
Suggested-by: Walter Lozano <walter.lozano@collabora.com>
Suggested-by: Grant Likely <grant.likely@linaro.org>
Acked-by: Kent Gibson <warthog618@gmail.com>
Acked-by: Kevin Hilman <khilman@baylibre.com>
Acked-by: Grant Likely <grant.likely@linaro.org>
Link: https://lore.kernel.org/r/20240416212141.6683-2-brgl@bgdev.pl
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-04-22 14:04:19 +02:00
Bartosz Golaszewski
1964b728ca bindings: cxx: fix doxygen description of line_request::release()
Seems like the description was copy-pasted from chip::close(). Fix it.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-04-08 11:26:11 +02:00
Kent Gibson
02e05a134f treewide: fix spelling of "immediately".
"immediately" is incorrectly spelled "immediatelly" in several places, so
replace with correct spelling.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-03-25 14:26:48 +01:00
orbea
b10af6b017 bindings: cxx: link using the libtool archives
When linking with internal dependencies that were built with libtool the
most reliable method is to use the libtool archive (.la) files.

When building with slibtool it fails when it doesn't find the -lgpiod
linker flag, but if libgpiod is already installed to the system it will
be built using the system version instead of the newly built libraries.

Link: https://bugs.gentoo.org/913899
Signed-off-by: orbea <orbea@riseup.net>
[Bartosz: fix a typo in tests Makefile]
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-03-20 17:44:14 +01:00
Kent Gibson
0c753417de bindings: cxx: examples: fix typo in comment
Fix typo in comment in watch_multiple_line_values.cpp.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-03-10 19:42:03 +01:00
Bartosz Golaszewski
6d5d8afc9c bindings: python: fix __repr__() implementations
The __repr__() function should - if possible - return a valid Python
expression that can be used to instantiate a new object when evaluated.

LineSettings.__repr__() is missing comas between arguments. Both Chip and
LineSettings also don't prefix the returned string with 'gpiod.'. Fix
both functions and add more test cases - including actually using the
strings returned by __repr__() to create new objects and compare their
contents.

Reported-by: Robert Thomas <rob.thomas@raspberrypi.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-02-05 09:49:10 +01:00
Bartosz Golaszewski
40adfc1eb6 bindings: python: fix package installation with Makefile build
The modules are installed in the .egg directory, and therefore cannot be
imported after that. Additionally PIP tries to remove the global gpiod
module if it's not invoked with the --ignore-installed option.

Specify correct --root and fix the --prefix switch.

Link: https://github.com/pypa/pip/issues/3063
Suggested-by: Maxim Devaev <mdevaev@gmail.com>
Reported-by: Maxim Devaev <mdevaev@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2024-01-09 20:09:37 +01:00
Erik Schilling
9f5e653fb5 bindings: rust: libgpiod-sys: new release
During 86860fb ("bindings: rust: libgpiod: release 0.2.2"), I forgot that
we also need a libgpiod-sys release in order to expose the new feature
flag to raise the minimum libgpiod version.

Changelog:

7552e5d (bindings: rust: expose v2.1 features as flag, 2023-11-06)
bc91656 (bindings: rust: add wrapper.h to EXTRA_DIST, 2023-11-03)
2e6ee87 (bindings: rust: mention the libgpiod crate from libgpiod-sys, 2023-07-03)
d04639d (bindings: rust: bump MSRV to 1.60, 2023-06-16)
ebfed6c (bindings: rust: document build without install, 2023-06-12)
bce8623 (bindings: rust: exclude Makefile.am from package, 2023-06-12)
caabf53 (bindings: rust: add missing license and copyright boilerplate, 2023-06-13)

Most changes only touch the build scripts or modify packaging details.
Bumping the MSRV and introducing a new feature does not require a major
bump.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-21 15:47:37 +01:00
Erik Schilling
86860fb65f bindings: rust: libgpiod: release 0.2.2
Minor update the exposes feature flag and bindings for v2.1.

Changelog:
==========

7552e5d (bindings: rust: expose v2.1 features as flag, 2023-11-06)
bc91656 (bindings: rust: add wrapper.h to EXTRA_DIST, 2023-11-03)
3b40a37 (bindings: rust: fix EXTRA_DIST for examples, 2023-11-03)

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-15 18:01:55 +01:00
Bartosz Golaszewski
665f360a26 bindings: python: gpiod v2.1.3
Another small bugfix release addressing an issue with building tests.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-09 21:26:53 +01:00
Bartosz Golaszewski
ab8662e50e bindings: python: add build_tests.py to the manifest
build_tests.py is not part of the sdist so tests cannot be built from
pypi releases. Add it to MANIFEST.in.

Fixes: d588a6a592 ("bindings: python: standalone build tooling for tests")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-09 21:22:07 +01:00
Bartosz Golaszewski
3a0f6a0dd3 bindings: python: gpiod v2.1.2
This is a small bugfix release improving how the C extensions for python
bindings tests are built.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-09 21:15:10 +01:00
Phil Howard
d588a6a592 bindings: python: standalone build tooling for tests
Move extension definitions and tooling for building tests into
`build_tests.py` and update Makefile.am to call it with appropriate path
prefixes.

`build_tests.py` will perform a standalone build of the text extensions,
keeping any build noise in a temporary directory and copying the final
built modules automatically out to `tests/gpiosim` and `tests/procname`.

Add "python-tests-run" to Makefile.am so it's clear how to run the tests.

Add .so object files generated by build_test.py to Makefile.am's
clean-local.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-08 20:50:02 +01:00
Erik Schilling
7552e5d9ed bindings: rust: expose v2.1 features as flag
v2.1 provided a getter to read the chip name from a request.

This adds v2_1 as feature in order to raise the minimum requested version
to v2.1 for exposing the new API.

This is identical to the concept patch that was posted [1] when this
feature flag mechanism was proposed. Only the commit message was
reworded.

[1] https://lore.kernel.org/all/20231006-b4-bindings-old-version-fix-v1-2-a65f431afb97@linaro.org/

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-08 14:06:03 +01:00
Bartosz Golaszewski
8c8547bfdd bindings: python: gpiod v2.1.1
This is a small bugfix release of the python bindings fixing how the
homepage on pypi is displayed.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 20:31:23 +01:00
Phil Howard
a98a0ff3d5 bindings: python: fix README.md SPDX license tags
Replace the # (which is a markdown heading) with HTML tags to hide SPDX
identifier tags within the project description on pypi.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 20:30:56 +01:00
Bartosz Golaszewski
b9cf47accb bindings: python: gpiod v2.1.0
Release libgpiod python bindings v2.1.0.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 19:34:25 +01:00
Bartosz Golaszewski
f054a7099d tests: fix version parsing (again)
This time we missed the fact that the last part is optional. Fix it
treewide.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 17:17:34 +01:00
Bartosz Golaszewski
bc91656de8 bindings: rust: add wrapper.h to EXTRA_DIST
We need to package wrapper.h in the release tarball so add it to
EXTRA_DIST.

Fixes: 1f80859530 ("bindings: rust: build against pkg-config info")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 14:01:27 +01:00
Bartosz Golaszewski
3b40a37e34 bindings: rust: fix EXTRA_DIST for examples
One of the examples listed in EXTRA_DIST is missing the .rs suffix while
another lists a file that doesn't exist. Fix both issues.

Fixes: aaed0f2e62 ("bindings: rust: examples: replace tools examples with use case examples")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 12:04:16 +01:00
Bartosz Golaszewski
16a35cc268 bindings: python: fix EXTRA_DIST for examples
One of the example scripts is named differently than what the Makefile
lists it as. Change the EXTRA_DIST variable to fix it.

Fixes: 96c52d3e59 ("bindings: python: examples: replace tools examples with use case examples")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 11:53:11 +01:00
Phil Howard
0e81b62c01 bindings: python: optionally include module in sdist
Optionally vendor libgpiod source into sdist so that the Python module can
be built from source, even with a missing or mismatched system libgpiod.

Add two new environment variables "LINK_SYSTEM_LIBGPIOD" and
"LIBGPIOD_VERSION" to control what kind of package setup.py will build.

In order to build an sdist or wheel package with a vendored libgpiod a
version must be specified via the "LIBGPIOD_VERSION" environment variable.

This will instruct setup.py to verify the given version against the list
in sha256sums.asc and ensure it meets or exceeds a LIBGPIOD_MINIMUM_VERSION
required for compatibility with the bindings.

It will then fetch the tarball matching the requested version from
mirrors.edge.kernel.org, verify the sha256 signature, unpack it, and copy
the lib and include directories into the package root so they can be
included in sdist or used to build a binary wheel.

eg: LIBGPIOD_VERSION=2.1.0 python3 setup.py sdist

Will build a source distribution with gpiod version 2.1.0 source included.

It will also save the gpiod version into "libgpiod-version.txt" so that it
can be passed to the build when the sdist is built by pip.

Requiring an explicit version ensures that the Python bindings - which
can be changed and versions independent of libgpiod - are built against a
stable libgpiod release.

In order to force a package with vendored gpiod source to link the system
libgpiod, the "LINK_SYSTEM_LIBGPIOD" environment variable can be used:

eg: LINK_SYSTEM_LIBGPIOD=1 pip install libgpiod

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-11-03 11:36:28 +01:00
Phil Howard
389a966d4e bindings: python: move long_description into README.md
Move the long_description into README.md and include some basic
information about libgpiod, and some simple Python example code.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-24 11:06:12 +02:00
Bartosz Golaszewski
1a15d301b9 bindings: python: change the package name to 'gpiod'
We now control the name 'gpiod' on pypi. Let's change the project name
as the package we import in python code is named 'gpiod' and not
'libgpiod'

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-24 10:20:21 +02:00
Bartosz Golaszewski
05658dc1bc bindings: python: fix the distutils import
The presence of _distutils in setuptools is an implementation detail. If
we want anything from the distutils package, we should import it directly.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-24 10:16:50 +02:00
Phil Howard
9f9671dbb6 bindings: python: simplify environ access
Use environ.get() in lieu of an explicit check for GPIOD_WITH_TESTS in
os.environ.

Returns None and evaluates as False if GPIOD_WITH_TESTS == "1" is
not specified.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-13 10:36:31 +02:00
Phil Howard
85089d0e40 bindings: python: add pyproject.toml, pep 518
Add pyproject.toml to prevent spurious deprecation warnings from pip.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-13 10:35:01 +02:00
Phil Howard
07a3265715 bindings: python: require python 3.9.0
Required minimum version for PyModule_AddType helper.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-12 21:07:11 +02:00
Erik Schilling
22173b739e bindings: rust: libgpiod: release 0.2.1
While the release technically removes a function, that function was not
available in any released libgpiod version. Therefore, this only bumps
the minor.

This release fixes building with releases from the 2.x series of the C
lib.

Changelog:
=========

acebcf2 (bindings: rust: feature gate unreleased features, 2023-10-06)

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-11 10:16:12 +02:00
Bartosz Golaszewski
b436d05809 bindings: python: replace PyModule_AddObjectRef() with PyModule_AddObject()
PyModule_AddObjectRef() was added in cpython v3.10 while libgpiod claims
to depend on python v3.9. Replace it with an older variant that steals the
reference to the added object on success.

Reported-by: Phil Howard <phil@gadgetoid.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-11 10:02:09 +02:00
Erik Schilling
acebcf2cbe bindings: rust: feature gate unreleased features
`gpiod_line_request_get_chip_name()` is not released yet. Still, libgpiod-sys
will just happily generate bindings for it if it sees the definition in the
header file.

This guards the unreleased features behind an optional feature `vnext`.

To sketch the process of what happens once these features get into an
assumed "2.1" release:

libgpiod-sys will get updated with a `v2_1` feature. That feature would
then raise the minimum version that is attempted to query from pkg-
config. An identical feature will then be introduced on the `libgpiod`
crate and `vnext` guards will be changed to `v2_1` guards. The `vnext`
feature will then be updated to require the new `v2_1` feature.

Eventually, we will probably raise the minimum supported version for the
rust bindings and drop all the version gates before that.

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-10 08:55:07 +02:00
Erik Schilling
e7b02c2259 bindings: rust: libgpiod: release 0.2.0
Since we had some (potentially-)breaking changes, we bump the major for
the next release of the crate.

Note:
I am using the term "major" as defined in the Rust SemVer compatibility
guide [1], where the first non-zero digit is considered as "major".

[1] https://doc.rust-lang.org/cargo/reference/semver.html

Changelog:
=========

(potentially-)breaking changes:
  a29f3e6 (bindings: rust: rename {event,settings}_clone to try_clone, 2023-10-04)
  b290348 (bindings: rust: fix soundness of line_info modeling, 2023-10-03)
  d04639d (bindings: rust: bump MSRV to 1.60, 2023-06-16)

new functionality:
  808d15e (bindings: rust: allow cloning line::InfoRef -> line::Info, 2023-10-03)
  64aac85 (bindings: rust: mark all owning types as `Send`, 2023-09-28)
  d12ce74 (bindings: rust: provide LineRequest::chip_name(), 2023-07-20)
  53226d5 (bindings: rust: examples: add dedicated examples, 2023-06-14)

other changes:
  0a570b6 (bindings: rust: drop unneeded Arc within Chip, 2023-09-27)
  a97fe96 (bindings: rust: construct chip infos by reference, 2023-09-27)
  27afa47 (bindings: rust: remove useless clone, 2023-09-28)
  3f6e0bf (bindings: rust: add README.md for libgpiod crate, 2023-07-03)
  4b8357b (bindings: rust: clippy: silence false-positive on iterator, 2023-06-30)
  39189f0 (bindings: rust: clippy: drop unneeded conversions, 2023-06-30)
  46115fd (bindings: rust: clippy: silence false-positives on casts, 2023-06-30)
  901104e (bindings: rust: clippy: drop unnecessary casts, 2023-06-30)
  46ecbe0 (rust: examples: file comment consistency, 2023-06-24)
  aaed0f2 (bindings: rust: examples: replace tools examples with use case examples, 2023-06-23)
  b37bd9e (bindings: rust: examples: consistency cleanup, 2023-06-23)
  06c8ad9 (bindings: rust: package new examples in the distro tarball, 2023-06-15)

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-05 19:48:40 +02:00
Erik Schilling
a29f3e6957 bindings: rust: rename {event,settings}_clone to try_clone
What is getting cloned is already clear from the type. This also aligns
a bit better with similar methods from the `std` crate [1].

[1] https://doc.rust-lang.org/std/index.html?search=try_clone

Link: https://lore.kernel.org/r/CVUKC1HXG1P8.13XIUCCXN95F0@ablu-work
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-04 20:40:36 +02:00
Erik Schilling
808d15ebff bindings: rust: allow cloning line::InfoRef -> line::Info
While one would usually use the ToOwned [1] contract in rust, libgpipd's
API only allows copying that may fail.

Thus, we cannot implement the existing trait and roll our own method. I
went with `try_clone` since that seems to be used in similar cases across
the `std` crate [2].

It also closes the gap of not having any way to clone owned instances.
Though - again - not through the Clone trait which may not fail [3].

[1] https://doc.rust-lang.org/std/borrow/trait.ToOwned.html
[2] https://doc.rust-lang.org/std/index.html?search=try_clone
[3] https://doc.rust-lang.org/std/clone/trait.Clone.html

Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-04 13:23:22 +02:00
Erik Schilling
b290348726 bindings: rust: fix soundness of line_info modeling
While attention was provided to prevent freeing in non-owned use-cases,
the lifetime of these object was not properly modeled.

The line_info from an event may only be used for as long as the event
exists.

This allowed us to write unsafe-free Rust code that causes a
use-after-free:

  let event = chip.read_info_event().unwrap();
  let line_info = event.line_info().unwrap();
  drop(event);
  dbg!(line_info.name().unwrap());

Which makes the AddressSanitizer scream:

  ==90154==ERROR: AddressSanitizer: heap-use-after-free on address 0x50b000005dc4 at pc 0x55a4f883a009 bp 0x7f60ac8fbbc0 sp 0x7f60ac8fb388
  READ of size 2 at 0x50b000005dc4 thread T2
      [...]
      #3 0x55a4f8c3d5f3 in libgpiod::line_info::Info::name::h5ba0bfd360ecb405 libgpiod/bindings/rust/libgpiod/src/line_info.rs:70:18
    	[...]

  0x50b000005dc4 is located 4 bytes inside of 112-byte region [0x50b000005dc0,0x50b000005e30)
  freed by thread T2 here:
      [...]
      #1 0x7f60b07f7e31 in gpiod_info_event_free libgpiod/lib/info-event.c:61:2
      [...]

  previously allocated by thread T2 here:
      #0 0x55a4f88b04be in malloc /rustc/llvm/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:69:3
      #1 0x7f60b07f8ff0 in gpiod_line_info_from_uapi libgpiod/lib/line-info.c:144:9

The fix is to distinguish between the owned and non-owned variants and
assigning lifetimes to non-owned variants.

For modeling the non-owned type there are a couple of options. The ideal
solution would be using extern_types [1]. But that is still unstable.
Instead, we are defining a #[repr(transparent)] wrapper around the opaque
gpiod_line_info struct and cast the pointer to a reference.

This was recommended on the Rust Discord server as good practise.
(Thanks to Kyuuhachi, shepmaster, pie_flavor and ilyvion! Also thanks to
@epilys for a brainstorming on this on #linaro-virtualization IRC).

Of course, determining the lifetimes and casting across the types
requires some care. So this adds a couple of SAFETY comments that would
probably also have helped the existing code.

[1] https://github.com/rust-lang/rfcs/blob/master/text/1861-extern-types.md

Fixes: 91f9373c65 ("bindings: rust: Add libgpiod crate")
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-04 13:17:50 +02:00
Erik Schilling
64aac85b59 bindings: rust: mark all owning types as Send
The thread-safety rules of libgpiod allow individual object instances to
be used from different threads. So far, this was not actually possible
with the Rust bindings. Not being `Send` disallowed the user to transfer
the ownership to different threads.

Rust also has a `Sync` marker. That one would even allow sending
references of objects to other threads. Since we wrap a lot of C
functions with `fn foo(&self)` signatures, that would not be safe.
libgpiod does not allow concurrent API calls to the same object instance
- which Rust would allow for read-only references. Thus, we do not
define that one.

Chip was already modeled correctly.

line::Info is not marked as Send since it may either be owning or non-
owning. That problem is fixed as part of a separate pull request [1].

[1] https://lore.kernel.org/r/20230927-rust-line-info-soundness-v1-0-990dce6f18ab@linaro.org

Link: https://lore.kernel.org/r/CVHO091CC80Y.3KUOSLSOBVL0T@ablu-work
Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-10-02 09:15:36 +02:00
Erik Schilling
0a570b6d5e bindings: rust: drop unneeded Arc within Chip
Chip was modeled with an Arc that only was used to pass the chip pointer
to the chip::Info constructor. With that refactored to take a reference,
we can just drop the Arc.

This allows to get rid of the `Internal` helper struct that was only
required by the Arc.

As a side-effect, we also get rid of this clippy warning:

  warning: usage of an `Arc` that is not `Send` or `Sync`
    --> libgpiod/src/chip.rs:75:21
     |
  75 |         let ichip = Arc::new(Internal::open(path)?);
     |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: the trait `Send` is not implemented for `Internal`
     = note: the trait `Sync` is not implemented for `Internal`
     = note: required for `Arc<Internal>` to implement `Send` and `Sync`
     = help: consider using an `Rc` instead or wrapping the inner type with a `Mutex`

Signed-off-by: Erik Schilling <erik.schilling@linaro.org>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-09-29 14:46:08 +02:00