blob: 58f20d54cad6adf41d3dcf4320302df572c00d3e [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
13enum class IpmbType
14{
Ed Tanousa771f6a2022-01-14 09:36:51 -080015 none,
James Feist6ef20402019-01-07 16:45:08 -080016 meSensor,
17 PXE1410CVR,
18 IR38363VR,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070019 ADM1278HSC,
James Feist6ef20402019-01-07 16:45:08 -080020 mpsVR
21};
22
Vijay Khemka682a5cb2019-07-18 17:34:03 -070023enum class IpmbSubType
24{
Ed Tanousa771f6a2022-01-14 09:36:51 -080025 none,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070026 temp,
27 curr,
28 power,
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +020029 volt,
30 util
Vijay Khemka682a5cb2019-07-18 17:34:03 -070031};
32
James Feistd7ae29a2020-06-25 15:42:46 -070033enum class ReadingFormat
34{
35 byte0,
36 byte3,
37 elevenBit,
38 elevenBitShift,
Jayashree-D37322572021-03-19 17:40:56 +053039 linearElevenBit
James Feistd7ae29a2020-06-25 15:42:46 -070040};
41
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020042namespace ipmi
43{
44namespace sensor
45{
46constexpr uint8_t netFn = 0x04;
47constexpr uint8_t getSensorReading = 0x2d;
48
Patrick Williamsd02ad492023-05-08 09:15:21 -050049static inline bool isValid(const std::vector<uint8_t>& data)
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020050{
Ed Tanous8a57ec02020-10-09 12:46:52 -070051 constexpr auto readingUnavailableBit = 5;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020052
53 // Proper 'Get Sensor Reading' response has at least 4 bytes, including
54 // Completion Code. Our IPMB stack strips Completion Code from payload so we
55 // compare here against the rest of payload
56 if (data.size() < 3)
57 {
58 return false;
59 }
60
61 // Per IPMI 'Get Sensor Reading' specification
Ed Tanous2049bd22022-07-09 07:20:26 -070062 if ((data[1] & (1 << readingUnavailableBit)) != 0)
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020063 {
64 return false;
65 }
66
67 return true;
68}
69
70} // namespace sensor
71namespace me_bridge
72{
73constexpr uint8_t netFn = 0x2e;
74constexpr uint8_t sendRawPmbus = 0xd9;
75} // namespace me_bridge
76} // namespace ipmi
77
Vikash Chandola1f847972022-09-28 09:47:32 +000078using IpmbMethodType =
79 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
80
81struct IpmbSensor :
82 public Sensor,
83 public std::enable_shared_from_this<IpmbSensor>
James Feist6ef20402019-01-07 16:45:08 -080084{
James Feistd8705872019-02-08 13:26:09 -080085 IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080086 boost::asio::io_context& io, const std::string& name,
James Feistd8705872019-02-08 13:26:09 -080087 const std::string& sensorConfiguration,
88 sdbusplus::asio::object_server& objectServer,
Ed Tanous2049bd22022-07-09 07:20:26 -070089 std::vector<thresholds::Threshold>&& thresholdData,
90 uint8_t deviceAddress, uint8_t hostSMbusIndex, float pollRate,
91 std::string& sensorTypeName);
Ed Tanous8a57ec02020-10-09 12:46:52 -070092 ~IpmbSensor() override;
James Feist6ef20402019-01-07 16:45:08 -080093
Ed Tanous201a1012024-04-03 18:07:28 -070094 void checkThresholds() override;
95 void read();
96 void init();
97 std::string getSubTypeUnits() const;
98 void loadDefaults();
99 void runInitCmd();
James Feist961bf092020-07-01 16:38:12 -0700100 bool processReading(const std::vector<uint8_t>& data, double& resp);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530101 void parseConfigValues(const SensorBaseConfigMap& entry);
102 bool sensorClassType(const std::string& sensorClass);
103 void sensorSubType(const std::string& sensorTypeName);
James Feist6ef20402019-01-07 16:45:08 -0800104
Ed Tanousa771f6a2022-01-14 09:36:51 -0800105 IpmbType type = IpmbType::none;
106 IpmbSubType subType = IpmbSubType::none;
107 double scaleVal = 1.0;
108 double offsetVal = 0.0;
109 uint8_t commandAddress = 0;
110 uint8_t netfn = 0;
111 uint8_t command = 0;
112 uint8_t deviceAddress = 0;
113 uint8_t errorCount = 0;
114 uint8_t hostSMbusIndex = 0;
James Feist6ef20402019-01-07 16:45:08 -0800115 std::vector<uint8_t> commandData;
116 std::optional<uint8_t> initCommand;
117 std::vector<uint8_t> initData;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +0530118 int sensorPollMs;
James Feist6ef20402019-01-07 16:45:08 -0800119
Ed Tanousa771f6a2022-01-14 09:36:51 -0800120 ReadingFormat readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800121
122 private:
Ed Tanous201a1012024-04-03 18:07:28 -0700123 void sendIpmbRequest();
James Feistd8705872019-02-08 13:26:09 -0800124 sdbusplus::asio::object_server& objectServer;
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700125 boost::asio::steady_timer waitTimer;
Vikash Chandola1f847972022-09-28 09:47:32 +0000126 void ipmbRequestCompletionCb(const boost::system::error_code& ec,
127 const IpmbMethodType& response);
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700128};