1 /*=============================================================================|
2 |  PROJECT SNAP7                                                         1.3.0 |
3 |==============================================================================|
4 |  Copyright (C) 2013, 2015 Davide Nardella                                    |
5 |  All rights reserved.                                                        |
6 |==============================================================================|
7 |  SNAP7 is free software: you can redistribute it and/or modify               |
8 |  it under the terms of the Lesser GNU General Public License as published by |
9 |  the Free Software Foundation, either version 3 of the License, or           |
10 |  (at your option) any later version.                                         |
11 |                                                                              |
12 |  It means that you can distribute your commercial software linked with       |
13 |  SNAP7 without the requirement to distribute the source code of your         |
14 |  application and without the requirement that your application be itself     |
15 |  distributed under LGPL.                                                     |
16 |                                                                              |
17 |  SNAP7 is distributed in the hope that it will be useful,                    |
18 |  but WITHOUT ANY WARRANTY; without even the implied warranty of              |
19 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
20 |  Lesser GNU General Public License for more details.                         |
21 |                                                                              |
22 |  You should have received a copy of the GNU General Public License and a     |
23 |  copy of Lesser GNU General Public License along with Snap7.                 |
24 |  If not, see  http://www.gnu.org/licenses/                                   |
25 |=============================================================================*/
26 #ifndef s7_types_h
27 #define s7_types_h
28 //------------------------------------------------------------------------------
29 #include "s7_isotcp.h"
30 //------------------------------------------------------------------------------
31 //                               EXPORT CONSTANTS
32 // Everything added in this section has to be copied into wrappers interface
33 //------------------------------------------------------------------------------
34 
35 #ifdef OS_WINDOWS
36 #define SM7API __stdcall
37 #else
38 #define SM7API
39 #endif
40 
41   // Area ID
42 const byte S7AreaPE   =	0x81;
43 const byte S7AreaPA   =	0x82;
44 const byte S7AreaMK   =	0x83;
45 const byte S7AreaDB   =	0x84;
46 const byte S7AreaCT   =	0x1C;
47 const byte S7AreaTM   =	0x1D;
48 
49 const int MaxVars     = 20;
50 
51 const int S7WLBit     = 0x01;
52 const int S7WLByte    = 0x02;
53 const int S7WLChar    = 0x03;
54 const int S7WLWord    = 0x04;
55 const int S7WLInt     = 0x05;
56 const int S7WLDWord   = 0x06;
57 const int S7WLDInt    = 0x07;
58 const int S7WLReal    = 0x08;
59 const int S7WLCounter = 0x1C;
60 const int S7WLTimer   = 0x1D;
61 
62   // Block type
63 const byte Block_OB   = 0x38;
64 const byte Block_DB   = 0x41;
65 const byte Block_SDB  = 0x42;
66 const byte Block_FC   = 0x43;
67 const byte Block_SFC  = 0x44;
68 const byte Block_FB   = 0x45;
69 const byte Block_SFB  = 0x46;
70 
71   // Sub Block Type
72 const byte SubBlk_OB  = 0x08;
73 const byte SubBlk_DB  = 0x0A;
74 const byte SubBlk_SDB = 0x0B;
75 const byte SubBlk_FC  = 0x0C;
76 const byte SubBlk_SFC = 0x0D;
77 const byte SubBlk_FB  = 0x0E;
78 const byte SubBlk_SFB = 0x0F;
79 
80   // Block languages
81 const byte BlockLangAWL       = 0x01;
82 const byte BlockLangKOP       = 0x02;
83 const byte BlockLangFUP       = 0x03;
84 const byte BlockLangSCL       = 0x04;
85 const byte BlockLangDB        = 0x05;
86 const byte BlockLangGRAPH     = 0x06;
87 
88   // CPU status
89 const byte S7CpuStatusUnknown = 0x00;
90 const byte S7CpuStatusRun     = 0x08;
91 const byte S7CpuStatusStop    = 0x04;
92 
93 const longword evcSnap7Base           = 0x00008000;
94 // S7 Server Event Code
95 const longword evcPDUincoming  	      = 0x00010000;
96 const longword evcDataRead            = 0x00020000;
97 const longword evcDataWrite    	      = 0x00040000;
98 const longword evcNegotiatePDU        = 0x00080000;
99 const longword evcReadSZL             = 0x00100000;
100 const longword evcClock               = 0x00200000;
101 const longword evcUpload              = 0x00400000;
102 const longword evcDownload            = 0x00800000;
103 const longword evcDirectory           = 0x01000000;
104 const longword evcSecurity            = 0x02000000;
105 const longword evcControl             = 0x04000000;
106 const longword evcReserved_08000000   = 0x08000000;
107 const longword evcReserved_10000000   = 0x10000000;
108 const longword evcReserved_20000000   = 0x20000000;
109 const longword evcReserved_40000000   = 0x40000000;
110 const longword evcReserved_80000000   = 0x80000000;
111 // Event SubCodes
112 const word evsUnknown                 = 0x0000;
113 const word evsStartUpload             = 0x0001;
114 const word evsStartDownload           = 0x0001;
115 const word evsGetBlockList            = 0x0001;
116 const word evsStartListBoT            = 0x0002;
117 const word evsListBoT                 = 0x0003;
118 const word evsGetBlockInfo            = 0x0004;
119 const word evsGetClock                = 0x0001;
120 const word evsSetClock                = 0x0002;
121 const word evsSetPassword             = 0x0001;
122 const word evsClrPassword             = 0x0002;
123 // Event Result
124 const word evrNoError                 = 0;
125 const word evrFragmentRejected        = 0x0001;
126 const word evrMalformedPDU            = 0x0002;
127 const word evrSparseBytes             = 0x0003;
128 const word evrCannotHandlePDU         = 0x0004;
129 const word evrNotImplemented          = 0x0005;
130 const word evrErrException            = 0x0006;
131 const word evrErrAreaNotFound         = 0x0007;
132 const word evrErrOutOfRange           = 0x0008;
133 const word evrErrOverPDU              = 0x0009;
134 const word evrErrTransportSize        = 0x000A;
135 const word evrInvalidGroupUData       = 0x000B;
136 const word evrInvalidSZL              = 0x000C;
137 const word evrDataSizeMismatch        = 0x000D;
138 const word evrCannotUpload            = 0x000E;
139 const word evrCannotDownload          = 0x000F;
140 const word evrUploadInvalidID         = 0x0010;
141 const word evrResNotFound             = 0x0011;
142 
143   // Async mode
144 const int amPolling   = 0;
145 const int amEvent     = 1;
146 const int amCallBack  = 2;
147 
148 //------------------------------------------------------------------------------
149 //                                  PARAMS LIST
150 // Notes for Local/Remote Port
151 //   If the local port for a server and remote port for a client is != 102 they
152 //   will be *no more compatible with S7 IsoTCP*
153 //   A good reason to change them could be inside a debug session under Unix.
154 //   Increasing the port over 1024 avoids the need of be root.
155 //   Obviously you need to work with the couple Snap7Client/Snap7Server and change
156 //   both, or, use iptable and nat the port.
157 //------------------------------------------------------------------------------
158 const int p_u16_LocalPort  	    = 1;
159 const int p_u16_RemotePort 	    = 2;
160 const int p_i32_PingTimeout	    = 3;
161 const int p_i32_SendTimeout     = 4;
162 const int p_i32_RecvTimeout     = 5;
163 const int p_i32_WorkInterval    = 6;
164 const int p_u16_SrcRef          = 7;
165 const int p_u16_DstRef          = 8;
166 const int p_u16_SrcTSap         = 9;
167 const int p_i32_PDURequest      = 10;
168 const int p_i32_MaxClients      = 11;
169 const int p_i32_BSendTimeout    = 12;
170 const int p_i32_BRecvTimeout    = 13;
171 const int p_u32_RecoveryTime    = 14;
172 const int p_u32_KeepAliveTime   = 15;
173 
174 // Bool param is passed as int32_t : 0->false, 1->true
175 // String param (only set) is passed as pointer
176 
177 typedef int16_t   *Pint16_t;
178 typedef uint16_t  *Puint16_t;
179 typedef int32_t   *Pint32_t;
180 typedef uint32_t  *Puint32_t;
181 typedef int64_t   *Pint64_t;
182 typedef uint64_t  *Puint64_t;
183 typedef uintptr_t *Puintptr_t;
184 //-----------------------------------------------------------------------------
185 //                               INTERNALS CONSTANTS
186 //------------------------------------------------------------------------------
187 
188 const word DBMaxName = 0xFFFF; // max number (name) of DB
189 
190 const longword errS7Mask         = 0xFFF00000;
191 const longword errS7Base         = 0x000FFFFF;
192 const longword errS7notConnected = errS7Base+0x0001; // Client not connected
193 const longword errS7InvalidMode  = errS7Base+0x0002; // Requested a connection to...
194 const longword errS7InvalidPDUin = errS7Base+0x0003; // Malformed input PDU
195 
196 // S7 outcoming Error code
197 const word Code7Ok                      = 0x0000;
198 const word Code7AddressOutOfRange       = 0x0005;
199 const word Code7InvalidTransportSize    = 0x0006;
200 const word Code7WriteDataSizeMismatch   = 0x0007;
201 const word Code7ResItemNotAvailable   	= 0x000A;
202 const word Code7ResItemNotAvailable1    = 0xD209;
203 const word Code7InvalidValue   	        = 0xDC01;
204 const word Code7NeedPassword            = 0xD241;
205 const word Code7InvalidPassword         = 0xD602;
206 const word Code7NoPasswordToClear   	= 0xD604;
207 const word Code7NoPasswordToSet         = 0xD605;
208 const word Code7FunNotAvailable         = 0x8104;
209 const word Code7DataOverPDU             = 0x8500;
210 
211 // Result transport size
212 const byte TS_ResBit   = 0x03;
213 const byte TS_ResByte  = 0x04;
214 const byte TS_ResInt   = 0x05;
215 const byte TS_ResReal  = 0x07;
216 const byte TS_ResOctet = 0x09;
217 
218 // Client Job status (lib internals, not S7)
219 const int JobComplete  = 0;
220 const int JobPending   = 1;
221 
222 // Control codes
223 const word CodeControlUnknown   = 0;
224 const word CodeControlColdStart = 1;      // Cold start
225 const word CodeControlWarmStart = 2;      // Warm start
226 const word CodeControlStop      = 3;      // Stop
227 const word CodeControlCompress  = 4;      // Compress
228 const word CodeControlCpyRamRom = 5;      // Copy Ram to Rom
229 const word CodeControlInsDel    = 6;      // Insert in working ram the block downloaded
230 					  // Delete from working ram the block selected
231 // PDU Type
232 const byte PduType_request      = 1;      // family request
233 const byte PduType_response     = 3;      // family response
234 const byte PduType_userdata     = 7;      // family user data
235 
236 // PDU Functions
237 const byte pduResponse    	= 0x02;   // Response (when error)
238 const byte pduFuncRead    	= 0x04;   // Read area
239 const byte pduFuncWrite   	= 0x05;   // Write area
240 const byte pduNegotiate   	= 0xF0;   // Negotiate PDU length
241 const byte pduStart         = 0x28;   // CPU start
242 const byte pduStop          = 0x29;   // CPU stop
243 const byte pduStartUpload   = 0x1D;   // Start Upload
244 const byte pduUpload        = 0x1E;   // Upload
245 const byte pduEndUpload     = 0x1F;   // EndUpload
246 const byte pduReqDownload   = 0x1A;   // Start Download request
247 const byte pduDownload      = 0x1B;   // Download request
248 const byte pduDownloadEnded = 0x1C;   // Download end request
249 const byte pduControl   	= 0x28;   // Control (insert/delete..)
250 
251 // PDU SubFunctions
252 const byte SFun_ListAll   	= 0x01;   // List all blocks
253 const byte SFun_ListBoT   	= 0x02;   // List Blocks of type
254 const byte SFun_BlkInfo   	= 0x03;   // Get Block info
255 const byte SFun_ReadSZL   	= 0x01;   // Read SZL
256 const byte SFun_ReadClock   = 0x01;   // Read Clock (Date and Time)
257 const byte SFun_SetClock  	= 0x02;   // Set Clock (Date and Time)
258 const byte SFun_EnterPwd    = 0x01;   // Enter password    for this session
259 const byte SFun_CancelPwd   = 0x02;   // Cancel password    for this session
260 const byte SFun_Insert   	= 0x50;   // Insert block
261 const byte SFun_Delete   	= 0x42;   // Delete block
262 
263 typedef tm *PTimeStruct;
264 
265 //==============================================================================
266 //                                   HEADERS
267 //==============================================================================
268 #pragma pack(1)
269 
270 // Tag Struct
271 typedef struct{
272 	int Area;
273 	int DBNumber;
274 	int Start;
275 	int Size;
276 	int WordLen;
277 }TS7Tag, *PS7Tag;
278 
279 // Incoming header, it will be mapped onto IsoPDU payload
280 typedef struct {
281 	byte    P;        // Telegram ID, always 32
282 	byte    PDUType;  // Header type 1 or 7
283 	word    AB_EX;    // AB currently unknown, maybe it can be used for long numbers.
284 	word    Sequence; // Message ID. This can be used to make sure a received answer
285 	word    ParLen;   // Length of parameters which follow this header
286 	word    DataLen;  // Length of data which follow the parameters
287 }TS7ReqHeader;
288 
289 typedef TS7ReqHeader* PS7ReqHeader;
290 
291 // Outcoming 12 bytes header , response for Request type 1
292 typedef struct{
293 	byte    P;        // Telegram ID, always 32
294 	byte    PDUType;  // Header type 2 or 3
295 	word    AB_EX;    // AB currently unknown, maybe it can be used for long numbers.
296 	word    Sequence; // Message ID. This can be used to make sure a received answer
297 	word    ParLen;   // Length of parameters which follow this header
298 	word    DataLen;  // Length of data which follow the parameters
299 	word    Error;    // Error code
300 } TS7ResHeader23;
301 
302 typedef TS7ResHeader23* PS7ResHeader23;
303 
304 // Outcoming 10 bytes header , response for Request type 7
305 typedef struct{
306 	byte    P;        // Telegram ID, always 32
307 	byte    PDUType;  // Header type 1 or 7
308 	word    AB_EX;    // AB currently unknown, maybe it can be used for long numbers.
309 	word    Sequence; // Message ID. This can be used to make sure a received answer
310 	word    ParLen;   // Length of parameters which follow this header
311 	word    DataLen;  // Length of data which follow the parameters
312 }TS7ResHeader17;
313 
314 typedef TS7ResHeader17* PS7ResHeader17;
315 
316 // Outcoming 10 bytes header , response for Request type 8 (server control)
317 typedef struct {
318 	byte    P;        // Telegram ID, always 32
319 	byte    PDUType;  // Header type 8
320 	word    AB_EX;    // Zero
321 	word    Sequence; // Message ID. This can be used to make sure a received answer
322 	word    DataLen;  // Length of data which follow this header
323 	word    Error;    // Error code
324 } TS7ResHeader8;
325 
326 typedef TS7ResHeader8* PS7ResHeader8;
327 
328 // Outcoming answer buffer header type 2 or header type 3
329 typedef struct{
330 	TS7ResHeader23 Header;
331 	byte   ResData [IsoPayload_Size - sizeof(TS7ResHeader23)];
332 } TS7Answer23;
333 
334 typedef TS7Answer23* PS7Answer23;
335 
336 // Outcoming buffer header type 1 or header type 7
337 typedef struct {
338 	TS7ResHeader17 Header;
339 	byte   ResData [IsoPayload_Size - sizeof(TS7ResHeader17)];
340 } TS7Answer17;
341 
342 typedef TS7Answer17* PS7Answer17;
343 
344 typedef byte   TTimeBuffer[8];
345 typedef byte   *PTimeBuffer[8];
346 
347 typedef struct{
348    byte bcd_year;
349    byte bcd_mon;
350    byte bcd_day;
351    byte bcd_hour;
352    byte bcd_min;
353    byte bcd_sec;
354    byte bcd_himsec;
355    byte bcd_dow;
356 }TS7Time, *PS7Time;
357 
358 typedef byte   TS7Buffer[65536];
359 typedef byte   *PS7Buffer;
360 
361 const int ReqHeaderSize   = sizeof(TS7ReqHeader);
362 const int ResHeaderSize23 = sizeof(TS7ResHeader23);
363 const int ResHeaderSize17 = sizeof(TS7ResHeader17);
364 
365 // Most used request type parameters record
366 typedef struct {
367 	byte   Head[3];// 0x00 0x01 0x12
368 	byte   Plen;   // par len 0x04
369 	byte   Uk;     // unknown
370 	byte   Tg;     // type and group  (4 bits type and 4 bits group)
371 	byte   SubFun; // subfunction
372 	byte   Seq;    // sequence
373 }TReqFunTypedParams;
374 
375 //==============================================================================
376 //                            FUNCTION NEGOTIATE
377 //==============================================================================
378 typedef struct {
379 	byte    FunNegotiate;
380 	byte    Unknown;
381 	word    ParallelJobs_1;
382 	word    ParallelJobs_2;
383 	word    PDULength;
384 }TReqFunNegotiateParams;
385 
386 typedef TReqFunNegotiateParams* PReqFunNegotiateParams;
387 
388 typedef struct {
389 	byte    FunNegotiate;
390 	byte    Unknown;
391 	word    ParallelJobs_1;
392 	word    ParallelJobs_2;
393 	word    PDULength;
394 }TResFunNegotiateParams;
395 
396 typedef TResFunNegotiateParams* PResFunNegotiateParams;
397 
398 //==============================================================================
399 //                               FUNCTION READ
400 //==============================================================================
401 typedef struct {
402 	byte    ItemHead[3];
403 	byte    TransportSize;
404 	word    Length;
405 	word    DBNumber;
406 	byte    Area;
407 	byte    Address[3];
408 }TReqFunReadItem, * PReqFunReadItem;
409 
410 //typedef TReqFunReadItem;
411 
412 typedef struct {
413 	byte   FunRead;
414 	byte   ItemsCount;
415 	TReqFunReadItem Items[MaxVars];
416 }TReqFunReadParams;
417 
418 typedef TReqFunReadParams* PReqFunReadParams;
419 
420 typedef struct {
421 	byte   FunRead;
422 	byte   ItemCount;
423 }TResFunReadParams;
424 
425 typedef TResFunReadParams* PResFunReadParams;
426 
427 typedef struct {
428 	byte    ReturnCode;
429 	byte    TransportSize;
430 	word    DataLength;
431 	byte    Data[IsoPayload_Size - 17]; // 17 = header + params + data header - 1
432 }TResFunReadItem, *PResFunReadItem;
433 
434 typedef PResFunReadItem TResFunReadData[MaxVars];
435 
436 //==============================================================================
437 //                               FUNCTION WRITE
438 //==============================================================================
439 typedef struct {
440 	byte    ItemHead[3];
441 	byte    TransportSize;
442 	word    Length;
443 	word    DBNumber;
444 	byte    Area;
445 	byte    Address[3];
446 }TReqFunWriteItem, * PReqFunWriteItem;
447 
448 typedef struct {
449 	byte   FunWrite;
450 	byte   ItemsCount;
451 	TReqFunWriteItem Items[MaxVars];
452 }TReqFunWriteParams;
453 
454 typedef TReqFunWriteParams* PReqFunWriteParams;
455 
456 typedef struct {
457 	byte    ReturnCode;
458 	byte    TransportSize;
459 	word    DataLength;
460 	byte    Data [IsoPayload_Size - 17]; // 17 = header + params + data header -1
461 }TReqFunWriteDataItem, *PReqFunWriteDataItem;
462 
463 typedef PReqFunWriteDataItem TReqFunWriteData[MaxVars];
464 
465 typedef struct {
466 	byte   FunWrite;
467 	byte   ItemCount;
468 	byte   Data[MaxVars];
469 }TResFunWrite;
470 
471 typedef TResFunWrite* PResFunWrite;
472 
473 //==============================================================================
474 //                                 GROUP UPLOAD
475 //==============================================================================
476 typedef struct {
477 	byte   FunSUpld;    // function start upload 0x1D
478 	byte   Uk6 [6];     // Unknown 6 bytes
479 	byte   Upload_ID;
480 	byte   Len_1;
481 	byte   Prefix;
482 	byte   BlkPrfx;     // always 0x30
483 	byte   BlkType;
484 	byte   AsciiBlk[5]; // BlockNum in ascii
485 	byte   A;           // always 0x41 ('A')
486 }TReqFunStartUploadParams;
487 
488 typedef TReqFunStartUploadParams* PReqFunStartUploadParams;
489 
490 typedef struct {
491 	byte   FunSUpld;  // function start upload 0x1D
492 	byte   Data_1[6];
493 	byte   Upload_ID;
494 	byte   Uk[3];
495 	byte   LenLoad[5];
496 }TResFunStartUploadParams;
497 
498 typedef TResFunStartUploadParams* PResFunStartUploadParams;
499 
500 typedef struct {
501 	byte   FunUpld;  // function upload 0x1E
502 	byte   Uk6[6];   // Unknown 6 bytes
503 	byte   Upload_ID;
504 }TReqFunUploadParams;
505 
506 typedef TReqFunUploadParams* PReqFunUploadParams;
507 
508 typedef struct {
509 	byte   FunUpld; // function upload 0x1E
510 	byte   EoU;     // 0 = End Of Upload, 1 = Upload in progress
511 }TResFunUploadParams;
512 
513 typedef TResFunUploadParams* PResFunUploadParams;
514 
515 typedef struct {
516 	word    Length;   // Payload length - 4
517 	byte    Uk_00;    // Unknown 0x00
518 	byte    Uk_FB;    // Unknown 0xFB
519 	// from here is the same of TS7CompactBlockInfo
520 	word    Cst_pp;
521 	byte    Uk_01;    // Unknown 0x01
522 	byte    BlkFlags;
523 	byte    BlkLang;
524 	byte    SubBlkType;
525 	word    BlkNum;
526 	u_int   LenLoadMem;
527 	u_int   BlkSec;
528 	u_int   CodeTime_ms;
529 	word    CodeTime_dy;
530 	u_int   IntfTime_ms;
531 	word    IntfTime_dy;
532 	word    SbbLen;
533 	word    AddLen;
534 	word    LocDataLen;
535 	word    MC7Len;
536 }TResFunUploadDataHeaderFirst;
537 
538 typedef TResFunUploadDataHeaderFirst* PResFunUploadDataHeaderFirst;
539 
540 typedef struct {
541 	word    Length;// Payload length - 4
542 	byte    Uk_00; // Unknown 0x00
543 	byte    Uk_FB; // Unknown 0xFB
544 }TResFunUploadDataHeaderNext;
545 
546 typedef TResFunUploadDataHeaderNext* PResFunUploadDataHeaderNext;
547 
548 typedef struct {
549 	word    Length;// Payload length - 4
550 	byte    Uk_00; // Unknown 0x00
551 	byte    Uk_FB; // Unknown 0xFB
552 }TResFunUploadDataHeader;
553 
554 typedef TResFunUploadDataHeader* PResFunUploadDataHeader;
555 
556 typedef struct {
557 	byte    ID;  // 0x65
558 	word    Seq; // Sequence
559 	byte    Const_1[10];
560 	word    Lo_bound;
561 	word    Hi_Bound;
562 	byte    u_shortLen;// 0x02 byte
563 			   // 0x04 word
564 			   // 0x05 int
565 			   // 0x06 dword
566 			   // 0x07 dint
567 			   // 0x08 real
568 	byte    c1, c2;
569 	char    Author[8];
570 	char    Family[8];
571 	char    Header[8];
572 	byte    B1; // 0x11
573 	byte    B2; // 0x00
574 	word    Chksum;
575 	byte    Uk_8[8];
576 }TArrayUpldFooter;
577 
578 typedef TArrayUpldFooter* PArrayUpldFooter;
579 
580 typedef struct {
581 	byte   FunEUpld; // function end upload 0x1F
582 	byte   Uk6[6];   // Unknown 6 bytes
583 	byte   Upload_ID;
584 }TReqFunEndUploadParams;
585 
586 typedef TReqFunEndUploadParams* PReqFunEndUploadParams;
587 
588 typedef struct {
589 	byte   FunEUpld;  // function end upload 0x1F
590 }TResFunEndUploadParams;
591 
592 typedef TResFunEndUploadParams* PResFunEndUploadParams;
593 
594 //==============================================================================
595 //                               GROUP DOWNLOAD
596 //==============================================================================
597 typedef struct {
598 	byte   FunSDwnld;   // function start Download 0x1A
599 	byte   Uk6[6];      // Unknown 6 bytes
600 	byte   Dwnld_ID;
601 	byte   Len_1;       // 0x09
602 	byte   Prefix;      // 0x5F
603 	byte   BlkPrfx;     // always 0x30
604 	byte   BlkType;
605 	byte   AsciiBlk[5]; // BlockNum in ascii
606 	byte   P;           // 0x50 ('P')
607 	byte   Len_2;       // 0x0D
608 	byte   Uk1;         // 0x01
609 	byte   AsciiLoad[6];// load memory size (MC7 size + 92)
610 	byte   AsciiMC7[6]; // Block size in bytes
611 }TReqStartDownloadParams;
612 
613 typedef TReqStartDownloadParams* PReqStartDownloadParams;
614 typedef byte  TResStartDownloadParams;
615 typedef TResStartDownloadParams* PResStartDownloadParams;
616 
617 typedef struct {
618 	byte   Fun;         // pduDownload or pduDownloadEnded
619 	byte   Uk7[7];
620 	byte   Len_1;       // 0x09
621 	byte   Prefix;      // 0x5F
622 	byte   BlkPrfx;     // always 0x30
623 	byte   BlkType;
624 	byte   AsciiBlk[5]; // BlockNum in ascii
625 	byte   P;           // 0x50 ('P')
626 }TReqDownloadParams;
627 
628 typedef TReqDownloadParams* PReqDownloadParams;
629 
630 typedef struct {
631 	byte   FunDwnld; // 0x1B
632 	byte   EoS;      // End of sequence : 0x00 - Sequence in progress : 0x01
633 }TResDownloadParams;
634 
635 typedef TResDownloadParams* PResDownloadParams;
636 
637 typedef struct {
638 	word    DataLen;
639 	word    FB_00;   // 0x00 0xFB
640 }TResDownloadDataHeader;
641 
642 typedef TResDownloadDataHeader* PResDownloadDataHeader;
643 typedef byte   TResEndDownloadParams;
644 typedef TResEndDownloadParams* PResEndDownloadParams;
645 
646 typedef struct {
647 	word    Cst_pp;
648 	byte    Uk_01; // Unknown 0x01
649 	byte    BlkFlags;
650 	byte    BlkLang;
651 	byte    SubBlkType;
652 	word    BlkNum;
653 	u_int   LenLoadMem;
654 	u_int   BlkSec;
655 	u_int   CodeTime_ms;
656 	word    CodeTime_dy;
657 	u_int   IntfTime_ms;
658 	word    IntfTime_dy;
659 	word    SbbLen;
660 	word    AddLen;
661 	word    LocDataLen;
662 	word    MC7Len;
663 }TS7CompactBlockInfo;
664 
665 typedef TS7CompactBlockInfo* PS7CompactBlockInfo;
666 
667 typedef struct {
668 	byte    Uk_20[20];
669 	byte    Author[8];
670 	byte    Family[8];
671 	byte    Header[8];
672 	byte    B1; // 0x11
673 	byte    B2; // 0x00
674 	word    Chksum;
675 	byte    Uk_12[8];
676 }TS7BlockFooter;
677 
678 typedef TS7BlockFooter* PS7BlockFooter;
679 
680 //==============================================================================
681 //                          FUNCTION INSERT/DELETE
682 //==============================================================================
683 typedef struct {
684 	byte    Fun;         // plc control 0x28
685 	byte    Uk7[7];      // unknown 7
686 	word    Len_1;       // Length part 1 : 10
687 	byte    NumOfBlocks; // number of blocks to insert
688 	byte    ByteZero;    // 0x00
689 	byte    AsciiZero;   // 0x30 '0'
690 	byte    BlkType;
691 	byte    AsciiBlk[5]; // BlockNum in ascii
692 	byte    SFun;        // 0x50 or 0x42
693 	byte    Len_2;       // Length part 2 : 0x05 bytes
694 	char    Cmd[5];      // ascii '_INSE' or '_DELE'
695 }TReqControlBlockParams;
696 
697 typedef TReqControlBlockParams* PReqControlBlockParams;
698 
699 //==============================================================================
700 //                FUNCTIONS START/STOP/COPY RAM TO ROM/COMPRESS
701 //==============================================================================
702 typedef struct {
703 	byte   Fun;     // stop 0x29
704 	byte   Uk_5[5]; // unknown 5 bytes 0x00
705 	byte   Len_2;   // Length part 2 : 0x09
706 	char   Cmd[9];  // ascii 'P_PROGRAM'
707 }TReqFunPlcStop;
708 
709 typedef TReqFunPlcStop* PReqFunPlcStop;
710 
711 typedef struct {
712 	byte    Fun;     // start 0x28
713 	byte    Uk_7[7]; // unknown 7
714 	word    Len_1;   // Length part 1 : 0x0000
715 	byte    Len_2;   // Length part 2 : 0x09
716 	char    Cmd [9]; // ascii 'P_PROGRAM'
717 }TReqFunPlcHotStart;
718 
719 typedef TReqFunPlcHotStart* PReqFunPlcHotStart;
720 
721 typedef struct {
722 	byte    Fun;     // start 0x28
723 	byte    Uk_7[7]; // unknown 7
724 	word    Len_1;   // Length part 1 : 0x0002
725 	word    SFun;    // 'C ' 0x4320
726 	byte    Len_2;   // Length part 2 : 0x09
727 	char    Cmd[9];  // ascii 'P_PROGRAM'
728 }TReqFunPlcColdStart;
729 
730 typedef TReqFunPlcColdStart* PReqFunPlcColdStart;
731 
732 typedef struct {
733 	byte    Fun;     // pduControl 0x28
734 	byte    Uk_7[7]; // unknown 7
735 	word    Len_1;   // Length part 1 : 0x0002
736 	word    SFun;    // 'EP' 0x4550
737 	byte    Len_2;   // Length part 2 : 0x05
738 	char    Cmd[5];  // ascii '_MODU'
739 }TReqFunCopyRamToRom;
740 
741 typedef TReqFunCopyRamToRom* PReqFunCopyRamToRom;
742 
743 typedef struct {
744 	byte    Fun;     // pduControl 0x28
745 	byte    Uk_7[7]; // unknown 7
746 	word    Len_1;   // Length part 1 : 0x00
747 	byte    Len_2;   // Length part 2 : 0x05
748 	char    Cmd[5];  // ascii '_GARB'
749 }TReqFunCompress;
750 
751 typedef TReqFunCompress* PReqFunCompress;
752 
753 typedef struct {
754 	byte   ResFun;
755 	byte   para;
756 }TResFunCtrl;
757 
758 typedef TResFunCtrl* PResFunCtrl;
759 
760 //==============================================================================
761 //                            FUNCTIONS USERDATA
762 //==============================================================================
763 typedef struct {
764 	byte    Head[3]; // Always 0x00 0x01 0x12
765 	byte    Plen;    // par len 0x04 or 0x08
766 	byte    Uk;      // unknown
767 	byte    Tg;      // type and group  (4 bits type and 4 bits group)
768 	byte    SubFun;  // subfunction
769 	byte    Seq;     // sequence
770 	word    resvd;   // present if plen=0x08 (S7 manager online functions)
771 	word    Err;     // present if plen=0x08 (S7 manager online functions)
772 }TS7Params7;
773 
774 typedef TS7Params7* PS7ReqParams7;
775 typedef TS7Params7* PS7ResParams7;
776 
777 // for convenience Hi order bit of type are included (0x4X)
778 const byte   grProgrammer  = 0x41;
779 const byte   grCyclicData  = 0x42;
780 const byte   grBlocksInfo  = 0x43;
781 const byte   grSZL         = 0x44;
782 const byte   grPassword    = 0x45;
783 const byte   grBSend       = 0x46;
784 const byte   grClock       = 0x47;
785 const byte   grSecurity    = 0x45;
786 
787 //==============================================================================
788 //                             GROUP SECURITY
789 //==============================================================================
790 typedef TReqFunTypedParams TReqFunSecurity;
791 typedef TReqFunSecurity* PReqFunSecurity;
792 
793 typedef char TS7Password[8];
794 
795 typedef struct {
796 	byte    Ret;    // 0xFF for request
797 	byte    TS;     // 0x09 Transport size
798 	word    DLen;   // Data len  : 8 bytes
799 	byte    Pwd[8]; // Password encoded into "AG" format
800 }TReqDataSecurity;
801 
802 typedef TReqDataSecurity* PReqDataSecurity;
803 typedef TS7Params7 TResParamsSecurity;
804 typedef TResParamsSecurity* PResParamsSecurity;
805 
806 typedef struct {
807 	byte    Ret;
808 	byte    TS;
809 	word    DLen;
810 }TResDataSecurity;
811 
812 typedef TResDataSecurity* PResDataSecurity;
813 
814 //==============================================================================
815 //                             GROUP BLOCKS SZL
816 //==============================================================================
817 typedef TReqFunTypedParams TReqFunReadSZLFirst;
818 typedef TReqFunReadSZLFirst* PReqFunReadSZLFirst;
819 
820 typedef struct {
821 	byte    Head[3]; // 0x00 0x01 0x12
822 	byte    Plen;    // par len 0x04
823 	byte    Uk;      // unknown
824 	byte    Tg;      // type and group (4 bits type and 4 bits group)
825 	byte    SubFun;  // subfunction
826 	byte    Seq;     // sequence
827 	word    Rsvd;    // Reserved 0x0000
828 	word    ErrNo;   // Error Code
829 }TReqFunReadSZLNext;
830 
831 typedef TReqFunReadSZLNext* PReqFunReadSZLNext;
832 
833 typedef struct {
834 	byte    Ret;  // 0xFF for request
835 	byte    TS;   // 0x09 Transport size
836 	word    DLen; // Data len
837 	word    ID;   // SZL-ID
838 	word    Index;// SZL-Index
839 }TS7ReqSZLData;
840 
841 typedef TS7ReqSZLData* PS7ReqSZLData;
842 
843 typedef struct {
844 	byte    Ret;
845 	byte    TS;
846 	word    DLen;
847 	word    ID;
848 	word    Index;
849 	word    ListLen;
850 	word    ListCount;
851 	word    Data[32747];
852 }TS7ResSZLDataFirst;
853 
854 typedef TS7ResSZLDataFirst* PS7ResSZLDataFirst;
855 
856 typedef struct {
857 	byte    Ret;
858 	byte    TS;
859 	word    DLen;
860 	word    Data[32751];
861 }TS7ResSZLDataNext;
862 
863 typedef TS7ResSZLDataNext* PS7ResSZLDataNext;
864 
865 typedef struct {
866 	byte    Ret;
867 	byte    OtherInfo[9];
868 	word    Count;
869 	word    Items[32747];
870 }TS7ResSZLData_0;
871 
872 typedef TS7ResSZLData_0* PS7ResSZLData_0;
873 
874 //==============================================================================
875 //                               GROUP CLOCK
876 //==============================================================================
877 typedef TReqFunTypedParams TReqFunDateTime;
878 typedef TReqFunDateTime* PReqFunDateTime;
879 
880 typedef byte   TReqDataGetDateTime[4];
881 
882 typedef longword *PReqDataGetDateTime;
883 
884 typedef struct {
885 	byte    RetVal;
886 	byte    TSize;
887 	word    Length;
888 	byte    Rsvd;
889 	byte    HiYear;
890 	TTimeBuffer Time;
891 }TResDataGetTime;
892 
893 typedef TResDataGetTime* PResDataGetTime;
894 typedef TResDataGetTime TReqDataSetTime;
895 typedef TReqDataSetTime* PReqDataSetTime;
896 
897 typedef struct {
898 	byte    RetVal;
899 	byte    TSize;
900 	word    Length;
901 }TResDataSetTime;
902 
903 typedef TResDataSetTime* PResDataSetTime;
904 
905 //==============================================================================
906 //                            GROUP BLOCKS INFO
907 //==============================================================================
908 typedef TReqFunTypedParams TReqFunGetBlockInfo;
909 typedef TReqFunGetBlockInfo* PReqFunGetBlockInfo;
910 
911 typedef byte   TReqDataFunBlocks[4];
912 typedef u_char* PReqDataFunBlocks;
913 
914 typedef struct {
915 	byte    Head[3]; // 0x00 0x01 0x12
916 	byte    Plen;    // par len 0x04
917 	byte    Uk;      // unknown
918 	byte    Tg;      // type and group  (4 bits type and 4 bits group)
919 	byte    SubFun;  // subfunction
920 	byte    Seq;     // sequence
921 	word    Rsvd;    // Reserved 0x0000
922 	word    ErrNo;   // Error Code
923 }TResFunGetBlockInfo;
924 
925 typedef TResFunGetBlockInfo* PResFunGetBlockInfo;
926 
927 typedef struct {
928 	byte    Zero;   // always 0x30 -> Ascii 0
929 	byte    BType;  // Block Type
930 	word    BCount; // Block count
931 }TResFunGetBlockItem;
932 
933 typedef struct {
934 	byte    RetVal;
935 	byte    TRSize;
936 	word    Length;
937 	TResFunGetBlockItem Blocks[7];
938 }TDataFunListAll;
939 
940 typedef TDataFunListAll* PDataFunListAll;
941 
942 typedef struct {
943 	word    BlockNum;
944 	byte    Unknown;
945 	byte    BlockLang;
946 }TDataFunGetBotItem;
947 
948 typedef struct {
949 	byte    RetVal;
950 	byte    TSize;
951 	word    DataLen;
952 	TDataFunGetBotItem Items[(IsoPayload_Size - 29 ) / 4];
953 }TDataFunGetBot;
954 // Note : 29 is the size of headers iso, COPT, S7 header, params, data
955 
956 typedef TDataFunGetBot* PDataFunGetBot;
957 
958 typedef struct {
959 	byte   RetVal;  // 0xFF
960 	byte   TSize;   // Octet (0x09)
961 	word   Length;  // 0x0002
962 	byte   Zero;    // Ascii '0' (0x30)
963 	byte   BlkType;
964 }TReqDataBlockOfType;
965 
966 typedef TReqDataBlockOfType* PReqDataBlockOfType;
967 
968 typedef struct {
969 	byte    RetVal;
970 	byte    TSize;
971 	word    DataLen;
972 	byte    BlkPrfx;     // always 0x30
973 	byte    BlkType;
974 	byte    AsciiBlk[5]; // BlockNum in ascii
975 	byte    A;           // always 0x41 ('A')
976 }TReqDataBlockInfo;
977 
978 typedef TReqDataBlockInfo* PReqDataBlockInfo;
979 
980 typedef struct {
981 	byte    RetVal;
982 	byte    TSize;
983 	word    Length;
984 	byte    Cst_b;
985 	byte    BlkType;
986 	word    Cst_w1;
987 	word    Cst_w2;
988 	word    Cst_pp;
989 	byte    Unknown_1;
990 	byte    BlkFlags;
991 	byte    BlkLang;
992 	byte    SubBlkType;
993 	word    BlkNumber;
994 	u_int   LenLoadMem;
995 	byte    BlkSec[4];
996 	u_int   CodeTime_ms;
997 	word    CodeTime_dy;
998 	u_int   IntfTime_ms;
999 	word    IntfTime_dy;
1000 	word    SbbLen;
1001 	word    AddLen;
1002 	word    LocDataLen;
1003 	word    MC7Len;
1004 	byte    Author[8];
1005 	byte    Family[8];
1006 	byte    Header[8];
1007 	byte    Version;
1008 	byte    Unknown_2;
1009 	word    BlkChksum;
1010 	byte    Resvd1[4];
1011 	byte    Resvd2[4];
1012 }TResDataBlockInfo;
1013 
1014 typedef TResDataBlockInfo* PResDataBlockInfo;
1015 
1016 //==============================================================================
1017 //                                 BSEND / BRECV
1018 //==============================================================================
1019 typedef struct {
1020 	int       Size;
1021 	longword  R_ID;
1022 	byte      Data[65536];
1023 }TPendingBuffer;
1024 
1025 typedef struct {
1026 	TTPKT    TPKT;
1027 	TCOTP_DT COTP;
1028 	byte     P;
1029 	byte     PDUType;
1030 }TPacketInfo;
1031 
1032 typedef struct {
1033 	byte    Head[3];// Always 0x00 0x01 0x12
1034 	byte    Plen;   // par len 0x04 or 0x08
1035 	byte    Uk;     // unknown  (0x12)
1036 	byte    Tg;     // type and group, 4 bits type and 4 bits group  (0x46)
1037 	byte    SubFun; // subfunction (0x01)
1038 	byte    Seq;    // sequence
1039 	byte    IDSeq;  // ID Sequence (come from partner)
1040 	byte    EoS;    // End of Sequence = 0x00 Sequence in progress = 0x01;
1041 	word    Err;    //
1042 }TBSendParams;
1043 
1044 typedef TBSendParams* PBSendReqParams;
1045 typedef TBSendParams* PBSendResParams;
1046 
1047 // Data frame
1048 
1049 typedef struct {
1050 	byte    FF;      // 0xFF
1051 	byte    TRSize;  // Transport Size 0x09 (octet)
1052 	word    Len;     // This Telegram Length
1053 	byte    DHead[4];// sequence 0x12 0x06 0x13 0x00
1054 	u_int   R_ID;    // R_ID
1055 }TBsendRequestData;
1056 
1057 typedef TBsendRequestData* PBsendRequestData;
1058 
1059 typedef struct {
1060 	byte   DHead[4]; // sequence 0x0A 0x00 0x00 0x00
1061 }TBSendResData;
1062 
1063 typedef TBSendResData* PBSendResData;
1064 
1065 #pragma pack()
1066 #endif // s7_types_h
1067