mirror of
https://github.com/eried/portapack-mayhem.git
synced 2024-12-26 15:59:28 -05:00
Converge File object with std::fstream. Simplify LogFile.
LogFile no longer watches SD card presence and tries to open/close files dynamically.
This commit is contained in:
parent
4d6fccd8ea
commit
0d6103916d
@ -136,7 +136,7 @@ AISLogger::AISLogger(
|
|||||||
|
|
||||||
void AISLogger::on_packet(const ais::Packet& packet) {
|
void AISLogger::on_packet(const ais::Packet& packet) {
|
||||||
// TODO: Unstuff here, not in baseband!
|
// TODO: Unstuff here, not in baseband!
|
||||||
if( log_file.is_ready() ) {
|
if( log_file.is_open() ) {
|
||||||
std::string entry;
|
std::string entry;
|
||||||
entry.reserve((packet.length() + 3) / 4);
|
entry.reserve((packet.length() + 3) / 4);
|
||||||
|
|
||||||
|
@ -110,7 +110,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
CaptureConfig config;
|
CaptureConfig config;
|
||||||
const std::string file_path;
|
const std::string file_path;
|
||||||
File file;
|
|
||||||
static Thread* thread;
|
static Thread* thread;
|
||||||
|
|
||||||
static msg_t static_fn(void* arg) {
|
static msg_t static_fn(void* arg) {
|
||||||
@ -119,7 +118,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg_t run() {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +133,7 @@ private:
|
|||||||
|
|
||||||
while( !chThdShouldTerminate() ) {
|
while( !chThdShouldTerminate() ) {
|
||||||
if( stream.available() >= write_size ) {
|
if( stream.available() >= write_size ) {
|
||||||
if( !transfer(stream, write_buffer.get(), write_size) ) {
|
if( !transfer(stream, file, write_buffer.get(), write_size) ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -144,7 +144,7 @@ private:
|
|||||||
return true;
|
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;
|
bool success = false;
|
||||||
|
|
||||||
led_usb.on();
|
led_usb.on();
|
||||||
|
@ -66,7 +66,7 @@ ERTLogger::ERTLogger(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ERTLogger::on_packet(const ert::Packet& packet) {
|
void ERTLogger::on_packet(const ert::Packet& packet) {
|
||||||
if( log_file.is_ready() ) {
|
if( log_file.is_open() ) {
|
||||||
const auto formatted = packet.symbols_formatted();
|
const auto formatted = packet.symbols_formatted();
|
||||||
log_file.write_entry(packet.received_at(), formatted.data + "/" + formatted.errors);
|
log_file.write_entry(packet.received_at(), formatted.data + "/" + formatted.errors);
|
||||||
}
|
}
|
||||||
|
@ -23,40 +23,32 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
File::~File() {
|
File::File(const std::string& filename, openmode mode) {
|
||||||
close();
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File::open_for_writing(const std::string& file_path) {
|
if( f_open(&f, filename.c_str(), fatfs_mode) == FR_OK ) {
|
||||||
const auto open_result = f_open(&f, file_path.c_str(), FA_WRITE | FA_OPEN_ALWAYS);
|
if( mode & openmode::ate ) {
|
||||||
return (open_result == FR_OK);
|
if( f_lseek(&f, f_size(&f)) != 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool File::close() {
|
|
||||||
f_close(&f);
|
f_close(&f);
|
||||||
return true;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File::is_ready() {
|
File::~File() {
|
||||||
return f_error(&f) == 0;
|
f_close(&f);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool File::read(void* const data, const size_t bytes_to_read) {
|
bool File::read(void* const data, const size_t bytes_to_read) {
|
||||||
|
@ -32,14 +32,21 @@
|
|||||||
|
|
||||||
class File {
|
class File {
|
||||||
public:
|
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();
|
~File();
|
||||||
|
|
||||||
bool open_for_writing(const std::string& file_path);
|
bool is_open() const {
|
||||||
bool open_for_reading(const std::string& file_path);
|
return f_error(&f) == 0;
|
||||||
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 read(void* const data, const size_t bytes_to_read);
|
||||||
bool write(const void* const data, const size_t bytes_to_write);
|
bool write(const void* const data, const size_t bytes_to_write);
|
||||||
@ -57,6 +64,10 @@ private:
|
|||||||
FIL f;
|
FIL f;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline constexpr File::openmode operator|(File::openmode a, File::openmode b) {
|
||||||
|
return File::openmode(static_cast<int>(a) | static_cast<int>(b));
|
||||||
|
}
|
||||||
|
|
||||||
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern);
|
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern);
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
|
@ -23,28 +23,14 @@
|
|||||||
|
|
||||||
#include "string_format.hpp"
|
#include "string_format.hpp"
|
||||||
|
|
||||||
#include "lpc43xx_cpp.hpp"
|
|
||||||
using namespace lpc43xx;
|
|
||||||
|
|
||||||
LogFile::LogFile(
|
LogFile::LogFile(
|
||||||
const std::string& file_path
|
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() {
|
bool LogFile::is_open() const {
|
||||||
sd_card::status_signal -= sd_card_status_signal_token;
|
return file.is_open();
|
||||||
|
|
||||||
file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LogFile::is_ready() {
|
|
||||||
return file.is_ready();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LogFile::write_entry(const rtc::RTC& datetime, const std::string& entry) {
|
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) {
|
bool LogFile::write(const std::string& message) {
|
||||||
return file.puts(message) && file.sync();
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "file.hpp"
|
#include "file.hpp"
|
||||||
#include "sd_card.hpp"
|
|
||||||
|
|
||||||
#include "lpc43xx_cpp.hpp"
|
#include "lpc43xx_cpp.hpp"
|
||||||
using namespace lpc43xx;
|
using namespace lpc43xx;
|
||||||
@ -33,22 +32,15 @@ using namespace lpc43xx;
|
|||||||
class LogFile {
|
class LogFile {
|
||||||
public:
|
public:
|
||||||
LogFile(const std::string& file_path);
|
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);
|
bool write_entry(const rtc::RTC& datetime, const std::string& entry);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const std::string file_path;
|
|
||||||
|
|
||||||
File file;
|
File file;
|
||||||
|
|
||||||
SignalToken sd_card_status_signal_token;
|
|
||||||
|
|
||||||
bool write(const std::string& message);
|
bool write(const std::string& message);
|
||||||
|
|
||||||
void on_sd_card_status(const sd_card::Status status);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif/*__LOG_FILE_H__*/
|
#endif/*__LOG_FILE_H__*/
|
||||||
|
@ -62,7 +62,7 @@ TPMSLogger::TPMSLogger(
|
|||||||
void TPMSLogger::on_packet(const tpms::Packet& packet, const uint32_t target_frequency) {
|
void TPMSLogger::on_packet(const tpms::Packet& packet, const uint32_t target_frequency) {
|
||||||
const auto hex_formatted = packet.symbols_formatted();
|
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!
|
// 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);
|
const auto tuning_frequency_str = to_string_dec_uint(target_frequency, 10);
|
||||||
|
|
||||||
|
@ -107,8 +107,7 @@ void RecordView::stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RecordView::write_metadata_file(const std::string& filename) {
|
void RecordView::write_metadata_file(const std::string& filename) {
|
||||||
File file;
|
File file { filename, File::openmode::out | File::openmode::trunc };
|
||||||
file.open_for_writing(filename);
|
|
||||||
file.puts("sample_rate=" + to_string_dec_uint(sampling_rate) + "\n");
|
file.puts("sample_rate=" + to_string_dec_uint(sampling_rate) + "\n");
|
||||||
file.puts("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()) + "\n");
|
file.puts("center_frequency=" + to_string_dec_uint(receiver_model.tuning_frequency()) + "\n");
|
||||||
}
|
}
|
||||||
|
@ -133,15 +133,15 @@ private:
|
|||||||
return Result::FailHeap;
|
return Result::FailHeap;
|
||||||
}
|
}
|
||||||
|
|
||||||
File file;
|
File file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc };
|
||||||
if( !file.open_for_writing(filename) ) {
|
if( !file.is_open() ) {
|
||||||
return Result::FailFileOpenWrite;
|
return Result::FailFileOpenWrite;
|
||||||
}
|
}
|
||||||
|
|
||||||
lfsr_word_t v = 1;
|
lfsr_word_t v = 1;
|
||||||
|
|
||||||
const halrtcnt_t test_start = halGetCounterValue();
|
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,
|
lfsr_fill(v,
|
||||||
reinterpret_cast<lfsr_word_t*>(buffer->data()),
|
reinterpret_cast<lfsr_word_t*>(buffer->data()),
|
||||||
sizeof(*buffer.get()) / sizeof(lfsr_word_t)
|
sizeof(*buffer.get()) / sizeof(lfsr_word_t)
|
||||||
@ -164,7 +164,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.sync();
|
||||||
|
|
||||||
const halrtcnt_t test_end = halGetCounterValue();
|
const halrtcnt_t test_end = halGetCounterValue();
|
||||||
_stats.write_test_duration = test_end - test_start;
|
_stats.write_test_duration = test_end - test_start;
|
||||||
@ -178,15 +178,15 @@ private:
|
|||||||
return Result::FailHeap;
|
return Result::FailHeap;
|
||||||
}
|
}
|
||||||
|
|
||||||
File file;
|
File file { filename, File::openmode::in | File::openmode::binary };
|
||||||
if( !file.open_for_reading(filename) ) {
|
if( !file.is_open() ) {
|
||||||
return Result::FailFileOpenRead;
|
return Result::FailFileOpenRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
lfsr_word_t v = 1;
|
lfsr_word_t v = 1;
|
||||||
|
|
||||||
const halrtcnt_t test_start = halGetCounterValue();
|
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();
|
const halrtcnt_t read_start = halGetCounterValue();
|
||||||
if( !file.read(buffer->data(), buffer->size()) ) {
|
if( !file.read(buffer->data(), buffer->size()) ) {
|
||||||
break;
|
break;
|
||||||
@ -211,7 +211,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.close();
|
file.sync();
|
||||||
|
|
||||||
const halrtcnt_t test_end = halGetCounterValue();
|
const halrtcnt_t test_end = halGetCounterValue();
|
||||||
_stats.read_test_duration = test_end - test_start;
|
_stats.read_test_duration = test_end - test_start;
|
||||||
|
@ -51,9 +51,8 @@ static constexpr std::array<uint8_t, 12> png_iend { {
|
|||||||
|
|
||||||
PNGWriter::PNGWriter(
|
PNGWriter::PNGWriter(
|
||||||
const std::string& filename
|
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_file_header);
|
||||||
file.write(png_ihdr_screen_capture);
|
file.write(png_ihdr_screen_capture);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user