#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Error {
NoSuchRecording,
+ StorageError,
}
enum SectorFlag {
let header_a = §or_header[a as usize];
let header_b = §or_header[b as usize];
- // Latest entries come first.
- if header_a.start_time > header_b.start_time {
+ if header_a.starts_recording() && header_b.starts_recording() {
+ // Latest entries come first.
+ if header_a.start_time > header_b.start_time {
+ -1
+ } else if header_a.start_time < header_b.start_time {
+ 1
+ } else {
+ 0
+ }
+ } else if header_a.starts_recording() {
+ -1
+ } else if header_b.starts_recording() {
+ 1
+ } else if a < b {
-1
- } else if header_a.start_time < header_b.start_time {
+ } else if a > b {
1
} else {
0
indices: [0; NUM_SECTORS]
};
- let mut num_used = 0;
-
- // Put the indices of the used directory entries at the beginning
- // of the array. Ignore the unused ones since we are not going
- // to sort them anyway.
for i in 0..NUM_SECTORS {
- let sector_header = &iter.sector_header[i];
-
- if sector_header.starts_recording() {
- iter.indices[num_used] = i as u16;
- num_used += 1;
- }
+ iter.indices[i] = i as u16;
}
- let num_elts_to_sort = num_used;
-
- if num_elts_to_sort != 0 {
- // Sort the used directory entries.
- iter.sort(num_elts_to_sort);
- }
-
- // Now put the indices of the unused directory entries in the array.
- if num_used == 0 {
- for i in 0..NUM_SECTORS {
- iter.indices[i] = i as u16;
- }
- } else {
- let latest_used = iter.indices[0] as usize;
- let mut offset_unused = num_used;
-
- // First put the entries that come after the latest one in use...
- for i in (latest_used + 1)..NUM_SECTORS {
- let sector_header = &iter.sector_header[i];
-
- if !sector_header.is_in_use() {
- iter.indices[offset_unused] = i as u16;
- offset_unused += 1;
- }
- }
-
- // ... then wrap around if necessary.
- for i in 0..latest_used {
- let sector_header = &iter.sector_header[i];
-
- if !sector_header.is_in_use() {
- iter.indices[offset_unused] = i as u16;
- offset_unused += 1;
- }
- }
- }
+ iter.sort(NUM_SECTORS);
// XXX:
// Need to handle those sectors that don't start recordings
self.prepare_write_buffer(true);
- self.write_packet(0, tap.latitude, tap.longitude);
+ self.write_packet(0, tap.latitude_deg, tap.longitude_deg);
self.recording_id
}
// the intervals to full seconds.
let d_time_s = (d_time_ms + 500) / 1000;
- let d_lat = tap.latitude - prev_tap.latitude;
- let d_lon = tap.longitude - prev_tap.longitude;
+ let d_lat = tap.latitude_deg - prev_tap.latitude_deg;
+ let d_lon = tap.longitude_deg - prev_tap.longitude_deg;
if self.write_packet(d_time_s, d_lat, d_lon) {
self.flush_in_flight(false);
continue;
}
+ let mut num_data_sectors = 0;
+
+ for d in 1..NUM_SECTORS {
+ let wrapped_index = ((index + d) & (NUM_SECTORS - 1)) as usize;
+ let other_sector_header = &self.sector_header[wrapped_index];
+
+ if other_sector_header.belongs_to(sector_header.recording_id) {
+ num_data_sectors += 1;
+ }
+ }
+
let mut date_time_s = [b' '; 19];
if let Some(tm) = Time::from_unix_time(sector_header.start_time) {
tm.fmt_time(&mut date_time_s[11..]);
}
+ let recording_size = (num_data_sectors + 1) * (SECTOR_SIZE >> 10);
+
+ let mut recording_size_s = [b'0'; 9];
+ let recording_size_s_len = fmt_u32_pad(&mut recording_size_s,
+ recording_size as u32,
+ 8, b' ');
+
let mut recording_id_s = [b'0'; 9];
let recording_id_s_len =
fmt_u32_pad(&mut recording_id_s,
8, b' ');
tx_buf.write(&date_time_s);
- tx_buf.write(b" ");
+ tx_buf.write(&recording_size_s[0..recording_size_s_len]);
+ tx_buf.write(b"K");
tx_buf.write(&recording_id_s[0..recording_id_s_len]);
tx_buf.write(b"\n");
Err(Error::NoSuchRecording)
}
}
+
+ ///
+ /// Remove recording @p recording_id.
+ pub fn remove_recording(&mut self, recording_id: u16) -> Result<(), Error> {
+ if let Some(found_index) = (0..NUM_SECTORS).find(|&index| {
+ let sector_header = &self.sector_header[index as usize];
+
+ sector_header.recording_id == recording_id &&
+ sector_header.starts_recording()
+ }) {
+ let mut next_sector = found_index as usize;
+
+ for _ in 0..NUM_SECTORS {
+ let address = next_sector * SECTOR_SIZE;
+
+ if let Err(_) = self.storage.erase(address) {
+ return Err(Error::StorageError);
+ }
+
+ // Mark this sector as eligible for the next recording
+ // and ensure it won't be picked up by list_recordings().
+ self.read_sector_header(next_sector);
+
+ next_sector += 1;
+ next_sector &= NUM_SECTORS - 1;
+
+ if !self.sector_header[next_sector].belongs_to(recording_id) {
+ break;
+ }
+ }
+
+ Ok(())
+ } else {
+ Err(Error::NoSuchRecording)
+ }
+ }
}