Path: blob/master/Tools/Replay/DataFlashFileReader.cpp
9388 views
#include "DataFlashFileReader.h"1#include <AP_Filesystem/AP_Filesystem.h>23#include <fcntl.h>4#include <string.h>5#include <sys/types.h>6#include <sys/stat.h>7#include <stdio.h>8#include <unistd.h>9#include <time.h>10#include <cinttypes>1112#ifndef PRIu6413#define PRIu64 "llu"14#endif1516AP_LoggerFileReader::AP_LoggerFileReader()17{}1819AP_LoggerFileReader::~AP_LoggerFileReader()20{21::printf("Replay counts: %" PRIu64 " bytes %u entries\n", bytes_read, message_count);22}2324bool AP_LoggerFileReader::open_log(const char *logfile)25{26fd = AP::FS().open(logfile, O_RDONLY);27if (fd == -1) {28return false;29}30// Get the file size for percentage calculation31struct stat st;32if (AP::FS().stat(logfile, &st) == 0) {33file_size = st.st_size;34}35return true;36}3738ssize_t AP_LoggerFileReader::read_input(void *buffer, const size_t count)39{40uint64_t ret = AP::FS().read(fd, buffer, count);41bytes_read += ret;42return ret;43}4445void AP_LoggerFileReader::format_type(uint16_t type, char dest[5])46{47const struct log_Format &f = formats[type];48memset(dest,0,5);49if (f.length == 0) {50return;51}52strncpy(dest, f.name, 4);53}54void AP_LoggerFileReader::get_packet_counts(uint64_t dest[])55{56memcpy(dest, packet_counts, sizeof(packet_counts));57}5859bool AP_LoggerFileReader::update()60{61uint8_t hdr[3];62if (read_input(hdr, 3) != 3) {63return false;64}65if (hdr[0] != HEAD_BYTE1 || hdr[1] != HEAD_BYTE2) {66printf("bad log header\n");67return false;68}6970#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS71// running on stm32 is slow enough it is nice to see progress72if (message_count % 500 == 0) {73::printf("line %u pkt 0x%02x t=%u\n", message_count, hdr[2], AP_HAL::millis());74}75#endif76packet_counts[hdr[2]]++;7778if (hdr[2] == LOG_FORMAT_MSG) {79struct log_Format f;80memcpy(&f, hdr, 3);81if (read_input(&f.type, sizeof(f)-3) != sizeof(f)-3) {82return false;83}84memcpy(&formats[f.type], &f, sizeof(formats[f.type]));8586message_count++;87return handle_log_format_msg(f);88}8990const struct log_Format &f = formats[hdr[2]];91if (f.length == 0) {92// can't just throw these away as the format specifies the93// number of bytes in the message94::printf("No format defined for type (%d)\n", hdr[2]);95exit(1);96}9798uint8_t msg[f.length];99100memcpy(msg, hdr, 3);101if (read_input(&msg[3], f.length-3) != f.length-3) {102return false;103}104105message_count++;106return handle_msg(f, msg);107}108109float AP_LoggerFileReader::get_percent_read()110{111if (file_size == 0) {112return 0.0f;113}114return (float)(bytes_read * 100.0 / file_size);115}116117118