xref: /openbmc/dbus-sensors/src/smbpbi/SmbpbiSensor.hpp (revision 021261ce7a0b6686e4f040f7212718ba74d15137)
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-2025 NVIDIA CORPORATION &
3  * AFFILIATES. All rights reserved.
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 #include "Thresholds.hpp"
9 
10 #include <boost/asio/io_context.hpp>
11 #include <boost/asio/random_access_file.hpp>
12 #include <boost/asio/steady_timer.hpp>
13 #include <sdbusplus/asio/connection.hpp>
14 #include <sdbusplus/asio/object_server.hpp>
15 #include <sensor.hpp>
16 
17 #include <array>
18 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 constexpr std::array<size_t, 3> i2CReadLenValues = {4, 8, 8};
25 
26 enum class I2C_READ_LEN_INDEX
27 {
28     FLOAT32,
29     FLOAT64,
30     UINT64
31 };
32 
33 struct SmbpbiSensor : public Sensor
34 {
35     SmbpbiSensor(
36         std::shared_ptr<sdbusplus::asio::connection>& conn,
37         boost::asio::io_context& io, const std::string& name,
38         const std::string& sensorConfiguration, const std::string& objType,
39         sdbusplus::asio::object_server& objectServer,
40         std::vector<thresholds::Threshold>&& thresholdData, uint8_t busId,
41         uint8_t addr, uint16_t offset, std::string& sensorUnits,
42         std::string& valueType, size_t pollTime, double minVal, double maxVal,
43         std::string& path);
44     ~SmbpbiSensor() override;
45 
46     void checkThresholds() override;
47 
getPollRateSmbpbiSensor48     size_t getPollRate() const
49     {
50         return pollRateSecond;
51     }
52     void read();
53     void init();
54 
55     uint8_t busId;
56     uint8_t addr;
57     uint16_t offset;
58     std::string sensorUnits;
59     std::string sensorType;
60     std::string valueType;
61 
62   private:
63     int i2cReadDataBytes(uint8_t* reading, int length);
64     int i2cReadDataBytesDouble(double& reading);
65     int i2cReadDataBytesUI64(uint64_t& reading);
66     int readRawEEPROMData(double& data);
67     int readFloat64EEPROMData(double& data);
68     static double convert2Temp(const uint8_t* rawData);
69     static double convert2Power(const uint8_t* rawData);
70     void waitReadCallback(const boost::system::error_code& ec);
71     sdbusplus::asio::object_server& objectServer;
72     boost::asio::random_access_file inputDev;
73     boost::asio::steady_timer waitTimer;
74     size_t pollRateSecond;
75 };
76 
checkInvalidReading(uint8_t * reading,int length)77 bool checkInvalidReading(uint8_t* reading, int length)
78 {
79     // there is no value updated from HMC if reading data is all 0xff
80     uint8_t* ptr = reading;
81     for (int i = 0; i < length; i++, ptr++)
82     {
83         if (*ptr != 0xFF)
84         {
85             return false;
86         }
87     }
88     return true;
89 }
90