CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Filesystem/AP_Filesystem_Param.h
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
16
#pragma once
17
18
#include "AP_Filesystem_config.h"
19
20
#if AP_FILESYSTEM_PARAM_ENABLED
21
22
#include "AP_Filesystem_backend.h"
23
#include <AP_Common/ExpandingString.h>
24
25
#include <AP_Param/AP_Param.h>
26
27
class AP_Filesystem_Param : public AP_Filesystem_Backend
28
{
29
public:
30
// functions that closely match the equivalent posix calls
31
int open(const char *fname, int flags, bool allow_absolute_paths = false) override;
32
int close(int fd) override;
33
int32_t read(int fd, void *buf, uint32_t count) override;
34
int32_t lseek(int fd, int32_t offset, int whence) override;
35
int stat(const char *pathname, struct stat *stbuf) override;
36
int32_t write(int fd, const void *buf, uint32_t count) override;
37
38
private:
39
// we maintain two cursors per open file to minimise seeking
40
// when filling in gaps
41
static constexpr uint8_t num_cursors = 2;
42
43
// only allow up to 4 files at a time
44
static constexpr uint8_t max_open_file = 4;
45
46
// maximum size of one packed parameter and default value
47
static constexpr uint8_t max_pack_len = AP_MAX_NAME_SIZE + 2 + 4 + 4 + 3;
48
49
// Support both protocol versions
50
static constexpr uint16_t pmagic = 0x671b;
51
static constexpr uint16_t pmagic_with_default = 0x671c;
52
53
// header at front of the file
54
struct header {
55
uint16_t magic = pmagic;
56
uint16_t num_params;
57
uint16_t total_params; // for upload this is total file length
58
};
59
60
struct cursor {
61
AP_Param::ParamToken token;
62
uint32_t token_ofs;
63
char last_name[AP_MAX_NAME_SIZE+1];
64
uint8_t trailer_len;
65
uint8_t trailer[max_pack_len];
66
uint16_t idx;
67
};
68
69
struct rfile {
70
bool open;
71
bool with_defaults;
72
uint16_t read_size;
73
uint16_t start;
74
uint16_t count;
75
uint32_t file_ofs;
76
uint32_t file_size;
77
struct cursor *cursors;
78
ExpandingString *writebuf; // for upload
79
} file[max_open_file];
80
81
bool token_seek(const struct rfile &r, const uint32_t data_ofs, struct cursor &c);
82
uint8_t pack_param(const struct rfile &r, struct cursor &c, uint8_t *buf);
83
bool check_file_name(const char *fname);
84
85
// finish uploading parameters
86
bool finish_upload(const rfile &r);
87
bool param_upload_parse(const rfile &r, bool &need_retry);
88
};
89
90
#endif // AP_FILESYSTEM_PARAM_ENABLED
91
92