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