From fc7a7d753d5e29041a0bf85f4de5807f7131c00b Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Sun, 17 Jan 2016 14:20:02 -0800 Subject: [PATCH] Extract general File class from LogFile. --- firmware/application/Makefile | 1 + firmware/application/file.cpp | 71 +++++++++++++++++++++++++++++++ firmware/application/file.hpp | 52 ++++++++++++++++++++++ firmware/application/log_file.cpp | 33 +++----------- firmware/application/log_file.hpp | 6 +-- 5 files changed, 132 insertions(+), 31 deletions(-) create mode 100644 firmware/application/file.cpp create mode 100644 firmware/application/file.hpp diff --git a/firmware/application/Makefile b/firmware/application/Makefile index 41c8e761..73b6cebc 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -176,6 +176,7 @@ CPPSRC = main.cpp \ ../common/ert_packet.cpp \ spectrum_analysis_app.cpp \ sd_card.cpp \ + file.cpp \ log_file.cpp \ manchester.cpp \ string_format.cpp \ diff --git a/firmware/application/file.cpp b/firmware/application/file.cpp new file mode 100644 index 00000000..439c0a9c --- /dev/null +++ b/firmware/application/file.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc. + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#include "file.hpp" + +File::~File() { + close(); +} + +bool File::open_for_append(const std::string file_path) { + const auto open_result = f_open(&f, file_path.c_str(), FA_WRITE | FA_OPEN_ALWAYS); + if( open_result == FR_OK ) { + const auto seek_result = f_lseek(&f, f_size(&f)); + if( seek_result == FR_OK ) { + return true; + } else { + close(); + } + } + + return false; +} + +bool File::close() { + 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) { + UINT bytes_read = 0; + const auto result = f_read(&f, data, bytes_to_read, &bytes_read); + return (result == FR_OK) && (bytes_read == bytes_to_read); +} + +bool File::write(const void* const data, const size_t bytes_to_write) { + UINT bytes_written = 0; + const auto result = f_write(&f, data, bytes_to_write, &bytes_written); + return (result == FR_OK) && (bytes_written == bytes_to_write); +} + +bool File::puts(const std::string string) { + const auto result = f_puts(string.c_str(), &f); + return (result >= 0); +} + +bool File::sync() { + const auto result = f_sync(&f); + return (result == FR_OK); +} diff --git a/firmware/application/file.hpp b/firmware/application/file.hpp new file mode 100644 index 00000000..363bd866 --- /dev/null +++ b/firmware/application/file.hpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc. + * + * This file is part of PortaPack. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __FILE_H__ +#define __FILE_H__ + +#include "ff.h" + +#include +#include + +class File { +public: + ~File(); + + bool open_for_append(const std::string file_path); + bool close(); + + bool is_ready(); + + bool read(void* const data, const size_t bytes_to_read); + bool write(const void* const data, const size_t bytes_to_write); + + bool puts(const std::string string); + + bool sync(); + +private: + const std::string file_path; + + FIL f; +}; + +#endif/*__FILE_H__*/ diff --git a/firmware/application/log_file.cpp b/firmware/application/log_file.cpp index 43528aa4..3b5f4f01 100644 --- a/firmware/application/log_file.cpp +++ b/firmware/application/log_file.cpp @@ -30,7 +30,7 @@ LogFile::LogFile( const std::string file_path ) : file_path { file_path } { - open(); + 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); @@ -40,30 +40,11 @@ LogFile::LogFile( LogFile::~LogFile() { sd_card::status_signal -= sd_card_status_signal_token; - close(); -} - -bool LogFile::open() { - const auto open_result = f_open(&f, file_path.c_str(), FA_WRITE | FA_OPEN_ALWAYS); - if( open_result == FR_OK ) { - const auto seek_result = f_lseek(&f, f_size(&f)); - if( seek_result == FR_OK ) { - return true; - } else { - close(); - } - } - - return false; -} - -bool LogFile::close() { - f_close(&f); - return true; + file.close(); } bool LogFile::is_ready() { - return !f_error(&f); + return file.is_ready(); } bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) { @@ -72,15 +53,13 @@ bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) { } bool LogFile::write(const std::string& message) { - const auto puts_result = f_puts(message.c_str(), &f); - const auto sync_result = f_sync(&f); - return (puts_result >= 0) && (sync_result == FR_OK); + return file.puts(message) && file.sync(); } void LogFile::on_sd_card_status(const sd_card::Status status) { if( status == sd_card::Status::Mounted ) { - open(); + file.open_for_append(file_path); } else { - close(); + file.close(); } } diff --git a/firmware/application/log_file.hpp b/firmware/application/log_file.hpp index c0f3138c..0ddf781c 100644 --- a/firmware/application/log_file.hpp +++ b/firmware/application/log_file.hpp @@ -24,7 +24,7 @@ #include -#include "ff.h" +#include "file.hpp" #include "sd_card.hpp" #include "lpc43xx_cpp.hpp" @@ -35,8 +35,6 @@ public: LogFile(const std::string file_path); ~LogFile(); - bool open(); - bool close(); bool is_ready(); bool write_entry(const rtc::RTC& datetime, const std::string& entry); @@ -44,7 +42,7 @@ public: private: const std::string file_path; - FIL f; + File file; SignalToken sd_card_status_signal_token;