Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/Tools/Replay/DataFlashFileReader.cpp
9353 views
1
#include "DataFlashFileReader.h"
2
#include <AP_Filesystem/AP_Filesystem.h>
3
4
#include <fcntl.h>
5
#include <string.h>
6
#include <sys/types.h>
7
#include <sys/stat.h>
8
#include <stdio.h>
9
#include <unistd.h>
10
#include <time.h>
11
#include <cinttypes>
12
13
#ifndef PRIu64
14
#define PRIu64 "llu"
15
#endif
16
17
AP_LoggerFileReader::AP_LoggerFileReader()
18
{}
19
20
AP_LoggerFileReader::~AP_LoggerFileReader()
21
{
22
::printf("Replay counts: %" PRIu64 " bytes %u entries\n", bytes_read, message_count);
23
}
24
25
bool AP_LoggerFileReader::open_log(const char *logfile)
26
{
27
fd = AP::FS().open(logfile, O_RDONLY);
28
if (fd == -1) {
29
return false;
30
}
31
// Get the file size for percentage calculation
32
struct stat st;
33
if (AP::FS().stat(logfile, &st) == 0) {
34
file_size = st.st_size;
35
}
36
return true;
37
}
38
39
ssize_t AP_LoggerFileReader::read_input(void *buffer, const size_t count)
40
{
41
uint64_t ret = AP::FS().read(fd, buffer, count);
42
bytes_read += ret;
43
return ret;
44
}
45
46
void AP_LoggerFileReader::format_type(uint16_t type, char dest[5])
47
{
48
const struct log_Format &f = formats[type];
49
memset(dest,0,5);
50
if (f.length == 0) {
51
return;
52
}
53
strncpy(dest, f.name, 4);
54
}
55
void AP_LoggerFileReader::get_packet_counts(uint64_t dest[])
56
{
57
memcpy(dest, packet_counts, sizeof(packet_counts));
58
}
59
60
bool AP_LoggerFileReader::update()
61
{
62
uint8_t hdr[3];
63
if (read_input(hdr, 3) != 3) {
64
return false;
65
}
66
if (hdr[0] != HEAD_BYTE1 || hdr[1] != HEAD_BYTE2) {
67
printf("bad log header\n");
68
return false;
69
}
70
71
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
72
// running on stm32 is slow enough it is nice to see progress
73
if (message_count % 500 == 0) {
74
::printf("line %u pkt 0x%02x t=%u\n", message_count, hdr[2], AP_HAL::millis());
75
}
76
#endif
77
packet_counts[hdr[2]]++;
78
79
if (hdr[2] == LOG_FORMAT_MSG) {
80
struct log_Format f;
81
memcpy(&f, hdr, 3);
82
if (read_input(&f.type, sizeof(f)-3) != sizeof(f)-3) {
83
return false;
84
}
85
memcpy(&formats[f.type], &f, sizeof(formats[f.type]));
86
87
message_count++;
88
return handle_log_format_msg(f);
89
}
90
91
const struct log_Format &f = formats[hdr[2]];
92
if (f.length == 0) {
93
// can't just throw these away as the format specifies the
94
// number of bytes in the message
95
::printf("No format defined for type (%d)\n", hdr[2]);
96
exit(1);
97
}
98
99
uint8_t msg[f.length];
100
101
memcpy(msg, hdr, 3);
102
if (read_input(&msg[3], f.length-3) != f.length-3) {
103
return false;
104
}
105
106
message_count++;
107
return handle_msg(f, msg);
108
}
109
110
float AP_LoggerFileReader::get_percent_read()
111
{
112
if (file_size == 0) {
113
return 0.0f;
114
}
115
return (float)(bytes_read * 100.0 / file_size);
116
}
117
118