blob: 9b35da205d4c2ba47a471ae211effa9ed44c975c [file] [log] [blame]
Qiang XUe28d1fa2019-02-27 13:50:56 +08001/*
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
Qiang XUe28d1fa2019-02-27 13:50:56 +080017#include <fcntl.h>
18#include <sys/ioctl.h>
19#include <unistd.h>
20
Ed Tanous8a57ec02020-10-09 12:46:52 -070021#include <ChassisIntrusionSensor.hpp>
22#include <boost/asio/io_service.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/object_server.hpp>
24
Ed Tanous8a57ec02020-10-09 12:46:52 -070025#include <cerrno>
Qiang XUe28d1fa2019-02-27 13:50:56 +080026#include <chrono>
27#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <memory>
Qiang XUe28d1fa2019-02-27 13:50:56 +080029#include <string>
30#include <thread>
Ed Tanous8a57ec02020-10-09 12:46:52 -070031#include <utility>
Qiang XUe28d1fa2019-02-27 13:50:56 +080032
James Feist38fb5982020-05-28 10:09:54 -070033extern "C"
34{
Qiang XUe28d1fa2019-02-27 13:50:56 +080035#include <i2c/smbus.h>
36#include <linux/i2c-dev.h>
37}
38
Ed Tanous8a57ec02020-10-09 12:46:52 -070039static constexpr bool debug = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080040
41static constexpr unsigned int intrusionSensorPollSec = 1;
42
43// SMLink Status Register
44const static constexpr size_t pchStatusRegIntrusion = 0x04;
45
46// Status bit field masks
47const static constexpr size_t pchRegMaskIntrusion = 0x01;
48
Ed Tanous8a57ec02020-10-09 12:46:52 -070049void ChassisIntrusionSensor::updateValue(const std::string& newValue)
Qiang XUe28d1fa2019-02-27 13:50:56 +080050{
Josh Lehan833661a2020-03-04 17:35:41 -080051 // Take no action if value already equal
52 // Same semantics as Sensor::updateValue(const double&)
53 if (newValue == mValue)
54 {
55 return;
56 }
57
Qiang XUe28d1fa2019-02-27 13:50:56 +080058 // indicate that it is internal set call
59 mInternalSet = true;
60 mIface->set_property("Status", newValue);
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053061 mInternalSet = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080062
63 mValue = newValue;
64
65 if (mOldValue == "Normal" && mValue != "Normal")
66 {
67 std::cerr << "save to SEL for intrusion assert event \n";
68 // TODO: call add SEL log API, depends on patch #13956
69 mOldValue = mValue;
70 }
71 else if (mOldValue != "Normal" && mValue == "Normal")
72 {
73 std::cerr << "save to SEL for intrusion de-assert event \n";
74 // TODO: call add SEL log API, depends on patch #13956
75 mOldValue = mValue;
76 }
77}
78
79int ChassisIntrusionSensor::i2cReadFromPch(int busId, int slaveAddr)
80{
81 std::string i2cBus = "/dev/i2c-" + std::to_string(busId);
82
Ed Tanous99c44092022-01-14 09:59:09 -080083 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Qiang XUe28d1fa2019-02-27 13:50:56 +080084 int fd = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
85 if (fd < 0)
86 {
87 std::cerr << "unable to open i2c device \n";
88 return -1;
89 }
Ed Tanous99c44092022-01-14 09:59:09 -080090
91 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Qiang XUe28d1fa2019-02-27 13:50:56 +080092 if (ioctl(fd, I2C_SLAVE_FORCE, slaveAddr) < 0)
93 {
94 std::cerr << "unable to set device address\n";
95 close(fd);
96 return -1;
97 }
98
99 unsigned long funcs = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800100
101 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800102 if (ioctl(fd, I2C_FUNCS, &funcs) < 0)
103 {
104 std::cerr << "not support I2C_FUNCS \n";
105 close(fd);
106 return -1;
107 }
108
109 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA))
110 {
111 std::cerr << "not support I2C_FUNC_SMBUS_READ_BYTE_DATA \n";
112 close(fd);
113 return -1;
114 }
115
Ed Tanous8a57ec02020-10-09 12:46:52 -0700116 int32_t statusMask = pchRegMaskIntrusion;
117 int32_t statusReg = pchStatusRegIntrusion;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800118
Ed Tanous8a57ec02020-10-09 12:46:52 -0700119 int32_t statusValue = i2c_smbus_read_byte_data(fd, statusReg);
120 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800121 {
122 std::cout << "\nRead bus " << busId << " addr " << slaveAddr
123 << ", value = " << statusValue << "\n";
124 }
125
126 close(fd);
127
128 if (statusValue < 0)
129 {
130 std::cerr << "i2c_smbus_read_byte_data failed \n";
131 return -1;
132 }
133
134 // Get status value with mask
135 int newValue = statusValue & statusMask;
136
Ed Tanous8a57ec02020-10-09 12:46:52 -0700137 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800138 {
139 std::cout << "statusValue is " << statusValue << "\n";
140 std::cout << "Intrusion sensor value is " << newValue << "\n";
141 }
142
143 return newValue;
144}
145
146void ChassisIntrusionSensor::pollSensorStatusByPch()
147{
148 // setting a new experation implicitly cancels any pending async wait
149 mPollTimer.expires_from_now(
150 boost::posix_time::seconds(intrusionSensorPollSec));
151
152 mPollTimer.async_wait([&](const boost::system::error_code& ec) {
153 // case of timer expired
154 if (!ec)
155 {
156 int statusValue = i2cReadFromPch(mBusId, mSlaveAddr);
157 std::string newValue = statusValue ? "HardwareIntrusion" : "Normal";
158
Qiang XUe28d1fa2019-02-27 13:50:56 +0800159 if (newValue != "unknown" && mValue != newValue)
160 {
161 std::cout << "update value from " << mValue << " to "
162 << newValue << "\n";
163 updateValue(newValue);
164 }
165
166 // trigger next polling
167 pollSensorStatusByPch();
168 }
169 // case of being canceled
170 else if (ec == boost::asio::error::operation_aborted)
171 {
172 std::cerr << "Timer of intrusion sensor is cancelled. Return \n";
173 return;
174 }
175 });
176}
177
178void ChassisIntrusionSensor::readGpio()
179{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800180 mGpioLine.event_read();
181 auto value = mGpioLine.get_value();
182
183 // set string defined in chassis redfish schema
184 std::string newValue = value ? "HardwareIntrusion" : "Normal";
185
Ed Tanous8a57ec02020-10-09 12:46:52 -0700186 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800187 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800188 std::cout << "\nGPIO value is " << value << "\n";
189 std::cout << "Intrusion sensor value is " << newValue << "\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800190 }
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800191
192 if (newValue != "unknown" && mValue != newValue)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800193 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800194 std::cout << "update value from " << mValue << " to " << newValue
195 << "\n";
196 updateValue(newValue);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800197 }
198}
199
200void ChassisIntrusionSensor::pollSensorStatusByGpio(void)
201{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800202 mGpioFd.async_wait(
203 boost::asio::posix::stream_descriptor::wait_read,
Qiang XUe28d1fa2019-02-27 13:50:56 +0800204 [this](const boost::system::error_code& ec) {
205 if (ec == boost::system::errc::bad_file_descriptor)
206 {
207 return; // we're being destroyed
208 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700209 if (ec)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800210 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800211 std::cerr
212 << "Error on GPIO based intrusion sensor wait event\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800213 }
214 else
215 {
216 readGpio();
217 }
218 pollSensorStatusByGpio();
219 });
220}
221
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800222void ChassisIntrusionSensor::initGpioDeviceFile()
Qiang XUe28d1fa2019-02-27 13:50:56 +0800223{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800224 mGpioLine = gpiod::find_line(mPinName);
225 if (!mGpioLine)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800226 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800227 std::cerr << "ChassisIntrusionSensor error finding gpio pin name: "
228 << mPinName << "\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800229 return;
230 }
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800231
232 try
233 {
234
235 mGpioLine.request(
236 {"ChassisIntrusionSensor", gpiod::line_request::EVENT_BOTH_EDGES,
237 mGpioInverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
238
239 // set string defined in chassis redfish schema
240 auto value = mGpioLine.get_value();
241 std::string newValue = value ? "HardwareIntrusion" : "Normal";
242 updateValue(newValue);
243
244 auto gpioLineFd = mGpioLine.event_get_fd();
245 if (gpioLineFd < 0)
246 {
247 std::cerr << "ChassisIntrusionSensor failed to get " << mPinName
248 << " fd\n";
249 return;
250 }
251
252 mGpioFd.assign(gpioLineFd);
253 }
Patrick Williams26601e82021-10-06 12:43:25 -0500254 catch (const std::system_error&)
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800255 {
256 std::cerr << "ChassisInrtusionSensor error requesting gpio pin name: "
257 << mPinName << "\n";
258 return;
259 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800260}
261
262int ChassisIntrusionSensor::setSensorValue(const std::string& req,
263 std::string& propertyValue)
264{
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530265 if (!mInternalSet)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800266 {
Qiang XUe28d1fa2019-02-27 13:50:56 +0800267 propertyValue = req;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800268 mOverridenState = true;
269 }
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530270 else if (!mOverridenState)
271 {
272 propertyValue = req;
273 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800274 return 1;
275}
276
277void ChassisIntrusionSensor::start(IntrusionSensorType type, int busId,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800278 int slaveAddr, bool gpioInverted)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800279{
Ed Tanous8a57ec02020-10-09 12:46:52 -0700280 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800281 {
282 std::cerr << "enter ChassisIntrusionSensor::start, type = " << type
283 << "\n";
284 if (type == IntrusionSensorType::pch)
285 {
286 std::cerr << "busId = " << busId << ", slaveAddr = " << slaveAddr
287 << "\n";
288 }
289 else if (type == IntrusionSensorType::gpio)
290 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800291 std::cerr << "gpio pinName = " << mPinName
Qiang XUe28d1fa2019-02-27 13:50:56 +0800292 << ", gpioInverted = " << gpioInverted << "\n";
293 }
294 }
295
296 if ((type == IntrusionSensorType::pch && busId == mBusId &&
297 slaveAddr == mSlaveAddr) ||
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800298 (type == IntrusionSensorType::gpio && gpioInverted == mGpioInverted &&
299 mInitialized))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800300 {
301 return;
302 }
303
304 mType = type;
305 mBusId = busId;
306 mSlaveAddr = slaveAddr;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800307 mGpioInverted = gpioInverted;
308
309 if ((mType == IntrusionSensorType::pch && mBusId > 0 && mSlaveAddr > 0) ||
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800310 (mType == IntrusionSensorType::gpio))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800311 {
312 // initialize first if not initialized before
313 if (!mInitialized)
314 {
315 mIface->register_property(
316 "Status", mValue,
317 [&](const std::string& req, std::string& propertyValue) {
318 return setSensorValue(req, propertyValue);
319 });
320 mIface->initialize();
321
322 if (mType == IntrusionSensorType::gpio)
323 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800324 initGpioDeviceFile();
Qiang XUe28d1fa2019-02-27 13:50:56 +0800325 }
326
327 mInitialized = true;
328 }
329
330 // start polling value
331 if (mType == IntrusionSensorType::pch)
332 {
333 pollSensorStatusByPch();
334 }
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800335 else if (mType == IntrusionSensorType::gpio && mGpioLine)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800336 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800337 std::cerr << "Start polling intrusion sensors\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800338 pollSensorStatusByGpio();
339 }
340 }
341
342 // invalid para, release resource
343 else
344 {
345 if (mInitialized)
346 {
347 if (mType == IntrusionSensorType::pch)
348 {
349 mPollTimer.cancel();
350 }
351 else if (mType == IntrusionSensorType::gpio)
352 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800353 mGpioFd.close();
354 if (mGpioLine)
355 {
356 mGpioLine.release();
357 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800358 }
359 mInitialized = false;
360 }
361 }
362}
363
364ChassisIntrusionSensor::ChassisIntrusionSensor(
365 boost::asio::io_service& io,
366 std::shared_ptr<sdbusplus::asio::dbus_interface> iface) :
Ed Tanous8a57ec02020-10-09 12:46:52 -0700367 mIface(std::move(iface)),
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500368 mType(IntrusionSensorType::gpio), mValue("unknown"), mOldValue("unknown"),
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800369 mBusId(-1), mSlaveAddr(-1), mPollTimer(io), mGpioInverted(false),
370 mGpioFd(io)
James Feist38fb5982020-05-28 10:09:54 -0700371{}
Qiang XUe28d1fa2019-02-27 13:50:56 +0800372
373ChassisIntrusionSensor::~ChassisIntrusionSensor()
374{
375 if (mType == IntrusionSensorType::pch)
376 {
377 mPollTimer.cancel();
378 }
379 else if (mType == IntrusionSensorType::gpio)
380 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800381 mGpioFd.close();
382 if (mGpioLine)
383 {
384 mGpioLine.release();
385 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800386 }
387}