Skip to content

Commit

Permalink
examples: Add second Rust example
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Aug 27, 2021
1 parent 0d2b3ff commit 281f109
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/rust-gcoap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "rust-gcoap"
version = "0.1.0"
authors = ["Christian Amsüss <[email protected]>"]
edition = "2018"
resolver = "2"

[lib]
crate-type = ["staticlib"]

[dependencies]
riot-wrappers = { version = "^0.7", features = [ "with_coap_message", "with_coap_handler" ] }

coap-message-demos = { git = "https://gitlab.com/chrysn/coap-message-demos/", default-features = false, features = [ "with-log" ] }
coap-handler-implementations = "0.1"
riot-coap-handler-demos = { git = "https://gitlab.com/etonomy/riot-module-examples" }
29 changes: 29 additions & 0 deletions examples/rust-gcoap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# name of your application
APPLICATION = rust_gcoap

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

# Basic networking, and gcoap
USEMODULE += gcoap
USEMODULE += gnrc_netdev_default
USEMODULE += auto_init_gnrc_netif
USEMODULE += gnrc_ipv6_default
USEMODULE += gnrc_icmpv6_echo

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# The name of crate (as per Cargo.toml package name, but with '-' replaced with '_')
APPLICATION_RUST_MODULE = rust_gcoap
BASELIBS += $(APPLICATION_RUST_MODULE).module

include $(RIOTBASE)/Makefile.include
34 changes: 34 additions & 0 deletions examples/rust-gcoap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2020 Christian Amsüss
//
// This file is subject to the terms and conditions of the GNU Lesser
// General Public License v2.1. See the file LICENSE in the top level
// directory for more details.
#![no_std]

use riot_wrappers::riot_main;
use riot_wrappers::{gcoap, thread, cstr::cstr};

use coap_handler_implementations::HandlerBuilder;

riot_main!(main);

fn main() {

let handler = coap_message_demos::full_application_tree(None)
.at(&["ps", ""], riot_coap_handler_demos::ps::ps())
.at(&["netif", ""], riot_coap_handler_demos::netif::netif())
.at(&["stdio", "write"], riot_coap_handler_demos::stdio::write())
.at(&["stdio", "read"], riot_coap_handler_demos::stdio::read())
;
let mut handler = riot_wrappers::coap_handler::GcoapHandler(handler);

let mut listener = gcoap::SingleHandlerListener::new_catch_all(&mut handler);

gcoap::scope(|greg| {
greg.register(&mut listener);

// Sending main thread to sleep; can't return or the Gcoap handler would need to be
// deregistered (which it can't).
loop { thread::sleep(); }
})
}

0 comments on commit 281f109

Please sign in to comment.