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