2020-03-21 02:19:15 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "AP_Filesystem_backend.h"
|
2021-04-07 22:11:29 -03:00
|
|
|
#include <AP_Common/ExpandingString.h>
|
2020-03-21 02:19:15 -03:00
|
|
|
|
|
|
|
#include <AP_Param/AP_Param.h>
|
|
|
|
|
|
|
|
class AP_Filesystem_Param : public AP_Filesystem_Backend
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// functions that closely match the equivalent posix calls
|
2022-03-14 11:33:32 -03:00
|
|
|
int open(const char *fname, int flags, bool allow_absolute_paths = false) override;
|
2020-03-21 02:19:15 -03:00
|
|
|
int close(int fd) override;
|
2020-03-29 22:53:29 -03:00
|
|
|
int32_t read(int fd, void *buf, uint32_t count) override;
|
|
|
|
int32_t lseek(int fd, int32_t offset, int whence) override;
|
2020-03-21 02:19:15 -03:00
|
|
|
int stat(const char *pathname, struct stat *stbuf) override;
|
2021-04-07 22:11:29 -03:00
|
|
|
int32_t write(int fd, const void *buf, uint32_t count) override;
|
2020-03-21 02:19:15 -03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// we maintain two cursors per open file to minimise seeking
|
|
|
|
// when filling in gaps
|
|
|
|
static constexpr uint8_t num_cursors = 2;
|
|
|
|
|
|
|
|
// only allow up to 4 files at a time
|
|
|
|
static constexpr uint8_t max_open_file = 4;
|
|
|
|
|
|
|
|
// maximum size of one packed parameter
|
2020-04-18 02:26:41 -03:00
|
|
|
static constexpr uint8_t max_pack_len = AP_MAX_NAME_SIZE + 2 + 4 + 3;
|
2020-03-21 02:19:15 -03:00
|
|
|
|
2021-04-07 22:11:29 -03:00
|
|
|
static constexpr uint16_t pmagic = 0x671b;
|
|
|
|
|
2020-04-18 02:26:41 -03:00
|
|
|
// header at front of the file
|
|
|
|
struct header {
|
2021-04-07 22:11:29 -03:00
|
|
|
uint16_t magic = pmagic;
|
2020-04-18 02:26:41 -03:00
|
|
|
uint16_t num_params;
|
2021-04-07 22:11:29 -03:00
|
|
|
uint16_t total_params; // for upload this is total file length
|
2020-04-18 02:26:41 -03:00
|
|
|
};
|
2020-04-13 00:55:04 -03:00
|
|
|
|
2020-03-21 02:19:15 -03:00
|
|
|
struct cursor {
|
|
|
|
AP_Param::ParamToken token;
|
|
|
|
uint32_t token_ofs;
|
|
|
|
char last_name[AP_MAX_NAME_SIZE+1];
|
|
|
|
uint8_t trailer_len;
|
|
|
|
uint8_t trailer[max_pack_len];
|
2020-04-18 02:26:41 -03:00
|
|
|
uint16_t idx;
|
2020-03-21 02:19:15 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rfile {
|
|
|
|
bool open;
|
2020-04-18 02:26:41 -03:00
|
|
|
uint16_t read_size;
|
|
|
|
uint16_t start;
|
|
|
|
uint16_t count;
|
2020-03-21 02:19:15 -03:00
|
|
|
uint32_t file_ofs;
|
2021-04-13 21:07:37 -03:00
|
|
|
uint32_t file_size;
|
2020-03-21 02:19:15 -03:00
|
|
|
struct cursor *cursors;
|
2021-04-07 22:11:29 -03:00
|
|
|
ExpandingString *writebuf; // for upload
|
2020-03-21 02:19:15 -03:00
|
|
|
} file[max_open_file];
|
|
|
|
|
2020-04-18 02:26:41 -03:00
|
|
|
bool token_seek(const struct rfile &r, const uint32_t data_ofs, struct cursor &c);
|
|
|
|
uint8_t pack_param(const struct rfile &r, struct cursor &c, uint8_t *buf);
|
|
|
|
bool check_file_name(const char *fname);
|
2021-04-07 22:11:29 -03:00
|
|
|
|
|
|
|
// finish uploading parameters
|
|
|
|
bool finish_upload(const rfile &r);
|
|
|
|
bool param_upload_parse(const rfile &r, bool &need_retry);
|
2020-03-21 02:19:15 -03:00
|
|
|
};
|