forked from ParrotSec/car-hacking-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogparser.cc
More file actions
63 lines (53 loc) · 1.69 KB
/
Copy pathlogparser.cc
File metadata and controls
63 lines (53 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "logparser.h"
#define COMMENTSZ 200
/* buffer sizes for CAN frame string representations */
#define CL_ID (sizeof("12345678##1"))
#define CL_DATA sizeof(".AA")
#define CL_BINDATA sizeof(".10101010")
/* CAN FD ASCII hex short representation with DATA_SEPERATORs */
#define CL_CFSZ (2*CL_ID + 64*CL_DATA)
/* CAN FD ASCII hex long representation with binary output */
#define CL_LONGCFSZ (2*CL_ID + sizeof(" [255] ") + (64*CL_BINDATA))
#define BUFSZ (sizeof("(1345212884.318850)") + IFNAMSIZ + 4 + CL_CFSZ + COMMENTSZ) /* for one line in the logfile */
LogParser::LogParser() {
}
LogParser::~LogParser() {
if(log_opened) {
fclose(logfp);
log_opened = false;
}
}
string LogParser::processNext() {
static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ];
static struct timeval log_tv;
struct canfd_frame cf;
char *fret;
if (!log_opened) {
logfp = fopen(logfile.c_str(), "r");
if(logfp < 0) {
log_eof = true;
return "Couldn't open logfile " + logfile;
} else {
log_eof = false;
log_opened = true;
}
} else { // Log already opened
/* read first non-comment frame from logfile */
while ((fret = fgets(buf, BUFSZ-1, logfp)) != NULL && buf[0] != '(') {
if (strlen(buf) >= BUFSZ-2) {
return "comment line too long for input buffer";
}
}
if(!fret) {
log_eof = true;
} else {
if (sscanf(buf, "(%ld.%ld) %s %s", &log_tv.tv_sec, &log_tv.tv_usec, device, ascframe) != 4) {
return "incorrect line format in logfile";
}
gd.getCan()->parse_canframe(ascframe, &cf);
gd.processPkt(&cf);
return "Packet: " + string(ascframe);
}
}
return "Finsihed processing logfile";
}