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
|
/*
* ipmi_sensor_factors.cpp
*
* Copyright (c) 2004 by FORCE Computers
* Copyright (c) 2005 by ESO Technologies.
*
* Note that this file is based on parts of OpenIPMI
* written by Corey Minyard <minyard@mvista.com>
* of MontaVista Software. Corey's code was helpful
* and many thanks go to him. He gave the permission
* to use this code in OpenHPI under BSD license.
*
* 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.
*
* Authors:
* Thomas Kanngieser <thomas.kanngieser@fci.com>
* Pierre Sangouard <psangouard@eso-tech.com>
*/
#include "ipmi_sensor_factors.h"
#include <math.h>
static const char *analoge_data_format[] =
{
"Unsigned",
"1Compl",
"2Compl",
"Analog"
};
const char *
IpmiAnalogeDataFormatToString( tIpmiAnalogeDataFormat fmt )
{
if ( (int)fmt <= eIpmiAnalogDataFormatNotAnalog )
return analoge_data_format[fmt];
return "Invalid";
}
static const char *linearization_map[] =
{
"Linear",
"Ln",
"Log10",
"Log2",
"E",
"Exp10",
"Exp2",
"1OverX",
"Sqr",
"Cube",
"Sqrt",
"1OverCube"
};
const char *
IpmiLinearizationToString( tIpmiLinearization val )
{
if ( val == eIpmiLinearizationNonlinear )
return "NonLinear";
if ( val <= eIpmiLinearization1OverCube )
return linearization_map[val];
return "Invalid";
}
cIpmiSensorFactors::cIpmiSensorFactors()
: m_analog_data_format( eIpmiAnalogDataFormatUnsigned ),
m_linearization( eIpmiLinearizationLinear ),
m_is_non_linear( false ),
m_m( 0 ),
m_tolerance( 0 ),
m_b( 0 ),
m_r_exp( 0 ),
m_accuracy_exp( 0 ),
m_accuracy( 0 ),
m_b_exp( 0 )
{
}
cIpmiSensorFactors::~cIpmiSensorFactors()
{
}
bool
cIpmiSensorFactors::GetDataFromSdr( const cIpmiSdr *sdr )
{
m_analog_data_format = (tIpmiAnalogeDataFormat)((sdr->m_data[20] >> 6) & 3);
m_linearization = (tIpmiLinearization)(sdr->m_data[23] & 0x7f);
if ( m_linearization <= 11 )
{
m_m = sdr->m_data[24] | ((sdr->m_data[25] & 0xc0) << 2);
m_tolerance = sdr->m_data[25] & 0x3f;
m_b = sdr->m_data[26] | ((sdr->m_data[27] & 0xc0) << 2);
m_accuracy = ((sdr->m_data[27] & 0x3f)
| ((sdr->m_data[28] & 0xf0) << 2));
m_accuracy_exp = (sdr->m_data[28] >> 2) & 0x3;
m_r_exp = (sdr->m_data[29] >> 4) & 0xf;
m_b_exp = sdr->m_data[29] & 0xf;
m_accuracy_factor = (m_accuracy * pow( 10.0, m_accuracy_exp)) / 100.0;
}
if ( m_linearization == eIpmiLinearizationLinear )
m_is_non_linear = false;
else
m_is_non_linear = true;
return true;
}
bool
cIpmiSensorFactors::Cmp( const cIpmiSensorFactors &sf ) const
{
if ( m_analog_data_format != sf.m_analog_data_format )
return false;
if ( m_linearization != sf.m_linearization )
return false;
if ( m_linearization <= 11 )
{
if ( m_m != sf.m_m )
return false;
if ( m_tolerance != sf.m_tolerance )
return false;
if ( m_b != sf.m_b )
return false;
if ( m_accuracy != sf.m_accuracy )
return false;
if ( m_accuracy_exp != sf.m_accuracy_exp )
return false;
if ( m_r_exp != sf.m_r_exp )
return false;
if ( m_b_exp != sf.m_b_exp )
return false;
}
return true;
}
static double
c_linear( double val )
{
return val;
}
static double
c_exp10( double val )
{
return pow( 10.0, val );
}
static double
c_exp2( double val )
{
return pow( 2.0, val );
}
static double
c_1_over_x( double val )
{
return 1.0 / val;
}
static double
c_sqr( double val )
{
return pow( val, 2.0 );
}
static double
c_cube( double val )
{
return pow( val, 3.0 );
}
static double
c_1_over_cube( double val )
{
return 1.0 / pow( val, 3.0 );
}
// We have to define our own log2 function
// because some versions of FreeBSD do not provide it
// and some versions of FreeBSD provide it in a very
// specific way.
static double ipmi_log2( double val )
{
return log( val ) / M_LN2;
}
typedef double (*linearizer)( double val );
static linearizer linearize[12] =
{
c_linear,
log,
log10,
ipmi_log2,
exp,
c_exp10,
c_exp2,
c_1_over_x,
c_sqr,
c_cube,
sqrt,
c_1_over_cube
};
static int
sign_extend( int m, int bits )
{
if ( m & (1 << (bits-1)) )
return m | (-1 << bits);
else
return m & (~(-1 << bits));
}
bool
cIpmiSensorFactors::ConvertFromRaw( unsigned int val,
double &result,
bool is_hysteresis) const
{
double m, b, b_exp, r_exp, fval;
linearizer c_func;
if ( m_linearization == eIpmiLinearizationNonlinear )
c_func = c_linear;
else if ( m_linearization <= 11 )
c_func = linearize[m_linearization];
else
return false;
val &= 0xff;
m = m_m;
b = m_b;
r_exp = m_r_exp;
b_exp = m_b_exp;
if ( is_hysteresis == true )
{
if ( val == 0 )
{
result = 0;
return true;
}
// For hysteresis : no offset + abs value
b = 0;
if ( m < 0 )
m = -m;
}
switch( m_analog_data_format )
{
case eIpmiAnalogDataFormatUnsigned:
fval = val;
break;
case eIpmiAnalogDataFormat1Compl:
val = sign_extend( val, 8 );
if ( val == 0xffffffff )
val += 1;
fval = val;
break;
case eIpmiAnalogDataFormat2Compl:
fval = sign_extend( val, 8 );
break;
default:
return false;
}
result = c_func( ((m * fval) + (b * pow(10, b_exp))) * pow(10, r_exp) );
return true;
}
bool
cIpmiSensorFactors::ConvertToRaw( tIpmiRound rounding,
double val,
unsigned int &result,
bool is_hysteresis,
bool swap_thresholds ) const
{
bool rv;
bool swap;
double cval;
int lowraw, highraw, raw, maxraw, minraw, next_raw;
if (is_hysteresis == true)
swap = false;
else
swap = swap_thresholds;
switch( m_analog_data_format )
{
case eIpmiAnalogDataFormatUnsigned:
lowraw = 0;
highraw = 255;
minraw = 0;
maxraw = 255;
next_raw = 128;
break;
case eIpmiAnalogDataFormat1Compl:
lowraw = -127;
highraw = 127;
minraw = -127;
maxraw = 127;
next_raw = 0;
break;
case eIpmiAnalogDataFormat2Compl:
lowraw = -128;
highraw = 127;
minraw = -128;
maxraw = 127;
next_raw = 0;
break;
default:
return false;
}
// We do a binary search for the right value. Yuck, but I don't
// have a better plan that will work with non-linear sensors.
do
{
raw = next_raw;
rv = ConvertFromRaw( raw, cval, is_hysteresis );
if ( !rv )
return false;
// If swap == true, when raw value increases
// the corresponding interpreted value decreases
// so we have to take that into account when searching
if ((( swap == false) && ( cval < val ))
|| (( swap == true) && ( cval > val )))
{
next_raw = ((highraw - raw) / 2) + raw;
lowraw = raw;
}
else
{
next_raw = ((raw - lowraw) / 2) + lowraw;
highraw = raw;
}
}
while( raw != next_raw );
// The above loop gets us to within 1 of what it should be, we
// have to look at rounding to make the final decision.
switch( rounding )
{
case eRoundNormal:
// If swap == true, when raw value increases
// the corresponding interpreted value decreases
// so we have to take that into account when searching
if ((( swap == false ) && ( val > cval ))
|| (( swap == true ) && ( val < cval )))
{
if ( raw < maxraw )
{
double nval;
rv = ConvertFromRaw( raw + 1, nval, is_hysteresis );
if ( !rv )
return false;
nval = cval + ((nval - cval) / 2.0);
if ((( swap == false ) && ( val >= nval ))
|| (( swap == true ) && ( val <= nval )))
raw++;
}
}
else
{
if ( raw > minraw )
{
double pval;
rv = ConvertFromRaw( raw-1, pval, is_hysteresis );
if ( !rv )
return false;
pval = pval + ((cval - pval) / 2.0);
if ((( swap == false ) && ( val < pval ))
|| (( swap == true ) && ( val > pval )))
raw--;
}
}
break;
case eRoundUp:
// If swap == true, when raw value increases
// the corresponding interpreted value decreases
// so we have to take that into account when searching
if ( swap == false )
{
if ((val > cval) && (raw < maxraw))
raw++;
}
else
{
if ((val < cval) && (raw < maxraw))
raw++;
};
break;
case eRoundDown:
// If swap == true, when raw value increases
// the corresponding interpreted value decreases
// so we have to take that into account when searching
if ( swap == false )
{
if ( ( val < cval) && (raw > minraw ) )
raw--;
}
else
{
if ( ( val > cval) && (raw > minraw ) )
raw--;
};
break;
}
if ( m_analog_data_format == eIpmiAnalogDataFormat1Compl )
if ( raw < 0 )
raw -= 1;
result = raw & 0xff;
return true;
}
|