| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 1 | #include "config.h" | 
 | 2 |  | 
 | 3 | #include "occ_finder.hpp" | 
 | 4 |  | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 5 | #include <algorithm> | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 6 | #include <experimental/filesystem> | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 7 | #include <iterator> | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> | 
 | 9 | #include <phosphor-logging/log.hpp> | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 10 | #include <sdbusplus/server.hpp> | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Common/error.hpp> | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 12 | namespace open_power | 
 | 13 | { | 
 | 14 | namespace occ | 
 | 15 | { | 
 | 16 | namespace finder | 
 | 17 | { | 
 | 18 |  | 
 | 19 | using namespace phosphor::logging; | 
 | 20 |  | 
 | 21 | constexpr auto toChar(size_t c) | 
 | 22 | { | 
 | 23 |     constexpr auto map = "0123456789abcdef"; | 
 | 24 |     return map[c]; | 
 | 25 | } | 
 | 26 |  | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 27 | template <typename T> | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 28 | T getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, | 
 | 29 |                   const std::string& objPath, const std::string& interface, | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 30 |                   const std::string& property) | 
 | 31 | { | 
 | 32 |     using namespace sdbusplus::xyz::openbmc_project::Common::Error; | 
 | 33 |  | 
 | 34 |     constexpr auto PROPERTY_INTF = "org.freedesktop.DBus.Properties"; | 
 | 35 |  | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 36 |     auto method = bus.new_method_call(service.c_str(), objPath.c_str(), | 
 | 37 |                                       PROPERTY_INTF, "Get"); | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 38 |     method.append(interface, property); | 
 | 39 |  | 
 | 40 |     auto reply = bus.call(method); | 
 | 41 |     if (reply.is_method_error()) | 
 | 42 |     { | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 43 |         log<level::ERR>("Failed to get property", | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 44 |                         entry("PROPERTY=%s", property.c_str()), | 
 | 45 |                         entry("PATH=%s", objPath.c_str()), | 
 | 46 |                         entry("INTERFACE=%s", interface.c_str())); | 
 | 47 |         elog<InternalFailure>(); | 
 | 48 |     } | 
 | 49 |  | 
 | 50 |     sdbusplus::message::variant<T> value; | 
 | 51 |     reply.read(value); | 
 | 52 |  | 
| Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame^] | 53 |     return std::get<T>(value); | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 54 | } | 
 | 55 |  | 
| Vishwanatha Subbanna | 1ec291f | 2017-08-21 17:07:29 +0530 | [diff] [blame] | 56 | std::vector<std::string> get(sdbusplus::bus::bus& bus) | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 57 | { | 
 | 58 |     namespace fs = std::experimental::filesystem; | 
 | 59 |     using Path = std::string; | 
 | 60 |     using Service = std::string; | 
 | 61 |     using Interface = std::string; | 
 | 62 |     using Interfaces = std::vector<Interface>; | 
 | 63 |  | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 64 |     auto mapper = | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 65 |         bus.new_method_call("xyz.openbmc_project.ObjectMapper", | 
 | 66 |                             "/xyz/openbmc_project/object_mapper", | 
 | 67 |                             "xyz.openbmc_project.ObjectMapper", "GetSubTree"); | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 68 |  | 
 | 69 |     auto depth = 0; | 
 | 70 |     Path path = CPU_SUBPATH; | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 71 |     Interfaces interfaces{ | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 72 |         "xyz.openbmc_project.Inventory.Item", | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 73 |         "xyz.openbmc_project.State.Decorator.OperationalStatus"}; | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 74 |  | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 75 |     mapper.append(path); | 
 | 76 |     mapper.append(depth); | 
 | 77 |     mapper.append(interfaces); | 
 | 78 |  | 
 | 79 |     auto result = bus.call(mapper); | 
 | 80 |     if (result.is_method_error()) | 
 | 81 |     { | 
 | 82 |         // It's okay to not have inventory, for example at BMC standby | 
 | 83 |         return {}; | 
 | 84 |     } | 
 | 85 |  | 
 | 86 |     using MapperResponse = std::map<Path, std::map<Service, Interfaces>>; | 
 | 87 |     MapperResponse response; | 
 | 88 |     result.read(response); | 
 | 89 |     if (response.empty()) | 
 | 90 |     { | 
 | 91 |         // It's okay to not have inventory, for example at BMC standby | 
 | 92 |         return {}; | 
 | 93 |     } | 
 | 94 |  | 
 | 95 |     std::vector<std::string> occs; | 
 | 96 |     for (auto count = 0; count < MAX_CPUS; ++count) | 
 | 97 |     { | 
 | 98 |         fs::path p(path); | 
 | 99 |         p /= std::string(CPU_NAME) + toChar(count); | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 100 |  | 
 | 101 |         auto entry = response.find(p.string()); | 
 | 102 |         if (response.end() != entry) | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 103 |         { | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 104 |             Criteria match{}; | 
 | 105 |             match.emplace_back(std::make_tuple( | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 106 |                 "xyz.openbmc_project.Inventory.Item", "Present", true)); | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 107 |  | 
| Deepak Kodihalli | 3f3fa56 | 2017-10-25 04:18:49 -0500 | [diff] [blame] | 108 |             // Select only if the CPU is marked 'Present'. | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 109 |             // Local variable to make it readable | 
 | 110 |             auto path = entry->first; | 
 | 111 |             auto service = entry->second.begin()->first; | 
 | 112 |             if (matchCriteria(bus, path, service, match)) | 
 | 113 |             { | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 114 |                 occs.emplace_back(std::string(OCC_NAME) + toChar(count)); | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 115 |             } | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 116 |         } | 
 | 117 |     } | 
 | 118 |  | 
 | 119 |     return occs; | 
 | 120 | } | 
 | 121 |  | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 122 | bool matchCriteria(sdbusplus::bus::bus& bus, const std::string& path, | 
 | 123 |                    const std::string& service, const Criteria& match) | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 124 | { | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 125 |     for (const auto& iter : match) | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 126 |     { | 
| Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 127 |         auto result = getDbusProperty<bool>( | 
 | 128 |             bus, service, path, std::get<0>(iter), std::get<1>(iter)); | 
| Vishwanatha Subbanna | 3ace757 | 2017-08-22 16:06:11 +0530 | [diff] [blame] | 129 |         if (result != std::get<2>(iter)) | 
 | 130 |         { | 
 | 131 |             return false; | 
 | 132 |         } | 
 | 133 |     } | 
 | 134 |     return true; | 
 | 135 | } | 
 | 136 |  | 
| Deepak Kodihalli | 5f031f3 | 2017-07-26 08:25:59 -0500 | [diff] [blame] | 137 | } // namespace finder | 
 | 138 | } // namespace occ | 
 | 139 | } // namespace open_power |