Skip to content

Commit 2711f17

Browse files
authored
Merge pull request #32 from vpetrigo/sntpc-30
Add `no_std` networking primitives for end users
2 parents 6f2a11a + e8716d3 commit 2711f17

File tree

24 files changed

+607
-228
lines changed

24 files changed

+607
-228
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[profile.no-std]
2+
inherits = "dev"
3+
panic = "abort"

.github/workflows/ci.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ jobs:
1212
clippy:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@v3
16-
- name: Run clippy with std
17-
run: cargo clippy --no-default-features --features="std log utils" -- -D clippy::all -D clippy::pedantic
15+
- uses: actions/checkout@v4
16+
- name: Run clippy with all features
17+
run: cargo clippy --workspace --exclude example-simple-no-std --all-features -- -D clippy::all -D clippy::pedantic
1818
- name: Run clippy with no_std
19-
run: cargo clippy --no-default-features --features="log" -- -D clippy::all -D clippy::pedantic
20-
- name: Run clippy for tokio feature
21-
run: cargo clippy --features="tokio" -- -D clippy::all -D clippy::pedantic
19+
run: cargo clippy -p example-simple-no-std --no-default-features --profile no-std -- -D clippy::all -D clippy::pedantic
2220
clippy_async_no_std:
2321
runs-on: ubuntu-latest
2422
container:
2523
image: rust:latest
2624
steps:
27-
- uses: actions/checkout@v3
25+
- uses: actions/checkout@v4
2826
- name: Install nightly toolchain
2927
run: rustup toolchain add nightly
3028
- name: Install clippy
@@ -34,17 +32,19 @@ jobs:
3432
check_format:
3533
runs-on: ubuntu-latest
3634
steps:
37-
- uses: actions/checkout@v3
35+
- uses: actions/checkout@v4
3836
- name: Check formatting
3937
run: cargo fmt --check
4038
build_test:
4139
runs-on: ubuntu-latest
4240
steps:
43-
- uses: actions/checkout@v3
41+
- uses: actions/checkout@v4
4442
- name: Build with std
45-
run: cargo build --all --no-default-features --features="std utils"
43+
run: cargo build --workspace --exclude example-simple-no-std --all-features
4644
- name: Build with no_std
47-
run: cargo build --all --no-default-features
45+
run: |
46+
cargo build -p sntpc --no-default-features
47+
cargo build -p example-simple-no-std --profile no-std
4848
- name: Run tests with std
4949
run: cargo test
5050
- name: Run tests with no_std

Cargo.toml

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,4 @@
1-
[package]
2-
name = "sntpc"
3-
version = "0.3.9"
4-
description = "Library for making SNTP requests"
5-
homepage = "https://github.com/vpetrigo/sntpc"
6-
repository = "https://github.com/vpetrigo/sntpc"
7-
documentation = "https://docs.rs/sntpc"
8-
readme = "README.md"
9-
categories = ["date-and-time", "no-std", "embedded", "asynchronous"]
10-
keywords = ["sntp", "ntp", "sntp-client", "ntp-client"]
11-
license = "BSD-3-Clause"
12-
authors = ["Vladimir Petrigo <[email protected]>"]
13-
edition = "2018"
14-
autoexamples = false
15-
16-
exclude = [
17-
".github/*",
18-
"CONTRIBUTING.md",
19-
".*",
20-
]
21-
22-
[features]
23-
default = ["std"]
24-
std = []
25-
utils = ["std", "chrono/clock"]
26-
async = []
27-
async_tokio = ["std", "async", "tokio", "async-trait"]
28-
29-
[dependencies]
30-
log = { version = "~0.4", optional = true }
31-
chrono = { version = "~0.4", default-features = false, optional = true }
32-
# requred till this https://github.com/rust-lang/rfcs/pull/2832 is not addressed
33-
no-std-net = "~0.6"
34-
35-
async-trait = { version = "0.1", optional = true }
36-
tokio = { version = "1", features = ["full"], optional = true }
37-
38-
[dev-dependencies]
39-
simple_logger = { version = "~1.13" }
40-
smoltcp = { version = "~0.9", default-features = false, features = ["phy-tuntap_interface", "socket-udp", "proto-ipv4"] }
41-
clap = { version = "2.33", default-features = false }
42-
43-
[badges]
44-
maintenance = { status = "actively-developed" }
45-
46-
[[example]]
47-
name = "simple_request"
48-
required-features = ["std"]
49-
50-
[[example]]
51-
name = "timesync"
52-
required-features = ["utils"]
53-
54-
[[example]]
55-
name = "smoltcp_request"
56-
required-features = ["std"]
57-
58-
[[example]]
59-
name = "tokio"
60-
required-features = ["async_tokio"]
1+
[workspace]
2+
members = ["sntpc", "examples/*"]
3+
default-members = ["sntpc"]
4+
resolver = "2"

README.md

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,49 @@ Supported SNTP protocol versions:
1616

1717
-----------------
1818

19-
https://docs.rs/sntpc
19+
More information about this crate can be found in the [crate documentation](https://docs.rs/sntpc)
2020

21-
### Installation
22-
23-
----------------
24-
25-
This crate works with Cargo and is on
26-
[crates.io](https://crates.io/crates/sntpc). Add it to your `Cargo.toml`
27-
like so:
28-
29-
```toml
30-
[dependencies]
31-
sntpc = "0.3.9"
32-
```
33-
34-
By calling the `get_time()` method and providing a proper NTP pool or server you
35-
should get a valid synchronization timestamp:
21+
### Usage example
3622

3723
```rust
3824
use std::net::UdpSocket;
25+
use std::thread;
3926
use std::time::Duration;
4027

28+
#[allow(dead_code)]
29+
const POOL_NTP_ADDR: &str = "pool.ntp.org:123";
30+
#[allow(dead_code)]
31+
const GOOGLE_NTP_ADDR: &str = "time.google.com:123";
32+
4133
fn main() {
42-
let socket =
43-
UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
44-
socket
45-
.set_read_timeout(Some(Duration::from_secs(2)))
46-
.expect("Unable to set UDP socket read timeout");
47-
let result = sntpc::simple_get_time("time.google.com:123", &socket);
48-
match result {
49-
Ok(time) => {
50-
println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
51-
}
52-
Err(err) => println!("Err: {:?}", err),
34+
for _ in 0..5 {
35+
let socket =
36+
UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
37+
socket
38+
.set_read_timeout(Some(Duration::from_secs(2)))
39+
.expect("Unable to set UDP socket read timeout");
40+
41+
let result = sntpc::simple_get_time(POOL_NTP_ADDR, &socket);
42+
43+
match result {
44+
Ok(time) => {
45+
assert_ne!(time.sec(), 0);
46+
let seconds = time.sec();
47+
let microseconds =
48+
u64::from(time.sec_fraction()) * 1_000_000 / u64::from(u32::MAX);
49+
println!("Got time: {seconds}.{microseconds}");
50+
}
51+
Err(err) => println!("Err: {err:?}"),
52+
}
53+
54+
thread::sleep(Duration::new(15, 0));
5355
}
5456
}
5557
```
5658

59+
You can find this [example](examples/simple-request) as well as other example projects in the
60+
[example directory](examples).
61+
5762
## `no_std` support
5863

5964
-------------------
@@ -72,18 +77,6 @@ There is an example: [`examples/tokio.rs`](examples/tokio.rs).
7277
There is also `no_std` support with feature `async`, but it requires Rust >= `1.75-nightly` version.
7378
The example can be found in [separate repository](https://github.com/vpikulik/sntpc_embassy).
7479

75-
# Examples
76-
77-
----------
78-
79-
You can find several examples that shows how to use the library in details under [examples/] folder.
80-
Currently, there are examples that show:
81-
- usage of SNTP library in `std` environment
82-
- usage of SNTP library with [`smoltcp`][smoltcp] TCP/IP stack. Some `std` dependencies
83-
required only due to smoltcp available interfaces
84-
85-
[smoltcp]: https://github.com/smoltcp-rs/smoltcp
86-
8780
# Contribution
8881

8982
--------------
@@ -108,10 +101,14 @@ Really appreciate all your efforts! Please [let me know](mailto:vladimir.petrigo
108101

109102
---------
110103

111-
This project is licensed under:
104+
<sup>
105+
This project is licensed under <a href="LICENSE.md">The 3-Clause BSD License</a>
106+
</sup>
112107

113-
- [The 3-Clause BSD License](LICENSE.md)
108+
<br/>
114109

110+
<sup>
115111
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in time by you, as
116112
defined in the 3-Clause BSD license, shall be licensed as above, without any additional terms or
117113
conditions.
114+
</sup>

examples/simple-no-std/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "example-simple-no-std"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
sntpc = { path = "../../sntpc", default-features = false, features = ["async"] }
9+
yash-executor = { version = "1.0.0", default-features = false }

examples/simple-no-std/build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
if cfg!(target_os = "linux") {
3+
println!("cargo::rustc-link-arg=-nostartfiles");
4+
println!("cargo::rustc-link-arg=-lc");
5+
}
6+
println!("cargo::rerun-if-changed=build.rs");
7+
}

0 commit comments

Comments
 (0)