blob: 0c27ef960e4e6adcc2437d151e01c9acec9ae222 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
2
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10303#include "DeviceMgmt.hpp"
4#include "Thresholds.hpp"
Ed Tanous18b61862025-01-30 10:56:28 -08005#include "Utils.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +10306#include "sensor.hpp"
7
Ed Tanous18b61862025-01-30 10:56:28 -08008#include <boost/asio/io_context.hpp>
Zev Weissd1c8f442022-07-29 11:54:41 -07009#include <boost/asio/random_access_file.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080010#include <boost/asio/steady_timer.hpp>
11#include <sdbusplus/asio/connection.hpp>
James Feist6714a252018-09-10 15:26:18 -070012#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070013
Ed Tanous18b61862025-01-30 10:56:28 -080014#include <array>
15#include <cstddef>
16#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070017#include <string>
18#include <vector>
James Feist6714a252018-09-10 15:26:18 -070019
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050020struct SensorParams
21{
22 double minValue;
23 double maxValue;
24 double offsetValue;
25 double scaleValue;
26 std::string units;
27 std::string typeName;
28};
29
James Feist38fb5982020-05-28 10:09:54 -070030class HwmonTempSensor :
31 public Sensor,
32 public std::enable_shared_from_this<HwmonTempSensor>
James Feist6714a252018-09-10 15:26:18 -070033{
34 public:
James Feistd8705872019-02-08 13:26:09 -080035 HwmonTempSensor(const std::string& path, const std::string& objectType,
36 sdbusplus::asio::object_server& objectServer,
37 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080038 boost::asio::io_context& io, const std::string& sensorName,
James Feistd8705872019-02-08 13:26:09 -080039 std::vector<thresholds::Threshold>&& thresholds,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050040 const struct SensorParams& thisSensorParameters,
Ed Tanous2049bd22022-07-09 07:20:26 -070041 float pollRate, const std::string& sensorConfiguration,
Zev Weissa1456c42022-07-18 16:59:35 -070042 PowerState powerState,
43 const std::shared_ptr<I2CDevice>& i2cDevice);
Ed Tanous8a57ec02020-10-09 12:46:52 -070044 ~HwmonTempSensor() override;
Ed Tanous201a1012024-04-03 18:07:28 -070045 void setupRead();
Zev Weissa1456c42022-07-18 16:59:35 -070046 void activate(const std::string& newPath,
47 const std::shared_ptr<I2CDevice>& newI2CDevice);
Ed Tanous201a1012024-04-03 18:07:28 -070048 void deactivate();
49 bool isActive();
James Feist6714a252018-09-10 15:26:18 -070050
Zev Weiss7627c862022-11-17 14:23:04 -080051 std::shared_ptr<I2CDevice> getI2CDevice() const
52 {
53 return i2cDevice;
54 }
55
James Feist6714a252018-09-10 15:26:18 -070056 private:
Zev Weissd1c8f442022-07-29 11:54:41 -070057 // Ordering is important here; readBuf is first so that it's not destroyed
58 // while async operations from other member fields might still be using it.
59 std::array<char, 128> readBuf{};
Zev Weissa1456c42022-07-18 16:59:35 -070060 std::shared_ptr<I2CDevice> i2cDevice;
James Feistd8705872019-02-08 13:26:09 -080061 sdbusplus::asio::object_server& objServer;
Zev Weissd1c8f442022-07-29 11:54:41 -070062 boost::asio::random_access_file inputDev;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070063 boost::asio::steady_timer waitTimer;
James Feist930fcde2019-05-28 12:58:43 -070064 std::string path;
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050065 double offsetValue;
66 double scaleValue;
Jeff Lin87bc67f2020-12-04 20:58:01 +080067 unsigned int sensorPollMs;
Yong Lif3fd1912020-03-25 21:35:23 +080068
Zev Weissd1c8f442022-07-29 11:54:41 -070069 void handleResponse(const boost::system::error_code& err, size_t bytesRead);
Konstantin Aladysheva7345942021-04-28 17:09:02 +030070 void restartRead();
Ed Tanous201a1012024-04-03 18:07:28 -070071 void checkThresholds() override;
Patrick Ventureca44b2f2019-10-31 11:02:26 -070072};