openwsn-berkeley / lakers

EDHOC implemented in Rust, optimized for microcontrollers, with bindings for C and Python.
https://crates.io/crates/lakers
BSD 3-Clause "New" or "Revised" License
12 stars 10 forks source link

Add an example that works with coap and riot in rust #131

Open geonnave opened 8 months ago

chrysn commented 8 months ago

This may look neater when the coapserver-coaphandler example is split into a library and a runtime part -- then we could share code. Also, I plan on having a client example on the same kind of API, which would allow us to run RIOT device-to-device demos.

geonnave commented 8 months ago

Hitting some issues apparently coming from riot-sys -> bindgen -> proc-macro2.

First, I updated the channel to nightly to satisfy the new dep coap-request.

Then, got the following:

Details

``` $ RUST_BACKTRACE=1 CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_DEBUG=true RIOTBASE=/Users/geovane/Developer/inria/dev/RIOT-FORK/ make BOARD=nrf52840dk warning: `edhoc-crypto-cryptocell310` (lib) generated 9 warnings (run `cargo fix --lib -p edhoc-crypto-cryptocell310` to apply 9 suggestions) error: failed to run custom build command for `riot-sys v0.7.9` Caused by: process didn't exit successfully: `/Users/geovane/Developer/inria/dev/edhoc-rs-FORK/examples/coap-riot/bin/nrf52840dk/target/release/build/riot-sys-c24d931d26b1e923/build-script-build` (exit status: 101) --- stdout cargo:rerun-if-env-changed=BUILDING_RIOT_RS cargo:rerun-if-env-changed=RIOT_CC cargo:rerun-if-env-changed=RIOT_CFLAGS cargo:rerun-if-env-changed=RIOT_COMPILE_COMMANDS_JSON cargo:rerun-if-changed=/Users/geovane/Developer/inria/dev/edhoc-rs-FORK/examples/coap-riot/bin/nrf52840dk/cargo-compile-commands.json cargo:rerun-if-env-changed=RIOT_USEMODULE cargo:CC=clang ... (truncated) ... cargo:rerun-if-changed=/Users/geovane/Developer/inria/dev/RIOT-FORK/drivers/include/led.h cargo:rerun-if-changed=/Users/geovane/Developer/inria/dev/RIOT-FORK/boards/nrf52840dk/include/board.h --- stderr thread 'main' panicked at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.69/src/fallback.rs:817:9: "APSR_Type_struct_(unnamed_at_/Users/geovane/Developer/inria/dev/RIOT-FORK/build/pkg/cmsis/CMSIS/Core/Include/core_cm4_h_266_3)" is not a valid Ident stack backtrace: 0: rust_begin_unwind at /rustc/a2f5f9691b6ce64c1703feaf9363710dfd7a56cf/library/std/src/panicking.rs:645:5 1: core::panicking::panic_fmt at /rustc/a2f5f9691b6ce64c1703feaf9363710dfd7a56cf/library/core/src/panicking.rs:72:14 2: proc_macro2::fallback::validate_ident at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.69/src/fallback.rs:817:9 3: proc_macro2::fallback::Ident::_new at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.69/src/fallback.rs:759:9 4: proc_macro2::fallback::Ident::new at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.69/src/fallback.rs:769:9 5: proc_macro2::imp::Ident::new at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.69/src/wrapper.rs:645:50 6: proc_macro2::Ident::new at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proc-macro2-1.0.69/src/lib.rs:953:21 7: bindgen::ir::context::BindgenContext::rust_ident_raw at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/ir/context.rs:861:9 8: bindgen::ir::context::BindgenContext::rust_ident at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/ir/context.rs:853:9 9: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:1786:31 10: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:814:39 11: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:500:17 12: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:2115:13 13: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:814:39 14: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:500:17 15: ::codegen::{{closure}} at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:523:21 16: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:553:13 17: ::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:491:17 18: bindgen::codegen::codegen::{{closure}} at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:4364:9 19: bindgen::ir::context::BindgenContext::gen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/ir/context.rs:1173:19 20: bindgen::codegen::codegen at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/codegen/mod.rs:4328:5 21: bindgen::Bindings::generate at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/lib.rs:2438:32 22: bindgen::Builder::generate at /Users/geovane/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.60.1/src/lib.rs:1502:9 23: build_script_build::main at ./build.rs:178:20 24: core::ops::function::FnOnce::call_once at /rustc/a2f5f9691b6ce64c1703feaf9363710dfd7a56cf/library/core/src/ops/function.rs:250:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. make: *** [/Users/geovane/Developer/inria/dev/RIOT-FORK/makefiles/cargo-targets.inc.mk:53: /Users/geovane/Developer/inria/dev/edhoc-rs-FORK/examples/coap-riot/bin/nrf52840dk/target/thumbv7em-none-eabihf/release/libedhoc_coap_riot.a] Error 101 ```

Note that previously I had issues with proc-macro2 in nightly, which are solved in version 1.0.60+. I solved it in the cryptocell310 crate by updating bindgen to 0.69.1.

Version of rustc is rustc 1.75.0-nightly (a2f5f9691 2023-11-02).

chrysn commented 8 months ago

Updating proc-macro2 is an unfortunate necessity when using nightly -- for reasons I haven't looked up yet, that crate insists on using nightly features when running on nightly, consequently breaking every other time you're running with an old nightly and new crate or vice versa. A cargo update usually does the trick. RIOT should build fine now on stable, though, so maybe this can all be built on stable anyway (and thus race less trouble with bindgen).

BTW, the client version of this is a bit backlogged because I'm running into a weird lifetime issue for setting payload, I may need to add a less powerful API there initially.

geonnave commented 8 months ago

Thanks for the update. I still get the same result after cargo update. Regarding RIOT on stable, it did work during the hackathon, but right now it doesn't because coap-request seems to require nightly (error: package coap-request v0.1.0 cannot be built because it requires rustc 1.75 or newer, while the currently active rustc version is 1.71.1).

chrysn commented 8 months ago

Hm, I've removed the obstacles for using stable (pushed to an own branch as I'm not a project member and can thus not push onto yours), and the problem persists; digging.

geonnave commented 8 months ago

Thanks for the update. Added you as collaborator in my fork.

chrysn commented 8 months ago

OK, sent a bunch of updates.

make all term now works (building on native). For local testing with nrf52840dongle and similar I recommend adding USEMODULE += usbus_cdc_ecm and removing USEMODULE += netdev_default (so this can all run on USB Ethernet rather than needing a 6LBR).

Now this would be testable if only let url = "coap://[fe80::3c63:beff:fe85:ca96%tapbr0]:5683/.well-known/edhoc"; (if you need the IP, find which tap device or bridge your RIOT is running on, and ping ff02::1%tapbr0 to see the other device on the network) worked with the CoAPClient of the coap crate ... about time I get the CoAP client side on my side working. (Or to update the aiocoap EDHOC implementation).

chrysn commented 8 months ago

We'll yet have to find a way to deal with cargo fmt. As the last fix clones RIOT, cargo-fmt would even complain about formatting errors there -- but it will also complain if it can't find the dependency at all.

geonnave commented 8 months ago

A nasty fix would be to remove rust_riotmodules just before the cargo fmt --check step:

-      run: cargo fmt --check
+      run: |
+        sed -i -E "s/rust_riotmodules.*//g" examples/coap-riot/Cargo.toml # avoiding cargo `fmt issues` in coap rust example
+        cargo fmt --check
chrysn commented 8 months ago

Now that https://github.com/RIOT-OS/rust-riot-wrappers/pull/67 is merged, the special branch is not there any more, but the default branch should do nicely.

Ad fmt: It's nasty indeed, but for the moment I guess it'll work. Will you do the next round of changes? (I think this is otherwise quite ready -- at least with the software implementation, PSA would be next).