Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

native: add option to turn RIOT into a Linux CLI tool #20656

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented May 7, 2024

Contribution description

To better integrate RIOT into some automated tests, it can be useful to run commands in a one-shot fashion.
This allows to use RIOT shell commands like Linux applications and integrate them into other tooling so e.g. Linux servers that interact with RIOT can be tested using a fixed RIOT binary.

Testing procedure

First create a TAP bridge with an (optional) uplink:

sudo dist/tools/tapsetup/tapsetup -u enxf4a80d90ba50

We can ping a host from RIOT

bin/native/nanocoap_tool.elf tap0 ping riot-os.org
12 bytes from 2a01:4f9:1a:9508::1: icmp_seq=0 ttl=57 time=58.149 ms
12 bytes from 2a01:4f9:1a:9508::1: icmp_seq=1 ttl=57 time=58.647 ms
12 bytes from 2a01:4f9:1a:9508::1: icmp_seq=2 ttl=57 time=58.381 ms

--- riot-os.org PING statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 58.149/58.392/58.647 ms

We can also access CoAP servers

bin/native/nanocoap_tool.elf tap0 ncget coap://coap.me/
/test
/validate
/hello
/blåbærsyltetøy
/sink
/separate
/large
/secret
/broken
/weird33
/weird44
/weird55
/weird333
/weird3333
/weird33333
/123412341234123412341234
/location-query
/create1
/large-update
/large-create
/query
/seg1
/path
/location1
/multi-format
/3
/4
/5

Issues/PRs references

@benpicco benpicco requested a review from kaspar030 as a code owner May 7, 2024 17:45
@github-actions github-actions bot added Platform: native Platform: This PR/issue effects the native platform Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: boards Area: Board ports Area: cpu Area: CPU/MCU ports Area: sys Area: System Area: examples Area: Example Applications labels May 7, 2024
@benpicco benpicco changed the title Native tool native: allow to run RIOT shell commands like native Linux commands May 7, 2024
@benpicco benpicco changed the title native: allow to run RIOT shell commands like native Linux commands native: add option to turn RIOT into a Linux CLI tool May 7, 2024
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label May 7, 2024
@riot-ci
Copy link

riot-ci commented May 7, 2024

Murdock results

FAILED

c0fd8ed examples/nanocoap_tool: add RIOT as a CLI tool

Success Failures Total Runtime
9568 0 10145 10m:56s

Artifacts

@Teufelchen1
Copy link
Contributor

Cool idea! How about making it behave like busybox? ./ncget == ./nanocoap_tool.elf ncget?

I dislike that there is a new example that does not really have value as it - add the very least add a README! I am also surprised it is nanocoap specific? While coap is a good example use case, the example itself is more "riot as cli tool" and should be presented as such.

@benpicco
Copy link
Contributor Author

benpicco commented May 8, 2024

I agree that the example needs to be more fleshed out, this is a bit specific to the 'send arbitrary CoAP payloads as a RIOT node would' use case, but not sure what else I should include.

@github-actions github-actions bot added Platform: MSP Platform: This PR/issue effects MSP-based platforms Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: network Area: Networking Area: doc Area: Documentation Area: tests Area: tests and testing framework labels May 15, 2024
@github-actions github-actions bot added Platform: ESP Platform: This PR/issue effects ESP-based platforms Area: CoAP Area: Constrained Application Protocol implementations Area: Kconfig Area: Kconfig integration labels May 15, 2024
@github-actions github-actions bot removed Platform: MSP Platform: This PR/issue effects MSP-based platforms Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: network Area: Networking Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: build system Area: Build system Area: pkg Area: External package ports Area: drivers Area: Device drivers Platform: ESP Platform: This PR/issue effects ESP-based platforms Area: CoAP Area: Constrained Application Protocol implementations Area: Kconfig Area: Kconfig integration labels May 15, 2024
@Teufelchen1
Copy link
Contributor

Is this ready for a review @benpicco ?

@benpicco
Copy link
Contributor Author

Sure!

Copy link
Contributor

@Teufelchen1 Teufelchen1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Only minor comments and nits.

examples/nanocoap_tool/Makefile Outdated Show resolved Hide resolved
USEMODULE += shell
USEMODULE += shell_cmds_default

# export the whole fs to RIOT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Wouldn't be "./" be sufficient for an example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paths in RIOT are always absolute, we don't have the concept of a current directory.
So to access main.c in the directory of the example we'd have to write /main.c which does not match the host file system.

examples/nanocoap_tool/main.c Outdated Show resolved Hide resolved
}

/* wait some time for the network to be ready */
ztimer_sleep(ZTIMER_MSEC, 1000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥲

@@ -169,19 +169,34 @@ static int _nanocoap_put_handler(int argc, char **argv)
file = argv[1];
url = argv[2];

/* append filename to remote dir */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? (I also find the difference between the comments wording "filename" & "remote dir" confusing, given that the variables are called "file" and "url", but that's very much nit picking.)

Copy link
Contributor Author

@benpicco benpicco Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was there before, so when you write

ncput /path/to/foo.txt coap://example.com/in/

it will actually make a PUT request to coap://example.com/in/foo.txt

Does changing it to 'remote URL' make it clearer?

puts("Constructed URI too long");
return -ENOBUFS;
}
url = buffer;
}

/* relative file path */
if (*file != '/') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would make it more clear that inspecting the first character only is deliberate. (Again, nit)

Suggested change
if (*file != '/') {
if (file[0] != '/') {

Copy link
Contributor

@Teufelchen1 Teufelchen1 Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this if is also take if file == "-". I think that is okay and can be ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added as a convenience. We already have CONFIG_NCGET_DEFAULT_DATA_DIR to store downloads from ncget when the destination is not specified - so you can do

ncget coap://example.com/files/data.txt

and it will end up in CONFIG_NCGET_DEFAULT_DATA_DIR.
This is just the reverse: It allows you do to

ncput foo.txt coap://example.com/in/

without typing out the whole path.

sys/shell/cmds/nanocoap_vfs.c Outdated Show resolved Hide resolved
sys/shell/cmds/nanocoap_vfs.c Outdated Show resolved Hide resolved
sys/shell/shell.c Show resolved Hide resolved
@miri64
Copy link
Member

miri64 commented Jul 18, 2024

A README for the example would be nice. It took me a while to wrap my head around the concept.

@github-actions github-actions bot added the Area: doc Area: Documentation label Aug 19, 2024
@benpicco
Copy link
Contributor Author

I added a readme and some wrapper scripts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: boards Area: Board ports Area: core Area: RIOT kernel. Handle PRs marked with this with care! Area: cpu Area: CPU/MCU ports Area: doc Area: Documentation Area: examples Area: Example Applications Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: native Platform: This PR/issue effects the native platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants