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
|
/* -*- c++ -*-
*
* (C) Copyright Pigeon Point Systems. 2011
*
* 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.
*
* Author(s):
* Anton Pak <anton.pak@pigeonpoint.com>
*/
#include <map>
#include <glib.h>
#include <SaHpi.h>
#include <oh_error.h>
#include <oh_utils.h>
#include "codec.h"
#include "handler.h"
#include "resource.h"
#include "timers.h"
#include "vars.h"
#include "sahpi_wrappers.h"
namespace TA {
/**************************************************************
* Helpers
*************************************************************/
/**************************************************************
* class cHandler
*************************************************************/
cHandler::cHandler( unsigned int id,
unsigned short port,
oh_evt_queue& eventq )
: cObject( "root" ),
cConsole( *this, port, *this ),
m_id( id ),
m_eventq( eventq ),
m_ai_timeout( SAHPI_TIMEOUT_IMMEDIATE )
{
wrap_g_static_mutex_init( &m_lock );
}
cHandler::~cHandler()
{
Resources::const_iterator iter = m_resources.begin();
Resources::const_iterator end = m_resources.end();
for ( ; iter != end; ++iter ) {
cResource * r = iter->second;
delete r;
}
m_resources.clear();
wrap_g_static_mutex_free_clear( &m_lock );
}
bool cHandler::Init()
{
bool rc;
rc = cConsole::Init();
if ( !rc ) {
CRIT( "cannot initialize console" );
return false;
}
rc = cTimers::Start();
if ( !rc ) {
CRIT( "cannot start timers" );
return false;
}
return true;
}
void cHandler::Lock()
{
wrap_g_static_mutex_lock( &m_lock );
}
void cHandler::Unlock()
{
wrap_g_static_mutex_unlock( &m_lock );
}
cResource * cHandler::GetResource( SaHpiResourceIdT rid ) const
{
Resources::const_iterator iter = m_resources.find( rid );
if ( iter != m_resources.end() ) {
cResource * r = iter->second;
return r;
}
return 0;
}
SaErrorT cHandler::RemoveFailedResource( SaHpiResourceIdT rid )
{
cResource * r = GetResource( rid );
if ( !r ) {
return SA_ERR_HPI_NOT_PRESENT;
}
if ( !r->IsFailed() ) {
return SA_ERR_HPI_INVALID_REQUEST;
}
m_resources.erase( rid );
delete r;
return SA_OK;
}
SaHpiTimeoutT cHandler::GetAutoInsertTimeout() const
{
return m_ai_timeout;
}
SaErrorT cHandler::SetAutoInsertTimeout( SaHpiTimeoutT t )
{
m_ai_timeout = t;
return SA_OK;
}
void cHandler::PostEvent( SaHpiEventTypeT type,
const SaHpiEventUnionT& data,
SaHpiSeverityT severity,
const cResource * r,
const InstrumentList& updates,
const InstrumentList& removals ) const
{
if ( !IsVisible() ) {
return;
}
struct oh_event * e = oh_new_event();
e->hid = m_id;
e->event.Source = r ? r->GetResourceId() :
SAHPI_UNSPECIFIED_RESOURCE_ID;
e->event.EventType = type;
oh_gettimeofday( &e->event.Timestamp );
e->event.Severity = severity;
e->event.EventDataUnion = data;
e->resource.ResourceId = SAHPI_UNSPECIFIED_RESOURCE_ID;
e->resource.ResourceCapabilities = 0;
if ( r ) {
e->resource = r->GetRptEntry();
}
InstrumentList::const_iterator i, end;
for ( i = updates.begin(), end = updates.end(); i != end; ++i ) {
const SaHpiRdrT& rdr = (*i)->GetRdr();
void * copy = g_memdup( &rdr, sizeof(rdr) );
e->rdrs = g_slist_append( e->rdrs, copy );
}
for ( i = removals.begin(), end = removals.end(); i != end; ++i ) {
const SaHpiRdrT& rdr = (*i)->GetRdr();
void * copy = g_memdup( &rdr, sizeof(rdr) );
e->rdrs_to_remove = g_slist_append( e->rdrs_to_remove, copy );
}
oh_evt_queue_push( &m_eventq, e );
}
void cHandler::GetNewNames( cObject::NewNames& names ) const
{
cObject::GetNewNames( names );
names.push_back( "Any Valid Entity Path" );
}
bool cHandler::CreateChild( const std::string& name )
{
bool rc;
rc = cObject::CreateChild( name );
if ( rc ) {
return true;
}
SaHpiEntityPathT ep;
rc = DisassembleResourceObjectName( name, ep );
if ( !rc ) {
return false;
}
cResource * r = new cResource( *this, ep );
m_resources[r->GetResourceId()] = r;
return true;
}
bool cHandler::RemoveChild( const std::string& name )
{
bool rc;
rc = cObject::RemoveChild( name );
if ( rc ) {
return true;
}
cObject * obj = cObject::GetChild( name );
if ( !obj ) {
return false;
}
cResource * r = static_cast<cResource *>(obj);
size_t n = m_resources.erase( r->GetResourceId() );
if ( n == 0 ) {
return false;
}
delete r;
return true;
}
void cHandler::GetChildren( Children& children ) const
{
cObject::GetChildren( children );
Resources::const_iterator iter = m_resources.begin();
Resources::const_iterator end = m_resources.end();
for ( ; iter != end; ++iter ) {
cResource * r = iter->second;
children.push_back( r );
}
}
void cHandler::GetVars( cVars& vars )
{
cObject::GetVars( vars );
vars << "AutoInsertTimeout"
<< dtSaHpiTimeoutT
<< DATA( m_ai_timeout )
<< VAR_END();
}
}; // namespace TA
|