blob: 30531bf3ff7ed4a472ca5a448dfd38422dd90cca [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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "SensorPaths.hpp"
21#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103022#include "Utils.hpp"
23#include "VariantVisitors.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include "sensor.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103025
Ed Tanouseacbfdd2024-04-04 12:00:24 -070026#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/steady_timer.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070032#include <sdbusplus/message.hpp>
33#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <algorithm>
36#include <array>
James Feist6ef20402019-01-07 16:45:08 -080037#include <chrono>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <cstddef>
39#include <cstdint>
James Feist6ef20402019-01-07 16:45:08 -080040#include <iostream>
41#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <memory>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070043#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070044#include <string>
45#include <tuple>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070046#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070047#include <variant>
James Feist6ef20402019-01-07 16:45:08 -080048#include <vector>
49
50constexpr const bool debug = false;
51
James Feist6ef20402019-01-07 16:45:08 -080052static constexpr double ipmbMaxReading = 0xFF;
53static constexpr double ipmbMinReading = 0;
54
55static constexpr uint8_t meAddress = 1;
56static constexpr uint8_t lun = 0;
Anoop S832a2c62020-11-20 19:21:22 +000057static constexpr uint8_t hostSMbusIndexDefault = 0x03;
Jayashree Dhanapal6ee62942021-12-14 15:22:23 +053058static constexpr uint8_t ipmbBusIndexDefault = 0;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053059static constexpr float pollRateDefault = 1; // in seconds
James Feist6ef20402019-01-07 16:45:08 -080060
Vijay Khemka682a5cb2019-07-18 17:34:03 -070061static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/";
62
James Feist6ef20402019-01-07 16:45:08 -080063IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080064 boost::asio::io_context& io,
James Feist6ef20402019-01-07 16:45:08 -080065 const std::string& sensorName,
66 const std::string& sensorConfiguration,
67 sdbusplus::asio::object_server& objectServer,
68 std::vector<thresholds::Threshold>&& thresholdData,
Anoop S832a2c62020-11-20 19:21:22 +000069 uint8_t deviceAddress, uint8_t hostSMbusIndex,
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053070 const float pollRate, std::string& sensorTypeName) :
Zhikui Renda98f092021-11-01 09:41:08 -070071 Sensor(escapeName(sensorName), std::move(thresholdData),
Zev Weiss054aad82022-08-18 01:37:34 -070072 sensorConfiguration, "IpmbSensor", false, false, ipmbMaxReading,
73 ipmbMinReading, conn, PowerState::on),
Anoop S832a2c62020-11-20 19:21:22 +000074 deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex),
Jayashree-D9f6d4fd2021-04-13 18:27:22 +053075 sensorPollMs(static_cast<int>(pollRate * 1000)), objectServer(objectServer),
76 waitTimer(io)
James Feist6ef20402019-01-07 16:45:08 -080077{
Vijay Khemka682a5cb2019-07-18 17:34:03 -070078 std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name;
79
James Feist6ef20402019-01-07 16:45:08 -080080 sensorInterface = objectServer.add_interface(
Vijay Khemka682a5cb2019-07-18 17:34:03 -070081 dbusPath, "xyz.openbmc_project.Sensor.Value");
James Feist6ef20402019-01-07 16:45:08 -080082
Jayashree Dhanapal56678082022-01-04 17:27:20 +053083 for (const auto& threshold : thresholds)
James Feist6ef20402019-01-07 16:45:08 -080084 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053085 std::string interface = thresholds::getInterface(threshold.level);
86 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
87 objectServer.add_interface(dbusPath, interface);
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();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053095 for (const auto& iface : thresholdInterfaces)
96 {
97 objectServer.remove_interface(iface);
98 }
James Feist6ef20402019-01-07 16:45:08 -080099 objectServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800100 objectServer.remove_interface(association);
James Feist6ef20402019-01-07 16:45:08 -0800101}
102
Ed Tanous201a1012024-04-03 18:07:28 -0700103std::string IpmbSensor::getSubTypeUnits() const
Zev Weiss6b6891c2021-04-22 02:46:21 -0500104{
105 switch (subType)
106 {
107 case IpmbSubType::temp:
108 return sensor_paths::unitDegreesC;
109 case IpmbSubType::curr:
110 return sensor_paths::unitAmperes;
111 case IpmbSubType::power:
112 return sensor_paths::unitWatts;
113 case IpmbSubType::volt:
114 return sensor_paths::unitVolts;
115 case IpmbSubType::util:
116 return sensor_paths::unitPercent;
117 default:
118 throw std::runtime_error("Invalid sensor type");
119 }
120}
121
Ed Tanous201a1012024-04-03 18:07:28 -0700122void IpmbSensor::init()
James Feist6ef20402019-01-07 16:45:08 -0800123{
James Feist6ef20402019-01-07 16:45:08 -0800124 loadDefaults();
Andrei Kartashev39287412022-02-04 16:04:47 +0300125 setInitialProperties(getSubTypeUnits());
James Feist6ef20402019-01-07 16:45:08 -0800126 if (initCommand)
127 {
James Feistf7e2c5d2019-02-13 17:27:51 -0800128 runInitCmd();
129 }
130 read();
131}
132
Vikash Chandola1f847972022-09-28 09:47:32 +0000133static void initCmdCb(const std::weak_ptr<IpmbSensor>& weakRef,
134 const boost::system::error_code& ec,
135 const IpmbMethodType& response)
136{
137 std::shared_ptr<IpmbSensor> self = weakRef.lock();
138 if (!self)
139 {
140 return;
141 }
142 const int& status = std::get<0>(response);
143 if (ec || (status != 0))
144 {
145 std::cerr << "Error setting init command for device: " << self->name
146 << "\n";
147 }
148}
149
James Feistf7e2c5d2019-02-13 17:27:51 -0800150void IpmbSensor::runInitCmd()
151{
Vikash Chandola1f847972022-09-28 09:47:32 +0000152 if (!initCommand)
James Feistf7e2c5d2019-02-13 17:27:51 -0800153 {
Vikash Chandola1f847972022-09-28 09:47:32 +0000154 return;
James Feist6ef20402019-01-07 16:45:08 -0800155 }
Vikash Chandola1f847972022-09-28 09:47:32 +0000156 dbusConnection->async_method_call(
157 [weakRef{weak_from_this()}](const boost::system::error_code& ec,
158 const IpmbMethodType& response) {
159 initCmdCb(weakRef, ec, response);
Patrick Williams597e8422023-10-20 11:19:01 -0500160 },
Vikash Chandola1f847972022-09-28 09:47:32 +0000161 "xyz.openbmc_project.Ipmi.Channel.Ipmb",
162 "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb",
163 "sendRequest", commandAddress, netfn, lun, *initCommand, initData);
James Feist6ef20402019-01-07 16:45:08 -0800164}
165
166void IpmbSensor::loadDefaults()
167{
168 if (type == IpmbType::meSensor)
169 {
170 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200171 netfn = ipmi::sensor::netFn;
172 command = ipmi::sensor::getSensorReading;
James Feist6ef20402019-01-07 16:45:08 -0800173 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700174 readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800175 }
176 else if (type == IpmbType::PXE1410CVR)
177 {
178 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200179 netfn = ipmi::me_bridge::netFn;
180 command = ipmi::me_bridge::sendRawPmbus;
181 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700182 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000183 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
184 deviceAddress, 0x00, 0x00, 0x00, 0x00,
185 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700186 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000187 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
188 deviceAddress, 0x00, 0x00, 0x00, 0x00,
189 0x02, 0x00, 0x00, 0x00};
Jayashree-D37322572021-03-19 17:40:56 +0530190 readingFormat = ReadingFormat::linearElevenBit;
James Feist6ef20402019-01-07 16:45:08 -0800191 }
192 else if (type == IpmbType::IR38363VR)
193 {
194 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200195 netfn = ipmi::me_bridge::netFn;
196 command = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700197 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000198 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
199 deviceAddress, 00, 0x00, 0x00, 0x00,
200 0x01, 0x02, 0x8D};
James Feistd7ae29a2020-06-25 15:42:46 -0700201 readingFormat = ReadingFormat::elevenBitShift;
James Feist6ef20402019-01-07 16:45:08 -0800202 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700203 else if (type == IpmbType::ADM1278HSC)
204 {
205 commandAddress = meAddress;
Ed Tanousa771f6a2022-01-14 09:36:51 -0800206 uint8_t snsNum = 0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700207 switch (subType)
208 {
209 case IpmbSubType::temp:
210 case IpmbSubType::curr:
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700211 if (subType == IpmbSubType::temp)
Ed Tanous8a57ec02020-10-09 12:46:52 -0700212 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700213 snsNum = 0x8d;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700214 }
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700215 else
Ed Tanous8a57ec02020-10-09 12:46:52 -0700216 {
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700217 snsNum = 0x8c;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700218 }
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200219 netfn = ipmi::me_bridge::netFn;
220 command = ipmi::me_bridge::sendRawPmbus;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700221 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress,
222 0x00, 0x00, 0x01, 0x02, snsNum};
James Feistd7ae29a2020-06-25 15:42:46 -0700223 readingFormat = ReadingFormat::elevenBit;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700224 break;
225 case IpmbSubType::power:
226 case IpmbSubType::volt:
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200227 netfn = ipmi::sensor::netFn;
228 command = ipmi::sensor::getSensorReading;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700229 commandData = {deviceAddress};
James Feistd7ae29a2020-06-25 15:42:46 -0700230 readingFormat = ReadingFormat::byte0;
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700231 break;
232 default:
233 throw std::runtime_error("Invalid sensor type");
234 }
235 }
James Feist6ef20402019-01-07 16:45:08 -0800236 else if (type == IpmbType::mpsVR)
237 {
238 commandAddress = meAddress;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200239 netfn = ipmi::me_bridge::netFn;
240 command = ipmi::me_bridge::sendRawPmbus;
241 initCommand = ipmi::me_bridge::sendRawPmbus;
James Feistd7ae29a2020-06-25 15:42:46 -0700242 // pmbus read temp
Anoop S832a2c62020-11-20 19:21:22 +0000243 commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex,
244 deviceAddress, 0x00, 0x00, 0x00, 0x00,
245 0x01, 0x02, 0x8d};
James Feistd7ae29a2020-06-25 15:42:46 -0700246 // goto page 0
Anoop S832a2c62020-11-20 19:21:22 +0000247 initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex,
248 deviceAddress, 0x00, 0x00, 0x00, 0x00,
249 0x02, 0x00, 0x00, 0x00};
James Feistd7ae29a2020-06-25 15:42:46 -0700250 readingFormat = ReadingFormat::byte3;
James Feist6ef20402019-01-07 16:45:08 -0800251 }
252 else
253 {
254 throw std::runtime_error("Invalid sensor type");
255 }
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +0200256
257 if (subType == IpmbSubType::util)
258 {
259 // Utilization need to be scaled to percent
260 maxValue = 100;
261 minValue = 0;
262 }
James Feist6ef20402019-01-07 16:45:08 -0800263}
264
Ed Tanous201a1012024-04-03 18:07:28 -0700265void IpmbSensor::checkThresholds()
James Feist6ef20402019-01-07 16:45:08 -0800266{
James Feist6ef20402019-01-07 16:45:08 -0800267 thresholds::checkThresholds(this);
268}
269
Ed Tanous828c5a62024-02-09 16:59:22 -0800270bool IpmbSensor::processReading(ReadingFormat readingFormat, uint8_t command,
271 const std::vector<uint8_t>& data, double& resp,
272 size_t errCount)
James Feistd7ae29a2020-06-25 15:42:46 -0700273{
James Feistd7ae29a2020-06-25 15:42:46 -0700274 switch (readingFormat)
275 {
276 case (ReadingFormat::byte0):
James Feiste4a970d2020-08-19 11:21:58 -0700277 {
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +0200278 if (command == ipmi::sensor::getSensorReading &&
279 !ipmi::sensor::isValid(data))
James Feistcf4238e2020-07-28 16:40:03 -0700280 {
281 return false;
282 }
James Feist961bf092020-07-01 16:38:12 -0700283 resp = data[0];
284 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700285 }
James Feistd7ae29a2020-06-25 15:42:46 -0700286 case (ReadingFormat::byte3):
James Feiste4a970d2020-08-19 11:21:58 -0700287 {
James Feistd7ae29a2020-06-25 15:42:46 -0700288 if (data.size() < 4)
289 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700290 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700291 {
Ed Tanous828c5a62024-02-09 16:59:22 -0800292 std::cerr << "Invalid data length returned\n";
James Feist961bf092020-07-01 16:38:12 -0700293 }
294 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700295 }
James Feist961bf092020-07-01 16:38:12 -0700296 resp = data[3];
297 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700298 }
James Feistd7ae29a2020-06-25 15:42:46 -0700299 case (ReadingFormat::elevenBit):
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 {
Ed Tanous828c5a62024-02-09 16:59:22 -0800305 std::cerr << "Invalid data length returned\n";
James Feist961bf092020-07-01 16:38:12 -0700306 }
307 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700308 }
309
James Feiste4a970d2020-08-19 11:21:58 -0700310 int16_t value = ((data[4] << 8) | data[3]);
James Feiste4a970d2020-08-19 11:21:58 -0700311 resp = value;
James Feist961bf092020-07-01 16:38:12 -0700312 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700313 }
James Feistd7ae29a2020-06-25 15:42:46 -0700314 case (ReadingFormat::elevenBitShift):
James Feiste4a970d2020-08-19 11:21:58 -0700315 {
James Feistd7ae29a2020-06-25 15:42:46 -0700316 if (data.size() < 5)
317 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700318 if (errCount == 0U)
James Feist961bf092020-07-01 16:38:12 -0700319 {
Ed Tanous828c5a62024-02-09 16:59:22 -0800320 std::cerr << "Invalid data length returned\n";
James Feist961bf092020-07-01 16:38:12 -0700321 }
322 return false;
James Feistd7ae29a2020-06-25 15:42:46 -0700323 }
324
James Feist961bf092020-07-01 16:38:12 -0700325 resp = ((data[4] << 8) | data[3]) >> 3;
326 return true;
James Feiste4a970d2020-08-19 11:21:58 -0700327 }
Jayashree-D37322572021-03-19 17:40:56 +0530328 case (ReadingFormat::linearElevenBit):
329 {
330 if (data.size() < 5)
331 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700332 if (errCount == 0U)
Jayashree-D37322572021-03-19 17:40:56 +0530333 {
Ed Tanous828c5a62024-02-09 16:59:22 -0800334 std::cerr << "Invalid data length returned\n";
Jayashree-D37322572021-03-19 17:40:56 +0530335 }
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
Ed Tanous828c5a62024-02-09 16:59:22 -0800380 if (!processReading(readingFormat, command, data, value, errCount))
Vikash Chandola1f847972022-09-28 09:47:32 +0000381 {
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
Ed Tanous201a1012024-04-03 18:07:28 -0700404void IpmbSensor::read()
James Feist6ef20402019-01-07 16:45:08 -0800405{
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);
Patrick Williams597e8422023-10-20 11:19:01 -0500439 },
Vikash Chandola1f847972022-09-28 09:47:32 +0000440 "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 {
Ed Tanous828c5a62024-02-09 16:59:22 -0800547 std::cerr << "error populating thresholds " << name << "\n";
Ed Tanousbb679322022-05-16 16:10:00 -0700548 }
Zev Weiss8ba551b2022-08-12 18:21:02 -0700549 uint8_t deviceAddress = loadVariant<uint8_t>(cfg, "Address");
Ed Tanousbb679322022-05-16 16:10:00 -0700550
Patrick Williams779c96a2023-05-10 07:50:42 -0500551 std::string sensorClass = loadVariant<std::string>(cfg,
552 "Class");
Ed Tanousbb679322022-05-16 16:10:00 -0700553
554 uint8_t hostSMbusIndex = hostSMbusIndexDefault;
Zev Weiss8ba551b2022-08-12 18:21:02 -0700555 auto findSmType = cfg.find("HostSMbusIndex");
556 if (findSmType != cfg.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700557 {
558 hostSMbusIndex = std::visit(VariantToUnsignedIntVisitor(),
559 findSmType->second);
560 }
561
Zev Weiss8569bf22022-10-11 15:37:44 -0700562 float pollRate = getPollRate(cfg, pollRateDefault);
Ed Tanousbb679322022-05-16 16:10:00 -0700563
Jayashree Dhanapal6ee62942021-12-14 15:22:23 +0530564 uint8_t ipmbBusIndex = ipmbBusIndexDefault;
565 auto findBusType = cfg.find("Bus");
566 if (findBusType != cfg.end())
567 {
568 ipmbBusIndex = std::visit(VariantToUnsignedIntVisitor(),
569 findBusType->second);
570 std::cerr << "Ipmb Bus Index for " << name << " is "
571 << static_cast<int>(ipmbBusIndex) << "\n";
572 }
573
Ed Tanousbb679322022-05-16 16:10:00 -0700574 /* Default sensor type is "temperature" */
575 std::string sensorTypeName = "temperature";
Zev Weiss8ba551b2022-08-12 18:21:02 -0700576 auto findType = cfg.find("SensorType");
577 if (findType != cfg.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700578 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500579 sensorTypeName = std::visit(VariantToStringVisitor(),
580 findType->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700581 }
582
583 auto& sensor = sensors[name];
Vikash Chandola1f847972022-09-28 09:47:32 +0000584 sensor = nullptr;
585 sensor = std::make_shared<IpmbSensor>(
Zev Weiss8ba551b2022-08-12 18:21:02 -0700586 dbusConnection, io, name, path, objectServer,
Ed Tanousbb679322022-05-16 16:10:00 -0700587 std::move(sensorThresholds), deviceAddress, hostSMbusIndex,
588 pollRate, sensorTypeName);
589
Zev Weiss8ba551b2022-08-12 18:21:02 -0700590 sensor->parseConfigValues(cfg);
Ed Tanousbb679322022-05-16 16:10:00 -0700591 if (!(sensor->sensorClassType(sensorClass)))
592 {
593 continue;
594 }
595 sensor->sensorSubType(sensorTypeName);
596 sensor->init();
James Feist6ef20402019-01-07 16:45:08 -0800597 }
Ed Tanousbb679322022-05-16 16:10:00 -0700598 }
Patrick Williams597e8422023-10-20 11:19:01 -0500599 },
JeffLin2c5a1f22022-10-05 15:19:09 +0800600 entityManagerName, "/xyz/openbmc_project/inventory",
601 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist6ef20402019-01-07 16:45:08 -0800602}
603
Kumar Thangavel5d032bc2022-11-30 20:24:47 +0530604void interfaceRemoved(
605 sdbusplus::message_t& message,
606 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
607 sensors)
608{
609 if (message.is_method_error())
610 {
611 std::cerr << "interfacesRemoved callback method error\n";
612 return;
613 }
614
615 sdbusplus::message::object_path removedPath;
616 std::vector<std::string> interfaces;
617
618 message.read(removedPath, interfaces);
619
620 // If the xyz.openbmc_project.Confguration.X interface was removed
621 // for one or more sensors, delete those sensor objects.
622 auto sensorIt = sensors.begin();
623 while (sensorIt != sensors.end())
624 {
625 if ((sensorIt->second->configurationPath == removedPath) &&
626 (std::find(interfaces.begin(), interfaces.end(),
627 configInterfaceName(sdrInterface)) != interfaces.end()))
628 {
629 sensorIt = sensors.erase(sensorIt);
630 }
631 else
632 {
633 sensorIt++;
634 }
635 }
636}