diff --git a/libraries/DataFlash/DataFlash.cpp b/libraries/DataFlash/DataFlash.cpp index 0e6475e785..8ddfe6edb9 100644 --- a/libraries/DataFlash/DataFlash.cpp +++ b/libraries/DataFlash/DataFlash.cpp @@ -66,5 +66,11 @@ void DataFlash_Class::EnableWrites(bool enable) { backend->EnableWrites(enable); } +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX + // currently only DataFlash_File support this: +void DataFlash_Class::flush(void) { + backend->flush(); +} +#endif // end functions pass straight through to backend diff --git a/libraries/DataFlash/DataFlash.h b/libraries/DataFlash/DataFlash.h index 1dd1bf51a7..55753fc817 100644 --- a/libraries/DataFlash/DataFlash.h +++ b/libraries/DataFlash/DataFlash.h @@ -104,6 +104,11 @@ public: bool logging_started(void); +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX + // currently only DataFlash_File support this: + void flush(void); +#endif + protected: void Log_Fill_Format(const struct LogStructure *structure, struct log_Format &pkt); void Log_Write_Parameter(const AP_Param *ap, const AP_Param::ParamToken &token, diff --git a/libraries/DataFlash/DataFlash_Backend.h b/libraries/DataFlash/DataFlash_Backend.h index 35588a6261..5f384e9317 100644 --- a/libraries/DataFlash/DataFlash_Backend.h +++ b/libraries/DataFlash/DataFlash_Backend.h @@ -68,6 +68,11 @@ public: void Log_Fill_Format(const struct LogStructure *structure, struct log_Format &pkt); +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX + // currently only DataFlash_File support this: + virtual void flush(void) { } +#endif + protected: DataFlash_Class &_front; diff --git a/libraries/DataFlash/DataFlash_File.cpp b/libraries/DataFlash/DataFlash_File.cpp index 653746e164..9134ada8ec 100644 --- a/libraries/DataFlash/DataFlash_File.cpp +++ b/libraries/DataFlash/DataFlash_File.cpp @@ -608,6 +608,25 @@ void DataFlash_File::ListAvailableLogs(AP_HAL::BetterStream *port) port->println(); } +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX +void DataFlash_File::flush(void) +{ + uint16_t _tail; + uint32_t tnow = hal.scheduler->micros(); + hal.scheduler->suspend_timer_procs(); + while (_write_fd != -1 && _initialised && !_open_error && + BUF_AVAILABLE(_writebuf)) { + // convince the IO timer that it really is OK to write out + // less than _writebuf_chunk bytes: + _last_write_time = tnow - 2000000; + _io_timer(); + } + hal.scheduler->resume_timer_procs(); + if (_write_fd != -1) { + ::fsync(_write_fd); + } +} +#endif void DataFlash_File::_io_timer(void) { diff --git a/libraries/DataFlash/DataFlash_File.h b/libraries/DataFlash/DataFlash_File.h index 438aea8909..6bd2521190 100644 --- a/libraries/DataFlash/DataFlash_File.h +++ b/libraries/DataFlash/DataFlash_File.h @@ -53,6 +53,10 @@ public: void ShowDeviceInfo(AP_HAL::BetterStream *port); void ListAvailableLogs(AP_HAL::BetterStream *port); +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX + void flush(void); +#endif + private: int _write_fd; int _read_fd;