Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/Replay/DataFlashFileReader.cpp
Views: 1798
#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 <stdio.h>7#include <unistd.h>8#include <time.h>9#include <cinttypes>1011#ifndef PRIu6412#define PRIu64 "llu"13#endif1415AP_LoggerFileReader::AP_LoggerFileReader()16{}1718AP_LoggerFileReader::~AP_LoggerFileReader()19{20::printf("Replay counts: %" PRIu64 " bytes %u entries\n", bytes_read, message_count);21}2223bool AP_LoggerFileReader::open_log(const char *logfile)24{25fd = AP::FS().open(logfile, O_RDONLY);26if (fd == -1) {27return false;28}29return true;30}3132ssize_t AP_LoggerFileReader::read_input(void *buffer, const size_t count)33{34uint64_t ret = AP::FS().read(fd, buffer, count);35bytes_read += ret;36return ret;37}3839void AP_LoggerFileReader::format_type(uint16_t type, char dest[5])40{41const struct log_Format &f = formats[type];42memset(dest,0,5);43if (f.length == 0) {44return;45}46strncpy(dest, f.name, 4);47}48void AP_LoggerFileReader::get_packet_counts(uint64_t dest[])49{50memcpy(dest, packet_counts, sizeof(packet_counts));51}5253bool AP_LoggerFileReader::update()54{55uint8_t hdr[3];56if (read_input(hdr, 3) != 3) {57return false;58}59if (hdr[0] != HEAD_BYTE1 || hdr[1] != HEAD_BYTE2) {60printf("bad log header\n");61return false;62}6364#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS65// running on stm32 is slow enough it is nice to see progress66if (message_count % 500 == 0) {67::printf("line %u pkt 0x%02x t=%u\n", message_count, hdr[2], AP_HAL::millis());68}69#endif70packet_counts[hdr[2]]++;7172if (hdr[2] == LOG_FORMAT_MSG) {73struct log_Format f;74memcpy(&f, hdr, 3);75if (read_input(&f.type, sizeof(f)-3) != sizeof(f)-3) {76return false;77}78memcpy(&formats[f.type], &f, sizeof(formats[f.type]));7980message_count++;81return handle_log_format_msg(f);82}8384const struct log_Format &f = formats[hdr[2]];85if (f.length == 0) {86// can't just throw these away as the format specifies the87// number of bytes in the message88::printf("No format defined for type (%d)\n", hdr[2]);89exit(1);90}9192uint8_t msg[f.length];9394memcpy(msg, hdr, 3);95if (read_input(&msg[3], f.length-3) != f.length-3) {96return false;97}9899message_count++;100return handle_msg(f, msg);101}102103104