blob: c5844cfb26c47d229767f44135a630a5457788a2 [file] [log] [blame]
James Feist6ef20402019-01-07 16:45:08 -08001#pragma once
Ed Tanous18b61862025-01-30 10:56:28 -08002#include "Thresholds.hpp"
3#include "Utils.hpp"
4
5#include <boost/asio/io_context.hpp>
Ed Tanous9b4a20e2022-09-06 08:47:11 -07006#include <boost/asio/steady_timer.hpp>
James Feist6ef20402019-01-07 16:45:08 -08007#include <boost/container/flat_map.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -08008#include <sdbusplus/asio/connection.hpp>
9#include <sdbusplus/asio/object_server.hpp>
10#include <sdbusplus/message.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070011#include <sensor.hpp>
James Feist38fb5982020-05-28 10:09:54 -070012
Ed Tanous18b61862025-01-30 10:56:28 -080013#include <cstddef>
14#include <cstdint>
Patrick Venturefd6ba732019-10-31 14:27:39 -070015#include <memory>
16#include <optional>
17#include <string>
Ed Tanous18b61862025-01-30 10:56:28 -080018#include <tuple>
James Feist6ef20402019-01-07 16:45:08 -080019#include <vector>
20
Ed Tanous828c5a62024-02-09 16:59:22 -080021constexpr const char* sensorType = "IpmbSensor";
22constexpr const char* sdrInterface = "IpmbDevice";
23
James Feist6ef20402019-01-07 16:45:08 -080024enum class IpmbType
25{
Ed Tanousa771f6a2022-01-14 09:36:51 -080026 none,
James Feist6ef20402019-01-07 16:45:08 -080027 meSensor,
28 PXE1410CVR,
29 IR38363VR,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070030 ADM1278HSC,
Rebecca Cran394f0c52023-12-17 20:08:55 -070031 mpsVR,
32 SMPro
James Feist6ef20402019-01-07 16:45:08 -080033};
34
Vijay Khemka682a5cb2019-07-18 17:34:03 -070035enum class IpmbSubType
36{
Ed Tanousa771f6a2022-01-14 09:36:51 -080037 none,
Vijay Khemka682a5cb2019-07-18 17:34:03 -070038 temp,
39 curr,
40 power,
Adrian Ambrożewicz45e92772020-06-04 13:59:55 +020041 volt,
42 util
Vijay Khemka682a5cb2019-07-18 17:34:03 -070043};
44
James Feistd7ae29a2020-06-25 15:42:46 -070045enum class ReadingFormat
46{
47 byte0,
48 byte3,
Rebecca Cran394f0c52023-12-17 20:08:55 -070049 nineBit,
50 tenBit,
James Feistd7ae29a2020-06-25 15:42:46 -070051 elevenBit,
52 elevenBitShift,
Rebecca Cran394f0c52023-12-17 20:08:55 -070053 linearElevenBit,
54 fifteenBit
James Feistd7ae29a2020-06-25 15:42:46 -070055};
56
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020057namespace ipmi
58{
59namespace sensor
60{
61constexpr uint8_t netFn = 0x04;
62constexpr uint8_t getSensorReading = 0x2d;
63
Patrick Williamsd02ad492023-05-08 09:15:21 -050064static inline bool isValid(const std::vector<uint8_t>& data)
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020065{
Ed Tanous8a57ec02020-10-09 12:46:52 -070066 constexpr auto readingUnavailableBit = 5;
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020067
68 // Proper 'Get Sensor Reading' response has at least 4 bytes, including
69 // Completion Code. Our IPMB stack strips Completion Code from payload so we
70 // compare here against the rest of payload
71 if (data.size() < 3)
72 {
73 return false;
74 }
75
76 // Per IPMI 'Get Sensor Reading' specification
Ed Tanous2049bd22022-07-09 07:20:26 -070077 if ((data[1] & (1 << readingUnavailableBit)) != 0)
Adrian Ambrożewicz58e02ef2020-08-06 14:42:38 +020078 {
79 return false;
80 }
81
82 return true;
83}
84
85} // namespace sensor
86namespace me_bridge
87{
88constexpr uint8_t netFn = 0x2e;
89constexpr uint8_t sendRawPmbus = 0xd9;
90} // namespace me_bridge
91} // namespace ipmi
92
Vikash Chandola1f847972022-09-28 09:47:32 +000093using IpmbMethodType =
94 std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
95
96struct IpmbSensor :
97 public Sensor,
98 public std::enable_shared_from_this<IpmbSensor>
James Feist6ef20402019-01-07 16:45:08 -080099{
James Feistd8705872019-02-08 13:26:09 -0800100 IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -0800101 boost::asio::io_context& io, const std::string& name,
James Feistd8705872019-02-08 13:26:09 -0800102 const std::string& sensorConfiguration,
103 sdbusplus::asio::object_server& objectServer,
Ed Tanous2049bd22022-07-09 07:20:26 -0700104 std::vector<thresholds::Threshold>&& thresholdData,
105 uint8_t deviceAddress, uint8_t hostSMbusIndex, float pollRate,
106 std::string& sensorTypeName);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700107 ~IpmbSensor() override;
James Feist6ef20402019-01-07 16:45:08 -0800108
Ed Tanous201a1012024-04-03 18:07:28 -0700109 void checkThresholds() override;
110 void read();
111 void init();
112 std::string getSubTypeUnits() const;
113 void loadDefaults();
114 void runInitCmd();
Ed Tanous828c5a62024-02-09 16:59:22 -0800115 static bool processReading(ReadingFormat readingFormat, uint8_t command,
116 const std::vector<uint8_t>& data, double& resp,
117 size_t errCount);
Jayashree Dhanapal84189752022-03-07 12:51:54 +0530118 void parseConfigValues(const SensorBaseConfigMap& entry);
119 bool sensorClassType(const std::string& sensorClass);
120 void sensorSubType(const std::string& sensorTypeName);
James Feist6ef20402019-01-07 16:45:08 -0800121
Ed Tanousa771f6a2022-01-14 09:36:51 -0800122 IpmbType type = IpmbType::none;
123 IpmbSubType subType = IpmbSubType::none;
124 double scaleVal = 1.0;
125 double offsetVal = 0.0;
126 uint8_t commandAddress = 0;
127 uint8_t netfn = 0;
128 uint8_t command = 0;
129 uint8_t deviceAddress = 0;
130 uint8_t errorCount = 0;
131 uint8_t hostSMbusIndex = 0;
James Feist6ef20402019-01-07 16:45:08 -0800132 std::vector<uint8_t> commandData;
133 std::optional<uint8_t> initCommand;
134 std::vector<uint8_t> initData;
Jayashree-D9f6d4fd2021-04-13 18:27:22 +0530135 int sensorPollMs;
James Feist6ef20402019-01-07 16:45:08 -0800136
Ed Tanousa771f6a2022-01-14 09:36:51 -0800137 ReadingFormat readingFormat = ReadingFormat::byte0;
James Feist6ef20402019-01-07 16:45:08 -0800138
139 private:
Ed Tanous201a1012024-04-03 18:07:28 -0700140 void sendIpmbRequest();
James Feistd8705872019-02-08 13:26:09 -0800141 sdbusplus::asio::object_server& objectServer;
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700142 boost::asio::steady_timer waitTimer;
Vikash Chandola1f847972022-09-28 09:47:32 +0000143 void ipmbRequestCompletionCb(const boost::system::error_code& ec,
144 const IpmbMethodType& response);
Vijay Khemka682a5cb2019-07-18 17:34:03 -0700145};
Ed Tanous828c5a62024-02-09 16:59:22 -0800146
147void createSensors(
148 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
149 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
150 sensors,
151 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection);
152
153void interfaceRemoved(
154 sdbusplus::message_t& message,
155 boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>&
156 sensors);