forked from andig/canprogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKSniffedFrame.cpp
executable file
·209 lines (175 loc) · 4.61 KB
/
KSniffedFrame.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
/*
*
* Copyright (C) 2017 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 "NTypes.h"
#include "KSniffedFrame.h"
#include "KScanTable.h"
void KSniffedFrame::Init(unsigned short Id, unsigned short Recv_id, unsigned short idx)
{
id = Id;
recv_id = Recv_id;
elster_idx = idx;
value = 0x8000;
TimeStampDay = 0;
TimeStampMs = 0;
State = st_invalid;
}
KSniffedFrame::KSniffedFrame(unsigned short Id, unsigned short Recv_id, unsigned short idx)
{
Init(Id, Recv_id, idx);
Next = NULL;
}
KSniffedFrame::KSniffedFrame(const KCanFrame & Frame)
{
Init(Frame.Id, Frame.GetReceiverId(), Frame.GetElsterIdx());
Next = NULL;
}
bool KSniffedFrame::SetValue(const KCanFrame & Frame)
{
if (!Frame.HasValue())
return false;
value = Frame.GetValue();
TimeStampDay = Frame.TimeStampDay;
TimeStampMs = Frame.TimeStampMs;
switch (State)
{
case st_invalid:
State = st_value_changed;
break;
case st_valid:
case st_only_time_changed:
if (Frame.GetValue() == value)
State = st_only_time_changed;
else
State = st_value_changed;
break;
case st_value_changed:
break;
}
return true;
}
KSniffedData::KSniffedData()
{
for (int i = 0; i <= (int) High(Sniffed); i++)
Sniffed[i] = NULL;
for (int i = 0; i <= (int) High(UsedCanId); i++)
UsedCanId[i] = false;
}
KSniffedData::~KSniffedData()
{
for (int i = 0; i <= (int) High(Sniffed); i++)
{
while (Sniffed[i])
{
KSniffedFrame * SF = Sniffed[i]->Next;
delete Sniffed[i];
Sniffed[i] = SF;
}
}
}
void KSniffedData::SetUsedCanId(unsigned char Id)
{
UsedCanId[Id] = true;
}
bool KSniffedData::AddToSniffed(const KCanFrame & Frame)
{
unsigned char Index = Frame.Data[0];
if ((Frame.Len != 3 && Frame.Len != 5) ||
(Index & 0xf) == 1)
return false;
if (SearchSniffedFrame(Frame))
return false;
KSniffedFrame * SF = new KSniffedFrame(Frame);
unsigned char ShortId = KScanTable::ShortCanId((unsigned short) Frame.GetReceiverId());
SF->Next = Sniffed[ShortId];
Sniffed[ShortId] = SF;
return true;
}
void KSniffedData::ReadAll()
{
for (int i = High(Sniffed); i >= 0; i--)
{
KSniffedFrame * SF = Sniffed[i];
while (SF)
{
if (!SF->State != KSniffedFrame::st_invalid)
{
SF->State = KSniffedFrame::st_value_changed;
}
SF = SF->Next;
}
}
}
const KSniffedFrame * KSniffedData::SearchSniffedFrame(unsigned short id, unsigned short elster_idx)
{
unsigned char ShortId = KScanTable::ShortCanId(id);
KSniffedFrame * SF = Sniffed[ShortId];
while (SF)
{
if (elster_idx == SF->elster_idx)
{
return SF;
}
SF = SF->Next;
}
return NULL;
}
const KSniffedFrame * KSniffedData::SearchSniffedFrame(const KCanFrame & Frame)
{
return SearchSniffedFrame(Frame.Id, Frame.GetElsterIdx());
}
const KSniffedFrame * KSniffedData::GetFirstSniffedFrame()
{
for (int i = High(Sniffed); i >= 0; i--)
{
KSniffedFrame * SF = Sniffed[i];
while (SF)
{
if (SF->State >= KSniffedFrame::st_only_time_changed)
{
SF->State = KSniffedFrame::st_valid;
return SF;
}
SF = SF->Next;
}
}
return NULL;
}
bool KSniffedData::GetFirstSniffedValue(unsigned short & Value, unsigned short & id, unsigned short & idx)
// Get first changed sniffed value.
{
const KSniffedFrame * SF = GetFirstSniffedFrame();
if (SF)
{
Value = SF->value;
id = SF->id;
idx = SF->elster_idx;
return true;
}
return false;
}
bool KSniffedData::ClearSniffedValue(const KCanFrame & Frame)
{
KSniffedFrame * SF = (KSniffedFrame *) SearchSniffedFrame(Frame);
if (SF && SF->State != KSniffedFrame::st_invalid)
{
SF->State = KSniffedFrame::st_valid;
}
return SF != NULL;
}