blob: be71e3f65288af7500554afbaf26acfa4437e50f [file] [log] [blame]
Qiang XUe28d1fa2019-02-27 13:50:56 +08001#pragma once
2
Ed Tanous1f978632023-02-28 18:16:39 -08003#include <boost/asio/io_context.hpp>
Ed Tanous9b4a20e2022-09-06 08:47:11 -07004#include <boost/asio/steady_timer.hpp>
ZhikuiRenba8a8bf2020-01-09 15:55:43 -08005#include <gpiod.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +08006#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -07007
8#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -07009#include <string>
Qiang XUe28d1fa2019-02-27 13:50:56 +080010
11enum IntrusionSensorType
12{
13 pch,
14 gpio
15};
16
17class ChassisIntrusionSensor
18{
19 public:
20 ChassisIntrusionSensor(
Ed Tanous1f978632023-02-28 18:16:39 -080021 boost::asio::io_context& io,
Qiang XUe28d1fa2019-02-27 13:50:56 +080022 std::shared_ptr<sdbusplus::asio::dbus_interface> iface);
23
24 ~ChassisIntrusionSensor();
25
26 void start(IntrusionSensorType type, int busId, int slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -080027 bool gpioInverted);
Qiang XUe28d1fa2019-02-27 13:50:56 +080028
29 private:
30 std::shared_ptr<sdbusplus::asio::dbus_interface> mIface;
31 std::shared_ptr<sdbusplus::asio::connection> mDbusConn;
32
Ed Tanousb429f312022-06-27 16:09:53 -070033 IntrusionSensorType mType{IntrusionSensorType::gpio};
Qiang XUe28d1fa2019-02-27 13:50:56 +080034
35 // intrusion status. 0: not intruded, 1: intruded
36 std::string mValue = "unknown";
37 std::string mOldValue = "unknown";
38
39 // valid if it is PCH register via i2c
Ed Tanousb429f312022-06-27 16:09:53 -070040 int mBusId{-1};
41 int mSlaveAddr{-1};
Ed Tanous9b4a20e2022-09-06 08:47:11 -070042 boost::asio::steady_timer mPollTimer;
Qiang XUe28d1fa2019-02-27 13:50:56 +080043
44 // valid if it is via GPIO
Ed Tanousb429f312022-06-27 16:09:53 -070045 bool mGpioInverted{false};
ZhikuiRenba8a8bf2020-01-09 15:55:43 -080046 std::string mPinName = "CHASSIS_INTRUSION";
47 gpiod::line mGpioLine;
48 boost::asio::posix::stream_descriptor mGpioFd;
Qiang XUe28d1fa2019-02-27 13:50:56 +080049
50 // common members
Qiang XUe28d1fa2019-02-27 13:50:56 +080051 bool mOverridenState = false;
52 bool mInternalSet = false;
53
54 bool mInitialized = false;
55
Ed Tanous8a57ec02020-10-09 12:46:52 -070056 void updateValue(const std::string& newValue);
Ed Tanous2049bd22022-07-09 07:20:26 -070057 static int i2cReadFromPch(int busId, int slaveAddr);
Qiang XUe28d1fa2019-02-27 13:50:56 +080058 void pollSensorStatusByPch();
59 void readGpio();
60 void pollSensorStatusByGpio();
ZhikuiRenba8a8bf2020-01-09 15:55:43 -080061 void initGpioDeviceFile();
Qiang XUe28d1fa2019-02-27 13:50:56 +080062 int setSensorValue(const std::string& req, std::string& propertyValue);
63};