blob: a2f867a3369c10f7115ea65dae467702df6f7d2b [file] [log] [blame]
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001/*
2// Copyright (c) 2017 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#pragma once
Jason M. Bills72867de2018-11-28 12:46:59 -080018#include <host-ipmid/ipmid-api.h>
19
20#include <cmath>
21#include <iostream>
22#include <phosphor-logging/log.hpp>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070023
24namespace ipmi
25{
Jason M. Bills72867de2018-11-28 12:46:59 -080026static constexpr int16_t maxInt10 = 0x1FF;
27static constexpr int16_t minInt10 = -(0x200);
28static constexpr int8_t maxInt4 = 7;
29static constexpr int8_t minInt4 = -8;
30
31static inline bool getSensorAttributes(const double max, const double min,
32 int16_t& mValue, int8_t& rExp,
33 int16_t& bValue, int8_t& bExp,
34 bool& bSigned)
35{
36 // computing y = (10^rRexp) * (Mx + (B*(10^Bexp)))
37 // check for 0, assume always positive
38 double mDouble;
39 double bDouble;
40 if (!(max > min))
41 {
42 phosphor::logging::log<phosphor::logging::level::DEBUG>(
43 "getSensorAttributes: Max must be greater than min");
44 return false;
45 }
46 else
47 {
48 mDouble = (max - min) / 0xFF;
49 }
50 if (!mDouble)
51 {
52 mDouble = 1;
53 }
54
55 if (min < 0)
56 {
57 bSigned = true;
58 bDouble = floor(0.5 + ((max + min) / 2));
59 }
60 else
61 {
62 bSigned = false;
63 bDouble = min;
64 }
65
66 rExp = 0;
67
68 // M too big for 10 bit variable
69 while (mDouble > maxInt10)
70 {
71 if (rExp == maxInt4)
72 {
73 phosphor::logging::log<phosphor::logging::level::DEBUG>(
74 "rExp Too big, Max and Min range too far",
75 phosphor::logging::entry("REXP=%d", rExp));
76 return false;
77 }
78 mDouble /= 10;
79 rExp += 1;
80 }
81
82 // M too small, loop until we lose less than 1 eight bit count of precision
83 while (((mDouble - floor(mDouble)) / mDouble) > (1.0 / 255))
84 {
85 if (rExp == minInt4)
86 {
87 phosphor::logging::log<phosphor::logging::level::DEBUG>(
88 "rExp Too Small, Max and Min range too close");
89 return false;
90 }
91 // check to see if we reached the limit of where we can adjust back the
92 // B value
93 if (bDouble / std::pow(10, rExp + minInt4 - 1) > bDouble)
94 {
95 if (mDouble < 1.0)
96 {
97 phosphor::logging::log<phosphor::logging::level::DEBUG>(
98 "Could not find mValue and B value with enough "
99 "precision.");
100 return false;
101 }
102 break;
103 }
104 // can't multiply M any more, max precision reached
105 else if (mDouble * 10 > maxInt10)
106 {
107 break;
108 }
109 mDouble *= 10;
110 rExp -= 1;
111 }
112
113 bDouble /= std::pow(10, rExp);
114 bExp = 0;
115
116 // B too big for 10 bit variable
117 while (bDouble > maxInt10 || bDouble < minInt10)
118 {
119 if (bExp == maxInt4)
120 {
121 phosphor::logging::log<phosphor::logging::level::DEBUG>(
122 "bExp Too Big, Max and Min range need to be adjusted");
123 return false;
124 }
125 bDouble /= 10;
126 bExp += 1;
127 }
128
129 while (((fabs(bDouble) - floor(fabs(bDouble))) / fabs(bDouble)) >
130 (1.0 / 255))
131 {
132 if (bExp == minInt4)
133 {
134 phosphor::logging::log<phosphor::logging::level::DEBUG>(
135 "bExp Too Small, Max and Min range need to be adjusted");
136 return false;
137 }
138 bDouble *= 10;
139 bExp -= 1;
140 }
141
142 mValue = static_cast<int16_t>(mDouble + 0.5) & maxInt10;
143 bValue = static_cast<int16_t>(bDouble + 0.5) & maxInt10;
144
145 return true;
146}
147
148static inline uint8_t
149 scaleIPMIValueFromDouble(const double value, const uint16_t mValue,
150 const int8_t rExp, const uint16_t bValue,
151 const int8_t bExp, const bool bSigned)
152{
153 uint32_t scaledValue =
154 (value - (bValue * std::pow(10, bExp) * std::pow(10, rExp))) /
155 (mValue * std::pow(10, rExp));
156 if (bSigned)
157 {
158 return static_cast<int8_t>(scaledValue);
159 }
160 else
161 {
162 return static_cast<uint8_t>(scaledValue);
163 }
164}
165
166static inline uint8_t getScaledIPMIValue(const double value, const double max,
167 const double min)
168{
169 int16_t mValue = 0;
170 int8_t rExp = 0;
171 int16_t bValue = 0;
172 int8_t bExp = 0;
173 bool bSigned = 0;
174 bool result = 0;
175
176 result = getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned);
177 if (!result)
178 {
179 return 0xFF;
180 }
181 return scaleIPMIValueFromDouble(value, mValue, rExp, bValue, bExp, bSigned);
182}
183
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700184} // namespace ipmi