Skip to content

Commit

Permalink
Merge pull request #32 from vpetrigo/sntpc-30
Browse files Browse the repository at this point in the history
Add `no_std` networking primitives for end users
  • Loading branch information
vpetrigo authored Oct 26, 2024
2 parents 6f2a11a + e8716d3 commit 2711f17
Show file tree
Hide file tree
Showing 24 changed files with 607 additions and 228 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[profile.no-std]
inherits = "dev"
panic = "abort"
22 changes: 11 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@ jobs:
clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run clippy with std
run: cargo clippy --no-default-features --features="std log utils" -- -D clippy::all -D clippy::pedantic
- uses: actions/checkout@v4
- name: Run clippy with all features
run: cargo clippy --workspace --exclude example-simple-no-std --all-features -- -D clippy::all -D clippy::pedantic
- name: Run clippy with no_std
run: cargo clippy --no-default-features --features="log" -- -D clippy::all -D clippy::pedantic
- name: Run clippy for tokio feature
run: cargo clippy --features="tokio" -- -D clippy::all -D clippy::pedantic
run: cargo clippy -p example-simple-no-std --no-default-features --profile no-std -- -D clippy::all -D clippy::pedantic
clippy_async_no_std:
runs-on: ubuntu-latest
container:
image: rust:latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install nightly toolchain
run: rustup toolchain add nightly
- name: Install clippy
Expand All @@ -34,17 +32,19 @@ jobs:
check_format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Check formatting
run: cargo fmt --check
build_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Build with std
run: cargo build --all --no-default-features --features="std utils"
run: cargo build --workspace --exclude example-simple-no-std --all-features
- name: Build with no_std
run: cargo build --all --no-default-features
run: |
cargo build -p sntpc --no-default-features
cargo build -p example-simple-no-std --profile no-std
- name: Run tests with std
run: cargo test
- name: Run tests with no_std
Expand Down
64 changes: 4 additions & 60 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,60 +1,4 @@
[package]
name = "sntpc"
version = "0.3.9"
description = "Library for making SNTP requests"
homepage = "https://github.com/vpetrigo/sntpc"
repository = "https://github.com/vpetrigo/sntpc"
documentation = "https://docs.rs/sntpc"
readme = "README.md"
categories = ["date-and-time", "no-std", "embedded", "asynchronous"]
keywords = ["sntp", "ntp", "sntp-client", "ntp-client"]
license = "BSD-3-Clause"
authors = ["Vladimir Petrigo <[email protected]>"]
edition = "2018"
autoexamples = false

exclude = [
".github/*",
"CONTRIBUTING.md",
".*",
]

[features]
default = ["std"]
std = []
utils = ["std", "chrono/clock"]
async = []
async_tokio = ["std", "async", "tokio", "async-trait"]

[dependencies]
log = { version = "~0.4", optional = true }
chrono = { version = "~0.4", default-features = false, optional = true }
# requred till this https://github.com/rust-lang/rfcs/pull/2832 is not addressed
no-std-net = "~0.6"

async-trait = { version = "0.1", optional = true }
tokio = { version = "1", features = ["full"], optional = true }

[dev-dependencies]
simple_logger = { version = "~1.13" }
smoltcp = { version = "~0.9", default-features = false, features = ["phy-tuntap_interface", "socket-udp", "proto-ipv4"] }
clap = { version = "2.33", default-features = false }

[badges]
maintenance = { status = "actively-developed" }

[[example]]
name = "simple_request"
required-features = ["std"]

[[example]]
name = "timesync"
required-features = ["utils"]

[[example]]
name = "smoltcp_request"
required-features = ["std"]

[[example]]
name = "tokio"
required-features = ["async_tokio"]
[workspace]
members = ["sntpc", "examples/*"]
default-members = ["sntpc"]
resolver = "2"
79 changes: 38 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,49 @@ Supported SNTP protocol versions:

-----------------

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

### Installation

----------------

This crate works with Cargo and is on
[crates.io](https://crates.io/crates/sntpc). Add it to your `Cargo.toml`
like so:

```toml
[dependencies]
sntpc = "0.3.9"
```

By calling the `get_time()` method and providing a proper NTP pool or server you
should get a valid synchronization timestamp:
### Usage example

```rust
use std::net::UdpSocket;
use std::thread;
use std::time::Duration;

#[allow(dead_code)]
const POOL_NTP_ADDR: &str = "pool.ntp.org:123";
#[allow(dead_code)]
const GOOGLE_NTP_ADDR: &str = "time.google.com:123";

fn main() {
let socket =
UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
socket
.set_read_timeout(Some(Duration::from_secs(2)))
.expect("Unable to set UDP socket read timeout");
let result = sntpc::simple_get_time("time.google.com:123", &socket);
match result {
Ok(time) => {
println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
}
Err(err) => println!("Err: {:?}", err),
for _ in 0..5 {
let socket =
UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
socket
.set_read_timeout(Some(Duration::from_secs(2)))
.expect("Unable to set UDP socket read timeout");

let result = sntpc::simple_get_time(POOL_NTP_ADDR, &socket);

match result {
Ok(time) => {
assert_ne!(time.sec(), 0);
let seconds = time.sec();
let microseconds =
u64::from(time.sec_fraction()) * 1_000_000 / u64::from(u32::MAX);
println!("Got time: {seconds}.{microseconds}");
}
Err(err) => println!("Err: {err:?}"),
}

thread::sleep(Duration::new(15, 0));
}
}
```

You can find this [example](examples/simple-request) as well as other example projects in the
[example directory](examples).

## `no_std` support

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

# Examples

----------

You can find several examples that shows how to use the library in details under [examples/] folder.
Currently, there are examples that show:
- usage of SNTP library in `std` environment
- usage of SNTP library with [`smoltcp`][smoltcp] TCP/IP stack. Some `std` dependencies
required only due to smoltcp available interfaces

[smoltcp]: https://github.com/smoltcp-rs/smoltcp

# Contribution

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

---------

This project is licensed under:
<sup>
This project is licensed under <a href="LICENSE.md">The 3-Clause BSD License</a>
</sup>

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

<sup>
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in time by you, as
defined in the 3-Clause BSD license, shall be licensed as above, without any additional terms or
conditions.
</sup>
9 changes: 9 additions & 0 deletions examples/simple-no-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "example-simple-no-std"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
sntpc = { path = "../../sntpc", default-features = false, features = ["async"] }
yash-executor = { version = "1.0.0", default-features = false }
7 changes: 7 additions & 0 deletions examples/simple-no-std/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
if cfg!(target_os = "linux") {
println!("cargo::rustc-link-arg=-nostartfiles");
println!("cargo::rustc-link-arg=-lc");
}
println!("cargo::rerun-if-changed=build.rs");
}
Loading

0 comments on commit 2711f17

Please sign in to comment.