-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNCanUtils.cpp
1105 lines (917 loc) · 23.6 KB
/
NCanUtils.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* 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
#if defined(__LINUX__)
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#endif
#if defined(__WINDOWS__)
#include <windows.h>
#endif
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "NTypes.h"
#include "NUtils.h"
#include "NCanUtils.h"
#include "KElsterTable.h"
#if defined(__ROTEX__) && !defined(__USE_SHORT__)
#define __USE_SHORT__
#endif
using namespace NCanUtils;
const char * NCanUtils::GetDriverTypeStr(driver_type type)
{
switch (type)
{
#if defined(__LINUX__) || !defined(__MAC__)
case dt_can: // SocketCAN: für PICAN CAN-Bus Board oder USB2CAN von 8devices.com
return "socket can";
#endif
case dt_can232: // RS232: CAN232, CANUSB von Lawicel oder USBtin von Fischl.de
return "USBtin";
#if !defined(__NO_REMOTE__)
case dt_can232_remote: // CAN232 via TCP-Client
return "remote";
#endif
case dt_elm327:
return "elm327";
case dt_cs: // optisches Interface: ComfortSoft-Protokoll
return "optical adapter";
#if defined(__WINDOWS__)
case dt_8dev: // Interface von 8devices.com - verwendet usb2com.dll (only for windows)
return "usb2com";
#endif
default:
return "unknown";
}
}
enum NCanUtils::driver_type NCanUtils::GetDriverType(const char * dev)
{
char copy[8];
if (!dev || strlen(dev) < 4)
return dt_unknown;
strncpy(copy, dev, 3);
copy[3] = 0;
#if defined(__LINUX__)
if (!stricmp(copy, "tty") ||
!stricmp(copy, "rfc") ||
!stricmp(copy, "cu."))
{
if (strstr(dev, "OBD"))
return dt_elm327;
else
return dt_can232;
}
#if !defined(__MAC__)
if (!strcmp(copy, "can") ||
!strncmp(dev, "slcan", 5) ||
!strncmp(dev, "vcan", 4))
{
return dt_can;
}
#endif
#elif defined(__WINDOWS__)
if (!stricmp(copy, "com"))
{
return dt_can232;
}
if (!stricmp(copy, "ED0"))
{
return dt_8dev;
}
#endif
return dt_unknown;
}
bool NCanUtils::IsMqtt(const char * arg, const char * AddrAndPort)
{
bool mqtt = !strncmp((char *) arg, "mqtt", 4);
AddrAndPort = NULL;
if (mqtt)
{
// mqtt@192.168.0.62:1883
arg += 4;
if (*arg++ == '@')
AddrAndPort = arg;
};
return mqtt;
}
KCanFrame::KCanFrame()
{
Init();
}
void KCanFrame::Init()
{
memset(this, 0, sizeof(*this));
}
bool KCanFrame::InitElsterFrame(unsigned short sender_id,
unsigned short receiver_id,
unsigned short elster_idx)
{
Init();
Id = sender_id;
Data[0] = 1; // receive flag
bool Ok = SetReceiverId(receiver_id);
if (!SetElsterIdx(elster_idx))
Ok = false;
return Ok;
}
bool KCanFrame::EqualData(const KCanFrame & Frame) const
{
if (Len != Frame.Len)
return false;
for (int i = 0; i < Len; i++)
if (Data[i] != Frame.Data[i])
return false;
return true;
}
bool KCanFrame::Changed(const KCanFrame & SendFrame) const
{
if (Len < 5 || !IsAnswerToElsterIndex(SendFrame))
return false;
return GetValue() == SendFrame.GetValue();
}
int KCanFrame::GetReceiverId() const
{
// 0x.79 ist keine ID (passive Abfrage)
// 480 00 [7] a0 79 fa 4e 30 00 00
// { 0x579, 0x4e30, 0x0000},
if (Len < 2 || Data[1] == 0x79)
return -1;
return ((Data[0] & 0xf0) << 3) + (Data[1] & 0x7f);
}
int KCanFrame::GetIdToValue() const
{
if (Len < 6)
return -1;
// Wert wird verändert. Als Id zählt der Empfänger!
if ((Data[0] & 0x0f) == 0x00)
{
return GetReceiverId();
} else
return Id;
}
bool KCanFrame::SetReceiverId(unsigned short id)
{
if (id > 0x7ff)
return false;
Data[0] = (Data[0] & 0xf) | ((id >> 3) & 0xf0);
Data[1] = id & 0x7f;
return true;
}
bool KCanFrame::IsAnswer(const KCanFrame & Frame) const
{
// CAN-Ids vergleichen
return (int) Id == Frame.GetReceiverId() && GetReceiverId() == (int) Frame.Id;
}
bool KCanFrame::IsAnswerToElsterIndex(const KCanFrame & Frame) const
{
return IsAnswer(Frame) &&
GetElsterIdx() == Frame.GetElsterIdx();
}
bool KCanFrame::Supported() const
{
int val = GetValue();
return val >= 0 && val != 0x8000;
}
bool KCanFrame::HasValue() const
{
return GetValue() >= 0;
}
int KCanFrame::GetValue() const
{
if (Len < 5 || Len > 7)
return -1;
if (Data[2] == 0xfa)
{
if (Len != 7)
return -1;
return 256*Data[5] + Data[6];
}
return 256*Data[3] + Data[4];
}
bool KCanFrame::SetValue(unsigned short value)
{
if (Len != 5 && Len != 7)
return false;
if (Data[2] == 0xfa)
{
Data[5] = value >> 8;
Data[6] = value & 0xff;
Len = 7;
} else {
Data[3] = value >> 8;
Data[4] = value & 0xff;
}
return true;
}
int KCanFrame::GetElsterIdx() const
{
if (Len > 7 || Len < 3)
return -1;
if (Data[2] == 0xfa)
{
if (Len < 5)
return -1;
return 256*Data[3] + Data[4];
} else
return Data[2];
}
bool KCanFrame::SetElsterIdx(unsigned short idx)
{
#if defined(__USE_SHORT__)
if (idx < 0x100)
{
Len = 5;
Data[2] = (unsigned char) idx;
} else
#endif
{
Len = 7;
Data[2] = 0xfa;
Data[3] = (unsigned char) (idx >> 8);
Data[4] = (unsigned char) idx;
}
#if defined(__ROTEX__)
Len = 7;
#endif
return idx != 0xfa;
}
bool KCanFrame::GetDataFromStream(const char * str)
{
Len = 0;
for ( ; Len < 8; Len++)
{
while (*str == ' ' || *str == '.')
str++;
if (!NUtils::GetHexByte(str, Data[Len]))
break;
}
while (*str == ' ' || *str == '.')
str++;
return !*str || *str == '\n' || *str == '\r';
}
void KCanFrame::FormMySqlInsert(char * statement) const
{
char fields[256];
char values[256];
char str[64];
NUtils::PrintMs(TimeStampDay, TimeStampMs, str);
strcpy(fields, "counter,timestamp,can_id");
sprintf(values, "%u,%s,%u",
Counter & ~ 0x80000000, str, Id & ~ 0x80000000);
for (int i = 0; i < Len; i++)
{
sprintf(fields + strlen(fields), ",d%d", i);
sprintf(values + strlen(values), ",%d", Data[i]);
}
sprintf(statement, "INSERT can_log(%s) VALUES(%s);", fields, values);
}
void KCanFrame::FormHttpVariables(char * stream) const
{
char time_str[64];
NUtils::PrintMs(TimeStampDay, TimeStampMs, time_str);
sprintf(stream, "counter=%d×tamp=%s&can_id=%d",
Counter & ~ 0x80000000, time_str, Id & ~ 0x80000000);
for (int i = 0; i < Len; i++)
{
sprintf(stream + strlen(stream), "&d%d=%d", i, Data[i]);
}
}
bool KCanFrame::SetElsterFrame(const char * str)
{
// format: 302.0016
unsigned recv_id;
unsigned elster_idx;
bool Ok;
Ok = NUtils::IsHexDigit(*str);
if (Ok)
{
recv_id = NUtils::GetHex(str);
Ok = *str == '.';
if (Ok)
{
str++;
Ok = NUtils::IsHexDigit(*str);
}
}
if (Ok)
{
elster_idx = NUtils::GetHex(str);
Ok = !*str;
}
if (Ok)
Ok = elster_idx <= 0xffff && !(recv_id & ~0x787);
if (Ok)
Ok = InitElsterFrame(0, recv_id, elster_idx);
return Ok;
}
static bool GetInteger(unsigned & num, const char * & Line)
{
while (*Line == ' ' || *Line == ',') Line++;
TInt64 res;
unsigned inc = NUtils::GetInt(res, Line);
if (!inc)
return false;
Line += inc;
num = (unsigned) res;
return true;
}
bool KCanFrame::SetElsterFrame2(const char * str)
{
// format: { 0x180, 0x0001, 0x0000},
Init();
while (*str && *str != '\n' && *str != '\r' && *str != '{')
str++;
if (*str != '{')
return false;
str++;
if (!GetInteger(Id, str) || Id >= 0x8000)
return false;
unsigned res;
if (!GetInteger(res, str) || res >= 0x10000)
return false;
if (!SetElsterIdx((unsigned short) res))
return false;;
if (!GetInteger(res, str) || res >= 0x10000)
return false;
SetValue((unsigned short) res);
return true;
}
void KCanFrame::PrintTime(char * OutStr, bool csv) const
{
NUtils::PrintTime(TimeStampDay, TimeStampMs, OutStr, csv);
}
void KCanFrame::PrintMs(int days, int ms, char * stream)
{
int time_stamp_1 = days;
int time_stamp_2;
time_stamp_1 += 30*365 + 30 / 4; // Tage zwischen 1970 und 2000
time_stamp_1 *= 24*36; // 86'4 * 10'957 = 9'466'848
time_stamp_1 += ms / 100000;
if (ms < 0)
{
time_stamp_2 = 100000 + ms % 100000;
time_stamp_1--;
} else
time_stamp_2 = ms % 100000;
// GMT time_stamp_1 -= 2*36;
// ./ms2time.sh 1'399'346'245'967 ==> 2014-05-06 05:17:25
// date -d "2014-05-06 05:17:25" ==> 1'399'346'245 (03:17:25 einsetzen GMT!)
sprintf(stream, "%d%5.5d", time_stamp_1, time_stamp_2);
}
void KCanFrame::PrintDateTime(int days, int ms, char * stream)
{
// format: YYYY-MM-DD HH:MM:SS.mmm
int Day, Month, Year;
char day_str[32];
NUtils::GetDateFromDays(days, Day, Month, Year);
NUtils::PrintDayTime(ms, day_str);
sprintf(stream, "%4.4d-%2.2d-%2.2d %s", Year, Month, Day, day_str);
}
void KCanFrame::FormatFrame(bool csv, char * OutStr, bool UseElster) const
{
char TimeStr[64];
PrintTime(TimeStr, csv);
if (csv)
{
sprintf(OutStr, "%u,%s,%u,%d", Counter, TimeStr,
Id & 0xffffffff, Flags & 0xff);
for (int i = 0; i < 8; i++)
{
strcat(OutStr, ",");
if (i < Len)
sprintf(OutStr + strlen(OutStr), "%d", Data[i]);
else
strcat(OutStr, "NULL");
}
} else {
if (!Counter && !TimeStampDay)
sprintf(OutStr, "%8x %2.2x [%d] ",
Id & 0x1fffffff, Flags & 0xff, Len);
else
sprintf(OutStr, "%3u %s %8x %2.2x [%d] ", Counter, TimeStr,
Id & 0x1fffffff, Flags & 0xff, Len);
for (int i = 0; i < 8; i++)
if (i < Len)
sprintf(OutStr + strlen(OutStr), "%2.2x ", Data[i]);
else
sprintf(OutStr + strlen(OutStr), " ");
strcat(OutStr, " ");
if (UseElster)
{
int elster_idx = GetElsterIdx();
if (elster_idx >= 0)
{
const ElsterIndex * elst_ind = GetElsterIndex(elster_idx);
int Value = GetValue();
int d = Data[0] & 0xf;
if (Value >= 0 && d != 1) // 1 is request flag
{
if (elst_ind)
SetValueType(OutStr + strlen(OutStr), elst_ind->Type, (unsigned short) Value);
else
sprintf(OutStr + strlen(OutStr), "%d", (unsigned) Value);
strcat(OutStr, " ");
}
strcat(OutStr, elst_ind ? elst_ind->Name : "?");
}
} else {
for (int i = 0; i < 8; i++)
if (i < Len)
sprintf(OutStr + strlen(OutStr), "%c",
' ' <= Data[i] && Data[i] < 127 ? Data[i] : '.');
}
}
strcat(OutStr, "\n");
}
void KCanFrame::PrintFrame() const
{
char Buffer[256];
FormatFrame(false, Buffer);
printf("%s", Buffer);
}
bool KCanFrame::InitFromLog(const char * log)
{
// 225811 24.7.2014 00:00:05:437 480 00 [7] a1 00 fa 07 a8 00 00 .......
//
// alternativ (von CS_Bruecke)
//
// send: 0d 00 06 01 02 fa 00 08 00 c5 01 dd
// recv: 06 02 0d 02 00 fa 00 08 00 c5 01 de
// Elster 302.0008.00c5: RAUMSOLLTEMP_NACHT = 19.7
bool Ok = true;
unsigned Day, Month, Year;
unsigned h, m, s, s1000;
unsigned u, cnt = 0;
Init();
Counter = 0;
NUtils::GetUnsigned(log, Counter);
if (!strncmp(log, "Elster", 6))
{
return false;
}
if (!strncmp(log, "send: ", 6) ||
!strncmp(log, "recv: ", 6))
{
log += 6;
KComfortFrame cf;
Ok = cf.SetFromString(log, false);
if (Ok)
Ok = cf.SetCanFrame(*this);
return Ok;
}
// Datum
NUtils::SkipBlanks(log);
if (Ok)
Ok = NUtils::GetUnsigned(log, Day);
if (Ok)
Ok = *log == '.';
if (Ok)
{
log++;
Ok = NUtils::GetUnsigned(log, Month);
}
if (Ok)
Ok = *log == '.';
if (Ok)
{
log++;
Ok = NUtils::GetUnsigned(log, Year);
}
if (Ok)
NUtils::GetDays(Year, Month, Day, TimeStampDay);
// Zeit
NUtils::SkipBlanks(log);
if (Ok)
Ok = NUtils::GetUnsigned(log, h);
if (Ok)
Ok = *log == ':';
if (Ok)
{
log++;
Ok = NUtils::GetUnsigned(log, m);
}
if (Ok)
Ok = *log == ':';
if (Ok)
{
log++;
Ok = NUtils::GetUnsigned(log, s);
}
if (Ok)
Ok = *log == ':' || *log == '.';
if (Ok)
{
log++;
Ok = NUtils::GetUnsigned(log, s1000);
}
if (Ok)
TimeStampMs = 1000*(3600*h + 60*m + s) + s1000;
// Id
NUtils::SkipBlanks(log);
if (Ok)
{
int id = NUtils::GetHex(log);
Id = (unsigned) id;
Ok = id > 0 && Id <= 0x79f;
}
NUtils::SkipBlanks(log);
NUtils::GetUnsigned(log, u);
NUtils::SkipBlanks(log);
if (Ok && *log == '[')
log++;
else
Ok = false;
if (Ok)
Ok = NUtils::GetUnsigned(log, cnt);
if (Ok)
Ok = cnt == 5 || cnt == 7;
Len = (int) cnt;
if (Ok && *log == ']')
log++;
else
Ok = false;
if (Ok)
{
for (int i = 0; i < Len; i++)
{
NUtils::SkipBlanks(log);
Data[i] = NUtils::GetHex(log);
}
}
return Ok;
}
////////////////////////////////////////////////////////////////////////////////
KComfortFrame KComfortFrame::InitFrame = {
{0x0d, 0x00, 0x0d, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26}
};
const KComfortFrame::t_scan_geraete_id KComfortFrame::scan_geraete_id[] =
{
{ 0x601, 0x000b},
{ 0x301, 0x000b},
{ 0x480, 0x000b},
{ 0x500, 0x000b},
// http://www.ip-symcon.de/forum/threads/15888-Wolf-CSZ-mit-ISM1
// eventuell mit direkter RS232-Schnittststelle an WP
{ 0x000, 0x015b}, // GERAETEKONFIGURATION_2
{ 0x601, 0x0199}, // WPMiw (Xanatos) und WPM3 (Wert von 0x019a gehört auch dazu)
{ 0x301, 0x0199},
{ 0x480, 0x0199},
{ 0x500, 0x0199},
{ 0x000, 0x0199},
{ 0x000, 0x0000} // Ende der List
};
unsigned short KComfortFrame::InitId = 0xa51b;
unsigned KComfortFrame::GetCheckSum() const
{
return 256*Data[10] + Data[11];
}
unsigned KComfortFrame::GetSum() const
{
unsigned Sum = 0;
for (int i = 0; i < 10; i++)
Sum += Data[i];
return Sum;
}
bool KComfortFrame::CheckSum() const
{
return GetSum() == GetCheckSum();
}
void KComfortFrame::SetCheckSum()
{
unsigned Sum = GetSum();
Data[10] = (unsigned char)(Sum >> 8);
Data[11] = (unsigned char)(Sum & 0xff);
}
bool KComfortFrame::SetCanFrame(KCanFrame & Frame) const
{
Frame.Init();
Frame.Len = 7;
Frame.Id = ((Data[0] & 0x0f) << 7) + (Data[1] & 0xf);
Frame.Data[0] = (Data[2] << 4) + (Data[3] & 0x0f);
for (int i = 0; i < 6; i++)
Frame.Data[i + 1] = Data[i + 4];
return CheckSum() &&
(Data[0] & 0xf0) == 0; // 11-Bit-Id
}
bool KComfortFrame::GetCanFrame(const KCanFrame & Frame)
{
Data[0] = (Frame.Id >> 7) & 0x0f;
Data[1] = Frame.Id & 0x0f;
Data[2] = Frame.Data[0] >> 4;
Data[3] = Frame.Data[0] & 0x0f; // Sollte zum Abfragen 1 und für eine Antwort 2 sein!
for (int i = 0; i < 6; i++)
Data[i + 4] = Frame.Data[i + 1];
if (Frame.Len == 5)
{
Data[8] = 0x80; // Stopf-Bytes initialisieren (siehe Marc2012)
Data[9] = 0x08;
}
SetCheckSum();
return (Frame.Len == 7 || Frame.Len == 5) &&
(Frame.Id & ~0x7ff) == 0; // 11-Bit-Id
}
void KComfortFrame::SetOk(bool Ok)
{
for (int i = 0; i < 10; i++)
Data[i] = Ok ? 0x55 : 0xAA;
SetCheckSum();
}
void KComfortFrame::SetOkValue(unsigned Value)
{
SetOk();
Data[8] = (unsigned char)(Value >> 8);
Data[9] = (unsigned char)(Value & 0xff);
SetCheckSum();
}
bool KComfortFrame::EqualData(const KComfortFrame & Frame) const
{
for (int i = 0; i < 12; i++)
if (Data[i] != Frame.Data[i])
return false;
return true;
}
void KComfortFrame::SetToString(char * Str) const
{
Str[0] = 0;
for (int i = 0; i < 12; i++)
sprintf(Str + strlen(Str), "%2.2x", Data[i]);
}
bool KComfortFrame::SetFromString(const char * Str, bool AppendCheck)
{
for (int i = 0; i < 12; i++)
{
while (*Str == ' ' || *Str == '.') Str++;
if (!NUtils::GetHexByte(Str, Data[i]))
{
if (!AppendCheck ||
i != 10)
return false;
SetCheckSum();
return true;
}
}
return true;
}
bool KComfortFrame::IsInitFrame() const
{
// 0d 00 00 00 00 fa 01 5b 00 00 01 63
if (Data[0] == 0x0d && Data[1] == 0x00 &&
Data[2] == 0x00 && Data[3] == 0x00 &&
Data[4] == 0x00 && Data[5] == 0xfa &&
Data[6] == 0x01 && Data[7] == 0x5b)
{
return true;
}
return EqualData(InitFrame);
}
int KComfortFrame::OkFrameValue() const
{
if (!CheckSum())
return -1;
for (int i = 0; i < 8; i++)
if (Data[i] != 0x55)
return -1;
return 256*Data[8] + Data[9];
}
void KComfortFrame::SetInitOk()
{
SetOkValue(InitId);
}
KComfortFrame::msg_type KComfortFrame::GetMsgType() const
{
if (IsInitFrame())
return mt_init;
// Vorsichtsmassnahmen:
// - nur reine Abfragen zulassen
// - Abfragen ohne 0xfa an das Gerät mit ID = 000 kann Nebenwirkungen
// auslösen! (deshalb nicht zulassen)
if (Data[3] == 0x01) // Abfrage
{
if ((Data[4] & 0xf0) == 0 && // reine Abfragen
(Data[5] == 0xfa || Data[5] == 0xfb || Data[5] == 0x10 || Data[2] != 0))
return mt_get;
return mt_invalid;
}
if (Data[3] == 0x00) // change
{
// Reset der Fehlerliste: 0d.00.09.00.00.fb.80.00.00.00.01.91 Mudultype: Manager
// abtauen manual: 0d.00.09.00.00.5f.00.00.00.00.00.75
if ((Data[4] & 0xf0) == 0 &&
Data[5] != 0xfa)
return mt_short_change;
// Wert ändern
// 0d 00 03-00-00 fa ... (marc2012)
if ((Data[4] & 0xf0) == 0 &&
Data[5] == 0xfa && Data[2] != 0)
return mt_change;
}
return mt_invalid;
}
////////////////////////////////////////////////////////////////////////////////
KCan232Frame::KCan232Frame(const char * str)
{
unsigned len = 0;
if (str)
len = (unsigned) strlen(str);
if (len >= High(msg))
len = High(msg);
if (len)
strncpy(msg, str, len);
msg[len] = 0;
}
bool KCan232Frame::SetCanFrame(KCanFrame & Frame) const
{
Frame.Init();
const char * Ptr = msg + 1;
unsigned char b[4];
if (!NUtils::GetHexByte(Ptr, b[0]))
return false;
switch (msg[0])
{
case 't':
case 'r':
if (b[0] > 0x7f ||
!NUtils::IsHexDigit(*Ptr))
return false;
Frame.Id = 16*b[0] + NUtils::GetHexDigit(*Ptr++);
break;
case 'T':
case 'R':
if (b[0] > 0x3f ||
!NUtils::GetHexByte(Ptr, b[1]) ||
!NUtils::GetHexByte(Ptr, b[2]) ||
!NUtils::GetHexByte(Ptr, b[3]))
return false;
for (int i = 0; i < 4; i++)
Frame.Id = 256*Frame.Id + b[i];
Frame.Id |= 0x80000000;
break;
default:
return false;
}
if (msg[0] == 'r' || msg[0] == 'R') // RTR
Frame.Id |= 0x40000000;
if (*Ptr < '0' || *Ptr > '8')
return false;
Frame.Len = *Ptr++ - '0';
for (int i = 0; i < Frame.Len; i++)
if (!NUtils::GetHexByte(Ptr, Frame.Data[i]))
return false;
return true;
}
bool KCan232Frame::IsOk() const
{
KCanFrame Frame;
return SetCanFrame(Frame);
}
bool KCan232Frame::GetCanFrame(const KCanFrame & Frame)
{
if (Frame.Id & 0x80000000)
sprintf(msg, "T%8.8X%1.1X", Frame.Id & 0x3ffffff, Frame.Len & 0xf);
else
sprintf(msg, "t%3.3X%1.1X", Frame.Id & 0x7ff, Frame.Len & 0xf);
// RTR
if (Frame.Id & 0x40000000)
msg[0] += 'R' - 'T';
for (int i = 0; i < Frame.Len; i++)
sprintf(msg + strlen(msg), "%2.2X", Frame.Data[i]);
return true;
}
KCan232Frame & KCan232Frame::operator =(const char * str)
{
unsigned l = 0;
int m = 0;
while (str && str[m] && l < High(msg))
{
if (str[m] > ' ')
msg[l++] = str[m];
m++;
}
msg[l] = 0;
return *this;
}
////////////////////////////////////////////////////////////////////////////////
KCanELM327Frame::KCanELM327Frame(const char * str)
{
unsigned len = 0;
if (str)
len = (unsigned) strlen(str);
if (len >= High(msg))
len = High(msg);
if (len)
strncpy(msg, str, len);
msg[len] = 0;
}
bool KCanELM327Frame::SetCanFrame(KCanFrame & Frame) const
{
Frame.Init();
const char * Ptr = msg + 1;
unsigned char b[4];
if (!NUtils::GetHexByte(Ptr, b[0]))
return false;
switch (msg[0])
{
case 't':
case 'r':
if (b[0] > 0x7f ||
!NUtils::IsHexDigit(*Ptr))