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