| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "ChassisIntrusionSensor.hpp" |
| 18 | |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include <linux/i2c.h> |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 21 | #include <sys/ioctl.h> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 22 | #include <sys/syslog.h> |
| Matt Simmering | 79a160b | 2022-11-14 16:50:48 -0800 | [diff] [blame] | 23 | #include <systemd/sd-journal.h> |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 26 | #include <Utils.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 27 | #include <boost/asio/error.hpp> |
| Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 28 | #include <boost/asio/io_context.hpp> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 29 | #include <boost/asio/posix/stream_descriptor.hpp> |
| 30 | #include <gpiod.hpp> |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 31 | #include <phosphor-logging/lg2.hpp> |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 32 | #include <sdbusplus/asio/object_server.hpp> |
| 33 | |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 34 | #include <chrono> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 35 | #include <cstddef> |
| 36 | #include <cstdint> |
| 37 | #include <filesystem> |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 38 | #include <fstream> |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 39 | #include <iostream> |
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 40 | #include <memory> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 41 | #include <stdexcept> |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 42 | #include <string> |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 43 | #include <utility> |
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 44 | #include <vector> |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 45 | |
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 46 | extern "C" |
| 47 | { |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 48 | #include <i2c/smbus.h> |
| 49 | #include <linux/i2c-dev.h> |
| 50 | } |
| 51 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 52 | static constexpr unsigned int defaultPollSec = 1; |
| 53 | static constexpr unsigned int sensorFailedPollSec = 5; |
| 54 | static unsigned int intrusionSensorPollSec = defaultPollSec; |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 55 | static constexpr const char* hwIntrusionValStr = |
| 56 | "xyz.openbmc_project.Chassis.Intrusion.Status.HardwareIntrusion"; |
| 57 | static constexpr const char* normalValStr = |
| 58 | "xyz.openbmc_project.Chassis.Intrusion.Status.Normal"; |
| 59 | static constexpr const char* manualRearmStr = |
| 60 | "xyz.openbmc_project.Chassis.Intrusion.RearmMode.Manual"; |
| 61 | static constexpr const char* autoRearmStr = |
| 62 | "xyz.openbmc_project.Chassis.Intrusion.RearmMode.Automatic"; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 63 | |
| 64 | // SMLink Status Register |
| 65 | const static constexpr size_t pchStatusRegIntrusion = 0x04; |
| 66 | |
| 67 | // Status bit field masks |
| 68 | const static constexpr size_t pchRegMaskIntrusion = 0x01; |
| 69 | |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 70 | // Value to clear intrusion status hwmon file |
| 71 | const static constexpr size_t intrusionStatusHwmonClearValue = 0; |
| 72 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 73 | void ChassisIntrusionSensor::updateValue(const size_t& value) |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 74 | { |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 75 | std::string newValue = value != 0 ? hwIntrusionValStr : normalValStr; |
| 76 | |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 77 | // Take no action if the hardware status does not change |
| Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 78 | // Same semantics as Sensor::updateValue(const double&) |
| 79 | if (newValue == mValue) |
| 80 | { |
| 81 | return; |
| 82 | } |
| 83 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 84 | lg2::debug("Update value from '{VALUE}' to '{NEWVALUE}'", "VALUE", mValue, |
| 85 | "NEWVALUE", newValue); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 86 | |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 87 | // Automatic Rearm mode allows direct update |
| 88 | // Manual Rearm mode requires a rearm action to clear the intrusion |
| 89 | // status |
| 90 | if (!mAutoRearm) |
| 91 | { |
| 92 | if (newValue == normalValStr) |
| 93 | { |
| 94 | // Chassis is first closed from being open. If it has been |
| 95 | // rearmed externally, reset the flag, update mValue and |
| 96 | // return, without having to write "Normal" to DBus property |
| 97 | // (because the rearm action already did). |
| 98 | // Otherwise, return with no more action. |
| 99 | if (mRearmFlag) |
| 100 | { |
| 101 | mRearmFlag = false; |
| 102 | mValue = newValue; |
| 103 | } |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Flush the rearm flag everytime it allows an update to Dbus |
| 109 | mRearmFlag = false; |
| 110 | |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 111 | // indicate that it is internal set call |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 112 | mOverridenState = false; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 113 | mInternalSet = true; |
| 114 | mIface->set_property("Status", newValue); |
| Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 115 | mInternalSet = false; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 116 | |
| 117 | mValue = newValue; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 120 | int ChassisIntrusionPchSensor::readSensor() |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 121 | { |
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 122 | int32_t statusMask = pchRegMaskIntrusion; |
| 123 | int32_t statusReg = pchStatusRegIntrusion; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 124 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 125 | int32_t value = i2c_smbus_read_byte_data(mBusFd, statusReg); |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 126 | lg2::debug("Pch type: raw value is '{VALUE}'", "VALUE", value); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 127 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 128 | if (value < 0) |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 129 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 130 | lg2::error("i2c_smbus_read_byte_data failed"); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | // Get status value with mask |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 135 | value &= statusMask; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 136 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 137 | lg2::debug("Pch type: masked raw value is '{VALUE}'", "VALUE", value); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 138 | return value; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 139 | } |
| 140 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 141 | void ChassisIntrusionPchSensor::pollSensorStatus() |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 142 | { |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 143 | std::weak_ptr<ChassisIntrusionPchSensor> weakRef = weak_from_this(); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 144 | |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 145 | // setting a new experation implicitly cancels any pending async wait |
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 146 | mPollTimer.expires_after(std::chrono::seconds(intrusionSensorPollSec)); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 147 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 148 | mPollTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 149 | // case of being canceled |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 150 | if (ec == boost::asio::error::operation_aborted) |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 151 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 152 | lg2::error("Timer of intrusion sensor is cancelled"); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 153 | return; |
| 154 | } |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 155 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 156 | std::shared_ptr<ChassisIntrusionPchSensor> self = weakRef.lock(); |
| 157 | if (!self) |
| 158 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 159 | lg2::error("ChassisIntrusionSensor no self"); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 160 | return; |
| 161 | } |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 162 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 163 | int value = self->readSensor(); |
| 164 | if (value < 0) |
| 165 | { |
| 166 | intrusionSensorPollSec = sensorFailedPollSec; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | intrusionSensorPollSec = defaultPollSec; |
| 171 | self->updateValue(value); |
| 172 | } |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 173 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 174 | // trigger next polling |
| 175 | self->pollSensorStatus(); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 176 | }); |
| 177 | } |
| 178 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 179 | int ChassisIntrusionGpioSensor::readSensor() |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 180 | { |
| ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 181 | mGpioLine.event_read(); |
| 182 | auto value = mGpioLine.get_value(); |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 183 | lg2::debug("Gpio type: raw value is '{VALUE}'", "VALUE", value); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 184 | return value; |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 185 | } |
| 186 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 187 | void ChassisIntrusionGpioSensor::pollSensorStatus() |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 188 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 189 | mGpioFd.async_wait( |
| 190 | boost::asio::posix::stream_descriptor::wait_read, |
| 191 | [this](const boost::system::error_code& ec) { |
| 192 | if (ec == boost::system::errc::bad_file_descriptor) |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 193 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 194 | return; // we're being destroyed |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 195 | } |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 196 | |
| 197 | if (ec) |
| 198 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 199 | lg2::error("Error on GPIO based intrusion sensor wait event"); |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | { |
| 203 | int value = readSensor(); |
| 204 | if (value >= 0) |
| 205 | { |
| 206 | updateValue(value); |
| 207 | } |
| 208 | // trigger next polling |
| 209 | pollSensorStatus(); |
| 210 | } |
| 211 | }); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 212 | } |
| 213 | |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 214 | int ChassisIntrusionHwmonSensor::readSensor() |
| 215 | { |
| 216 | int value = 0; |
| 217 | |
| 218 | std::fstream stream(mHwmonPath, std::ios::in | std::ios::out); |
| 219 | if (!stream.good()) |
| 220 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 221 | lg2::error("Error reading status at '{PATH}'", "PATH", mHwmonPath); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | std::string line; |
| 226 | if (!std::getline(stream, line)) |
| 227 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 228 | lg2::error("Error reading status at '{PATH}'", "PATH", mHwmonPath); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | try |
| 233 | { |
| 234 | value = std::stoi(line); |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 235 | lg2::debug("Hwmon type: raw value is '{VALUE}'", "VALUE", value); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 236 | } |
| 237 | catch (const std::invalid_argument& e) |
| 238 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 239 | lg2::error("Error reading status at '{PATH}': '{ERR}'", "PATH", |
| 240 | mHwmonPath, "ERR", e); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | // Reset chassis intrusion status after every reading |
| 245 | stream << intrusionStatusHwmonClearValue; |
| 246 | |
| 247 | return value; |
| 248 | } |
| 249 | |
| 250 | void ChassisIntrusionHwmonSensor::pollSensorStatus() |
| 251 | { |
| 252 | std::weak_ptr<ChassisIntrusionHwmonSensor> weakRef = weak_from_this(); |
| 253 | |
| 254 | // setting a new experation implicitly cancels any pending async wait |
| 255 | mPollTimer.expires_after(std::chrono::seconds(intrusionSensorPollSec)); |
| 256 | |
| 257 | mPollTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 258 | // case of being canceled |
| 259 | if (ec == boost::asio::error::operation_aborted) |
| 260 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 261 | lg2::error("Timer of intrusion sensor is cancelled"); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
| 265 | std::shared_ptr<ChassisIntrusionHwmonSensor> self = weakRef.lock(); |
| 266 | if (!self) |
| 267 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 268 | lg2::error("ChassisIntrusionSensor no self"); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
| 272 | int value = self->readSensor(); |
| 273 | if (value < 0) |
| 274 | { |
| 275 | intrusionSensorPollSec = sensorFailedPollSec; |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | intrusionSensorPollSec = defaultPollSec; |
| 280 | self->updateValue(value); |
| 281 | } |
| 282 | |
| 283 | // trigger next polling |
| 284 | self->pollSensorStatus(); |
| 285 | }); |
| 286 | } |
| 287 | |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 288 | int ChassisIntrusionSensor::setSensorValue(const std::string& req, |
| 289 | std::string& propertyValue) |
| 290 | { |
| Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 291 | if (!mInternalSet) |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 292 | { |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 293 | /* |
| 294 | 1. Assuming that setting property in Automatic mode causes |
| 295 | no effect but only event logs and propertiesChanged signal |
| 296 | (because the property will be updated continuously to the |
| 297 | current hardware status anyway), only update Status property |
| 298 | and raise rearm flag in Manual rearm mode. |
| 299 | 2. Only accept Normal value from an external call. |
| 300 | */ |
| 301 | if (!mAutoRearm && req == normalValStr) |
| 302 | { |
| 303 | mRearmFlag = true; |
| 304 | propertyValue = req; |
| 305 | mOverridenState = true; |
| 306 | } |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 307 | } |
| Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 308 | else if (!mOverridenState) |
| 309 | { |
| 310 | propertyValue = req; |
| 311 | } |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 312 | else |
| 313 | { |
| 314 | return 1; |
| 315 | } |
| 316 | // Send intrusion event to Redfish |
| 317 | if (mValue == normalValStr && propertyValue != normalValStr) |
| 318 | { |
| 319 | sd_journal_send("MESSAGE=%s", "Chassis intrusion assert event", |
| 320 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 321 | "OpenBMC.0.1.ChassisIntrusionDetected", NULL); |
| 322 | } |
| 323 | else if (mValue == hwIntrusionValStr && propertyValue == normalValStr) |
| 324 | { |
| 325 | sd_journal_send("MESSAGE=%s", "Chassis intrusion de-assert event", |
| 326 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 327 | "OpenBMC.0.1.ChassisIntrusionReset", NULL); |
| 328 | } |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 329 | return 1; |
| 330 | } |
| 331 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 332 | void ChassisIntrusionSensor::start() |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 333 | { |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 334 | mIface->register_property( |
| 335 | "Status", mValue, |
| 336 | [&](const std::string& req, std::string& propertyValue) { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 337 | return setSensorValue(req, propertyValue); |
| 338 | }); |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 339 | std::string rearmStr = mAutoRearm ? autoRearmStr : manualRearmStr; |
| 340 | mIface->register_property("Rearm", rearmStr); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 341 | mIface->initialize(); |
| 342 | pollSensorStatus(); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | ChassisIntrusionSensor::ChassisIntrusionSensor( |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 346 | bool autoRearm, sdbusplus::asio::object_server& objServer) : |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 347 | mValue(normalValStr), mAutoRearm(autoRearm), mObjServer(objServer) |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 348 | { |
| 349 | mIface = mObjServer.add_interface("/xyz/openbmc_project/Chassis/Intrusion", |
| 350 | "xyz.openbmc_project.Chassis.Intrusion"); |
| 351 | } |
| 352 | |
| 353 | ChassisIntrusionPchSensor::ChassisIntrusionPchSensor( |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 354 | bool autoRearm, boost::asio::io_context& io, |
| 355 | sdbusplus::asio::object_server& objServer, int busId, int slaveAddr) : |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 356 | ChassisIntrusionSensor(autoRearm, objServer), mPollTimer(io) |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 357 | { |
| 358 | if (busId < 0 || slaveAddr <= 0) |
| 359 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 360 | throw std::invalid_argument( |
| 361 | "Invalid i2c bus " + std::to_string(busId) + " address " + |
| 362 | std::to_string(slaveAddr) + "\n"); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 363 | } |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 364 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 365 | mSlaveAddr = slaveAddr; |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 366 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 367 | std::string devPath = "/dev/i2c-" + std::to_string(busId); |
| 368 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 369 | mBusFd = open(devPath.c_str(), O_RDWR | O_CLOEXEC); |
| 370 | if (mBusFd < 0) |
| 371 | { |
| 372 | throw std::invalid_argument("Unable to open " + devPath + "\n"); |
| 373 | } |
| 374 | |
| 375 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 376 | if (ioctl(mBusFd, I2C_SLAVE_FORCE, mSlaveAddr) < 0) |
| 377 | { |
| 378 | throw std::runtime_error("Unable to set device address\n"); |
| 379 | } |
| 380 | |
| 381 | unsigned long funcs = 0; |
| 382 | |
| 383 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 384 | if (ioctl(mBusFd, I2C_FUNCS, &funcs) < 0) |
| 385 | { |
| 386 | throw std::runtime_error("Don't support I2C_FUNCS\n"); |
| 387 | } |
| 388 | |
| 389 | if ((funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA) == 0U) |
| 390 | { |
| 391 | throw std::runtime_error( |
| 392 | "Do not have I2C_FUNC_SMBUS_READ_BYTE_DATA \n"); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | ChassisIntrusionGpioSensor::ChassisIntrusionGpioSensor( |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 397 | bool autoRearm, boost::asio::io_context& io, |
| 398 | sdbusplus::asio::object_server& objServer, bool gpioInverted) : |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 399 | ChassisIntrusionSensor(autoRearm, objServer), mGpioInverted(gpioInverted), |
| 400 | mGpioFd(io) |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 401 | { |
| 402 | mGpioLine = gpiod::find_line(mPinName); |
| 403 | if (!mGpioLine) |
| 404 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 405 | throw std::invalid_argument( |
| 406 | "Error finding gpio pin name: " + mPinName + "\n"); |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 407 | } |
| 408 | mGpioLine.request( |
| 409 | {"ChassisIntrusionSensor", gpiod::line_request::EVENT_BOTH_EDGES, |
| 410 | mGpioInverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0}); |
| 411 | |
| 412 | auto gpioLineFd = mGpioLine.event_get_fd(); |
| 413 | if (gpioLineFd < 0) |
| 414 | { |
| 415 | throw std::invalid_argument("Failed to get " + mPinName + " fd\n"); |
| 416 | } |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 417 | |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 418 | mGpioFd.assign(gpioLineFd); |
| 419 | } |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 420 | |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 421 | ChassisIntrusionHwmonSensor::ChassisIntrusionHwmonSensor( |
| Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 422 | bool autoRearm, boost::asio::io_context& io, |
| 423 | sdbusplus::asio::object_server& objServer, std::string hwmonName) : |
| 424 | ChassisIntrusionSensor(autoRearm, objServer), |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 425 | mHwmonName(std::move(hwmonName)), mPollTimer(io) |
| 426 | { |
| Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 427 | std::vector<std::filesystem::path> paths; |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 428 | |
| Ed Tanous | 2e46696 | 2025-01-30 10:59:49 -0800 | [diff] [blame] | 429 | if (!findFiles(std::filesystem::path("/sys/class/hwmon"), mHwmonName, |
| 430 | paths)) |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 431 | { |
| 432 | throw std::invalid_argument("Failed to find hwmon path in sysfs\n"); |
| 433 | } |
| 434 | |
| 435 | if (paths.empty()) |
| 436 | { |
| Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 437 | throw std::invalid_argument( |
| 438 | "Hwmon file " + mHwmonName + " can't be found in sysfs\n"); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (paths.size() > 1) |
| 442 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 443 | lg2::error("Found more than 1 hwmon file to read chassis intrusion" |
| 444 | " status. Taking the first one."); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Expecting only one hwmon file for one given chassis |
| 448 | mHwmonPath = paths[0].string(); |
| 449 | |
| Alexander Hansen | 89be614 | 2025-09-18 15:36:16 +0200 | [diff] [blame^] | 450 | lg2::debug( |
| 451 | "Found '{NUM_PATHS}' paths for intrusion status. The first path is: '{PATH}'", |
| 452 | "NUM_PATHS", paths.size(), "PATH", mHwmonPath); |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 455 | ChassisIntrusionSensor::~ChassisIntrusionSensor() |
| 456 | { |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 457 | mObjServer.remove_interface(mIface); |
| 458 | } |
| 459 | |
| 460 | ChassisIntrusionPchSensor::~ChassisIntrusionPchSensor() |
| 461 | { |
| 462 | mPollTimer.cancel(); |
| 463 | if (close(mBusFd) < 0) |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 464 | { |
| George Liu | e9a1c9c | 2025-02-20 15:02:35 +0800 | [diff] [blame] | 465 | lg2::error("Failed to close fd '{FD}'", "FD", mBusFd); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 466 | } |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | ChassisIntrusionGpioSensor::~ChassisIntrusionGpioSensor() |
| 470 | { |
| 471 | mGpioFd.close(); |
| 472 | if (mGpioLine) |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 473 | { |
| Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 474 | mGpioLine.release(); |
| Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 475 | } |
| 476 | } |
| Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 477 | |
| 478 | ChassisIntrusionHwmonSensor::~ChassisIntrusionHwmonSensor() |
| 479 | { |
| 480 | mPollTimer.cancel(); |
| 481 | } |