blob: 51ccde1e91de07c25913f60daf7fefe4efab90c3 [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "IpmbSensor.hpp"
18
19#include "IpmbSDRSensor.hpp"
20#include "Utils.hpp"
21#include "VariantVisitors.hpp"
22
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
James Feist6ef20402019-01-07 16:45:08 -080028#include <chrono>
Ed Tanous8a57ec02020-10-09 12:46:52 -070029#include <cmath>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <functional>
James Feist6ef20402019-01-07 16:45:08 -080031#include <iostream>
32#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <memory>
James Feist6ef20402019-01-07 16:45:08 -080034#include <numeric>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <string>
36#include <tuple>
37#include <variant>
James Feist6ef20402019-01-07 16:45:08 -080038#include <vector>
39
40constexpr const bool debug = false;
41
Zev Weiss054aad82022-08-18 01:37:34 -070042constexpr const char* sensorType = "IpmbSensor";
Jayashree Dhanapal3746c552022-03-21 14:45:52 +053043constexpr const char* sdrInterface = "IpmbDevice";
44
James Feist6ef20402019-01-07 16:45:08 -080045static constexpr double ipmbMaxReading = 0xFF;
46static constexpr double ipmbMinReading = 0;
47
48static constexpr uint8_t meAddress = 1;
49static constexpr uint8_t lun = 0;
Anoop S832a2c62020-11-20 19:21:22 +000050static constexpr uint8_t hostSMbusIndexDefault = 0x03;
Jayashree Dhanapal6ee62942021-12-14 15:22:23 +053051static constexpr uint8_t ipmbBusIndexDefault = 0;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053052static constexpr float pollRateDefault = 1; // in seconds
James Feist6ef20402019-01-07 16:45:08 -080053
Vijay Khemka682a5cb2019-07-18 17:34:03 -070054static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
55
Vikash Chandola1f847972022-09-28 09:47:32 +000056boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>> sensors;
Jayashree Dhanapal3746c552022-03-21 14:45:52 +053057boost::container::flat_map<uint8_t, std::shared_ptr<IpmbSDRDevice>> sdrsensor;
James Feistf7e2c5d2019-02-13 17:27:51 -080058
Ed Tanous9b4a20e2022-09-06 08:47:11 -070059std::unique_ptr<boost::asio::steady_timer> initCmdTimer;
James Feist0d4f2bd2019-03-05 13:15:40 -080060
James Feist6ef20402019-01-07 16:45:08 -080061IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080062 boost::asio::io_context& io,
James Feist6ef20402019-01-07 16:45:08 -080063 const std::string& sensorName,
64 const std::string& sensorConfiguration,
65 sdbusplus::asio::object_server& objectServer,
66 std::vector<thresholds::Threshold>&& thresholdData,
Anoop S832a2c62020-11-20 19:21:22 +000067 uint8_t deviceAddress, uint8_t hostSMbusIndex,
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053068 const float pollRate, std::string& sensorTypeName) :
Zhikui Renda98f092021-11-01 09:41:08 -070069 Sensor(escapeName(sensorName), std::move(thresholdData),
Zev Weiss054aad82022-08-18 01:37:34 -070070 sensorConfiguration, "IpmbSensor", false, false, ipmbMaxReading,
71 ipmbMinReading, conn, PowerState::on),
Anoop S832a2c62020-11-20 19:21:22 +000072 deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex),
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053073 sensorPollMs(static_cast<int>(pollRate * 1000)), objectServer(objectServer),
74 waitTimer(io)
James Feist6ef20402019-01-07 16:45:08 -080075{
Vijay Khemka682a5cb2019-07-18 17:34:03 -070076 std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name;
77
James Feist6ef20402019-01-07 16:45:08 -080078 sensorInterface = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070079 dbusPath, "xyz.openbmc_project.Sensor.Value");
James Feist6ef20402019-01-07 16:45:08 -080080
Jayashree Dhanapal56678082022-01-04 17:27:20 +053081 for (const auto& threshold : thresholds)
James Feist6ef20402019-01-07 16:45:08 -080082 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053083 std::string interface = thresholds::getInterface(threshold.level);
84 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
85 objectServer.add_interface(dbusPath, interface);
James Feist6ef20402019-01-07 16:45:08 -080086 }
James Feist2adc95c2019-09-30 14:55:28 -070087 association = objectServer.add_interface(dbusPath, association::interface);
James Feist6ef20402019-01-07 16:45:08 -080088}
89
90IpmbSensor::~IpmbSensor()
91{
92 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053093 for (const auto& iface : thresholdInterfaces)
94 {
95 objectServer.remove_interface(iface);
96 }
James Feist6ef20402019-01-07 16:45:08 -080097 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
Ed Tanous2049bd22022-07-09 07:20:26 -0700101std::string IpmbSensor::getSubTypeUnits(void) const
Zev Weiss6b6891c2021-04-22 02:46:21 -0500102{
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();
Andrei Kartashev39287412022-02-04 16:04:47 +0300123 setInitialProperties(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
Vikash Chandola1f847972022-09-28 09:47:32 +0000131static void initCmdCb(const std::weak_ptr<IpmbSensor>& weakRef,
132 const boost::system::error_code& ec,
133 const IpmbMethodType& response)
134{
135 std::shared_ptr<IpmbSensor> self = weakRef.lock();
136 if (!self)
137 {
138 return;
139 }
140 const int& status = std::get<0>(response);
141 if (ec || (status != 0))
142 {
143 std::cerr << "Error setting init command for device: " << self->name
144 << "\n";
145 }
146}
147
James Feistf7e2c5d2019-02-13 17:27:51 -0800148void IpmbSensor::runInitCmd()
149{
Vikash Chandola1f847972022-09-28 09:47:32 +0000150 if (!initCommand)
James Feistf7e2c5d2019-02-13 17:27:51 -0800151 {
Vikash Chandola1f847972022-09-28 09:47:32 +0000152 return;
James Feist6ef20402019-01-07 16:45:08 -0800153 }
Vikash Chandola1f847972022-09-28 09:47:32 +0000154 dbusConnection->async_method_call(
155 [weakRef{weak_from_this()}](const boost::system::error_code& ec,
156 const IpmbMethodType& response) {
157 initCmdCb(weakRef, ec, response);
158 },
159 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
160 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
161 "sendRequest", commandAddress, netfn, lun, *initCommand, initData);
James Feist6ef20402019-01-07 16:45:08 -0800162}
163
164void IpmbSensor::loadDefaults()
165{
166 if (type == IpmbType::meSensor)
167 {
168 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200169 netfn = ipmi::sensor::netFn;
170 command = ipmi::sensor::getSensorReading;
James Feist6ef20402019-01-07 16:45:08 -0800171 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700172 readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800173 }
174 else if (type == IpmbType::PXE1410CVR)
175 {
176 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200177 netfn = ipmi::me_bridge::netFn;
178 command = ipmi::me_bridge::sendRawPmbus;
179 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700180 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000181 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
182 deviceAddress, 0x00, 0x00, 0x00, 0x00,
183 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700184 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000185 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
186 deviceAddress, 0x00, 0x00, 0x00, 0x00,
187 0x02, 0x00, 0x00, 0x00};
Jayashree-D37322572021-03-19 17:40:56 +0530188 readingFormat = ReadingFormat::linearElevenBit;
James Feist6ef20402019-01-07 16:45:08 -0800189 }
190 else if (type == IpmbType::IR38363VR)
191 {
192 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200193 netfn = ipmi::me_bridge::netFn;
194 command = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700195 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000196 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
197 deviceAddress, 00, 0x00, 0x00, 0x00,
198 0x01, 0x02, 0x8D};
James Feistd7ae29a2020-06-25 15:42:46 -0700199 readingFormat = ReadingFormat::elevenBitShift;
James Feist6ef20402019-01-07 16:45:08 -0800200 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700201 else if (type == IpmbType::ADM1278HSC)
202 {
203 commandAddress = meAddress;
Ed Tanousa771f6a2022-01-14 09:36:51 -0800204 uint8_t snsNum = 0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700205 switch (subType)
206 {
207 case IpmbSubType::temp:
208 case IpmbSubType::curr:
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700209 if (subType == IpmbSubType::temp)
Ed Tanous8a57ec02020-10-09 12:46:52 -0700210 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700211 snsNum = 0x8d;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700212 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700213 else
Ed Tanous8a57ec02020-10-09 12:46:52 -0700214 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700215 snsNum = 0x8c;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700216 }
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200217 netfn = ipmi::me_bridge::netFn;
218 command = ipmi::me_bridge::sendRawPmbus;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700219 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress,
220 0x00, 0x00, 0x01, 0x02, snsNum};
James Feistd7ae29a2020-06-25 15:42:46 -0700221 readingFormat = ReadingFormat::elevenBit;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700222 break;
223 case IpmbSubType::power:
224 case IpmbSubType::volt:
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200225 netfn = ipmi::sensor::netFn;
226 command = ipmi::sensor::getSensorReading;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700227 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700228 readingFormat = ReadingFormat::byte0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700229 break;
230 default:
231 throw std::runtime_error("Invalid sensor type");
232 }
233 }
James Feist6ef20402019-01-07 16:45:08 -0800234 else if (type == IpmbType::mpsVR)
235 {
236 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200237 netfn = ipmi::me_bridge::netFn;
238 command = ipmi::me_bridge::sendRawPmbus;
239 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700240 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000241 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
242 deviceAddress, 0x00, 0x00, 0x00, 0x00,
243 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700244 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000245 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
246 deviceAddress, 0x00, 0x00, 0x00, 0x00,
247 0x02, 0x00, 0x00, 0x00};
James Feistd7ae29a2020-06-25 15:42:46 -0700248 readingFormat = ReadingFormat::byte3;
James Feist6ef20402019-01-07 16:45:08 -0800249 }
250 else
251 {
252 throw std::runtime_error("Invalid sensor type");
253 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200254
255 if (subType == IpmbSubType::util)
256 {
257 // Utilization need to be scaled to percent
258 maxValue = 100;
259 minValue = 0;
260 }
James Feist6ef20402019-01-07 16:45:08 -0800261}
262
263void IpmbSensor::checkThresholds(void)
264{
James Feist6ef20402019-01-07 16:45:08 -0800265 thresholds::checkThresholds(this);
266}
267
James Feist961bf092020-07-01 16:38:12 -0700268bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp)
James Feistd7ae29a2020-06-25 15:42:46 -0700269{
James Feistd7ae29a2020-06-25 15:42:46 -0700270 switch (readingFormat)
271 {
272 case (ReadingFormat::byte0):
James Feiste4a970d2020-08-19 11:21:58 -0700273 {
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200274 if (command == ipmi::sensor::getSensorReading &&
275 !ipmi::sensor::isValid(data))
James Feistcf4238e2020-07-28 16:40:03 -0700276 {
277 return false;
278 }
James Feist961bf092020-07-01 16:38:12 -0700279 resp = data[0];
280 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700281 }
James Feistd7ae29a2020-06-25 15:42:46 -0700282 case (ReadingFormat::byte3):
James Feiste4a970d2020-08-19 11:21:58 -0700283 {
James Feistd7ae29a2020-06-25 15:42:46 -0700284 if (data.size() < 4)
285 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700286 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700287 {
288 std::cerr << "Invalid data length returned for " << name
289 << "\n";
290 }
291 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700292 }
James Feist961bf092020-07-01 16:38:12 -0700293 resp = data[3];
294 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700295 }
James Feistd7ae29a2020-06-25 15:42:46 -0700296 case (ReadingFormat::elevenBit):
James Feiste4a970d2020-08-19 11:21:58 -0700297 {
James Feistd7ae29a2020-06-25 15:42:46 -0700298 if (data.size() < 5)
299 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700300 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700301 {
302 std::cerr << "Invalid data length returned for " << name
303 << "\n";
304 }
305 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700306 }
307
James Feiste4a970d2020-08-19 11:21:58 -0700308 int16_t value = ((data[4] << 8) | data[3]);
James Feiste4a970d2020-08-19 11:21:58 -0700309 resp = value;
James Feist961bf092020-07-01 16:38:12 -0700310 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700311 }
James Feistd7ae29a2020-06-25 15:42:46 -0700312 case (ReadingFormat::elevenBitShift):
James Feiste4a970d2020-08-19 11:21:58 -0700313 {
James Feistd7ae29a2020-06-25 15:42:46 -0700314 if (data.size() < 5)
315 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700316 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700317 {
318 std::cerr << "Invalid data length returned for " << name
319 << "\n";
320 }
321 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700322 }
323
James Feist961bf092020-07-01 16:38:12 -0700324 resp = ((data[4] << 8) | data[3]) >> 3;
325 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700326 }
Jayashree-D37322572021-03-19 17:40:56 +0530327 case (ReadingFormat::linearElevenBit):
328 {
329 if (data.size() < 5)
330 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700331 if (errCount == 0U)
Jayashree-D37322572021-03-19 17:40:56 +0530332 {
333 std::cerr << "Invalid data length returned for " << name
334 << "\n";
335 }
336 return false;
337 }
338
339 int16_t value = ((data[4] << 8) | data[3]);
340 constexpr const size_t shift = 16 - 11; // 11bit into 16bit
341 value <<= shift;
342 value >>= shift;
343 resp = value;
344 return true;
345 }
James Feistd7ae29a2020-06-25 15:42:46 -0700346 default:
347 throw std::runtime_error("Invalid reading type");
348 }
349}
350
Vikash Chandola1f847972022-09-28 09:47:32 +0000351void IpmbSensor::ipmbRequestCompletionCb(const boost::system::error_code& ec,
352 const IpmbMethodType& response)
353{
354 const int& status = std::get<0>(response);
355 if (ec || (status != 0))
356 {
357 incrementError();
358 read();
359 return;
360 }
361 const std::vector<uint8_t>& data = std::get<5>(response);
362 if constexpr (debug)
363 {
364 std::cout << name << ": ";
365 for (size_t d : data)
366 {
367 std::cout << d << " ";
368 }
369 std::cout << "\n";
370 }
371 if (data.empty())
372 {
373 incrementError();
374 read();
375 return;
376 }
377
378 double value = 0;
379
380 if (!processReading(data, value))
381 {
382 incrementError();
383 read();
384 return;
385 }
386
387 // rawValue only used in debug logging
388 // up to 5th byte in data are used to derive value
389 size_t end = std::min(sizeof(uint64_t), data.size());
390 uint64_t rawData = 0;
391 for (size_t i = 0; i < end; i++)
392 {
393 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
394 reinterpret_cast<uint8_t*>(&rawData)[i] = data[i];
395 }
396 rawValue = static_cast<double>(rawData);
397
398 /* Adjust value as per scale and offset */
399 value = (value * scaleVal) + offsetVal;
400 updateValue(value);
401 read();
402}
403
James Feist6ef20402019-01-07 16:45:08 -0800404void IpmbSensor::read(void)
405{
Ed Tanous83db50c2023-03-01 10:20:24 -0800406 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Vikash Chandola1f847972022-09-28 09:47:32 +0000407 waitTimer.async_wait(
408 [weakRef{weak_from_this()}](const boost::system::error_code& ec) {
James Feist6ef20402019-01-07 16:45:08 -0800409 if (ec == boost::asio::error::operation_aborted)
410 {
411 return; // we're being canceled
412 }
Vikash Chandola1f847972022-09-28 09:47:32 +0000413 std::shared_ptr<IpmbSensor> self = weakRef.lock();
414 if (!self)
James Feist6ef20402019-01-07 16:45:08 -0800415 {
James Feist6ef20402019-01-07 16:45:08 -0800416 return;
417 }
Vikash Chandola1f847972022-09-28 09:47:32 +0000418 self->sendIpmbRequest();
James Feist6ef20402019-01-07 16:45:08 -0800419 });
420}
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530421
Vikash Chandola1f847972022-09-28 09:47:32 +0000422void IpmbSensor::sendIpmbRequest()
423{
424 if (!readingStateGood())
425 {
426 updateValue(std::numeric_limits<double>::quiet_NaN());
427 read();
428 return;
429 }
430 dbusConnection->async_method_call(
431 [weakRef{weak_from_this()}](boost::system::error_code ec,
432 const IpmbMethodType& response) {
433 std::shared_ptr<IpmbSensor> self = weakRef.lock();
434 if (!self)
435 {
436 return;
437 }
438 self->ipmbRequestCompletionCb(ec, response);
439 },
440 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
441 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
442 "sendRequest", commandAddress, netfn, lun, command, commandData);
443}
444
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530445bool IpmbSensor::sensorClassType(const std::string& sensorClass)
446{
447 if (sensorClass == "PxeBridgeTemp")
448 {
449 type = IpmbType::PXE1410CVR;
450 }
451 else if (sensorClass == "IRBridgeTemp")
452 {
453 type = IpmbType::IR38363VR;
454 }
455 else if (sensorClass == "HSCBridge")
456 {
457 type = IpmbType::ADM1278HSC;
458 }
459 else if (sensorClass == "MpsBridgeTemp")
460 {
461 type = IpmbType::mpsVR;
462 }
463 else if (sensorClass == "METemp" || sensorClass == "MESensor")
464 {
465 type = IpmbType::meSensor;
466 }
467 else
468 {
469 std::cerr << "Invalid class " << sensorClass << "\n";
470 return false;
471 }
472 return true;
473}
474
475void IpmbSensor::sensorSubType(const std::string& sensorTypeName)
476{
477 if (sensorTypeName == "voltage")
478 {
479 subType = IpmbSubType::volt;
480 }
481 else if (sensorTypeName == "power")
482 {
483 subType = IpmbSubType::power;
484 }
485 else if (sensorTypeName == "current")
486 {
487 subType = IpmbSubType::curr;
488 }
489 else if (sensorTypeName == "utilization")
490 {
491 subType = IpmbSubType::util;
492 }
493 else
494 {
495 subType = IpmbSubType::temp;
496 }
497}
498
499void IpmbSensor::parseConfigValues(const SensorBaseConfigMap& entry)
500{
501 auto findScaleVal = entry.find("ScaleValue");
502 if (findScaleVal != entry.end())
503 {
504 scaleVal = std::visit(VariantToDoubleVisitor(), findScaleVal->second);
505 }
506
507 auto findOffsetVal = entry.find("OffsetValue");
508 if (findOffsetVal != entry.end())
509 {
510 offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second);
511 }
512
Zev Weissa4d27682022-07-19 15:30:36 -0700513 readState = getPowerState(entry);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530514}
515
James Feist6ef20402019-01-07 16:45:08 -0800516void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -0800517 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Vikash Chandola1f847972022-09-28 09:47:32 +0000518 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
James Feist6ef20402019-01-07 16:45:08 -0800519 sensors,
520 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
521{
522 if (!dbusConnection)
523 {
524 std::cerr << "Connection not created\n";
525 return;
526 }
527 dbusConnection->async_method_call(
528 [&](boost::system::error_code ec, const ManagedObjectType& resp) {
Ed Tanousbb679322022-05-16 16:10:00 -0700529 if (ec)
530 {
531 std::cerr << "Error contacting entity manager\n";
532 return;
533 }
Zev Weiss8ba551b2022-08-12 18:21:02 -0700534 for (const auto& [path, interfaces] : resp)
Ed Tanousbb679322022-05-16 16:10:00 -0700535 {
Zev Weiss8ba551b2022-08-12 18:21:02 -0700536 for (const auto& [intf, cfg] : interfaces)
James Feist6ef20402019-01-07 16:45:08 -0800537 {
Zev Weiss054aad82022-08-18 01:37:34 -0700538 if (intf != configInterfaceName(sensorType))
James Feist6ef20402019-01-07 16:45:08 -0800539 {
Ed Tanousbb679322022-05-16 16:10:00 -0700540 continue;
James Feist6ef20402019-01-07 16:45:08 -0800541 }
Zev Weiss8ba551b2022-08-12 18:21:02 -0700542 std::string name = loadVariant<std::string>(cfg, "Name");
Ed Tanousbb679322022-05-16 16:10:00 -0700543
544 std::vector<thresholds::Threshold> sensorThresholds;
Zev Weiss8ba551b2022-08-12 18:21:02 -0700545 if (!parseThresholdsFromConfig(interfaces, sensorThresholds))
Ed Tanousbb679322022-05-16 16:10:00 -0700546 {
547 std::cerr << "error populating thresholds for " << name
548 << "\n";
549 }
Zev Weiss8ba551b2022-08-12 18:21:02 -0700550 uint8_t deviceAddress = loadVariant<uint8_t>(cfg, "Address");
Ed Tanousbb679322022-05-16 16:10:00 -0700551
Patrick Williams779c96a2023-05-10 07:50:42 -0500552 std::string sensorClass = loadVariant<std::string>(cfg,
553 "Class");
Ed Tanousbb679322022-05-16 16:10:00 -0700554
555 uint8_t hostSMbusIndex = hostSMbusIndexDefault;
Zev Weiss8ba551b2022-08-12 18:21:02 -0700556 auto findSmType = cfg.find("HostSMbusIndex");
557 if (findSmType != cfg.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700558 {
559 hostSMbusIndex = std::visit(VariantToUnsignedIntVisitor(),
560 findSmType->second);
561 }
562
Zev Weiss8569bf22022-10-11 15:37:44 -0700563 float pollRate = getPollRate(cfg, pollRateDefault);
Ed Tanousbb679322022-05-16 16:10:00 -0700564
Jayashree Dhanapal6ee62942021-12-14 15:22:23 +0530565 uint8_t ipmbBusIndex = ipmbBusIndexDefault;
566 auto findBusType = cfg.find("Bus");
567 if (findBusType != cfg.end())
568 {
569 ipmbBusIndex = std::visit(VariantToUnsignedIntVisitor(),
570 findBusType->second);
571 std::cerr << "Ipmb Bus Index for " << name << " is "
572 << static_cast<int>(ipmbBusIndex) << "\n";
573 }
574
Ed Tanousbb679322022-05-16 16:10:00 -0700575 /* Default sensor type is "temperature" */
576 std::string sensorTypeName = "temperature";
Zev Weiss8ba551b2022-08-12 18:21:02 -0700577 auto findType = cfg.find("SensorType");
578 if (findType != cfg.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700579 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500580 sensorTypeName = std::visit(VariantToStringVisitor(),
581 findType->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700582 }
583
584 auto& sensor = sensors[name];
Vikash Chandola1f847972022-09-28 09:47:32 +0000585 sensor = nullptr;
586 sensor = std::make_shared<IpmbSensor>(
Zev Weiss8ba551b2022-08-12 18:21:02 -0700587 dbusConnection, io, name, path, objectServer,
Ed Tanousbb679322022-05-16 16:10:00 -0700588 std::move(sensorThresholds), deviceAddress, hostSMbusIndex,
589 pollRate, sensorTypeName);
590
Zev Weiss8ba551b2022-08-12 18:21:02 -0700591 sensor->parseConfigValues(cfg);
Ed Tanousbb679322022-05-16 16:10:00 -0700592 if (!(sensor->sensorClassType(sensorClass)))
593 {
594 continue;
595 }
596 sensor->sensorSubType(sensorTypeName);
597 sensor->init();
James Feist6ef20402019-01-07 16:45:08 -0800598 }
Ed Tanousbb679322022-05-16 16:10:00 -0700599 }
James Feist6ef20402019-01-07 16:45:08 -0800600 },
JeffLin2c5a1f22022-10-05 15:19:09 +0800601 entityManagerName, "/xyz/openbmc_project/inventory",
602 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist6ef20402019-01-07 16:45:08 -0800603}
604
Jayashree Dhanapal3746c552022-03-21 14:45:52 +0530605void sdrHandler(sdbusplus::message_t& message,
606 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
607{
608 std::string objectName;
609 SensorBaseConfigMap values;
610 message.read(objectName, values);
611
612 auto findBus = values.find("Bus");
613 if (findBus == values.end())
614 {
615 return;
616 }
617
618 uint8_t busIndex = loadVariant<uint8_t>(values, "Bus");
619
620 auto& sdrsen = sdrsensor[busIndex];
621 sdrsen = nullptr;
622 sdrsen = std::make_shared<IpmbSDRDevice>(dbusConnection, busIndex);
623 sdrsen->getSDRRepositoryInfo();
624}
625
Patrick Williams92f8f512022-07-22 19:26:55 -0500626void reinitSensors(sdbusplus::message_t& message)
James Feistf7e2c5d2019-02-13 17:27:51 -0800627{
James Feist0d4f2bd2019-03-05 13:15:40 -0800628 constexpr const size_t reinitWaitSeconds = 2;
James Feistf7e2c5d2019-02-13 17:27:51 -0800629 std::string objectName;
James Feist52497fd2019-06-07 13:01:33 -0700630 boost::container::flat_map<std::string, std::variant<std::string>> values;
James Feistf7e2c5d2019-02-13 17:27:51 -0800631 message.read(objectName, values);
James Feist0d4f2bd2019-03-05 13:15:40 -0800632
James Feist52497fd2019-06-07 13:01:33 -0700633 auto findStatus = values.find(power::property);
634 if (findStatus != values.end())
James Feistf7e2c5d2019-02-13 17:27:51 -0800635 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700636 bool powerStatus =
637 std::get<std::string>(findStatus->second).ends_with(".Running");
James Feistf7e2c5d2019-02-13 17:27:51 -0800638 if (powerStatus)
639 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800640 if (!initCmdTimer)
James Feistf7e2c5d2019-02-13 17:27:51 -0800641 {
James Feist0d4f2bd2019-03-05 13:15:40 -0800642 // this should be impossible
643 return;
James Feistf7e2c5d2019-02-13 17:27:51 -0800644 }
James Feist0d4f2bd2019-03-05 13:15:40 -0800645 // we seem to send this command too fast sometimes, wait before
646 // sending
Ed Tanous83db50c2023-03-01 10:20:24 -0800647 initCmdTimer->expires_after(
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700648 std::chrono::seconds(reinitWaitSeconds));
James Feist0d4f2bd2019-03-05 13:15:40 -0800649
650 initCmdTimer->async_wait([](const boost::system::error_code ec) {
651 if (ec == boost::asio::error::operation_aborted)
652 {
653 return; // we're being canceled
654 }
655
Zev Weiss8ba551b2022-08-12 18:21:02 -0700656 for (const auto& [name, sensor] : sensors)
James Feist0d4f2bd2019-03-05 13:15:40 -0800657 {
Zev Weiss8ba551b2022-08-12 18:21:02 -0700658 if (sensor)
James Feist0d4f2bd2019-03-05 13:15:40 -0800659 {
Zev Weiss8ba551b2022-08-12 18:21:02 -0700660 sensor->runInitCmd();
James Feist0d4f2bd2019-03-05 13:15:40 -0800661 }
662 }
663 });
James Feistf7e2c5d2019-02-13 17:27:51 -0800664 }
665 }
666}
667
Kumar Thangavel5d032bc2022-11-30 20:24:47 +0530668void interfaceRemoved(
669 sdbusplus::message_t& message,
670 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
671 sensors)
672{
673 if (message.is_method_error())
674 {
675 std::cerr << "interfacesRemoved callback method error\n";
676 return;
677 }
678
679 sdbusplus::message::object_path removedPath;
680 std::vector<std::string> interfaces;
681
682 message.read(removedPath, interfaces);
683
684 // If the xyz.openbmc_project.Confguration.X interface was removed
685 // for one or more sensors, delete those sensor objects.
686 auto sensorIt = sensors.begin();
687 while (sensorIt != sensors.end())
688 {
689 if ((sensorIt->second->configurationPath == removedPath) &&
690 (std::find(interfaces.begin(), interfaces.end(),
691 configInterfaceName(sdrInterface)) != interfaces.end()))
692 {
693 sensorIt = sensors.erase(sensorIt);
694 }
695 else
696 {
697 sensorIt++;
698 }
699 }
700}
701
James Feistb6c0b912019-07-09 12:21:44 -0700702int main()
James Feist6ef20402019-01-07 16:45:08 -0800703{
Ed Tanous1f978632023-02-28 18:16:39 -0800704 boost::asio::io_context io;
James Feist6ef20402019-01-07 16:45:08 -0800705 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700706 sdbusplus::asio::object_server objectServer(systemBus, true);
707 objectServer.add_manager("/xyz/openbmc_project/sensors");
James Feist6ef20402019-01-07 16:45:08 -0800708 systemBus->request_name("xyz.openbmc_project.IpmbSensor");
James Feist6ef20402019-01-07 16:45:08 -0800709
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700710 initCmdTimer = std::make_unique<boost::asio::steady_timer>(io);
James Feist0d4f2bd2019-03-05 13:15:40 -0800711
Ed Tanous83db50c2023-03-01 10:20:24 -0800712 boost::asio::post(
713 io, [&]() { createSensors(io, objectServer, sensors, systemBus); });
James Feist6ef20402019-01-07 16:45:08 -0800714
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700715 boost::asio::steady_timer configTimer(io);
James Feist6ef20402019-01-07 16:45:08 -0800716
Patrick Williams92f8f512022-07-22 19:26:55 -0500717 std::function<void(sdbusplus::message_t&)> eventHandler =
718 [&](sdbusplus::message_t&) {
Ed Tanous83db50c2023-03-01 10:20:24 -0800719 configTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700720 // create a timer because normally multiple properties change
721 configTimer.async_wait([&](const boost::system::error_code& ec) {
722 if (ec == boost::asio::error::operation_aborted)
723 {
724 return; // we're being canceled
725 }
726 createSensors(io, objectServer, sensors, systemBus);
727 if (sensors.empty())
728 {
729 std::cout << "Configuration not detected\n";
730 }
731 });
732 };
James Feist6ef20402019-01-07 16:45:08 -0800733
Zev Weiss214d9712022-08-12 12:54:31 -0700734 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
735 setupPropertiesChangedMatches(
Zev Weiss054aad82022-08-18 01:37:34 -0700736 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
James Feist6ef20402019-01-07 16:45:08 -0800737
Patrick Williams92f8f512022-07-22 19:26:55 -0500738 sdbusplus::bus::match_t powerChangeMatch(
739 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist52497fd2019-06-07 13:01:33 -0700740 "type='signal',interface='" + std::string(properties::interface) +
741 "',path='" + std::string(power::path) + "',arg0='" +
742 std::string(power::interface) + "'",
James Feistf7e2c5d2019-02-13 17:27:51 -0800743 reinitSensors);
744
Jayashree Dhanapal3746c552022-03-21 14:45:52 +0530745 auto matchSignal = std::make_shared<sdbusplus::bus::match_t>(
746 static_cast<sdbusplus::bus_t&>(*systemBus),
747 "type='signal',member='PropertiesChanged',path_namespace='" +
748 std::string(inventoryPath) + "',arg0namespace='" +
749 configInterfaceName(sdrInterface) + "'",
750 [&systemBus](sdbusplus::message_t& msg) {
751 sdrHandler(msg, systemBus);
752 });
753
Kumar Thangavel5d032bc2022-11-30 20:24:47 +0530754 // Watch for entity-manager to remove configuration interfaces
755 // so the corresponding sensors can be removed.
756 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
757 static_cast<sdbusplus::bus_t&>(*systemBus),
758 "type='signal',member='InterfacesRemoved',arg0path='" +
759 std::string(inventoryPath) + "/'",
760 [](sdbusplus::message_t& msg) {
761 std::cerr << " InterfacesRemoved called test.." << std::endl;
762 interfaceRemoved(msg, sensors);
763 });
764
Bruce Lee1263c3d2021-06-04 15:16:33 +0800765 setupManufacturingModeMatch(*systemBus);
James Feist6ef20402019-01-07 16:45:08 -0800766 io.run();
Zhikui Rene76a5a62021-07-09 15:16:32 -0700767 return 0;
James Feist6ef20402019-01-07 16:45:08 -0800768}