forked from netdata-be/libnodave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodave.c
7110 lines (6573 loc) · 198 KB
/
nodave.c
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
/*
Part of Libnodave, a free communication libray for Siemens S7 200/300/400 via
the MPI adapter 6ES7 972-0CA22-0XAC
or MPI adapter 6ES7 972-0CA23-0XAC
or TS adapter 6ES7 972-0CA33-0XAC
or MPI adapter 6ES7 972-0CA11-0XAC,
IBH/MHJ-NetLink or CPs 243, 343 and 443
(C) Thomas Hergenhahn ([email protected]) 2002..2005
S5 basic communication parts (C) Andrew Rostovtsew 2004.
Libnodave is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Libnodave 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 General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with Libnodave; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "nodave.h"
#include <stdio.h>
#include "log2.h"
#include <string.h>
//#define DEBUG_CALLS // Define this and recompile to get parameters and results
// of each function call printed. I could have made this an
// option bit in daveDebug, but most applications will never,never
// need it. Still they would have to do the jumps...
// This option is just useful when developing bindings to new
// programming languages.
/**
Library specific:
**/
#ifdef LINUX
#define HAVE_UNISTD
#define HAVE_SELECT
#define DECL2
#include <time.h>
#include <sys/time.h>
#endif
#ifdef HAVE_UNISTD
#include <unistd.h>
#define daveWriteFile(a,b,c,d) d=write(a,b,c)
#endif
#ifdef AVR
#include <unistd.h>
#endif
int daveDebug=0;
#ifdef BCCWIN
#include <winsock2.h>
#include "openS7online.h" // We can use the Siemens transport dlls only on Windows
void setTimeOut(daveInterface * di, int tmo) {
COMMTIMEOUTS cto;
#ifdef DEBUG_CALLS
LOG3("setTimeOut(di:%p, time:%d)\n", di,tmo);
FLUSH;
#endif
// if(di->fd.connectionType==daveSerialConnection) {
GetCommTimeouts(di->fd.rfd, &cto);
cto.ReadIntervalTimeout=0;
cto.ReadTotalTimeoutMultiplier=0;
cto.ReadTotalTimeoutConstant=tmo/1000;
SetCommTimeouts(di->fd.rfd,&cto);
// } else if(di->fd.connectionType==daveTcpConnection) {
// }
}
#endif
#ifdef HAVE_SELECT
int DECL2 stdwrite(daveInterface * di, char * buffer, int length) {
if (daveDebug & daveDebugByte)
_daveDump("I send", (uc*)buffer, length);
return write(di->fd.wfd, buffer,length);
}
int DECL2 stdread(daveInterface * di, char * buffer, int length) {
fd_set FDS;
struct timeval t;
int i;
t.tv_sec = di->timeout / 1000000;
t.tv_usec = (di->timeout % 1000000);
FD_ZERO(&FDS);
FD_SET(di->fd.rfd, &FDS);
i=0;
if(select(di->fd.rfd + 1, &FDS, NULL, NULL, &t)>0) {
i=read(di->fd.rfd, buffer, length);
}
// if (daveDebug & daveDebugByte)
// _daveDump("got",buffer,i);
return i;
}
#endif
#ifdef BCCWIN
int DECL2 stdread(daveInterface * di,
char * buffer,
int length) {
unsigned long i;
ReadFile(di->fd.rfd, buffer, length, &i, NULL);
// if (daveDebug & daveDebugByte)
// _daveDump("got",buffer,i);
return i;
}
int DECL2 stdwrite(daveInterface * di, char * buffer, int length) {
unsigned long i;
if (daveDebug & daveDebugByte)
_daveDump("I send",buffer,length);
// EscapeCommFunction(di->fd.rfd, CLRRTS); // patch from Keith Harris. He says:
//******* this is what microwin does (needed for usb-serial)
WriteFile(di->fd.rfd, buffer, length, &i,NULL);
// patch from Andrea. He says:
// In this way the PC Adapter connected to CPU313C hangs waiting for RTS line before answering back.
// Added the following to regain answers:
// EscapeCommFunction(di->fd.rfd, SETRTS);
return i;
}
#endif
/*
Setup a new interface structure from an initialized serial interface's handle and a name.
*/
daveInterface * DECL2 daveNewInterface(_daveOSserialType nfd, char * nname, int localMPI, int protocol, int speed){
daveInterface * di=(daveInterface *) calloc(1, sizeof(daveInterface));
#ifdef DEBUG_CALLS
LOG7("daveNewInterface(fd.rfd:%d fd.wfd:%d name:%s local MPI:%d protocol:%d PB speed:%d)\n",
nfd.rfd,nfd.wfd,nname, localMPI, protocol, speed);
FLUSH;
#endif
if (di) {
// di->name=nname;
strncpy(di->realName,nname,20);
di->name=di->realName;
di->fd=nfd;
di->localMPI=localMPI;
di->protocol=protocol;
di->timeout=1000000; /* 1 second */
di->nextConnection=0x14;
di->speed=speed;
di->getResponse=_daveGetResponseISO_TCP;
di->ifread=stdread;
di->ifwrite=stdwrite;
di->initAdapter=_daveReturnOkDummy;
di->connectPLC=_daveReturnOkDummy2;
di->disconnectPLC=_daveReturnOkDummy2;
di->disconnectAdapter=_daveReturnOkDummy;
di->listReachablePartners=_daveListReachablePartnersDummy;
switch (protocol) {
case daveProtoMPI:
di->initAdapter=_daveInitAdapterMPI1;
di->connectPLC=_daveConnectPLCMPI1;
di->disconnectPLC=_daveDisconnectPLCMPI;
di->disconnectAdapter=_daveDisconnectAdapterMPI;
di->exchange=_daveExchangeMPI;
di->sendMessage=_daveSendMessageMPI;
di->getResponse=_daveGetResponseMPI;
di->listReachablePartners=_daveListReachablePartnersMPI;
break;
case daveProtoMPI2:
case daveProtoMPI4:
di->initAdapter=_daveInitAdapterMPI2;
di->connectPLC=_daveConnectPLCMPI2;
di->disconnectPLC=_daveDisconnectPLCMPI;
di->disconnectAdapter=_daveDisconnectAdapterMPI;
di->exchange=_daveExchangeMPI;
di->sendMessage=_daveSendMessageMPI;
di->getResponse=_daveGetResponseMPI;
di->listReachablePartners=_daveListReachablePartnersMPI;
di->nextConnection=0x3;
break;
case daveProtoMPI3:
di->initAdapter=_daveInitAdapterMPI3;
di->connectPLC=_daveConnectPLCMPI3;
di->disconnectPLC=_daveDisconnectPLCMPI3;
di->disconnectAdapter=_daveDisconnectAdapterMPI3;
di->exchange=_daveExchangeMPI3;
di->sendMessage=_daveSendMessageMPI3;
di->getResponse=_daveGetResponseMPI3;
di->listReachablePartners=_daveListReachablePartnersMPI3;
di->nextConnection=0x3;
break;
case daveProtoISOTCP:
case daveProtoISOTCP243:
case daveProtoISOTCPR: // routing over MPI network
di->getResponse=_daveGetResponseISO_TCP;
di->connectPLC=_daveConnectPLCTCP;
di->exchange=_daveExchangeTCP;
break;
case daveProtoPPI:
di->getResponse=_daveGetResponsePPI;
di->exchange=_daveExchangePPI;
di->connectPLC=_daveConnectPLCPPI;
di->timeout=150000; /* 0.15 seconds */
break;
case daveProtoMPI_IBH:
di->exchange=_daveExchangeIBH;
di->connectPLC=_daveConnectPLC_IBH;
di->disconnectPLC=_daveDisconnectPLC_IBH;
di->sendMessage=_daveSendMessageMPI_IBH;
di->getResponse=_daveGetResponseMPI_IBH;
di->listReachablePartners=_daveListReachablePartnersMPI_IBH;
break;
case daveProtoPPI_IBH:
di->exchange=_daveExchangePPI_IBH;
di->connectPLC=_daveConnectPLCPPI;
di->sendMessage=_daveSendMessageMPI_IBH;
di->getResponse=_daveGetResponsePPI_IBH;
di->listReachablePartners=_daveListReachablePartnersMPI_IBH;
break;
case daveProtoS7online:
di->exchange=_daveExchangeS7online;
di->connectPLC=_daveConnectPLCS7online;
di->sendMessage=_daveSendMessageS7online;
di->getResponse=_daveGetResponseS7online;
di->listReachablePartners=_daveListReachablePartnersS7online;
// di->disconnectAdapter=_daveDisconnectAdapterS7online;
break;
case daveProtoAS511:
di->connectPLC=_daveConnectPLCAS511;
di->disconnectPLC=_daveDisconnectPLCAS511;
di->exchange=_daveFakeExchangeAS511;
di->sendMessage=_daveFakeExchangeAS511;
break;
case daveProtoNLpro:
di->initAdapter=_daveInitAdapterNLpro;
di->connectPLC=_daveConnectPLCNLpro;
di->disconnectPLC=_daveDisconnectPLCNLpro;
di->disconnectAdapter=_daveDisconnectAdapterNLpro;
di->exchange=_daveExchangeNLpro;
di->sendMessage=_daveSendMessageNLpro;
di->getResponse=_daveGetResponseNLpro;
di->listReachablePartners=_daveListReachablePartnersNLpro;
break;
}
#ifdef BCCWIN
setTimeOut(di, di->timeout);
#endif
}
return di;
}
daveInterface * DECL2 davePascalNewInterface(_daveOSserialType* nfd, char * nname, int localMPI, int protocol, int speed){
#ifdef DEBUG_CALLS
LOG7("davePascalNewInterface(fd.rfd:%d fd.wfd:%d name:%s local MPI:%d protocol:%d PB speed:%d)\n",
nfd->rfd,nfd->wfd,nname, localMPI, protocol, speed);
FLUSH;
#endif
return daveNewInterface(*nfd,nname, localMPI, protocol, speed);
}
/*
debugging:
set debug level by setting variable daveDebug. Debugging is split into
several topics. Output goes to stderr.
The file descriptor is written after the module name, so you may
distinguish messages from multiple connections.
naming: all stuff begins with dave... to avoid conflicts with other
namespaces. Things beginning with _dave.. are not intended for
public use.
*/
void DECL2 daveSetDebug(int nDebug) {
#ifdef DEBUG_CALLS
LOG2("daveSetDebug(%d)\n",nDebug);
FLUSH;
#endif
daveDebug=nDebug;
}
int DECL2 daveGetDebug() {
#ifdef DEBUG_CALLS
LOG1("daveGetDebug()\n");
FLUSH;
#endif
return daveDebug;
}
/**
C# interoperability:
**/
void DECL2 daveSetTimeout(daveInterface * di, int tmo) {
#ifdef DEBUG_CALLS
LOG3("daveSetTimeOut(di:%p, time:%d)\n", di,tmo);
#endif
di->timeout=tmo;
#ifdef BCCWIN
setTimeOut(di,tmo);
#endif
}
int DECL2 daveGetTimeout(daveInterface * di) {
#ifdef DEBUG_CALLS
LOG2("daveGetTimeOut(di:%p)\n",di);
FLUSH;
#endif
return di->timeout;
}
char * DECL2 daveGetName(daveInterface * di) {
#ifdef DEBUG_CALLS
LOG2("daveGetName(di:%p)\n",di);
FLUSH;
#endif
return di->name;
}
int DECL2 daveGetMPIAdr(daveConnection * dc) {
#ifdef DEBUG_CALLS
LOG2("daveGetMPIAdr(dc:%p)\n",dc);
FLUSH;
#endif
return dc->MPIAdr;
}
int DECL2 daveGetAnswLen(daveConnection * dc) {
#ifdef DEBUG_CALLS
LOG2("daveGetAnswLen(dc:%p)\n",dc);
FLUSH;
#endif
return dc->AnswLen;
}
int DECL2 daveGetMaxPDULen(daveConnection * dc) {
#ifdef DEBUG_CALLS
LOG2("daveGetMaxPDULen(dc:%p)\n",dc);
FLUSH;
#endif
return dc->maxPDUlength;
}
/**
PDU handling:
**/
/*
set up the header. Needs valid header pointer
*/
void DECL2 _daveInitPDUheader(PDU * p, int type) {
memset(p->header, 0, sizeof(PDUHeader));
if (type==2 || type==3)
p->hlen=12;
else
p->hlen=10;
p->param=p->header+p->hlen;
((PDUHeader*)p->header)->P=0x32;
((PDUHeader*)p->header)->type=type;
p->dlen=0;
p->plen=0;
p->udlen=0;
p->data=NULL;
p->udata=NULL;
}
/*
add parameters after header, adjust pointer to data.
needs valid header
*/
void DECL2 _daveAddParam(PDU * p,uc * param,us len) {
#ifdef ARM_FIX
us tmplen;
#endif
p->plen=len;
memcpy(p->param, param, len);
#ifdef ARM_FIX
tmplen=daveSwapIed_16(len);
memcpy(&(((PDUHeader*)p->header)->plen),&tmplen,sizeof(us));
#else
((PDUHeader*)p->header)->plen=daveSwapIed_16(len);
#endif
p->data=p->param+len;
p->dlen=0;
}
/*
add data after parameters, set dlen
needs valid header,parameters
*/
void DECL2 _daveAddData(PDU * p,void * data,int len) {
#ifdef ARM_FIX
us tmplen;
#endif
uc * dn= p->data+p->dlen;
p->dlen+=len;
memcpy(dn, data, len);
#ifdef ARM_FIX
tmplen=daveSwapIed_16(p->dlen);
memcpy(&(((PDUHeader*)p->header)->dlen),&tmplen,sizeof(us));
#else
((PDUHeader*)p->header)->dlen=daveSwapIed_16(p->dlen);
#endif
}
/*
add values after value header in data, adjust dlen and data count.
needs valid header,parameters,data,dlen
*/
void DECL2 _daveAddValue(PDU * p,void * data,int len) {
us dCount;
uc * dtype;
dtype=p->data+p->dlen-4+1; /* position of first byte in the 4 byte sequence */
#ifdef ARM_FIX
memcpy(&dCount, (p->data+p->dlen-4+2), sizeof(us));
#else
dCount=* ((us *)(p->data+p->dlen-4+2)); /* changed for multiple write */
#endif
dCount=daveSwapIed_16(dCount);
if (daveDebug & daveDebugPDU)
LOG2("dCount: %d\n", dCount);
if (*dtype==4) { /* bit data, length is in bits */
dCount+=8*len;
} else if (*dtype==9) { /* byte data, length is in bytes */
dCount+=len;
} else if (* dtype==3) { /* bit data, length is in bits */
dCount+=len;
} else {
if (daveDebug & daveDebugPDU)
LOG2("unknown data type/length: %d\n", *dtype);
}
if (p->udata==NULL) p->udata=p->data+4;
p->udlen+=len;
if (daveDebug & daveDebugPDU)
LOG2("dCount: %d\n", dCount);
dCount=daveSwapIed_16(dCount);
#ifdef ARM_FIX
memcpy((p->data+p->dlen-4+2), &dCount, sizeof(us));
#else
*((us *)(p->data+p->dlen-4+2))=dCount;
#endif
_daveAddData(p, data, len);
}
/*
add data in user data. Add a user data header, if not yet present.
*/
void DECL2 _daveAddUserData(PDU * p, uc * da, int len) {
uc udh[]={0xff,9,0,0};
if (p->dlen==0) {
if (daveDebug & daveDebugPDU)
LOG1("adding user data header.\n");
_daveAddData(p, udh, sizeof(udh));
}
_daveAddValue(p, da, len);
}
void DECL2 davePrepareReadRequest(daveConnection * dc, PDU *p) {
uc pa[]= {daveFuncRead,0};
#ifdef DEBUG_CALLS
LOG3("davePrepareReadRequest(dc:%p PDU:%p)\n", dc, p);
FLUSH;
#endif
p->header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(p,1);
_daveAddParam(p, pa, sizeof(pa));
}
PDU * DECL2 daveNewPDU() {
PDU * p;
p=(PDU*)malloc(sizeof(PDU));
#ifdef DEBUG_CALLS
LOG2("daveNewPDU() = %p\n",p);
FLUSH;
#endif
return p;
}
void DECL2 davePrepareWriteRequest(daveConnection * dc, PDU *p) {
uc pa[]= {daveFuncWrite, 0};
#ifdef DEBUG_CALLS
LOG3("davePrepareWriteRequest(dc:%p PDU:%p)\n", dc, p);
FLUSH;
#endif
p->header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(p,1);
_daveAddParam(p, pa, sizeof(pa));
p->dlen=0;
}
void DECL2 daveAddToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount, int isBit) {
uc pa[]= {
0x12, 0x0a, 0x10,
0x02, /* 1=single bit, 2=byte, 4=word */
0,0, /* length in bytes */
0,0, /* DB number */
0, /* area code */
0,0,0 /* start address in bits */
};
#ifdef ARM_FIX
us tmplen;
#endif
if ((area==daveAnaIn) || (area==daveAnaOut) /*|| (area==daveP)*/) {
pa[3]=4;
start*=8; /* bits */
} else if ((area==daveTimer) || (area==daveCounter)||(area==daveTimer200) || (area==daveCounter200)) {
pa[3]=area;
} else {
if(isBit) {
pa[3]=1;
} else {
start*=8; /* bit address of byte */
}
}
pa[4]=byteCount / 256;
pa[5]=byteCount & 0xff;
pa[6]=DBnum / 256;
pa[7]=DBnum & 0xff;
pa[8]=area;
pa[11]=start & 0xff;
pa[10]=(start / 0x100) & 0xff;
pa[9]=start / 0x10000;
p->param[1]++;
memcpy(p->param+p->plen, pa, sizeof(pa));
p->plen+=sizeof(pa);
#ifdef ARM_FIX
tmplen=daveSwapIed_16(p->plen);
memcpy(&(((PDUHeader*)p->header)->plen), &tmplen, sizeof(us));
#else
((PDUHeader*)p->header)->plen=daveSwapIed_16(p->plen);
#endif
p->data=p->param+p->plen;
p->dlen=0;
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(p);
}
}
void DECL2 daveAddVarToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount) {
#ifdef DEBUG_CALLS
LOG6("daveAddVarToReadRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d)\n",
p, daveAreaName(area), DBnum, start, byteCount);
FLUSH;
#endif
daveAddToReadRequest(p, area, DBnum, start, byteCount, 0);
}
void DECL2 daveAddBitVarToReadRequest(PDU *p, int area, int DBnum, int start, int byteCount) {
daveAddToReadRequest(p, area, DBnum, start, byteCount, 1);
}
void DECL2 daveAddToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount,
void * buffer,
uc * da,
int dasize,
uc * pa,
int pasize
) {
uc saveData[1024];
#ifdef ARM_FIX
us tmplen;
#endif
if ((area==daveTimer) || (area==daveCounter)||(area==daveTimer200) || (area==daveCounter200)) {
pa[3]=area;
pa[4]=((byteCount+1)/2) / 0x100;
pa[5]=((byteCount+1)/2) & 0xff;
} else if ((area==daveAnaIn) || (area==daveAnaOut)) {
pa[3]=4;
pa[4]=((byteCount+1)/2) / 0x100;
pa[5]=((byteCount+1)/2) & 0xff;
} else {
pa[4]=byteCount / 0x100;
pa[5]=byteCount & 0xff;
}
pa[6]=DBnum / 256;
pa[7]=DBnum & 0xff;
pa[8]=area;
pa[11]=start & 0xff;
pa[10]=(start / 0x100) & 0xff;
pa[9]=start / 0x10000;
if(p->dlen%2) {
_daveAddData(p, da, 1);
}
p->param[1]++;
if(p->dlen){
memcpy(saveData, p->data, p->dlen);
memcpy(p->data+pasize, saveData, p->dlen);
}
memcpy(p->param+p->plen, pa, pasize);
p->plen+=pasize;
#ifdef ARM_FIX
tmplen=daveSwapIed_16(p->plen);
memcpy(&(((PDUHeader*)p->header)->plen), &tmplen, sizeof(us));
#else
((PDUHeader*)p->header)->plen=daveSwapIed_16(p->plen);
#endif
p->data=p->param+p->plen;
_daveAddData(p, da, dasize);
_daveAddValue(p, buffer, byteCount);
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(p);
}
}
void DECL2 daveAddVarToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer) {
uc da[]= {0,4,0,0,};
uc pa[]= {
0x12, 0x0a, 0x10,
0x02, /* unit (for count?, for consistency?) byte */
0,0, /* length in bytes */
0,0, /* DB number */
0, /* area code */
0,0,0 /* start address in bits */
};
#ifdef DEBUG_CALLS
LOG7("daveAddVarToWriteRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n",
p, daveAreaName(area), DBnum, start, byteCount, buffer);
FLUSH;
#endif
daveAddToWriteRequest(p, area, DBnum, 8*start, byteCount,buffer,da,sizeof(da),pa,sizeof(pa));
}
void DECL2 daveAddBitVarToWriteRequest(PDU *p, int area, int DBnum, int start, int byteCount, void * buffer) {
uc da[]= {0,3,0,0,};
uc pa[]= {
0x12, 0x0a, 0x10,
0x01, /* single bit */
0,0, /* insert length in bytes here */
0,0, /* insert DB number here */
0, /* change this to real area code */
0,0,0 /* insert start address in bits */
};
#ifdef DEBUG_CALLS
LOG7("daveAddBitVarToWriteRequest(PDU:%p area:%s area number:%d start address:%d byte count:%d buffer:%p)\n",
p, daveAreaName(area), DBnum, start, byteCount, buffer);
FLUSH;
#endif
daveAddToWriteRequest(p, area, DBnum, start, byteCount,buffer,da,sizeof(da),pa,sizeof(pa));
}
/*
Get the eror code from a PDU, if one.
*/
int DECL2 daveGetPDUerror(PDU * p) {
#ifdef DEBUG_CALLS
LOG2("daveGetPDUerror(PDU:%p\n", p);
FLUSH;
#endif
if (p->header[1]==2 || p->header[1]==3) {
return daveGetU16from(p->header+10);
} else
return 0;
}
/*
Sets up pointers to the fields of a received message.
*/
int DECL2 _daveSetupReceivedPDU(daveConnection * dc, PDU * p) {
int res; /* = daveResCannotEvaluatePDU; */
p->header=dc->msgIn+dc->PDUstartI;
res=0;
if (p->header[1]==2 || p->header[1]==3) {
p->hlen=12;
res=256*p->header[10]+p->header[11];
} else {
p->hlen=10;
}
p->param=p->header+p->hlen;
p->plen=256*p->header[6] + p->header[7];
p->data=p->param+p->plen;
p->dlen=256*p->header[8] + p->header[9];
p->udlen=0;
p->udata=NULL;
if (daveDebug & daveDebugPDU)
_daveDumpPDU(p);
return res;
}
int DECL2 _daveTestResultData(PDU * p) {
int res; /*=daveResCannotEvaluatePDU;*/
if ((p->data[0]==255)&&(p->dlen>4)) {
res=daveResOK;
p->udata=p->data+4;
p->udlen=p->data[2]*0x100+p->data[3];
if (p->data[1]==4) {
p->udlen>>=3; /* len is in bits, adjust */
} else if (p->data[1]==9) {
/* len is already in bytes, ok */
} else if (p->data[1]==3) {
/* len is in bits, but there is a byte per result bit, ok */
} else {
if (daveDebug & daveDebugPDU)
LOG2("fixme: what to do with data type %d?\n",p->data[1]);
res = daveResUnknownDataUnitSize;
}
} else {
res=p->data[0];
}
return res;
}
int DECL2 _daveTestReadResult(PDU * p) {
if (p->param[0]!=daveFuncRead) return daveResUnexpectedFunc;
return _daveTestResultData(p);
}
int DECL2 _daveTestPGReadResult(PDU * p) {
int pres=0;
if (p->param[0]!=0) return daveResUnexpectedFunc;
if (p->plen==12) pres=(256*p->param[10]+p->param[11]);
if (pres==0)return _daveTestResultData(p); else return pres;
}
int DECL2 _daveTestWriteResult(PDU * p) {
int res;/* =daveResCannotEvaluatePDU; */
if (p->param[0]!=daveFuncWrite) return daveResUnexpectedFunc;
if ((p->data[0]==255)) {
res=daveResOK;
} else
res=p->data[0];
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(p);
}
return res;
}
/*****
Utilities:
****/
/*
This is an extended memory compare routine. It can handle don't care and stop flags
in the sample data. A stop flag lets it return success.
*/
int DECL2 _daveMemcmp(us * a, uc *b, size_t len) {
unsigned int i;
us * a1=(us*)a;
uc * b1=(uc*)b;
for (i=0;i<len;i++){
if (((*a1)&0xff)!=*b1) {
if (((*a1)&0x100)!=0x100) {
// LOG3("want:%02X got:%02X\n",*a1,*b1);
return i+1;
}
if (((*a1)&0x200)==0x200) return 0;
}
a1++;
b1++;
}
return 0;
}
/*
Hex dump:
*/
void DECL2 _daveDump(char * name, void*b,int len) {//void DECL2 _daveDump(char * name,uc*b,int len) {
int j;
LOG2("%s: ",name);
if (len>daveMaxRawLen) len=daveMaxRawLen; /* this will avoid to dump zillions of chars */
for (j=0; j<len; j++){
if((j & 0xf)==0) LOG2("\n%x:",j);
LOG2("0x%02X,",((uc*)(b))[j]);
}
LOG1("\n");
}
/*
Hex dump PDU:
*/
void DECL2 _daveDumpPDU(PDU * p) {
int i,dl;
uc * pd;
_daveDump("PDU header", p->header, p->hlen);
LOG3("plen: %d dlen: %d\n",p->plen, p->dlen);
if(p->plen>0) _daveDump("Parameter",p->param,p->plen);
if(p->dlen>0) _daveDump("Data ",p->data,p->dlen);
if ((p->plen==2)&&(p->param[0]==daveFuncRead)) {
pd=p->data;
for (i=0;i<p->param[1];i++) {
_daveDump("Data hdr ",pd,4);
dl=0x100*pd[2]+pd[3];
if (pd[1]==4) dl/=8;
pd+=4;
_daveDump("Data ",pd,dl);
if(i<p->param[1]-1) dl=dl+(dl%2); // the PLC places extra bytes at the end of all
// but last result, if length is not a multiple
// of 2
pd+=dl;
}
} else if ((p->header[1]==1)&&/*(p->plen==2)&&*/(p->param[0]==daveFuncWrite)) {
pd=p->data;
for (i=0;i<p->param[1];i++) {
_daveDump("Write Data hdr ",pd,4);
dl=0x100*pd[2]+pd[3];
if (pd[1]==4) dl/=8;
pd+=4;
_daveDump("Data ",pd,dl);
if(i<p->param[1]-1) dl=dl+(dl%2); // the PLC places extra bytes at the end of all
// but last result, if length is not a multiple
// of 2
pd+=dl;
}
} else {
/*
if(p->dlen>0) {
if(p->udlen==0)
_daveDump("Data ",p->data,p->dlen);
else
_daveDump("Data hdr ",p->data,4);
}
if(p->udlen>0) _daveDump("result Data ",p->udata,p->udlen);
*/
}
if ((p->header[1]==2)||(p->header[1]==3)) {
LOG2("error: %s\n",daveStrerror(daveGetPDUerror(p)));
}
}
/*
name Objects:
*/
char * DECL2 daveBlockName(uc bn) {
#ifdef DEBUG_CALLS
LOG2("daveBlockName(bn:%d)\n", bn);
FLUSH;
#endif
switch(bn) {
case daveBlockType_OB: return "OB";
case daveBlockType_DB: return "DB";
case daveBlockType_SDB: return "SDB";
case daveBlockType_FC: return "FC";
case daveBlockType_SFC: return "SFC";
case daveBlockType_FB: return "FB";
case daveBlockType_SFB: return "SFB";
default:return "unknown block type!";
}
}
char * DECL2 daveAreaName(uc n) {
#ifdef DEBUG_CALLS
LOG2("daveAreaName(n:%d)\n", n);
FLUSH;
#endif
switch (n) {
case daveSysInfo: return "System info mem.area of 200 family";
case daveSysFlags: return "System flags of 200 family";
case daveAnaIn: return "analog inputs of 200 family";
case daveAnaOut: return "analog outputs of 200 family";
case daveP: return "Peripheral I/O";
case daveInputs: return "Inputs";
case daveOutputs: return "Outputs";
case daveDB: return "DB";
case daveDI: return "DI (instance data)";
case daveFlags: return "Flags";
case daveLocal: return "local data";
case daveV: return "caller's local data";
case daveCounter: return "S7 counters";
case daveTimer: return "S7 timers";
case daveCounter200: return "IEC counters";
case daveTimer200: return "IEC timers";
default:return "unknown area!";
}
}
/*
Functions to load blocks from PLC:
*/
void DECL2 _daveConstructUpload(PDU *p,char blockType, int blockNr) {
uc pa[]= {0x1d,
0,0,0,0,0,0,0,9,0x5f,0x30,0x41,48,48,48,48,49,65};
pa[11]=blockType;
sprintf((char*)(pa+12),"%05d",blockNr);
pa[17]='A';
_daveInitPDUheader(p,1);
_daveAddParam(p, pa, sizeof(pa));
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(p);
}
}
void DECL2 _daveConstructDoUpload(PDU * p, int uploadID) {
uc pa[]= {0x1e,0,0,0,0,0,0,1};
pa[7]=uploadID;
_daveInitPDUheader(p,1);
_daveAddParam(p, pa, sizeof(pa));
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(p);
}
}
void DECL2 _daveConstructEndUpload(PDU * p, int uploadID) {
uc pa[]= {0x1f,0,0,0,0,0,0,1};
pa[7]=uploadID;
_daveInitPDUheader(p,1);
_daveAddParam(p, pa, sizeof(pa));
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(p);
}
}
uc paInsert[]= { // sended after transmission of a complete block,
// I guess this makes the CPU link the block into a program.
0x28,0,0,0,0,0,0,0xFD,0,0x0A,1,0,0x30,0x42,0x30,0x30,0x30,0x30,0x34,0x50, // block type code and number
0x05,'_','I','N','S','E',
};
uc paMakeRun[]= {
0x28,0,0,0,0,0,0,0xFD,0,0x00,9,'P','_','P','R','O','G','R','A','M'
};
uc paCompress[]= {
0x28,0,0,0,0,0,0,0xFD,0,0x00,5,'_','G','A','R','B'
};
uc paMakeStop[]= {
0x29,0,0,0,0,0,9,'P','_','P','R','O','G','R','A','M'
};
uc paCopyRAMtoROM[]= {
0x28,0,0,0,0,0,0,0xfd,0,2,'E','P',5,'_','M','O','D','U'
};
int DECL2 daveStop(daveConnection * dc) {
int res;
PDU p,p2;
#ifdef DEBUG_CALLS
LOG2("daveStop(dc:%p)\n", dc);
FLUSH;
#endif
if (dc->iface->protocol==daveProtoAS511) {
return daveStopS5(dc);
}
p.header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(&p, 1);
_daveAddParam(&p, paMakeStop, sizeof(paMakeStop));
res=_daveExchange(dc, &p);
if (res==daveResOK) {
res=_daveSetupReceivedPDU(dc,&p2);
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(&p2);
}
}
return res;
}
int DECL2 daveStart(daveConnection*dc) {
int res;
PDU p,p2;
#ifdef DEBUG_CALLS
LOG2("daveStart(dc:%p)\n", dc);
FLUSH;
#endif
if (dc->iface->protocol==daveProtoAS511) {
return daveStartS5(dc);
}
p.header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(&p, 1);
_daveAddParam(&p, paMakeRun, sizeof(paMakeRun));
res=_daveExchange(dc, &p);
if (res==daveResOK) {
res=_daveSetupReceivedPDU(dc, &p2);
if (daveDebug & daveDebugPDU) {
_daveDumpPDU(&p2);
}
}
return res;
}
int DECL2 daveCopyRAMtoROM(daveConnection * dc) {
int res;
PDU p,p2;
#ifdef DEBUG_CALLS
LOG2("davecopyRAMtoROM(dc:%p)\n", dc);
FLUSH;
#endif
p.header=dc->msgOut+dc->PDUstartO;
_daveInitPDUheader(&p, 1);