Add time synchronisation with NTP (#579)

* Add api::time::from_timestamp_utc function

* Add date function to lisp

* Add file/exists? function to lisp

* Rewrite lisp ntp client

* Update NTP packet to work with more servers

* Fix userspace binaries build
This commit is contained in:
Vincent Ollivier 2024-02-18 21:41:03 +01:00 committed by GitHub
parent 5e92dcb2d0
commit 699ddf0ad8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 52 additions and 20 deletions

View File

@ -37,7 +37,8 @@ user-rust:
cargo rustc --no-default-features --features userspace --release --bin {}
basename -s .rs src/bin/*.rs | xargs -I {} \
cp target/x86_64-moros/release/{} dsk/bin/{}
strip dsk/bin/*
basename -s .rs src/bin/*.rs | xargs -I {} \
strip dsk/bin/{}
bin = target/x86_64-moros/$(mode)/bootimage-moros.bin
img = disk.img

21
dsk/bin/ntp Normal file
View File

@ -0,0 +1,21 @@
#!lisp
(load "/lib/lisp/core.lsp")
(var config "/ini/ntp")
(var default-server "time.cloudflare.com")
(var server (if (not (nil? args))
(first args)
(if (file/exists? config) (str/trim (read config)) default-server)))
(var addr (or (host server) server))
(var port 123)
(var socket (socket/connect "udp" addr port))
(var req (map (fun (i) (if (eq? i 0) 0x23 0)) (range 0 48)))
(file/write socket req)
(var res (file/read socket 48))
(var buf (slice res 40 4))
(var time (- (bin->num (concat '(0 0 0 0) buf) "int") 2208988800))
(print (date time))

View File

@ -1,13 +0,0 @@
(load "/lib/lisp/core.lsp")
(var addr (or (host (head args)) (head args)))
(var port 123)
(var socket (socket/connect "udp" addr port))
(var req (map (fun (i) (if (eq? i 0) 0x33 0)) (range 0 48)))
(file/write socket req)
(var res (file/read socket 48))
(var buf (slice res 40 4))
(var time (- (bin->num (concat '(0 0 0 0) buf) "int") 2208988800))
(print time)

View File

@ -16,7 +16,11 @@ pub fn now_utc() -> OffsetDateTime {
}
pub fn from_timestamp(ts: i64) -> OffsetDateTime {
OffsetDateTime::from_unix_timestamp(ts).to_offset(offset())
from_timestamp_utc(ts).to_offset(offset())
}
pub fn from_timestamp_utc(ts: i64) -> OffsetDateTime {
OffsetDateTime::from_unix_timestamp(ts)
}
fn offset() -> UtcOffset {

View File

@ -24,6 +24,7 @@ pub fn copy_files(verbose: bool) {
//copy_file("/bin/exec", include_bytes!("../../dsk/bin/exec"), verbose);
copy_file("/bin/halt", include_bytes!("../../dsk/bin/halt"), verbose);
//copy_file("/bin/hello", include_bytes!("../../dsk/bin/hello"), verbose);
copy_file("/bin/ntp", include_bytes!("../../dsk/bin/ntp"), verbose);
copy_file("/bin/print", include_bytes!("../../dsk/bin/print"), verbose);
copy_file(
"/bin/reboot",
@ -168,11 +169,6 @@ pub fn copy_files(verbose: bool) {
include_bytes!("../../dsk/tmp/lisp/geotime.lsp"),
verbose,
);
copy_file(
"/tmp/lisp/ntp.lsp",
include_bytes!("../../dsk/tmp/lisp/ntp.lsp"),
verbose,
);
copy_file(
"/tmp/lisp/pi.lsp",
include_bytes!("../../dsk/tmp/lisp/pi.lsp"),

View File

@ -199,6 +199,10 @@ pub fn default_env() -> Rc<RefCell<Env>> {
"file/size".to_string(),
Exp::Primitive(primitive::lisp_file_size),
);
data.insert(
"file/exists?".to_string(),
Exp::Primitive(primitive::lisp_file_exists),
);
data.insert(
"file/open".to_string(),
Exp::Primitive(primitive::lisp_file_open),
@ -243,6 +247,10 @@ pub fn default_env() -> Rc<RefCell<Env>> {
"put".to_string(),
Exp::Primitive(primitive::lisp_put),
);
data.insert(
"date".to_string(),
Exp::Primitive(primitive::lisp_date),
);
// Setup autocompletion
*FUNCTIONS.lock() = data.keys().cloned().

View File

@ -3,6 +3,7 @@ use super::{bytes, numbers, strings};
use super::{float, number, string};
use super::{Err, Exp, Number};
use crate::api;
use crate::api::regex::Regex;
use crate::api::syscall;
use crate::sys::fs::OpenFlag;
@ -468,6 +469,12 @@ pub fn lisp_file_size(args: &[Exp]) -> Result<Exp, Err> {
}
}
pub fn lisp_file_exists(args: &[Exp]) -> Result<Exp, Err> {
ensure_length_eq!(args, 1);
let path = string(&args[0])?;
Ok(Exp::Bool(syscall::info(&path).is_some()))
}
pub fn lisp_file_open(args: &[Exp]) -> Result<Exp, Err> {
ensure_length_eq!(args, 2);
let path = string(&args[0])?;
@ -654,3 +661,11 @@ pub fn lisp_host(args: &[Exp]) -> Result<Exp, Err> {
Err(_) => Ok(Exp::List(vec![])),
}
}
pub fn lisp_date(args: &[Exp]) -> Result<Exp, Err> {
ensure_length_eq!(args, 1);
let ts = usize::try_from(number(&args[0])?)? as i64;
let fmt = api::clock::DATE_TIME;
let date = api::time::from_timestamp_utc(ts).format(fmt);
Ok(Exp::Str(date))
}