xref: /openbmc/dbus-sensors/src/Utils.cpp (revision e73bd0a1)
1 /*
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 
17 #include "Utils.hpp"
18 
19 #include "dbus-sensor_config.h"
20 
21 #include "DeviceMgmt.hpp"
22 
23 #include <boost/container/flat_map.hpp>
24 #include <sdbusplus/asio/connection.hpp>
25 #include <sdbusplus/asio/object_server.hpp>
26 #include <sdbusplus/bus/match.hpp>
27 
28 #include <filesystem>
29 #include <fstream>
30 #include <memory>
31 #include <regex>
32 #include <stdexcept>
33 #include <string>
34 #include <utility>
35 #include <variant>
36 #include <vector>
37 
38 namespace fs = std::filesystem;
39 
40 static bool powerStatusOn = false;
41 static bool biosHasPost = false;
42 static bool manufacturingMode = false;
43 static bool chassisStatusOn = false;
44 
45 static std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
46 static std::unique_ptr<sdbusplus::bus::match_t> postMatch = nullptr;
47 static std::unique_ptr<sdbusplus::bus::match_t> chassisMatch = nullptr;
48 
49 /**
50  * return the contents of a file
51  * @param[in] hwmonFile - the path to the file to read
52  * @return the contents of the file as a string or nullopt if the file could not
53  * be opened.
54  */
55 
56 std::optional<std::string> openAndRead(const std::string& hwmonFile)
57 {
58     std::string fileVal;
59     std::ifstream fileStream(hwmonFile);
60     if (!fileStream.is_open())
61     {
62         return std::nullopt;
63     }
64     std::getline(fileStream, fileVal);
65     return fileVal;
66 }
67 
68 /**
69  * given a hwmon temperature base name if valid return the full path else
70  * nullopt
71  * @param[in] directory - the hwmon sysfs directory
72  * @param[in] permitSet - a set of labels or hwmon basenames to permit. If this
73  * is empty then *everything* is permitted.
74  * @return a string to the full path of the file to create a temp sensor with or
75  * nullopt to indicate that no sensor should be created for this basename.
76  */
77 std::optional<std::string>
78     getFullHwmonFilePath(const std::string& directory,
79                          const std::string& hwmonBaseName,
80                          const std::set<std::string>& permitSet)
81 {
82     std::optional<std::string> result;
83     std::string filename;
84     if (permitSet.empty())
85     {
86         result = directory + "/" + hwmonBaseName + "_input";
87         return result;
88     }
89     filename = directory + "/" + hwmonBaseName + "_label";
90     auto searchVal = openAndRead(filename);
91     if (!searchVal)
92     {
93         /* if the hwmon temp doesn't have a corresponding label file
94          * then use the hwmon temperature base name
95          */
96         searchVal = hwmonBaseName;
97     }
98     if (permitSet.find(*searchVal) != permitSet.end())
99     {
100         result = directory + "/" + hwmonBaseName + "_input";
101     }
102     return result;
103 }
104 
105 /**
106  * retrieve a set of basenames and labels to allow sensor creation for.
107  * @param[in] config - a map representing the configuration for a specific
108  * device
109  * @return a set of basenames and labels to allow sensor creation for. An empty
110  * set indicates that everything is permitted.
111  */
112 std::set<std::string> getPermitSet(const SensorBaseConfigMap& config)
113 {
114     auto permitAttribute = config.find("Labels");
115     std::set<std::string> permitSet;
116     if (permitAttribute != config.end())
117     {
118         try
119         {
120             auto val =
121                 std::get<std::vector<std::string>>(permitAttribute->second);
122 
123             permitSet.insert(std::make_move_iterator(val.begin()),
124                              std::make_move_iterator(val.end()));
125         }
126         catch (const std::bad_variant_access& err)
127         {
128             std::cerr << err.what()
129                       << ":PermitList does not contain a list, wrong "
130                          "variant type.\n";
131         }
132     }
133     return permitSet;
134 }
135 
136 bool getSensorConfiguration(
137     const std::string& type,
138     const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
139     ManagedObjectType& resp)
140 {
141     return getSensorConfiguration(type, dbusConnection, resp, false);
142 }
143 
144 bool getSensorConfiguration(
145     const std::string& type,
146     const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
147     ManagedObjectType& resp, bool useCache)
148 {
149     static ManagedObjectType managedObj;
150     std::string typeIntf = configInterfaceName(type);
151 
152     if (!useCache)
153     {
154         managedObj.clear();
155         sdbusplus::message_t getManagedObjects =
156             dbusConnection->new_method_call(
157                 entityManagerName, "/xyz/openbmc_project/inventory",
158                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
159         try
160         {
161             sdbusplus::message_t reply =
162                 dbusConnection->call(getManagedObjects);
163             reply.read(managedObj);
164         }
165         catch (const sdbusplus::exception_t& e)
166         {
167             std::cerr << "While calling GetManagedObjects on service:"
168                       << entityManagerName << " exception name:" << e.name()
169                       << "and description:" << e.description()
170                       << " was thrown\n";
171             return false;
172         }
173     }
174     for (const auto& pathPair : managedObj)
175     {
176         for (const auto& [intf, cfg] : pathPair.second)
177         {
178             if (intf.starts_with(typeIntf))
179             {
180                 resp.emplace(pathPair);
181                 break;
182             }
183         }
184     }
185     return true;
186 }
187 
188 bool findFiles(const fs::path& dirPath, std::string_view matchString,
189                std::vector<fs::path>& foundPaths, int symlinkDepth)
190 {
191     std::error_code ec;
192     if (!fs::exists(dirPath, ec))
193     {
194         return false;
195     }
196 
197     std::vector<std::regex> matchPieces;
198 
199     size_t pos = 0;
200     std::string token;
201     // Generate the regex expressions list from the match we were given
202     while ((pos = matchString.find('/')) != std::string::npos)
203     {
204         token = matchString.substr(0, pos);
205         matchPieces.emplace_back(token);
206         matchString.remove_prefix(pos + 1);
207     }
208     matchPieces.emplace_back(std::string{matchString});
209 
210     // Check if the match string contains directories, and skip the match of
211     // subdirectory if not
212     if (matchPieces.size() <= 1)
213     {
214         std::regex search(std::string{matchString});
215         std::smatch match;
216         for (auto p = fs::recursive_directory_iterator(
217                  dirPath, fs::directory_options::follow_directory_symlink);
218              p != fs::recursive_directory_iterator(); ++p)
219         {
220             std::string path = p->path().string();
221             if (!is_directory(*p))
222             {
223                 if (std::regex_search(path, match, search))
224                 {
225                     foundPaths.emplace_back(p->path());
226                 }
227             }
228             if (p.depth() >= symlinkDepth)
229             {
230                 p.disable_recursion_pending();
231             }
232         }
233         return true;
234     }
235 
236     // The match string contains directories, verify each level of sub
237     // directories
238     for (auto p = fs::recursive_directory_iterator(
239              dirPath, fs::directory_options::follow_directory_symlink);
240          p != fs::recursive_directory_iterator(); ++p)
241     {
242         std::vector<std::regex>::iterator matchPiece = matchPieces.begin();
243         fs::path::iterator pathIt = p->path().begin();
244         for (const fs::path& dir : dirPath)
245         {
246             if (dir.empty())
247             {
248                 // When the path ends with '/', it gets am empty path
249                 // skip such case.
250                 break;
251             }
252             pathIt++;
253         }
254 
255         while (pathIt != p->path().end())
256         {
257             // Found a path deeper than match.
258             if (matchPiece == matchPieces.end())
259             {
260                 p.disable_recursion_pending();
261                 break;
262             }
263             std::smatch match;
264             std::string component = pathIt->string();
265             std::regex regexPiece(*matchPiece);
266             if (!std::regex_match(component, match, regexPiece))
267             {
268                 // path prefix doesn't match, no need to iterate further
269                 p.disable_recursion_pending();
270                 break;
271             }
272             matchPiece++;
273             pathIt++;
274         }
275 
276         if (!is_directory(*p))
277         {
278             if (matchPiece == matchPieces.end())
279             {
280                 foundPaths.emplace_back(p->path());
281             }
282         }
283 
284         if (p.depth() >= symlinkDepth)
285         {
286             p.disable_recursion_pending();
287         }
288     }
289     return true;
290 }
291 
292 bool isPowerOn(void)
293 {
294     if (!powerMatch)
295     {
296         throw std::runtime_error("Power Match Not Created");
297     }
298     return powerStatusOn;
299 }
300 
301 bool hasBiosPost(void)
302 {
303     if (!postMatch)
304     {
305         throw std::runtime_error("Post Match Not Created");
306     }
307     return biosHasPost;
308 }
309 
310 bool isChassisOn(void)
311 {
312     if (!chassisMatch)
313     {
314         throw std::runtime_error("Chassis On Match Not Created");
315     }
316     return chassisStatusOn;
317 }
318 
319 bool readingStateGood(const PowerState& powerState)
320 {
321     if (powerState == PowerState::on && !isPowerOn())
322     {
323         return false;
324     }
325     if (powerState == PowerState::biosPost && (!hasBiosPost() || !isPowerOn()))
326     {
327         return false;
328     }
329     if (powerState == PowerState::chassisOn && !isChassisOn())
330     {
331         return false;
332     }
333 
334     return true;
335 }
336 
337 static void
338     getPowerStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
339                    size_t retries = 2)
340 {
341     conn->async_method_call(
342         [conn, retries](boost::system::error_code ec,
343                         const std::variant<std::string>& state) {
344         if (ec)
345         {
346             if (retries != 0U)
347             {
348                 auto timer = std::make_shared<boost::asio::steady_timer>(
349                     conn->get_io_context());
350                 timer->expires_after(std::chrono::seconds(15));
351                 timer->async_wait(
352                     [timer, conn, retries](boost::system::error_code) {
353                     getPowerStatus(conn, retries - 1);
354                 });
355                 return;
356             }
357 
358             // we commonly come up before power control, we'll capture the
359             // property change later
360             std::cerr << "error getting power status " << ec.message() << "\n";
361             return;
362         }
363         powerStatusOn = std::get<std::string>(state).ends_with(".Running");
364         },
365         power::busname, power::path, properties::interface, properties::get,
366         power::interface, power::property);
367 }
368 
369 static void
370     getPostStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
371                   size_t retries = 2)
372 {
373     conn->async_method_call(
374         [conn, retries](boost::system::error_code ec,
375                         const std::variant<std::string>& state) {
376         if (ec)
377         {
378             if (retries != 0U)
379             {
380                 auto timer = std::make_shared<boost::asio::steady_timer>(
381                     conn->get_io_context());
382                 timer->expires_after(std::chrono::seconds(15));
383                 timer->async_wait(
384                     [timer, conn, retries](boost::system::error_code) {
385                     getPostStatus(conn, retries - 1);
386                 });
387                 return;
388             }
389             // we commonly come up before power control, we'll capture the
390             // property change later
391             std::cerr << "error getting post status " << ec.message() << "\n";
392             return;
393         }
394         const auto& value = std::get<std::string>(state);
395         biosHasPost = (value != "Inactive") &&
396                       (value != "xyz.openbmc_project.State.OperatingSystem."
397                                 "Status.OSStatus.Inactive");
398         },
399         post::busname, post::path, properties::interface, properties::get,
400         post::interface, post::property);
401 }
402 
403 static void
404     getChassisStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn,
405                      size_t retries = 2)
406 {
407     conn->async_method_call(
408         [conn, retries](boost::system::error_code ec,
409                         const std::variant<std::string>& state) {
410         if (ec)
411         {
412             if (retries != 0U)
413             {
414                 auto timer = std::make_shared<boost::asio::steady_timer>(
415                     conn->get_io_context());
416                 timer->expires_after(std::chrono::seconds(15));
417                 timer->async_wait(
418                     [timer, conn, retries](boost::system::error_code) {
419                     getChassisStatus(conn, retries - 1);
420                 });
421                 return;
422             }
423 
424             // we commonly come up before power control, we'll capture the
425             // property change later
426             std::cerr << "error getting chassis power status " << ec.message()
427                       << "\n";
428             return;
429         }
430         chassisStatusOn = std::get<std::string>(state).ends_with(chassis::sOn);
431         },
432         chassis::busname, chassis::path, properties::interface, properties::get,
433         chassis::interface, chassis::property);
434 }
435 
436 void setupPowerMatchCallback(
437     const std::shared_ptr<sdbusplus::asio::connection>& conn,
438     std::function<void(PowerState type, bool state)>&& hostStatusCallback)
439 {
440     static boost::asio::steady_timer timer(conn->get_io_context());
441     static boost::asio::steady_timer timerChassisOn(conn->get_io_context());
442     // create a match for powergood changes, first time do a method call to
443     // cache the correct value
444     if (powerMatch)
445     {
446         return;
447     }
448 
449     powerMatch = std::make_unique<sdbusplus::bus::match_t>(
450         static_cast<sdbusplus::bus_t&>(*conn),
451         "type='signal',interface='" + std::string(properties::interface) +
452             "',path='" + std::string(power::path) + "',arg0='" +
453             std::string(power::interface) + "'",
454         [hostStatusCallback](sdbusplus::message_t& message) {
455         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(power::property);
460         if (findState != values.end())
461         {
462             bool on =
463                 std::get<std::string>(findState->second).ends_with(".Running");
464             if (!on)
465             {
466                 timer.cancel();
467                 powerStatusOn = false;
468                 hostStatusCallback(PowerState::on, powerStatusOn);
469                 return;
470             }
471             // on comes too quickly
472             timer.expires_after(std::chrono::seconds(10));
473             timer.async_wait(
474                 [hostStatusCallback](boost::system::error_code ec) {
475                 if (ec == boost::asio::error::operation_aborted)
476                 {
477                     return;
478                 }
479                 if (ec)
480                 {
481                     std::cerr << "Timer error " << ec.message() << "\n";
482                     return;
483                 }
484                 powerStatusOn = true;
485                 hostStatusCallback(PowerState::on, powerStatusOn);
486             });
487         }
488         });
489 
490     postMatch = std::make_unique<sdbusplus::bus::match_t>(
491         static_cast<sdbusplus::bus_t&>(*conn),
492         "type='signal',interface='" + std::string(properties::interface) +
493             "',path='" + std::string(post::path) + "',arg0='" +
494             std::string(post::interface) + "'",
495         [hostStatusCallback](sdbusplus::message_t& message) {
496         std::string objectName;
497         boost::container::flat_map<std::string, std::variant<std::string>>
498             values;
499         message.read(objectName, values);
500         auto findState = values.find(post::property);
501         if (findState != values.end())
502         {
503             auto& value = std::get<std::string>(findState->second);
504             biosHasPost = (value != "Inactive") &&
505                           (value != "xyz.openbmc_project.State.OperatingSystem."
506                                     "Status.OSStatus.Inactive");
507             hostStatusCallback(PowerState::biosPost, biosHasPost);
508         }
509         });
510 
511     chassisMatch = std::make_unique<sdbusplus::bus::match_t>(
512         static_cast<sdbusplus::bus_t&>(*conn),
513         "type='signal',interface='" + std::string(properties::interface) +
514             "',path='" + std::string(chassis::path) + "',arg0='" +
515             std::string(chassis::interface) + "'",
516         [hostStatusCallback](sdbusplus::message_t& message) {
517         std::string objectName;
518         boost::container::flat_map<std::string, std::variant<std::string>>
519             values;
520         message.read(objectName, values);
521         auto findState = values.find(chassis::property);
522         if (findState != values.end())
523         {
524             bool on = std::get<std::string>(findState->second)
525                           .ends_with(chassis::sOn);
526             if (!on)
527             {
528                 timerChassisOn.cancel();
529                 chassisStatusOn = false;
530                 hostStatusCallback(PowerState::chassisOn, chassisStatusOn);
531                 return;
532             }
533             // on comes too quickly
534             timerChassisOn.expires_after(std::chrono::seconds(10));
535             timerChassisOn.async_wait(
536                 [hostStatusCallback](boost::system::error_code ec) {
537                 if (ec == boost::asio::error::operation_aborted)
538                 {
539                     return;
540                 }
541                 if (ec)
542                 {
543                     std::cerr << "Timer error " << ec.message() << "\n";
544                     return;
545                 }
546                 chassisStatusOn = true;
547                 hostStatusCallback(PowerState::chassisOn, chassisStatusOn);
548             });
549         }
550         });
551     getPowerStatus(conn);
552     getPostStatus(conn);
553     getChassisStatus(conn);
554 }
555 
556 void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
557 {
558     setupPowerMatchCallback(conn, [](PowerState, bool) {});
559 }
560 
561 // replaces limits if MinReading and MaxReading are found.
562 void findLimits(std::pair<double, double>& limits,
563                 const SensorBaseConfiguration* data)
564 {
565     if (data == nullptr)
566     {
567         return;
568     }
569     auto maxFind = data->second.find("MaxReading");
570     auto minFind = data->second.find("MinReading");
571 
572     if (minFind != data->second.end())
573     {
574         limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
575     }
576     if (maxFind != data->second.end())
577     {
578         limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
579     }
580 }
581 
582 void createAssociation(
583     std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
584     const std::string& path)
585 {
586     if (association)
587     {
588         fs::path p(path);
589 
590         std::vector<Association> associations;
591         associations.emplace_back("chassis", "all_sensors",
592                                   p.parent_path().string());
593         association->register_property("Associations", associations);
594         association->initialize();
595     }
596 }
597 
598 void setInventoryAssociation(
599     const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
600     const std::string& path,
601     const std::vector<std::string>& chassisPaths = std::vector<std::string>())
602 {
603     if (association)
604     {
605         fs::path p(path);
606         std::vector<Association> associations;
607         std::string objPath(p.parent_path().string());
608 
609         associations.emplace_back("inventory", "sensors", objPath);
610         associations.emplace_back("chassis", "all_sensors", objPath);
611 
612         for (const std::string& chassisPath : chassisPaths)
613         {
614             associations.emplace_back("chassis", "all_sensors", chassisPath);
615         }
616 
617         association->register_property("Associations", associations);
618         association->initialize();
619     }
620 }
621 
622 void createInventoryAssoc(
623     const std::shared_ptr<sdbusplus::asio::connection>& conn,
624     const std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
625     const std::string& path)
626 {
627     if (!association)
628     {
629         return;
630     }
631 
632     conn->async_method_call(
633         [association, path](const boost::system::error_code ec,
634                             const std::vector<std::string>& invSysObjPaths) {
635         if (ec)
636         {
637             // In case of error, set the default associations and
638             // initialize the association Interface.
639             setInventoryAssociation(association, path);
640             return;
641         }
642         setInventoryAssociation(association, path, invSysObjPaths);
643         },
644         mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths",
645         "/xyz/openbmc_project/inventory/system", 2,
646         std::array<std::string, 1>{
647             "xyz.openbmc_project.Inventory.Item.System"});
648 }
649 
650 std::optional<double> readFile(const std::string& thresholdFile,
651                                const double& scaleFactor)
652 {
653     std::string line;
654     std::ifstream labelFile(thresholdFile);
655     if (labelFile.good())
656     {
657         std::getline(labelFile, line);
658         labelFile.close();
659 
660         try
661         {
662             return std::stod(line) / scaleFactor;
663         }
664         catch (const std::invalid_argument&)
665         {
666             return std::nullopt;
667         }
668     }
669     return std::nullopt;
670 }
671 
672 std::optional<std::tuple<std::string, std::string, std::string>>
673     splitFileName(const fs::path& filePath)
674 {
675     if (filePath.has_filename())
676     {
677         const auto fileName = filePath.filename().string();
678 
679         size_t numberPos = std::strcspn(fileName.c_str(), "1234567890");
680         size_t itemPos = std::strcspn(fileName.c_str(), "_");
681 
682         if (numberPos > 0 && itemPos > numberPos && fileName.size() > itemPos)
683         {
684             return std::make_optional(
685                 std::make_tuple(fileName.substr(0, numberPos),
686                                 fileName.substr(numberPos, itemPos - numberPos),
687                                 fileName.substr(itemPos + 1, fileName.size())));
688         }
689     }
690     return std::nullopt;
691 }
692 
693 static void handleSpecialModeChange(const std::string& manufacturingModeStatus)
694 {
695     manufacturingMode = false;
696     if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
697                                    "SpecialMode.Modes.Manufacturing")
698     {
699         manufacturingMode = true;
700     }
701     if (validateUnsecureFeature == 1)
702     {
703         if (manufacturingModeStatus == "xyz.openbmc_project.Control.Security."
704                                        "SpecialMode.Modes.ValidationUnsecure")
705         {
706             manufacturingMode = true;
707         }
708     }
709 }
710 
711 void setupManufacturingModeMatch(sdbusplus::asio::connection& conn)
712 {
713     namespace rules = sdbusplus::bus::match::rules;
714     static constexpr const char* specialModeInterface =
715         "xyz.openbmc_project.Security.SpecialMode";
716 
717     const std::string filterSpecialModeIntfAdd =
718         rules::interfacesAdded() +
719         rules::argNpath(0, "/xyz/openbmc_project/security/special_mode");
720     static std::unique_ptr<sdbusplus::bus::match_t> specialModeIntfMatch =
721         std::make_unique<sdbusplus::bus::match_t>(conn,
722                                                   filterSpecialModeIntfAdd,
723                                                   [](sdbusplus::message_t& m) {
724         sdbusplus::message::object_path path;
725         using PropertyMap =
726             boost::container::flat_map<std::string, std::variant<std::string>>;
727         boost::container::flat_map<std::string, PropertyMap> interfaceAdded;
728         m.read(path, interfaceAdded);
729         auto intfItr = interfaceAdded.find(specialModeInterface);
730         if (intfItr == interfaceAdded.end())
731         {
732             return;
733         }
734         PropertyMap& propertyList = intfItr->second;
735         auto itr = propertyList.find("SpecialMode");
736         if (itr == propertyList.end())
737         {
738             std::cerr << "error getting  SpecialMode property "
739                       << "\n";
740             return;
741         }
742         auto* manufacturingModeStatus = std::get_if<std::string>(&itr->second);
743         handleSpecialModeChange(*manufacturingModeStatus);
744         });
745 
746     const std::string filterSpecialModeChange =
747         rules::type::signal() + rules::member("PropertiesChanged") +
748         rules::interface("org.freedesktop.DBus.Properties") +
749         rules::argN(0, specialModeInterface);
750     static std::unique_ptr<sdbusplus::bus::match_t> specialModeChangeMatch =
751         std::make_unique<sdbusplus::bus::match_t>(conn, filterSpecialModeChange,
752                                                   [](sdbusplus::message_t& m) {
753         std::string interfaceName;
754         boost::container::flat_map<std::string, std::variant<std::string>>
755             propertiesChanged;
756 
757         m.read(interfaceName, propertiesChanged);
758         auto itr = propertiesChanged.find("SpecialMode");
759         if (itr == propertiesChanged.end())
760         {
761             return;
762         }
763         auto* manufacturingModeStatus = std::get_if<std::string>(&itr->second);
764         handleSpecialModeChange(*manufacturingModeStatus);
765         });
766 
767     conn.async_method_call(
768         [](const boost::system::error_code ec,
769            const std::variant<std::string>& getManufactMode) {
770         if (ec)
771         {
772             std::cerr << "error getting  SpecialMode status " << ec.message()
773                       << "\n";
774             return;
775         }
776         const auto* manufacturingModeStatus =
777             std::get_if<std::string>(&getManufactMode);
778         handleSpecialModeChange(*manufacturingModeStatus);
779         },
780         "xyz.openbmc_project.SpecialMode",
781         "/xyz/openbmc_project/security/special_mode",
782         "org.freedesktop.DBus.Properties", "Get", specialModeInterface,
783         "SpecialMode");
784 }
785 
786 bool getManufacturingMode()
787 {
788     return manufacturingMode;
789 }
790 
791 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
792     setupPropertiesChangedMatches(
793         sdbusplus::asio::connection& bus, std::span<const char* const> types,
794         const std::function<void(sdbusplus::message_t&)>& handler)
795 {
796     std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches;
797     for (const char* type : types)
798     {
799         auto match = std::make_unique<sdbusplus::bus::match_t>(
800             static_cast<sdbusplus::bus_t&>(bus),
801             "type='signal',member='PropertiesChanged',path_namespace='" +
802                 std::string(inventoryPath) + "',arg0namespace='" +
803                 configInterfaceName(type) + "'",
804             handler);
805         matches.emplace_back(std::move(match));
806     }
807     return matches;
808 }
809 
810 std::vector<std::unique_ptr<sdbusplus::bus::match_t>>
811     setupPropertiesChangedMatches(
812         sdbusplus::asio::connection& bus, const I2CDeviceTypeMap& typeMap,
813         const std::function<void(sdbusplus::message_t&)>& handler)
814 {
815     std::vector<const char*> types;
816     types.reserve(typeMap.size());
817     for (const auto& [type, dt] : typeMap)
818     {
819         types.push_back(type.data());
820     }
821     return setupPropertiesChangedMatches(bus, {types}, handler);
822 }
823