From 9464bc61732f5d5946aab5b8b4c9c8b9e77e944c Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Fri, 9 Jun 2017 11:36:18 +1000 Subject: [PATCH] DataFlash: create a WritesOK method for WritePrioritisedBlock --- libraries/DataFlash/DataFlash_Backend.cpp | 8 ++++++++ libraries/DataFlash/DataFlash_Backend.h | 2 ++ libraries/DataFlash/DataFlash_Block.cpp | 16 +++++++++++++++- libraries/DataFlash/DataFlash_Block.h | 4 +++- libraries/DataFlash/DataFlash_File.cpp | 19 ++++++++++++++++++- libraries/DataFlash/DataFlash_File.h | 4 ++++ libraries/DataFlash/DataFlash_MAVLink.cpp | 17 ++++++++++++++++- libraries/DataFlash/DataFlash_MAVLink.h | 4 ++++ libraries/DataFlash/DataFlash_SITL.cpp | 2 +- libraries/DataFlash/DataFlash_SITL.h | 2 +- 10 files changed, 72 insertions(+), 6 deletions(-) diff --git a/libraries/DataFlash/DataFlash_Backend.cpp b/libraries/DataFlash/DataFlash_Backend.cpp index bffd9f3296..e579ec813a 100644 --- a/libraries/DataFlash/DataFlash_Backend.cpp +++ b/libraries/DataFlash/DataFlash_Backend.cpp @@ -259,3 +259,11 @@ bool DataFlash_Backend::Log_Write(const uint8_t msg_type, va_list arg_list, bool return WritePrioritisedBlock(buffer, msg_len, is_critical); } + +bool DataFlash_Backend::WritesOK() const +{ + if (!_writes_enabled) { + return false; + } + return true; +} diff --git a/libraries/DataFlash/DataFlash_Backend.h b/libraries/DataFlash/DataFlash_Backend.h index d74e83746b..e1f6ceb864 100644 --- a/libraries/DataFlash/DataFlash_Backend.h +++ b/libraries/DataFlash/DataFlash_Backend.h @@ -140,6 +140,8 @@ protected: print_mode_fn print_mode, AP_HAL::BetterStream *port); + virtual bool WritesOK() const; + bool _writes_enabled = false; /* diff --git a/libraries/DataFlash/DataFlash_Block.cpp b/libraries/DataFlash/DataFlash_Block.cpp index 204b6f054d..91aafac95d 100644 --- a/libraries/DataFlash/DataFlash_Block.cpp +++ b/libraries/DataFlash/DataFlash_Block.cpp @@ -42,13 +42,27 @@ void DataFlash_Block::FinishWrite(void) df_BufferIdx = 0; } +bool DataFlash_Block::WritesOK() const +{ + if (!DataFlash_Backend::WritesOK()) { + return false; + } + if (!CardInserted()) { + return false; + } + if (!log_write_started) { + return false; + } + return true; +} + bool DataFlash_Block::WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical) { // is_critical is ignored - we're a ring buffer and never run out // of space. possibly if we do more complicated bandwidth // limiting we can reservice bandwidth based on is_critical - if (!CardInserted() || !log_write_started || !_writes_enabled) { + if (!WritesOK()) { return false; } diff --git a/libraries/DataFlash/DataFlash_Block.h b/libraries/DataFlash/DataFlash_Block.h index e77a8b6d49..286f80a686 100644 --- a/libraries/DataFlash/DataFlash_Block.h +++ b/libraries/DataFlash/DataFlash_Block.h @@ -13,7 +13,7 @@ public: DataFlash_Block(DataFlash_Class &front, DFMessageWriter_DFLogStart *writer) : DataFlash_Backend(front, writer) { } - virtual bool CardInserted(void) = 0; + virtual bool CardInserted(void) const = 0; // erase handling void EraseAll(); @@ -115,6 +115,8 @@ protected: uint16_t df_NumPages; virtual void ReadManufacturerID() = 0; + + bool WritesOK() const override; }; diff --git a/libraries/DataFlash/DataFlash_File.cpp b/libraries/DataFlash/DataFlash_File.cpp index f7e9197313..c571c31aa7 100644 --- a/libraries/DataFlash/DataFlash_File.cpp +++ b/libraries/DataFlash/DataFlash_File.cpp @@ -509,10 +509,27 @@ void DataFlash_File::EraseAll() } } +bool DataFlash_File::WritesOK() const +{ + if (!DataFlash_Backend::WritesOK()) { + return false; + } + if (_write_fd == -1) { + return false; + } + if (!_initialised) { + return false; + } + if (_open_error) { + return false; + } + return true; +} + /* Write a block of data at current offset */ bool DataFlash_File::WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical) { - if (_write_fd == -1 || !_initialised || _open_error || !_writes_enabled) { + if (!WritesOK()) { return false; } diff --git a/libraries/DataFlash/DataFlash_File.h b/libraries/DataFlash/DataFlash_File.h index cb8f8ec33d..13a3879564 100644 --- a/libraries/DataFlash/DataFlash_File.h +++ b/libraries/DataFlash/DataFlash_File.h @@ -73,6 +73,10 @@ public: void vehicle_was_disarmed() override; +protected: + + bool WritesOK() const override; + private: int _write_fd; int _read_fd; diff --git a/libraries/DataFlash/DataFlash_MAVLink.cpp b/libraries/DataFlash/DataFlash_MAVLink.cpp index 81f9801c42..12e9de460d 100644 --- a/libraries/DataFlash/DataFlash_MAVLink.cpp +++ b/libraries/DataFlash/DataFlash_MAVLink.cpp @@ -118,12 +118,27 @@ bool DataFlash_MAVLink::free_seqno_from_queue(uint32_t seqno, dm_block_queue_t & return false; } + +bool DataFlash_MAVLink::WritesOK() const +{ + if (!DataFlash_Backend::WritesOK()) { + return false; + } + if (!_initialised) { + return false; + } + if (!_sending_to_client) { + return false; + } + return true; +} + /* Write a block of data at current offset */ // DM_write: 70734 events, 0 overruns, 167806us elapsed, 2us avg, min 1us max 34us 0.620us rms bool DataFlash_MAVLink::WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical) { - if (!_initialised || !_sending_to_client || !_writes_enabled) { + if (!WritesOK()) { return false; } diff --git a/libraries/DataFlash/DataFlash_MAVLink.h b/libraries/DataFlash/DataFlash_MAVLink.h index 60faa25013..b030870495 100644 --- a/libraries/DataFlash/DataFlash_MAVLink.h +++ b/libraries/DataFlash/DataFlash_MAVLink.h @@ -67,6 +67,10 @@ public: void remote_log_block_status_msg(mavlink_channel_t chan, mavlink_message_t* msg) override; +protected: + + bool WritesOK() const override; + private: struct dm_block { diff --git a/libraries/DataFlash/DataFlash_SITL.cpp b/libraries/DataFlash/DataFlash_SITL.cpp index 3ae175de71..2973d4a2ab 100644 --- a/libraries/DataFlash/DataFlash_SITL.cpp +++ b/libraries/DataFlash/DataFlash_SITL.cpp @@ -55,7 +55,7 @@ void DataFlash_SITL::ReadManufacturerID() df_device = 0x0203; } -bool DataFlash_SITL::CardInserted(void) +bool DataFlash_SITL::CardInserted(void) const { return true; } diff --git a/libraries/DataFlash/DataFlash_SITL.h b/libraries/DataFlash/DataFlash_SITL.h index 09c2142661..78018d5ff2 100644 --- a/libraries/DataFlash/DataFlash_SITL.h +++ b/libraries/DataFlash/DataFlash_SITL.h @@ -42,7 +42,7 @@ public: DataFlash_Block(front, writer) { } void Init() override; void ReadManufacturerID(); - bool CardInserted(); + bool CardInserted() const; }; #endif // CONFIG_HAL_BOARD == HAL_BOARD_SITL