2021-04-04 23:46:49 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
ArduPilot filesystem interface for mission/fence/rally
|
|
|
|
*/
|
2022-08-14 19:14:57 -03:00
|
|
|
|
|
|
|
#include "AP_Filesystem_config.h"
|
|
|
|
|
|
|
|
#if AP_FILESYSTEM_MISSION_ENABLED
|
|
|
|
|
2021-04-04 23:46:49 -03:00
|
|
|
#include "AP_Filesystem.h"
|
|
|
|
#include "AP_Filesystem_Mission.h"
|
|
|
|
#include <AP_Mission/AP_Mission.h>
|
|
|
|
#include <AC_Fence/AC_Fence.h>
|
|
|
|
#include <AP_Rally/AP_Rally.h>
|
|
|
|
#include <GCS_MAVLink/MissionItemProtocol_Rally.h>
|
|
|
|
#include <GCS_MAVLink/MissionItemProtocol_Fence.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
extern int errno;
|
|
|
|
|
2021-04-05 03:53:49 -03:00
|
|
|
#define IDLE_TIMEOUT_MS 30000
|
|
|
|
|
2022-03-14 11:33:32 -03:00
|
|
|
int AP_Filesystem_Mission::open(const char *fname, int flags, bool allow_absolute_paths)
|
2021-04-04 23:46:49 -03:00
|
|
|
{
|
|
|
|
enum MAV_MISSION_TYPE mtype;
|
|
|
|
|
|
|
|
if (!check_file_name(fname, mtype)) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint8_t idx;
|
2021-04-05 03:53:49 -03:00
|
|
|
bool readonly = ((flags & O_ACCMODE) == O_RDONLY);
|
|
|
|
uint32_t now = AP_HAL::millis();
|
2021-04-04 23:46:49 -03:00
|
|
|
for (idx=0; idx<max_open_file; idx++) {
|
2021-04-05 03:53:49 -03:00
|
|
|
if (now - file[idx].last_op_ms > IDLE_TIMEOUT_MS) {
|
|
|
|
file[idx].open = false;
|
|
|
|
delete file[idx].writebuf;
|
|
|
|
file[idx].writebuf = nullptr;
|
|
|
|
}
|
|
|
|
if (!readonly && file[idx].writebuf != nullptr) {
|
|
|
|
// only one upload at a time
|
|
|
|
return -1;
|
|
|
|
}
|
2021-04-04 23:46:49 -03:00
|
|
|
if (!file[idx].open) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (idx == max_open_file) {
|
|
|
|
errno = ENFILE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct rfile &r = file[idx];
|
|
|
|
r.file_ofs = 0;
|
|
|
|
r.open = true;
|
|
|
|
r.mtype = mtype;
|
|
|
|
r.num_items = get_num_items(r.mtype);
|
2021-04-05 03:53:49 -03:00
|
|
|
if (!readonly) {
|
|
|
|
// setup for upload
|
|
|
|
r.writebuf = new ExpandingString();
|
|
|
|
} else {
|
|
|
|
r.writebuf = nullptr;
|
|
|
|
}
|
|
|
|
r.last_op_ms = now;
|
2021-04-04 23:46:49 -03:00
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AP_Filesystem_Mission::close(int fd)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= max_open_file || !file[fd].open) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct rfile &r = file[fd];
|
|
|
|
r.open = false;
|
2021-04-05 03:53:49 -03:00
|
|
|
if (r.writebuf != nullptr) {
|
|
|
|
bool ok = finish_upload(r);
|
|
|
|
delete r.writebuf;
|
|
|
|
r.writebuf = nullptr;
|
|
|
|
if (!ok) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 23:46:49 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
packed format:
|
|
|
|
file header:
|
|
|
|
uint16_t magic = 0x671b
|
|
|
|
uint16_t data_type MAV_MISSION_TYPE_*
|
|
|
|
uint32_t total_items
|
|
|
|
|
|
|
|
per-entry is mavlink packed item
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
read from file handle
|
|
|
|
*/
|
|
|
|
int32_t AP_Filesystem_Mission::read(int fd, void *buf, uint32_t count)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= max_open_file || !file[fd].open) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rfile &r = file[fd];
|
2021-04-05 03:53:49 -03:00
|
|
|
|
|
|
|
if (r.writebuf != nullptr) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.last_op_ms = AP_HAL::millis();
|
|
|
|
|
2021-04-04 23:46:49 -03:00
|
|
|
size_t header_total = 0;
|
|
|
|
uint8_t *ubuf = (uint8_t *)buf;
|
|
|
|
|
|
|
|
if (r.file_ofs < sizeof(struct header)) {
|
2021-04-20 04:18:05 -03:00
|
|
|
struct header hdr {};
|
2021-04-04 23:46:49 -03:00
|
|
|
hdr.data_type = uint16_t(r.mtype);
|
|
|
|
hdr.num_items = r.num_items;
|
|
|
|
uint8_t n = MIN(sizeof(hdr) - r.file_ofs, count);
|
|
|
|
const uint8_t *b = (const uint8_t *)&hdr;
|
|
|
|
memcpy(ubuf, &b[r.file_ofs], n);
|
|
|
|
count -= n;
|
|
|
|
header_total += n;
|
|
|
|
r.file_ofs += n;
|
|
|
|
ubuf += n;
|
|
|
|
if (count == 0) {
|
|
|
|
return header_total;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t data_ofs = r.file_ofs - sizeof(struct header);
|
2021-04-20 04:18:05 -03:00
|
|
|
mavlink_mission_item_int_t item {};
|
2021-04-04 23:46:49 -03:00
|
|
|
const uint8_t item_size = MAVLINK_MSG_ID_MISSION_ITEM_INT_LEN;
|
|
|
|
uint32_t item_ofs = data_ofs % item_size;
|
|
|
|
uint32_t total = 0;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
uint32_t item_idx = data_ofs / item_size;
|
|
|
|
|
|
|
|
const uint8_t *ibuf = (const uint8_t *)&item;
|
|
|
|
if (!get_item(item_idx, r.mtype, item)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const uint8_t n = MIN(item_size - item_ofs, count);
|
|
|
|
memcpy(ubuf, &ibuf[item_ofs], n);
|
|
|
|
ubuf += n;
|
|
|
|
count -= n;
|
|
|
|
total += n;
|
|
|
|
item_ofs = 0;
|
|
|
|
data_ofs += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.file_ofs += total;
|
|
|
|
return total + header_total;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t AP_Filesystem_Mission::lseek(int fd, int32_t offset, int seek_from)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= max_open_file || !file[fd].open) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct rfile &r = file[fd];
|
|
|
|
switch (seek_from) {
|
|
|
|
case SEEK_SET:
|
|
|
|
r.file_ofs = offset;
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
r.file_ofs += offset;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return r.file_ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AP_Filesystem_Mission::stat(const char *name, struct stat *stbuf)
|
|
|
|
{
|
|
|
|
enum MAV_MISSION_TYPE mtype;
|
|
|
|
if (!check_file_name(name, mtype)) {
|
|
|
|
errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(stbuf, 0, sizeof(*stbuf));
|
|
|
|
// give fixed size to avoid needing to scan entire file
|
|
|
|
stbuf->st_size = 1024*1024;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
check for the right file name
|
|
|
|
*/
|
|
|
|
bool AP_Filesystem_Mission::check_file_name(const char *name, enum MAV_MISSION_TYPE &mtype)
|
|
|
|
{
|
|
|
|
if (strcmp(name, "mission.dat") == 0) {
|
|
|
|
mtype = MAV_MISSION_TYPE_MISSION;
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-21 09:58:19 -03:00
|
|
|
#if AP_FENCE_ENABLED
|
2021-04-04 23:46:49 -03:00
|
|
|
if (strcmp(name, "fence.dat") == 0) {
|
|
|
|
mtype = MAV_MISSION_TYPE_FENCE;
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-21 09:58:19 -03:00
|
|
|
#endif
|
2022-09-21 10:13:20 -03:00
|
|
|
#if HAL_RALLY_ENABLED
|
2021-04-04 23:46:49 -03:00
|
|
|
if (strcmp(name, "rally.dat") == 0) {
|
|
|
|
mtype = MAV_MISSION_TYPE_RALLY;
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-21 10:13:20 -03:00
|
|
|
#endif
|
2021-04-04 23:46:49 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
get one item
|
|
|
|
*/
|
2021-04-20 04:18:05 -03:00
|
|
|
bool AP_Filesystem_Mission::get_item(uint32_t idx, enum MAV_MISSION_TYPE mtype, mavlink_mission_item_int_t &item) const
|
2021-04-04 23:46:49 -03:00
|
|
|
{
|
|
|
|
switch (mtype) {
|
|
|
|
case MAV_MISSION_TYPE_MISSION: {
|
|
|
|
auto *mission = AP::mission();
|
|
|
|
if (!mission) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mission->get_item(idx, item);
|
|
|
|
}
|
2022-07-19 08:33:12 -03:00
|
|
|
#if AP_FENCE_ENABLED
|
2021-04-04 23:46:49 -03:00
|
|
|
case MAV_MISSION_TYPE_FENCE:
|
|
|
|
return MissionItemProtocol_Fence::get_item_as_mission_item(idx, item);
|
2022-03-04 12:37:59 -04:00
|
|
|
#endif
|
2022-09-21 10:13:20 -03:00
|
|
|
#if HAL_RALLY_ENABLED
|
2021-04-04 23:46:49 -03:00
|
|
|
case MAV_MISSION_TYPE_RALLY:
|
|
|
|
return MissionItemProtocol_Rally::get_item_as_mission_item(idx, item);
|
2022-09-21 10:13:20 -03:00
|
|
|
#endif
|
2021-04-04 23:46:49 -03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get number of items
|
|
|
|
uint32_t AP_Filesystem_Mission::get_num_items(enum MAV_MISSION_TYPE mtype) const
|
|
|
|
{
|
|
|
|
switch (mtype) {
|
|
|
|
case MAV_MISSION_TYPE_MISSION: {
|
|
|
|
auto *mission = AP::mission();
|
|
|
|
if (!mission) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mission->num_commands();
|
|
|
|
}
|
|
|
|
|
|
|
|
case MAV_MISSION_TYPE_FENCE: {
|
2022-07-19 08:33:12 -03:00
|
|
|
#if AP_FENCE_ENABLED
|
2021-04-04 23:46:49 -03:00
|
|
|
auto *fence = AP::fence();
|
|
|
|
if (fence == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fence->polyfence().num_stored_items();
|
2022-03-04 12:37:59 -04:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2021-04-04 23:46:49 -03:00
|
|
|
}
|
|
|
|
|
2022-09-21 10:13:20 -03:00
|
|
|
#if HAL_RALLY_ENABLED
|
2021-04-04 23:46:49 -03:00
|
|
|
case MAV_MISSION_TYPE_RALLY: {
|
|
|
|
auto *rally = AP::rally();
|
|
|
|
if (rally == nullptr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return rally->get_rally_total();
|
|
|
|
}
|
2022-09-21 10:13:20 -03:00
|
|
|
#endif
|
|
|
|
|
2021-04-04 23:46:49 -03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-05 03:53:49 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
support mission upload
|
|
|
|
*/
|
|
|
|
int32_t AP_Filesystem_Mission::write(int fd, const void *buf, uint32_t count)
|
|
|
|
{
|
|
|
|
if (fd < 0 || fd >= max_open_file || !file[fd].open) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
struct rfile &r = file[fd];
|
|
|
|
if (r.writebuf == nullptr) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
r.last_op_ms = AP_HAL::millis();
|
|
|
|
struct header hdr;
|
|
|
|
if (r.file_ofs == 0 && count >= sizeof(hdr)) {
|
|
|
|
// pre-expand the buffer to the full size when we get the header
|
|
|
|
memcpy(&hdr, buf, sizeof(hdr));
|
|
|
|
if (hdr.num_items < 0xFFFF) {
|
|
|
|
const uint32_t flen = sizeof(hdr) + hdr.num_items * MAVLINK_MSG_ID_MISSION_ITEM_INT_LEN;
|
|
|
|
if (flen > r.writebuf->get_length()) {
|
|
|
|
if (!r.writebuf->append(nullptr, flen - r.writebuf->get_length())) {
|
|
|
|
// not enough memory
|
2021-04-20 04:18:05 -03:00
|
|
|
errno = ENOSPC;
|
2021-04-05 03:53:49 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r.file_ofs + count > r.writebuf->get_length()) {
|
|
|
|
if (!r.writebuf->append(nullptr, r.file_ofs + count - r.writebuf->get_length())) {
|
2021-04-20 04:18:05 -03:00
|
|
|
errno = ENOSPC;
|
2021-04-05 03:53:49 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint8_t *b = (uint8_t *)r.writebuf->get_writeable_string();
|
|
|
|
memcpy(&b[r.file_ofs], buf, count);
|
|
|
|
r.file_ofs += count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:18:56 -03:00
|
|
|
// see if a block of memory is all zero
|
|
|
|
bool AP_Filesystem_Mission::all_zero(const uint8_t *b, uint8_t len) const
|
|
|
|
{
|
|
|
|
while (len--) {
|
|
|
|
if (*b++ != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-05 03:53:49 -03:00
|
|
|
/*
|
|
|
|
finish mission upload
|
|
|
|
*/
|
|
|
|
bool AP_Filesystem_Mission::finish_upload(const rfile &r)
|
|
|
|
{
|
|
|
|
const uint32_t flen = r.writebuf->get_length();
|
2021-04-07 22:11:29 -03:00
|
|
|
const uint8_t *b = (const uint8_t *)r.writebuf->get_string();
|
2021-04-05 03:53:49 -03:00
|
|
|
struct header hdr;
|
|
|
|
if (flen < sizeof(hdr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const uint8_t item_size = MAVLINK_MSG_ID_MISSION_ITEM_INT_LEN;
|
|
|
|
const uint32_t nitems = (flen - sizeof(hdr)) / item_size;
|
|
|
|
|
|
|
|
memcpy(&hdr, b, sizeof(hdr));
|
|
|
|
if (hdr.magic != mission_magic) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nitems != hdr.num_items) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-06 20:18:56 -03:00
|
|
|
// if any item is all zeros then reject, it means client didn't
|
|
|
|
// fill in the whole file
|
|
|
|
for (uint32_t i=0; i<nitems; i++) {
|
|
|
|
const uint8_t *b2 = b + sizeof(hdr) + i*item_size;
|
|
|
|
if (all_zero(b2, item_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 03:53:49 -03:00
|
|
|
auto *mission = AP::mission();
|
|
|
|
if (mission == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
WITH_SEMAPHORE(mission->get_semaphore());
|
2021-04-07 01:09:59 -03:00
|
|
|
if ((hdr.options & unsigned(Options::NO_CLEAR)) == 0) {
|
|
|
|
mission->clear();
|
|
|
|
}
|
2021-04-05 03:53:49 -03:00
|
|
|
for (uint32_t i=0; i<nitems; i++) {
|
2021-04-20 04:18:05 -03:00
|
|
|
mavlink_mission_item_int_t m {};
|
2021-04-05 03:53:49 -03:00
|
|
|
AP_Mission::Mission_Command cmd;
|
|
|
|
memcpy(&m, &b[sizeof(hdr)+i*item_size], item_size);
|
|
|
|
const MAV_MISSION_RESULT res = AP_Mission::mavlink_int_to_mission_cmd(m, cmd);
|
|
|
|
if (res != MAV_MISSION_ACCEPTED) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (cmd.id == MAV_CMD_DO_JUMP &&
|
|
|
|
(cmd.content.jump.target >= nitems || cmd.content.jump.target == 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-07 01:09:59 -03:00
|
|
|
uint16_t idx = i + hdr.start;
|
|
|
|
if (idx == mission->num_commands()) {
|
|
|
|
if (!mission->add_cmd(cmd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!mission->replace_cmd(idx, cmd)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-05 03:53:49 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-19 23:34:12 -03:00
|
|
|
|
2022-08-14 19:14:57 -03:00
|
|
|
#endif // AP_FILESYSTEM_MISSION_ENABLED
|