xref: /openbmc/dbus-sensors/src/ipmb/IpmbSDRSensor.hpp (revision 18b6186e531ae37dd22b634c6530f793528473f4)
1 #pragma once
2 
3 #include <sdbusplus/asio/connection.hpp>
4 
5 #include <cstdint>
6 #include <map>
7 #include <memory>
8 #include <string>
9 #include <tuple>
10 #include <vector>
11 
12 using IpmbMethodType =
13     std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>;
14 
15 enum class SDRType
16 {
17     sdrType01 = 1,
18     sdrType02 = 2,
19     sdrType03 = 3
20 };
21 
22 namespace sdr
23 {
24 // IPMB Commands
25 static constexpr uint8_t netfnStorageReq = 0x0a;
26 static constexpr uint8_t cmdStorageGetSdrInfo = 0x20;
27 static constexpr uint8_t cmdStorageReserveSdr = 0x22;
28 static constexpr uint8_t cmdStorageGetSdr = 0x23;
29 
30 // Get SDR Commands
31 static constexpr uint8_t sdrNxtRecLSB = 0;
32 static constexpr uint8_t sdrNxtRecMSB = 1;
33 static constexpr uint8_t perCountByte = 16;
34 
35 // Sensor Record Bytes
36 static constexpr uint8_t sdrType = 5;
37 static constexpr uint8_t dataLengthByte = 6;
38 static constexpr uint8_t sdrSensorNum = 9;
39 
40 } // namespace sdr
41 
42 namespace sdrtype01
43 {
44 // Negative Handle Commands
45 static constexpr uint8_t maxPosReadingMargin = 127;
46 static constexpr uint8_t twosCompVal = 128;
47 static constexpr double thermalConst = 256;
48 
49 static constexpr uint8_t sdrSensNoThres = 0;
50 static constexpr uint8_t sensorCapability = 13;
51 static constexpr uint8_t sdrNegHandle = 24;
52 static constexpr uint8_t sdrUnitType = 25;
53 static constexpr uint8_t sdrLinearByte = 27;
54 
55 // SDR Type 1 Thresholds Commands
56 static constexpr uint8_t mDataByte = 28;
57 static constexpr uint8_t mTolDataByte = 29;
58 static constexpr uint8_t bDataByte = 30;
59 static constexpr uint8_t bAcuDataByte = 31;
60 static constexpr uint8_t rbExpDataByte = 33;
61 static constexpr uint8_t upperCriticalThreshold = 43;
62 static constexpr uint8_t lowerCriticalThreshold = 46;
63 static constexpr uint8_t nameLengthByte = 53;
64 
65 } // namespace sdrtype01
66 
67 struct SensorInfo
68 {
69     std::string sensorReadName;
70     uint8_t sensorUnit = 0;
71     double thresUpperCri = 0;
72     double thresLowerCri = 0;
73     uint8_t sensorNumber = 0;
74     uint8_t sensCap = 0;
75 };
76 
77 struct SensorValConversion
78 {
79     uint16_t mValue = 0;
80     double bValue = 0;
81     double expoVal = 0;
82     uint8_t negRead = 0;
83 };
84 
85 inline std::map<int, std::vector<SensorInfo>> sensorRecord;
86 inline std::map<int, std::map<uint8_t, SensorValConversion>> sensorValRecord;
87 
88 class IpmbSDRDevice : public std::enable_shared_from_this<IpmbSDRDevice>
89 {
90   public:
91     IpmbSDRDevice(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
92                   uint8_t cmdAddr);
93 
94     uint8_t commandAddress = 0;
95     int hostIndex = 0;
96 
97     std::shared_ptr<sdbusplus::asio::connection> conn;
98 
99     std::vector<uint8_t> sdrData;
100     uint16_t validRecordCount = 1;
101     uint8_t iCnt = 0;
102     uint8_t nextRecordIDLSB = 0;
103     uint8_t nextRecordIDMSB = 0;
104 
105     std::vector<uint8_t> sdrCommandData;
106 
107     void getSDRRepositoryInfo();
108 
109     void reserveSDRRepository(uint16_t recordCount);
110 
111     void getSDRSensorData(uint16_t recordCount, uint8_t resrvIDLSB,
112                           uint8_t resrvIDMSB);
113 
114     void handleSDRData(const std::vector<uint8_t>& data, uint16_t recordCount,
115                        uint8_t resrvIDLSB, uint8_t resrvIDMSB);
116 
117     void checkSDRData(std::vector<uint8_t>& sdrDataBytes,
118                       uint8_t dataLength) const;
119 
120     static void checkSDRType01Threshold(std::vector<uint8_t>& sdrDataBytes,
121                                         int busIndex, std::string tempName);
122 
123     inline static double sensorValCalculation(uint16_t mValue, double bValue,
124                                               double expValue, double value);
125 };
126