Skip to content

Commit

Permalink
Add time synchronisation with NTP (#579)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vinc authored Feb 18, 2024
1 parent 5e92dcb commit 699ddf0
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions dsk/bin/ntp
Original file line number Diff line number Diff line change
@@ -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))
13 changes: 0 additions & 13 deletions dsk/tmp/lisp/ntp.lsp

This file was deleted.

6 changes: 5 additions & 1 deletion src/api/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 1 addition & 5 deletions src/usr/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"),
Expand Down
8 changes: 8 additions & 0 deletions src/usr/lisp/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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().
Expand Down
15 changes: 15 additions & 0 deletions src/usr/lisp/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])?;
Expand Down Expand Up @@ -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))
}

0 comments on commit 699ddf0

Please sign in to comment.