blob: 15ed888998e453ba07bfac2060ec9c942b212278 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "ChassisIntrusionSensor.hpp"
18
Qiang XUe28d1fa2019-02-27 13:50:56 +080019#include <errno.h>
20#include <fcntl.h>
21#include <sys/ioctl.h>
22#include <unistd.h>
23
Qiang XUe28d1fa2019-02-27 13:50:56 +080024#include <boost/asio.hpp>
25#include <boost/bind.hpp>
26#include <chrono>
27#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <memory>
Qiang XUe28d1fa2019-02-27 13:50:56 +080029#include <sdbusplus/asio/object_server.hpp>
30#include <string>
31#include <thread>
32
33extern "C" {
34#include <i2c/smbus.h>
35#include <linux/i2c-dev.h>
36}
37
38static constexpr bool DEBUG = false;
39
40static constexpr unsigned int intrusionSensorPollSec = 1;
41
42// SMLink Status Register
43const static constexpr size_t pchStatusRegIntrusion = 0x04;
44
45// Status bit field masks
46const static constexpr size_t pchRegMaskIntrusion = 0x01;
47
Qiang XUe28d1fa2019-02-27 13:50:56 +080048void ChassisIntrusionSensor::updateValue(const std::string newValue)
49{
Josh Lehan833661a2020-03-04 17:35:41 -080050 // 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 XUe28d1fa2019-02-27 13:50:56 +080057 // indicate that it is internal set call
58 mInternalSet = true;
59 mIface->set_property("Status", newValue);
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +053060 mInternalSet = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080061
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
78int 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 Feistb6c0b912019-07-09 12:21:44 -0700110 int statusValue;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800111 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
141void 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 XUe28d1fa2019-02-27 13:50:56 +0800154 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
173void ChassisIntrusionSensor::readGpio()
174{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800175 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 XUe28d1fa2019-02-27 13:50:56 +0800182 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800183 std::cout << "\nGPIO value is " << value << "\n";
184 std::cout << "Intrusion sensor value is " << newValue << "\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800185 }
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800186
187 if (newValue != "unknown" && mValue != newValue)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800188 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800189 std::cout << "update value from " << mValue << " to " << newValue
190 << "\n";
191 updateValue(newValue);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800192 }
193}
194
195void ChassisIntrusionSensor::pollSensorStatusByGpio(void)
196{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800197 mGpioFd.async_wait(
198 boost::asio::posix::stream_descriptor::wait_read,
Qiang XUe28d1fa2019-02-27 13:50:56 +0800199 [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 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800206 std::cerr
207 << "Error on GPIO based intrusion sensor wait event\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800208 }
209 else
210 {
211 readGpio();
212 }
213 pollSensorStatusByGpio();
214 });
215}
216
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800217void ChassisIntrusionSensor::initGpioDeviceFile()
Qiang XUe28d1fa2019-02-27 13:50:56 +0800218{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800219 mGpioLine = gpiod::find_line(mPinName);
220 if (!mGpioLine)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800221 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800222 std::cerr << "ChassisIntrusionSensor error finding gpio pin name: "
223 << mPinName << "\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800224 return;
225 }
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800226
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 XUe28d1fa2019-02-27 13:50:56 +0800255}
256
257int ChassisIntrusionSensor::setSensorValue(const std::string& req,
258 std::string& propertyValue)
259{
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530260 if (!mInternalSet)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800261 {
Qiang XUe28d1fa2019-02-27 13:50:56 +0800262 propertyValue = req;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800263 mOverridenState = true;
264 }
Richard Marian Thomaiyaraf6b87c2019-04-03 23:54:28 +0530265 else if (!mOverridenState)
266 {
267 propertyValue = req;
268 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800269 return 1;
270}
271
272void ChassisIntrusionSensor::start(IntrusionSensorType type, int busId,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800273 int slaveAddr, bool gpioInverted)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800274{
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 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800286 std::cerr << "gpio pinName = " << mPinName
Qiang XUe28d1fa2019-02-27 13:50:56 +0800287 << ", gpioInverted = " << gpioInverted << "\n";
288 }
289 }
290
291 if ((type == IntrusionSensorType::pch && busId == mBusId &&
292 slaveAddr == mSlaveAddr) ||
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800293 (type == IntrusionSensorType::gpio && gpioInverted == mGpioInverted &&
294 mInitialized))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800295 {
296 return;
297 }
298
299 mType = type;
300 mBusId = busId;
301 mSlaveAddr = slaveAddr;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800302 mGpioInverted = gpioInverted;
303
304 if ((mType == IntrusionSensorType::pch && mBusId > 0 && mSlaveAddr > 0) ||
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800305 (mType == IntrusionSensorType::gpio))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800306 {
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 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800319 initGpioDeviceFile();
Qiang XUe28d1fa2019-02-27 13:50:56 +0800320 }
321
322 mInitialized = true;
323 }
324
325 // start polling value
326 if (mType == IntrusionSensorType::pch)
327 {
328 pollSensorStatusByPch();
329 }
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800330 else if (mType == IntrusionSensorType::gpio && mGpioLine)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800331 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800332 std::cerr << "Start polling intrusion sensors\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800333 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 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800348 mGpioFd.close();
349 if (mGpioLine)
350 {
351 mGpioLine.release();
352 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800353 }
354 mInitialized = false;
355 }
356 }
357}
358
359ChassisIntrusionSensor::ChassisIntrusionSensor(
360 boost::asio::io_service& io,
361 std::shared_ptr<sdbusplus::asio::dbus_interface> iface) :
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500362 mIface(iface),
363 mType(IntrusionSensorType::gpio), mValue("unknown"), mOldValue("unknown"),
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800364 mBusId(-1), mSlaveAddr(-1), mPollTimer(io), mGpioInverted(false),
365 mGpioFd(io)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800366{
367}
368
369ChassisIntrusionSensor::~ChassisIntrusionSensor()
370{
371 if (mType == IntrusionSensorType::pch)
372 {
373 mPollTimer.cancel();
374 }
375 else if (mType == IntrusionSensorType::gpio)
376 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800377 mGpioFd.close();
378 if (mGpioLine)
379 {
380 mGpioLine.release();
381 }
Qiang XUe28d1fa2019-02-27 13:50:56 +0800382 }
383}