2019-01-18 02:18:52 -04:00
|
|
|
#include "DataFlashFileReader.h"
|
2015-06-12 09:52:52 -03:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2016-08-25 00:00:41 -03:00
|
|
|
#include <time.h>
|
2017-10-24 05:36:26 -03:00
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
#ifndef PRIu64
|
|
|
|
#define PRIu64 "llu"
|
|
|
|
#endif
|
2016-08-25 00:00:41 -03:00
|
|
|
|
|
|
|
// flogged from AP_Hal_Linux/system.cpp; we don't want to use stopped clock here
|
|
|
|
uint64_t now() {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return 1.0e6*((ts.tv_sec + (ts.tv_nsec*1.0e-9)));
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
AP_LoggerFileReader::AP_LoggerFileReader() :
|
2016-08-25 00:00:41 -03:00
|
|
|
start_micros(now())
|
|
|
|
{}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
AP_LoggerFileReader::~AP_LoggerFileReader()
|
2016-08-25 00:00:41 -03:00
|
|
|
{
|
|
|
|
const uint64_t micros = now();
|
|
|
|
const uint64_t delta = micros - start_micros;
|
2017-10-24 05:36:26 -03:00
|
|
|
::printf("Replay counts: %" PRIu64 " bytes %u entries\n", bytes_read, message_count);
|
|
|
|
::printf("Replay rates: %" PRIu64 " bytes/second %" PRIu64 " messages/second\n", bytes_read*1000000/delta, message_count*1000000/delta);
|
2016-08-25 00:00:41 -03:00
|
|
|
}
|
2015-06-12 09:52:52 -03:00
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
bool AP_LoggerFileReader::open_log(const char *logfile)
|
2015-06-12 09:52:52 -03:00
|
|
|
{
|
2016-11-02 15:02:57 -03:00
|
|
|
fd = ::open(logfile, O_RDONLY|O_CLOEXEC);
|
2015-06-12 09:52:52 -03:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
ssize_t AP_LoggerFileReader::read_input(void *buffer, const size_t count)
|
2016-08-25 00:00:41 -03:00
|
|
|
{
|
|
|
|
uint64_t ret = ::read(fd, buffer, count);
|
|
|
|
bytes_read += ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_LoggerFileReader::format_type(uint16_t type, char dest[5])
|
2015-06-29 23:48:49 -03:00
|
|
|
{
|
|
|
|
const struct log_Format &f = formats[type];
|
|
|
|
memset(dest,0,5);
|
|
|
|
if (f.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strncpy(dest, f.name, 4);
|
|
|
|
}
|
2019-01-18 00:23:42 -04:00
|
|
|
void AP_LoggerFileReader::get_packet_counts(uint64_t dest[])
|
2015-06-29 23:48:49 -03:00
|
|
|
{
|
|
|
|
memcpy(dest, packet_counts, sizeof(packet_counts));
|
|
|
|
}
|
|
|
|
|
2020-03-09 00:28:23 -03:00
|
|
|
bool AP_LoggerFileReader::update(char type[5], uint8_t &core)
|
2015-06-12 09:52:52 -03:00
|
|
|
{
|
|
|
|
uint8_t hdr[3];
|
2016-08-25 00:00:41 -03:00
|
|
|
if (read_input(hdr, 3) != 3) {
|
2015-06-12 09:52:52 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hdr[0] != HEAD_BYTE1 || hdr[1] != HEAD_BYTE2) {
|
|
|
|
printf("bad log header\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-29 23:48:49 -03:00
|
|
|
packet_counts[hdr[2]]++;
|
|
|
|
|
2015-06-12 09:52:52 -03:00
|
|
|
if (hdr[2] == LOG_FORMAT_MSG) {
|
|
|
|
struct log_Format f;
|
|
|
|
memcpy(&f, hdr, 3);
|
2016-08-25 00:00:41 -03:00
|
|
|
if (read_input(&f.type, sizeof(f)-3) != sizeof(f)-3) {
|
2015-06-12 09:52:52 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(&formats[f.type], &f, sizeof(formats[f.type]));
|
|
|
|
strncpy(type, "FMT", 3);
|
|
|
|
type[3] = 0;
|
|
|
|
|
2016-08-25 00:00:41 -03:00
|
|
|
message_count++;
|
2015-06-12 09:52:52 -03:00
|
|
|
return handle_log_format_msg(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct log_Format &f = formats[hdr[2]];
|
|
|
|
if (f.length == 0) {
|
|
|
|
// can't just throw these away as the format specifies the
|
|
|
|
// number of bytes in the message
|
|
|
|
::printf("No format defined for type (%d)\n", hdr[2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t msg[f.length];
|
|
|
|
|
|
|
|
memcpy(msg, hdr, 3);
|
2016-08-25 00:00:41 -03:00
|
|
|
if (read_input(&msg[3], f.length-3) != f.length-3) {
|
2015-06-12 09:52:52 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(type, f.name, 4);
|
|
|
|
type[4] = 0;
|
|
|
|
|
2016-08-25 00:00:41 -03:00
|
|
|
message_count++;
|
2020-03-09 00:28:23 -03:00
|
|
|
return handle_msg(f, msg, core);
|
2015-06-12 09:52:52 -03:00
|
|
|
}
|