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

feat: sntp #14

Closed
wants to merge 2 commits into from
Closed

feat: sntp #14

wants to merge 2 commits into from

Conversation

ForsakenHarmony
Copy link
Contributor

@ForsakenHarmony ForsakenHarmony commented Oct 26, 2021

Will get the time from a NTP server

let sntp = EspSntp::new(SntpConf::default())?;
sntp.wait_for_sync(Duration::from_secs(30));

After you can use the usual time APIs and get the real-world time (i.e. std::time::SystemTime::now())

Depends on #13

}

impl EspSntp {
pub fn new(conf: SntpConf) -> Result<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: perhaps take the conf by ref?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure

}
}

unsafe fn init(conf: SntpConf) -> Result<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto.

}
}

unsafe fn init(conf: SntpConf) -> Result<Self> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You probably want to reduce the unsafe not to cover the whole function?
I had a few like those that I reduced in the meantime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will do

@@ -0,0 +1,191 @@
use core::{time::Duration};

use anyhow::*;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm eradicating anyhow from the esp-idf-svc APIs, as it kinda does not belong in a library. Only remaining usage (I think) is in httpd, but that module is anyway going away in favor of http::server You should be using EspError. And yes, there's a complication with the LwIP errors that we need to handle and I'll comment in a separate review, in a separate pull request.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I probably just copied it from one of the things you did, sntp was the first thing I worked on

I'll replace it with EspError if possible

src/sntp.rs Show resolved Hide resolved
waiter.start();

let c_tz = CString::new("TZ")?;
let c_tz_val = CString::new("UTC")?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really want to hard-code the timezone as UTC? I might want to do EST? Also, what do timezones have to do with sntp (which - I hope - is just working with epoch millis (with or without leap seconds, but definitely not concerned with the local timezone))?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, probably don't want to hard-code this, I went off examples from espressif, I'll check again if it's necessary

@ivmarkov
Copy link
Collaborator

Since I did not know when (or even if) you would be resuming work on this PR, I've taken it and - with a few changes - merged it: d20b9bd

The changes in details:

  • The suggested changes as per my feedback
  • Additionally:
    • Make it work in no_std
    • Remove the waiter (even though we have generic Waitable now!). I just was not sure what is the benefit of that. If somebody really wants to wait until the first sync with the NTP server had happened, we can always restore/implement a similar functionality

Thanks for your effort!

@ivmarkov ivmarkov closed this Nov 13, 2021
@ForsakenHarmony
Copy link
Contributor Author

Remove the waiter (even though we have generic Waitable now!). I just was not sure what is the benefit of that. If somebody really wants to wait until the first sync with the NTP server had happened, we can always restore/implement a similar functionality

Why would you not want a way to wait for it?
For me, the purpose was to get a timestamp I can send to influxdb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants