forked from andig/canprogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCanLogFile.cpp
executable file
·229 lines (196 loc) · 5.25 KB
/
KCanLogFile.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
*
* Copyright (C) 2016 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#if defined(__PYTHON__)
#include <Python.h>
#endif
#include <string.h>
#include <sys/stat.h>
#include "NTypes.h"
#include "NUtils.h"
#include "KCanLogFile.h"
#include "KElsterTable.h"
// Aus einem log-File, welches durch can_logger erzeugt wurde, einen Array von
// KCanFrame erstellen.
KCanLogFile::KCanLogFile()
{
Frames = NULL;
count = 0;
pos = 0;
}
KCanLogFile::~KCanLogFile()
{
Init();
}
void KCanLogFile::Init()
{
if (Frames)
delete [] Frames;
Frames = NULL;
pos = 0;
}
unsigned KCanLogFile::LogFileToCanFrames(const KStream & stream, KCanFrame * Frames)
{
KCanFrame frame;
unsigned long pos = 0;
unsigned count = 0;
unsigned long size = stream.Size();
while (pos < size)
{
while (pos < size && stream.GetByte(pos) < ' ')
pos++;
if (pos+10 >= size)
break;
unsigned long off = pos + 1;
while (off < size && stream.GetByte(off) >= ' ')
off++;
if (frame.InitFromLog(stream.GetCharMemory() + pos))
{
if (Frames != NULL)
Frames[count] = frame;
count++;
}
pos = off;
}
return count;
}
KCanFrame * KCanLogFile::BuildCanFrames(const KStream & stream)
{
Init();
count = LogFileToCanFrames(stream, NULL);
if (count)
{
Frames = new KCanFrame[count];
LogFileToCanFrames(stream, Frames);
}
return Frames;
}
bool KCanLogFile::ReadFile(const char * Filename)
{
KStream stream;
if (!stream.ReadFile(Filename))
{
printf("\nlog file \"%s\" not loaded\n", Filename);
return false;
}
printf("\nreading log file \"%s\" ...\n", Filename);
BuildCanFrames(stream);
printf("log file read: frame count %d\n\n", count);
return Frames != NULL;
}
void KCanLogFile::PrintScanTable() const
{
if (Frames)
{
unsigned short id_table[200];
unsigned id_count = 0;
// CAN-IDs ermitteln und im Array "id_table" speichern.
for (unsigned i = 0; i < count; i++)
{
bool found = false;
for (unsigned j = 0; !found && j < id_count; j++)
found = id_table[j] == Frames[i].GetIdToValue();
if (!found && id_count < 200)
id_table[id_count++] = Frames[i].GetIdToValue();
}
// Array "id_table" sortieren.
for (unsigned i = 0; i < id_count-1; i++)
for (unsigned k = i+1; k < id_count; k++)
if (id_table[i] > id_table[k])
{
unsigned short u = id_table[i];
id_table[i] = id_table[k];
id_table[k] = u;
}
printf("list of valid can id's:\n\n");
for (unsigned i = 0; i < id_count; i++)
printf(" %3.3x\n", id_table[i]);
bool * idx = new bool[0x10000];
unsigned short * val = new unsigned short[0x10000];
for (unsigned i = 0; i < id_count; i++)
{
for (unsigned k = 0; k < 0x10000; k++)
{
idx[k] = false;
}
// alle Werte zu "id_table[i]" in "val" speichern.
for (unsigned k = 0; k < count; k++)
{
if (Frames[k].GetIdToValue() == id_table[i] &&
(Frames[k].Data[0] & 0x0f) != 0x01)
{
int val_ = Frames[k].GetValue();
int idx_ = Frames[k].GetElsterIdx();
if (val_ >= 0 && idx_ >= 0 && !idx[idx_])
{
idx[idx_] = true;
val[idx_] = val_;
}
}
}
printf("\n");
for (unsigned k = 0; k < 0x10000; k++)
if (idx[k])
{
printf(" { 0x%3.3x, 0x%4.4x, 0x%4.4x},", id_table[i], k, val[k]);
const ElsterIndex * elst_idx = GetElsterIndex(k);
if (elst_idx)
{
char buff[200];
SetValueType(buff, elst_idx->Type, val[k]);
printf(" // %s: %s", elst_idx->Name, buff);
}
printf("\n");
}
}
delete [] idx;
delete [] val;
}
}
unsigned KCanLogFile::SetPos()
{
if (count)
{
int day, time_ms;
NUtils::Time(day, time_ms);
pos = 0;
last_day = day;
while (pos < count &&
time_ms > Frames[pos].TimeStampMs)
pos++;
}
return pos;
}
bool KCanLogFile::GetNextFrame(KCanFrame & Frame)
{
if (count)
{
int day, time_ms;
NUtils::Time(day, time_ms);
if (day > last_day)
{
last_day = day;
pos = 0;
}
if (pos < count &&
time_ms >= Frames[pos].TimeStampMs)
{
Frame = Frames[pos++];
return true;
}
}
return false;
}