File: srv_resourceless.c

package info (click to toggle)
snap7 1.4.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,928 kB
  • sloc: cpp: 16,151; pascal: 5,295; cs: 2,246; ansic: 726; makefile: 271; xml: 2
file content (203 lines) | stat: -rw-r--r-- 7,393 bytes parent folder | download
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
/*=============================================================================|
|  PROJECT SNAP7                                                         1.4.0 |
|==============================================================================|
|  Copyright (C) 2013, 2014 Davide Nardella                                    |
|  All rights reserved.                                                        |
|==============================================================================|
|  SNAP7 is free software: you can redistribute it and/or modify               |
|  it under the terms of the Lesser GNU General Public License as published by |
|  the Free Software Foundation, either version 3 of the License, or           |
|  (at your option) any later version.                                         |
|                                                                              |
|  It means that you can distribute your commercial software linked with       |
|  SNAP7 without the requirement to distribute the source code of your         |
|  application and without the requirement that your application be itself     |
|  distributed under LGPL.                                                     |
|                                                                              |
|  SNAP7 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               |
|  Lesser GNU General Public License for more details.                         |
|                                                                              |
|  You should have received a copy of the GNU General Public License and a     |
|  copy of Lesser GNU General Public License along with Snap7.                 |
|  If not, see  http://www.gnu.org/licenses/                                   |
|==============================================================================|
|                                                                              |
|  Server Example                                                              |
|                                                                              |
|=============================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "snap7.h"

     S7Object Server;
	 typedef byte TRWBuffer[1024];
	 typedef byte *PRWBuffer;
	 byte cnt = 0;

//------------------------------------------------------------------------------
// hexdump, a very nice function, it's not mine.
// I found it on the net somewhere some time ago... thanks to the author ;-)
//------------------------------------------------------------------------------
#ifndef HEXDUMP_COLS
#define HEXDUMP_COLS 16
#endif
	 void hexdump(void *mem, unsigned int len)
	 {
		 unsigned int i, j;

		 for (i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
		 {
			 /* print offset */
			 if (i % HEXDUMP_COLS == 0)
			 {
				 printf("0x%04x: ", i);
			 }
			 /* print hex data */
			 if (i < len)
			 {
				 printf("%02x ", 0xFF & ((char*)mem)[i]);
			 }
			 else /* end of block, just aligning for ASCII dump */
			 {
				 printf("   ");
			 }
			 /* print ASCII dump */
			 if (i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
			 {
				 for (j = i - (HEXDUMP_COLS - 1); j <= i; j++)
				 {
					 if (j >= len) /* end of block, not really printing */
					 {
						 putchar(' ');
					 }
					 else if (isprint((((char*)mem)[j] & 0x7F))) /* printable char */
					 {
						 putchar(0xFF & ((char*)mem)[j]);
					 }
					 else /* other char */
					 {
						 putchar('.');
					 }
				 }
				 putchar('\n');
			 }
		 }
	 }

 //------------------------------------------------------------------------------
 // Read/Write callback
 //------------------------------------------------------------------------------
	 int S7API RWAreaCallBack(void *usrPtr, int Sender, int Operation, PS7Tag PTag, void *pUsrData)
	 {
		 PRWBuffer PBuffer = (PRWBuffer)pUsrData;
		 int c;

		 if (Operation == OperationRead)
			 printf("Read Request\n");
		 else
			 printf("Write Request\n");

		 switch (PTag->Area)
		 {
         //case S7AreaPE: printf("Area : PE, ");
		 case 0x81: printf("Area : PE, ");
			 break;
		 case 0x82: printf("Area : PA, ");
			 break;
		 case 0x83: printf("Area : MK, ");
			 break;
		 case 0x1C: printf("Area : CT, ");
			 break;
		 case 0x1D: printf("Area : TM, ");
			 break;
		 case 0x84: printf("Area : DB%d, ", PTag->DBNumber);
			 break;
		 default: printf("Unknown area %d, ", PTag->Area);
		 }
		 printf("Start : %d, ", PTag->Start);
		 printf("Size : %d\n", PTag->Size);

		 if (Operation == OperationWrite)
			 hexdump(pUsrData, PTag->Size);
		 else
		 {
			 for (c = 0; c < 1024; c++)
				 PBuffer[c] = cnt;
			 cnt++;

		 }
		 printf("\n");
		 return 0;
	 };
// Here we use the callback to show the log, this is not the best choice since
// the callback is synchronous with the client access, i.e. the server cannot
// handle futher request from that client until the callback is complete.
// The right choice is to use the log queue via the method PickEvent.

void S7API EventCallBack(void *usrPtr, PSrvEvent PEvent, int Size)
{
    // print the event
	char text[1024];
	Srv_EventText(PEvent, text, 1024);
    printf("%s\n",text);
};

int main(int argc, char* argv[])
{
    int Error;
	char text[1024];

	Server = Srv_Create();

	// Filter a bit of noise
	Srv_SetMask(Server, mkEvent, 0x3ff);
	// Set the Read/Write callback 
	Srv_SetRWAreaCallback(Server, RWAreaCallBack, NULL);
	// Set the event callback to show something : it's not strictly needed.
    // If you comment next line the server still works fine.
    Srv_SetEventsCallback(Server, EventCallBack, NULL);

    // Start the server onto the default adapter.
    // To select an adapter we have to use Srv_StartTo(Server, "192.168.x.y").
    // Start() is the same of StartTo("0.0.0.0");
    Error=Srv_Start(Server);
    if (Error==0)
    {
	// Now the server is running ... wait a key to terminate
        getchar();
    }
    else
	{
        Srv_ErrorText(Error, text, 1024);
        printf("%s\n", text);
	}

    // If you got a start error:
    // Windows - most likely you ar running the server in a pc on wich is
    //           installed step 7 : open a command prompt and type
    //             "net stop s7oiehsx"    (Win32) or
    //             "net stop s7oiehsx64"  (Win64)
    //           And after this test :
    //             "net start s7oiehsx"   (Win32) or
    //             "net start s7oiehsx64" (Win64)
    // Unix - you need root rights :-( because the isotcp port (102) is
    //        low and so it's considered "privileged".

    Srv_Stop(Server); // <- not strictly needed, every server is stopped on deletion
                    //    and every client is gracefully disconnected.
	Srv_Destroy(&Server);
}

// Finally, this is a very minimalist (but working) server :
/*
int main(int argc, char* argv[])
{
   TS7Server *Server = new TS7Server;
   Server->Start();
   getchar();
   delete Server;
}
*/