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_ESP32.cpp
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
This program is distributed in the hope that it will be useful,
7
but WITHOUT ANY WARRANTY; without even the implied warranty of
8
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
GNU General Public License for more details.
10
You should have received a copy of the GNU General Public License
11
along with this program. If not, see <http://www.gnu.org/licenses/>.
12
*/
13
14
/*
15
ArduPilot filesystem interface for esp32 systems
16
*/
17
#include "AP_Filesystem.h"
18
#include <AP_HAL/AP_HAL.h>
19
20
#if AP_FILESYSTEM_ESP32_ENABLED
21
22
#define FSDEBUG 0
23
24
#include <utime.h>
25
26
extern const AP_HAL::HAL& hal;
27
28
int AP_Filesystem_ESP32::open(const char *fname, int flags, bool allow_absolute_paths)
29
{
30
#if FSDEBUG
31
printf("DO open %s \n", fname);
32
#endif
33
// we automatically add O_CLOEXEC as we always want it for ArduPilot FS usage
34
return ::open(fname, flags | O_TRUNC | O_CLOEXEC, 0666);
35
}
36
37
int AP_Filesystem_ESP32::close(int fd)
38
{
39
#if FSDEBUG
40
printf("DO close \n");
41
#endif
42
return ::close(fd);
43
}
44
45
int32_t AP_Filesystem_ESP32::read(int fd, void *buf, uint32_t count)
46
{
47
#if FSDEBUG
48
printf("DO read \n");
49
#endif
50
return ::read(fd, buf, count);
51
}
52
53
int32_t AP_Filesystem_ESP32::write(int fd, const void *buf, uint32_t count)
54
{
55
#if FSDEBUG
56
printf("DO write \n");
57
#endif
58
return ::write(fd, buf, count);
59
}
60
61
int AP_Filesystem_ESP32::fsync(int fd)
62
{
63
#if FSDEBUG
64
printf("DO fsync \n");
65
#endif
66
return ::fsync(fd);
67
}
68
69
int32_t AP_Filesystem_ESP32::lseek(int fd, int32_t offset, int seek_from)
70
{
71
#if FSDEBUG
72
printf("DO lseek \n");
73
#endif
74
return ::lseek(fd, offset, seek_from);
75
}
76
77
int AP_Filesystem_ESP32::stat(const char *pathname, struct stat *stbuf)
78
{
79
#if FSDEBUG
80
printf("DO stat %s \n", pathname);
81
#endif
82
return ::stat(pathname, stbuf);
83
}
84
85
int AP_Filesystem_ESP32::unlink(const char *pathname)
86
{
87
#if FSDEBUG
88
printf("DO unlink %s \n", pathname);
89
#endif
90
return ::unlink(pathname);
91
}
92
93
int AP_Filesystem_ESP32::rename(const char *oldpath, const char *newpath)
94
{
95
#if FSDEBUG
96
printf("DO rename %s \n", oldpath, newpath);
97
#endif
98
return ::rename(oldpath, newpath);
99
}
100
101
int AP_Filesystem_ESP32::mkdir(const char *pathname)
102
{
103
#if FSDEBUG
104
printf("DO mkdir %s \n", pathname);
105
#endif
106
return ::mkdir(pathname, 0777);
107
}
108
109
void* AP_Filesystem_ESP32::opendir(const char *pathname)
110
{
111
#if FSDEBUG
112
printf("DO opendir %s \n", pathname);
113
#endif
114
115
return (void*)::opendir(pathname);
116
// return NULL;
117
}
118
119
struct dirent *AP_Filesystem_ESP32::readdir(void *dirp)
120
{
121
#if FSDEBUG
122
printf("DO readdir \n");
123
#endif
124
return ::readdir((DIR*)dirp);
125
// return NULL;
126
}
127
128
int AP_Filesystem_ESP32::closedir(void *dirp)
129
{
130
#if FSDEBUG
131
printf("DO closedir \n");
132
#endif
133
134
return ::closedir((DIR*)dirp);
135
// return 0;
136
}
137
138
// return free disk space in bytes
139
int64_t AP_Filesystem_ESP32::disk_free(const char *path)
140
{
141
142
#if FSDEBUG
143
printf("DO free disk %s \n", path);
144
#endif
145
FATFS *fs;
146
DWORD fre_clust, fre_sect;
147
148
/* Get volume information and free clusters of sdcard */
149
auto res = f_getfree("/SDCARD/", &fre_clust, &fs);
150
if (res) {
151
return -1;
152
}
153
154
/* Get total sectors and free sectors */
155
fre_sect = fre_clust * fs->csize;
156
157
int64_t tmp_free_bytes = fre_sect * FF_SS_SDCARD;
158
159
return tmp_free_bytes;
160
}
161
162
// return total disk space in bytes
163
int64_t AP_Filesystem_ESP32::disk_space(const char *path)
164
{
165
#if FSDEBUG
166
printf("DO usage disk %s \n", path);
167
#endif
168
FATFS *fs;
169
DWORD fre_clust, tot_sect;
170
171
/* Get volume information and free clusters of sdcard */
172
auto res = f_getfree("/SDCARD/", &fre_clust, &fs);
173
if (res) {
174
return -1;
175
}
176
177
/* Get total sectors and free sectors */
178
tot_sect = (fs->n_fatent - 2) * fs->csize;
179
180
int64_t tmp_total_bytes = tot_sect * FF_SS_SDCARD;
181
182
return tmp_total_bytes;
183
}
184
185
/*
186
set mtime on a file
187
*/
188
bool AP_Filesystem_ESP32::set_mtime(const char *filename, const uint32_t mtime_sec)
189
{
190
191
#if FSDEBUG
192
printf("DO time %s \n", filename);
193
#endif
194
struct utimbuf times {};
195
times.actime = mtime_sec;
196
times.modtime = mtime_sec;
197
198
return utime(filename, &times) == 0;
199
}
200
201
#endif // AP_FILESYSTEM_ESP32_ENABLED
202
203