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