blob: 503a25638d8d634196c42fc9b6dce4a80b5a9ff1 [file] [log] [blame]
James Feist6ef20402019-01-07 16:45:08 -08001/*
2// Copyright (c) 2019 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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <IpmbSensor.hpp>
18#include <Utils.hpp>
19#include <VariantVisitors.hpp>
James Feist6ef20402019-01-07 16:45:08 -080020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
James Feist6ef20402019-01-07 16:45:08 -080027#include <chrono>
Ed Tanous8a57ec02020-10-09 12:46:52 -070028#include <cmath>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <functional>
James Feist6ef20402019-01-07 16:45:08 -080030#include <iostream>
31#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <memory>
James Feist6ef20402019-01-07 16:45:08 -080033#include <numeric>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <string>
35#include <tuple>
36#include <variant>
James Feist6ef20402019-01-07 16:45:08 -080037#include <vector>
38
39constexpr const bool debug = false;
40
41constexpr const char* configInterface =
42 "xyz.openbmc_project.Configuration.IpmbSensor";
43static constexpr double ipmbMaxReading = 0xFF;
44static constexpr double ipmbMinReading = 0;
45
46static constexpr uint8_t meAddress = 1;
47static constexpr uint8_t lun = 0;
Anoop S832a2c62020-11-20 19:21:22 +000048static constexpr uint8_t hostSMbusIndexDefault = 0x03;
James Feist6ef20402019-01-07 16:45:08 -080049
Vijay Khemka682a5cb2019-07-18 17:34:03 -070050static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
51
James Feist6ef20402019-01-07 16:45:08 -080052using IpmbMethodType =
53 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
54
James Feistf7e2c5d2019-02-13 17:27:51 -080055boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>> sensors;
56
James Feist0d4f2bd2019-03-05 13:15:40 -080057std::unique_ptr<boost::asio::deadline_timer> initCmdTimer;
58
James Feist6ef20402019-01-07 16:45:08 -080059IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
60 boost::asio::io_service& io,
61 const std::string& sensorName,
62 const std::string& sensorConfiguration,
63 sdbusplus::asio::object_server& objectServer,
64 std::vector<thresholds::Threshold>&& thresholdData,
Anoop S832a2c62020-11-20 19:21:22 +000065 uint8_t deviceAddress, uint8_t hostSMbusIndex,
66 std::string& sensorTypeName) :
James Feist6ef20402019-01-07 16:45:08 -080067 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
James Feist930fcde2019-05-28 12:58:43 -070068 std::move(thresholdData), sensorConfiguration,
69 "xyz.openbmc_project.Configuration.ExitAirTemp", ipmbMaxReading,
James Feiste3338522020-09-15 15:40:30 -070070 ipmbMinReading, conn, PowerState::on),
Anoop S832a2c62020-11-20 19:21:22 +000071 deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex),
72 objectServer(objectServer), waitTimer(io)
James Feist6ef20402019-01-07 16:45:08 -080073{
Vijay Khemka682a5cb2019-07-18 17:34:03 -070074 std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name;
75
James Feist6ef20402019-01-07 16:45:08 -080076 sensorInterface = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070077 dbusPath, "xyz.openbmc_project.Sensor.Value");
James Feist6ef20402019-01-07 16:45:08 -080078
79 if (thresholds::hasWarningInterface(thresholds))
80 {
81 thresholdInterfaceWarning = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070082 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning");
James Feist6ef20402019-01-07 16:45:08 -080083 }
84 if (thresholds::hasCriticalInterface(thresholds))
85 {
86 thresholdInterfaceCritical = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070087 dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical");
James Feist6ef20402019-01-07 16:45:08 -080088 }
James Feist2adc95c2019-09-30 14:55:28 -070089 association = objectServer.add_interface(dbusPath, association::interface);
James Feist6ef20402019-01-07 16:45:08 -080090}
91
92IpmbSensor::~IpmbSensor()
93{
94 waitTimer.cancel();
95 objectServer.remove_interface(thresholdInterfaceWarning);
96 objectServer.remove_interface(thresholdInterfaceCritical);
97 objectServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080098 objectServer.remove_interface(association);
James Feist6ef20402019-01-07 16:45:08 -080099}
100
Zev Weiss6b6891c2021-04-22 02:46:21 -0500101std::string IpmbSensor::getSubTypeUnits(void)
102{
103 switch (subType)
104 {
105 case IpmbSubType::temp:
106 return sensor_paths::unitDegreesC;
107 case IpmbSubType::curr:
108 return sensor_paths::unitAmperes;
109 case IpmbSubType::power:
110 return sensor_paths::unitWatts;
111 case IpmbSubType::volt:
112 return sensor_paths::unitVolts;
113 case IpmbSubType::util:
114 return sensor_paths::unitPercent;
115 default:
116 throw std::runtime_error("Invalid sensor type");
117 }
118}
119
James Feist6ef20402019-01-07 16:45:08 -0800120void IpmbSensor::init(void)
121{
James Feist6ef20402019-01-07 16:45:08 -0800122 loadDefaults();
Zev Weiss6b6891c2021-04-22 02:46:21 -0500123 setInitialProperties(dbusConnection, getSubTypeUnits());
James Feist6ef20402019-01-07 16:45:08 -0800124 if (initCommand)
125 {
James Feistf7e2c5d2019-02-13 17:27:51 -0800126 runInitCmd();
127 }
128 read();
129}
130
131void IpmbSensor::runInitCmd()
132{
133 if (initCommand)
134 {
James Feist6ef20402019-01-07 16:45:08 -0800135 dbusConnection->async_method_call(
136 [this](boost::system::error_code ec,
137 const IpmbMethodType& response) {
138 const int& status = std::get<0>(response);
139
140 if (ec || status)
141 {
142 std::cerr
143 << "Error setting init command for device: " << name
144 << "\n";
145 }
James Feist6ef20402019-01-07 16:45:08 -0800146 },
147 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
148 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
149 "sendRequest", commandAddress, netfn, lun, *initCommand, initData);
150 }
James Feist6ef20402019-01-07 16:45:08 -0800151}
152
153void IpmbSensor::loadDefaults()
154{
155 if (type == IpmbType::meSensor)
156 {
157 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200158 netfn = ipmi::sensor::netFn;
159 command = ipmi::sensor::getSensorReading;
James Feist6ef20402019-01-07 16:45:08 -0800160 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700161 readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800162 }
163 else if (type == IpmbType::PXE1410CVR)
164 {
165 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200166 netfn = ipmi::me_bridge::netFn;
167 command = ipmi::me_bridge::sendRawPmbus;
168 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700169 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000170 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
171 deviceAddress, 0x00, 0x00, 0x00, 0x00,
172 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700173 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000174 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
175 deviceAddress, 0x00, 0x00, 0x00, 0x00,
176 0x02, 0x00, 0x00, 0x00};
Jayashree-D37322572021-03-19 17:40:56 +0530177 readingFormat = ReadingFormat::linearElevenBit;
James Feist6ef20402019-01-07 16:45:08 -0800178 }
179 else if (type == IpmbType::IR38363VR)
180 {
181 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200182 netfn = ipmi::me_bridge::netFn;
183 command = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700184 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000185 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
186 deviceAddress, 00, 0x00, 0x00, 0x00,
187 0x01, 0x02, 0x8D};
James Feistd7ae29a2020-06-25 15:42:46 -0700188 readingFormat = ReadingFormat::elevenBitShift;
James Feist6ef20402019-01-07 16:45:08 -0800189 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700190 else if (type == IpmbType::ADM1278HSC)
191 {
192 commandAddress = meAddress;
193 switch (subType)
194 {
195 case IpmbSubType::temp:
196 case IpmbSubType::curr:
197 uint8_t snsNum;
198 if (subType == IpmbSubType::temp)
Ed Tanous8a57ec02020-10-09 12:46:52 -0700199 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700200 snsNum = 0x8d;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700201 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700202 else
Ed Tanous8a57ec02020-10-09 12:46:52 -0700203 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700204 snsNum = 0x8c;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700205 }
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200206 netfn = ipmi::me_bridge::netFn;
207 command = ipmi::me_bridge::sendRawPmbus;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700208 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress,
209 0x00, 0x00, 0x01, 0x02, snsNum};
James Feistd7ae29a2020-06-25 15:42:46 -0700210 readingFormat = ReadingFormat::elevenBit;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700211 break;
212 case IpmbSubType::power:
213 case IpmbSubType::volt:
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200214 netfn = ipmi::sensor::netFn;
215 command = ipmi::sensor::getSensorReading;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700216 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700217 readingFormat = ReadingFormat::byte0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700218 break;
219 default:
220 throw std::runtime_error("Invalid sensor type");
221 }
222 }
James Feist6ef20402019-01-07 16:45:08 -0800223 else if (type == IpmbType::mpsVR)
224 {
225 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200226 netfn = ipmi::me_bridge::netFn;
227 command = ipmi::me_bridge::sendRawPmbus;
228 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700229 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000230 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
231 deviceAddress, 0x00, 0x00, 0x00, 0x00,
232 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700233 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000234 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
235 deviceAddress, 0x00, 0x00, 0x00, 0x00,
236 0x02, 0x00, 0x00, 0x00};
James Feistd7ae29a2020-06-25 15:42:46 -0700237 readingFormat = ReadingFormat::byte3;
James Feist6ef20402019-01-07 16:45:08 -0800238 }
239 else
240 {
241 throw std::runtime_error("Invalid sensor type");
242 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200243
244 if (subType == IpmbSubType::util)
245 {
246 // Utilization need to be scaled to percent
247 maxValue = 100;
248 minValue = 0;
249 }
James Feist6ef20402019-01-07 16:45:08 -0800250}
251
252void IpmbSensor::checkThresholds(void)
253{
James Feist6ef20402019-01-07 16:45:08 -0800254 thresholds::checkThresholds(this);
255}
256
James Feist961bf092020-07-01 16:38:12 -0700257bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp)
James Feistd7ae29a2020-06-25 15:42:46 -0700258{
259
260 switch (readingFormat)
261 {
262 case (ReadingFormat::byte0):
James Feiste4a970d2020-08-19 11:21:58 -0700263 {
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200264 if (command == ipmi::sensor::getSensorReading &&
265 !ipmi::sensor::isValid(data))
James Feistcf4238e2020-07-28 16:40:03 -0700266 {
267 return false;
268 }
James Feist961bf092020-07-01 16:38:12 -0700269 resp = data[0];
270 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700271 }
James Feistd7ae29a2020-06-25 15:42:46 -0700272 case (ReadingFormat::byte3):
James Feiste4a970d2020-08-19 11:21:58 -0700273 {
James Feistd7ae29a2020-06-25 15:42:46 -0700274 if (data.size() < 4)
275 {
James Feist961bf092020-07-01 16:38:12 -0700276 if (!errCount)
277 {
278 std::cerr << "Invalid data length returned for " << name
279 << "\n";
280 }
281 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700282 }
James Feist961bf092020-07-01 16:38:12 -0700283 resp = data[3];
284 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700285 }
James Feistd7ae29a2020-06-25 15:42:46 -0700286 case (ReadingFormat::elevenBit):
James Feiste4a970d2020-08-19 11:21:58 -0700287 {
James Feistd7ae29a2020-06-25 15:42:46 -0700288 if (data.size() < 5)
289 {
James Feist961bf092020-07-01 16:38:12 -0700290 if (!errCount)
291 {
292 std::cerr << "Invalid data length returned for " << name
293 << "\n";
294 }
295 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700296 }
297
James Feiste4a970d2020-08-19 11:21:58 -0700298 int16_t value = ((data[4] << 8) | data[3]);
James Feiste4a970d2020-08-19 11:21:58 -0700299 resp = value;
James Feist961bf092020-07-01 16:38:12 -0700300 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700301 }
James Feistd7ae29a2020-06-25 15:42:46 -0700302 case (ReadingFormat::elevenBitShift):
James Feiste4a970d2020-08-19 11:21:58 -0700303 {
James Feistd7ae29a2020-06-25 15:42:46 -0700304 if (data.size() < 5)
305 {
James Feist961bf092020-07-01 16:38:12 -0700306 if (!errCount)
307 {
308 std::cerr << "Invalid data length returned for " << name
309 << "\n";
310 }
311 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700312 }
313
James Feist961bf092020-07-01 16:38:12 -0700314 resp = ((data[4] << 8) | data[3]) >> 3;
315 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700316 }
Jayashree-D37322572021-03-19 17:40:56 +0530317 case (ReadingFormat::linearElevenBit):
318 {
319 if (data.size() < 5)
320 {
321 if (!errCount)
322 {
323 std::cerr << "Invalid data length returned for " << name
324 << "\n";
325 }
326 return false;
327 }
328
329 int16_t value = ((data[4] << 8) | data[3]);
330 constexpr const size_t shift = 16 - 11; // 11bit into 16bit
331 value <<= shift;
332 value >>= shift;
333 resp = value;
334 return true;
335 }
James Feistd7ae29a2020-06-25 15:42:46 -0700336 default:
337 throw std::runtime_error("Invalid reading type");
338 }
339}
340
James Feist6ef20402019-01-07 16:45:08 -0800341void IpmbSensor::read(void)
342{
343 static constexpr size_t pollTime = 1; // in seconds
344
345 waitTimer.expires_from_now(boost::posix_time::seconds(pollTime));
346 waitTimer.async_wait([this](const boost::system::error_code& ec) {
347 if (ec == boost::asio::error::operation_aborted)
348 {
349 return; // we're being canceled
350 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200351 if (!readingStateGood())
James Feist6ef20402019-01-07 16:45:08 -0800352 {
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200353 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist6ef20402019-01-07 16:45:08 -0800354 read();
355 return;
356 }
357 dbusConnection->async_method_call(
358 [this](boost::system::error_code ec,
359 const IpmbMethodType& response) {
360 const int& status = std::get<0>(response);
361 if (ec || status)
362 {
James Feist961bf092020-07-01 16:38:12 -0700363 incrementError();
James Feist6ef20402019-01-07 16:45:08 -0800364 read();
365 return;
366 }
367 const std::vector<uint8_t>& data = std::get<5>(response);
368 if constexpr (debug)
369 {
370 std::cout << name << ": ";
371 for (size_t d : data)
372 {
373 std::cout << d << " ";
374 }
375 std::cout << "\n";
376 }
James Feistd7ae29a2020-06-25 15:42:46 -0700377 if (data.empty())
James Feist6ef20402019-01-07 16:45:08 -0800378 {
James Feist961bf092020-07-01 16:38:12 -0700379 incrementError();
James Feistd7ae29a2020-06-25 15:42:46 -0700380 read();
381 return;
James Feist6ef20402019-01-07 16:45:08 -0800382 }
James Feist961bf092020-07-01 16:38:12 -0700383
384 double value = 0;
385
386 if (!processReading(data, value))
387 {
388 incrementError();
389 read();
390 return;
391 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700392
393 // rawValue only used in debug logging
394 // up to 5th byte in data are used to derive value
395 size_t end = std::min(sizeof(uint64_t), data.size());
396 uint64_t rawData = 0;
397 for (size_t i = 0; i < end; i++)
Zhikui Rend3da1282020-09-11 17:02:01 -0700398 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700399 reinterpret_cast<uint8_t*>(&rawData)[i] = data[i];
Zhikui Rend3da1282020-09-11 17:02:01 -0700400 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700401 rawValue = static_cast<double>(rawData);
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700402
403 /* Adjust value as per scale and offset */
404 value = (value * scaleVal) + offsetVal;
James Feist6ef20402019-01-07 16:45:08 -0800405 updateValue(value);
406 read();
407 },
408 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
409 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
410 "sendRequest", commandAddress, netfn, lun, command, commandData);
411 });
412}
413void createSensors(
414 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
415 boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>&
416 sensors,
417 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
418{
419 if (!dbusConnection)
420 {
421 std::cerr << "Connection not created\n";
422 return;
423 }
424 dbusConnection->async_method_call(
425 [&](boost::system::error_code ec, const ManagedObjectType& resp) {
426 if (ec)
427 {
428 std::cerr << "Error contacting entity manager\n";
429 return;
430 }
431 for (const auto& pathPair : resp)
432 {
433 for (const auto& entry : pathPair.second)
434 {
435 if (entry.first != configInterface)
436 {
437 continue;
438 }
439 std::string name =
440 loadVariant<std::string>(entry.second, "Name");
441
442 std::vector<thresholds::Threshold> sensorThresholds;
443 if (!parseThresholdsFromConfig(pathPair.second,
444 sensorThresholds))
445 {
446 std::cerr << "error populating thresholds for " << name
447 << "\n";
448 }
449 uint8_t deviceAddress =
450 loadVariant<uint8_t>(entry.second, "Address");
451
452 std::string sensorClass =
453 loadVariant<std::string>(entry.second, "Class");
Anoop S832a2c62020-11-20 19:21:22 +0000454 uint8_t hostSMbusIndex = hostSMbusIndexDefault;
455 auto findSmType = entry.second.find("HostSMbusIndex");
456 if (findSmType != entry.second.end())
457 {
458 hostSMbusIndex = std::visit(
459 VariantToUnsignedIntVisitor(), findSmType->second);
460 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700461
462 /* Default sensor type is "temperature" */
463 std::string sensorTypeName = "temperature";
464 auto findType = entry.second.find("SensorType");
465 if (findType != entry.second.end())
466 {
467 sensorTypeName = std::visit(VariantToStringVisitor(),
468 findType->second);
469 }
470
James Feist6ef20402019-01-07 16:45:08 -0800471 auto& sensor = sensors[name];
472 sensor = std::make_unique<IpmbSensor>(
473 dbusConnection, io, name, pathPair.first, objectServer,
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700474 std::move(sensorThresholds), deviceAddress,
Anoop S832a2c62020-11-20 19:21:22 +0000475 hostSMbusIndex, sensorTypeName);
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700476
477 /* Initialize scale and offset value */
478 sensor->scaleVal = 1;
479 sensor->offsetVal = 0;
480
481 auto findScaleVal = entry.second.find("ScaleValue");
482 if (findScaleVal != entry.second.end())
483 {
484 sensor->scaleVal = std::visit(VariantToDoubleVisitor(),
485 findScaleVal->second);
486 }
487
488 auto findOffsetVal = entry.second.find("OffsetValue");
489 if (findOffsetVal != entry.second.end())
490 {
491 sensor->offsetVal = std::visit(VariantToDoubleVisitor(),
492 findOffsetVal->second);
493 }
James Feist6ef20402019-01-07 16:45:08 -0800494
James Feistfc94b212019-02-06 16:14:51 -0800495 auto findPowerState = entry.second.find("PowerState");
496
497 if (findPowerState != entry.second.end())
498 {
499 std::string powerState = std::visit(
500 VariantToStringVisitor(), findPowerState->second);
501
502 setReadState(powerState, sensor->readState);
503 }
504
James Feist6ef20402019-01-07 16:45:08 -0800505 if (sensorClass == "PxeBridgeTemp")
506 {
507 sensor->type = IpmbType::PXE1410CVR;
508 }
509 else if (sensorClass == "IRBridgeTemp")
510 {
511 sensor->type = IpmbType::IR38363VR;
512 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700513 else if (sensorClass == "HSCBridge")
514 {
515 sensor->type = IpmbType::ADM1278HSC;
516 }
James Feist6ef20402019-01-07 16:45:08 -0800517 else if (sensorClass == "MpsBridgeTemp")
518 {
519 sensor->type = IpmbType::mpsVR;
520 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200521 else if (sensorClass == "METemp" ||
522 sensorClass == "MESensor")
James Feist6ef20402019-01-07 16:45:08 -0800523 {
524 sensor->type = IpmbType::meSensor;
525 }
526 else
527 {
528 std::cerr << "Invalid class " << sensorClass << "\n";
529 continue;
530 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700531
532 if (sensorTypeName == "voltage")
533 {
534 sensor->subType = IpmbSubType::volt;
535 }
536 else if (sensorTypeName == "power")
537 {
538 sensor->subType = IpmbSubType::power;
539 }
540 else if (sensorTypeName == "current")
541 {
542 sensor->subType = IpmbSubType::curr;
543 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200544 else if (sensorTypeName == "utilization")
545 {
546 sensor->subType = IpmbSubType::util;
547 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700548 else
549 {
550 sensor->subType = IpmbSubType::temp;
551 }
James Feist6ef20402019-01-07 16:45:08 -0800552 sensor->init();
553 }
554 }
555 },
556 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
557 "GetManagedObjects");
558}
559
James Feistf7e2c5d2019-02-13 17:27:51 -0800560void reinitSensors(sdbusplus::message::message& message)
561{
James Feist0d4f2bd2019-03-05 13:15:40 -0800562 constexpr const size_t reinitWaitSeconds = 2;
James Feistf7e2c5d2019-02-13 17:27:51 -0800563 std::string objectName;
James Feist52497fd2019-06-07 13:01:33 -0700564 boost::container::flat_map<std::string, std::variant<std::string>> values;
James Feistf7e2c5d2019-02-13 17:27:51 -0800565 message.read(objectName, values);
James Feist0d4f2bd2019-03-05 13:15:40 -0800566
James Feist52497fd2019-06-07 13:01:33 -0700567 auto findStatus = values.find(power::property);
568 if (findStatus != values.end())
James Feistf7e2c5d2019-02-13 17:27:51 -0800569 {
James Feist52497fd2019-06-07 13:01:33 -0700570 bool powerStatus = boost::ends_with(
571 std::get<std::string>(findStatus->second), "Running");
James Feistf7e2c5d2019-02-13 17:27:51 -0800572 if (powerStatus)
573 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800574 if (!initCmdTimer)
James Feistf7e2c5d2019-02-13 17:27:51 -0800575 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800576 // this should be impossible
577 return;
James Feistf7e2c5d2019-02-13 17:27:51 -0800578 }
James Feist0d4f2bd2019-03-05 13:15:40 -0800579 // we seem to send this command too fast sometimes, wait before
580 // sending
581 initCmdTimer->expires_from_now(
582 boost::posix_time::seconds(reinitWaitSeconds));
583
584 initCmdTimer->async_wait([](const boost::system::error_code ec) {
585 if (ec == boost::asio::error::operation_aborted)
586 {
587 return; // we're being canceled
588 }
589
590 for (const auto& sensor : sensors)
591 {
592 if (sensor.second)
593 {
594 sensor.second->runInitCmd();
595 }
596 }
597 });
James Feistf7e2c5d2019-02-13 17:27:51 -0800598 }
599 }
600}
601
James Feistb6c0b912019-07-09 12:21:44 -0700602int main()
James Feist6ef20402019-01-07 16:45:08 -0800603{
604
605 boost::asio::io_service io;
606 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
607 systemBus->request_name("xyz.openbmc_project.IpmbSensor");
608 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6ef20402019-01-07 16:45:08 -0800609
James Feist0d4f2bd2019-03-05 13:15:40 -0800610 initCmdTimer = std::make_unique<boost::asio::deadline_timer>(io);
611
James Feist6ef20402019-01-07 16:45:08 -0800612 io.post([&]() { createSensors(io, objectServer, sensors, systemBus); });
613
614 boost::asio::deadline_timer configTimer(io);
615
616 std::function<void(sdbusplus::message::message&)> eventHandler =
James Feistb6c0b912019-07-09 12:21:44 -0700617 [&](sdbusplus::message::message&) {
James Feist6ef20402019-01-07 16:45:08 -0800618 configTimer.expires_from_now(boost::posix_time::seconds(1));
619 // create a timer because normally multiple properties change
620 configTimer.async_wait([&](const boost::system::error_code& ec) {
621 if (ec == boost::asio::error::operation_aborted)
622 {
623 return; // we're being canceled
624 }
625 createSensors(io, objectServer, sensors, systemBus);
626 if (sensors.empty())
627 {
628 std::cout << "Configuration not detected\n";
629 }
630 });
631 };
632
James Feistf7e2c5d2019-02-13 17:27:51 -0800633 sdbusplus::bus::match::match configMatch(
James Feist6ef20402019-01-07 16:45:08 -0800634 static_cast<sdbusplus::bus::bus&>(*systemBus),
635 "type='signal',member='PropertiesChanged',path_namespace='" +
636 std::string(inventoryPath) + "',arg0namespace='" + configInterface +
637 "'",
638 eventHandler);
639
James Feistf7e2c5d2019-02-13 17:27:51 -0800640 sdbusplus::bus::match::match powerChangeMatch(
641 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist52497fd2019-06-07 13:01:33 -0700642 "type='signal',interface='" + std::string(properties::interface) +
643 "',path='" + std::string(power::path) + "',arg0='" +
644 std::string(power::interface) + "'",
James Feistf7e2c5d2019-02-13 17:27:51 -0800645 reinitSensors);
646
James Feist6ef20402019-01-07 16:45:08 -0800647 io.run();
648}