From 0d6103916d80ab449b7e19bc216c974a7c8fb12d Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sat, 30 Apr 2016 15:09:34 -0700 Subject: [PATCH] Converge File object with std::fstream. Simplify LogFile. LogFile no longer watches SD card presence and tries to open/close files dynamically. --- firmware/application/ais_app.cpp | 2 +- firmware/application/capture_thread.hpp | 8 ++-- firmware/application/ert_app.cpp | 2 +- firmware/application/file.cpp | 50 ++++++++++------------- firmware/application/file.hpp | 23 ++++++++--- firmware/application/log_file.cpp | 28 ++----------- firmware/application/log_file.hpp | 10 +---- firmware/application/tpms_app.cpp | 2 +- firmware/application/ui_record_view.cpp | 3 +- firmware/application/ui_sd_card_debug.cpp | 16 ++++---- firmware/common/png_writer.cpp | 3 +- 11 files changed, 59 insertions(+), 88 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index 7fde92a4..a6508c91 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -136,7 +136,7 @@ AISLogger::AISLogger( void AISLogger::on_packet(const ais::Packet& packet) { // TODO: Unstuff here, not in baseband! - if( log_file.is_ready() ) { + if( log_file.is_open() ) { std::string entry; entry.reserve((packet.length() + 3) / 4); diff --git a/firmware/application/capture_thread.hpp b/firmware/application/capture_thread.hpp index b2787f93..1568ebc8 100644 --- a/firmware/application/capture_thread.hpp +++ b/firmware/application/capture_thread.hpp @@ -110,7 +110,6 @@ public: private: CaptureConfig config; const std::string file_path; - File file; static Thread* thread; static msg_t static_fn(void* arg) { @@ -119,7 +118,8 @@ private: } msg_t run() { - if( !file.open_for_writing(file_path) ) { + File file { file_path, File::openmode::out | File::openmode::binary | File::openmode::trunc }; + if( !file.is_open() ) { return false; } @@ -133,7 +133,7 @@ private: while( !chThdShouldTerminate() ) { if( stream.available() >= write_size ) { - if( !transfer(stream, write_buffer.get(), write_size) ) { + if( !transfer(stream, file, write_buffer.get(), write_size) ) { return false; } } else { @@ -144,7 +144,7 @@ private: return true; } - bool transfer(StreamOutput& stream, uint8_t* const write_buffer, const size_t write_size) { + bool transfer(StreamOutput& stream, File& file, uint8_t* const write_buffer, const size_t write_size) { bool success = false; led_usb.on(); diff --git a/firmware/application/ert_app.cpp b/firmware/application/ert_app.cpp index 938aef9d..bbdf5848 100644 --- a/firmware/application/ert_app.cpp +++ b/firmware/application/ert_app.cpp @@ -66,7 +66,7 @@ ERTLogger::ERTLogger( } void ERTLogger::on_packet(const ert::Packet& packet) { - if( log_file.is_ready() ) { + if( log_file.is_open() ) { const auto formatted = packet.symbols_formatted(); log_file.write_entry(packet.received_at(), formatted.data + "/" + formatted.errors); } diff --git a/firmware/application/file.cpp b/firmware/application/file.cpp index 871c246f..ff523de8 100644 --- a/firmware/application/file.cpp +++ b/firmware/application/file.cpp @@ -23,40 +23,32 @@ #include -File::~File() { - close(); -} - -bool File::open_for_writing(const std::string& file_path) { - const auto open_result = f_open(&f, file_path.c_str(), FA_WRITE | FA_OPEN_ALWAYS); - return (open_result == FR_OK); -} - -bool File::open_for_reading(const std::string& file_path) { - const auto open_result = f_open(&f, file_path.c_str(), FA_READ | FA_OPEN_EXISTING); - return (open_result == FR_OK); -} - -bool File::open_for_append(const std::string& file_path) { - if( open_for_writing(file_path) ) { - const auto seek_result = f_lseek(&f, f_size(&f)); - if( seek_result == FR_OK ) { - return true; - } else { - close(); - } +File::File(const std::string& filename, openmode mode) { + BYTE fatfs_mode = 0; + if( mode & openmode::in ) { + fatfs_mode |= FA_READ; + } + if( mode & openmode::out ) { + fatfs_mode |= FA_WRITE; + } + if( mode & openmode::trunc ) { + fatfs_mode |= FA_CREATE_ALWAYS; + } + if( mode & openmode::ate ) { + fatfs_mode |= FA_OPEN_ALWAYS; } - return false; + if( f_open(&f, filename.c_str(), fatfs_mode) == FR_OK ) { + if( mode & openmode::ate ) { + if( f_lseek(&f, f_size(&f)) != FR_OK ) { + f_close(&f); + } + } + } } -bool File::close() { +File::~File() { f_close(&f); - return true; -} - -bool File::is_ready() { - return f_error(&f) == 0; } bool File::read(void* const data, const size_t bytes_to_read) { diff --git a/firmware/application/file.hpp b/firmware/application/file.hpp index 7a45af5d..b4b0c74f 100644 --- a/firmware/application/file.hpp +++ b/firmware/application/file.hpp @@ -32,14 +32,21 @@ class File { public: + enum openmode { + app = 0x100, + binary = 0x200, + in = FA_READ, + out = FA_WRITE, + trunc = FA_CREATE_ALWAYS, + ate = FA_OPEN_ALWAYS, + }; + + File(const std::string& filename, openmode mode); ~File(); - bool open_for_writing(const std::string& file_path); - bool open_for_reading(const std::string& file_path); - bool open_for_append(const std::string& file_path); - bool close(); - - bool is_ready(); + bool is_open() const { + return f_error(&f) == 0; + } bool read(void* const data, const size_t bytes_to_read); bool write(const void* const data, const size_t bytes_to_write); @@ -57,6 +64,10 @@ private: FIL f; }; +inline constexpr File::openmode operator|(File::openmode a, File::openmode b) { + return File::openmode(static_cast(a) | static_cast(b)); +} + std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern); namespace std { diff --git a/firmware/application/log_file.cpp b/firmware/application/log_file.cpp index e87cce36..06e56246 100644 --- a/firmware/application/log_file.cpp +++ b/firmware/application/log_file.cpp @@ -23,28 +23,14 @@ #include "string_format.hpp" -#include "lpc43xx_cpp.hpp" -using namespace lpc43xx; - LogFile::LogFile( const std::string& file_path -) : file_path { file_path } +) : file { file_path, File::openmode::out | File::openmode::ate } { - file.open_for_append(file_path); - - sd_card_status_signal_token = sd_card::status_signal += [this](const sd_card::Status status) { - this->on_sd_card_status(status); - }; } -LogFile::~LogFile() { - sd_card::status_signal -= sd_card_status_signal_token; - - file.close(); -} - -bool LogFile::is_ready() { - return file.is_ready(); +bool LogFile::is_open() const { + return file.is_open(); } bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) { @@ -55,11 +41,3 @@ bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) { bool LogFile::write(const std::string& message) { return file.puts(message) && file.sync(); } - -void LogFile::on_sd_card_status(const sd_card::Status status) { - if( status == sd_card::Status::Mounted ) { - file.open_for_append(file_path); - } else { - file.close(); - } -} diff --git a/firmware/application/log_file.hpp b/firmware/application/log_file.hpp index 30883b0f..70a1651c 100644 --- a/firmware/application/log_file.hpp +++ b/firmware/application/log_file.hpp @@ -25,7 +25,6 @@ #include #include "file.hpp" -#include "sd_card.hpp" #include "lpc43xx_cpp.hpp" using namespace lpc43xx; @@ -33,22 +32,15 @@ using namespace lpc43xx; class LogFile { public: LogFile(const std::string& file_path); - ~LogFile(); - bool is_ready(); + bool is_open() const; bool write_entry(const rtc::RTC& datetime, const std::string& entry); private: - const std::string file_path; - File file; - SignalToken sd_card_status_signal_token; - bool write(const std::string& message); - - void on_sd_card_status(const sd_card::Status status); }; #endif/*__LOG_FILE_H__*/ diff --git a/firmware/application/tpms_app.cpp b/firmware/application/tpms_app.cpp index c80bfb9d..abe27450 100644 --- a/firmware/application/tpms_app.cpp +++ b/firmware/application/tpms_app.cpp @@ -62,7 +62,7 @@ TPMSLogger::TPMSLogger( void TPMSLogger::on_packet(const tpms::Packet& packet, const uint32_t target_frequency) { const auto hex_formatted = packet.symbols_formatted(); - if( log_file.is_ready() ) { + if( log_file.is_open() ) { // TODO: function doesn't take uint64_t, so when >= 1<<32, weirdness will ensue! const auto tuning_frequency_str = to_string_dec_uint(target_frequency, 10); diff --git a/firmware/application/ui_record_view.cpp b/firmware/application/ui_record_view.cpp index dc83d42d..07f40f17 100644 --- a/firmware/application/ui_record_view.cpp +++ b/firmware/application/ui_record_view.cpp @@ -107,8 +107,7 @@ void RecordView::stop() { } void RecordView::write_metadata_file(const std::string& filename) { - File file; - file.open_for_writing(filename); + File file { filename, File::openmode::out | File::openmode::trunc }; file.puts("sample_rate=" + to_string_dec_uint(sampling_rate) + "\n"); file.puts("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()) + "\n"); } diff --git a/firmware/application/ui_sd_card_debug.cpp b/firmware/application/ui_sd_card_debug.cpp index 25d9ba5d..28d164e2 100644 --- a/firmware/application/ui_sd_card_debug.cpp +++ b/firmware/application/ui_sd_card_debug.cpp @@ -133,15 +133,15 @@ private: return Result::FailHeap; } - File file; - if( !file.open_for_writing(filename) ) { + File file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc }; + if( !file.is_open() ) { return Result::FailFileOpenWrite; } lfsr_word_t v = 1; const halrtcnt_t test_start = halGetCounterValue(); - while( !chThdShouldTerminate() && file.is_ready() && (_stats.write_bytes < bytes_to_write) ) { + while( !chThdShouldTerminate() && file.is_open() && (_stats.write_bytes < bytes_to_write) ) { lfsr_fill(v, reinterpret_cast(buffer->data()), sizeof(*buffer.get()) / sizeof(lfsr_word_t) @@ -164,7 +164,7 @@ private: } } - file.close(); + file.sync(); const halrtcnt_t test_end = halGetCounterValue(); _stats.write_test_duration = test_end - test_start; @@ -178,15 +178,15 @@ private: return Result::FailHeap; } - File file; - if( !file.open_for_reading(filename) ) { + File file { filename, File::openmode::in | File::openmode::binary }; + if( !file.is_open() ) { return Result::FailFileOpenRead; } lfsr_word_t v = 1; const halrtcnt_t test_start = halGetCounterValue(); - while( !chThdShouldTerminate() && file.is_ready() && (_stats.read_bytes < bytes_to_read) ) { + while( !chThdShouldTerminate() && file.is_open() && (_stats.read_bytes < bytes_to_read) ) { const halrtcnt_t read_start = halGetCounterValue(); if( !file.read(buffer->data(), buffer->size()) ) { break; @@ -211,7 +211,7 @@ private: } } - file.close(); + file.sync(); const halrtcnt_t test_end = halGetCounterValue(); _stats.read_test_duration = test_end - test_start; diff --git a/firmware/common/png_writer.cpp b/firmware/common/png_writer.cpp index 15e1b8aa..c1d59244 100644 --- a/firmware/common/png_writer.cpp +++ b/firmware/common/png_writer.cpp @@ -51,9 +51,8 @@ static constexpr std::array png_iend { { PNGWriter::PNGWriter( const std::string& filename -) +) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc } { - file.open_for_writing(filename); file.write(png_file_header); file.write(png_ihdr_screen_capture);