blob: 953e44a20ad531c4b7125a927fb5448824af6319 [file] [log] [blame]
James Feist6ef20402019-01-07 16:45:08 -08001#pragma once
Ed Tanous9b4a20e2022-09-06 08:47:11 -07002#include <boost/asio/steady_timer.hpp>
James Feist6ef20402019-01-07 16:45:08 -08003#include <boost/container/flat_map.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -07004#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -07005
James Feist6ef20402019-01-07 16:45:08 -08006#include <chrono>
7#include <limits>
Patrick Venturefd6ba732019-10-31 14:27:39 -07008#include <memory>
9#include <optional>
10#include <string>
James Feist6ef20402019-01-07 16:45:08 -080011#include <vector>
12
Ed Tanous828c5a62024-02-09 16:59:22 -080013constexpr const char* sensorType = "IpmbSensor";
14constexpr const char* sdrInterface = "IpmbDevice";
15
James Feist6ef20402019-01-07 16:45:08 -080016enum class IpmbType
17{
Ed Tanousa771f6a2022-01-14 09:36:51 -080018 none,
James Feist6ef20402019-01-07 16:45:08 -080019 meSensor,
20 PXE1410CVR,
21 IR38363VR,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070022 ADM1278HSC,
Rebecca Cran394f0c52023-12-17 20:08:55 -070023 mpsVR,
24 SMPro
James Feist6ef20402019-01-07 16:45:08 -080025};
26
Vijay Khemka682a5cb2019-07-18 17:34:03 -070027enum class IpmbSubType
28{
Ed Tanousa771f6a2022-01-14 09:36:51 -080029 none,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070030 temp,
31 curr,
32 power,
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +020033 volt,
34 util
Vijay Khemka682a5cb2019-07-18 17:34:03 -070035};
36
James Feistd7ae29a2020-06-25 15:42:46 -070037enum class ReadingFormat
38{
39 byte0,
40 byte3,
Rebecca Cran394f0c52023-12-17 20:08:55 -070041 nineBit,
42 tenBit,
James Feistd7ae29a2020-06-25 15:42:46 -070043 elevenBit,
44 elevenBitShift,
Rebecca Cran394f0c52023-12-17 20:08:55 -070045 linearElevenBit,
46 fifteenBit
James Feistd7ae29a2020-06-25 15:42:46 -070047};
48
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020049namespace ipmi
50{
51namespace sensor
52{
53constexpr uint8_t netFn = 0x04;
54constexpr uint8_t getSensorReading = 0x2d;
55
Patrick Williamsd02ad492023-05-08 09:15:21 -050056static inline bool isValid(const std::vector<uint8_t>& data)
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020057{
Ed Tanous8a57ec02020-10-09 12:46:52 -070058 constexpr auto readingUnavailableBit = 5;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020059
60 // Proper 'Get Sensor Reading' response has at least 4 bytes, including
61 // Completion Code. Our IPMB stack strips Completion Code from payload so we
62 // compare here against the rest of payload
63 if (data.size() < 3)
64 {
65 return false;
66 }
67
68 // Per IPMI 'Get Sensor Reading' specification
Ed Tanous2049bd22022-07-09 07:20:26 -070069 if ((data[1] & (1 << readingUnavailableBit)) != 0)
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020070 {
71 return false;
72 }
73
74 return true;
75}
76
77} // namespace sensor
78namespace me_bridge
79{
80constexpr uint8_t netFn = 0x2e;
81constexpr uint8_t sendRawPmbus = 0xd9;
82} // namespace me_bridge
83} // namespace ipmi
84
Vikash Chandola1f847972022-09-28 09:47:32 +000085using IpmbMethodType =
86 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
87
88struct IpmbSensor :
89 public Sensor,
90 public std::enable_shared_from_this<IpmbSensor>
James Feist6ef20402019-01-07 16:45:08 -080091{
James Feistd8705872019-02-08 13:26:09 -080092 IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080093 boost::asio::io_context& io, const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080094 const std::string& sensorConfiguration,
95 sdbusplus::asio::object_server& objectServer,
Ed Tanous2049bd22022-07-09 07:20:26 -070096 std::vector<thresholds::Threshold>&& thresholdData,
97 uint8_t deviceAddress, uint8_t hostSMbusIndex, float pollRate,
98 std::string& sensorTypeName);
Ed Tanous8a57ec02020-10-09 12:46:52 -070099 ~IpmbSensor() override;
James Feist6ef20402019-01-07 16:45:08 -0800100
Ed Tanous201a1012024-04-03 18:07:28 -0700101 void checkThresholds() override;
102 void read();
103 void init();
104 std::string getSubTypeUnits() const;
105 void loadDefaults();
106 void runInitCmd();
Ed Tanous828c5a62024-02-09 16:59:22 -0800107 static bool processReading(ReadingFormat readingFormat, uint8_t command,
108 const std::vector<uint8_t>& data, double& resp,
109 size_t errCount);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530110 void parseConfigValues(const SensorBaseConfigMap& entry);
111 bool sensorClassType(const std::string& sensorClass);
112 void sensorSubType(const std::string& sensorTypeName);
James Feist6ef20402019-01-07 16:45:08 -0800113
Ed Tanousa771f6a2022-01-14 09:36:51 -0800114 IpmbType type = IpmbType::none;
115 IpmbSubType subType = IpmbSubType::none;
116 double scaleVal = 1.0;
117 double offsetVal = 0.0;
118 uint8_t commandAddress = 0;
119 uint8_t netfn = 0;
120 uint8_t command = 0;
121 uint8_t deviceAddress = 0;
122 uint8_t errorCount = 0;
123 uint8_t hostSMbusIndex = 0;
James Feist6ef20402019-01-07 16:45:08 -0800124 std::vector<uint8_t> commandData;
125 std::optional<uint8_t> initCommand;
126 std::vector<uint8_t> initData;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +0530127 int sensorPollMs;
James Feist6ef20402019-01-07 16:45:08 -0800128
Ed Tanousa771f6a2022-01-14 09:36:51 -0800129 ReadingFormat readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800130
131 private:
Ed Tanous201a1012024-04-03 18:07:28 -0700132 void sendIpmbRequest();
James Feistd8705872019-02-08 13:26:09 -0800133 sdbusplus::asio::object_server& objectServer;
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700134 boost::asio::steady_timer waitTimer;
Vikash Chandola1f847972022-09-28 09:47:32 +0000135 void ipmbRequestCompletionCb(const boost::system::error_code& ec,
136 const IpmbMethodType& response);
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700137};
Ed Tanous828c5a62024-02-09 16:59:22 -0800138
139void createSensors(
140 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
141 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
142 sensors,
143 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection);
144
145void interfaceRemoved(
146 sdbusplus::message_t& message,
147 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
148 sensors);