-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCanServer.cpp
303 lines (262 loc) · 6.48 KB
/
KCanServer.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
*
* Copyright (C) 2014 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 <stdio.h>
#include <string.h>
#include "NTypes.h"
#include "NUtils.h"
#include "NCanUtils.h"
#include "KCriticalSection.h"
#include "KCanCommDriver.h"
#if !defined(__NO_REMOTE__)
#include "KCanTcpDriver.h"
#endif
#if defined(__LINUX__) && !defined(__MAC__)
#include "KSocketCanDriver.h"
#endif
#if defined(__WINDOWS__)
#include "special/Kusb2can.h"
#endif
#include "KCanServer.h"
KCanServer::KCanServer(bool UseMqtt)
{
DriverType = NCanUtils::dt_can;
Trace = false;
AsSimulation = false;
CanDriver = NULL;
IsInitalized = false;
FrameCount = 0;
#if defined (__LINUX__) && !defined(__MAC__)
CanSendDriver = NULL;
#endif
#if defined(__MQTT__)
CanRingBuffer = new KRingBuffer<KCanFrame>();
MqttRingBuffer = MqttRing;
#else
RingHead = RingTail = 0;
RingBufferSize = 10240;
RingBuffer = new KCanFrame[RingBufferSize];
RingBufferSemaphore = new KCriticalSection;
#endif
}
KCanServer::~KCanServer()
{
if (CanDriver)
{
CanDriver->Close();
delete CanDriver;
}
#if defined (__LINUX__) && !defined(__MAC__)
if (CanSendDriver)
{
CanSendDriver->Close();
delete CanSendDriver;
}
#endif
#if defined(__MQTT__)
delete CanRingBuffer;
#else
delete RingBufferSemaphore;
delete [] RingBuffer;
#endif
}
bool KCanServer::Init(const char * CanDev)
{
if (IsInitalized)
return false;
IsInitalized = true;
if (!CanDriver)
{
enum NCanUtils::driver_type new_dt;
if (DriverType == NCanUtils::dt_cs || DriverType == NCanUtils::dt_elm327)
new_dt = DriverType;
else
new_dt = NCanUtils::GetDriverType(CanDev);
#if !defined(__NO_REMOTE__)
if (new_dt == NCanUtils::dt_unknown && KIpSocket::IsHostAddr(CanDev))
new_dt = NCanUtils::dt_can232_remote;
#endif
if (new_dt != NCanUtils::dt_unknown)
DriverType = new_dt;
switch (DriverType)
{
#if defined (__LINUX__) && !defined(__MAC__)
case NCanUtils::dt_can:
CanDriver = new KSocketCanDriver;
#if !defined(__UVR__)
CanSendDriver = new KSocketCanDriver;
#endif
break;
#endif
#if !defined(__NO_REMOTE__)
case NCanUtils::dt_can232_remote:
CanDriver = new KCan232Tcp;
break;
#endif
case NCanUtils::dt_cs:
CanDriver = new KCanCS;
break;
case NCanUtils::dt_can232:
CanDriver = new KCan232;
break;
case NCanUtils::dt_elm327:
CanDriver = new KCanELM327;
break;
#if defined(__WINDOWS__)
case NCanUtils::dt_8dev:
CanDriver = new Kusb2canDriver;
break;
#endif
default:
return false;
}
}
CanDriver->AsSimulation = AsSimulation;
if (!CanDriver->Init(CanDev))
return false;
#if defined (__LINUX__) && !defined(__MAC__)
if (CanSendDriver)
{
if (!CanSendDriver->Init(CanDev) ||
!CanSendDriver->Connect())
return false;
}
#endif
return true;
}
void KCanServer::Halt()
{
Terminate();
while (!Terminated())
;
NUtils::SleepMs(10);
}
#if !defined(__MQTT__)
int KCanServer::RingCount() const
{
return (RingHead - RingTail + RingBufferSize) % RingBufferSize;
}
void KCanServer::IncRingPtr(volatile int & RingPtr)
{
RingPtr = (RingPtr + RingBufferSize + 1) % RingBufferSize;
}
#endif
bool KCanServer::RingBufferFull() const
{
#if defined(__MQTT__)
return CanRingBuffer->RingBufferFull();
#else
return RingCount() > 3*(RingBufferSize / 4);
#endif
}
void KCanServer::PutFrame(const KCanFrame & Frame)
{
#if defined(__MQTT__)
CanRingBuffer->Push(Frame);
if (MqttRingBuffer != NULL)
MqttRingBuffer->Push(Frame);
#else
if (RingBufferSemaphore->Acquire())
{
IncRingPtr(RingHead);
if (RingTail == RingHead)
IncRingPtr(RingTail);
RingBuffer[RingHead] = Frame;
RingBufferSemaphore->Release();
}
#endif
}
bool KCanServer::GetFrame(KCanFrame & Frame)
{
#if defined(__MQTT__)
return CanRingBuffer->Pop(Frame);
#else
bool Ok = false;
if (RingHead == RingTail)
return Ok;
if (RingBufferSemaphore->Acquire())
{
if (RingTail != RingHead)
{
Ok = true;
IncRingPtr(RingTail);
Frame = RingBuffer[RingTail];
}
RingBufferSemaphore->Release();
}
return Ok;
#endif
}
bool KCanServer::SendData(const KCanFrame & Frame)
{
#if defined (__LINUX__) && !defined(__MAC__)
if (CanSendDriver && (Frame.Data[0] & 0xf) == 2)
return CanSendDriver->SendData(Frame);
#endif
if (!CanDriver)
return false;
return CanDriver->SendData(Frame);
}
bool KCanServer::SendData(const KComfortFrame & Frame)
{
#if defined (__LINUX__) && !defined(__MAC__)
if (CanSendDriver)
return CanSendDriver->SendData(Frame);
#endif
if (!CanDriver)
return false;
return CanDriver->SendData(Frame);
}
bool KCanServer::ReceiveData(KCanFrame & Frame)
{
if (CanDriver)
{
return CanDriver->ReceiveData(Frame);
}
return false;
}
void KCanServer::SetTrace(bool trace)
{
Trace = trace;
if (CanDriver)
CanDriver->Trace = trace;
}
void KCanServer::Execute()
{
#if defined(__CONSOLE__) && defined(__DEBUG__)
printf("start CanServer\n");
#endif
Init(NULL);
if (!CanDriver->Connect())
Terminate();
while (!Terminated())
{
KCanFrame Frame;
if (CanDriver->ReceiveData(Frame) &&
Frame.Data[0] == 0xd2) // Antwort für ID 680
{
Frame.Counter = (int)FrameCount++;
PutFrame(Frame);
}
NUtils::SleepMs(1);
}
#if defined(__CONSOLE__) && defined(__DEBUG__)
printf("stop CanServer\n");
#endif
}