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
|
/**
* @file new_sim_utils.h
*
* The file includes an some utilities and a class for time handling\n
* cClass
*
* @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.
*
* The new Simulator plugin is adapted from the ipmidirect plugin.
* Thanks to
* Thomas Kanngieser <thomas.kanngieser@fci.com>
*
*/
#ifndef __NEW_SIM_UTILS_H__
#define __NEW_SIM_UTILS_H__
#include <sys/time.h>
/**
* @enum tNewSimulatorFruState
*
* Mapping Fru states to the ipmi state numbering.
**/
enum tNewSimulatorFruState
{
eFruStateNotInstalled = 0,
eFruStateInactive = 1,
eFruStateActivationRequest = 2,
eFruStateActivationInProgress = 3,
eFruStateActive = 4,
eFruStateDeactivationRequest = 5,
eFruStateDeactivationInProgress = 6,
eFruStateCommunicationLost = 7
};
/**
* Returns a string for a Fru state
*
* @param state IPMI state
* @return string with the name of the state
**/
const char *NewSimulatorFruStateToString( tNewSimulatorFruState state );
/// Size of a date string
#define dDateStringSize 11
void NewSimulatorDateToString( unsigned int time, char *str );
/// Size of a time string
#define dTimeStringSize 9
void NewSimulatorTimeToString( unsigned int time, char *str );
/// Size of a date and time string
#define dDateTimeStringSize 20
void NewSimulatorDateTimeToString( unsigned int time, char *str );
/**
* @class cTime
*
* The time class can be used to work with time values
**/
class cTime
{
public:
/// time value
timeval m_time;
/**
* Constructor
**/
cTime()
{
m_time.tv_sec = 0;
m_time.tv_usec = 0;
}
/**
* qualified Constructor with timeval
**/
cTime( const struct timeval &tv )
{
m_time.tv_sec = tv.tv_sec;
m_time.tv_usec = tv.tv_usec;
}
/**
* copy Constructor
**/
cTime( const cTime &t )
{
m_time.tv_sec = t.m_time.tv_sec;
m_time.tv_usec = t.m_time.tv_usec;
}
/**
* qualified Constructor with seconds and usec
**/
cTime( unsigned int s, unsigned int u )
{
m_time.tv_sec = s;
m_time.tv_usec = u;
}
/**
* noramlize the internal data
*/
void Normalize()
{
while( m_time.tv_usec > 1000000 )
{
m_time.tv_usec -= 1000000;
m_time.tv_sec++;
}
while( m_time.tv_usec < 0 )
{
m_time.tv_usec += 1000000;
m_time.tv_sec--;
}
}
/**
* compare two time classes
*/
int Cmp( const cTime &t )
{
if ( m_time.tv_sec < t.m_time.tv_sec )
return -1;
if ( m_time.tv_sec > t.m_time.tv_sec )
return 1;
if ( m_time.tv_usec < t.m_time.tv_usec )
return -1;
if ( m_time.tv_usec > t.m_time.tv_usec )
return 1;
return 0;
}
/**
* lower operator
**/
bool operator<( const cTime &t )
{
return Cmp( t ) < 0;
}
/**
* lower equal operator
**/
bool operator<=( const cTime &t )
{
return Cmp( t ) < 0;
}
/**
* higher operator
**/
bool operator>( const cTime &t )
{
return Cmp( t ) > 0;
}
/**
* higher equal operator
**/
bool operator>=( const cTime &t )
{
return Cmp( t ) >= 0;
}
/**
* equal operator
**/
bool operator==( const cTime &t )
{
return Cmp( t ) == 0;
}
/**
* not equal operator
**/
bool operator!=( const cTime &t )
{
return Cmp( t ) == 0;
}
/**
* add cTime class operator
**/
cTime &operator+=( const cTime &t )
{
m_time.tv_sec += t.m_time.tv_sec;
m_time.tv_usec += t.m_time.tv_usec;
Normalize();
return *this;
}
/**
* add msec value operator
**/
cTime &operator+=( int ms )
{
m_time.tv_sec += ms / 1000;
m_time.tv_usec += (ms % 1000) * 1000;
Normalize();
return *this;
}
/**
* subtract time value operator
**/
cTime &operator-=( int ms )
{
m_time.tv_sec -= ms / 1000;
m_time.tv_usec -= (ms % 1000) * 1000;
Normalize();
return *this;
}
/**
* subtract time value operator
**/
cTime &operator-=( cTime t )
{
m_time.tv_sec -= t.m_time.tv_sec;
m_time.tv_usec -= t.m_time.tv_usec;
Normalize();
return *this;
}
/**
* fill the class with actual time
**/
static cTime Now()
{
cTime t;
gettimeofday( &t.m_time, 0 );
return t;
}
/**
* get the time value in msec
**/
unsigned int GetMsec()
{
return (unsigned int) (m_time.tv_sec * 1000 + m_time.tv_usec / 1000);
}
/**
* Clear the value
**/
void Clear()
{
m_time.tv_sec = 0;
m_time.tv_usec = 0;
}
/**
* Check whether the time was set
**/
bool IsSet()
{
return ( !(( m_time.tv_sec == 0 ) && ( m_time.tv_usec == 0 )) );
}
};
#endif
|