2014-07-23 08:02:50 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
handle disk IO for terrain code
|
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:46 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
#include <AP_Math/AP_Math.h>
|
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2019-08-01 02:13:49 -03:00
|
|
|
#include <stdio.h>
|
2014-07-23 08:02:50 -03:00
|
|
|
#include "AP_Terrain.h"
|
|
|
|
|
2014-07-24 18:56:55 -03:00
|
|
|
#if AP_TERRAIN_AVAILABLE
|
2014-07-23 08:02:50 -03:00
|
|
|
|
2019-08-01 02:13:49 -03:00
|
|
|
#include <AP_Filesystem/AP_Filesystem.h>
|
2014-07-23 08:02:50 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
check for blocks that need to be read from disk
|
|
|
|
*/
|
|
|
|
void AP_Terrain::check_disk_read(void)
|
|
|
|
{
|
2015-09-22 19:31:17 -03:00
|
|
|
for (uint16_t i=0; i<cache_size; i++) {
|
2014-07-23 08:02:50 -03:00
|
|
|
if (cache[i].state == GRID_CACHE_DISKWAIT) {
|
|
|
|
disk_block.block = cache[i].grid;
|
|
|
|
disk_io_state = DiskIoWaitRead;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
check for blocks that need to be written to disk
|
|
|
|
*/
|
|
|
|
void AP_Terrain::check_disk_write(void)
|
|
|
|
{
|
2015-09-22 19:31:17 -03:00
|
|
|
for (uint16_t i=0; i<cache_size; i++) {
|
2014-07-23 08:02:50 -03:00
|
|
|
if (cache[i].state == GRID_CACHE_DIRTY) {
|
|
|
|
disk_block.block = cache[i].grid;
|
|
|
|
disk_io_state = DiskIoWaitWrite;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check if we need to do disk IO for grids.
|
|
|
|
*/
|
|
|
|
void AP_Terrain::schedule_disk_io(void)
|
|
|
|
{
|
2015-09-22 19:31:17 -03:00
|
|
|
if (enable == 0 || !allocate()) {
|
2014-07-23 08:02:50 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!timer_setup) {
|
|
|
|
timer_setup = true;
|
2015-05-24 20:24:11 -03:00
|
|
|
hal.scheduler->register_io_process(FUNCTOR_BIND_MEMBER(&AP_Terrain::io_timer, void));
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (disk_io_state) {
|
|
|
|
case DiskIoIdle:
|
|
|
|
// look for a block that needs reading or writing
|
|
|
|
check_disk_read();
|
|
|
|
if (disk_io_state == DiskIoIdle) {
|
|
|
|
// still idle, check for writes
|
|
|
|
check_disk_write();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DiskIoDoneRead: {
|
|
|
|
// a read has completed
|
|
|
|
int16_t cache_idx = find_io_idx(GRID_CACHE_DISKWAIT);
|
|
|
|
if (cache_idx != -1) {
|
|
|
|
if (disk_block.block.bitmap != 0) {
|
|
|
|
// when bitmap is zero we read an empty block
|
|
|
|
cache[cache_idx].grid = disk_block.block;
|
|
|
|
}
|
|
|
|
cache[cache_idx].state = GRID_CACHE_VALID;
|
2015-11-19 23:14:56 -04:00
|
|
|
cache[cache_idx].last_access_ms = AP_HAL::millis();
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
|
|
|
disk_io_state = DiskIoIdle;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DiskIoDoneWrite: {
|
|
|
|
// a write has completed
|
|
|
|
int16_t cache_idx = find_io_idx(GRID_CACHE_DIRTY);
|
|
|
|
if (cache_idx != -1) {
|
|
|
|
if (cache[cache_idx].grid.bitmap == disk_block.block.bitmap) {
|
|
|
|
// only mark valid if more grids haven't been added
|
|
|
|
cache[cache_idx].state = GRID_CACHE_VALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
disk_io_state = DiskIoIdle;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DiskIoWaitWrite:
|
|
|
|
case DiskIoWaitRead:
|
|
|
|
// waiting for io_timer()
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************
|
|
|
|
All the functions below this point run in the IO timer context, which
|
|
|
|
is a separate thread. The code uses the state machine controlled by
|
|
|
|
disk_io_state to manage who has access to the structures and to
|
|
|
|
prevent race conditions.
|
|
|
|
|
|
|
|
The IO timer context owns the data when disk_io_state is
|
|
|
|
DiskIoWaitWrite or DiskIoWaitRead. The main thread owns the data when
|
|
|
|
disk_io_state is DiskIoIdle, DiskIoDoneWrite or DiskIoDoneRead
|
|
|
|
|
|
|
|
All file operations are done by the IO thread.
|
|
|
|
*********************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
open the current degree file
|
|
|
|
*/
|
|
|
|
void AP_Terrain::open_file(void)
|
|
|
|
{
|
|
|
|
struct grid_block &block = disk_block.block;
|
|
|
|
if (fd != -1 &&
|
|
|
|
block.lat_degrees == file_lat_degrees &&
|
|
|
|
block.lon_degrees == file_lon_degrees) {
|
|
|
|
// already open on right file
|
|
|
|
return;
|
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
if (file_path == nullptr) {
|
2015-06-30 01:35:52 -03:00
|
|
|
const char* terrain_dir = hal.util->get_custom_terrain_directory();
|
2016-10-30 02:24:21 -03:00
|
|
|
if (terrain_dir == nullptr) {
|
2015-06-30 01:35:52 -03:00
|
|
|
terrain_dir = HAL_BOARD_TERRAIN_DIRECTORY;
|
2015-06-28 16:13:54 -03:00
|
|
|
}
|
2015-06-30 01:35:52 -03:00
|
|
|
if (asprintf(&file_path, "%s/NxxExxx.DAT", terrain_dir) <= 0) {
|
|
|
|
io_failure = true;
|
2016-10-30 02:24:21 -03:00
|
|
|
file_path = nullptr;
|
2015-06-30 01:35:52 -03:00
|
|
|
return;
|
2015-06-28 16:13:54 -03:00
|
|
|
}
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
2016-10-30 02:24:21 -03:00
|
|
|
if (file_path == nullptr) {
|
2015-06-30 01:35:52 -03:00
|
|
|
io_failure = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *p = &file_path[strlen(file_path)-12];
|
|
|
|
if (*p != '/') {
|
|
|
|
io_failure = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
snprintf(p, 13, "/%c%02u%c%03u.DAT",
|
|
|
|
block.lat_degrees<0?'S':'N',
|
2018-04-04 18:46:49 -03:00
|
|
|
(unsigned)MIN(abs((int32_t)block.lat_degrees), 99),
|
2015-06-30 01:35:52 -03:00
|
|
|
block.lon_degrees<0?'W':'E',
|
2018-04-04 18:46:49 -03:00
|
|
|
(unsigned)MIN(abs((int32_t)block.lon_degrees), 999));
|
2015-06-30 01:35:52 -03:00
|
|
|
|
|
|
|
// create directory if need be
|
|
|
|
if (!directory_created) {
|
|
|
|
*p = 0;
|
2019-08-01 02:13:49 -03:00
|
|
|
directory_created = !AP::FS().mkdir(file_path);
|
2015-06-30 01:35:52 -03:00
|
|
|
*p = '/';
|
2017-03-04 03:13:15 -04:00
|
|
|
|
|
|
|
if (!directory_created) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
// directory already existed
|
|
|
|
directory_created = true;
|
|
|
|
} else {
|
|
|
|
// if we didn't succeed at making the directory, then IO failed
|
|
|
|
io_failure = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 01:35:52 -03:00
|
|
|
}
|
2014-07-23 08:02:50 -03:00
|
|
|
|
|
|
|
if (fd != -1) {
|
2019-08-01 02:13:49 -03:00
|
|
|
AP::FS().close(fd);
|
2014-07-23 08:02:50 -03:00
|
|
|
}
|
2019-08-01 02:13:49 -03:00
|
|
|
fd = AP::FS().open(file_path, O_RDWR|O_CREAT);
|
2014-07-23 08:02:50 -03:00
|
|
|
if (fd == -1) {
|
|
|
|
#if TERRAIN_DEBUG
|
|
|
|
hal.console->printf("Open %s failed - %s\n",
|
2015-06-30 01:35:52 -03:00
|
|
|
file_path, strerror(errno));
|
2014-07-23 08:02:50 -03:00
|
|
|
#endif
|
|
|
|
io_failure = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_lat_degrees = block.lat_degrees;
|
|
|
|
file_lon_degrees = block.lon_degrees;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-05-01 22:52:00 -03:00
|
|
|
work out how many blocks needed in a stride for a given location
|
2014-07-23 08:02:50 -03:00
|
|
|
*/
|
2020-05-01 22:52:00 -03:00
|
|
|
uint32_t AP_Terrain::east_blocks(struct grid_block &block) const
|
2014-07-23 08:02:50 -03:00
|
|
|
{
|
|
|
|
Location loc1, loc2;
|
|
|
|
loc1.lat = block.lat_degrees*10*1000*1000L;
|
|
|
|
loc1.lng = block.lon_degrees*10*1000*1000L;
|
|
|
|
loc2.lat = block.lat_degrees*10*1000*1000L;
|
|
|
|
loc2.lng = (block.lon_degrees+1)*10*1000*1000L;
|
|
|
|
|
|
|
|
// shift another two blocks east to ensure room is available
|
2019-02-24 20:16:28 -04:00
|
|
|
loc2.offset(0, 2*grid_spacing*TERRAIN_GRID_BLOCK_SIZE_Y);
|
2019-04-08 10:16:20 -03:00
|
|
|
const Vector2f offset = loc1.get_distance_NE(loc2);
|
2020-05-01 22:52:00 -03:00
|
|
|
return offset.y / (grid_spacing*TERRAIN_GRID_BLOCK_SPACING_Y);
|
|
|
|
}
|
2014-07-23 08:02:50 -03:00
|
|
|
|
2020-05-01 22:52:00 -03:00
|
|
|
/*
|
|
|
|
seek to the right offset for disk_block
|
|
|
|
*/
|
|
|
|
void AP_Terrain::seek_offset(void)
|
|
|
|
{
|
|
|
|
struct grid_block &block = disk_block.block;
|
|
|
|
// work out how many longitude blocks there are at this latitude
|
|
|
|
uint32_t blocknum = east_blocks(block) * block.grid_idx_x + block.grid_idx_y;
|
|
|
|
uint32_t file_offset = blocknum * sizeof(union grid_io_block);
|
2019-08-01 02:13:49 -03:00
|
|
|
if (AP::FS().lseek(fd, file_offset, SEEK_SET) != (off_t)file_offset) {
|
2014-07-23 08:02:50 -03:00
|
|
|
#if TERRAIN_DEBUG
|
|
|
|
hal.console->printf("Seek %lu failed - %s\n",
|
|
|
|
(unsigned long)file_offset, strerror(errno));
|
|
|
|
#endif
|
2019-08-01 02:13:49 -03:00
|
|
|
AP::FS().close(fd);
|
2014-07-23 08:02:50 -03:00
|
|
|
fd = -1;
|
|
|
|
io_failure = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
write out disk_block
|
|
|
|
*/
|
|
|
|
void AP_Terrain::write_block(void)
|
|
|
|
{
|
|
|
|
seek_offset();
|
|
|
|
if (io_failure) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
disk_block.block.crc = get_block_crc(disk_block.block);
|
|
|
|
|
2019-08-01 02:13:49 -03:00
|
|
|
ssize_t ret = AP::FS().write(fd, &disk_block, sizeof(disk_block));
|
2014-07-23 08:02:50 -03:00
|
|
|
if (ret != sizeof(disk_block)) {
|
|
|
|
#if TERRAIN_DEBUG
|
|
|
|
hal.console->printf("write failed - %s\n", strerror(errno));
|
|
|
|
#endif
|
2019-08-01 02:13:49 -03:00
|
|
|
AP::FS().close(fd);
|
2014-07-23 08:02:50 -03:00
|
|
|
fd = -1;
|
|
|
|
io_failure = true;
|
|
|
|
} else {
|
2019-08-01 02:13:49 -03:00
|
|
|
AP::FS().fsync(fd);
|
2014-07-23 08:02:50 -03:00
|
|
|
#if TERRAIN_DEBUG
|
|
|
|
printf("wrote block at %ld %ld ret=%d mask=%07llx\n",
|
|
|
|
(long)disk_block.block.lat,
|
|
|
|
(long)disk_block.block.lon,
|
|
|
|
(int)ret,
|
|
|
|
(unsigned long long)disk_block.block.bitmap);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
disk_io_state = DiskIoDoneWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read in disk_block
|
|
|
|
*/
|
|
|
|
void AP_Terrain::read_block(void)
|
|
|
|
{
|
|
|
|
seek_offset();
|
|
|
|
if (io_failure) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int32_t lat = disk_block.block.lat;
|
|
|
|
int32_t lon = disk_block.block.lon;
|
|
|
|
|
2019-08-01 02:13:49 -03:00
|
|
|
ssize_t ret = AP::FS().read(fd, &disk_block, sizeof(disk_block));
|
2014-07-23 08:02:50 -03:00
|
|
|
if (ret != sizeof(disk_block) ||
|
2020-05-01 22:52:00 -03:00
|
|
|
!TERRAIN_LATLON_EQUAL(disk_block.block.lat,lat) ||
|
|
|
|
!TERRAIN_LATLON_EQUAL(disk_block.block.lon,lon) ||
|
2014-07-23 08:02:50 -03:00
|
|
|
disk_block.block.bitmap == 0 ||
|
|
|
|
disk_block.block.spacing != grid_spacing ||
|
|
|
|
disk_block.block.version != TERRAIN_GRID_FORMAT_VERSION ||
|
|
|
|
disk_block.block.crc != get_block_crc(disk_block.block)) {
|
|
|
|
#if TERRAIN_DEBUG
|
2020-05-01 22:52:00 -03:00
|
|
|
printf("read empty block at %ld %ld ret=%d (%ld %ld %u 0x%08lx) 0x%04x:0x%04x\n",
|
2014-07-23 08:02:50 -03:00
|
|
|
(long)lat,
|
|
|
|
(long)lon,
|
2020-05-01 22:52:00 -03:00
|
|
|
(int)ret,
|
|
|
|
(long)disk_block.block.lat,
|
|
|
|
(long)disk_block.block.lon,
|
|
|
|
(unsigned)disk_block.block.spacing,
|
|
|
|
(unsigned long)disk_block.block.bitmap,
|
|
|
|
(unsigned)disk_block.block.crc,
|
|
|
|
(unsigned)get_block_crc(disk_block.block));
|
2014-07-23 08:02:50 -03:00
|
|
|
#endif
|
|
|
|
// a short read or bad data is not an IO failure, just a
|
|
|
|
// missing block on disk
|
|
|
|
memset(&disk_block, 0, sizeof(disk_block));
|
|
|
|
disk_block.block.lat = lat;
|
|
|
|
disk_block.block.lon = lon;
|
|
|
|
disk_block.block.bitmap = 0;
|
|
|
|
} else {
|
|
|
|
#if TERRAIN_DEBUG
|
|
|
|
printf("read block at %ld %ld ret=%d mask=%07llx\n",
|
|
|
|
(long)lat,
|
|
|
|
(long)lon,
|
|
|
|
(int)ret,
|
|
|
|
(unsigned long long)disk_block.block.bitmap);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
disk_io_state = DiskIoDoneRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
timer called to do disk IO
|
|
|
|
*/
|
|
|
|
void AP_Terrain::io_timer(void)
|
|
|
|
{
|
|
|
|
if (io_failure) {
|
|
|
|
// don't keep trying io, so we don't thrash the filesystem
|
|
|
|
// code while flying
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (disk_io_state) {
|
|
|
|
case DiskIoIdle:
|
|
|
|
case DiskIoDoneRead:
|
|
|
|
case DiskIoDoneWrite:
|
|
|
|
// nothing to do
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DiskIoWaitWrite:
|
|
|
|
// need to write out the block
|
|
|
|
open_file();
|
|
|
|
if (fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
write_block();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DiskIoWaitRead:
|
|
|
|
// need to read in the block
|
|
|
|
open_file();
|
|
|
|
if (fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
read_block();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 18:56:55 -03:00
|
|
|
#endif // AP_TERRAIN_AVAILABLE
|