-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKTcpElsterServer.cpp
402 lines (339 loc) · 9.25 KB
/
KTcpElsterServer.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
*
* 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 "KElsterTable.h"
#include "KScanTable.h"
#include "KTcpElsterServer.h"
#if !defined(__CAN_CLIENT__)
KTcpElsterServer::KTcpElsterServer()
{
Trace = false;
Simulation = false;
CanElster = NULL;
UseBinaryProtocol = false;
SetLocalPort(5524);
mTryToConnectTimeout = 2000;
mConnectionSleep = 200;
}
bool KTcpElsterServer::Init(const char * CanDev, bool trace)
{
Trace = trace;
if (!Simulation)
{
if (!CanElster)
CanElster = new KCanElster;
if (!CanElster->Init(CanDev, 0x680))
return false;
CanElster->Trace = trace;
CanElster->Resume();
NUtils::SleepMs(100);
return CanElster->GetInitCS_Id();
}
return true;
}
const char * KTcpElsterServer::GetCanDev() const
{
if (CanElster)
return CanElster->GetDev();
return "simulation";
}
void KTcpElsterServer::ServeToDevice(const KComfortFrame & cf, KComfortFrame & cfRes)
{
if (CanElster->SendFrame.Id != 0x680)
{
cfRes.SetOk(false);
if (!Trace)
{
printf("wrong id> ");
CanElster->PrintSendFrame();
}
} else {
KComfortFrame::msg_type msg = cf.GetMsgType();
if (cf.Data[0] != 0x0d) // Sender muss Id 680 haben
msg = KComfortFrame::mt_invalid;
switch (msg)
{
case KComfortFrame::mt_get:
if (CanElster->Send(1, true, 50))
cfRes.GetCanFrame(CanElster->RecvFrame);
break;
case KComfortFrame::mt_short_change:
case KComfortFrame::mt_change:
{
unsigned char u = CanElster->SendFrame.Data[0] & 0xf0;
CanElster->SendFrame.Data[0] = u + 2;
bool Ok = CanElster->SetValue();
cfRes.SetOk(Ok);
if (!Trace)
{
char str[128];
printf("set param (%s)> ", Ok ? "true" : "false");
if (FormElsterTable(CanElster->SendFrame, str))
{
printf("%s\n", str);
}
CanElster->PrintSendFrame();
}
break;
}
default:
if (!Trace)
{
printf("no answer> ");
CanElster->PrintSendFrame();
}
cfRes.SetOk(false);
break;
}
}
}
void KTcpElsterServer::ServeSimulation(const KComfortFrame & cf, KComfortFrame & cfRes)
{
cfRes.SetOk(false);
KScanTable::Get_scan_value(cf, cfRes);
}
bool KTcpElsterServer::ReadComfortFrame(KTcpCanClient & aClientSocket, KComfortFrame & cf)
{
char Buffer[1024];
if (UseBinaryProtocol)
{
if (!aClientSocket.ReadElster(cf))
return false;
if (Trace)
{
cf.SetToString(Buffer);
printf("recv: %s\n", Buffer);
}
} else {
if (!aClientSocket.ReadElster(Buffer))
return false;
if (Trace)
printf("recv: %s", Buffer);
if (!cf.SetFromString(Buffer, false))
{
if (Trace)
printf("- invalid Comfort format\n");
return false;
}
}
return true;
}
void KTcpElsterServer::ServeConnection(KTcpCanClient & aClientSocket)
{
char Res[128];
KComfortFrame cf;
KComfortFrame cfRes;
if (Trace)
aClientSocket.mTrace = true;
while (!IsTerminated() &&
(!CanElster || !CanElster->Terminated()) &&
ReadComfortFrame(aClientSocket, cf))
{
cfRes.SetOk(false);
if (cf.IsInitFrame())
{
cfRes.SetInitOk();
} else
if (CanElster && !cf.SetCanFrame(CanElster->SendFrame))
{
if (Trace)
printf("- cannot convert to can frame\n");
} else
if (!Simulation)
ServeToDevice(cf, cfRes);
else
ServeSimulation(cf, cfRes);
cfRes.SetToString(Res);
if (Trace)
printf("send: %s\n", Res);
if (UseBinaryProtocol)
{
if (!aClientSocket.SendElster(cfRes, 100))
break;
} else {
if (!aClientSocket.SendElster(Res, 100))
break;
}
ReadyForReading(50);
}
}
#endif
#if defined(__CAN_SERVER__)
#if defined(__LINUX__)
#include <sys/signal.h>
#endif
int main(int argc, char* argv[])
{
KTcpElsterServer Server;
bool start_ok;
#if defined(__WINDOWS__)
// DOS tabelle cp850
printf("Elster-Kromschr\x94 \bder TCP/IP server\n"
"copyright (c) 2014 J\x81rg M\x81ller, CH-5524\n");
#else
printf("Elster-Kromschröder TCP/IP server\n"
"copyright (c) 2015 Jürg Müller, CH-5524\n");
#endif
bool trace = !strcmp(argv[argc-1], "trace");
if (trace)
argc--;
Server.UseBinaryProtocol = !strcmp(argv[argc-1], "binary_protocol");
if (Server.UseBinaryProtocol)
argc--;
start_ok = 2 <= argc && argc <= 3;
if (argc >= 2)
{
if (!strncmp(argv[1], "simul", 5))
{
Server.Simulation = true;
if (argc == 3)
{
if (!KScanTable::LoadScanTable(argv[2]))
{
printf("\ncould scan table \"%s\" not load\n", argv[2]);
start_ok = false;
}
} else
KScanTable::Init_scan_table();
} else
if (argc > 2)
start_ok = false;
}
if (!start_ok)
{
printf("\nusage:\n"
" can_server ( <can device> | \"simulation\" ) [ <scan table name> ] [ \"binary_protocol\" ] [ \"trace\" ]\n\n"
#if defined(__WINDOWS__)
"example: can_server COM1 \n\n");
#else
"example: ./can_server can0 \n\n");
#endif
if (argc >= 2)
return -1;
}
if (!Server.Init(argc == 2 ? argv[1] : NULL, trace))
{
printf("can (%s) not opened\n\n", Server.GetCanDev());
return -1;
}
char Addr[128];
char str[128];
KIpSocket::LocalHostAddr(Addr, High(Addr));
printf("\ndevice: %s\n", Server.GetCanDev());
SetValueType(str, et_dev_id, KComfortFrame::InitId);
printf("Kennwort: 0x%4.4x Version: %s\n", KComfortFrame::InitId, str);
#if defined(SIGPIPE)
signal(SIGPIPE, SIG_IGN);
#endif
if (!NUtils::set_term_signal())
{
return -1;
}
if (!Server.Open())
{
printf("TCP/IP server not established\n\n");
return -1;
}
printf("server established: %s:%d\n\n", Addr, Server.GetLocalPort());
while (Server.Opened() && !Server.IsTerminated() &&
!NUtils::term_signal_is_set())
{
NUtils::SleepMs(100);
}
Server.Halt();
printf("\nbye-bye\n");
return 0;
}
#endif
#if defined(__CAN_CLIENT__)
#include "KTcpCanClient.h"
int main(int argc, char* argv[])
{
KTcpCanClient Client;
char Buffer[1024];
#if defined(__WINDOWS__)
// DOS tabelle cp850
printf("Elster-Kromschr\x94 \bder TCP/IP client\n"
"copyright (c) 2014 J\x81rg M\x81ller, CH-5524\n\n");
#else
printf("Elster-Kromschröder TCP/IP client\n"
"copyright (c) 2014 Jürg Müller, CH-5524\n\n");
#endif
Client.SetRemotePort(5524);
Client.SetRemoteHost(argc == 2 ? argv[1] : "localhost");
Client.SetBlockMode(bmBlocking);
bool UseBinaryProtocol = !strcmp(argv[argc-1], "binary_protocol");
if (UseBinaryProtocol)
argc--;
if (argc != 2)
{
printf("usage:\n"
" can_client <ip address host> [ \"binary_protocol\" ]\n\n");
}
printf("used IP address %s\n\n", Client.GetRemoteHost());
while (true)
{
printf(">> ");
if (fgets(Buffer, High(Buffer), stdin))
{
while (*Buffer == '\n' ||
*Buffer == '\r' ||
*Buffer == ' ')
memmove(Buffer, Buffer+1, strlen(Buffer));
if (!strncmp(Buffer, "en", 2) ||
*Buffer == 'q' ||
*Buffer == 0)
break;
int len = (int) strlen(Buffer);
while (len > 0 &&
(Buffer[len-1] == ' ' || Buffer[len-1] == '.' ||
Buffer[len-1] == '\n' || Buffer[len-1] == '\r'))
len--;
Buffer[len] = 0;
int DigitCount = 0;
for (int i = 0; i < len; i++)
if (NUtils::IsHexDigit(Buffer[i]))
DigitCount++;
if (DigitCount == 20) // Check Summe anfügen
{
KComfortFrame cf;
cf.SetFromString(Buffer, true);
sprintf(Buffer + strlen(Buffer), " %2.2x %2.2x", cf.Data[10], cf.Data[11]);
}
if (Client.Connect(2000))
{
printf("connected\n");
if (Client.SendElster(Buffer, 100)) // 100 ms timeout
{
if (Client.ReadElster(Buffer))
printf("%s", Buffer);
}
Client.Disconnect();
} else
printf("not connected\n");
}
}
printf("bye-bye\n\n");
return 0;
}
#endif