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