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(); 72 bool hasBiosPost(); 73 bool isChassisOn(); 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 void createAssociation( 84 std::shared_ptr<sdbusplus::asio::dbus_interface>& association, 85 const std::string& path); 86 87 // replaces limits if MinReading and MaxReading are found. 88 void findLimits(std::pair<double, double>& limits, 89 const SensorBaseConfiguration* data); 90 91 bool readingStateGood(const PowerState& powerState); 92 93 constexpr const char* configInterfacePrefix = 94 "xyz.openbmc_project.Configuration."; 95 96 inline std::string configInterfaceName(const std::string& type) 97 { 98 return std::string(configInterfacePrefix) + type; 99 } 100 101 namespace mapper 102 { 103 constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; 104 constexpr const char* path = "/xyz/openbmc_project/object_mapper"; 105 constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; 106 constexpr const char* subtree = "GetSubTree"; 107 } // namespace mapper 108 109 namespace properties 110 { 111 constexpr const char* interface = "org.freedesktop.DBus.Properties"; 112 constexpr const char* get = "Get"; 113 constexpr const char* set = "Set"; 114 } // namespace properties 115 116 namespace power 117 { 118 const static constexpr char* busname = "xyz.openbmc_project.State.Host0"; 119 const static constexpr char* interface = "xyz.openbmc_project.State.Host"; 120 const static constexpr char* path = "/xyz/openbmc_project/state/host0"; 121 const static constexpr char* property = "CurrentHostState"; 122 } // namespace power 123 124 namespace chassis 125 { 126 const static constexpr char* busname = "xyz.openbmc_project.State.Chassis0"; 127 const static constexpr char* interface = "xyz.openbmc_project.State.Chassis"; 128 const static constexpr char* path = "/xyz/openbmc_project/state/chassis0"; 129 const static constexpr char* property = "CurrentPowerState"; 130 const static constexpr char* sOn = ".On"; 131 } // namespace chassis 132 133 namespace post 134 { 135 const static constexpr char* busname = "xyz.openbmc_project.State.Host0"; 136 const static constexpr char* interface = 137 "xyz.openbmc_project.State.OperatingSystem.Status"; 138 const static constexpr char* path = "/xyz/openbmc_project/state/host0"; 139 const static constexpr char* property = "OperatingSystemState"; 140 } // namespace post 141 142 namespace association 143 { 144 const static constexpr char* interface = 145 "xyz.openbmc_project.Association.Definitions"; 146 } // namespace association 147 148 template <typename T> 149 inline T loadVariant(const SensorBaseConfigMap& data, const std::string& key) 150 { 151 auto it = data.find(key); 152 if (it == data.end()) 153 { 154 std::cerr << "Configuration missing " << key << "\n"; 155 throw std::invalid_argument("Key Missing"); 156 } 157 if constexpr (std::is_same_v<T, double>) 158 { 159 return std::visit(VariantToDoubleVisitor(), it->second); 160 } 161 else if constexpr (std::is_unsigned_v<T>) 162 { 163 return std::visit(VariantToUnsignedIntVisitor(), it->second); 164 } 165 else if constexpr (std::is_same_v<T, std::string>) 166 { 167 return std::visit(VariantToStringVisitor(), it->second); 168 } 169 else 170 { 171 static_assert(!std::is_same_v<T, T>, "Type Not Implemented"); 172 } 173 } 174 175 inline void setReadState(const std::string& str, PowerState& val) 176 { 177 if (str == "On") 178 { 179 val = PowerState::on; 180 } 181 else if (str == "BiosPost") 182 { 183 val = PowerState::biosPost; 184 } 185 else if (str == "Always") 186 { 187 val = PowerState::always; 188 } 189 else if (str == "ChassisOn") 190 { 191 val = PowerState::chassisOn; 192 } 193 } 194 195 inline PowerState getPowerState(const SensorBaseConfigMap& cfg) 196 { 197 PowerState state = PowerState::always; 198 auto findPowerState = cfg.find("PowerState"); 199 if (findPowerState != cfg.end()) 200 { 201 std::string powerState = std::visit(VariantToStringVisitor(), 202 findPowerState->second); 203 setReadState(powerState, state); 204 } 205 return state; 206 } 207 208 inline float getPollRate(const SensorBaseConfigMap& cfg, float dflt) 209 { 210 float pollRate = dflt; 211 auto findPollRate = cfg.find("PollRate"); 212 if (findPollRate != cfg.end()) 213 { 214 pollRate = std::visit(VariantToFloatVisitor(), findPollRate->second); 215 if (!std::isfinite(pollRate) || pollRate <= 0.0F) 216 { 217 pollRate = dflt; // poll time invalid, fall back to default 218 } 219 } 220 return pollRate; 221 } 222 223 inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn, 224 const std::string& name, bool on) 225 { 226 conn->async_method_call( 227 [name](const boost::system::error_code ec) { 228 if (ec) 229 { 230 std::cerr << "Failed to set LED " << name << "\n"; 231 } 232 }, 233 "xyz.openbmc_project.LED.GroupManager", 234 "/xyz/openbmc_project/led/groups/" + name, properties::interface, 235 properties::set, "xyz.openbmc_project.Led.Group", "Asserted", 236 std::variant<bool>(on)); 237 } 238 239 void createInventoryAssoc( 240 const std::shared_ptr<sdbusplus::asio::connection>& conn, 241 const std::shared_ptr<sdbusplus::asio::dbus_interface>& association, 242 const std::string& path); 243 244 struct GetSensorConfiguration : 245 std::enable_shared_from_this<GetSensorConfiguration> 246 { 247 GetSensorConfiguration( 248 std::shared_ptr<sdbusplus::asio::connection> connection, 249 std::function<void(ManagedObjectType& resp)>&& callbackFunc) : 250 dbusConnection(std::move(connection)), 251 callback(std::move(callbackFunc)) 252 {} 253 254 void getPath(const std::string& path, const std::string& interface, 255 const std::string& owner, size_t retries = 5) 256 { 257 if (retries > 5) 258 { 259 retries = 5; 260 } 261 std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); 262 263 self->dbusConnection->async_method_call( 264 [self, path, interface, owner, retries]( 265 const boost::system::error_code ec, SensorBaseConfigMap& data) { 266 if (ec) 267 { 268 std::cerr << "Error getting " << path << ": retries left" 269 << retries - 1 << "\n"; 270 if (retries == 0U) 271 { 272 return; 273 } 274 auto timer = std::make_shared<boost::asio::steady_timer>( 275 self->dbusConnection->get_io_context()); 276 timer->expires_after(std::chrono::seconds(10)); 277 timer->async_wait([self, timer, path, interface, owner, 278 retries](boost::system::error_code ec) { 279 if (ec) 280 { 281 std::cerr << "Timer error!\n"; 282 return; 283 } 284 self->getPath(path, interface, owner, retries - 1); 285 }); 286 return; 287 } 288 289 self->respData[path][interface] = std::move(data); 290 }, 291 owner, path, "org.freedesktop.DBus.Properties", "GetAll", 292 interface); 293 } 294 295 void getConfiguration(const std::vector<std::string>& types, 296 size_t retries = 0) 297 { 298 if (retries > 5) 299 { 300 retries = 5; 301 } 302 303 std::vector<std::string> interfaces(types.size()); 304 for (const auto& type : types) 305 { 306 interfaces.push_back(configInterfaceName(type)); 307 } 308 309 std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); 310 dbusConnection->async_method_call( 311 [self, interfaces, retries](const boost::system::error_code ec, 312 const GetSubTreeType& ret) { 313 if (ec) 314 { 315 std::cerr << "Error calling mapper\n"; 316 if (retries == 0U) 317 { 318 return; 319 } 320 auto timer = std::make_shared<boost::asio::steady_timer>( 321 self->dbusConnection->get_io_context()); 322 timer->expires_after(std::chrono::seconds(10)); 323 timer->async_wait([self, timer, interfaces, 324 retries](boost::system::error_code ec) { 325 if (ec) 326 { 327 std::cerr << "Timer error!\n"; 328 return; 329 } 330 self->getConfiguration(interfaces, retries - 1); 331 }); 332 333 return; 334 } 335 for (const auto& [path, objDict] : ret) 336 { 337 if (objDict.empty()) 338 { 339 return; 340 } 341 const std::string& owner = objDict.begin()->first; 342 343 for (const std::string& interface : objDict.begin()->second) 344 { 345 // anything that starts with a requested configuration 346 // is good 347 if (std::find_if(interfaces.begin(), interfaces.end(), 348 [interface](const std::string& possible) { 349 return interface.starts_with(possible); 350 }) == interfaces.end()) 351 { 352 continue; 353 } 354 self->getPath(path, interface, owner); 355 } 356 } 357 }, 358 mapper::busName, mapper::path, mapper::interface, mapper::subtree, 359 "/", 0, interfaces); 360 } 361 362 ~GetSensorConfiguration() 363 { 364 callback(respData); 365 } 366 367 std::shared_ptr<sdbusplus::asio::connection> dbusConnection; 368 std::function<void(ManagedObjectType& resp)> callback; 369 ManagedObjectType respData; 370 }; 371 372 // The common scheme for sysfs files naming is: <type><number>_<item>. 373 // This function returns optionally these 3 elements as a tuple. 374 std::optional<std::tuple<std::string, std::string, std::string>> 375 splitFileName(const std::filesystem::path& filePath); 376 std::optional<double> readFile(const std::string& thresholdFile, 377 const double& scaleFactor); 378 void setupManufacturingModeMatch(sdbusplus::asio::connection& conn); 379 bool getManufacturingMode(); 380 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> 381 setupPropertiesChangedMatches( 382 sdbusplus::asio::connection& bus, std::span<const char* const> types, 383 const std::function<void(sdbusplus::message_t&)>& handler); 384 385 template <typename T> 386 bool getDeviceBusAddr(const std::string& deviceName, T& bus, T& addr) 387 { 388 auto findHyphen = deviceName.find('-'); 389 if (findHyphen == std::string::npos) 390 { 391 std::cerr << "found bad device " << deviceName << "\n"; 392 return false; 393 } 394 std::string busStr = deviceName.substr(0, findHyphen); 395 std::string addrStr = deviceName.substr(findHyphen + 1); 396 397 std::from_chars_result res{}; 398 res = std::from_chars(&*busStr.begin(), &*busStr.end(), bus); 399 if (res.ec != std::errc{} || res.ptr != &*busStr.end()) 400 { 401 std::cerr << "Error finding bus for " << deviceName << "\n"; 402 return false; 403 } 404 res = std::from_chars(&*addrStr.begin(), &*addrStr.end(), addr, 16); 405 if (res.ec != std::errc{} || res.ptr != &*addrStr.end()) 406 { 407 std::cerr << "Error finding addr for " << deviceName << "\n"; 408 return false; 409 } 410 411 return true; 412 } 413