Skip to contentSkip to conversation

libgpiod: Add Rust bindings

PATCH Superseded Viresh Kumar 2 participants 5 messages Nov 29, 2021 – Dec 1, 2021 GPIO

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

cover libgpiod: Add Rust bindings

Hi Bartosz,

This patch adds rust bindings for libgpiod v2.0, this is already partially tested with the virtio rust backend I am developing, which uses these to talk to the host kernel.

This is based of the next/post-libgpiod-2.0 branch.

I haven't added any mock test for this as of now and I am not sure how exactly am I required to add them. I did see what you mentioned in your patchset about mock-test vs gpio-sim stuff. Rust also have its own test-framework and I am not sure if that should be used instead or something else.

Since I am posting this publicly for the first time, it is still named as V1. I have not made significant changes to the code since last time, but just divided the same into multiple files.

--
Viresh

Viresh Kumar (2):
libgpiod: Generate rust FFI bindings
libgpiod: Add rust wrappers

 .gitignore                          |   6 +
 bindings/rust/Cargo.toml            |  14 +
 bindings/rust/build.rs              |  60 ++++
 bindings/rust/src/bindings.rs       |  16 ++
 bindings/rust/src/chip.rs           | 197 +++++++++++++
 bindings/rust/src/edge_event.rs     |  78 +++++
 bindings/rust/src/event_buffer.rs   |  59 ++++
 bindings/rust/src/info_event.rs     |  70 +++++
 bindings/rust/src/lib.rs            | 268 +++++++++++++++++
 bindings/rust/src/line_config.rs    | 431 ++++++++++++++++++++++++++++
 bindings/rust/src/line_info.rs      | 186 ++++++++++++
 bindings/rust/src/line_request.rs   | 218 ++++++++++++++
 bindings/rust/src/request_config.rs | 118 ++++++++
 bindings/rust/wrapper.h             |   2 +
 14 files changed, 1723 insertions(+)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/build.rs
 create mode 100644 bindings/rust/src/bindings.rs
 create mode 100644 bindings/rust/src/chip.rs
 create mode 100644 bindings/rust/src/edge_event.rs
 create mode 100644 bindings/rust/src/event_buffer.rs
 create mode 100644 bindings/rust/src/info_event.rs
 create mode 100644 bindings/rust/src/lib.rs
 create mode 100644 bindings/rust/src/line_config.rs
 create mode 100644 bindings/rust/src/line_info.rs
 create mode 100644 bindings/rust/src/line_request.rs
 create mode 100644 bindings/rust/src/request_config.rs
 create mode 100644 bindings/rust/wrapper.h
Signature · 1 line
2.31.1.272.g89b43f80a514
#1 lore raw

PATCH 1/2 libgpiod: Generate rust FFI bindings

Build FFI bindings for the libgpiod helpers.

---
 .gitignore                    |  6 ++++
 bindings/rust/Cargo.toml      | 12 +++++++
 bindings/rust/build.rs        | 60 +++++++++++++++++++++++++++++++++++
 bindings/rust/src/bindings.rs | 16 ++++++++++
 bindings/rust/src/lib.rs      |  0
 bindings/rust/wrapper.h       |  2 ++
 6 files changed, 96 insertions(+)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/build.rs
 create mode 100644 bindings/rust/src/bindings.rs
 create mode 100644 bindings/rust/src/lib.rs
 create mode 100644 bindings/rust/wrapper.h
.gitignore +6 -0
index 2d7cc7fc0758..8b82ada3bc80 100644
@@ -29,3 +29,9 @@ libtool
 *-libtool
 m4/
 stamp-h1
+
+# Added by cargo
+
+bindings/rust/target
+bindings/rust/Cargo.lock
+bindings/rust/bindings_gen.rs
bindings/rust/Cargo.toml new file +12 -0
new file mode 100644
index 000000000000..62f3d52ddb0f
@@ -0,0 +1,12 @@
+[package]
+name = "libgpiod"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+
+[build-dependencies]
+bindgen = "0.59.1"
+cc = "1.0.46"
bindings/rust/build.rs new file +60 -0

60 lines not on this page · raw message

bindings/rust/src/bindings.rs new file +16 -0
new file mode 100644
index 000000000000..7d6caa7d9c11
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#[allow(
+    clippy::all,
+    deref_nullptr,
+    dead_code,
+    non_camel_case_types,
+    non_upper_case_globals,
+    non_snake_case,
+    improper_ctypes
+)]
+
+mod bindings_raw {
+    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+}
+pub use bindings_raw::*;
bindings/rust/src/lib.rs new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
bindings/rust/wrapper.h new file +2 -0
new file mode 100644
index 000000000000..50dc5f4db406
@@ -0,0 +1,2 @@
+#include <string.h>
+#include "../../include/gpiod.h"
Signature · 1 line
2.31.1.272.g89b43f80a514
#2 lore raw

PATCH 2/2 libgpiod: Add rust wrappers

In reply to Viresh Kumar

Add rust wrappers around FFI bindings to provide a convenient interface for the users.

---
 bindings/rust/Cargo.toml            |   2 +
 bindings/rust/src/chip.rs           | 197 +++++++++++++
 bindings/rust/src/edge_event.rs     |  78 +++++
 bindings/rust/src/event_buffer.rs   |  59 ++++
 bindings/rust/src/info_event.rs     |  70 +++++
 bindings/rust/src/lib.rs            | 268 +++++++++++++++++
 bindings/rust/src/line_config.rs    | 431 ++++++++++++++++++++++++++++
 bindings/rust/src/line_info.rs      | 186 ++++++++++++
 bindings/rust/src/line_request.rs   | 218 ++++++++++++++
 bindings/rust/src/request_config.rs | 118 ++++++++
 10 files changed, 1627 insertions(+)
 create mode 100644 bindings/rust/src/chip.rs
 create mode 100644 bindings/rust/src/edge_event.rs
 create mode 100644 bindings/rust/src/event_buffer.rs
 create mode 100644 bindings/rust/src/info_event.rs
 create mode 100644 bindings/rust/src/line_config.rs
 create mode 100644 bindings/rust/src/line_info.rs
 create mode 100644 bindings/rust/src/line_request.rs
 create mode 100644 bindings/rust/src/request_config.rs
bindings/rust/Cargo.toml +2 -0
index 62f3d52ddb0f..d57dfd65ba7d 100644
@@ -6,6 +6,8 @@ edition = "2021"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+thiserror = "1.0"
+vmm-sys-util = "=0.9.0"
 
 [build-dependencies]
 bindgen = "0.59.1"
bindings/rust/src/chip.rs new file +197 -0

197 lines not on this page · raw message

bindings/rust/src/edge_event.rs new file +78 -0

78 lines not on this page · raw message

bindings/rust/src/event_buffer.rs new file +59 -0

59 lines not on this page · raw message

bindings/rust/src/info_event.rs new file +70 -0

70 lines not on this page · raw message

bindings/rust/src/lib.rs +268 -0

268 lines not on this page · raw message

bindings/rust/src/line_config.rs new file +431 -0

431 lines not on this page · raw message

bindings/rust/src/line_info.rs new file +186 -0

186 lines not on this page · raw message

bindings/rust/src/line_request.rs new file +218 -0

218 lines not on this page · raw message

bindings/rust/src/request_config.rs new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
2.31.1.272.g89b43f80a514
#3 lore raw
In reply to Viresh Kumar
Quoted 54 lines · Viresh Kumar

Hi Bartosz,

This patch adds rust bindings for libgpiod v2.0, this is already partially tested with the virtio rust backend I am developing, which uses these to talk to the host kernel.

This is based of the next/post-libgpiod-2.0 branch.

I haven't added any mock test for this as of now and I am not sure how exactly am I required to add them. I did see what you mentioned in your patchset about mock-test vs gpio-sim stuff. Rust also have its own test-framework and I am not sure if that should be used instead or something else.

Since I am posting this publicly for the first time, it is still named as V1. I have not made significant changes to the code since last time, but just divided the same into multiple files.

--
Viresh

Viresh Kumar (2):
libgpiod: Generate rust FFI bindings
libgpiod: Add rust wrappers

 .gitignore                          |   6 +
 bindings/rust/Cargo.toml            |  14 +
 bindings/rust/build.rs              |  60 ++++
 bindings/rust/src/bindings.rs       |  16 ++
 bindings/rust/src/chip.rs           | 197 +++++++++++++
 bindings/rust/src/edge_event.rs     |  78 +++++
 bindings/rust/src/event_buffer.rs   |  59 ++++
 bindings/rust/src/info_event.rs     |  70 +++++
 bindings/rust/src/lib.rs            | 268 +++++++++++++++++
 bindings/rust/src/line_config.rs    | 431 ++++++++++++++++++++++++++++
 bindings/rust/src/line_info.rs      | 186 ++++++++++++
 bindings/rust/src/line_request.rs   | 218 ++++++++++++++
 bindings/rust/src/request_config.rs | 118 ++++++++
 bindings/rust/wrapper.h             |   2 +
 14 files changed, 1723 insertions(+)
 create mode 100644 bindings/rust/Cargo.toml
 create mode 100644 bindings/rust/build.rs
 create mode 100644 bindings/rust/src/bindings.rs
 create mode 100644 bindings/rust/src/chip.rs
 create mode 100644 bindings/rust/src/edge_event.rs
 create mode 100644 bindings/rust/src/event_buffer.rs
 create mode 100644 bindings/rust/src/info_event.rs
 create mode 100644 bindings/rust/src/lib.rs
 create mode 100644 bindings/rust/src/line_config.rs
 create mode 100644 bindings/rust/src/line_info.rs
 create mode 100644 bindings/rust/src/line_request.rs
 create mode 100644 bindings/rust/src/request_config.rs
 create mode 100644 bindings/rust/wrapper.h
Quoted 3 lines

--
2.31.1.272.g89b43f80a514

Hi Viresh!

Thanks for the hard work on that. Before I even dig into the patches themselves, I'd like to clarify a couple things (because I'm still not sure we're on the same page) and the projected timeline for v2.

#1. The v2 API is obviously not stable yet and we're still reworking certain structures (like line_config's accessors). This means I can't merge the bindings just yet but I'm fine with taking them in for v2 given that the missing elements are added, which brings me to:

#2. If you look at the existing bindings, you'll notice they all have tests implemented. These tests use the combination of whatever testing framework was chosen for a given language and a wrapper around libgpio-mockup that allows to use the gpio-mockup kernel module to instantiate simulated GPIO devices. I'd like to see a comprehensive test suite for the rust bindings too before they get into the repo. Except that:

#3. The development of other parts of the project is currently blocked by the gpio-sim development in the kernel. I don't want to reuse gpio-mockup in v2 because it's simply badly designed. Currently the v11 of the gpio-sim series[1] (based on configfs & sysfs) is on the list and only once it lands in mainline, will we be able to advance the tests for the C, C++ and Python interfaces. This is when the rust tests should be developed too by following what other parts of libgpiod do: providing a wrapper around the future libgpiosim (a wrapper around gpio-sim configfs/sysfs interface that I'll implement once gpio-sim is in next) and then using whatever testing framework for rust is preferable.

#4. This one is something you could already add at this time: other bindings contain the examples directory. Inside there are simplified reimplementations of the gpio-tools. It would be very useful for me (as someone not very fluent in rust) to see how those bindings are used in practice. Could you please add it?

Thanks!
Bart

[1] https://lkml.org/lkml/2021/11/30/898

#4 lore raw
Quoted 9 lines · Bartosz Golaszewski

Thanks for the hard work on that. Before I even dig into the patches themselves, I'd like to clarify a couple things (because I'm still not sure we're on the same page) and the projected timeline for v2.

#1. The v2 API is obviously not stable yet and we're still reworking certain structures (like line_config's accessors). This means I can't merge the bindings just yet but I'm fine with taking them in for v2 given that the missing elements are added, which brings me to:

Quoted 7 lines

#2. If you look at the existing bindings, you'll notice they all have tests implemented. These tests use the combination of whatever testing framework was chosen for a given language and a wrapper around libgpio-mockup that allows to use the gpio-mockup kernel module to instantiate simulated GPIO devices. I'd like to see a comprehensive test suite for the rust bindings too before they get into the repo. Except that:

Yeah, I was able to get around that yesterday, while I was integrating rust's build with Make and understood what we are doing for cpp and python.

For now, I dropped the idea of implementing rust tests, and wait for you to go ahead with migrating the python/cpp ones to libgpiosim.

Quoted 5 lines

#3. The development of other parts of the project is currently blocked by the gpio-sim development in the kernel. I don't want to reuse gpio-mockup in v2 because it's simply badly designed. Currently the v11 of the gpio-sim series[1] (based on configfs & sysfs) is on the list

I went into details of that as well yesterday, looked nice.

Quoted 7 lines

and only once it lands in mainline, will we be able to advance the tests for the C, C++ and Python interfaces. This is when the rust tests should be developed too by following what other parts of libgpiod do: providing a wrapper around the future libgpiosim (a wrapper around gpio-sim configfs/sysfs interface that I'll implement once gpio-sim is in next) and then using whatever testing framework for rust is preferable.

Exactly.

Quoted 5 lines

#4. This one is something you could already add at this time: other bindings contain the examples directory. Inside there are simplified reimplementations of the gpio-tools. It would be very useful for me (as someone not very fluent in rust) to see how those bindings are used in practice. Could you please add it?

Oh yes, sure. I can do that.

Signature · 1 line
viresh