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 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 17 | #include "ChassisIntrusionSensor.hpp" |
| 18 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/ioctl.h> |
| 22 | #include <unistd.h> |
| 23 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 24 | #include <boost/asio.hpp> |
| 25 | #include <boost/bind.hpp> |
| 26 | #include <chrono> |
| 27 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 28 | #include <memory> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 29 | #include <sdbusplus/asio/object_server.hpp> |
| 30 | #include <string> |
| 31 | #include <thread> |
| 32 | |
| 33 | extern "C" { |
| 34 | #include <i2c/smbus.h> |
| 35 | #include <linux/i2c-dev.h> |
| 36 | } |
| 37 | |
| 38 | static constexpr bool DEBUG = false; |
| 39 | |
| 40 | static constexpr unsigned int intrusionSensorPollSec = 1; |
| 41 | |
| 42 | // SMLink Status Register |
| 43 | const static constexpr size_t pchStatusRegIntrusion = 0x04; |
| 44 | |
| 45 | // Status bit field masks |
| 46 | const static constexpr size_t pchRegMaskIntrusion = 0x01; |
| 47 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 48 | void ChassisIntrusionSensor::updateValue(const std::string newValue) |
| 49 | { |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 50 | // Take no action if value already equal |
| 51 | // Same semantics as Sensor::updateValue(const double&) |
| 52 | if (newValue == mValue) |
| 53 | { |
| 54 | return; |
| 55 | } |
| 56 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 57 | // indicate that it is internal set call |
| 58 | mInternalSet = true; |
| 59 | mIface->set_property("Status", newValue); |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 60 | mInternalSet = false; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 61 | |
| 62 | mValue = newValue; |
| 63 | |
| 64 | if (mOldValue == "Normal" && mValue != "Normal") |
| 65 | { |
| 66 | std::cerr << "save to SEL for intrusion assert event \n"; |
| 67 | // TODO: call add SEL log API, depends on patch #13956 |
| 68 | mOldValue = mValue; |
| 69 | } |
| 70 | else if (mOldValue != "Normal" && mValue == "Normal") |
| 71 | { |
| 72 | std::cerr << "save to SEL for intrusion de-assert event \n"; |
| 73 | // TODO: call add SEL log API, depends on patch #13956 |
| 74 | mOldValue = mValue; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | int ChassisIntrusionSensor::i2cReadFromPch(int busId, int slaveAddr) |
| 79 | { |
| 80 | std::string i2cBus = "/dev/i2c-" + std::to_string(busId); |
| 81 | |
| 82 | int fd = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 83 | if (fd < 0) |
| 84 | { |
| 85 | std::cerr << "unable to open i2c device \n"; |
| 86 | return -1; |
| 87 | } |
| 88 | if (ioctl(fd, I2C_SLAVE_FORCE, slaveAddr) < 0) |
| 89 | { |
| 90 | std::cerr << "unable to set device address\n"; |
| 91 | close(fd); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | unsigned long funcs = 0; |
| 96 | if (ioctl(fd, I2C_FUNCS, &funcs) < 0) |
| 97 | { |
| 98 | std::cerr << "not support I2C_FUNCS \n"; |
| 99 | close(fd); |
| 100 | return -1; |
| 101 | } |
| 102 | |
| 103 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) |
| 104 | { |
| 105 | std::cerr << "not support I2C_FUNC_SMBUS_READ_BYTE_DATA \n"; |
| 106 | close(fd); |
| 107 | return -1; |
| 108 | } |
| 109 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 110 | int statusValue; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 111 | unsigned int statusMask = pchRegMaskIntrusion; |
| 112 | unsigned int statusReg = pchStatusRegIntrusion; |
| 113 | |
| 114 | statusValue = i2c_smbus_read_byte_data(fd, statusReg); |
| 115 | if (DEBUG) |
| 116 | { |
| 117 | std::cout << "\nRead bus " << busId << " addr " << slaveAddr |
| 118 | << ", value = " << statusValue << "\n"; |
| 119 | } |
| 120 | |
| 121 | close(fd); |
| 122 | |
| 123 | if (statusValue < 0) |
| 124 | { |
| 125 | std::cerr << "i2c_smbus_read_byte_data failed \n"; |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | // Get status value with mask |
| 130 | int newValue = statusValue & statusMask; |
| 131 | |
| 132 | if (DEBUG) |
| 133 | { |
| 134 | std::cout << "statusValue is " << statusValue << "\n"; |
| 135 | std::cout << "Intrusion sensor value is " << newValue << "\n"; |
| 136 | } |
| 137 | |
| 138 | return newValue; |
| 139 | } |
| 140 | |
| 141 | void ChassisIntrusionSensor::pollSensorStatusByPch() |
| 142 | { |
| 143 | // setting a new experation implicitly cancels any pending async wait |
| 144 | mPollTimer.expires_from_now( |
| 145 | boost::posix_time::seconds(intrusionSensorPollSec)); |
| 146 | |
| 147 | mPollTimer.async_wait([&](const boost::system::error_code& ec) { |
| 148 | // case of timer expired |
| 149 | if (!ec) |
| 150 | { |
| 151 | int statusValue = i2cReadFromPch(mBusId, mSlaveAddr); |
| 152 | std::string newValue = statusValue ? "HardwareIntrusion" : "Normal"; |
| 153 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 154 | if (newValue != "unknown" && mValue != newValue) |
| 155 | { |
| 156 | std::cout << "update value from " << mValue << " to " |
| 157 | << newValue << "\n"; |
| 158 | updateValue(newValue); |
| 159 | } |
| 160 | |
| 161 | // trigger next polling |
| 162 | pollSensorStatusByPch(); |
| 163 | } |
| 164 | // case of being canceled |
| 165 | else if (ec == boost::asio::error::operation_aborted) |
| 166 | { |
| 167 | std::cerr << "Timer of intrusion sensor is cancelled. Return \n"; |
| 168 | return; |
| 169 | } |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | void ChassisIntrusionSensor::readGpio() |
| 174 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 175 | mGpioLine.event_read(); |
| 176 | auto value = mGpioLine.get_value(); |
| 177 | |
| 178 | // set string defined in chassis redfish schema |
| 179 | std::string newValue = value ? "HardwareIntrusion" : "Normal"; |
| 180 | |
| 181 | if (DEBUG) |
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 | std::cout << "\nGPIO value is " << value << "\n"; |
| 184 | std::cout << "Intrusion sensor value is " << newValue << "\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 185 | } |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 186 | |
| 187 | if (newValue != "unknown" && mValue != newValue) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 188 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 189 | std::cout << "update value from " << mValue << " to " << newValue |
| 190 | << "\n"; |
| 191 | updateValue(newValue); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | void ChassisIntrusionSensor::pollSensorStatusByGpio(void) |
| 196 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 197 | mGpioFd.async_wait( |
| 198 | boost::asio::posix::stream_descriptor::wait_read, |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 199 | [this](const boost::system::error_code& ec) { |
| 200 | if (ec == boost::system::errc::bad_file_descriptor) |
| 201 | { |
| 202 | return; // we're being destroyed |
| 203 | } |
| 204 | else if (ec) |
| 205 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 206 | std::cerr |
| 207 | << "Error on GPIO based intrusion sensor wait event\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 208 | } |
| 209 | else |
| 210 | { |
| 211 | readGpio(); |
| 212 | } |
| 213 | pollSensorStatusByGpio(); |
| 214 | }); |
| 215 | } |
| 216 | |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 217 | void ChassisIntrusionSensor::initGpioDeviceFile() |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 218 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 219 | mGpioLine = gpiod::find_line(mPinName); |
| 220 | if (!mGpioLine) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 221 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 222 | std::cerr << "ChassisIntrusionSensor error finding gpio pin name: " |
| 223 | << mPinName << "\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 224 | return; |
| 225 | } |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 226 | |
| 227 | try |
| 228 | { |
| 229 | |
| 230 | mGpioLine.request( |
| 231 | {"ChassisIntrusionSensor", gpiod::line_request::EVENT_BOTH_EDGES, |
| 232 | mGpioInverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0}); |
| 233 | |
| 234 | // set string defined in chassis redfish schema |
| 235 | auto value = mGpioLine.get_value(); |
| 236 | std::string newValue = value ? "HardwareIntrusion" : "Normal"; |
| 237 | updateValue(newValue); |
| 238 | |
| 239 | auto gpioLineFd = mGpioLine.event_get_fd(); |
| 240 | if (gpioLineFd < 0) |
| 241 | { |
| 242 | std::cerr << "ChassisIntrusionSensor failed to get " << mPinName |
| 243 | << " fd\n"; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | mGpioFd.assign(gpioLineFd); |
| 248 | } |
| 249 | catch (std::system_error&) |
| 250 | { |
| 251 | std::cerr << "ChassisInrtusionSensor error requesting gpio pin name: " |
| 252 | << mPinName << "\n"; |
| 253 | return; |
| 254 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | int ChassisIntrusionSensor::setSensorValue(const std::string& req, |
| 258 | std::string& propertyValue) |
| 259 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 260 | if (!mInternalSet) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 261 | { |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 262 | propertyValue = req; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 263 | mOverridenState = true; |
| 264 | } |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 265 | else if (!mOverridenState) |
| 266 | { |
| 267 | propertyValue = req; |
| 268 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | void ChassisIntrusionSensor::start(IntrusionSensorType type, int busId, |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 273 | int slaveAddr, bool gpioInverted) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 274 | { |
| 275 | if (DEBUG) |
| 276 | { |
| 277 | std::cerr << "enter ChassisIntrusionSensor::start, type = " << type |
| 278 | << "\n"; |
| 279 | if (type == IntrusionSensorType::pch) |
| 280 | { |
| 281 | std::cerr << "busId = " << busId << ", slaveAddr = " << slaveAddr |
| 282 | << "\n"; |
| 283 | } |
| 284 | else if (type == IntrusionSensorType::gpio) |
| 285 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 286 | std::cerr << "gpio pinName = " << mPinName |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 287 | << ", gpioInverted = " << gpioInverted << "\n"; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if ((type == IntrusionSensorType::pch && busId == mBusId && |
| 292 | slaveAddr == mSlaveAddr) || |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 293 | (type == IntrusionSensorType::gpio && gpioInverted == mGpioInverted && |
| 294 | mInitialized)) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 295 | { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | mType = type; |
| 300 | mBusId = busId; |
| 301 | mSlaveAddr = slaveAddr; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 302 | mGpioInverted = gpioInverted; |
| 303 | |
| 304 | if ((mType == IntrusionSensorType::pch && mBusId > 0 && mSlaveAddr > 0) || |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 305 | (mType == IntrusionSensorType::gpio)) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 306 | { |
| 307 | // initialize first if not initialized before |
| 308 | if (!mInitialized) |
| 309 | { |
| 310 | mIface->register_property( |
| 311 | "Status", mValue, |
| 312 | [&](const std::string& req, std::string& propertyValue) { |
| 313 | return setSensorValue(req, propertyValue); |
| 314 | }); |
| 315 | mIface->initialize(); |
| 316 | |
| 317 | if (mType == IntrusionSensorType::gpio) |
| 318 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 319 | initGpioDeviceFile(); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | mInitialized = true; |
| 323 | } |
| 324 | |
| 325 | // start polling value |
| 326 | if (mType == IntrusionSensorType::pch) |
| 327 | { |
| 328 | pollSensorStatusByPch(); |
| 329 | } |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 330 | else if (mType == IntrusionSensorType::gpio && mGpioLine) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 331 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 332 | std::cerr << "Start polling intrusion sensors\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 333 | pollSensorStatusByGpio(); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // invalid para, release resource |
| 338 | else |
| 339 | { |
| 340 | if (mInitialized) |
| 341 | { |
| 342 | if (mType == IntrusionSensorType::pch) |
| 343 | { |
| 344 | mPollTimer.cancel(); |
| 345 | } |
| 346 | else if (mType == IntrusionSensorType::gpio) |
| 347 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 348 | mGpioFd.close(); |
| 349 | if (mGpioLine) |
| 350 | { |
| 351 | mGpioLine.release(); |
| 352 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 353 | } |
| 354 | mInitialized = false; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | ChassisIntrusionSensor::ChassisIntrusionSensor( |
| 360 | boost::asio::io_service& io, |
| 361 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface) : |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 362 | mIface(iface), |
| 363 | mType(IntrusionSensorType::gpio), mValue("unknown"), mOldValue("unknown"), |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 364 | mBusId(-1), mSlaveAddr(-1), mPollTimer(io), mGpioInverted(false), |
| 365 | mGpioFd(io) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 366 | { |
| 367 | } |
| 368 | |
| 369 | ChassisIntrusionSensor::~ChassisIntrusionSensor() |
| 370 | { |
| 371 | if (mType == IntrusionSensorType::pch) |
| 372 | { |
| 373 | mPollTimer.cancel(); |
| 374 | } |
| 375 | else if (mType == IntrusionSensorType::gpio) |
| 376 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 377 | mGpioFd.close(); |
| 378 | if (mGpioLine) |
| 379 | { |
| 380 | mGpioLine.release(); |
| 381 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 382 | } |
| 383 | } |