blob: 69c1e46111837a1f184f184fb158a97a6ac9a1e2 [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>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070022#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/asio/object_server.hpp>
24#include <sdbusplus/bus/match.hpp>
25
James Feist6ef20402019-01-07 16:45:08 -080026#include <chrono>
Ed Tanous8a57ec02020-10-09 12:46:52 -070027#include <cmath>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <functional>
James Feist6ef20402019-01-07 16:45:08 -080029#include <iostream>
30#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
James Feist6ef20402019-01-07 16:45:08 -080032#include <numeric>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <string>
34#include <tuple>
35#include <variant>
James Feist6ef20402019-01-07 16:45:08 -080036#include <vector>
37
38constexpr const bool debug = false;
39
40constexpr const char* configInterface =
41 "xyz.openbmc_project.Configuration.IpmbSensor";
42static constexpr double ipmbMaxReading = 0xFF;
43static constexpr double ipmbMinReading = 0;
44
45static constexpr uint8_t meAddress = 1;
46static constexpr uint8_t lun = 0;
Anoop S832a2c62020-11-20 19:21:22 +000047static constexpr uint8_t hostSMbusIndexDefault = 0x03;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053048static constexpr float pollRateDefault = 1; // in seconds
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,
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053066 const float pollRate, std::string& sensorTypeName) :
Zhikui Renda98f092021-11-01 09:41:08 -070067 Sensor(escapeName(sensorName), std::move(thresholdData),
68 sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
69 false, false, ipmbMaxReading, ipmbMinReading, conn, PowerState::on),
Anoop S832a2c62020-11-20 19:21:22 +000070 deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex),
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053071 sensorPollMs(static_cast<int>(pollRate * 1000)), objectServer(objectServer),
72 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
Jayashree Dhanapal56678082022-01-04 17:27:20 +053079 for (const auto& threshold : thresholds)
James Feist6ef20402019-01-07 16:45:08 -080080 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053081 std::string interface = thresholds::getInterface(threshold.level);
82 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
83 objectServer.add_interface(dbusPath, interface);
James Feist6ef20402019-01-07 16:45:08 -080084 }
James Feist2adc95c2019-09-30 14:55:28 -070085 association = objectServer.add_interface(dbusPath, association::interface);
James Feist6ef20402019-01-07 16:45:08 -080086}
87
88IpmbSensor::~IpmbSensor()
89{
90 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053091 for (const auto& iface : thresholdInterfaces)
92 {
93 objectServer.remove_interface(iface);
94 }
James Feist6ef20402019-01-07 16:45:08 -080095 objectServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080096 objectServer.remove_interface(association);
James Feist6ef20402019-01-07 16:45:08 -080097}
98
Ed Tanous2049bd22022-07-09 07:20:26 -070099std::string IpmbSensor::getSubTypeUnits(void) const
Zev Weiss6b6891c2021-04-22 02:46:21 -0500100{
101 switch (subType)
102 {
103 case IpmbSubType::temp:
104 return sensor_paths::unitDegreesC;
105 case IpmbSubType::curr:
106 return sensor_paths::unitAmperes;
107 case IpmbSubType::power:
108 return sensor_paths::unitWatts;
109 case IpmbSubType::volt:
110 return sensor_paths::unitVolts;
111 case IpmbSubType::util:
112 return sensor_paths::unitPercent;
113 default:
114 throw std::runtime_error("Invalid sensor type");
115 }
116}
117
James Feist6ef20402019-01-07 16:45:08 -0800118void IpmbSensor::init(void)
119{
James Feist6ef20402019-01-07 16:45:08 -0800120 loadDefaults();
Andrei Kartashev39287412022-02-04 16:04:47 +0300121 setInitialProperties(getSubTypeUnits());
James Feist6ef20402019-01-07 16:45:08 -0800122 if (initCommand)
123 {
James Feistf7e2c5d2019-02-13 17:27:51 -0800124 runInitCmd();
125 }
126 read();
127}
128
129void IpmbSensor::runInitCmd()
130{
131 if (initCommand)
132 {
James Feist6ef20402019-01-07 16:45:08 -0800133 dbusConnection->async_method_call(
134 [this](boost::system::error_code ec,
135 const IpmbMethodType& response) {
Ed Tanousbb679322022-05-16 16:10:00 -0700136 const int& status = std::get<0>(response);
James Feist6ef20402019-01-07 16:45:08 -0800137
Ed Tanous2049bd22022-07-09 07:20:26 -0700138 if (ec || (status != 0))
Ed Tanousbb679322022-05-16 16:10:00 -0700139 {
140 std::cerr << "Error setting init command for device: " << name
141 << "\n";
142 }
James Feist6ef20402019-01-07 16:45:08 -0800143 },
144 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
145 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
146 "sendRequest", commandAddress, netfn, lun, *initCommand, initData);
147 }
James Feist6ef20402019-01-07 16:45:08 -0800148}
149
150void IpmbSensor::loadDefaults()
151{
152 if (type == IpmbType::meSensor)
153 {
154 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200155 netfn = ipmi::sensor::netFn;
156 command = ipmi::sensor::getSensorReading;
James Feist6ef20402019-01-07 16:45:08 -0800157 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700158 readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800159 }
160 else if (type == IpmbType::PXE1410CVR)
161 {
162 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200163 netfn = ipmi::me_bridge::netFn;
164 command = ipmi::me_bridge::sendRawPmbus;
165 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700166 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000167 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
168 deviceAddress, 0x00, 0x00, 0x00, 0x00,
169 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700170 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000171 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
172 deviceAddress, 0x00, 0x00, 0x00, 0x00,
173 0x02, 0x00, 0x00, 0x00};
Jayashree-D37322572021-03-19 17:40:56 +0530174 readingFormat = ReadingFormat::linearElevenBit;
James Feist6ef20402019-01-07 16:45:08 -0800175 }
176 else if (type == IpmbType::IR38363VR)
177 {
178 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200179 netfn = ipmi::me_bridge::netFn;
180 command = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700181 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000182 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
183 deviceAddress, 00, 0x00, 0x00, 0x00,
184 0x01, 0x02, 0x8D};
James Feistd7ae29a2020-06-25 15:42:46 -0700185 readingFormat = ReadingFormat::elevenBitShift;
James Feist6ef20402019-01-07 16:45:08 -0800186 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700187 else if (type == IpmbType::ADM1278HSC)
188 {
189 commandAddress = meAddress;
Ed Tanousa771f6a2022-01-14 09:36:51 -0800190 uint8_t snsNum = 0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700191 switch (subType)
192 {
193 case IpmbSubType::temp:
194 case IpmbSubType::curr:
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700195 if (subType == IpmbSubType::temp)
Ed Tanous8a57ec02020-10-09 12:46:52 -0700196 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700197 snsNum = 0x8d;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700198 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700199 else
Ed Tanous8a57ec02020-10-09 12:46:52 -0700200 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700201 snsNum = 0x8c;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700202 }
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200203 netfn = ipmi::me_bridge::netFn;
204 command = ipmi::me_bridge::sendRawPmbus;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700205 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress,
206 0x00, 0x00, 0x01, 0x02, snsNum};
James Feistd7ae29a2020-06-25 15:42:46 -0700207 readingFormat = ReadingFormat::elevenBit;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700208 break;
209 case IpmbSubType::power:
210 case IpmbSubType::volt:
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200211 netfn = ipmi::sensor::netFn;
212 command = ipmi::sensor::getSensorReading;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700213 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700214 readingFormat = ReadingFormat::byte0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700215 break;
216 default:
217 throw std::runtime_error("Invalid sensor type");
218 }
219 }
James Feist6ef20402019-01-07 16:45:08 -0800220 else if (type == IpmbType::mpsVR)
221 {
222 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200223 netfn = ipmi::me_bridge::netFn;
224 command = ipmi::me_bridge::sendRawPmbus;
225 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700226 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000227 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
228 deviceAddress, 0x00, 0x00, 0x00, 0x00,
229 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700230 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000231 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
232 deviceAddress, 0x00, 0x00, 0x00, 0x00,
233 0x02, 0x00, 0x00, 0x00};
James Feistd7ae29a2020-06-25 15:42:46 -0700234 readingFormat = ReadingFormat::byte3;
James Feist6ef20402019-01-07 16:45:08 -0800235 }
236 else
237 {
238 throw std::runtime_error("Invalid sensor type");
239 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200240
241 if (subType == IpmbSubType::util)
242 {
243 // Utilization need to be scaled to percent
244 maxValue = 100;
245 minValue = 0;
246 }
James Feist6ef20402019-01-07 16:45:08 -0800247}
248
249void IpmbSensor::checkThresholds(void)
250{
James Feist6ef20402019-01-07 16:45:08 -0800251 thresholds::checkThresholds(this);
252}
253
James Feist961bf092020-07-01 16:38:12 -0700254bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp)
James Feistd7ae29a2020-06-25 15:42:46 -0700255{
256
257 switch (readingFormat)
258 {
259 case (ReadingFormat::byte0):
James Feiste4a970d2020-08-19 11:21:58 -0700260 {
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200261 if (command == ipmi::sensor::getSensorReading &&
262 !ipmi::sensor::isValid(data))
James Feistcf4238e2020-07-28 16:40:03 -0700263 {
264 return false;
265 }
James Feist961bf092020-07-01 16:38:12 -0700266 resp = data[0];
267 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700268 }
James Feistd7ae29a2020-06-25 15:42:46 -0700269 case (ReadingFormat::byte3):
James Feiste4a970d2020-08-19 11:21:58 -0700270 {
James Feistd7ae29a2020-06-25 15:42:46 -0700271 if (data.size() < 4)
272 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700273 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700274 {
275 std::cerr << "Invalid data length returned for " << name
276 << "\n";
277 }
278 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700279 }
James Feist961bf092020-07-01 16:38:12 -0700280 resp = data[3];
281 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700282 }
James Feistd7ae29a2020-06-25 15:42:46 -0700283 case (ReadingFormat::elevenBit):
James Feiste4a970d2020-08-19 11:21:58 -0700284 {
James Feistd7ae29a2020-06-25 15:42:46 -0700285 if (data.size() < 5)
286 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700287 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700288 {
289 std::cerr << "Invalid data length returned for " << name
290 << "\n";
291 }
292 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700293 }
294
James Feiste4a970d2020-08-19 11:21:58 -0700295 int16_t value = ((data[4] << 8) | data[3]);
James Feiste4a970d2020-08-19 11:21:58 -0700296 resp = value;
James Feist961bf092020-07-01 16:38:12 -0700297 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700298 }
James Feistd7ae29a2020-06-25 15:42:46 -0700299 case (ReadingFormat::elevenBitShift):
James Feiste4a970d2020-08-19 11:21:58 -0700300 {
James Feistd7ae29a2020-06-25 15:42:46 -0700301 if (data.size() < 5)
302 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700303 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700304 {
305 std::cerr << "Invalid data length returned for " << name
306 << "\n";
307 }
308 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700309 }
310
James Feist961bf092020-07-01 16:38:12 -0700311 resp = ((data[4] << 8) | data[3]) >> 3;
312 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700313 }
Jayashree-D37322572021-03-19 17:40:56 +0530314 case (ReadingFormat::linearElevenBit):
315 {
316 if (data.size() < 5)
317 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700318 if (errCount == 0U)
Jayashree-D37322572021-03-19 17:40:56 +0530319 {
320 std::cerr << "Invalid data length returned for " << name
321 << "\n";
322 }
323 return false;
324 }
325
326 int16_t value = ((data[4] << 8) | data[3]);
327 constexpr const size_t shift = 16 - 11; // 11bit into 16bit
328 value <<= shift;
329 value >>= shift;
330 resp = value;
331 return true;
332 }
James Feistd7ae29a2020-06-25 15:42:46 -0700333 default:
334 throw std::runtime_error("Invalid reading type");
335 }
336}
337
James Feist6ef20402019-01-07 16:45:08 -0800338void IpmbSensor::read(void)
339{
Jayashree-D9f6d4fd2021-04-13 18:27:22 +0530340 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feist6ef20402019-01-07 16:45:08 -0800341 waitTimer.async_wait([this](const boost::system::error_code& ec) {
342 if (ec == boost::asio::error::operation_aborted)
343 {
344 return; // we're being canceled
345 }
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200346 if (!readingStateGood())
James Feist6ef20402019-01-07 16:45:08 -0800347 {
Adrian Ambrożewicz623723b2020-07-29 12:53:54 +0200348 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist6ef20402019-01-07 16:45:08 -0800349 read();
350 return;
351 }
352 dbusConnection->async_method_call(
353 [this](boost::system::error_code ec,
354 const IpmbMethodType& response) {
Ed Tanousbb679322022-05-16 16:10:00 -0700355 const int& status = std::get<0>(response);
Ed Tanous2049bd22022-07-09 07:20:26 -0700356 if (ec || (status != 0))
Ed Tanousbb679322022-05-16 16:10:00 -0700357 {
358 incrementError();
James Feist6ef20402019-01-07 16:45:08 -0800359 read();
Ed Tanousbb679322022-05-16 16:10:00 -0700360 return;
361 }
362 const std::vector<uint8_t>& data = std::get<5>(response);
363 if constexpr (debug)
364 {
365 std::cout << name << ": ";
366 for (size_t d : data)
367 {
368 std::cout << d << " ";
369 }
370 std::cout << "\n";
371 }
372 if (data.empty())
373 {
374 incrementError();
375 read();
376 return;
377 }
378
379 double value = 0;
380
381 if (!processReading(data, value))
382 {
383 incrementError();
384 read();
385 return;
386 }
387
388 // rawValue only used in debug logging
389 // up to 5th byte in data are used to derive value
390 size_t end = std::min(sizeof(uint64_t), data.size());
391 uint64_t rawData = 0;
392 for (size_t i = 0; i < end; i++)
393 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700394 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Ed Tanousbb679322022-05-16 16:10:00 -0700395 reinterpret_cast<uint8_t*>(&rawData)[i] = data[i];
396 }
397 rawValue = static_cast<double>(rawData);
398
399 /* Adjust value as per scale and offset */
400 value = (value * scaleVal) + offsetVal;
401 updateValue(value);
402 read();
James Feist6ef20402019-01-07 16:45:08 -0800403 },
404 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
405 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
406 "sendRequest", commandAddress, netfn, lun, command, commandData);
407 });
408}
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530409
410bool IpmbSensor::sensorClassType(const std::string& sensorClass)
411{
412 if (sensorClass == "PxeBridgeTemp")
413 {
414 type = IpmbType::PXE1410CVR;
415 }
416 else if (sensorClass == "IRBridgeTemp")
417 {
418 type = IpmbType::IR38363VR;
419 }
420 else if (sensorClass == "HSCBridge")
421 {
422 type = IpmbType::ADM1278HSC;
423 }
424 else if (sensorClass == "MpsBridgeTemp")
425 {
426 type = IpmbType::mpsVR;
427 }
428 else if (sensorClass == "METemp" || sensorClass == "MESensor")
429 {
430 type = IpmbType::meSensor;
431 }
432 else
433 {
434 std::cerr << "Invalid class " << sensorClass << "\n";
435 return false;
436 }
437 return true;
438}
439
440void IpmbSensor::sensorSubType(const std::string& sensorTypeName)
441{
442 if (sensorTypeName == "voltage")
443 {
444 subType = IpmbSubType::volt;
445 }
446 else if (sensorTypeName == "power")
447 {
448 subType = IpmbSubType::power;
449 }
450 else if (sensorTypeName == "current")
451 {
452 subType = IpmbSubType::curr;
453 }
454 else if (sensorTypeName == "utilization")
455 {
456 subType = IpmbSubType::util;
457 }
458 else
459 {
460 subType = IpmbSubType::temp;
461 }
462}
463
464void IpmbSensor::parseConfigValues(const SensorBaseConfigMap& entry)
465{
466 auto findScaleVal = entry.find("ScaleValue");
467 if (findScaleVal != entry.end())
468 {
469 scaleVal = std::visit(VariantToDoubleVisitor(), findScaleVal->second);
470 }
471
472 auto findOffsetVal = entry.find("OffsetValue");
473 if (findOffsetVal != entry.end())
474 {
475 offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second);
476 }
477
Zev Weissa4d27682022-07-19 15:30:36 -0700478 readState = getPowerState(entry);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530479}
480
James Feist6ef20402019-01-07 16:45:08 -0800481void createSensors(
482 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
483 boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>&
484 sensors,
485 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
486{
487 if (!dbusConnection)
488 {
489 std::cerr << "Connection not created\n";
490 return;
491 }
492 dbusConnection->async_method_call(
493 [&](boost::system::error_code ec, const ManagedObjectType& resp) {
Ed Tanousbb679322022-05-16 16:10:00 -0700494 if (ec)
495 {
496 std::cerr << "Error contacting entity manager\n";
497 return;
498 }
499 for (const auto& pathPair : resp)
500 {
501 for (const auto& entry : pathPair.second)
James Feist6ef20402019-01-07 16:45:08 -0800502 {
Ed Tanousbb679322022-05-16 16:10:00 -0700503 if (entry.first != configInterface)
James Feist6ef20402019-01-07 16:45:08 -0800504 {
Ed Tanousbb679322022-05-16 16:10:00 -0700505 continue;
James Feist6ef20402019-01-07 16:45:08 -0800506 }
Ed Tanousbb679322022-05-16 16:10:00 -0700507 std::string name =
508 loadVariant<std::string>(entry.second, "Name");
509
510 std::vector<thresholds::Threshold> sensorThresholds;
511 if (!parseThresholdsFromConfig(pathPair.second,
512 sensorThresholds))
513 {
514 std::cerr << "error populating thresholds for " << name
515 << "\n";
516 }
517 uint8_t deviceAddress =
518 loadVariant<uint8_t>(entry.second, "Address");
519
520 std::string sensorClass =
521 loadVariant<std::string>(entry.second, "Class");
522
523 uint8_t hostSMbusIndex = hostSMbusIndexDefault;
524 auto findSmType = entry.second.find("HostSMbusIndex");
525 if (findSmType != entry.second.end())
526 {
527 hostSMbusIndex = std::visit(VariantToUnsignedIntVisitor(),
528 findSmType->second);
529 }
530
531 float pollRate = pollRateDefault;
532 auto findPollRate = entry.second.find("PollRate");
533 if (findPollRate != entry.second.end())
534 {
535 pollRate = std::visit(VariantToFloatVisitor(),
536 findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700537 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700538 {
539 pollRate = pollRateDefault;
540 }
541 }
542
543 /* Default sensor type is "temperature" */
544 std::string sensorTypeName = "temperature";
545 auto findType = entry.second.find("SensorType");
546 if (findType != entry.second.end())
547 {
548 sensorTypeName =
549 std::visit(VariantToStringVisitor(), findType->second);
550 }
551
552 auto& sensor = sensors[name];
553 sensor = std::make_unique<IpmbSensor>(
554 dbusConnection, io, name, pathPair.first, objectServer,
555 std::move(sensorThresholds), deviceAddress, hostSMbusIndex,
556 pollRate, sensorTypeName);
557
558 sensor->parseConfigValues(entry.second);
559 if (!(sensor->sensorClassType(sensorClass)))
560 {
561 continue;
562 }
563 sensor->sensorSubType(sensorTypeName);
564 sensor->init();
James Feist6ef20402019-01-07 16:45:08 -0800565 }
Ed Tanousbb679322022-05-16 16:10:00 -0700566 }
James Feist6ef20402019-01-07 16:45:08 -0800567 },
568 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
569 "GetManagedObjects");
570}
571
James Feistf7e2c5d2019-02-13 17:27:51 -0800572void reinitSensors(sdbusplus::message::message& message)
573{
James Feist0d4f2bd2019-03-05 13:15:40 -0800574 constexpr const size_t reinitWaitSeconds = 2;
James Feistf7e2c5d2019-02-13 17:27:51 -0800575 std::string objectName;
James Feist52497fd2019-06-07 13:01:33 -0700576 boost::container::flat_map<std::string, std::variant<std::string>> values;
James Feistf7e2c5d2019-02-13 17:27:51 -0800577 message.read(objectName, values);
James Feist0d4f2bd2019-03-05 13:15:40 -0800578
James Feist52497fd2019-06-07 13:01:33 -0700579 auto findStatus = values.find(power::property);
580 if (findStatus != values.end())
James Feistf7e2c5d2019-02-13 17:27:51 -0800581 {
James Feist52497fd2019-06-07 13:01:33 -0700582 bool powerStatus = boost::ends_with(
Thu Ba Nguyen585d3a02021-07-20 20:25:15 +0700583 std::get<std::string>(findStatus->second), ".Running");
James Feistf7e2c5d2019-02-13 17:27:51 -0800584 if (powerStatus)
585 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800586 if (!initCmdTimer)
James Feistf7e2c5d2019-02-13 17:27:51 -0800587 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800588 // this should be impossible
589 return;
James Feistf7e2c5d2019-02-13 17:27:51 -0800590 }
James Feist0d4f2bd2019-03-05 13:15:40 -0800591 // we seem to send this command too fast sometimes, wait before
592 // sending
593 initCmdTimer->expires_from_now(
594 boost::posix_time::seconds(reinitWaitSeconds));
595
596 initCmdTimer->async_wait([](const boost::system::error_code ec) {
597 if (ec == boost::asio::error::operation_aborted)
598 {
599 return; // we're being canceled
600 }
601
602 for (const auto& sensor : sensors)
603 {
604 if (sensor.second)
605 {
606 sensor.second->runInitCmd();
607 }
608 }
609 });
James Feistf7e2c5d2019-02-13 17:27:51 -0800610 }
611 }
612}
613
James Feistb6c0b912019-07-09 12:21:44 -0700614int main()
James Feist6ef20402019-01-07 16:45:08 -0800615{
616
617 boost::asio::io_service io;
618 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
619 systemBus->request_name("xyz.openbmc_project.IpmbSensor");
620 sdbusplus::asio::object_server objectServer(systemBus);
James Feist6ef20402019-01-07 16:45:08 -0800621
James Feist0d4f2bd2019-03-05 13:15:40 -0800622 initCmdTimer = std::make_unique<boost::asio::deadline_timer>(io);
623
James Feist6ef20402019-01-07 16:45:08 -0800624 io.post([&]() { createSensors(io, objectServer, sensors, systemBus); });
625
626 boost::asio::deadline_timer configTimer(io);
627
628 std::function<void(sdbusplus::message::message&)> eventHandler =
James Feistb6c0b912019-07-09 12:21:44 -0700629 [&](sdbusplus::message::message&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700630 configTimer.expires_from_now(boost::posix_time::seconds(1));
631 // create a timer because normally multiple properties change
632 configTimer.async_wait([&](const boost::system::error_code& ec) {
633 if (ec == boost::asio::error::operation_aborted)
634 {
635 return; // we're being canceled
636 }
637 createSensors(io, objectServer, sensors, systemBus);
638 if (sensors.empty())
639 {
640 std::cout << "Configuration not detected\n";
641 }
642 });
643 };
James Feist6ef20402019-01-07 16:45:08 -0800644
James Feistf7e2c5d2019-02-13 17:27:51 -0800645 sdbusplus::bus::match::match configMatch(
James Feist6ef20402019-01-07 16:45:08 -0800646 static_cast<sdbusplus::bus::bus&>(*systemBus),
647 "type='signal',member='PropertiesChanged',path_namespace='" +
648 std::string(inventoryPath) + "',arg0namespace='" + configInterface +
649 "'",
650 eventHandler);
651
James Feistf7e2c5d2019-02-13 17:27:51 -0800652 sdbusplus::bus::match::match powerChangeMatch(
653 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist52497fd2019-06-07 13:01:33 -0700654 "type='signal',interface='" + std::string(properties::interface) +
655 "',path='" + std::string(power::path) + "',arg0='" +
656 std::string(power::interface) + "'",
James Feistf7e2c5d2019-02-13 17:27:51 -0800657 reinitSensors);
658
Bruce Lee1263c3d2021-06-04 15:16:33 +0800659 setupManufacturingModeMatch(*systemBus);
James Feist6ef20402019-01-07 16:45:08 -0800660 io.run();
Zhikui Rene76a5a62021-07-09 15:16:32 -0700661 return 0;
James Feist6ef20402019-01-07 16:45:08 -0800662}