blob: cf9ff7568201998715d29775e89dc1e3444554be [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 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
Bruce Lee1263c3d2021-06-04 15:16:33 +080017#include "dbus-sensor_config.h"
18
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <Utils.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070020#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070021#include <sdbusplus/asio/connection.hpp>
22#include <sdbusplus/asio/object_server.hpp>
23#include <sdbusplus/bus/match.hpp>
24
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <memory>
James Feist6714a252018-09-10 15:26:18 -070028#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <stdexcept>
30#include <string>
31#include <utility>
32#include <variant>
33#include <vector>
James Feist6714a252018-09-10 15:26:18 -070034
James Feistcf3bce62019-01-08 10:07:19 -080035namespace fs = std::filesystem;
James Feist6714a252018-09-10 15:26:18 -070036
James Feist6ef20402019-01-07 16:45:08 -080037static bool powerStatusOn = false;
James Feistfc94b212019-02-06 16:14:51 -080038static bool biosHasPost = false;
Bruce Lee1263c3d2021-06-04 15:16:33 +080039static bool manufacturingMode = false;
James Feist58295ad2019-05-30 15:01:41 -070040
Patrick Williams92f8f512022-07-22 19:26:55 -050041static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
42static std::unique_ptr<sdbusplus::bus::match_t> postMatch = nullptr;
James Feist71d31b22019-01-02 16:57:54 -080043
Jason Ling100c20b2020-08-11 14:50:33 -070044/**
45 * return the contents of a file
46 * @param[in] hwmonFile - the path to the file to read
47 * @return the contents of the file as a string or nullopt if the file could not
48 * be opened.
49 */
50
51std::optional<std::string> openAndRead(const std::string& hwmonFile)
52{
53 std::string fileVal;
54 std::ifstream fileStream(hwmonFile);
55 if (!fileStream.is_open())
56 {
57 return std::nullopt;
58 }
59 std::getline(fileStream, fileVal);
60 return fileVal;
61}
62
63/**
64 * given a hwmon temperature base name if valid return the full path else
65 * nullopt
66 * @param[in] directory - the hwmon sysfs directory
67 * @param[in] permitSet - a set of labels or hwmon basenames to permit. If this
68 * is empty then *everything* is permitted.
69 * @return a string to the full path of the file to create a temp sensor with or
70 * nullopt to indicate that no sensor should be created for this basename.
71 */
72std::optional<std::string>
73 getFullHwmonFilePath(const std::string& directory,
74 const std::string& hwmonBaseName,
75 const std::set<std::string>& permitSet)
76{
77 std::optional<std::string> result;
78 std::string filename;
79 if (permitSet.empty())
80 {
81 result = directory + "/" + hwmonBaseName + "_input";
82 return result;
83 }
84 filename = directory + "/" + hwmonBaseName + "_label";
85 auto searchVal = openAndRead(filename);
86 if (!searchVal)
87 {
88 /* if the hwmon temp doesn't have a corresponding label file
89 * then use the hwmon temperature base name
90 */
91 searchVal = hwmonBaseName;
92 }
93 if (permitSet.find(*searchVal) != permitSet.end())
94 {
95 result = directory + "/" + hwmonBaseName + "_input";
96 }
97 return result;
98}
99
100/**
101 * retrieve a set of basenames and labels to allow sensor creation for.
102 * @param[in] config - a map representing the configuration for a specific
103 * device
104 * @return a set of basenames and labels to allow sensor creation for. An empty
105 * set indicates that everything is permitted.
106 */
107std::set<std::string> getPermitSet(const SensorBaseConfigMap& config)
108{
109 auto permitAttribute = config.find("Labels");
110 std::set<std::string> permitSet;
111 if (permitAttribute != config.end())
112 {
113 try
114 {
115 auto val =
116 std::get<std::vector<std::string>>(permitAttribute->second);
117
118 permitSet.insert(std::make_move_iterator(val.begin()),
119 std::make_move_iterator(val.end()));
120 }
121 catch (const std::bad_variant_access& err)
122 {
123 std::cerr << err.what()
124 << ":PermitList does not contain a list, wrong "
125 "variant type.\n";
126 }
127 }
128 return permitSet;
129}
130
James Feist6714a252018-09-10 15:26:18 -0700131bool getSensorConfiguration(
132 const std::string& type,
133 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700134 ManagedObjectType& resp)
135{
136 return getSensorConfiguration(type, dbusConnection, resp, false);
137}
138
139bool getSensorConfiguration(
140 const std::string& type,
141 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist6714a252018-09-10 15:26:18 -0700142 ManagedObjectType& resp, bool useCache)
143{
144 static ManagedObjectType managedObj;
145
146 if (!useCache)
147 {
148 managedObj.clear();
Patrick Williams92f8f512022-07-22 19:26:55 -0500149 sdbusplus::message_t getManagedObjects =
James Feist6714a252018-09-10 15:26:18 -0700150 dbusConnection->new_method_call(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700151 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
James Feist6714a252018-09-10 15:26:18 -0700152 "GetManagedObjects");
James Feist6714a252018-09-10 15:26:18 -0700153 try
154 {
Patrick Williams92f8f512022-07-22 19:26:55 -0500155 sdbusplus::message_t reply =
James Feist6714a252018-09-10 15:26:18 -0700156 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -0700157 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -0700158 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500159 catch (const sdbusplus::exception_t& e)
James Feist6714a252018-09-10 15:26:18 -0700160 {
Jason Ling5747fab2019-10-02 16:46:23 -0700161 std::cerr << "While calling GetManagedObjects on service:"
162 << entityManagerName << " exception name:" << e.name()
163 << "and description:" << e.description()
164 << " was thrown\n";
James Feist6714a252018-09-10 15:26:18 -0700165 return false;
166 }
167 }
168 for (const auto& pathPair : managedObj)
169 {
Zev Weissfd3d5f72022-08-12 18:21:02 -0700170 for (const auto& [intf, cfg] : pathPair.second)
James Feist6714a252018-09-10 15:26:18 -0700171 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700172 if (intf.starts_with(type))
James Feist6714a252018-09-10 15:26:18 -0700173 {
Zev Weiss79d8aef2022-08-17 21:45:44 -0700174 resp.emplace(pathPair);
James Feist6714a252018-09-10 15:26:18 -0700175 break;
176 }
177 }
James Feist6714a252018-09-10 15:26:18 -0700178 }
179 return true;
180}
181
Lei YU6a4e9732021-10-20 13:27:34 +0800182bool findFiles(const fs::path& dirPath, std::string_view matchString,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700183 std::vector<fs::path>& foundPaths, int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700184{
Lei YU6a4e9732021-10-20 13:27:34 +0800185 std::error_code ec;
186 if (!fs::exists(dirPath, ec))
Ed Tanous8a57ec02020-10-09 12:46:52 -0700187 {
James Feist6714a252018-09-10 15:26:18 -0700188 return false;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700189 }
James Feist6714a252018-09-10 15:26:18 -0700190
Lei YU6a4e9732021-10-20 13:27:34 +0800191 std::vector<std::regex> matchPieces;
192
193 size_t pos = 0;
194 std::string token;
195 // Generate the regex expressions list from the match we were given
196 while ((pos = matchString.find('/')) != std::string::npos)
197 {
198 token = matchString.substr(0, pos);
199 matchPieces.emplace_back(token);
200 matchString.remove_prefix(pos + 1);
201 }
202 matchPieces.emplace_back(std::string{matchString});
203
204 // Check if the match string contains directories, and skip the match of
205 // subdirectory if not
206 if (matchPieces.size() <= 1)
207 {
208 std::regex search(std::string{matchString});
209 std::smatch match;
210 for (auto p = fs::recursive_directory_iterator(
211 dirPath, fs::directory_options::follow_directory_symlink);
212 p != fs::recursive_directory_iterator(); ++p)
213 {
214 std::string path = p->path().string();
215 if (!is_directory(*p))
216 {
217 if (std::regex_search(path, match, search))
218 {
219 foundPaths.emplace_back(p->path());
220 }
221 }
222 if (p.depth() >= symlinkDepth)
223 {
224 p.disable_recursion_pending();
225 }
226 }
227 return true;
228 }
229
230 // The match string contains directories, verify each level of sub
231 // directories
Ed Tanous8a57ec02020-10-09 12:46:52 -0700232 for (auto p = fs::recursive_directory_iterator(
233 dirPath, fs::directory_options::follow_directory_symlink);
234 p != fs::recursive_directory_iterator(); ++p)
James Feist6714a252018-09-10 15:26:18 -0700235 {
Lei YU6a4e9732021-10-20 13:27:34 +0800236 std::vector<std::regex>::iterator matchPiece = matchPieces.begin();
237 fs::path::iterator pathIt = p->path().begin();
238 for (const fs::path& dir : dirPath)
239 {
240 if (dir.empty())
241 {
242 // When the path ends with '/', it gets am empty path
243 // skip such case.
244 break;
245 }
246 pathIt++;
247 }
248
249 while (pathIt != p->path().end())
250 {
251 // Found a path deeper than match.
252 if (matchPiece == matchPieces.end())
253 {
254 p.disable_recursion_pending();
255 break;
256 }
257 std::smatch match;
258 std::string component = pathIt->string();
259 std::regex regexPiece(*matchPiece);
260 if (!std::regex_match(component, match, regexPiece))
261 {
262 // path prefix doesn't match, no need to iterate further
263 p.disable_recursion_pending();
264 break;
265 }
266 matchPiece++;
267 pathIt++;
268 }
269
Ed Tanous8a57ec02020-10-09 12:46:52 -0700270 if (!is_directory(*p))
James Feist6714a252018-09-10 15:26:18 -0700271 {
Lei YU6a4e9732021-10-20 13:27:34 +0800272 if (matchPiece == matchPieces.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -0700273 {
274 foundPaths.emplace_back(p->path());
275 }
James Feist6714a252018-09-10 15:26:18 -0700276 }
Lei YU6a4e9732021-10-20 13:27:34 +0800277
Ed Tanous8a57ec02020-10-09 12:46:52 -0700278 if (p.depth() >= symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700279 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700280 p.disable_recursion_pending();
James Feist6714a252018-09-10 15:26:18 -0700281 }
282 }
283 return true;
284}
285
James Feist71d31b22019-01-02 16:57:54 -0800286bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700287{
James Feist71d31b22019-01-02 16:57:54 -0800288 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700289 {
James Feist71d31b22019-01-02 16:57:54 -0800290 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700291 }
James Feist71d31b22019-01-02 16:57:54 -0800292 return powerStatusOn;
293}
294
James Feistfc94b212019-02-06 16:14:51 -0800295bool hasBiosPost(void)
296{
James Feist52497fd2019-06-07 13:01:33 -0700297 if (!postMatch)
James Feistfc94b212019-02-06 16:14:51 -0800298 {
James Feist52497fd2019-06-07 13:01:33 -0700299 throw std::runtime_error("Post Match Not Created");
James Feistfc94b212019-02-06 16:14:51 -0800300 }
301 return biosHasPost;
302}
303
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000304bool readingStateGood(const PowerState& powerState)
305{
306 if (powerState == PowerState::on && !isPowerOn())
307 {
308 return false;
309 }
310 if (powerState == PowerState::biosPost && (!hasBiosPost() || !isPowerOn()))
311 {
312 return false;
313 }
314
315 return true;
316}
317
James Feist8aeffd92020-09-14 12:08:28 -0700318static void
319 getPowerStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
320 size_t retries = 2)
321{
322 conn->async_method_call(
323 [conn, retries](boost::system::error_code ec,
324 const std::variant<std::string>& state) {
Ed Tanousbb679322022-05-16 16:10:00 -0700325 if (ec)
326 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700327 if (retries != 0U)
James Feist8aeffd92020-09-14 12:08:28 -0700328 {
Ed Tanousbb679322022-05-16 16:10:00 -0700329 auto timer = std::make_shared<boost::asio::steady_timer>(
330 conn->get_io_context());
331 timer->expires_after(std::chrono::seconds(15));
332 timer->async_wait(
333 [timer, conn, retries](boost::system::error_code) {
334 getPowerStatus(conn, retries - 1);
335 });
James Feist8aeffd92020-09-14 12:08:28 -0700336 return;
337 }
Ed Tanousbb679322022-05-16 16:10:00 -0700338
339 // we commonly come up before power control, we'll capture the
340 // property change later
341 std::cerr << "error getting power status " << ec.message() << "\n";
342 return;
343 }
Zev Weiss6c106d62022-08-17 20:50:00 -0700344 powerStatusOn = std::get<std::string>(state).ends_with(".Running");
James Feist8aeffd92020-09-14 12:08:28 -0700345 },
346 power::busname, power::path, properties::interface, properties::get,
347 power::interface, power::property);
348}
349
350static void
351 getPostStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
352 size_t retries = 2)
353{
354 conn->async_method_call(
355 [conn, retries](boost::system::error_code ec,
356 const std::variant<std::string>& state) {
Ed Tanousbb679322022-05-16 16:10:00 -0700357 if (ec)
358 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700359 if (retries != 0U)
James Feist8aeffd92020-09-14 12:08:28 -0700360 {
Ed Tanousbb679322022-05-16 16:10:00 -0700361 auto timer = std::make_shared<boost::asio::steady_timer>(
362 conn->get_io_context());
363 timer->expires_after(std::chrono::seconds(15));
364 timer->async_wait(
365 [timer, conn, retries](boost::system::error_code) {
366 getPostStatus(conn, retries - 1);
367 });
James Feist8aeffd92020-09-14 12:08:28 -0700368 return;
369 }
Ed Tanousbb679322022-05-16 16:10:00 -0700370 // we commonly come up before power control, we'll capture the
371 // property change later
372 std::cerr << "error getting post status " << ec.message() << "\n";
373 return;
374 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700375 const auto& value = std::get<std::string>(state);
Ed Tanousbb679322022-05-16 16:10:00 -0700376 biosHasPost = (value != "Inactive") &&
377 (value != "xyz.openbmc_project.State.OperatingSystem."
378 "Status.OSStatus.Inactive");
James Feist8aeffd92020-09-14 12:08:28 -0700379 },
380 post::busname, post::path, properties::interface, properties::get,
381 post::interface, post::property);
382}
383
Zev Weiss88cb29d2022-05-09 03:46:15 +0000384void setupPowerMatchCallback(
385 const std::shared_ptr<sdbusplus::asio::connection>& conn,
386 std::function<void(PowerState type, bool state)>&& hostStatusCallback)
James Feist71d31b22019-01-02 16:57:54 -0800387{
James Feist43d32fe2019-09-04 10:35:20 -0700388 static boost::asio::steady_timer timer(conn->get_io_context());
James Feist6714a252018-09-10 15:26:18 -0700389 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800390 // cache the correct value
James Feist52497fd2019-06-07 13:01:33 -0700391 if (powerMatch)
392 {
393 return;
394 }
James Feist6714a252018-09-10 15:26:18 -0700395
Patrick Williams92f8f512022-07-22 19:26:55 -0500396 powerMatch = std::make_unique<sdbusplus::bus::match_t>(
397 static_cast<sdbusplus::bus_t&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700398 "type='signal',interface='" + std::string(properties::interface) +
399 "',path='" + std::string(power::path) + "',arg0='" +
400 std::string(power::interface) + "'",
Zev Weiss88cb29d2022-05-09 03:46:15 +0000401 [hostStatusCallback](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700402 std::string objectName;
403 boost::container::flat_map<std::string, std::variant<std::string>>
404 values;
405 message.read(objectName, values);
406 auto findState = values.find(power::property);
407 if (findState != values.end())
408 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700409 bool on =
410 std::get<std::string>(findState->second).ends_with(".Running");
Ed Tanousbb679322022-05-16 16:10:00 -0700411 if (!on)
James Feist6714a252018-09-10 15:26:18 -0700412 {
Ed Tanousbb679322022-05-16 16:10:00 -0700413 timer.cancel();
414 powerStatusOn = false;
Zev Weiss88cb29d2022-05-09 03:46:15 +0000415 hostStatusCallback(PowerState::on, powerStatusOn);
Ed Tanousbb679322022-05-16 16:10:00 -0700416 return;
417 }
418 // on comes too quickly
419 timer.expires_after(std::chrono::seconds(10));
Zev Weiss88cb29d2022-05-09 03:46:15 +0000420 timer.async_wait(
421 [hostStatusCallback](boost::system::error_code ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700422 if (ec == boost::asio::error::operation_aborted)
James Feist43d32fe2019-09-04 10:35:20 -0700423 {
James Feist43d32fe2019-09-04 10:35:20 -0700424 return;
425 }
Ed Tanousbb679322022-05-16 16:10:00 -0700426 if (ec)
427 {
428 std::cerr << "Timer error " << ec.message() << "\n";
429 return;
430 }
431 powerStatusOn = true;
Zev Weiss88cb29d2022-05-09 03:46:15 +0000432 hostStatusCallback(PowerState::on, powerStatusOn);
Ed Tanousbb679322022-05-16 16:10:00 -0700433 });
434 }
James Feist52497fd2019-06-07 13:01:33 -0700435 });
436
Patrick Williams92f8f512022-07-22 19:26:55 -0500437 postMatch = std::make_unique<sdbusplus::bus::match_t>(
438 static_cast<sdbusplus::bus_t&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700439 "type='signal',interface='" + std::string(properties::interface) +
440 "',path='" + std::string(post::path) + "',arg0='" +
441 std::string(post::interface) + "'",
Zev Weiss88cb29d2022-05-09 03:46:15 +0000442 [hostStatusCallback](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700443 std::string objectName;
444 boost::container::flat_map<std::string, std::variant<std::string>>
445 values;
446 message.read(objectName, values);
447 auto findState = values.find(post::property);
448 if (findState != values.end())
449 {
450 auto& value = std::get<std::string>(findState->second);
451 biosHasPost = (value != "Inactive") &&
452 (value != "xyz.openbmc_project.State.OperatingSystem."
453 "Status.OSStatus.Inactive");
Zev Weiss88cb29d2022-05-09 03:46:15 +0000454 hostStatusCallback(PowerState::biosPost, biosHasPost);
Ed Tanousbb679322022-05-16 16:10:00 -0700455 }
James Feist52497fd2019-06-07 13:01:33 -0700456 });
James Feistfc94b212019-02-06 16:14:51 -0800457
James Feist8aeffd92020-09-14 12:08:28 -0700458 getPowerStatus(conn);
459 getPostStatus(conn);
James Feist3f0e8762018-11-27 11:30:42 -0800460}
James Feist87d713a2018-12-06 16:06:24 -0800461
Zev Weiss88cb29d2022-05-09 03:46:15 +0000462void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
463{
464 setupPowerMatchCallback(conn, [](PowerState, bool) {});
465}
466
James Feist87d713a2018-12-06 16:06:24 -0800467// replaces limits if MinReading and MaxReading are found.
468void findLimits(std::pair<double, double>& limits,
469 const SensorBaseConfiguration* data)
470{
Ed Tanous2049bd22022-07-09 07:20:26 -0700471 if (data == nullptr)
James Feist87d713a2018-12-06 16:06:24 -0800472 {
473 return;
474 }
475 auto maxFind = data->second.find("MaxReading");
476 auto minFind = data->second.find("MinReading");
477
478 if (minFind != data->second.end())
479 {
James Feist3eb82622019-02-08 13:10:22 -0800480 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800481 }
482 if (maxFind != data->second.end())
483 {
James Feist3eb82622019-02-08 13:10:22 -0800484 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800485 }
James Feistfc94b212019-02-06 16:14:51 -0800486}
James Feist82bac4c2019-03-11 11:16:53 -0700487
488void createAssociation(
489 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
490 const std::string& path)
491{
492 if (association)
493 {
Lei YU6a4e9732021-10-20 13:27:34 +0800494 fs::path p(path);
James Feistd2543f82019-04-09 11:10:06 -0700495
James Feist82bac4c2019-03-11 11:16:53 -0700496 std::vector<Association> associations;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700497 associations.emplace_back("chassis", "all_sensors",
498 p.parent_path().string());
James Feist2adc95c2019-09-30 14:55:28 -0700499 association->register_property("Associations", associations);
James Feist82bac4c2019-03-11 11:16:53 -0700500 association->initialize();
501 }
James Feist43d32fe2019-09-04 10:35:20 -0700502}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800503
504void setInventoryAssociation(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700505 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
AppaRao Pulic82213c2020-02-27 01:24:58 +0530506 const std::string& path,
507 const std::vector<std::string>& chassisPaths = std::vector<std::string>())
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800508{
509 if (association)
510 {
Lei YU6a4e9732021-10-20 13:27:34 +0800511 fs::path p(path);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800512 std::vector<Association> associations;
AppaRao Pulic82213c2020-02-27 01:24:58 +0530513 std::string objPath(p.parent_path().string());
514
Ed Tanous8a57ec02020-10-09 12:46:52 -0700515 associations.emplace_back("inventory", "sensors", objPath);
516 associations.emplace_back("chassis", "all_sensors", objPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530517
518 for (const std::string& chassisPath : chassisPaths)
519 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700520 associations.emplace_back("chassis", "all_sensors", chassisPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530521 }
522
James Feist2adc95c2019-09-30 14:55:28 -0700523 association->register_property("Associations", associations);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800524 association->initialize();
525 }
526}
527
528void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700529 const std::shared_ptr<sdbusplus::asio::connection>& conn,
530 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800531 const std::string& path)
532{
533 if (!association)
534 {
535 return;
536 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530537
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800538 conn->async_method_call(
539 [association, path](const boost::system::error_code ec,
AppaRao Pulic82213c2020-02-27 01:24:58 +0530540 const std::vector<std::string>& invSysObjPaths) {
Ed Tanousbb679322022-05-16 16:10:00 -0700541 if (ec)
542 {
543 // In case of error, set the default associations and
544 // initialize the association Interface.
545 setInventoryAssociation(association, path);
546 return;
547 }
548 setInventoryAssociation(association, path, invSysObjPaths);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800549 },
550 mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths",
551 "/xyz/openbmc_project/inventory/system", 2,
552 std::array<std::string, 1>{
553 "xyz.openbmc_project.Inventory.Item.System"});
554}
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200555
556std::optional<double> readFile(const std::string& thresholdFile,
557 const double& scaleFactor)
558{
559 std::string line;
560 std::ifstream labelFile(thresholdFile);
561 if (labelFile.good())
562 {
563 std::getline(labelFile, line);
564 labelFile.close();
565
566 try
567 {
568 return std::stod(line) / scaleFactor;
569 }
570 catch (const std::invalid_argument&)
571 {
572 return std::nullopt;
573 }
574 }
575 return std::nullopt;
576}
577
578std::optional<std::tuple<std::string, std::string, std::string>>
Lei YU6a4e9732021-10-20 13:27:34 +0800579 splitFileName(const fs::path& filePath)
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200580{
581 if (filePath.has_filename())
582 {
583 const auto fileName = filePath.filename().string();
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200584
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200585 size_t numberPos = std::strcspn(fileName.c_str(), "1234567890");
586 size_t itemPos = std::strcspn(fileName.c_str(), "_");
587
588 if (numberPos > 0 && itemPos > numberPos && fileName.size() > itemPos)
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200589 {
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200590 return std::make_optional(
591 std::make_tuple(fileName.substr(0, numberPos),
592 fileName.substr(numberPos, itemPos - numberPos),
593 fileName.substr(itemPos + 1, fileName.size())));
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200594 }
595 }
596 return std::nullopt;
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200597}
Bruce Lee1263c3d2021-06-04 15:16:33 +0800598
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530599static void handleSpecialModeChange(const std::string& manufacturingModeStatus)
600{
601 manufacturingMode = false;
602 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
603 "SpecialMode.Modes.Manufacturing")
604 {
605 manufacturingMode = true;
606 }
Ed Tanous74cffa82022-01-25 13:00:28 -0800607 if (validateUnsecureFeature == 1)
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530608 {
609 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
610 "SpecialMode.Modes.ValidationUnsecure")
611 {
612 manufacturingMode = true;
613 }
614 }
615}
616
Bruce Lee1263c3d2021-06-04 15:16:33 +0800617void setupManufacturingModeMatch(sdbusplus::asio::connection& conn)
618{
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530619 namespace rules = sdbusplus::bus::match::rules;
620 static constexpr const char* specialModeInterface =
621 "xyz.openbmc_project.Security.SpecialMode";
622
623 const std::string filterSpecialModeIntfAdd =
624 rules::interfacesAdded() +
625 rules::argNpath(0, "/xyz/openbmc_project/security/special_mode");
Patrick Williams92f8f512022-07-22 19:26:55 -0500626 static std::unique_ptr<sdbusplus::bus::match_t> specialModeIntfMatch =
627 std::make_unique<sdbusplus::bus::match_t>(conn,
628 filterSpecialModeIntfAdd,
629 [](sdbusplus::message_t& m) {
Ed Tanousbb679322022-05-16 16:10:00 -0700630 sdbusplus::message::object_path path;
631 using PropertyMap =
632 boost::container::flat_map<std::string, std::variant<std::string>>;
633 boost::container::flat_map<std::string, PropertyMap> interfaceAdded;
634 m.read(path, interfaceAdded);
635 auto intfItr = interfaceAdded.find(specialModeInterface);
636 if (intfItr == interfaceAdded.end())
637 {
638 return;
639 }
640 PropertyMap& propertyList = intfItr->second;
641 auto itr = propertyList.find("SpecialMode");
642 if (itr == propertyList.end())
643 {
644 std::cerr << "error getting SpecialMode property "
645 << "\n";
646 return;
647 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700648 auto* manufacturingModeStatus = std::get_if<std::string>(&itr->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700649 handleSpecialModeChange(*manufacturingModeStatus);
Patrick Williams92f8f512022-07-22 19:26:55 -0500650 });
Bruce Lee1263c3d2021-06-04 15:16:33 +0800651
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530652 const std::string filterSpecialModeChange =
653 rules::type::signal() + rules::member("PropertiesChanged") +
654 rules::interface("org.freedesktop.DBus.Properties") +
655 rules::argN(0, specialModeInterface);
Patrick Williams92f8f512022-07-22 19:26:55 -0500656 static std::unique_ptr<sdbusplus::bus::match_t> specialModeChangeMatch =
657 std::make_unique<sdbusplus::bus::match_t>(conn, filterSpecialModeChange,
658 [](sdbusplus::message_t& m) {
Ed Tanousbb679322022-05-16 16:10:00 -0700659 std::string interfaceName;
660 boost::container::flat_map<std::string, std::variant<std::string>>
661 propertiesChanged;
Bruce Lee1263c3d2021-06-04 15:16:33 +0800662
Ed Tanousbb679322022-05-16 16:10:00 -0700663 m.read(interfaceName, propertiesChanged);
664 auto itr = propertiesChanged.find("SpecialMode");
665 if (itr == propertiesChanged.end())
666 {
667 return;
668 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700669 auto* manufacturingModeStatus = std::get_if<std::string>(&itr->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700670 handleSpecialModeChange(*manufacturingModeStatus);
Patrick Williams92f8f512022-07-22 19:26:55 -0500671 });
Bruce Lee1263c3d2021-06-04 15:16:33 +0800672
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530673 conn.async_method_call(
674 [](const boost::system::error_code ec,
675 const std::variant<std::string>& getManufactMode) {
Ed Tanousbb679322022-05-16 16:10:00 -0700676 if (ec)
677 {
678 std::cerr << "error getting SpecialMode status " << ec.message()
679 << "\n";
680 return;
681 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700682 const auto* manufacturingModeStatus =
Ed Tanousbb679322022-05-16 16:10:00 -0700683 std::get_if<std::string>(&getManufactMode);
684 handleSpecialModeChange(*manufacturingModeStatus);
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530685 },
686 "xyz.openbmc_project.SpecialMode",
687 "/xyz/openbmc_project/security/special_mode",
688 "org.freedesktop.DBus.Properties", "Get", specialModeInterface,
689 "SpecialMode");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800690}
691
692bool getManufacturingMode()
693{
694 return manufacturingMode;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000695}
Zev Weiss214d9712022-08-12 12:54:31 -0700696
697std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
698 setupPropertiesChangedMatches(
699 sdbusplus::asio::connection& bus, std::span<const char* const> types,
700 const std::function<void(sdbusplus::message_t&)>& handler)
701{
702 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
703 for (const char* type : types)
704 {
705 auto match = std::make_unique<sdbusplus::bus::match_t>(
706 static_cast<sdbusplus::bus_t&>(bus),
707 "type='signal',member='PropertiesChanged',path_namespace='" +
708 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
709 handler);
710 matches.emplace_back(std::move(match));
711 }
712 return matches;
713}