blob: 7dc40c4e8aafd5021af5a9188bdaa9a87a94e574 [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");
153 bool err = false;
154 try
155 {
Patrick Williams92f8f512022-07-22 19:26:55 -0500156 sdbusplus::message_t reply =
James Feist6714a252018-09-10 15:26:18 -0700157 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -0700158 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -0700159 }
Patrick Williams92f8f512022-07-22 19:26:55 -0500160 catch (const sdbusplus::exception_t& e)
James Feist6714a252018-09-10 15:26:18 -0700161 {
Jason Ling5747fab2019-10-02 16:46:23 -0700162 std::cerr << "While calling GetManagedObjects on service:"
163 << entityManagerName << " exception name:" << e.name()
164 << "and description:" << e.description()
165 << " was thrown\n";
James Feist6714a252018-09-10 15:26:18 -0700166 err = true;
167 }
168
169 if (err)
170 {
171 std::cerr << "Error communicating to entity manager\n";
172 return false;
173 }
174 }
175 for (const auto& pathPair : managedObj)
176 {
James Feist6714a252018-09-10 15:26:18 -0700177 bool correctType = false;
Zev Weissfd3d5f72022-08-12 18:21:02 -0700178 for (const auto& [intf, cfg] : pathPair.second)
James Feist6714a252018-09-10 15:26:18 -0700179 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700180 if (intf.starts_with(type))
James Feist6714a252018-09-10 15:26:18 -0700181 {
182 correctType = true;
183 break;
184 }
185 }
186 if (correctType)
187 {
188 resp.emplace(pathPair);
189 }
190 }
191 return true;
192}
193
Lei YU6a4e9732021-10-20 13:27:34 +0800194bool findFiles(const fs::path& dirPath, std::string_view matchString,
Ed Tanous8a57ec02020-10-09 12:46:52 -0700195 std::vector<fs::path>& foundPaths, int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700196{
Lei YU6a4e9732021-10-20 13:27:34 +0800197 std::error_code ec;
198 if (!fs::exists(dirPath, ec))
Ed Tanous8a57ec02020-10-09 12:46:52 -0700199 {
James Feist6714a252018-09-10 15:26:18 -0700200 return false;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700201 }
James Feist6714a252018-09-10 15:26:18 -0700202
Lei YU6a4e9732021-10-20 13:27:34 +0800203 std::vector<std::regex> matchPieces;
204
205 size_t pos = 0;
206 std::string token;
207 // Generate the regex expressions list from the match we were given
208 while ((pos = matchString.find('/')) != std::string::npos)
209 {
210 token = matchString.substr(0, pos);
211 matchPieces.emplace_back(token);
212 matchString.remove_prefix(pos + 1);
213 }
214 matchPieces.emplace_back(std::string{matchString});
215
216 // Check if the match string contains directories, and skip the match of
217 // subdirectory if not
218 if (matchPieces.size() <= 1)
219 {
220 std::regex search(std::string{matchString});
221 std::smatch match;
222 for (auto p = fs::recursive_directory_iterator(
223 dirPath, fs::directory_options::follow_directory_symlink);
224 p != fs::recursive_directory_iterator(); ++p)
225 {
226 std::string path = p->path().string();
227 if (!is_directory(*p))
228 {
229 if (std::regex_search(path, match, search))
230 {
231 foundPaths.emplace_back(p->path());
232 }
233 }
234 if (p.depth() >= symlinkDepth)
235 {
236 p.disable_recursion_pending();
237 }
238 }
239 return true;
240 }
241
242 // The match string contains directories, verify each level of sub
243 // directories
Ed Tanous8a57ec02020-10-09 12:46:52 -0700244 for (auto p = fs::recursive_directory_iterator(
245 dirPath, fs::directory_options::follow_directory_symlink);
246 p != fs::recursive_directory_iterator(); ++p)
James Feist6714a252018-09-10 15:26:18 -0700247 {
Lei YU6a4e9732021-10-20 13:27:34 +0800248 std::vector<std::regex>::iterator matchPiece = matchPieces.begin();
249 fs::path::iterator pathIt = p->path().begin();
250 for (const fs::path& dir : dirPath)
251 {
252 if (dir.empty())
253 {
254 // When the path ends with '/', it gets am empty path
255 // skip such case.
256 break;
257 }
258 pathIt++;
259 }
260
261 while (pathIt != p->path().end())
262 {
263 // Found a path deeper than match.
264 if (matchPiece == matchPieces.end())
265 {
266 p.disable_recursion_pending();
267 break;
268 }
269 std::smatch match;
270 std::string component = pathIt->string();
271 std::regex regexPiece(*matchPiece);
272 if (!std::regex_match(component, match, regexPiece))
273 {
274 // path prefix doesn't match, no need to iterate further
275 p.disable_recursion_pending();
276 break;
277 }
278 matchPiece++;
279 pathIt++;
280 }
281
Ed Tanous8a57ec02020-10-09 12:46:52 -0700282 if (!is_directory(*p))
James Feist6714a252018-09-10 15:26:18 -0700283 {
Lei YU6a4e9732021-10-20 13:27:34 +0800284 if (matchPiece == matchPieces.end())
Ed Tanous8a57ec02020-10-09 12:46:52 -0700285 {
286 foundPaths.emplace_back(p->path());
287 }
James Feist6714a252018-09-10 15:26:18 -0700288 }
Lei YU6a4e9732021-10-20 13:27:34 +0800289
Ed Tanous8a57ec02020-10-09 12:46:52 -0700290 if (p.depth() >= symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700291 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700292 p.disable_recursion_pending();
James Feist6714a252018-09-10 15:26:18 -0700293 }
294 }
295 return true;
296}
297
James Feist71d31b22019-01-02 16:57:54 -0800298bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700299{
James Feist71d31b22019-01-02 16:57:54 -0800300 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700301 {
James Feist71d31b22019-01-02 16:57:54 -0800302 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700303 }
James Feist71d31b22019-01-02 16:57:54 -0800304 return powerStatusOn;
305}
306
James Feistfc94b212019-02-06 16:14:51 -0800307bool hasBiosPost(void)
308{
James Feist52497fd2019-06-07 13:01:33 -0700309 if (!postMatch)
James Feistfc94b212019-02-06 16:14:51 -0800310 {
James Feist52497fd2019-06-07 13:01:33 -0700311 throw std::runtime_error("Post Match Not Created");
James Feistfc94b212019-02-06 16:14:51 -0800312 }
313 return biosHasPost;
314}
315
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000316bool readingStateGood(const PowerState& powerState)
317{
318 if (powerState == PowerState::on && !isPowerOn())
319 {
320 return false;
321 }
322 if (powerState == PowerState::biosPost && (!hasBiosPost() || !isPowerOn()))
323 {
324 return false;
325 }
326
327 return true;
328}
329
James Feist8aeffd92020-09-14 12:08:28 -0700330static void
331 getPowerStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
332 size_t retries = 2)
333{
334 conn->async_method_call(
335 [conn, retries](boost::system::error_code ec,
336 const std::variant<std::string>& state) {
Ed Tanousbb679322022-05-16 16:10:00 -0700337 if (ec)
338 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700339 if (retries != 0U)
James Feist8aeffd92020-09-14 12:08:28 -0700340 {
Ed Tanousbb679322022-05-16 16:10:00 -0700341 auto timer = std::make_shared<boost::asio::steady_timer>(
342 conn->get_io_context());
343 timer->expires_after(std::chrono::seconds(15));
344 timer->async_wait(
345 [timer, conn, retries](boost::system::error_code) {
346 getPowerStatus(conn, retries - 1);
347 });
James Feist8aeffd92020-09-14 12:08:28 -0700348 return;
349 }
Ed Tanousbb679322022-05-16 16:10:00 -0700350
351 // we commonly come up before power control, we'll capture the
352 // property change later
353 std::cerr << "error getting power status " << ec.message() << "\n";
354 return;
355 }
Zev Weiss6c106d62022-08-17 20:50:00 -0700356 powerStatusOn = std::get<std::string>(state).ends_with(".Running");
James Feist8aeffd92020-09-14 12:08:28 -0700357 },
358 power::busname, power::path, properties::interface, properties::get,
359 power::interface, power::property);
360}
361
362static void
363 getPostStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
364 size_t retries = 2)
365{
366 conn->async_method_call(
367 [conn, retries](boost::system::error_code ec,
368 const std::variant<std::string>& state) {
Ed Tanousbb679322022-05-16 16:10:00 -0700369 if (ec)
370 {
Ed Tanous2049bd22022-07-09 07:20:26 -0700371 if (retries != 0U)
James Feist8aeffd92020-09-14 12:08:28 -0700372 {
Ed Tanousbb679322022-05-16 16:10:00 -0700373 auto timer = std::make_shared<boost::asio::steady_timer>(
374 conn->get_io_context());
375 timer->expires_after(std::chrono::seconds(15));
376 timer->async_wait(
377 [timer, conn, retries](boost::system::error_code) {
378 getPostStatus(conn, retries - 1);
379 });
James Feist8aeffd92020-09-14 12:08:28 -0700380 return;
381 }
Ed Tanousbb679322022-05-16 16:10:00 -0700382 // we commonly come up before power control, we'll capture the
383 // property change later
384 std::cerr << "error getting post status " << ec.message() << "\n";
385 return;
386 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700387 const auto& value = std::get<std::string>(state);
Ed Tanousbb679322022-05-16 16:10:00 -0700388 biosHasPost = (value != "Inactive") &&
389 (value != "xyz.openbmc_project.State.OperatingSystem."
390 "Status.OSStatus.Inactive");
James Feist8aeffd92020-09-14 12:08:28 -0700391 },
392 post::busname, post::path, properties::interface, properties::get,
393 post::interface, post::property);
394}
395
Zev Weiss88cb29d2022-05-09 03:46:15 +0000396void setupPowerMatchCallback(
397 const std::shared_ptr<sdbusplus::asio::connection>& conn,
398 std::function<void(PowerState type, bool state)>&& hostStatusCallback)
James Feist71d31b22019-01-02 16:57:54 -0800399{
James Feist43d32fe2019-09-04 10:35:20 -0700400 static boost::asio::steady_timer timer(conn->get_io_context());
James Feist6714a252018-09-10 15:26:18 -0700401 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800402 // cache the correct value
James Feist52497fd2019-06-07 13:01:33 -0700403 if (powerMatch)
404 {
405 return;
406 }
James Feist6714a252018-09-10 15:26:18 -0700407
Patrick Williams92f8f512022-07-22 19:26:55 -0500408 powerMatch = std::make_unique<sdbusplus::bus::match_t>(
409 static_cast<sdbusplus::bus_t&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700410 "type='signal',interface='" + std::string(properties::interface) +
411 "',path='" + std::string(power::path) + "',arg0='" +
412 std::string(power::interface) + "'",
Zev Weiss88cb29d2022-05-09 03:46:15 +0000413 [hostStatusCallback](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700414 std::string objectName;
415 boost::container::flat_map<std::string, std::variant<std::string>>
416 values;
417 message.read(objectName, values);
418 auto findState = values.find(power::property);
419 if (findState != values.end())
420 {
Zev Weiss6c106d62022-08-17 20:50:00 -0700421 bool on =
422 std::get<std::string>(findState->second).ends_with(".Running");
Ed Tanousbb679322022-05-16 16:10:00 -0700423 if (!on)
James Feist6714a252018-09-10 15:26:18 -0700424 {
Ed Tanousbb679322022-05-16 16:10:00 -0700425 timer.cancel();
426 powerStatusOn = false;
Zev Weiss88cb29d2022-05-09 03:46:15 +0000427 hostStatusCallback(PowerState::on, powerStatusOn);
Ed Tanousbb679322022-05-16 16:10:00 -0700428 return;
429 }
430 // on comes too quickly
431 timer.expires_after(std::chrono::seconds(10));
Zev Weiss88cb29d2022-05-09 03:46:15 +0000432 timer.async_wait(
433 [hostStatusCallback](boost::system::error_code ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700434 if (ec == boost::asio::error::operation_aborted)
James Feist43d32fe2019-09-04 10:35:20 -0700435 {
James Feist43d32fe2019-09-04 10:35:20 -0700436 return;
437 }
Ed Tanousbb679322022-05-16 16:10:00 -0700438 if (ec)
439 {
440 std::cerr << "Timer error " << ec.message() << "\n";
441 return;
442 }
443 powerStatusOn = true;
Zev Weiss88cb29d2022-05-09 03:46:15 +0000444 hostStatusCallback(PowerState::on, powerStatusOn);
Ed Tanousbb679322022-05-16 16:10:00 -0700445 });
446 }
James Feist52497fd2019-06-07 13:01:33 -0700447 });
448
Patrick Williams92f8f512022-07-22 19:26:55 -0500449 postMatch = std::make_unique<sdbusplus::bus::match_t>(
450 static_cast<sdbusplus::bus_t&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700451 "type='signal',interface='" + std::string(properties::interface) +
452 "',path='" + std::string(post::path) + "',arg0='" +
453 std::string(post::interface) + "'",
Zev Weiss88cb29d2022-05-09 03:46:15 +0000454 [hostStatusCallback](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700455 std::string objectName;
456 boost::container::flat_map<std::string, std::variant<std::string>>
457 values;
458 message.read(objectName, values);
459 auto findState = values.find(post::property);
460 if (findState != values.end())
461 {
462 auto& value = std::get<std::string>(findState->second);
463 biosHasPost = (value != "Inactive") &&
464 (value != "xyz.openbmc_project.State.OperatingSystem."
465 "Status.OSStatus.Inactive");
Zev Weiss88cb29d2022-05-09 03:46:15 +0000466 hostStatusCallback(PowerState::biosPost, biosHasPost);
Ed Tanousbb679322022-05-16 16:10:00 -0700467 }
James Feist52497fd2019-06-07 13:01:33 -0700468 });
James Feistfc94b212019-02-06 16:14:51 -0800469
James Feist8aeffd92020-09-14 12:08:28 -0700470 getPowerStatus(conn);
471 getPostStatus(conn);
James Feist3f0e8762018-11-27 11:30:42 -0800472}
James Feist87d713a2018-12-06 16:06:24 -0800473
Zev Weiss88cb29d2022-05-09 03:46:15 +0000474void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
475{
476 setupPowerMatchCallback(conn, [](PowerState, bool) {});
477}
478
James Feist87d713a2018-12-06 16:06:24 -0800479// replaces limits if MinReading and MaxReading are found.
480void findLimits(std::pair<double, double>& limits,
481 const SensorBaseConfiguration* data)
482{
Ed Tanous2049bd22022-07-09 07:20:26 -0700483 if (data == nullptr)
James Feist87d713a2018-12-06 16:06:24 -0800484 {
485 return;
486 }
487 auto maxFind = data->second.find("MaxReading");
488 auto minFind = data->second.find("MinReading");
489
490 if (minFind != data->second.end())
491 {
James Feist3eb82622019-02-08 13:10:22 -0800492 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800493 }
494 if (maxFind != data->second.end())
495 {
James Feist3eb82622019-02-08 13:10:22 -0800496 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800497 }
James Feistfc94b212019-02-06 16:14:51 -0800498}
James Feist82bac4c2019-03-11 11:16:53 -0700499
500void createAssociation(
501 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
502 const std::string& path)
503{
504 if (association)
505 {
Lei YU6a4e9732021-10-20 13:27:34 +0800506 fs::path p(path);
James Feistd2543f82019-04-09 11:10:06 -0700507
James Feist82bac4c2019-03-11 11:16:53 -0700508 std::vector<Association> associations;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700509 associations.emplace_back("chassis", "all_sensors",
510 p.parent_path().string());
James Feist2adc95c2019-09-30 14:55:28 -0700511 association->register_property("Associations", associations);
James Feist82bac4c2019-03-11 11:16:53 -0700512 association->initialize();
513 }
James Feist43d32fe2019-09-04 10:35:20 -0700514}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800515
516void setInventoryAssociation(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700517 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
AppaRao Pulic82213c2020-02-27 01:24:58 +0530518 const std::string& path,
519 const std::vector<std::string>& chassisPaths = std::vector<std::string>())
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800520{
521 if (association)
522 {
Lei YU6a4e9732021-10-20 13:27:34 +0800523 fs::path p(path);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800524 std::vector<Association> associations;
AppaRao Pulic82213c2020-02-27 01:24:58 +0530525 std::string objPath(p.parent_path().string());
526
Ed Tanous8a57ec02020-10-09 12:46:52 -0700527 associations.emplace_back("inventory", "sensors", objPath);
528 associations.emplace_back("chassis", "all_sensors", objPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530529
530 for (const std::string& chassisPath : chassisPaths)
531 {
Ed Tanous8a57ec02020-10-09 12:46:52 -0700532 associations.emplace_back("chassis", "all_sensors", chassisPath);
AppaRao Pulic82213c2020-02-27 01:24:58 +0530533 }
534
James Feist2adc95c2019-09-30 14:55:28 -0700535 association->register_property("Associations", associations);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800536 association->initialize();
537 }
538}
539
540void createInventoryAssoc(
Ed Tanous8a57ec02020-10-09 12:46:52 -0700541 const std::shared_ptr<sdbusplus::asio::connection>& conn,
542 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800543 const std::string& path)
544{
545 if (!association)
546 {
547 return;
548 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530549
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800550 conn->async_method_call(
551 [association, path](const boost::system::error_code ec,
AppaRao Pulic82213c2020-02-27 01:24:58 +0530552 const std::vector<std::string>& invSysObjPaths) {
Ed Tanousbb679322022-05-16 16:10:00 -0700553 if (ec)
554 {
555 // In case of error, set the default associations and
556 // initialize the association Interface.
557 setInventoryAssociation(association, path);
558 return;
559 }
560 setInventoryAssociation(association, path, invSysObjPaths);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800561 },
562 mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths",
563 "/xyz/openbmc_project/inventory/system", 2,
564 std::array<std::string, 1>{
565 "xyz.openbmc_project.Inventory.Item.System"});
566}
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200567
568std::optional<double> readFile(const std::string& thresholdFile,
569 const double& scaleFactor)
570{
571 std::string line;
572 std::ifstream labelFile(thresholdFile);
573 if (labelFile.good())
574 {
575 std::getline(labelFile, line);
576 labelFile.close();
577
578 try
579 {
580 return std::stod(line) / scaleFactor;
581 }
582 catch (const std::invalid_argument&)
583 {
584 return std::nullopt;
585 }
586 }
587 return std::nullopt;
588}
589
590std::optional<std::tuple<std::string, std::string, std::string>>
Lei YU6a4e9732021-10-20 13:27:34 +0800591 splitFileName(const fs::path& filePath)
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200592{
593 if (filePath.has_filename())
594 {
595 const auto fileName = filePath.filename().string();
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200596
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200597 size_t numberPos = std::strcspn(fileName.c_str(), "1234567890");
598 size_t itemPos = std::strcspn(fileName.c_str(), "_");
599
600 if (numberPos > 0 && itemPos > numberPos && fileName.size() > itemPos)
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200601 {
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200602 return std::make_optional(
603 std::make_tuple(fileName.substr(0, numberPos),
604 fileName.substr(numberPos, itemPos - numberPos),
605 fileName.substr(itemPos + 1, fileName.size())));
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200606 }
607 }
608 return std::nullopt;
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200609}
Bruce Lee1263c3d2021-06-04 15:16:33 +0800610
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530611static void handleSpecialModeChange(const std::string& manufacturingModeStatus)
612{
613 manufacturingMode = false;
614 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
615 "SpecialMode.Modes.Manufacturing")
616 {
617 manufacturingMode = true;
618 }
Ed Tanous74cffa82022-01-25 13:00:28 -0800619 if (validateUnsecureFeature == 1)
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530620 {
621 if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
622 "SpecialMode.Modes.ValidationUnsecure")
623 {
624 manufacturingMode = true;
625 }
626 }
627}
628
Bruce Lee1263c3d2021-06-04 15:16:33 +0800629void setupManufacturingModeMatch(sdbusplus::asio::connection& conn)
630{
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530631 namespace rules = sdbusplus::bus::match::rules;
632 static constexpr const char* specialModeInterface =
633 "xyz.openbmc_project.Security.SpecialMode";
634
635 const std::string filterSpecialModeIntfAdd =
636 rules::interfacesAdded() +
637 rules::argNpath(0, "/xyz/openbmc_project/security/special_mode");
Patrick Williams92f8f512022-07-22 19:26:55 -0500638 static std::unique_ptr<sdbusplus::bus::match_t> specialModeIntfMatch =
639 std::make_unique<sdbusplus::bus::match_t>(conn,
640 filterSpecialModeIntfAdd,
641 [](sdbusplus::message_t& m) {
Ed Tanousbb679322022-05-16 16:10:00 -0700642 sdbusplus::message::object_path path;
643 using PropertyMap =
644 boost::container::flat_map<std::string, std::variant<std::string>>;
645 boost::container::flat_map<std::string, PropertyMap> interfaceAdded;
646 m.read(path, interfaceAdded);
647 auto intfItr = interfaceAdded.find(specialModeInterface);
648 if (intfItr == interfaceAdded.end())
649 {
650 return;
651 }
652 PropertyMap& propertyList = intfItr->second;
653 auto itr = propertyList.find("SpecialMode");
654 if (itr == propertyList.end())
655 {
656 std::cerr << "error getting SpecialMode property "
657 << "\n";
658 return;
659 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700660 auto* manufacturingModeStatus = std::get_if<std::string>(&itr->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700661 handleSpecialModeChange(*manufacturingModeStatus);
Patrick Williams92f8f512022-07-22 19:26:55 -0500662 });
Bruce Lee1263c3d2021-06-04 15:16:33 +0800663
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530664 const std::string filterSpecialModeChange =
665 rules::type::signal() + rules::member("PropertiesChanged") +
666 rules::interface("org.freedesktop.DBus.Properties") +
667 rules::argN(0, specialModeInterface);
Patrick Williams92f8f512022-07-22 19:26:55 -0500668 static std::unique_ptr<sdbusplus::bus::match_t> specialModeChangeMatch =
669 std::make_unique<sdbusplus::bus::match_t>(conn, filterSpecialModeChange,
670 [](sdbusplus::message_t& m) {
Ed Tanousbb679322022-05-16 16:10:00 -0700671 std::string interfaceName;
672 boost::container::flat_map<std::string, std::variant<std::string>>
673 propertiesChanged;
Bruce Lee1263c3d2021-06-04 15:16:33 +0800674
Ed Tanousbb679322022-05-16 16:10:00 -0700675 m.read(interfaceName, propertiesChanged);
676 auto itr = propertiesChanged.find("SpecialMode");
677 if (itr == propertiesChanged.end())
678 {
679 return;
680 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700681 auto* manufacturingModeStatus = std::get_if<std::string>(&itr->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700682 handleSpecialModeChange(*manufacturingModeStatus);
Patrick Williams92f8f512022-07-22 19:26:55 -0500683 });
Bruce Lee1263c3d2021-06-04 15:16:33 +0800684
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530685 conn.async_method_call(
686 [](const boost::system::error_code ec,
687 const std::variant<std::string>& getManufactMode) {
Ed Tanousbb679322022-05-16 16:10:00 -0700688 if (ec)
689 {
690 std::cerr << "error getting SpecialMode status " << ec.message()
691 << "\n";
692 return;
693 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700694 const auto* manufacturingModeStatus =
Ed Tanousbb679322022-05-16 16:10:00 -0700695 std::get_if<std::string>(&getManufactMode);
696 handleSpecialModeChange(*manufacturingModeStatus);
Arun P. Mohanan45f844a2021-11-03 22:18:09 +0530697 },
698 "xyz.openbmc_project.SpecialMode",
699 "/xyz/openbmc_project/security/special_mode",
700 "org.freedesktop.DBus.Properties", "Get", specialModeInterface,
701 "SpecialMode");
Bruce Lee1263c3d2021-06-04 15:16:33 +0800702}
703
704bool getManufacturingMode()
705{
706 return manufacturingMode;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +0000707}
Zev Weiss214d9712022-08-12 12:54:31 -0700708
709std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
710 setupPropertiesChangedMatches(
711 sdbusplus::asio::connection& bus, std::span<const char* const> types,
712 const std::function<void(sdbusplus::message_t&)>& handler)
713{
714 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
715 for (const char* type : types)
716 {
717 auto match = std::make_unique<sdbusplus::bus::match_t>(
718 static_cast<sdbusplus::bus_t&>(bus),
719 "type='signal',member='PropertiesChanged',path_namespace='" +
720 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
721 handler);
722 matches.emplace_back(std::move(match));
723 }
724 return matches;
725}