intrusionsensor: Refactor source code

This commit splits the intrusionsensor source code into a base class and
derived classes to dictate sensor of different methods. Only one method
of chassis intrusion detection is expected for one given system.

Tested with hwmon method on Ampere Mt.Mitchell platform which is
implemented in the patch:
https://gerrit.openbmc.org/c/openbmc/dbus-sensors/+/62775

Signed-off-by: Chau Ly <chaul@amperecomputing.com>
Change-Id: Ia2b71d897a27ee7c7955e233d060ad14d62be3a8
diff --git a/src/ChassisIntrusionSensor.hpp b/src/ChassisIntrusionSensor.hpp
index be71e3f..e690bd2 100644
--- a/src/ChassisIntrusionSensor.hpp
+++ b/src/ChassisIntrusionSensor.hpp
@@ -8,56 +8,69 @@
 #include <memory>
 #include <string>
 
-enum IntrusionSensorType
-{
-    pch,
-    gpio
-};
+namespace fs = std::filesystem;
 
 class ChassisIntrusionSensor
 {
   public:
-    ChassisIntrusionSensor(
-        boost::asio::io_context& io,
-        std::shared_ptr<sdbusplus::asio::dbus_interface> iface);
+    explicit ChassisIntrusionSensor(sdbusplus::asio::object_server& objServer);
 
-    ~ChassisIntrusionSensor();
+    virtual ~ChassisIntrusionSensor();
 
-    void start(IntrusionSensorType type, int busId, int slaveAddr,
-               bool gpioInverted);
+    void start();
+
+  protected:
+    virtual int readSensor() = 0;
+    virtual void pollSensorStatus() = 0;
+    void updateValue(const size_t& value);
 
   private:
-    std::shared_ptr<sdbusplus::asio::dbus_interface> mIface;
-    std::shared_ptr<sdbusplus::asio::connection> mDbusConn;
-
-    IntrusionSensorType mType{IntrusionSensorType::gpio};
-
     // intrusion status. 0: not intruded, 1: intruded
     std::string mValue = "unknown";
     std::string mOldValue = "unknown";
+    std::shared_ptr<sdbusplus::asio::dbus_interface> mIface;
+    sdbusplus::asio::object_server& mObjServer;
+    bool mOverridenState = false;
+    bool mInternalSet = false;
 
-    // valid if it is PCH register via i2c
-    int mBusId{-1};
+    int setSensorValue(const std::string& req, std::string& propertyValue);
+};
+
+class ChassisIntrusionPchSensor :
+    public ChassisIntrusionSensor,
+    public std::enable_shared_from_this<ChassisIntrusionPchSensor>
+{
+  public:
+    ChassisIntrusionPchSensor(boost::asio::io_context& io,
+                              sdbusplus::asio::object_server& objServer,
+                              int busId, int slaveAddr);
+
+    ~ChassisIntrusionPchSensor() override;
+
+  private:
+    int mBusFd{-1};
     int mSlaveAddr{-1};
     boost::asio::steady_timer mPollTimer;
+    int readSensor() override;
+    void pollSensorStatus() override;
+};
 
-    // valid if it is via GPIO
+class ChassisIntrusionGpioSensor :
+    public ChassisIntrusionSensor,
+    public std::enable_shared_from_this<ChassisIntrusionGpioSensor>
+{
+  public:
+    ChassisIntrusionGpioSensor(boost::asio::io_context& io,
+                               sdbusplus::asio::object_server& objServer,
+                               bool gpioInverted);
+
+    ~ChassisIntrusionGpioSensor() override;
+
+  private:
     bool mGpioInverted{false};
     std::string mPinName = "CHASSIS_INTRUSION";
     gpiod::line mGpioLine;
     boost::asio::posix::stream_descriptor mGpioFd;
-
-    // common members
-    bool mOverridenState = false;
-    bool mInternalSet = false;
-
-    bool mInitialized = false;
-
-    void updateValue(const std::string& newValue);
-    static int i2cReadFromPch(int busId, int slaveAddr);
-    void pollSensorStatusByPch();
-    void readGpio();
-    void pollSensorStatusByGpio();
-    void initGpioDeviceFile();
-    int setSensorValue(const std::string& req, std::string& propertyValue);
+    int readSensor() override;
+    void pollSensorStatus() override;
 };