AP_Logger_Block.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. AP_Logger logging - block oriented variant
  3. */
  4. #pragma once
  5. #include "AP_Logger_Backend.h"
  6. class AP_Logger_Block : public AP_Logger_Backend {
  7. public:
  8. AP_Logger_Block(AP_Logger &front, LoggerMessageWriter_DFLogStart *writer);
  9. virtual void Init(void) override;
  10. virtual bool CardInserted(void) const override = 0;
  11. // erase handling
  12. void EraseAll() override;
  13. bool NeedPrep(void) override;
  14. void Prep() override;
  15. void PrepForArming() override;
  16. // high level interface
  17. uint16_t find_last_log() override;
  18. void get_log_boundaries(uint16_t log_num, uint32_t & start_page, uint32_t & end_page) override;
  19. void get_log_info(uint16_t log_num, uint32_t &size, uint32_t &time_utc) override;
  20. int16_t get_log_data(uint16_t log_num, uint16_t page, uint32_t offset, uint16_t len, uint8_t *data) override WARN_IF_UNUSED;
  21. uint16_t get_num_logs() override;
  22. uint16_t start_new_log(void) override;
  23. uint32_t bufferspace_available() override;
  24. void stop_logging(void) override { log_write_started = false; }
  25. bool logging_enabled() const override { return true; }
  26. bool logging_failed() const override { return false; }
  27. bool logging_started(void) const override { return log_write_started; }
  28. protected:
  29. /* Write a block of data at current offset */
  30. bool _WritePrioritisedBlock(const void *pBuffer, uint16_t size, bool is_critical) override;
  31. private:
  32. /*
  33. functions implemented by the board specific backends
  34. */
  35. virtual void BufferToPage(uint32_t PageAdr) = 0;
  36. virtual void PageToBuffer(uint32_t PageAdr) = 0;
  37. virtual void SectorErase(uint32_t PageAdr) = 0;
  38. virtual void StartErase() = 0;
  39. virtual bool InErase() = 0;
  40. struct PACKED PageHeader {
  41. uint32_t FilePage;
  42. uint16_t FileNumber;
  43. };
  44. HAL_Semaphore_Recursive sem;
  45. ByteBuffer writebuf;
  46. // state variables
  47. uint16_t df_Read_BufferIdx;
  48. uint32_t df_PageAdr;
  49. uint32_t df_Read_PageAdr;
  50. uint16_t df_FileNumber;
  51. uint32_t df_FilePage;
  52. // offset from adding FMT messages to log data
  53. bool adding_fmt_headers;
  54. // are we waiting on an erase to finish?
  55. bool erase_started;
  56. // read size bytes of data to a page. The caller must ensure that
  57. // the data fits within the page, otherwise it will wrap to the
  58. // start of the page
  59. bool BlockRead(uint16_t IntPageAdr, void *pBuffer, uint16_t size);
  60. // erase handling
  61. bool NeedErase(void);
  62. // internal high level functions
  63. int16_t get_log_data_raw(uint16_t log_num, uint32_t page, uint32_t offset, uint16_t len, uint8_t *data) WARN_IF_UNUSED;
  64. void StartRead(uint32_t PageAdr);
  65. uint32_t find_last_page(void);
  66. uint32_t find_last_page_of_log(uint16_t log_number);
  67. bool check_wrapped(void);
  68. void StartWrite(uint32_t PageAdr);
  69. void FinishWrite(void);
  70. // Read methods
  71. bool ReadBlock(void *pBuffer, uint16_t size);
  72. // file numbers
  73. void SetFileNumber(uint16_t FileNumber);
  74. uint16_t GetFileNumber();
  75. void _print_log_formats(AP_HAL::BetterStream *port);
  76. // callback on IO thread
  77. void io_timer(void);
  78. protected:
  79. // page handling
  80. uint32_t df_PageSize;
  81. uint16_t df_PagePerSector;
  82. uint32_t df_NumPages;
  83. bool log_write_started;
  84. static const uint16_t page_size_max = 256;
  85. uint8_t *buffer;
  86. bool WritesOK() const override;
  87. };