Skip to content

Commit

Permalink
Fix TimeSlot implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
7h0ma5 committed Jan 9, 2017
1 parent f3740d9 commit 335fb6f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/pocsag/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ impl SchedulerCore {
}
}

let message = message.unwrap();
let next_slot = self.slots.next_allowed();

if let Some(next_slot) = next_slot {
if self.slots.is_current_allowed() { /* transmit immediately */ }
else if let Some(next_slot) = self.slots.next_allowed() {
let mut duration = next_slot.duration_until();

info!("Waiting {} seconds until {:?}...",
Expand Down Expand Up @@ -111,7 +109,7 @@ impl SchedulerCore {
}

info!("Transmitting...");
transmitter.send(Generator::new(self, message));
transmitter.send(Generator::new(self, message.unwrap()));
info!("Transmission completed.");
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/pocsag/timeslots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,42 @@ impl TimeSlot {
pub fn duration_until(&self) -> Duration {
let now = unix_time();
let now_decis = deciseconds(now);
let current_slot = (now_decis >> 6) & 0b1111;

let slot_offset = (self.index() as u64) << 6;
let current_slot = (now_decis >> 6) & 0b1111;
let this_slot = self.index() as u64;
let mut block_start = now_decis & !0b1111111111;

// if the slot is already over use the next block
if (slot_offset + 1) < current_slot {
if this_slot == current_slot {
return Duration::new(0, 0)
}
else if this_slot < current_slot {
block_start += 1 << 10;
}

let slot_offset = this_slot << 6;
let slot_start = block_start + slot_offset;

let seconds = slot_start/10;
let nanoseconds = (slot_start % 10) as u32 * 100_000_000;

let start = Duration::new(seconds, nanoseconds);

start.checked_sub(now).expect("TimeSlot calculation broken")
match start.checked_sub(now) {
Some(duration) => duration,
None => {
error!("TimeSlot calculation broken");
error!("Current Slot: {:X} This Slot: {:X}", current_slot, this_slot);
error!("Now: {:?}", now);
error!("Start: {:?}", start);
if !self.active() {
Duration::new(1, 0)
}
else {
Duration::new(0, 0)
}
}
}
}
}

Expand Down

0 comments on commit 335fb6f

Please sign in to comment.