Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 5 | #include <libpldm/base.h> |
| 6 | #include <libpldm/bios.h> |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 7 | #include <libpldm/entity.h> |
| 8 | #include <libpldm/pdr.h> |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 9 | #include <libpldm/platform.h> |
| 10 | #include <libpldm/utils.h> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 11 | #include <stdint.h> |
| 12 | #include <systemd/sd-bus.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 13 | #include <unistd.h> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 14 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 15 | #include <nlohmann/json.hpp> |
| 16 | #include <sdbusplus/server.hpp> |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Inventory/Manager/client.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 18 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 19 | #include <xyz/openbmc_project/ObjectMapper/client.hpp> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 20 | |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 21 | #include <deque> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 22 | #include <exception> |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 23 | #include <filesystem> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 24 | #include <iostream> |
George Liu | df9a6d3 | 2020-12-22 16:27:16 +0800 | [diff] [blame] | 25 | #include <map> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 26 | #include <string> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 27 | #include <variant> |
| 28 | #include <vector> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 29 | |
vkaverap@in.ibm.com | 5b71b86 | 2023-08-21 05:19:04 +0000 | [diff] [blame] | 30 | constexpr uint64_t dbusTimeout = |
| 31 | std::chrono::duration_cast<std::chrono::microseconds>( |
| 32 | std::chrono::seconds(DBUS_TIMEOUT)) |
| 33 | .count(); |
| 34 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 35 | namespace pldm |
| 36 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 37 | namespace utils |
| 38 | { |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 39 | namespace fs = std::filesystem; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 40 | using Json = nlohmann::json; |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 41 | constexpr bool Tx = true; |
| 42 | constexpr bool Rx = false; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 43 | using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 44 | using inventoryManager = |
| 45 | sdbusplus::client::xyz::openbmc_project::inventory::Manager<>; |
| 46 | |
| 47 | constexpr auto dbusProperties = "org.freedesktop.DBus.Properties"; |
| 48 | constexpr auto mapperService = ObjectMapper::default_service; |
| 49 | constexpr auto inventoryPath = "/xyz/openbmc_project/inventory"; |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 50 | /** @struct CustomFD |
| 51 | * |
| 52 | * RAII wrapper for file descriptor. |
| 53 | */ |
| 54 | struct CustomFD |
| 55 | { |
| 56 | CustomFD(const CustomFD&) = delete; |
| 57 | CustomFD& operator=(const CustomFD&) = delete; |
| 58 | CustomFD(CustomFD&&) = delete; |
| 59 | CustomFD& operator=(CustomFD&&) = delete; |
| 60 | |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 61 | CustomFD(int fd) : fd(fd) {} |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 62 | |
| 63 | ~CustomFD() |
| 64 | { |
| 65 | if (fd >= 0) |
| 66 | { |
| 67 | close(fd); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | int operator()() const |
| 72 | { |
| 73 | return fd; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | int fd = -1; |
| 78 | }; |
| 79 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 80 | /** @brief Calculate the pad for PLDM data |
| 81 | * |
| 82 | * @param[in] data - Length of the data |
| 83 | * @return - uint8_t - number of pad bytes |
| 84 | */ |
| 85 | uint8_t getNumPadBytes(uint32_t data); |
| 86 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 87 | /** @brief Convert uint64 to date |
| 88 | * |
| 89 | * @param[in] data - time date of uint64 |
| 90 | * @param[out] year - year number in dec |
| 91 | * @param[out] month - month number in dec |
| 92 | * @param[out] day - day of the month in dec |
| 93 | * @param[out] hour - number of hours in dec |
| 94 | * @param[out] min - number of minutes in dec |
| 95 | * @param[out] sec - number of seconds in dec |
Manojkiran Eda | 2576aec | 2024-06-17 12:05:17 +0530 | [diff] [blame] | 96 | * @return true if decode success, false if decode failed |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 97 | */ |
| 98 | bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day, |
| 99 | uint8_t* hour, uint8_t* min, uint8_t* sec); |
| 100 | |
| 101 | /** @brief Convert effecter data to structure of set_effecter_state_field |
| 102 | * |
| 103 | * @param[in] effecterData - the date of effecter |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 104 | * @param[in] effecterCount - the number of individual sets of effecter |
| 105 | * information |
| 106 | * @return[out] parse success and get a valid set_effecter_state_field |
| 107 | * structure, return nullopt means parse failed |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 108 | */ |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 109 | std::optional<std::vector<set_effecter_state_field>> |
| 110 | parseEffecterData(const std::vector<uint8_t>& effecterData, |
| 111 | uint8_t effecterCount); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 112 | |
| 113 | /** |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 114 | * @brief creates an error log |
| 115 | * @param[in] errorMsg - the error message |
| 116 | */ |
Manojkiran Eda | 92fb0b5 | 2024-04-17 10:48:17 +0530 | [diff] [blame] | 117 | void reportError(const char* errorMsg); |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 118 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 119 | /** @brief Convert any Decimal number to BCD |
| 120 | * |
| 121 | * @tparam[in] decimal - Decimal number |
| 122 | * @return Corresponding BCD number |
| 123 | */ |
| 124 | template <typename T> |
| 125 | T decimalToBcd(T decimal) |
| 126 | { |
| 127 | T bcd = 0; |
| 128 | T rem = 0; |
| 129 | auto cnt = 0; |
| 130 | |
| 131 | while (decimal) |
| 132 | { |
| 133 | rem = decimal % 10; |
| 134 | bcd = bcd + (rem << cnt); |
| 135 | decimal = decimal / 10; |
| 136 | cnt += 4; |
| 137 | } |
| 138 | |
| 139 | return bcd; |
| 140 | } |
| 141 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 142 | struct DBusMapping |
| 143 | { |
| 144 | std::string objectPath; //!< D-Bus object path |
| 145 | std::string interface; //!< D-Bus interface |
| 146 | std::string propertyName; //!< D-Bus property name |
| 147 | std::string propertyType; //!< D-Bus property type |
| 148 | }; |
| 149 | |
| 150 | using PropertyValue = |
| 151 | std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 152 | uint64_t, double, std::string, std::vector<uint8_t>, |
| 153 | std::vector<std::string>>; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 154 | using DbusProp = std::string; |
| 155 | using DbusChangedProps = std::map<DbusProp, PropertyValue>; |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 156 | using DBusInterfaceAdded = std::vector< |
| 157 | std::pair<pldm::dbus::Interface, |
| 158 | std::vector<std::pair<pldm::dbus::Property, |
| 159 | std::variant<pldm::dbus::Property>>>>>; |
Kamalkumar Patel | 7d427f1 | 2024-05-16 03:44:00 -0500 | [diff] [blame] | 160 | |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 161 | using ObjectPath = std::string; |
Kamalkumar Patel | 7d427f1 | 2024-05-16 03:44:00 -0500 | [diff] [blame] | 162 | using EntityName = std::string; |
| 163 | using Entities = std::vector<pldm_entity_node*>; |
| 164 | using EntityAssociations = std::vector<Entities>; |
| 165 | using ObjectPathMaps = std::map<fs::path, pldm_entity_node*>; |
| 166 | using EntityMaps = std::map<pldm::pdr::EntityType, EntityName>; |
| 167 | |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 168 | using ServiceName = std::string; |
| 169 | using Interfaces = std::vector<std::string>; |
| 170 | using MapperServiceMap = std::vector<std::pair<ServiceName, Interfaces>>; |
| 171 | using GetSubTreeResponse = std::vector<std::pair<ObjectPath, MapperServiceMap>>; |
Pavithra Barithaya | 2ec8269 | 2024-04-29 06:31:10 -0500 | [diff] [blame] | 172 | using GetSubTreePathsResponse = std::vector<std::string>; |
Sridevi Ramesh | eefe49b | 2022-06-27 11:51:02 -0500 | [diff] [blame] | 173 | using PropertyMap = std::map<std::string, PropertyValue>; |
| 174 | using InterfaceMap = std::map<std::string, PropertyMap>; |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 175 | using ObjectValueTree = std::map<sdbusplus::message::object_path, InterfaceMap>; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * @brief The interface for DBusHandler |
| 179 | */ |
| 180 | class DBusHandlerInterface |
| 181 | { |
| 182 | public: |
| 183 | virtual ~DBusHandlerInterface() = default; |
| 184 | |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 185 | virtual std::string getService(const char* path, |
| 186 | const char* interface) const = 0; |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 187 | virtual GetSubTreeResponse |
| 188 | getSubtree(const std::string& path, int depth, |
| 189 | const std::vector<std::string>& ifaceList) const = 0; |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 190 | |
Pavithra Barithaya | 2ec8269 | 2024-04-29 06:31:10 -0500 | [diff] [blame] | 191 | virtual GetSubTreePathsResponse |
| 192 | getSubTreePaths(const std::string& objectPath, int depth, |
| 193 | const std::vector<std::string>& ifaceList) const = 0; |
| 194 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 195 | virtual void setDbusProperty(const DBusMapping& dBusMap, |
| 196 | const PropertyValue& value) const = 0; |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 197 | |
| 198 | virtual PropertyValue |
| 199 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 200 | const char* dbusInterface) const = 0; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 201 | |
| 202 | virtual PropertyMap |
| 203 | getDbusPropertiesVariant(const char* serviceName, const char* objPath, |
| 204 | const char* dbusInterface) const = 0; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 205 | }; |
| 206 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 207 | /** |
| 208 | * @class DBusHandler |
| 209 | * |
| 210 | * Wrapper class to handle the D-Bus calls |
| 211 | * |
| 212 | * This class contains the APIs to handle the D-Bus calls |
| 213 | * to cater the request from pldm requester. |
| 214 | * A class is created to mock the apis in the test cases |
| 215 | */ |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 216 | class DBusHandler : public DBusHandlerInterface |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 217 | { |
| 218 | public: |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 219 | /** @brief Get the bus connection. */ |
| 220 | static auto& getBus() |
| 221 | { |
| 222 | static auto bus = sdbusplus::bus::new_default(); |
| 223 | return bus; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @brief Get the DBUS Service name for the input dbus path |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 228 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 229 | * @param[in] path - DBUS object path |
| 230 | * @param[in] interface - DBUS Interface |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 231 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 232 | * @return std::string - the dbus service name |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 233 | * |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 234 | * @throw sdbusplus::exception_t when it fails |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 235 | */ |
George Liu | 36e8135 | 2020-07-01 14:40:30 +0800 | [diff] [blame] | 236 | std::string getService(const char* path, |
| 237 | const char* interface) const override; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 238 | |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 239 | /** |
| 240 | * @brief Get the Subtree response from the mapper |
| 241 | * |
| 242 | * @param[in] path - DBUS object path |
| 243 | * @param[in] depth - Search depth |
| 244 | * @param[in] ifaceList - list of the interface that are being |
| 245 | * queried from the mapper |
| 246 | * |
| 247 | * @return GetSubTreeResponse - the mapper subtree response |
| 248 | * |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 249 | * @throw sdbusplus::exception_t when it fails |
Manojkiran Eda | 1ef62c3 | 2021-04-24 07:23:18 +0530 | [diff] [blame] | 250 | */ |
| 251 | GetSubTreeResponse |
| 252 | getSubtree(const std::string& path, int depth, |
| 253 | const std::vector<std::string>& ifaceList) const override; |
| 254 | |
Pavithra Barithaya | 2ec8269 | 2024-04-29 06:31:10 -0500 | [diff] [blame] | 255 | /** @brief Get Subtree path response from the mapper |
| 256 | * |
| 257 | * @param[in] path - DBUS object path |
| 258 | * @param[in] depth - Search depth |
| 259 | * @param[in] ifaceList - list of the interface that are being |
| 260 | * queried from the mapper |
| 261 | * |
| 262 | * @return std::vector<std::string> vector of subtree paths |
| 263 | */ |
| 264 | GetSubTreePathsResponse getSubTreePaths( |
| 265 | const std::string& objectPath, int depth, |
| 266 | const std::vector<std::string>& ifaceList) const override; |
| 267 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 268 | /** @brief Get property(type: variant) from the requested dbus |
| 269 | * |
| 270 | * @param[in] objPath - The Dbus object path |
| 271 | * @param[in] dbusProp - The property name to get |
| 272 | * @param[in] dbusInterface - The Dbus interface |
| 273 | * |
| 274 | * @return The value of the property(type: variant) |
| 275 | * |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 276 | * @throw sdbusplus::exception_t when it fails |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 277 | */ |
| 278 | PropertyValue |
| 279 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 280 | const char* dbusInterface) const override; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 281 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 282 | /** @brief Get All properties(type: variant) from the requested dbus |
| 283 | * |
| 284 | * @param[in] serviceName - The Dbus service name |
| 285 | * @param[in] objPath - The Dbus object path |
| 286 | * @param[in] dbusInterface - The Dbus interface |
| 287 | * |
| 288 | * @return The values of the properties(type: variant) |
| 289 | * |
| 290 | * @throw sdbusplus::exception_t when it fails |
| 291 | */ |
| 292 | PropertyMap |
| 293 | getDbusPropertiesVariant(const char* serviceName, const char* objPath, |
| 294 | const char* dbusInterface) const override; |
| 295 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 296 | /** @brief The template function to get property from the requested dbus |
| 297 | * path |
| 298 | * |
| 299 | * @tparam Property - Excepted type of the property on dbus |
| 300 | * |
| 301 | * @param[in] objPath - The Dbus object path |
| 302 | * @param[in] dbusProp - The property name to get |
| 303 | * @param[in] dbusInterface - The Dbus interface |
| 304 | * |
| 305 | * @return The value of the property |
| 306 | * |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 307 | * @throw sdbusplus::exception_t when dbus request fails |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 308 | * std::bad_variant_access when \p Property and property on dbus do |
| 309 | * not match |
| 310 | */ |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 311 | template <typename Property> |
| 312 | auto getDbusProperty(const char* objPath, const char* dbusProp, |
| 313 | const char* dbusInterface) |
| 314 | { |
Patrick Williams | 6da4f91 | 2023-05-10 07:50:53 -0500 | [diff] [blame] | 315 | auto VariantValue = getDbusPropertyVariant(objPath, dbusProp, |
| 316 | dbusInterface); |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 317 | return std::get<Property>(VariantValue); |
| 318 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 319 | |
| 320 | /** @brief Set Dbus property |
| 321 | * |
| 322 | * @param[in] dBusMap - Object path, property name, interface and property |
| 323 | * type for the D-Bus object |
| 324 | * @param[in] value - The value to be set |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 325 | * |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 326 | * @throw sdbusplus::exception_t when it fails |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 327 | */ |
| 328 | void setDbusProperty(const DBusMapping& dBusMap, |
| 329 | const PropertyValue& value) const override; |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 330 | |
| 331 | /** @brief This function retrieves the properties of an object managed |
| 332 | * by the specified D-Bus service located at the given object path. |
| 333 | * |
| 334 | * @param[in] service - The D-Bus service providing the managed object |
| 335 | * @param[in] value - The object path of the managed object |
| 336 | * |
| 337 | * @return A hierarchical structure representing the properties of the |
| 338 | * managed object. |
Patrick Williams | 897b0f8 | 2024-04-14 02:31:48 -0500 | [diff] [blame] | 339 | * @throw sdbusplus::exception_t when it fails |
Riya Dixit | 754041d | 2024-02-20 06:15:49 -0600 | [diff] [blame] | 340 | */ |
| 341 | static ObjectValueTree getManagedObj(const char* service, const char* path); |
| 342 | |
| 343 | /** @brief Retrieve the inventory objects managed by a specified class. |
| 344 | * The retrieved inventory objects are cached statically |
| 345 | * and returned upon subsequent calls to this function. |
| 346 | * |
| 347 | * @tparam ClassType - The class type that manages the inventory objects. |
| 348 | * |
| 349 | * @return A reference to the cached inventory objects. |
| 350 | */ |
| 351 | template <typename ClassType> |
| 352 | static auto& getInventoryObjects() |
| 353 | { |
| 354 | static ObjectValueTree object = ClassType::getManagedObj( |
| 355 | inventoryManager::interface, inventoryPath); |
| 356 | return object; |
| 357 | } |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 358 | }; |
| 359 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 360 | /** @brief Fetch parent D-Bus object based on pathname |
| 361 | * |
| 362 | * @param[in] dbusObj - child D-Bus object |
| 363 | * |
| 364 | * @return std::string - the parent D-Bus object path |
| 365 | */ |
| 366 | inline std::string findParent(const std::string& dbusObj) |
| 367 | { |
| 368 | fs::path p(dbusObj); |
| 369 | return p.parent_path().string(); |
| 370 | } |
| 371 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 372 | /** @brief Read (static) MCTP EID of host firmware from a file |
| 373 | * |
| 374 | * @return uint8_t - MCTP EID |
| 375 | */ |
| 376 | uint8_t readHostEID(); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 377 | |
Gilbert Chen | 6c7fed4 | 2022-02-22 15:40:17 +0000 | [diff] [blame] | 378 | /** @brief Validate the MCTP EID of MCTP endpoint |
| 379 | * In `Table 2 - Special endpoint IDs` of DSP0236. EID 0 is NULL_EID. |
| 380 | * EID from 1 to 7 is reserved EID. EID 0xFF is broadcast EID. |
| 381 | * Those are invalid EID of one MCTP Endpoint. |
| 382 | * |
| 383 | * @param[in] eid - MCTP EID |
| 384 | * |
| 385 | * @return true if the MCTP EID is valid otherwise return false. |
| 386 | */ |
| 387 | bool isValidEID(eid mctpEid); |
| 388 | |
TOM JOSEPH | d4d97a5 | 2020-03-23 14:36:34 +0530 | [diff] [blame] | 389 | /** @brief Convert a value in the JSON to a D-Bus property value |
| 390 | * |
| 391 | * @param[in] type - type of the D-Bus property |
| 392 | * @param[in] value - value in the JSON file |
| 393 | * |
| 394 | * @return PropertyValue - the D-Bus property value |
| 395 | */ |
| 396 | PropertyValue jsonEntryToDbusVal(std::string_view type, |
| 397 | const nlohmann::json& value); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 398 | |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 399 | /** @brief Find State Effecter PDR |
| 400 | * @param[in] tid - PLDM terminus ID. |
| 401 | * @param[in] entityID - entity that can be associated with PLDM State set. |
| 402 | * @param[in] stateSetId - value that identifies PLDM State set. |
| 403 | * @param[in] repo - pointer to BMC's primary PDR repo. |
| 404 | * @return array[array[uint8_t]] - StateEffecterPDRs |
| 405 | */ |
| 406 | std::vector<std::vector<uint8_t>> findStateEffecterPDR(uint8_t tid, |
| 407 | uint16_t entityID, |
| 408 | uint16_t stateSetId, |
| 409 | const pldm_pdr* repo); |
Chicago Duan | 738e4d8 | 2020-05-28 16:39:19 +0800 | [diff] [blame] | 410 | /** @brief Find State Sensor PDR |
| 411 | * @param[in] tid - PLDM terminus ID. |
| 412 | * @param[in] entityID - entity that can be associated with PLDM State set. |
| 413 | * @param[in] stateSetId - value that identifies PLDM State set. |
| 414 | * @param[in] repo - pointer to BMC's primary PDR repo. |
| 415 | * @return array[array[uint8_t]] - StateSensorPDRs |
| 416 | */ |
| 417 | std::vector<std::vector<uint8_t>> findStateSensorPDR(uint8_t tid, |
| 418 | uint16_t entityID, |
| 419 | uint16_t stateSetId, |
| 420 | const pldm_pdr* repo); |
Pavithra Barithaya | 0f74c98 | 2020-04-27 02:17:10 -0500 | [diff] [blame] | 421 | |
Sampa Misra | 3a0e3b9 | 2020-10-21 05:58:00 -0500 | [diff] [blame] | 422 | /** @brief Find sensor id from a state sensor PDR |
| 423 | * |
| 424 | * @param[in] pdrRepo - PDR repository |
| 425 | * @param[in] tid - terminus id |
| 426 | * @param[in] entityType - entity type |
| 427 | * @param[in] entityInstance - entity instance number |
| 428 | * @param[in] containerId - container id |
| 429 | * @param[in] stateSetId - state set id |
| 430 | * |
| 431 | * @return uint16_t - the sensor id |
| 432 | */ |
| 433 | uint16_t findStateSensorId(const pldm_pdr* pdrRepo, uint8_t tid, |
| 434 | uint16_t entityType, uint16_t entityInstance, |
| 435 | uint16_t containerId, uint16_t stateSetId); |
| 436 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 437 | /** @brief Find effecter id from a state effecter pdr |
| 438 | * @param[in] pdrRepo - PDR repository |
| 439 | * @param[in] entityType - entity type |
| 440 | * @param[in] entityInstance - entity instance number |
| 441 | * @param[in] containerId - container id |
| 442 | * @param[in] stateSetId - state set id |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 443 | * @param[in] localOrRemote - true for checking local repo and false for remote |
| 444 | * repo |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 445 | * |
| 446 | * @return uint16_t - the effecter id |
| 447 | */ |
| 448 | uint16_t findStateEffecterId(const pldm_pdr* pdrRepo, uint16_t entityType, |
| 449 | uint16_t entityInstance, uint16_t containerId, |
Sampa Misra | a4a9616 | 2020-07-14 05:33:46 -0500 | [diff] [blame] | 450 | uint16_t stateSetId, bool localOrRemote); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 451 | |
Chicago Duan | fe4d88b | 2020-06-12 16:44:13 +0800 | [diff] [blame] | 452 | /** @brief Emit the sensor event signal |
| 453 | * |
| 454 | * @param[in] tid - the terminus id |
| 455 | * @param[in] sensorId - sensorID value of the sensor |
| 456 | * @param[in] sensorOffset - Identifies which state sensor within a |
| 457 | * composite state sensor the event is being returned for |
| 458 | * @param[in] eventState - The event state value from the state change that |
| 459 | * triggered the event message |
| 460 | * @param[in] previousEventState - The event state value for the state from |
| 461 | * which the present event state was entered. |
| 462 | * @return PLDM completion code |
| 463 | */ |
| 464 | int emitStateSensorEventSignal(uint8_t tid, uint16_t sensorId, |
| 465 | uint8_t sensorOffset, uint8_t eventState, |
| 466 | uint8_t previousEventState); |
| 467 | |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 468 | /** @brief Print the buffer |
| 469 | * |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 470 | * @param[in] isTx - True if the buffer is an outgoing PLDM message, false if |
| 471 | the buffer is an incoming PLDM message |
| 472 | * @param[in] buffer - Buffer to print |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 473 | * |
| 474 | * @return - None |
| 475 | */ |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 476 | void printBuffer(bool isTx, const std::vector<uint8_t>& buffer); |
Sridevi Ramesh | ae28bc7 | 2020-12-10 07:21:16 -0600 | [diff] [blame] | 477 | |
Tom Joseph | 5492207 | 2021-06-19 02:45:46 -0700 | [diff] [blame] | 478 | /** @brief Convert the buffer to std::string |
| 479 | * |
| 480 | * If there are characters that are not printable characters, it is replaced |
| 481 | * with space(0x20). |
| 482 | * |
| 483 | * @param[in] var - pointer to data and length of the data |
| 484 | * |
| 485 | * @return std::string equivalent of variable field |
| 486 | */ |
| 487 | std::string toString(const struct variable_field& var); |
| 488 | |
George Liu | 872f0f6 | 2021-11-25 16:26:16 +0800 | [diff] [blame] | 489 | /** @brief Split strings according to special identifiers |
| 490 | * |
| 491 | * We can split the string according to the custom identifier(';', ',', '&' or |
| 492 | * others) and store it to vector. |
| 493 | * |
| 494 | * @param[in] srcStr - The string to be split |
| 495 | * @param[in] delim - The custom identifier |
| 496 | * @param[in] trimStr - The first and last string to be trimmed |
| 497 | * |
| 498 | * @return std::vector<std::string> Vectors are used to store strings |
| 499 | */ |
| 500 | std::vector<std::string> split(std::string_view srcStr, std::string_view delim, |
| 501 | std::string_view trimStr = ""); |
Manojkiran Eda | ef77305 | 2021-07-29 09:29:28 +0530 | [diff] [blame] | 502 | /** @brief Get the current system time in readable format |
| 503 | * |
| 504 | * @return - std::string equivalent of the system time |
| 505 | */ |
| 506 | std::string getCurrentSystemTime(); |
George Liu | 872f0f6 | 2021-11-25 16:26:16 +0800 | [diff] [blame] | 507 | |
Sridevi Ramesh | eefe49b | 2022-06-27 11:51:02 -0500 | [diff] [blame] | 508 | /** @brief checks if the FRU is actually present. |
| 509 | * @param[in] objPath - FRU object path. |
| 510 | * |
| 511 | * @return bool to indicate presence or absence of FRU. |
| 512 | */ |
| 513 | bool checkForFruPresence(const std::string& objPath); |
| 514 | |
Sagar Srinivas | 5db6e87 | 2023-12-01 10:03:30 -0600 | [diff] [blame] | 515 | /** @brief Method to check if the logical bit is set |
| 516 | * |
| 517 | * @param[containerId] - container id of the entity |
| 518 | * |
| 519 | * @return true or false based on the logic bit set |
| 520 | */ |
| 521 | bool checkIfLogicalBitSet(const uint16_t& containerId); |
Pavithra Barithaya | 5e542be | 2021-08-13 00:33:31 -0500 | [diff] [blame] | 522 | |
| 523 | /** @brief setting the present property |
| 524 | * |
| 525 | * @param[in] objPath - the object path of the fru |
| 526 | * @param[in] present - status to set either true/false |
| 527 | */ |
| 528 | void setFruPresence(const std::string& fruObjPath, bool present); |
Thu Nguyen | b8cf46b | 2024-06-15 02:44:35 +0000 | [diff] [blame] | 529 | |
| 530 | /** @brief Trim `\0` in string and replace ` ` by `_` to use name in D-Bus |
| 531 | * object path |
| 532 | * |
| 533 | * @param[in] name - the input string |
| 534 | * |
| 535 | * @return the result string |
| 536 | */ |
| 537 | std::string_view trimNameForDbus(std::string& name); |
| 538 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 539 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 540 | } // namespace pldm |