Skip to main content

Module clock

Module clock 

Source
Expand description

Application layer clock synchronization, TS003-2.0.0.

A device that has no GPS and no real-time clock still needs to know the time: a Class B beacon, a multicast window and a scheduled reboot all depend on it. This package is how it finds out. The device sends ClockCommand::AppTimeReq with the time it believes it is, the server compares that against when the uplink actually arrived, and answers with ClockCommand::AppTimeAns carrying the difference in seconds. Most of the time there is nothing to say and the server stays quiet.

The exchange is guarded by a four-bit token: the device raises it every time it applies a correction, and an answer whose token does not match the request is ignored, so a late answer cannot pull a corrected clock back.

ClockSync holds that state for a device: it builds each request, reads what comes back, and says what the device owes its server in return.

§Examples

A device asks for the time and applies the correction it gets back:

use pamoja_lorawan::packages::clock::{ClockCommand, ClockSync};
use pamoja_lorawan::Direction;

let mut sync = ClockSync::new();
let mut frame = [0u8; 8];
let len = sync.app_time_req(1_000_000, false, &mut frame)?;

// The server saw the uplink 12 seconds later than the device thinks it is.
let answer = ClockCommand::AppTimeAns { time_correction: 12, token: 0 };
let mut down = [0u8; 8];
let len = answer.encode(&mut down)?;

let read = sync.heard(&down[..len])?;
assert_eq!(read.correction, Some(12));
assert_eq!(sync.token(), 1, "the token moves on, so a repeat is ignored");

Structs§

ClockCommands
Walks the commands packed into one clock synchronization message.
ClockSync
The clock synchronization package running on a device, TS003-2.0.0.
Heard
What a downlink on the clock port asked of the device.

Enums§

ClockCommand
One command of the clock synchronization package.

Constants§

MAX_COMMAND
The longest clock synchronization command, in bytes.
PACKAGE
The identifier of this package, section 3.
PORT
The port the clock synchronization package is spoken on, section 3.
VERSION
The version of this package implemented here, section 3.