X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fcommon%2Flogger.rs;h=9b498ce64a5cec5e7f76be504fab5395a80ac41c;hb=20891d38ee4363b3cbc9fc5fd69dae5c6ae59a82;hp=022b155f7d1b90b9667d5291dd4b8da9115b5dbb;hpb=a8836e8e83b9fbcd551eb1882dabac01c03efd3b;p=gps-watch.git diff --git a/src/common/logger.rs b/src/common/logger.rs index 022b155..9b498ce 100644 --- a/src/common/logger.rs +++ b/src/common/logger.rs @@ -38,6 +38,7 @@ const NUM_SECTORS: usize = MEMORY_SIZE / SECTOR_SIZE; #[derive(Clone, Copy, PartialEq, Debug)] pub enum Error { NoSuchRecording, + StorageError, } enum SectorFlag { @@ -144,10 +145,22 @@ fn cmp_sector_header_indices(a: u16, b: u16, 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 @@ -187,56 +200,11 @@ impl<'a> SectorHeaderIter<'a> { 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 @@ -252,9 +220,7 @@ impl<'a> SectorHeaderIter<'a> { } for i in (1..num_elts_to_sort).rev() { - let t = self.indices[0]; - self.indices[0] = self.indices[i]; - self.indices[i] = t; + self.indices.swap(0, i); downheap(&mut self.indices, 0, i - 1, self.sector_header); } @@ -395,7 +361,7 @@ impl<'a> Logger<'a> { 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 } @@ -411,8 +377,8 @@ impl<'a> Logger<'a> { // 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); @@ -589,6 +555,17 @@ impl<'a> Logger<'a> { 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) { @@ -596,6 +573,13 @@ impl<'a> Logger<'a> { 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, @@ -603,7 +587,8 @@ impl<'a> Logger<'a> { 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"); @@ -682,4 +667,40 @@ impl<'a> Logger<'a> { 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) + } + } }