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 204 205 206 207 208 209 210 211 212 213 214 215 216 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 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 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 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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 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 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
|
/**
* @file new_sim_watchdog.cpp
*
* The file includes a class for watchdog handling:\n
* NewSimulatorWatchdog
*
* @todo maybe some watchdog actions
*
* @author Lars Wetzel <larswetzel@users.sourceforge.net>
* @version 0.1
* @date 2010
*
* 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. This
* file and program are licensed under a BSD style license. See
* the Copying file included with the OpenHPI distribution for
* full licensing terms.
*
*/
#include <oh_error.h>
#include "new_sim_watchdog.h"
#include "new_sim_utils.h"
#include "new_sim_domain.h"
#include "new_sim_timer_thread.h"
/**
* Constructor
**/
NewSimulatorWatchdog::NewSimulatorWatchdog( NewSimulatorResource *res )
: NewSimulatorRdr( res, SAHPI_WATCHDOG_RDR ),
NewSimulatorTimerThread( 0 ),
m_state( NONE ) {
memset( &m_wdt_rec, 0, sizeof( SaHpiWatchdogRecT ));
memset( &m_wdt_data, 0, sizeof( SaHpiWatchdogT ));
}
/**
* Full qualified constructor to fill an object with the parsed data
**/
NewSimulatorWatchdog::NewSimulatorWatchdog( NewSimulatorResource *res,
SaHpiRdrT rdr,
SaHpiWatchdogT wdt_data)
: NewSimulatorRdr( res, SAHPI_WATCHDOG_RDR, rdr.Entity, rdr.IsFru, rdr.IdString ),
NewSimulatorTimerThread( (wdt_data.InitialCount - wdt_data.PreTimeoutInterval) ),
m_state( NONE ) {
memcpy( &m_wdt_rec, &rdr.RdrTypeUnion.WatchdogRec, sizeof( SaHpiWatchdogRecT ));
memcpy( &m_wdt_data, &wdt_data, sizeof( SaHpiWatchdogT ));
}
/**
* Destructor
**/
NewSimulatorWatchdog::~NewSimulatorWatchdog() {
Stop();
}
/**
* A rdr structure is filled with the data
*
* This method is called by method NewSimulatorRdr::Populate().
*
* @param resource Address of resource structure
* @param rdr Address of rdr structure
*
* @return true
**/
bool NewSimulatorWatchdog::CreateRdr( SaHpiRptEntryT &resource, SaHpiRdrT &rdr ) {
if ( NewSimulatorRdr::CreateRdr( resource, rdr ) == false )
return false;
/// Watchdog record
memcpy(&rdr.RdrTypeUnion.WatchdogRec, &m_wdt_rec, sizeof(SaHpiWatchdogRecT));
return true;
}
/**
* Dump the Watchdog information
*
* @param dump address of the log
*
**/
void NewSimulatorWatchdog::Dump( NewSimulatorLog &dump ) const {
char str[256];
IdString().GetAscii( str, 256 );
dump << "Watchdog: " << m_wdt_rec.WatchdogNum << " " << str << "\n";
dump << "Oem: " << m_wdt_rec.Oem << "\n";
dump << "Watchdog data:\n";
dump << "Log: " << m_wdt_data.Log << "\n";
dump << "Running: " << m_wdt_data.Running << "\n";
dump << "TimerUse: " << m_wdt_data.TimerUse << "\n";
dump << "TimerAction: " << m_wdt_data.TimerAction << "\n";
dump << "PretimerInterrupt: " << m_wdt_data.PretimerInterrupt << "\n";
dump << "PreTimeoutInterval: " << m_wdt_data.PreTimeoutInterval << "\n";
dump << "TimerUseExpFlags: " << m_wdt_data.TimerUseExpFlags << "\n";
dump << "InitialCount: " << m_wdt_data.InitialCount << "\n";
dump << "PresentCount: " << m_wdt_data.PresentCount << "\n";
}
/**
* Check whether the watchdog timer is running and trigger proper action
*
* @return true if thread can exit, false if not
**/
bool NewSimulatorWatchdog::TriggerAction() {
stdlog << "DBG: CheckWatchdogTimer\n";
if ( m_wdt_data.Running == SAHPI_FALSE )
return true;
if ( ! m_start.IsSet() )
return true;
// Ok, we have a running wdt
cTime now( cTime::Now() );
now -= m_start;
if ( now.GetMsec() >= m_wdt_data.InitialCount ) {
if ( m_state != PRETIMEOUT )
TriggerAction( PRETIMEOUT );
TriggerAction( TIMEOUT );
stdlog << "DBG: WatchdogTimer expires.\n";
return true;
}
if ( now.GetMsec() >= m_wdt_data.InitialCount - m_wdt_data.PreTimeoutInterval ) {
TriggerAction( PRETIMEOUT );
return false;
}
m_wdt_data.PresentCount = m_wdt_data.InitialCount - now.GetMsec();
return false;
}
/**
* Trigger an action, like sending an event and setting the exp_mask
*
* @param state watchdog state which should be triggered
**/
void NewSimulatorWatchdog::TriggerAction( WdtStateT state ) {
SaHpiWatchdogActionEventT wdtaction;
SaHpiSeverityT sev = SAHPI_MAJOR;
if ( ( state == PRETIMEOUT ) &&
( m_state != PRETIMEOUT ) ) {
cTime now( cTime::Now() );
now -= m_start;
m_state = PRETIMEOUT;
wdtaction = SAHPI_WAE_TIMER_INT;
sev = SAHPI_MAJOR;
m_wdt_data.PresentCount = m_wdt_data.InitialCount - now.GetMsec();
Reset( m_wdt_data.PreTimeoutInterval );
if ( m_wdt_data.Log == SAHPI_TRUE ) {
// The next is implementation specific, HPI-B, p. 154
// An event is generated when the pre-timer expires, unless the
// pre-timer interrupt action is “None” and the pre-timer interval is zero,
// in which case it is implementation-dependent whether an event is generated.
if (!( (m_wdt_data.PretimerInterrupt == SAHPI_WPI_NONE)
&& (m_wdt_data.PreTimeoutInterval == 0) ))
SendEvent( wdtaction, sev );
}
}
if ( state == TIMEOUT ) {
m_wdt_data.Running = SAHPI_FALSE;
m_wdt_data.PresentCount = 0;
m_start.Clear();
stdlog << "DBG: Stop TimerThread due to TimerAction\n";
Stop();
m_state = TIMEOUT;
switch ( m_wdt_data.TimerAction ) {
case SAHPI_WA_NO_ACTION:
sev = SAHPI_INFORMATIONAL;
wdtaction = SAHPI_WAE_NO_ACTION;
break;
case SAHPI_WA_RESET:
sev = SAHPI_MAJOR;
wdtaction = SAHPI_WAE_RESET;
break;
case SAHPI_WA_POWER_DOWN:
sev = SAHPI_MAJOR;
wdtaction = SAHPI_WAE_POWER_DOWN;
break;
case SAHPI_WA_POWER_CYCLE:
sev = SAHPI_MAJOR;
wdtaction = SAHPI_WAE_POWER_CYCLE;
break;
default:
err("Invalid TimerAction is configured inside Watchdog");
sev = SAHPI_INFORMATIONAL;
wdtaction = SAHPI_WAE_NO_ACTION;
break;
}
switch ( m_wdt_data.TimerUse ) {
case SAHPI_WTU_NONE:
case SAHPI_WTU_UNSPECIFIED:
break;
case SAHPI_WTU_BIOS_FRB2:
m_wdt_data.TimerUseExpFlags |= SAHPI_WATCHDOG_EXP_BIOS_FRB2;
break;
case SAHPI_WTU_BIOS_POST:
m_wdt_data.TimerUseExpFlags |= SAHPI_WATCHDOG_EXP_BIOS_POST;
break;
case SAHPI_WTU_OS_LOAD:
m_wdt_data.TimerUseExpFlags |= SAHPI_WATCHDOG_EXP_OS_LOAD;
break;
case SAHPI_WTU_SMS_OS:
m_wdt_data.TimerUseExpFlags |= SAHPI_WATCHDOG_EXP_SMS_OS;
break;
case SAHPI_WTU_OEM:
m_wdt_data.TimerUseExpFlags |= SAHPI_WATCHDOG_EXP_OEM;
break;
default:
err("Invalid TimerUse is configured inside Watchdog");
break;
}
stdlog << "DBG: Watchdog::SendEvent if allowed\n";
if ( m_wdt_data.Log == SAHPI_TRUE )
SendEvent( wdtaction, sev );
}
}
/**
* Send a watchdog event
*
* @param wdtaction watchdog action event flag to be set
* @param sev severity of event to be set
**/
void NewSimulatorWatchdog::SendEvent( SaHpiWatchdogActionEventT wdtaction, SaHpiSeverityT sev ) {
NewSimulatorResource *res = Resource();
if( !res ) {
stdlog << "DBG: Watchdog::TriggerAction: No resource !\n";
return;
}
oh_event *e = (oh_event *)g_malloc0( sizeof( struct oh_event ) );
e->event.EventType = SAHPI_ET_WATCHDOG;
SaHpiRptEntryT *rptentry = oh_get_resource_by_id( res->Domain()->GetHandler()->rptcache, res->ResourceId() );
SaHpiRdrT *rdrentry = oh_get_rdr_by_id( res->Domain()->GetHandler()->rptcache, res->ResourceId(), m_record_id );
if ( rptentry )
e->resource = *rptentry;
else
e->resource.ResourceCapabilities = 0;
if ( rdrentry )
e->rdrs = g_slist_append(e->rdrs, g_memdup(rdrentry, sizeof(SaHpiRdrT)));
else
e->rdrs = NULL;
// hpi events
e->event.Source = res->ResourceId();
e->event.EventType = SAHPI_ET_WATCHDOG;
e->event.Severity = sev;
oh_gettimeofday(&e->event.Timestamp);
SaHpiWatchdogEventT *wdte = &e->event.EventDataUnion.WatchdogEvent;
wdte->WatchdogNum = m_wdt_rec.WatchdogNum;
wdte->WatchdogAction = wdtaction;
wdte->WatchdogPreTimerAction = m_wdt_data.PretimerInterrupt;
wdte->WatchdogUse = m_wdt_data.TimerUse;
stdlog << "DBG: NewSimWatchdog::SendEvent Wdt for resource " << res->ResourceId() << "\n";
res->Domain()->AddHpiEvent( e );
}
// Official HPI functions
/**
* HPI function saHpiWatchdogTimerGet
*
* See also the description of the function inside the specification or header file.
* Copying the internal values and show the remaining time if the timer was started.
*
* @param watchdog address of watchdog record to be filled
*
* @return HPI return code
**/
SaErrorT NewSimulatorWatchdog::GetWatchdogInfo( SaHpiWatchdogT &watchdog ) {
if ( &watchdog == NULL)
return SA_ERR_HPI_INVALID_PARAMS;
memcpy( &watchdog, &m_wdt_data, sizeof( SaHpiWatchdogT ));
if ( m_start.IsSet() ) {
cTime now( cTime::Now() );
now -= m_start;
if ( m_wdt_data.InitialCount < now.GetMsec() ) {
watchdog.PresentCount = 0;
} else {
watchdog.PresentCount = m_wdt_data.InitialCount - now.GetMsec();
}
stdlog << "DBG: GetWatchdogInfo PresentCount == " << watchdog.PresentCount << "\n";
}
stdlog << "DBG: Call of GetWatchdogInfo: num " << m_wdt_rec.WatchdogNum << "\n";
return SA_OK;
}
/**
* HPI function saHpiWatchdogTimerSet
*
* See also the description of the function inside the specification or header file.
* Copying the internal reading values (if a read is allowed).
*
* @param watchdog address of watchdog record to be filled
*
* @return HPI return code
**/
SaErrorT NewSimulatorWatchdog::SetWatchdogInfo( SaHpiWatchdogT &watchdog ) {
SaHpiWatchdogExpFlagsT origFlags;
if ( &watchdog == NULL )
return SA_ERR_HPI_INVALID_PARAMS;
if ( watchdog.PreTimeoutInterval > watchdog.InitialCount )
return SA_ERR_HPI_INVALID_DATA;
origFlags = m_wdt_data.TimerUseExpFlags;
memcpy( &m_wdt_data, &watchdog, sizeof( SaHpiWatchdogT ));
if ( watchdog.Running == SAHPI_TRUE ) {
if ( m_start.IsSet() ) {
m_start = cTime::Now();
Reset( m_wdt_data.InitialCount - m_wdt_data.PreTimeoutInterval );
if ( !m_running )
Start();
} else {
m_wdt_data.Running = SAHPI_FALSE;
m_wdt_data.PresentCount = 0;
}
} else {
m_start.Clear();
Stop();
m_wdt_data.PresentCount = 0;
}
// ClearFlags
m_wdt_data.TimerUseExpFlags = origFlags & ~watchdog.TimerUseExpFlags;
stdlog << "DBG: SetWatchdogInfo successfully: num " << m_wdt_rec.WatchdogNum << "\n";
return SA_OK;
}
/**
* HPI function saHpiWatchdogTimerReset
*
* See also the description of the function inside the specification or header file.
* Starting or resetting a watchdog timer if it is allowed
*
* @return HPI return code
**/
SaErrorT NewSimulatorWatchdog::ResetWatchdog() {
if ( m_start.IsSet() ) {
cTime now( cTime::Now() );
now -= m_start;
if ( now.GetMsec() > m_wdt_data.InitialCount - m_wdt_data.PreTimeoutInterval ) {
stdlog << "DBG: ResetWatchdog not allowed: num " << m_wdt_rec.WatchdogNum << ":\n";
stdlog << "DBG: Time expire in ms: " << now.GetMsec() << " > "
<< (m_wdt_data.InitialCount - m_wdt_data.PreTimeoutInterval) << "\n";
return SA_ERR_HPI_INVALID_REQUEST;
}
// Reset the Timer
Reset( m_wdt_data.InitialCount - m_wdt_data.PreTimeoutInterval );
m_start = cTime::Now();
} else {
m_start = cTime::Now();
// Reset the Timer
Reset( m_wdt_data.InitialCount - m_wdt_data.PreTimeoutInterval );
if (!m_running)
Start();
}
m_wdt_data.Running = SAHPI_TRUE;
Domain()->SetRunningWdt( true );
stdlog << "DBG: ResetWatchdog successfully: num " << m_wdt_rec.WatchdogNum << "\n";
return SA_OK;
}
|