Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_backend.h
9448 views
/*1This program is free software: you can redistribute it and/or modify2it under the terms of the GNU General Public License as published by3the Free Software Foundation, either version 3 of the License, or4(at your option) any later version.56This program is distributed in the hope that it will be useful,7but WITHOUT ANY WARRANTY; without even the implied warranty of8MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9GNU General Public License for more details.1011You should have received a copy of the GNU General Public License12along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14/*15ArduPilot filesystem backend interface.16*/17#pragma once1819#include <stdint.h>20#include <AP_HAL/AP_HAL_Boards.h>2122#include "AP_Filesystem_config.h"2324#include <AP_InternalError/AP_InternalError.h>2526// returned structure from a load_file() call27class FileData {28public:29uint32_t length;30const uint8_t *data;3132FileData(void *_backend) :33backend(_backend) {}3435// destructor to free data36~FileData();37private:38const void *backend;39};4041class AP_Filesystem_Backend {4243public:44// functions that closely match the equivalent posix calls45virtual int open(const char *fname, int flags, bool allow_absolute_paths = false) {46return -1;47}48virtual int close(int fd) { return -1; }49virtual int32_t read(int fd, void *buf, uint32_t count) { return -1; }50virtual int32_t write(int fd, const void *buf, uint32_t count) { return -1; }51virtual int fsync(int fd) { return 0; }52virtual int32_t lseek(int fd, int32_t offset, int whence) { return -1; }53virtual int stat(const char *pathname, struct stat *stbuf) { return -1; }54virtual int unlink(const char *pathname) { return -1; }55virtual int mkdir(const char *pathname) { return -1; }56virtual void *opendir(const char *pathname) { return nullptr; }57virtual struct dirent *readdir(void *dirp) { return nullptr; }58virtual int closedir(void *dirp) { return -1; }59virtual int rename(const char *oldpath, const char *newpath) { return -1; }6061// return number of bytes that should be written before fsync for optimal62// streaming performance/robustness. if zero, any number can be written.63virtual uint32_t bytes_until_fsync(int fd) { return 0; }6465// return free disk space in bytes, -1 on error66virtual int64_t disk_free(const char *path) { return 0; }6768// return total disk space in bytes, -1 on error69virtual int64_t disk_space(const char *path) { return 0; }7071// set modification time on a file72virtual bool set_mtime(const char *filename, const uint32_t mtime_sec) { return false; }7374// retry mount of filesystem if needed75virtual bool retry_mount(void) { return true; }7677// unmount filesystem for reboot78virtual void unmount(void) {}7980enum class FormatStatus {81NOT_STARTED,82PENDING,83IN_PROGRESS,84SUCCESS,85FAILURE,86};8788// format sdcard. This is async, monitor get_format_status for progress89virtual bool format(void) { return false; }90virtual AP_Filesystem_Backend::FormatStatus get_format_status() const { return FormatStatus::NOT_STARTED; }9192/*93Load a file's contents into memory. Returned object must be `delete`d to94free the data. The data is guaranteed to be null-terminated such that it95can be treated as a string.96*/97virtual FileData *load_file(const char *filename);9899// unload data from load_file()100virtual void unload_file(FileData *fd);101102protected:103// return true if file operations are allowed104bool file_op_allowed(void) const;105};106107108#if CONFIG_HAL_BOARD == HAL_BOARD_SITL109#define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { INTERNAL_ERROR(AP_InternalError::error_t::flow_of_control); return retfail; } } while(0)110#else111#define FS_CHECK_ALLOWED(retfail) do { if (!file_op_allowed()) { return retfail; } } while(0)112#endif113114115