spss7.c 18.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * spss7.c
 *
 * This is an interface between SICS and a S7 Siemens SPS speaking the
 * Fetch/Write protocol via TCP/IP. On initialization, the S7 DB area reserved
 * for communication with SICS is read, parsed and nodes created representing the
 * various data items in there. Suitable callbacks allow to write values into the
 * SPS. Communication is via the device serializer and the genbin protocol handler.
 *
 * copyright: see file COPYRIGHT
 *
 *  Created on: Jul 7, 2010
 *      Author: Mark Koennecke
14 15 16 17
 *
 * Updated to use long names to avoid collisions 
 *
 * Mark Koennecke, May 2017
18 19 20 21
 *
 * Updated to write switches directly. Not how it should be ... but ...
 *
 * Mark Koennecke, June 2018
22 23 24 25 26
 */
#include <sics.h>
#include <sicsobj.h>
#include <devser.h>
#include <sicshipadaba.h>
27

28 29
#define MAXDATA 65552 /* 64KB + 16 Byte for headers */

30 31 32
#define NOTLOADED -10
#define NOSWITCHES -20

33 34 35 36 37 38 39
/*----------------------- private data structure --------------*/
typedef struct {
	DevSer *devser;
	int dbnum;
	int dblength;
	int switchdbnum;
	int switchdblength;
40
        int longName;
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
}SPSS7, *pSPSS7;
/*------------------------------------------------------------------*/
typedef struct {
	pSPSS7 sps;
	pHdb spsNode;
	char sendData[MAXDATA];
	char replyData[MAXDATA];
	int toSend;
	int toRead;
	char message[132];
	SConnection *pCon; /* only used when writing */
}S7Action, *pS7Action;
/*------------------------------------------------------------------*/
static int S7ActionMatch(void *a1, void *a2)
{
	if (a1 == a2){
		return 1;
	} else {
		return 0;
	}
}
/*------------------------------------------------------------------*/
static void MakeFetchMessage(pS7Action self, int db, int start, int length)
{
	unsigned short num;

	memset(self->sendData,0,MAXDATA);
	memset(self->replyData,0,MAXDATA);
	self->sendData[0] = 'S';
	self->sendData[1] = '5';
	self->sendData[2] = 0x10;
	self->sendData[3] = 0x01;
	self->sendData[4] = 0x03;
	self->sendData[5] = 0x05;
	self->sendData[6] = 0x03;
	self->sendData[7] = 0x08;
	self->sendData[8] = 0x01;
	self->sendData[9] = db;
	self->sendData[6] = 0x03;
	/*
	 * addresses are in words which is 16 bit = 2 bytes
	 */
	num = htons(start/2);
	memcpy(self->sendData+10,&num,2);
	num = htons(length/2);
	memcpy(self->sendData+12,&num,2);
	self->sendData[14] = 0xFF;
	self->sendData[15] = 0x02;
	self->toSend = 16;
	self->toRead = 16 + length;

	sprintf(self->message,"%lx:%d:%lx:%d", (long) self->sendData, self->toSend,
			(long)self->replyData, self->toRead);
}
/*-------------------------------------------------------------------*/
static void MakeWriteMessage(pS7Action self,
			int start, int length, void *data)
{
	unsigned short num;

	memset(self->sendData,0,MAXDATA);
	memset(self->replyData,0,MAXDATA);
	self->sendData[0] = 'S';
	self->sendData[1] = '5';
	self->sendData[2] = 0x10;
	self->sendData[3] = 0x01;
	self->sendData[4] = 0x03;
	self->sendData[5] = 0x03;
	self->sendData[6] = 0x03;
	self->sendData[7] = 0x08;
	self->sendData[8] = 0x01;
	self->sendData[9] = self->sps->switchdbnum;
	self->sendData[6] = 0x03;
	/*
	 * Start is in bytes, length is in words which is 2 bytes....
	 */
	num = htons(start);
	memcpy(self->sendData+10,&num,2);
	num = htons(length/2);
	memcpy(self->sendData+12,&num,2);
	self->sendData[14] = 0xFF;
	self->sendData[15] = 0x02;
	memcpy(self->sendData+16, data, length);
	self->toSend = 16 + length;
	self->toRead = 16;

	sprintf(self->message,"%lx:%d:%lx:%d", (long) self->sendData, self->toSend,
			(long)self->replyData, self->toRead);
}
/*--------------------------------------------------------------------*/
static int decodeString(char *pPtr, char *string, int maxlen)
{
	int fullength, used;
	fullength = (int)pPtr[0];
	used = (int)pPtr[1];
	if(fullength != maxlen || used > maxlen){
		printf("Correcting DB String error\n");
		fullength = maxlen;
		used = maxlen;
	}
koennecke's avatar
koennecke committed
141
	memset(string,0,fullength+1);
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
	memcpy(string, pPtr+2, used);
	return fullength + 2;
}
/*------------------------------------------------------------------
 * This is the action handler for writing SPS data
 *-------------------------------------------------------------------*/
static char *S7WriteHandler(void *actionData, char *reply, int comerror)
{
	pS7Action self = (pS7Action)actionData;
	char message[132];
	SConnection *pCon = NULL;

	if(reply == NULL){
		return self->message;
	}

	pCon = self->pCon;
	if(self->replyData[8] != 0){
		snprintf(message,132,"ERROR: code %d when writing to S7/%s",
				self->replyData[8], GetHipadabaPath(self->spsNode));
		if(pCon != NULL){
			SCWrite(pCon,message,eError);
			printf("Write Shit %d happened\n", self->replyData[8]);
		} else {
			printf("%s\n", message);
		}
	} else {
		if(pCon != NULL){
			SCSendOK(pCon);
		}
	}

	return NULL;
}
/*--------------------------------------------------------------------*/
static void UpdateSPSDataBase(pS7Action self)
{
	char *pPtr;
koennecke's avatar
koennecke committed
180
    char name[15], unit[9], description[25], reference[11], error[50], alarms[10];
181
    char longName[80]; /* must hold name+unit+description */
182
    unsigned char type, alarm;
koennecke's avatar
koennecke committed
183
    unsigned short val;
184
    int ival;
koennecke's avatar
koennecke committed
185
    unsigned char bval, b2;
186 187 188 189 190 191 192 193 194 195 196 197 198
    float fval;
    pHdb node = NULL;
    hdbValue hdbVal;

    pPtr = self->replyData + 16 + 2;
	while(pPtr - self->replyData < self->toRead){
		type = pPtr[0];
		alarm = pPtr[1];
		pPtr+= 2;
		pPtr += decodeString(pPtr, name,14);
		pPtr += decodeString(pPtr, unit,8);
		pPtr += decodeString(pPtr, description,24);
		pPtr += decodeString(pPtr, reference,10);
199 200 201 202 203 204
		if(self->sps->longName == 1) {
		  snprintf(longName,sizeof(longName),"%s%s%s", name,unit,description);
		  node = GetHipadabaNode(self->spsNode,longName);
                } else {
		  node = GetHipadabaNode(self->spsNode,name);
                }
koennecke's avatar
koennecke committed
205 206 207 208
		if(node == NULL){
			printf("Something very fishy is happening here: did Roman change the SPS layout under our feet?\n");
			continue;
		}
209 210 211
		switch (type) {
		case 1:
		case 4:
koennecke's avatar
koennecke committed
212 213 214 215 216
			memcpy(&bval,pPtr,1);
			/*
			memcpy(&b2,pPtr+1,1);
			printf("Bytes: %d %d\n", bval, b2);
			*/
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
			hdbVal = MakeHdbInt(bval);
			UpdateHipadabaPar(node,hdbVal,NULL);
			pPtr += 2;
			break;
		case 2:
			memcpy(&ival,pPtr,4);
			hdbVal = MakeHdbInt(ntohl(ival));
			UpdateHipadabaPar(node,hdbVal,NULL);
			pPtr += 4;
			break;
		case 3:
			memcpy(&ival,pPtr+2,4);
			ival = htonl(ival);
			memcpy(&fval,&ival,4);
			hdbVal = MakeHdbFloat(fval);
			UpdateHipadabaPar(node,hdbVal,NULL);
			pPtr += 6;
			break;
		}
		if(alarm == 0){
			SetHdbProperty(node,"geterror", NULL);
		} else {
			snprintf(error,50,"Alarm %d on par",alarm);
			SetHdbProperty(node,"geterror", error);
		}
koennecke's avatar
koennecke committed
242 243
		snprintf(alarms,sizeof(alarms),"%d",alarm);
		SetHdbProperty(node,"alarm", alarms);
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
	}
}
/*------------------------------------------------------------------
 * This is the action handler for updating the SPS data in SICS
 *-------------------------------------------------------------------*/
static char *S7UpdateHandler(void *actionData, char *reply, int comerror)
{
	pS7Action self = (pS7Action)actionData;

	if(reply == NULL){
		if(self->sps->dblength > 0){
		  MakeFetchMessage(self,self->sps->dbnum, 0,self->sps->dblength);
		  return self->message;
		} else {
			return NULL;
		}
	}

	UpdateSPSDataBase(self);
	return NULL;
}
/*-------------------------------------------------------------------*/
static hdbCallbackReturn S7WriteCallback(pHdb currentNode,
		void *userData, pHdbMessage message)
{
	pS7Action writeAction = NULL, updateAction = NULL;
	pSPSS7 self = (pSPSS7)userData;
	pHdbDataMessage mm = NULL;
	SConnection *pCon = NULL;
	int offset, type, length, idata;
	float fval;
	short sdata;
	unsigned char bdata;
	char prop[50];

	mm = GetHdbSetMessage(message);
	if(mm != NULL){
		writeAction = calloc(1,sizeof(S7Action));
		if(writeAction == NULL && mm->callData != NULL){
			pCon = (SConnection *)mm->callData;
			SCWrite(pCon, "ERROR: out of memory in S7WriteCallback", eError);
			return hdbContinue;
		}
		writeAction->sps = self;
		writeAction->pCon = mm->callData;
		writeAction->spsNode = currentNode;
		GetHdbProperty(currentNode,"offset",prop,50);
		offset = atoi(prop);
		GetHdbProperty(currentNode,"type",prop,50);
		type = atoi(prop);
		switch(type){
		case 1:
		case 4:
			sdata = (short)mm->v->v.intValue;
			/* sdata = htons(sdata); */
			MakeWriteMessage(writeAction,offset,2,&sdata);
			break;
		case 2:
			idata = mm->v->v.intValue;
			idata = htonl(idata);
			MakeWriteMessage(writeAction,offset,4,&idata);
			break;
		case 3:
				fval = (float)mm->v->v.doubleValue;
308 309
				memcpy(&idata,&fval,4);
				idata = htonl(idata);
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
				MakeWriteMessage(writeAction,offset,4,&idata);
				break;
		default:
				assert(0);
		}
		DevQueue(self->devser, writeAction, WritePRIO,
				S7WriteHandler, S7ActionMatch, free, NULL);

		updateAction = calloc(1,sizeof(S7Action));
		if(updateAction != NULL){
			updateAction->sps = self;
			updateAction->spsNode = currentNode->mama->mama;
			DevQueue(self->devser, updateAction, ProgressPRIO,
					S7UpdateHandler, S7ActionMatch, free, NULL);
		}
	}

	return hdbContinue;
}
/*--------------------------------------------------------------------*/
static void InitializeSPSDataBase(pS7Action self, pHdb parent)
{
	char *pPtr;
koennecke's avatar
koennecke committed
333
    char name[15], unit[9], description[25], reference[11], num[11], error[50], alarms[10];
334
    char longName[80]; /* must hold name+unit+description */
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    unsigned char type, alarm;
    short val;
    int ival;
    float fval;
    unsigned char bval;
    pHdb node = NULL;
    hdbValue hdbVal;

    pPtr = self->replyData + 16 + 2;
	while(pPtr - self->replyData < self->toRead){
		type = pPtr[0];
		alarm = pPtr[1];
		pPtr+= 2;
		pPtr += decodeString(pPtr, name,14);
		pPtr += decodeString(pPtr, unit,8);
		pPtr += decodeString(pPtr, description,24);
		pPtr += decodeString(pPtr, reference,10);
352 353 354 355 356
		if(self->sps->longName == 1) {
		  snprintf(longName,sizeof(longName),"%s%s%s", name,unit,description);
                } else {
		  strcpy(longName,name);
                }
357 358 359
		switch (type) {
		case 1:
		case 4:
360
			node = MakeHipadabaNode(longName,HIPINT,1);
361 362 363
			memcpy(&bval,pPtr+1,1);
			hdbVal = MakeHdbInt(bval);
			UpdateHipadabaPar(node,hdbVal,NULL);
364
			snprintf(num,10,"%ld", (long)(pPtr - self->replyData - 16));
365 366 367 368
			SetHdbProperty(node,"offset",num);
			pPtr += 2;
			break;
		case 2:
369
			node = MakeHipadabaNode(longName,HIPINT,1);
370 371 372
			memcpy(&ival,pPtr,4);
			hdbVal = MakeHdbInt(ntohl(ival));
			UpdateHipadabaPar(node,hdbVal,NULL);
373
			snprintf(num,10,"%ld", (long)(pPtr - self->replyData - 16));
374 375 376 377
			SetHdbProperty(node,"offset",num);
			pPtr += 4;
			break;
		case 3:
378
			node = MakeHipadabaNode(longName,HIPFLOAT,1);
379 380 381 382 383
			memcpy(&ival,pPtr+2,4);
			ival = htonl(ival);
			memcpy(&fval,&ival,4);
			hdbVal = MakeHdbFloat(fval);
			UpdateHipadabaPar(node,hdbVal,NULL);
384
			snprintf(num,10,"%ld", (long)(pPtr - self->replyData + 2 - 16));
385 386 387 388 389 390 391 392 393 394
			SetHdbProperty(node,"offset",num);
			pPtr += 6;
			break;
		}
		snprintf(num,10,"%d",type);
		SetHdbProperty(node,"type", num);
		if(alarm != 0){
			snprintf(error,10,"Alarm %d on par",alarm);
			SetHdbProperty(node,"geterror", error);
		}
koennecke's avatar
koennecke committed
395 396
		snprintf(alarms,sizeof(alarms),"%d",alarm);
		SetHdbProperty(node,"alarm", alarms);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		SetHdbProperty(node,"unit", unit);
		SetHdbProperty(node,"description", description);
		SetHdbProperty(node,"reference", reference);
		AddHipadabaChild(parent,node, NULL);
		printf("Found parameter %s\n", name);
	}
}
/*------------------------------------------------------------------
 * This is the action handler for doing the initialisation of
 * The SPS
 *-------------------------------------------------------------------*/
static char *S7InitHandler(void *actionData, char *reply, int comerror)
{
	pS7Action self = (pS7Action)actionData;
	short dblength;
koennecke's avatar
koennecke committed
412
	pHdb node = NULL;
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

	/*
	 * Start: read length of the database
	 */
	if(reply == NULL){
		self->sps->dblength  = 0;
		MakeFetchMessage(self,self->sps->dbnum, 0,2);
		return self->message;
	}

	/*
	 * we are reading the database length
	 */
	if(self->sps->dblength == 0){
		memcpy(&dblength, self->replyData+16,2);
		self->sps->dblength = ntohs(dblength);
		MakeFetchMessage(self,self->sps->dbnum, 0,ntohs(dblength));
		return self->message;
	} else {
		memcpy(&dblength, self->replyData+16,2);
		dblength = ntohs(dblength);
		InitializeSPSDataBase(self,self->spsNode);
koennecke's avatar
koennecke committed
435 436 437 438
		node = GetHipadabaNode(self->spsNode,"init");
		if(node != NULL){
			UpdateHipadabaPar(node,MakeHdbInt(1), NULL);
		}
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
		return NULL;
	}
}
/*-------------------------------------------------------------------------*/
static void InitializeSwitches(pS7Action self)
{
	pHdb switches = NULL, node = NULL;
	int i;
	char name[20];

	switches = GetHipadabaNode(self->spsNode,"switches");
	assert(switches != NULL);

	node = switches->child;
	while(node != NULL){
		AppendHipadabaCallback(node,
			MakeHipadabaCallback(S7WriteCallback,self->sps,NULL));
		node = node->next;
	}

	node = GetHipadabaNode(self->spsNode,"init");
	if(node != NULL){
		UpdateHipadabaPar(node,MakeHdbInt(1), NULL);
	}
}
/*-------------------------------------------------------------------------*/
static char *S7InitSwitchHandler(void *actionData, char *reply, int comerror)
{
	pS7Action self = (pS7Action)actionData;
	short dblength;
    pHdb parent = NULL;

	/*
	 * Start: read length of the database
	 */
474
	if(reply == NULL && self->sps->switchdblength == NOTLOADED){
475 476 477 478 479 480 481
		MakeFetchMessage(self,self->sps->switchdbnum, 0,2);
		return self->message;
	}

	/*
	 * we are reading the database length
	 */
482
	if(self->sps->switchdblength == NOTLOADED){
483 484
		memcpy(&dblength, self->replyData+16,2);
		self->sps->switchdblength = ntohs(dblength);
485 486 487 488
		if(self->sps->switchdblength == 0){
		  self->sps->switchdblength = NOSWITCHES;
		  return NULL;
		}
489 490 491 492 493 494 495 496 497 498 499
		MakeFetchMessage(self,self->sps->switchdbnum,
				0,ntohs(dblength));
		return self->message;
	} else {
		parent = GetHipadabaNode(self->spsNode,"switches");
		assert(parent != NULL);
		InitializeSPSDataBase(self,parent);
		InitializeSwitches(self);
		return NULL;
	}
}
500 501 502 503 504 505 506
/*-------------------------------------------------------------------*/
static void KillS7Action(void *data)
{
  if(data != NULL){
    free(data);
  }
}
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
/*===================================================================*/
static int S7UpdateCmd(pSICSOBJ ccmd, SConnection * con,
                        Hdb * cmdNode, Hdb * par[], int nPar)
{
	pSPSS7 self = (pSPSS7)ccmd->pPrivate;
	pS7Action updateAction = NULL;

	updateAction = calloc(1,sizeof(S7Action));
	if(updateAction == NULL){
		SCWrite(con,"ERROR: out of memory in S7 update", eError);
		return 0;
	}
	updateAction->sps = self;
	updateAction->spsNode = ccmd->objectNode;
	DevQueue(self->devser, updateAction, ProgressPRIO,
522
			S7UpdateHandler, S7ActionMatch, KillS7Action, NULL);
523 524 525
	SCSendOK(con);
	return 1;
}
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
/*-------------------------------------------------------------------------*/
static int S7SetSwitchCmd(pSICSOBJ ccmd, SConnection * con,
                        Hdb * cmdNode, Hdb * par[], int nPar)
{
	pSPSS7 self = (pSPSS7)ccmd->pPrivate;
	unsigned int offset, size;
	unsigned char value;
	pS7Action writeAction = NULL;


	if(nPar < 3) {
	  SCWrite(con,"ERROR: not enough parameters for setswitch: offset, size, value required",
		  eError);
	  return 0;
        }
	offset = par[0]->value.v.intValue;
	size = par[1]->value.v.intValue;
	value = par[2]->value.v.intValue;

	writeAction = calloc(1,sizeof(S7Action));
	if(writeAction == NULL){
	  SCWrite(con, "ERROR: out of memory in S7WriteCallback", eError);
	  return 0;
	}
	writeAction->sps = self;
	writeAction->pCon = con;
	writeAction->spsNode = ccmd->objectNode;
	
	MakeWriteMessage(writeAction,offset,size*2,&value);
	
	DevQueue(self->devser, writeAction, WritePRIO,
		 S7WriteHandler, S7ActionMatch, free, NULL);

	SCSendOK(con);
	return 1;
}
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
/*---------------------------------------------------------------------*/
static void KillSPSS7(void *data)
{
	pSPSS7 self = (pSPSS7)data;
	if(self == NULL){
		return;
	}
	if(self->devser != NULL){
		DevKill(self->devser);
	}
	free(self);
}
/*--------------------------------------------------------------------*/
int MakeSPSS7(SConnection * con, SicsInterp * sics,
                             void *object, int argc, char *argv[])
{
	pSPSS7 self = NULL;
	pSICSOBJ pNew = NULL;
	char *devArgs[3];
	pS7Action initAction = NULL, updateAction = NULL,
			initSwitchAction = NULL;
583
	pHdb child = NULL;
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

	int status;

	if(argc < 5){
		SCWrite(con,"ERROR: not enough arguments for MakeSPSS7", eError);
		return 0;
	}

	self = calloc(1,sizeof(SPSS7));
	if(self == NULL){
		SCWrite(con,"ERROR: out of memory in MakeSPSS7", eError);
		return 0;
	}

	devArgs[0] = strdup("genbin");
	devArgs[1] = strdup(argv[4]);

	self->devser = DevMake(con,2,devArgs);
	if(self->devser == NULL){
		return 0;
	}
	free(devArgs[0]);
	free(devArgs[1]);

	self->dbnum = atoi(argv[2]);
	self->switchdbnum = atoi(argv[3]);
610
	self->switchdblength = NOTLOADED;
611 612 613 614 615
	self->longName = 0;

	/* 
	   test the longname flag
	*/
koennecke's avatar
koennecke committed
616 617
        if(argc > 5) {
	  self->longName = atoi(argv[5]);
618 619 620 621 622 623
	  if(self->longName != 0 && self->longName != 1) {
	    SCWrite(con,"ERROR: invalid value for longName flag on spss7", eError);
	    self->longName = 0;
          }
        }

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

	pNew = MakeSICSOBJv(argv[1],"SPS-S7", HIPNONE, usInternal);
	if(pNew == NULL){
		SCWrite(con,"ERROR: out of memory in MakeSPSS7", eError);
		return 0;
	}
	pNew->pPrivate = self;
	pNew->KillPrivate = KillSPSS7;

	status = AddCommand(sics,
	            argv[1], InterInvokeSICSOBJ, KillSICSOBJ, pNew);
	if (status != 1) {
		KillSICSOBJ(pNew);
	    SCPrintf(con, eError, "ERROR: failed create duplicate command %s",
	             argv[1]);
	    return 0;
	}

	AddSICSHdbPar(pNew->objectNode,
	                      "update", usSpy, MakeSICSFunc(S7UpdateCmd));

645 646 647 648 649 650 651
	child = AddSICSHdbPar(pNew->objectNode,
	                      "setswitch", usUser, MakeSICSFunc(S7SetSwitchCmd));
	AddSICSHdbPar(child, "offset", usUser, MakeHdbInt(2));
	AddSICSHdbPar(child, "size", usUser, MakeHdbInt(1));
	AddSICSHdbPar(child, "value", usUser, MakeHdbInt(0));


652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
	AddHipadabaChild(pNew->objectNode,
				MakeSICSROPar("init",MakeHdbInt(0)),NULL);

	AddHipadabaChild(pNew->objectNode,
			MakeHipadabaNode("switches",HIPNONE,0),NULL);


	initAction = calloc(1,sizeof(S7Action));
	initSwitchAction = calloc(1,sizeof(S7Action));
	updateAction = calloc(1,sizeof(S7Action));
	if(initAction == NULL || updateAction == NULL || initSwitchAction == NULL){
		SCWrite(con,"ERROR: out of memory in MakeSPSS7", eError);
		return 0;
	}
	initAction->sps = self;
	initAction->spsNode = pNew->objectNode;
	DevQueue(self->devser, initAction, WritePRIO,
			S7InitHandler, S7ActionMatch, free, NULL);

	initSwitchAction->sps = self;
	initSwitchAction->spsNode = pNew->objectNode;
	DevQueue(self->devser, initSwitchAction, WritePRIO,
674
			S7InitSwitchHandler, S7ActionMatch, KillS7Action, NULL);
675 676 677 678 679

	updateAction->sps = self;
	updateAction->spsNode = pNew->objectNode;
	DevSchedule(self->devser, updateAction,
			ReadPRIO, 60.,
680
			S7UpdateHandler, S7ActionMatch, KillS7Action, NULL);
681 682 683

	return 1;
}