1 #pragma once 2 3 #include "VariantVisitors.hpp" 4 5 #include <boost/algorithm/string/replace.hpp> 6 #include <boost/asio/steady_timer.hpp> 7 #include <boost/container/flat_map.hpp> 8 #include <sdbusplus/asio/connection.hpp> 9 #include <sdbusplus/asio/object_server.hpp> 10 #include <sdbusplus/message/types.hpp> 11 12 #include <filesystem> 13 #include <functional> 14 #include <iostream> 15 #include <memory> 16 #include <optional> 17 #include <regex> 18 #include <span> 19 #include <string> 20 #include <tuple> 21 #include <utility> 22 #include <variant> 23 #include <vector> 24 25 const constexpr char* jsonStore = "/var/configuration/flattened.json"; 26 const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory"; 27 const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager"; 28 29 constexpr const char* cpuInventoryPath = 30 "/xyz/openbmc_project/inventory/system/chassis/motherboard"; 31 const std::regex illegalDbusRegex("[^A-Za-z0-9_]"); 32 33 using BasicVariantType = 34 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t, 35 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>; 36 using SensorBaseConfigMap = 37 boost::container::flat_map<std::string, BasicVariantType>; 38 using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>; 39 using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>; 40 using ManagedObjectType = 41 boost::container::flat_map<sdbusplus::message::object_path, SensorData>; 42 43 using GetSubTreeType = std::vector< 44 std::pair<std::string, 45 std::vector<std::pair<std::string, std::vector<std::string>>>>>; 46 using Association = std::tuple<std::string, std::string, std::string>; 47 48 inline std::string escapeName(const std::string& sensorName) 49 { 50 return boost::replace_all_copy(sensorName, " ", "_"); 51 } 52 53 enum class PowerState 54 { 55 on, 56 biosPost, 57 always, 58 chassisOn 59 }; 60 61 std::optional<std::string> openAndRead(const std::string& hwmonFile); 62 std::optional<std::string> 63 getFullHwmonFilePath(const std::string& directory, 64 const std::string& hwmonBaseName, 65 const std::set<std::string>& permitSet); 66 std::set<std::string> getPermitSet(const SensorBaseConfigMap& config); 67 bool findFiles(const std::filesystem::path& dirPath, 68 std::string_view matchString, 69 std::vector<std::filesystem::path>& foundPaths, 70 int symlinkDepth = 1); 71 bool isPowerOn(void); 72 bool hasBiosPost(void); 73 bool isChassisOn(void); 74 void setupPowerMatchCallback( 75 const std::shared_ptr<sdbusplus::asio::connection>& conn, 76 std::function<void(PowerState type, bool state)>&& callback); 77 void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); 78 bool getSensorConfiguration( 79 const std::string& type, 80 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 81 ManagedObjectType& resp, bool useCache); 82 83 bool getSensorConfiguration( 84 const std::string& type, 85 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 86 ManagedObjectType& resp); 87 88 void createAssociation( 89 std::shared_ptr<sdbusplus::asio::dbus_interface>& association, 90 const std::string& path); 91 92 // replaces limits if MinReading and MaxReading are found. 93 void findLimits(std::pair<double, double>& limits, 94 const SensorBaseConfiguration* data); 95 96 bool readingStateGood(const PowerState& powerState); 97 98 constexpr const char* configInterfacePrefix = 99 "xyz.openbmc_project.Configuration."; 100 101 inline std::string configInterfaceName(const std::string& type) 102 { 103 return std::string(configInterfacePrefix) + type; 104 } 105 106 namespace mapper 107 { 108 constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; 109 constexpr const char* path = "/xyz/openbmc_project/object_mapper"; 110 constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; 111 constexpr const char* subtree = "GetSubTree"; 112 } // namespace mapper 113 114 namespace properties 115 { 116 constexpr const char* interface = "org.freedesktop.DBus.Properties"; 117 constexpr const char* get = "Get"; 118 constexpr const char* set = "Set"; 119 } // namespace properties 120 121 namespace power 122 { 123 const static constexpr char* busname = "xyz.openbmc_project.State.Host"; 124 const static constexpr char* interface = "xyz.openbmc_project.State.Host"; 125 const static constexpr char* path = "/xyz/openbmc_project/state/host0"; 126 const static constexpr char* property = "CurrentHostState"; 127 } // namespace power 128 129 namespace chassis 130 { 131 const static constexpr char* busname = "xyz.openbmc_project.State.Chassis"; 132 const static constexpr char* interface = "xyz.openbmc_project.State.Chassis"; 133 const static constexpr char* path = "/xyz/openbmc_project/state/chassis0"; 134 const static constexpr char* property = "CurrentPowerState"; 135 const static constexpr char* sOn = "On"; 136 } // namespace chassis 137 138 namespace post 139 { 140 const static constexpr char* busname = 141 "xyz.openbmc_project.State.OperatingSystem"; 142 const static constexpr char* interface = 143 "xyz.openbmc_project.State.OperatingSystem.Status"; 144 const static constexpr char* path = "/xyz/openbmc_project/state/os"; 145 const static constexpr char* property = "OperatingSystemState"; 146 } // namespace post 147 148 namespace association 149 { 150 const static constexpr char* interface = 151 "xyz.openbmc_project.Association.Definitions"; 152 } // namespace association 153 154 template <typename T> 155 inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key) 156 { 157 auto it = data.find(key); 158 if (it == data.end()) 159 { 160 std::cerr << "Configuration missing " << key << "\n"; 161 throw std::invalid_argument("Key Missing"); 162 } 163 if constexpr (std::is_same_v<T, double>) 164 { 165 return std::visit(VariantToDoubleVisitor(), it->second); 166 } 167 else if constexpr (std::is_unsigned_v<T>) 168 { 169 return std::visit(VariantToUnsignedIntVisitor(), it->second); 170 } 171 else if constexpr (std::is_same_v<T, std::string>) 172 { 173 return std::visit(VariantToStringVisitor(), it->second); 174 } 175 else 176 { 177 static_assert(!std::is_same_v<T, T>, "Type Not Implemented"); 178 } 179 } 180 181 inline void setReadState(const std::string& str, PowerState& val) 182 { 183 184 if (str == "On") 185 { 186 val = PowerState::on; 187 } 188 else if (str == "BiosPost") 189 { 190 val = PowerState::biosPost; 191 } 192 else if (str == "Always") 193 { 194 val = PowerState::always; 195 } 196 else if (str == "ChassisOn") 197 { 198 val = PowerState::chassisOn; 199 } 200 } 201 202 inline PowerState getPowerState(const SensorBaseConfigMap& cfg) 203 { 204 PowerState state = PowerState::always; 205 auto findPowerState = cfg.find("PowerState"); 206 if (findPowerState != cfg.end()) 207 { 208 std::string powerState = 209 std::visit(VariantToStringVisitor(), findPowerState->second); 210 setReadState(powerState, state); 211 } 212 return state; 213 } 214 215 inline float getPollRate(const SensorBaseConfigMap& cfg, float dflt) 216 { 217 float pollRate = dflt; 218 auto findPollRate = cfg.find("PollRate"); 219 if (findPollRate != cfg.end()) 220 { 221 pollRate = std::visit(VariantToFloatVisitor(), findPollRate->second); 222 if (!std::isfinite(pollRate) || pollRate <= 0.0F) 223 { 224 pollRate = dflt; // poll time invalid, fall back to default 225 } 226 } 227 return pollRate; 228 } 229 230 inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn, 231 const std::string& name, bool on) 232 { 233 conn->async_method_call( 234 [name](const boost::system::error_code ec) { 235 if (ec) 236 { 237 std::cerr << "Failed to set LED " << name << "\n"; 238 } 239 }, 240 "xyz.openbmc_project.LED.GroupManager", 241 "/xyz/openbmc_project/led/groups/" + name, properties::interface, 242 properties::set, "xyz.openbmc_project.Led.Group", "Asserted", 243 std::variant<bool>(on)); 244 } 245 246 void createInventoryAssoc( 247 const std::shared_ptr<sdbusplus::asio::connection>& conn, 248 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association, 249 const std::string& path); 250 251 struct GetSensorConfiguration : 252 std::enable_shared_from_this<GetSensorConfiguration> 253 { 254 GetSensorConfiguration( 255 std::shared_ptr<sdbusplus::asio::connection> connection, 256 std::function<void(ManagedObjectType& resp)>&& callbackFunc) : 257 dbusConnection(std::move(connection)), 258 callback(std::move(callbackFunc)) 259 {} 260 261 void getPath(const std::string& path, const std::string& interface, 262 const std::string& owner, size_t retries = 5) 263 { 264 if (retries > 5) 265 { 266 retries = 5; 267 } 268 std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); 269 270 self->dbusConnection->async_method_call( 271 [self, path, interface, owner, retries]( 272 const boost::system::error_code ec, SensorBaseConfigMap& data) { 273 if (ec) 274 { 275 std::cerr << "Error getting " << path << ": retries left" 276 << retries - 1 << "\n"; 277 if (retries == 0U) 278 { 279 return; 280 } 281 auto timer = std::make_shared<boost::asio::steady_timer>( 282 self->dbusConnection->get_io_context()); 283 timer->expires_after(std::chrono::seconds(10)); 284 timer->async_wait([self, timer, path, interface, owner, 285 retries](boost::system::error_code ec) { 286 if (ec) 287 { 288 std::cerr << "Timer error!\n"; 289 return; 290 } 291 self->getPath(path, interface, owner, retries - 1); 292 }); 293 return; 294 } 295 296 self->respData[path][interface] = std::move(data); 297 }, 298 owner, path, "org.freedesktop.DBus.Properties", "GetAll", 299 interface); 300 } 301 302 void getConfiguration(const std::vector<std::string>& types, 303 size_t retries = 0) 304 { 305 if (retries > 5) 306 { 307 retries = 5; 308 } 309 310 std::vector<std::string> interfaces(types.size()); 311 for (const auto& type : types) 312 { 313 interfaces.push_back(configInterfaceName(type)); 314 } 315 316 std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); 317 dbusConnection->async_method_call( 318 [self, interfaces, retries](const boost::system::error_code ec, 319 const GetSubTreeType& ret) { 320 if (ec) 321 { 322 std::cerr << "Error calling mapper\n"; 323 if (retries == 0U) 324 { 325 return; 326 } 327 auto timer = std::make_shared<boost::asio::steady_timer>( 328 self->dbusConnection->get_io_context()); 329 timer->expires_after(std::chrono::seconds(10)); 330 timer->async_wait([self, timer, interfaces, 331 retries](boost::system::error_code ec) { 332 if (ec) 333 { 334 std::cerr << "Timer error!\n"; 335 return; 336 } 337 self->getConfiguration(interfaces, retries - 1); 338 }); 339 340 return; 341 } 342 for (const auto& [path, objDict] : ret) 343 { 344 if (objDict.empty()) 345 { 346 return; 347 } 348 const std::string& owner = objDict.begin()->first; 349 350 for (const std::string& interface : objDict.begin()->second) 351 { 352 // anything that starts with a requested configuration 353 // is good 354 if (std::find_if(interfaces.begin(), interfaces.end(), 355 [interface](const std::string& possible) { 356 return interface.starts_with(possible); 357 }) == interfaces.end()) 358 { 359 continue; 360 } 361 self->getPath(path, interface, owner); 362 } 363 } 364 }, 365 mapper::busName, mapper::path, mapper::interface, mapper::subtree, 366 "/", 0, interfaces); 367 } 368 369 ~GetSensorConfiguration() 370 { 371 callback(respData); 372 } 373 374 std::shared_ptr<sdbusplus::asio::connection> dbusConnection; 375 std::function<void(ManagedObjectType& resp)> callback; 376 ManagedObjectType respData; 377 }; 378 379 // The common scheme for sysfs files naming is: <type><number>_<item>. 380 // This function returns optionally these 3 elements as a tuple. 381 std::optional<std::tuple<std::string, std::string, std::string>> 382 splitFileName(const std::filesystem::path& filePath); 383 std::optional<double> readFile(const std::string& thresholdFile, 384 const double& scaleFactor); 385 void setupManufacturingModeMatch(sdbusplus::asio::connection& conn); 386 bool getManufacturingMode(); 387 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> 388 setupPropertiesChangedMatches( 389 sdbusplus::asio::connection& bus, std::span<const char* const> types, 390 const std::function<void(sdbusplus::message_t&)>& handler); 391