pub struct ClockSync { /* private fields */ }Expand description
The clock synchronization package running on a device, TS003-2.0.0.
It keeps the token that pairs an answer with its request, the period the server set, and whatever answer the device owes. It holds no clock: the caller passes the device’s own time into each request and applies the correction that comes back.
Implementations§
Source§impl ClockSync
impl ClockSync
Sourcepub const DEFAULT_PERIOD: u8 = 0
pub const DEFAULT_PERIOD: u8 = 0
The period a device asks at until its server says otherwise, section 3.3.
Sourcepub const fn new() -> ClockSync
pub const fn new() -> ClockSync
Starts the package on a device, with the token at zero.
§Returns
The package, with nothing outstanding.
Sourcepub const fn self_managed() -> ClockSync
pub const fn self_managed() -> ClockSync
Starts the package on a device that manages its own periodicity.
Such a device answers a server that tries to set a period with the not-supported bit, section 3.3.
§Returns
The package.
Sourcepub const fn token(&self) -> u8
pub const fn token(&self) -> u8
The token the next request will carry.
§Returns
A value 0 to 15, which rises every time a correction is applied.
Sourcepub const fn period_s(&self) -> u32
pub const fn period_s(&self) -> u32
How often the device should ask, as the server last set it.
§Returns
The seconds between requests, before the random spread of thirty seconds either way that section 3.3 asks for.
Sourcepub fn app_time_req(
&mut self,
device_time: u32,
ans_required: bool,
out: &mut [u8],
) -> Result<usize, LorawanError>
pub fn app_time_req( &mut self, device_time: u32, ans_required: bool, out: &mut [u8], ) -> Result<usize, LorawanError>
Builds the request that asks for a correction, section 3.2.
§Arguments
device_time- what the device believes the time is, in seconds since the GPS epoch, read immediately before the frame goes out.ans_required- whether the server must answer even if the clock is right.out- where to write the command.
§Returns
How many bytes were written.
§Errors
LorawanError::PayloadTooLong when the buffer is too small.
Sourcepub fn heard(&mut self, payload: &[u8]) -> Result<Heard, LorawanError>
pub fn heard(&mut self, payload: &[u8]) -> Result<Heard, LorawanError>
Reads a downlink on the clock port and acts on it.
A correction whose token does not match the outstanding request is ignored, as section 3.2 asks, and so is any answer with no request waiting on it.
§Arguments
payload- what arrived on portPORT.
§Returns
The correction to apply, whether more of one is coming, and what the device now owes.
§Errors
The first error a command in the message raised.
Sourcepub fn answer(
&mut self,
device_time: u32,
out: &mut [u8],
) -> Result<usize, LorawanError>
pub fn answer( &mut self, device_time: u32, out: &mut [u8], ) -> Result<usize, LorawanError>
Writes the answer the device owes, if it owes one.
§Arguments
device_time- what the device believes the time is, for the answer that reports it.out- where to write the command.
§Returns
How many bytes were written, and zero when nothing is owed.
§Errors
LorawanError::PayloadTooLong when the buffer is too small, leaving the answer
owed.