From 57aed55cc71c56d756ec59fc0608e713dbad9599 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Tue, 1 Dec 2015 22:39:27 -0800 Subject: [PATCH] Extract LogFile, add (dumb) logging to ERT, AIS apps. --- firmware/application/Makefile | 1 + firmware/application/app_ais.cpp | 11 ++++++- firmware/application/app_ais.hpp | 4 +++ firmware/application/app_ert.cpp | 7 ++++ firmware/application/app_ert.hpp | 4 +++ firmware/application/app_tpms.cpp | 27 ++------------- firmware/application/app_tpms.hpp | 8 ++--- firmware/application/log_file.cpp | 55 +++++++++++++++++++++++++++++++ firmware/application/log_file.hpp | 42 +++++++++++++++++++++++ 9 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 firmware/application/log_file.cpp create mode 100644 firmware/application/log_file.hpp diff --git a/firmware/application/Makefile b/firmware/application/Makefile index f8507d83..3ecf44e2 100755 --- a/firmware/application/Makefile +++ b/firmware/application/Makefile @@ -175,6 +175,7 @@ CPPSRC = main.cpp \ app_ert.cpp \ app_spectrum_analysis.cpp \ sd_card.cpp \ + log_file.cpp \ manchester.cpp \ ../common/utility.cpp \ ../common/chibios_cpp.cpp \ diff --git a/firmware/application/app_ais.cpp b/firmware/application/app_ais.cpp index 118323ad..dbee7c74 100644 --- a/firmware/application/app_ais.cpp +++ b/firmware/application/app_ais.cpp @@ -31,10 +31,19 @@ AISModel::AISModel() { .decimation_factor = 4, }); receiver_model.set_baseband_bandwidth(1750000); + + log_file.open_for_append("ais.txt"); } baseband::ais::decoded_packet AISModel::on_packet(const AISPacketMessage& message) { - return baseband::ais::packet_decode(message.packet.payload, message.packet.bits_received); + const auto result = baseband::ais::packet_decode(message.packet.payload, message.packet.bits_received); + + if( log_file.is_ready() ) { + std::string entry = result.first + "/" + result.second + "\r\n"; + log_file.write(entry); + } + + return result; } namespace ui { diff --git a/firmware/application/app_ais.hpp b/firmware/application/app_ais.hpp index 606aee87..19b4d018 100644 --- a/firmware/application/app_ais.hpp +++ b/firmware/application/app_ais.hpp @@ -24,6 +24,7 @@ #include "ui_console.hpp" #include "message.hpp" +#include "log_file.hpp" #include "ais_baseband.hpp" @@ -32,6 +33,9 @@ public: AISModel(); baseband::ais::decoded_packet on_packet(const AISPacketMessage& message); + +private: + LogFile log_file; }; namespace ui { diff --git a/firmware/application/app_ert.cpp b/firmware/application/app_ert.cpp index 30ef4736..9a4facb6 100644 --- a/firmware/application/app_ert.cpp +++ b/firmware/application/app_ert.cpp @@ -33,6 +33,8 @@ ERTModel::ERTModel() { .decimation_factor = 1, }); receiver_model.set_baseband_bandwidth(1750000); + + log_file.open_for_append("ert.txt"); } std::string ERTModel::on_packet(const ERTPacketMessage& message) { @@ -53,6 +55,11 @@ std::string ERTModel::on_packet(const ERTPacketMessage& message) { s += hex_formatted.errors; s += "\n"; + if( log_file.is_ready() ) { + std::string entry = hex_formatted.data + "/" + hex_formatted.errors + "\r\n"; + log_file.write(entry); + } + return s; } diff --git a/firmware/application/app_ert.hpp b/firmware/application/app_ert.hpp index 05f0eba7..8bb46138 100644 --- a/firmware/application/app_ert.hpp +++ b/firmware/application/app_ert.hpp @@ -24,6 +24,7 @@ #include "ui_console.hpp" #include "message.hpp" +#include "log_file.hpp" #include @@ -32,6 +33,9 @@ public: ERTModel(); std::string on_packet(const ERTPacketMessage& message); + +private: + LogFile log_file; }; namespace ui { diff --git a/firmware/application/app_tpms.cpp b/firmware/application/app_tpms.cpp index 598cabce..67da089d 100644 --- a/firmware/application/app_tpms.cpp +++ b/firmware/application/app_tpms.cpp @@ -39,18 +39,14 @@ TPMSModel::TPMSModel() { }); receiver_model.set_baseband_bandwidth(1750000); - open_file(); -} - -TPMSModel::~TPMSModel() { - f_close(&fil); + log_file.open_for_append("tpms.txt"); } ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) { const ManchesterDecoder decoder(message.packet.payload, message.packet.bits_received, 1); const auto hex_formatted = format_manchester(decoder); - if( !f_error(&fil) ) { + if( log_file.is_ready() ) { rtc::RTC datetime; rtcGetTime(&RTCD1, &datetime); std::string timestamp = @@ -66,29 +62,12 @@ ManchesterFormatted TPMSModel::on_packet(const TPMSPacketMessage& message) { const auto tuning_frequency_str = ui::to_string_dec_uint(tuning_frequency, 10); std::string log = timestamp + " " + tuning_frequency_str + " FSK 38.4 19.2 " + hex_formatted.data + "/" + hex_formatted.errors + "\r\n"; - f_puts(log.c_str(), &fil); - f_sync(&fil); + log_file.write(log); } return hex_formatted; } -void TPMSModel::open_file() { - const auto open_result = f_open(&fil, "tpms.txt", FA_WRITE | FA_OPEN_ALWAYS); - if( open_result == FR_OK ) { - const auto fil_size = f_size(&fil); - const auto seek_result = f_lseek(&fil, fil_size); - if( seek_result != FR_OK ) { - f_close(&fil); - // TODO: Error, indicate somehow. - } else { - // TODO: Indicate success. - } - } else { - // TODO: Error, indicate somehow. - } -} - namespace ui { void TPMSView::on_show() { diff --git a/firmware/application/app_tpms.hpp b/firmware/application/app_tpms.hpp index f1d431be..962129f0 100644 --- a/firmware/application/app_tpms.hpp +++ b/firmware/application/app_tpms.hpp @@ -26,20 +26,16 @@ #include "message.hpp" #include "manchester.hpp" - -#include "ff.h" +#include "log_file.hpp" class TPMSModel { public: TPMSModel(); - ~TPMSModel(); ManchesterFormatted on_packet(const TPMSPacketMessage& message); private: - FIL fil; - - void open_file(); + LogFile log_file; }; namespace ui { diff --git a/firmware/application/log_file.cpp b/firmware/application/log_file.cpp new file mode 100644 index 00000000..82b2a046 --- /dev/null +++ b/firmware/application/log_file.cpp @@ -0,0 +1,55 @@ +/* + * 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 "log_file.hpp" + +LogFile::~LogFile() { + close(); +} + +bool LogFile::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 LogFile::close() { + f_close(&f); + return true; +} + +bool LogFile::is_ready() { + return !f_error(&f); +} + +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); +} diff --git a/firmware/application/log_file.hpp b/firmware/application/log_file.hpp new file mode 100644 index 00000000..19887d00 --- /dev/null +++ b/firmware/application/log_file.hpp @@ -0,0 +1,42 @@ +/* + * 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 __LOG_FILE_H__ +#define __LOG_FILE_H__ + +#include + +#include "ff.h" + +class LogFile { +public: + ~LogFile(); + + bool open_for_append(const std::string file_path); + bool close(); + bool is_ready(); + bool write(const std::string message); + +private: + FIL f; +}; + +#endif/*__LOG_FILE_H__*/