Newer
Older
// Copyright 2016 CERN and GSI
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <fcntl.h>
#include <signal.h>
#include <silecs-communication/interface/equipment/SilecsCluster.h>
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
#include <silecs-communication/interface/utility/SilecsLog.h>
#include <silecs-communication/interface/communication/SilecsConnection.h>
#include <silecs-communication/interface/equipment/SilecsPLC.h>
#include <silecs-communication/protocol/core/silecs.h>
namespace Silecs
{
// static definition
bool Connection::isAlive_ = false;
const unsigned int Connection::numberConn_ = 3;
//------------------------------------------------------------------------------------------------------------------------------------------------
Connection::Connection(PLC* thePLC)
{
// Start the PLC Thread
readMux_ = new Mutex("readMux");
writeMux_ = new Mutex("writeMux");
connMux_ = new Mutex("connMux");
//Not yet allowed, not yet connected!
isEnabled_ = false;
isConnected_ = false;
//Reset the Reconnection mechanism
timeConn_ = NULL;
delayConn_ = UrgentConnection; //initial reconnection delay
remainConn_ = numberConn_; //initial number of attempt
}
//------------------------------------------------------------------------------------------------------------------------------------------------
Connection::~Connection()
{
//Connection has been closed before from the concrete connection object
if (timeConn_ != NULL) delete timeConn_;
delete connMux_;
delete writeMux_;
delete readMux_;
}
//------------------------------------------------------------------------------------------------------------------------------------------------
bool Connection::enable(PLC* thePLC, bool connectNow)
{
{
Lock lock(connMux_);
isEnabled_ = true;
}
if (connectNow) doOpen(thePLC);
return isConnected_;
}
//------------------------------------------------------------------------------------------------------------------------------------------------
void Connection::disable(PLC* thePLC)
{
if (!isEnabled_) {
LOG(DEBUG) << "Trying to disconnect a PLC that is not enabled: " << thePLC->getName();
return;
}
Lock lock(connMux_);
{
Lock lock(readMux_);
{
Lock lock(writeMux_);
isEnabled_ = false;
doClose(thePLC, /*withLock =*/false);
}
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
bool Connection::doOpen(PLC* thePLC)
{
//LOG((COMM|DIAG)) << "Start attempt to open connection ..."; .. commented, seem like this creates to many log-entries for KIBANA
bool isReachable = false;
bool isOpen = false;
bool justConnected = false;
{
Lock lock(connMux_);
if (isConnected_)
{ if (!isEnabled_)
{
LOG((COMM|DIAG)) << "The PLC is connected but the client wants to disable the transactions";
doClose(thePLC, /*withLock =*/false);
}
return isConnected_;
}
if (!isEnabled_)
{
LOG((COMM|DIAG)) << "The communication currently is not enabled";
return isConnected_;
}
if (isTimeToReconnect())
{
if (isReachable = (ping((char *) thePLC->getName().c_str(), NULL) == 0))
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
{
LOG((COMM|DIAG)) << "It's time to reconnect";
// It's time to open the connection according to the (re)connection timing
// Let's try several times with limited delay (some ms).
// It allows wake-up frozen PLC (SIEMENS in particular) after long stop period.
bool isOpen = false;
unsigned int nbConn = 2; //for fast low-level iteration
for(unsigned int i = 0; i<nbConn; i++)
{
LOG((COMM|DIAG)) << "Attempt to open PLC connection ....";
isOpen = open(thePLC);
if(isOpen)
{
LOG((COMM|DIAG)) << "Connection opened successfully";
break;
}
usleep(100000); // wait 100ms
}
}//pingable?
if(!isOpen)
{
logError(thePLC,isReachable);
return isConnected_;
}
if (thePLC->isSharedConnection())
{
std::ostringstream os;
os << "Shared connection with " << thePLC->getName() << " is established.";
if (thePLC->theHeader_ != NULL) TRACE("info") << os.str();
LOG(COMM) << os.str();
std::ostringstream os;
os << "Connection with " << thePLC->getName() <<
":" << thePLC->theCluster_->getClassName() <<
"/v" << thePLC->theCluster_->getClassVersion() <<
" is established.";
if (thePLC->theHeader_ != NULL) TRACE("info") << os.str();
LOG(COMM) << os.str();
}
isAlive_ = true;
isConnected_ = true;
justConnected = true; //retentive registers synchronization is required!
//Connection status has changed: update the diagnostic variables
LOG((COMM|DIAG)) << "Updating PLC status";
updateStatus(thePLC);
}
}//release lock
/* Process the Retentive registers synchronization each time the PLC is just (re)connected.
* This is a recursive call: performs task::execute method from doOpen that is
* called into execute itself. The recursion is terminated when SilecsHeader connection is closed finally.
*/
if (justConnected)
{
LOG((COMM|DIAG)) << "First connection - performing registers synchronization";
thePLC->updateLocalData();
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
}
LOG((COMM|DIAG)) << "isConnected_:" << isConnected_;
return isConnected_;
}
//------------------------------------------------------------------------------------------------------------------------------------------------
void Connection::doClose(PLC* thePLC, bool withLock)
{
//This process that can be called inside and outside protected section.
//withLock argument is used to avoid Recursive mutex that is not supported
//by LynxOS platform.
if(withLock)
Lock lock(connMux_);
if (isConnected_)
{
if (close(thePLC))
{
isConnected_ = false;
//Connection status has changed: update the diagnostic variables
updateStatus(thePLC);
if (thePLC->isSharedConnection())
{
std::ostringstream os;
os << "Shared connection with " << thePLC->getName() << " is closed.";
if (thePLC->theHeader_ != NULL) TRACE("warn") << os.str();
LOG(COMM) << os.str();
std::ostringstream os;
os << "Connection with " << thePLC->getName() <<
" (" << thePLC->theCluster_->getClassName() <<
"/v" << thePLC->theCluster_->getClassVersion() <<
")" << " is closed.";
if (thePLC->theHeader_ != NULL) TRACE("warn") << os.str();
LOG(COMM) << os.str();
}
}
else
{ LOG(COMM) << "Close connection with " << thePLC->getName() <<
" (" << thePLC->theCluster_->getClassName() <<
"/v" << thePLC->theCluster_->getClassVersion() <<
")" << " has failed.";
}
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
bool Connection::reOpen(PLC* thePLC)
{
if (ping((char *) thePLC->getName().c_str(), 0) == 0)
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
{ doClose(thePLC, /*withLock =*/true);
return doOpen(thePLC);
}
isAlive_ = false;
return false;
}
bool Connection::isEnabled() { return isEnabled_; }
bool Connection::isConnected() { return isConnected_; }
//-------------------------------------------------------------------------------------------------------------------
int Connection::readUnitCode(PLC* thePLC, UnitCodeType& dataStruct)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
int Connection::readUnitStatus(PLC* thePLC, UnitStatusType& dataStruct)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
int Connection::readCPUInfo(PLC* thePLC, CPUInfoType& dataStruct)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
int Connection::readCPInfo(PLC* thePLC, CPInfoType& dataStruct)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
bool Connection::isRunning(PLC* thePLC)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
int Connection::coldRestart(PLC* thePLC)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
//PERFORM COLD RESTART
int Connection::plcStop(PLC* thePLC)
{
throw SilecsException(__FILE__, __LINE__, DIAG_PLC_REPORT_NOT_SUPPORTED, thePLC->getName());
return -1;
}
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
//-------------------------------------------------------------------------------------------------------------------
bool Connection::checkError(PLC* thePLC, int err, bool retry)
{
if (err < 0)
{
LOG(COMM) << "Transaction failure with PLC: " << thePLC->getName() << ". SILECS[" << err << "]: " << IeGetErrorMessage(err);
switch(err)
{
case IE_TIMEOUT_ERROR:
case IE_EPIPE_ERROR:
case IE_DISCONNECT_ERROR:
if (retry)
{
LOG(COMM) << "Try to reconnect the PLC: " << thePLC->getName();
if (reOpen(thePLC))
{ // can repeat the request after the connection was successfully reopened
return true;
}
// reconnection has failed again, just close the connection
LOG(COMM) << "Unable to reconnect the PLC: " << thePLC->getName();
}
// else { // no retry, we just want to close (use default case)
default:
doClose(thePLC, /*withLock =*/true);
}
}
return false; // no particular error
}
//------------------------------------------------------------------------------------------------------------------------------------------------
void Connection::updateStatus(PLC* thePLC)
{
//Connection status has changed (opened/closed)
//Update the PLC diagnostic variables
thePLC->updateStatus();
//Reset reconnection mecanisme in case of connection succeed
if (isConnected_)
{ delayConn_ = UrgentConnection; //initial reconnection delay
remainConn_ = numberConn_; //initial number of attempt
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------
void Connection::logError(PLC* thePLC, bool isReachable)
{
std::string errorMsg = isReachable ? "Connection with " + thePLC->getName() + ":" + thePLC->theCluster_->getClassName() +
"/v" + thePLC->theCluster_->getClassVersion() + " has failed.\n"
: "Controller " + thePLC->getName() + " does not respond to ping, might be OFF!\n";
if (delayConn_ == LongTermConnection)
{
if (remainConn_ > 0)
{
std::ostringstream os;
os << errorMsg << "Periodic attempt to reconnect, delay " << delayConn_ << "s (tracing off).";
if (thePLC->theHeader_ != NULL) TRACE("error") << os.str();
LOG(COMM) << os.str();
remainConn_ = 1; //Try to reconnect again and again (=1 means disable tracing).
}
/*else
PLC does not respond anymore. It's probably stopped for a long time.
Do not log error anymore (but still try to reconnect, with long-term delay).
*/
else
{
std::ostringstream os;
os << errorMsg << "Next attempt to reconnect in " << delayConn_ << "s if requested. Remains: " << remainConn_;
if (thePLC->theHeader_ != NULL) TRACE("error") << os.str();
LOG(COMM) << os.str();
}
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
}
//------------------------------------------------------------------------------------------------------------------------------------------------
bool Connection::isTimeToReconnect()
{
bool toBeReconnected = false;
if (timeConn_ != NULL)
{ //how many time from the last connect attempt
double delay = timeConn_->getDelay(S_UNIT);
if (delay >= double(delayConn_))
{ timeConn_->getValue(S_UNIT); //restart delay counting from now
toBeReconnected = true;
if (remainConn_ > 0) --remainConn_;
if (remainConn_ == 0)
{ if (delayConn_ != LongTermConnection)
{ if (delayConn_ == UrgentConnection)
delayConn_ = ShortTermConnection;
else //(delayConn_ == ShortTermConnection)
delayConn_ = LongTermConnection;
remainConn_ = numberConn_;
}
}
}
}
else
{
//This is the first connection attempt, just start the Time counter.
timeConn_ = new TsCounter(true); //using hardware clock
timeConn_->getValue(S_UNIT); //start delay counting from now
toBeReconnected = true;
}
return toBeReconnected;
}
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
/* This macro is used to trap the unexpected broken pipe and
return an error instead of exit process.
*/
static __sighandler_t sigpipeHandler = (__sighandler_t)-1;
#define _DISABLE_SIGPIPE sigpipeHandler = signal(SIGPIPE, SIG_IGN)
#define _ENABLE_SIGPIPE signal(SIGPIPE, sigpipeHandler)
/*----------------------------------------------------------*/
/* This is a function used to connect an host on a port.
* 'ip' is the ip address string
* 'port' is the port number (102 for rfc1006 server)
* 'dst' is the TSAP destination string (exp: "TCP-1")
* 'src' is the TSAP source string (exp: "TCP-1")
* 'ts' timeout in second (1s minimum)
* return a socket descriptor or <0 on error
* (see constant error)
*
* Details:
* intermediate connect_nonb() function is used to perform a non-blockant connection.
* intermediate rfcPing() function is used to check if PLC is ON
*/
int connect_nonb(int sockfd, struct sockaddr *saptr, socklen_t salen, int nsec)
{
int flags, n, error;
socklen_t len;
fd_set rset, wset;
struct timeval tval;
flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
error = 0;
/*v1.1*/
_DISABLE_SIGPIPE;
n = connect(sockfd, (struct sockaddr *) saptr, salen);
_ENABLE_SIGPIPE;
if (n < 0){
if (errno != EINPROGRESS)
return(-1);
}
/* Do whatever we want while the connect is taking place. */
if (n == 0)
goto done; /* connect completed immediately */
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
wset = rset;
tval.tv_sec = nsec;
tval.tv_usec = 0;
if ( (n = select(sockfd+1, &rset, &wset, NULL,
nsec ? &tval : NULL)) == 0) {
close(sockfd); /* timeout */
errno = ETIMEDOUT;
return(-1);
}
if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
return(-1); /* Solaris pending error */
}
}
done:
fcntl(sockfd, F_SETFL, flags); /* restore file status flags */
if (error) {
/*close(sockfd); just in case */
errno = error;
return(-1);
}
return(0);
}
/*----------------------------------------------------------*/
int rfcPing(char *ip, long ts)
{
int err, s, val = 1;
struct protoent *pent;
struct sockaddr_in rsock;
/* Socket create/connect */
memset ((char *)&rsock,0,sizeof(rsock));
if ((s=socket(AF_INET,SOCK_STREAM,0)) == -1)
{
#ifdef COMMENT
printf("#Error - Can't create socket: %s\n", strerror(errno));
#endif
return RFC_SYS_ERROR;
}
/* Set TCP_NODELAY option to force immediate TCP/IP acknowledgement */
if ((pent=getprotobyname("TCP")) == NULL)
{
#ifdef COMMENT
printf("#Error - Can't configure socket: %s\n", strerror(errno));
#endif
return RFC_SYS_ERROR;
}
if (setsockopt(s,pent->p_proto,TCP_NODELAY,&val,4) == -1)
{
#ifdef COMMENT
printf("#Error - Can't configure socket: %s\n", strerror(errno));
#endif
return RFC_SYS_ERROR;
}
#ifdef COMMENT
printf("#Comment - Socket created: %i\n", s);
#endif
rsock.sin_addr.s_addr=inet_addr(ip);
rsock.sin_family=AF_INET;
/*check any port to detect if the host is OFF*/
rsock.sin_port=htons(102);
err = 0;
if (connect_nonb(s,(struct sockaddr *)(&rsock),sizeof(rsock), ts) == -1)
{
/*if hostname is OFF, connect() fails on TIMEOUT*/
if ((errno == ETIMEDOUT) || (errno == EHOSTDOWN) || (errno == EHOSTUNREACH)) {
err = RFC_TIMEOUT_ERROR;
}
}
close(s);
return err;
}
/*..........................................................*/
int ping(char *hostName, char *plcIP)
{
struct in_addr addr;
struct hostent *hp;
char *ipstr = plcIP;
if ((hostName == NULL) && (plcIP == NULL))
return(IE_PARAM_ERROR);
// use hostName reference in priority else plcIP
if (hostName)
{
hp = gethostbyname(hostName);
if (hp)
{
addr.s_addr = *((unsigned long int *) hp->h_addr);
ipstr = inet_ntoa(addr);
}
}
if (ipstr == NULL)
return(IE_HOST_UNKNOWN_ERROR);
/*trying to connect PLC (1 second max)*/
return (rfcPing(ipstr, 1));
}