iCub-main
ethMonitorPresence.cpp
Go to the documentation of this file.
1 // -*- Mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 
4 /*
5  * Copyright (C) 2017 iCub Facility - Istituto Italiano di Tecnologia
6  * Author: Marco Accame
7  * email: marco.accame@iit.it
8  * website: www.robotcub.org
9  * Permission is granted to copy, distribute, and/or modify this program
10  * under the terms of the GNU General Public License, version 2 or any
11  * later version published by the Free Software Foundation.
12  *
13  * A copy of the license can be found at
14  * http://www.robotcub.org/icub/license/gpl.txt
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19  * Public License for more details
20 */
21 
22 
23 // --------------------------------------------------------------------------------------------------------------------
24 // - public interface
25 // --------------------------------------------------------------------------------------------------------------------
26 
27 #include "ethMonitorPresence.h"
28 
29 
30 
31 // --------------------------------------------------------------------------------------------------------------------
32 // - external dependencies
33 // --------------------------------------------------------------------------------------------------------------------
34 
35 
36 #include <yarp/os/SystemClock.h>
37 #include <yarp/os/Log.h>
38 #include <yarp/os/LogStream.h>
39 using yarp::os::Log;
40 
41 
42 // --------------------------------------------------------------------------------------------------------------------
43 // - pimpl: private implementation (see scott meyers: item 22 of effective modern c++, item 31 of effective c++
44 // --------------------------------------------------------------------------------------------------------------------
45 
46 
47 
48 // --------------------------------------------------------------------------------------------------------------------
49 // - the class
50 // --------------------------------------------------------------------------------------------------------------------
51 
52 
53 // - class eth::EthMonitorPresence
54 
56 {
57  lastTickTime = 0;
58  lastMissingReportTime = 0;
59  lastHeardTime = 0;
60  reportedMissing = false;
61 }
62 
63 
65 {
66 
67 }
68 
69 
71 {
72  configuration = cfg;
73 
74  if(true == configuration.enabled)
75  {
76  yDebug() << "eth::EthMonitorPresence::config(): monitoring of presence is ON for BOARD" << configuration.name << "with timeout =" << configuration.timeout << "sec and period of missing report =" << configuration.periodmissingreport << "sec";
77  }
78  else
79  {
80  yDebug() << "eth::EthMonitorPresence::config(): monitoring of presence is OFF for BOARD" << configuration.name;
81  }
82 }
83 
84 
86 {
87  configuration.enabled = en;
88 }
89 
90 
92 {
93  return configuration.enabled;
94 }
95 
96 
98 {
99  lastTickTime = yarp::os::SystemClock::nowSystem();
100 }
101 
102 
104 {
105 
106  if(false == configuration.enabled)
107  {
108  return true;
109  }
110 
111 
112  double tnow = yarp::os::SystemClock::nowSystem();
113 
114  if((true == reportedMissing) && (lastTickTime > 0))
115  {
116  yDebug() << "eth::EthMonitorPresence: BOARD" << configuration.name << "has shown after being lost for" << tnow - lastHeardTime << "sec";
117  reportedMissing = false;
118  lastHeardTime = tnow;
119  }
120 
121  if((true == reportedMissing) && (configuration.periodmissingreport > 0))
122  {
123  // i report the board is still missing. but at a smaller rate
124  if((tnow - lastMissingReportTime) >= configuration.periodmissingreport)
125  {
126  yDebug() << "eth::EthMonitorPresence: BOARD" << configuration.name << "has been silent for another" << tnow - lastMissingReportTime << "sec, for a total of" << tnow - lastHeardTime << "sec";
127  lastMissingReportTime = tnow;
128  }
129  return false;
130  }
131 
132 
133  // check vs the target timeout
134  double delta = tnow - lastTickTime;
135 
136  if(delta > configuration.timeout)
137  {
138  yDebug() << "eth::EthMonitorPresence: BOARD" << configuration.name << "has been silent for" << delta << "sec (its timeout is" << configuration.timeout << "sec)";
139 
140  // also: mark the board as lost.
141  lastMissingReportTime = tnow;
142  reportedMissing = true;
143  lastHeardTime = lastTickTime;
144  lastTickTime = -1;
145 
146  return false;
147  }
148 
149  // we have the board and we hard of it by its timeout
150  return true;
151 }
152 
153 
154 
155 // - end-of-file (leave a blank line after)----------------------------------------------------------------------------
156 
157 
158 
159 
160 
void config(const Config &cfg)