LCOV - code coverage report
Current view: top level - StorageManager - StorageManager.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 5 5 100.0 %
Date: 2026-09-09 11:02:08 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :    Please contribute your ideas! See https://ardupilot.club/dev for details
       3             : 
       4             :    This program is free software: you can redistribute it and/or modify
       5             :    it under the terms of the GNU General Public License as published by
       6             :    the Free Software Foundation, either version 3 of the License, or
       7             :    (at your option) any later version.
       8             : 
       9             :    This program is distributed in the hope that it will be useful,
      10             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :    GNU General Public License for more details.
      13             : 
      14             :    You should have received a copy of the GNU General Public License
      15             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : /*
      18             :   Management for hal.storage to allow for backwards compatible mapping
      19             :   of storage offsets to available storage
      20             :  */
      21             : #pragma once
      22             : 
      23             : #include <AP_HAL/AP_HAL.h>
      24             : #include <AP_BoardConfig/AP_BoardConfig_config.h>
      25             : 
      26             : /*
      27             :   use just one area per storage type for boards with 4k of
      28             :   storage. Use larger areas for other boards
      29             :  */
      30             : #if HAL_STORAGE_SIZE >= 32768
      31             : #define STORAGE_NUM_AREAS 18
      32             : #elif HAL_STORAGE_SIZE >= 16384
      33             : #define STORAGE_NUM_AREAS 15
      34             : #elif HAL_STORAGE_SIZE >= 15360 && defined(HAL_NUM_CAN_IFACES)
      35             : #define STORAGE_NUM_AREAS 12
      36             : #elif HAL_STORAGE_SIZE >= 15360
      37             : #define STORAGE_NUM_AREAS 11
      38             : #elif HAL_STORAGE_SIZE >= 8192
      39             : #define STORAGE_NUM_AREAS 10
      40             : #elif HAL_STORAGE_SIZE >= 4096
      41             : #define STORAGE_NUM_AREAS 4
      42             : #elif HAL_STORAGE_SIZE > 0
      43             : #define STORAGE_NUM_AREAS 1
      44             : #else
      45             : #error "Unsupported storage size"
      46             : #endif
      47             : 
      48             : /*
      49             :   The StorageManager holds the layout of non-volatile storage
      50             :  */
      51             : class StorageManager {
      52             :     friend class StorageAccess;
      53             : public:
      54             :     enum StorageType {
      55             :         StorageParam   = 0,
      56             :         StorageFence   = 1,
      57             :         StorageRally   = 2,
      58             :         StorageMission = 3,
      59             :         StorageKeys    = 4,
      60             :         StorageBindInfo= 5,
      61             :         StorageCANDNA  = 6,
      62             :         StorageParamBak = 7
      63             :     };
      64             : 
      65             :     // erase whole of storage
      66             :     static void erase(void);
      67             : 
      68      133761 :     static bool storage_failed(void) {
      69      133761 :         return last_io_failed;
      70             :     }
      71             : 
      72             : private:
      73             :     static bool last_io_failed;
      74             : 
      75             :     struct StorageArea {
      76             :         StorageType type;
      77             :         uint16_t    offset;
      78             :         uint16_t    length;
      79             :     };
      80             : 
      81             :     // available layouts
      82             :     static const StorageArea layout[STORAGE_NUM_AREAS];
      83             : };
      84             : 
      85             : /*
      86             :   A StorageAccess object allows access to one type of storage
      87             : 
      88             :   NOTE: this object may be declared on the stack, so it will not be
      89             :   zero initialised
      90             :  */
      91             : class StorageAccess {
      92             : public:
      93             :     // constructor
      94             :     StorageAccess(StorageManager::StorageType _type);
      95             : 
      96             :     // return total size of this accessor
      97    73507452 :     uint16_t size(void) const { return total_size; }
      98             : 
      99             :     // base access via block functions
     100             :     bool read_block(void *dst, uint16_t src, size_t n) const;
     101             :     bool write_block(uint16_t dst, const void* src, size_t n) const;    
     102             : 
     103             :     // helper functions
     104             :     uint8_t  read_byte(uint16_t loc) const;
     105       39923 :     uint8_t  read_uint8(uint16_t loc) const { return read_byte(loc); }
     106             :     uint16_t read_uint16(uint16_t loc) const;
     107             :     uint32_t read_uint32(uint16_t loc) const;
     108             :     float read_float(uint16_t loc) const;
     109             : 
     110             :     void write_byte(uint16_t loc, uint8_t value) const;
     111        3710 :     void write_uint8(uint16_t loc, uint8_t value) const { return write_byte(loc, value); }
     112             :     void write_uint16(uint16_t loc, uint16_t value) const;
     113             :     void write_uint32(uint16_t loc, uint32_t value) const;
     114             :     void write_float(uint16_t loc, float value) const;
     115             : 
     116             :     // copy from one storage area to another
     117             :     bool copy_area(const StorageAccess &source) const;
     118             : 
     119             :     // attach a storage file from microSD
     120             :     bool attach_file(const char *fname, uint16_t size_kbyte);
     121             : 
     122             : private:
     123             :     const StorageManager::StorageType type;
     124             :     uint16_t total_size;
     125             : 
     126             : #if AP_SDCARD_STORAGE_ENABLED
     127             :     /*
     128             :       support for storage regions on microSD. Only the StorageMission
     129             :       for now
     130             :      */
     131             :     struct FileStorage {
     132             :         HAL_Semaphore sem;
     133             :         int fd;
     134             :         uint8_t *buffer;
     135             :         uint32_t bufsize;
     136             :         uint32_t last_clean_ms;
     137             :         uint32_t last_io_fail_ms;
     138             :         // each bit of the dirty mask covers 1k of data
     139             :         uint64_t dirty_mask;
     140             :     } *file;
     141             : 
     142             :     void flush_file(void);
     143             : #endif
     144             : };

Generated by: LCOV version 1.14