1 #pragma once
2 
3 #include <boost/asio/io_context.hpp>
4 #include <boost/asio/steady_timer.hpp>
5 #include <gpiod.hpp>
6 #include <sdbusplus/asio/object_server.hpp>
7 
8 #include <memory>
9 #include <string>
10 
11 namespace fs = std::filesystem;
12 
13 class ChassisIntrusionSensor
14 {
15   public:
16     explicit ChassisIntrusionSensor(bool autoRearm,
17                                     sdbusplus::asio::object_server& objServer);
18 
19     virtual ~ChassisIntrusionSensor();
20 
21     void start();
22 
23   protected:
24     virtual int readSensor() = 0;
25     virtual void pollSensorStatus() = 0;
26     void updateValue(const size_t& value);
27 
28   private:
29     std::string mValue;
30     // If this sensor uses automatic rearm method. Otherwise, manually rearm it
31     bool mAutoRearm;
32     std::shared_ptr<sdbusplus::asio::dbus_interface> mIface;
33     sdbusplus::asio::object_server& mObjServer;
34     bool mOverridenState = false;
35     bool mInternalSet = false;
36     bool mRearmFlag = false;
37 
38     int setSensorValue(const std::string& req, std::string& propertyValue);
39 };
40 
41 class ChassisIntrusionPchSensor :
42     public ChassisIntrusionSensor,
43     public std::enable_shared_from_this<ChassisIntrusionPchSensor>
44 {
45   public:
46     ChassisIntrusionPchSensor(bool autoRearm, boost::asio::io_context& io,
47                               sdbusplus::asio::object_server& objServer,
48                               int busId, int slaveAddr);
49 
50     ~ChassisIntrusionPchSensor() override;
51 
52   private:
53     int mBusFd{-1};
54     int mSlaveAddr{-1};
55     boost::asio::steady_timer mPollTimer;
56     int readSensor() override;
57     void pollSensorStatus() override;
58 };
59 
60 class ChassisIntrusionGpioSensor :
61     public ChassisIntrusionSensor,
62     public std::enable_shared_from_this<ChassisIntrusionGpioSensor>
63 {
64   public:
65     ChassisIntrusionGpioSensor(bool autoRearm, boost::asio::io_context& io,
66                                sdbusplus::asio::object_server& objServer,
67                                bool gpioInverted);
68 
69     ~ChassisIntrusionGpioSensor() override;
70 
71   private:
72     bool mGpioInverted{false};
73     std::string mPinName = "CHASSIS_INTRUSION";
74     gpiod::line mGpioLine;
75     boost::asio::posix::stream_descriptor mGpioFd;
76     int readSensor() override;
77     void pollSensorStatus() override;
78 };
79 
80 class ChassisIntrusionHwmonSensor :
81     public ChassisIntrusionSensor,
82     public std::enable_shared_from_this<ChassisIntrusionHwmonSensor>
83 {
84   public:
85     ChassisIntrusionHwmonSensor(bool autoRearm, boost::asio::io_context& io,
86                                 sdbusplus::asio::object_server& objServer,
87                                 std::string hwmonName);
88 
89     ~ChassisIntrusionHwmonSensor() override;
90 
91   private:
92     std::string mHwmonName;
93     std::string mHwmonPath;
94     boost::asio::steady_timer mPollTimer;
95     int readSensor() override;
96     void pollSensorStatus() override;
97 };
98