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 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
|
/* -*- linux-c -*-
*
* (C) Copyright IBM Corp. 2004-2008
* (C) Copyright Pigeon Point Systems. 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.
*
* Author(s):
* W. David Ashley <dashley@us.ibm.com>
* Renier Morales <renier@openhpi.org>
* Anton Pak <anton.pak@pigeonpoint.com>
*
*/
#include <string.h>
#include <glib.h>
#include <oh_error.h>
#include <marshal_hpi.h>
#include <strmsock.h>
#include "conf.h"
#include "init.h"
#include "lock.h"
#include "session.h"
#include <sahpi_wrappers.h>
/***************************************************************
* Session Layer: class cSession
**************************************************************/
class cSession
{
public:
explicit cSession();
~cSession();
void Ref()
{
++m_ref_cnt;
}
void Unref()
{
--m_ref_cnt;
}
int GetRefCnf() const
{
return m_ref_cnt;
}
SaHpiDomainIdT GetDomainId() const
{
return m_did;
}
SaHpiSessionIdT GetSid() const
{
return m_sid;
}
void SetSid( SaHpiSessionIdT sid )
{
m_sid = sid;
}
SaErrorT GetEntityRoot( SaHpiEntityPathT& entity_root ) const;
SaErrorT RpcOpen( SaHpiDomainIdT did );
SaErrorT RpcClose();
SaErrorT Rpc( uint32_t id,
ClientRpcParams& iparams,
ClientRpcParams& oparams );
private:
cSession( const cSession& );
cSession& operator =( cSession& );
SaErrorT DoRpc( uint32_t id,
ClientRpcParams& iparams,
ClientRpcParams& oparams );
SaErrorT GetSock( cClientStreamSock * & sock );
static void DeleteSock( gpointer ptr );
private:
static const size_t RPC_ATTEMPTS = 2;
static const gulong NEXT_RPC_ATTEMPT_TIMEOUT = 2 * G_USEC_PER_SEC;
// data
volatile int m_ref_cnt;
SaHpiDomainIdT m_did;
SaHpiSessionIdT m_sid;
SaHpiSessionIdT m_remote_sid;
#if GLIB_CHECK_VERSION (2, 32, 0)
GPrivate m_sockets;
#else
GStaticPrivate m_sockets;
#endif
};
cSession::cSession()
: m_ref_cnt( 0 ),
m_did( SAHPI_UNSPECIFIED_DOMAIN_ID ),
m_sid( 0 ),
m_remote_sid( 0 )
{
#if GLIB_CHECK_VERSION (2, 32, 0)
m_sockets = G_PRIVATE_INIT (g_free);
#else
wrap_g_static_private_init( &m_sockets );
#endif
}
cSession::~cSession()
{
wrap_g_static_private_free( &m_sockets );
}
SaErrorT cSession::GetEntityRoot( SaHpiEntityPathT& entity_root ) const
{
ohc_lock();
const struct ohc_domain_conf * dc = ohc_get_domain_conf( m_did );
if ( dc ) {
memcpy( &entity_root, &dc->entity_root, sizeof(SaHpiEntityPathT) );
}
ohc_unlock();
if (!dc) {
return SA_ERR_HPI_INVALID_DOMAIN;
}
return SA_OK;
}
SaErrorT cSession::RpcOpen( SaHpiDomainIdT did )
{
m_did = did;
SaHpiDomainIdT remote_did = SAHPI_UNSPECIFIED_DOMAIN_ID;
ClientRpcParams iparams, oparams( &m_remote_sid );
iparams.SetFirst( &remote_did );
return DoRpc( eFsaHpiSessionOpen, iparams, oparams );
}
SaErrorT cSession::RpcClose()
{
ClientRpcParams iparams, oparams;
iparams.SetFirst( &m_remote_sid );
return DoRpc( eFsaHpiSessionClose, iparams, oparams );
}
SaErrorT cSession::Rpc( uint32_t id,
ClientRpcParams& iparams,
ClientRpcParams& oparams )
{
iparams.SetFirst( &m_remote_sid );
return DoRpc( id, iparams, oparams );
}
SaErrorT cSession::DoRpc( uint32_t id,
ClientRpcParams& iparams,
ClientRpcParams& oparams )
{
SaErrorT rv;
cHpiMarshal * hm = HpiMarshalFind( id );
if ( !hm ) {
return SA_ERR_HPI_UNSUPPORTED_API;
}
int cc;
char data[dMaxPayloadLength];
uint32_t data_len;
uint8_t rp_type;
uint32_t rp_id;
int rp_byte_order;
cc = HpiMarshalRequest( hm, data, iparams.const_array );
if ( cc < 0 ) {
return SA_ERR_HPI_INTERNAL_ERROR;
}
data_len = cc;
bool rc = false;
for ( size_t attempt = 0; attempt < RPC_ATTEMPTS; ++attempt ) {
if ( attempt > 0 ) {
DBG( "Session: RPC request %u, Attempt %u\n", id, (unsigned int)attempt );
}
cClientStreamSock * sock;
rv = GetSock( sock );
if ( rv != SA_OK ) {
return rv;
}
rc = sock->WriteMsg( eMhMsg, id, data, data_len );
if ( rc ) {
rc = sock->ReadMsg( rp_type, rp_id, data, data_len, rp_byte_order );
if ( rc ) {
break;
}
}
#if GLIB_CHECK_VERSION (2, 32, 0)
wrap_g_static_private_set( &m_sockets, 0);// close socket
#else
wrap_g_static_private_set( &m_sockets, 0, 0 ); // close socket
#endif
g_usleep( NEXT_RPC_ATTEMPT_TIMEOUT );
}
if ( !rc ) {
return SA_ERR_HPI_NO_RESPONSE;
}
oparams.SetFirst( &rv );
cc = HpiDemarshalReply( rp_byte_order, hm, data, oparams.array );
if ( ( cc <= 0 ) || ( rp_type != eMhMsg ) || ( id != rp_id ) ) {
//Closing main socket(the socket that was used for saHpiSessionOpen)
// may disrupt HPI session on remote side.
// TODO: Investigate and fix on OpenHPI daemon side and then uncomment.
//g_static_private_set( &m_sockets, 0, 0 ); // close socket
return SA_ERR_HPI_NO_RESPONSE;
}
return rv;
}
SaErrorT cSession::GetSock( cClientStreamSock * & sock )
{
gpointer ptr = wrap_g_static_private_get( &m_sockets );
if ( ptr ) {
sock = reinterpret_cast<cClientStreamSock *>(ptr);
} else {
ohc_lock();
const struct ohc_domain_conf * dc = ohc_get_domain_conf( m_did );
ohc_unlock();
if (!dc) {
return SA_ERR_HPI_INVALID_DOMAIN;
}
sock = new cClientStreamSock;
bool rc = sock->Create( dc->host, dc->port );
if ( !rc ) {
delete sock;
CRIT("Session: cannot open connection to domain %u.", m_did );
return SA_ERR_HPI_NO_RESPONSE;
}
// TODO configuration file, env vars?
sock->EnableKeepAliveProbes( /* keepalive_time*/ 1,
/* keepalive_intvl */ 1,
/* keepalive_probes */ 3 );
#if GLIB_CHECK_VERSION (2, 32, 0)
wrap_g_static_private_set( &m_sockets, sock );
#else
wrap_g_static_private_set( &m_sockets, sock, DeleteSock );
#endif
}
return SA_OK;
}
void cSession::DeleteSock( gpointer ptr )
{
cClientStreamSock * sock = reinterpret_cast<cClientStreamSock *>(ptr);
delete sock;
}
/***************************************************************
* Session Layer: Session Table
**************************************************************/
static GHashTable * sessions = 0;
gpointer sid_key( SaHpiSessionIdT sid )
{
char * key = 0;
key += sid;
return key;
}
static void sessions_init()
{
ohc_lock();
if ( !sessions ) {
sessions = g_hash_table_new( g_direct_hash, g_direct_equal );
}
ohc_unlock();
}
static SaHpiSessionIdT sessions_add( cSession * session )
{
static SaHpiSessionIdT next_sid = 1;
ohc_lock();
SaHpiSessionIdT sid = next_sid;
++next_sid;
session->SetSid( sid );
g_hash_table_insert( sessions, sid_key( sid ), session );
ohc_unlock();
return sid;
}
static cSession * sessions_get_ref( SaHpiSessionIdT sid )
{
ohc_lock();
gpointer value = g_hash_table_lookup( sessions, sid_key( sid ) );
cSession * session = reinterpret_cast<cSession*>(value);
if ( session ) {
session->Ref();
}
ohc_unlock();
return session;
}
static void dehash_func( gpointer /* key */, gpointer value, gpointer user_data )
{
GList ** pvalues = reinterpret_cast<GList **>(user_data);
cSession * session = reinterpret_cast<cSession*>(value);
session->Ref();
*pvalues = g_list_append( *pvalues, value );
}
static GList * sessions_get_ref_all()
{
ohc_lock();
GList * sessions_list = 0;
if ( sessions ) {
g_hash_table_foreach( sessions, dehash_func, &sessions_list );
}
ohc_unlock();
return sessions_list;
}
static void sessions_unref( cSession * session, bool closed = false )
{
ohc_lock();
session->Unref();
if ( closed ) {
session->Unref();
g_hash_table_remove( sessions, sid_key( session->GetSid() ) );
}
if ( session->GetRefCnf() < 0 ) {
delete session;
}
ohc_unlock();
}
/***************************************************************
* Session Layer: Interface
**************************************************************/
void ohc_sess_init()
{
sessions_init();
}
SaErrorT ohc_sess_open( SaHpiDomainIdT did, SaHpiSessionIdT& sid )
{
ohc_init(); // TODO investigate
cSession * session = new cSession;
SaErrorT rv = session->RpcOpen( did );
if ( rv == SA_OK ) {
sid = sessions_add( session );
} else {
delete session;
}
return rv;
}
SaErrorT ohc_sess_close( SaHpiSessionIdT sid )
{
cSession * session = sessions_get_ref( sid );
if ( !session ) {
return SA_ERR_HPI_INVALID_SESSION;
}
SaErrorT rv = session->RpcClose();
sessions_unref( session, ( rv == SA_OK ) );
return rv;
}
SaErrorT ohc_sess_close_all()
{
SaErrorT rv = SA_OK;
GList * sessions_list = sessions_get_ref_all();
if ( g_list_length( sessions_list ) == 0 ) {
//rv = SA_ERR_HPI_INVALID_REQUEST;
rv = SA_OK;
} else {
GList * item = sessions_list;
while ( item ) {
cSession * session = reinterpret_cast<cSession*>(item->data);
session->RpcClose();
sessions_unref( session, true );
item = item->next;
}
}
g_list_free( sessions_list );
return rv;
}
SaErrorT ohc_sess_rpc( uint32_t id,
SaHpiSessionIdT sid,
ClientRpcParams& iparams,
ClientRpcParams& oparams )
{
cSession * session = sessions_get_ref( sid );
if ( !session ) {
return SA_ERR_HPI_INVALID_SESSION;
}
SaErrorT rv = session->Rpc( id, iparams, oparams );
sessions_unref( session );
return rv;
}
SaErrorT ohc_sess_get_did( SaHpiSessionIdT sid, SaHpiDomainIdT& did )
{
cSession * session = sessions_get_ref( sid );
if ( !session ) {
return SA_ERR_HPI_INVALID_SESSION;
}
did = session->GetDomainId();
sessions_unref( session );
return SA_OK;
}
SaErrorT ohc_sess_get_entity_root( SaHpiSessionIdT sid, SaHpiEntityPathT& ep )
{
cSession * session = sessions_get_ref( sid );
if ( !session ) {
return SA_ERR_HPI_INVALID_SESSION;
}
SaErrorT rv = session->GetEntityRoot( ep );
sessions_unref( session );
return rv;
}
|