Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdint.h> |
| 4 | #include <systemd/sd-bus.h> |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 5 | #include <unistd.h> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 6 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 7 | #include <exception> |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 8 | #include <filesystem> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 9 | #include <iostream> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 10 | #include <sdbusplus/server.hpp> |
| 11 | #include <string> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 12 | #include <variant> |
| 13 | #include <vector> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 15 | |
| 16 | #include "libpldm/base.h" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 17 | #include "libpldm/bios.h" |
| 18 | #include "libpldm/platform.h" |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 19 | |
| 20 | namespace pldm |
| 21 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 22 | namespace utils |
| 23 | { |
| 24 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 25 | namespace fs = std::filesystem; |
| 26 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 27 | /** @struct CustomFD |
| 28 | * |
| 29 | * RAII wrapper for file descriptor. |
| 30 | */ |
| 31 | struct CustomFD |
| 32 | { |
| 33 | CustomFD(const CustomFD&) = delete; |
| 34 | CustomFD& operator=(const CustomFD&) = delete; |
| 35 | CustomFD(CustomFD&&) = delete; |
| 36 | CustomFD& operator=(CustomFD&&) = delete; |
| 37 | |
| 38 | CustomFD(int fd) : fd(fd) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | ~CustomFD() |
| 43 | { |
| 44 | if (fd >= 0) |
| 45 | { |
| 46 | close(fd); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | int operator()() const |
| 51 | { |
| 52 | return fd; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | int fd = -1; |
| 57 | }; |
| 58 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 59 | /** @brief Calculate the pad for PLDM data |
| 60 | * |
| 61 | * @param[in] data - Length of the data |
| 62 | * @return - uint8_t - number of pad bytes |
| 63 | */ |
| 64 | uint8_t getNumPadBytes(uint32_t data); |
| 65 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 66 | /** @brief Convert uint64 to date |
| 67 | * |
| 68 | * @param[in] data - time date of uint64 |
| 69 | * @param[out] year - year number in dec |
| 70 | * @param[out] month - month number in dec |
| 71 | * @param[out] day - day of the month in dec |
| 72 | * @param[out] hour - number of hours in dec |
| 73 | * @param[out] min - number of minutes in dec |
| 74 | * @param[out] sec - number of seconds in dec |
| 75 | * @return true if decode success, false if decode faild |
| 76 | */ |
| 77 | bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day, |
| 78 | uint8_t* hour, uint8_t* min, uint8_t* sec); |
| 79 | |
| 80 | /** @brief Convert effecter data to structure of set_effecter_state_field |
| 81 | * |
| 82 | * @param[in] effecterData - the date of effecter |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 83 | * @param[in] effecterCount - the number of individual sets of effecter |
| 84 | * information |
| 85 | * @return[out] parse success and get a valid set_effecter_state_field |
| 86 | * structure, return nullopt means parse failed |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 87 | */ |
George Liu | ba4c1fb | 2020-02-05 14:13:30 +0800 | [diff] [blame] | 88 | std::optional<std::vector<set_effecter_state_field>> |
| 89 | parseEffecterData(const std::vector<uint8_t>& effecterData, |
| 90 | uint8_t effecterCount); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 91 | |
| 92 | /** |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 93 | * @brief creates an error log |
| 94 | * @param[in] errorMsg - the error message |
| 95 | */ |
| 96 | void reportError(const char* errorMsg); |
| 97 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 98 | /** @brief Convert any Decimal number to BCD |
| 99 | * |
| 100 | * @tparam[in] decimal - Decimal number |
| 101 | * @return Corresponding BCD number |
| 102 | */ |
| 103 | template <typename T> |
| 104 | T decimalToBcd(T decimal) |
| 105 | { |
| 106 | T bcd = 0; |
| 107 | T rem = 0; |
| 108 | auto cnt = 0; |
| 109 | |
| 110 | while (decimal) |
| 111 | { |
| 112 | rem = decimal % 10; |
| 113 | bcd = bcd + (rem << cnt); |
| 114 | decimal = decimal / 10; |
| 115 | cnt += 4; |
| 116 | } |
| 117 | |
| 118 | return bcd; |
| 119 | } |
| 120 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 121 | constexpr auto dbusProperties = "org.freedesktop.DBus.Properties"; |
| 122 | |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 123 | struct DBusMapping |
| 124 | { |
| 125 | std::string objectPath; //!< D-Bus object path |
| 126 | std::string interface; //!< D-Bus interface |
| 127 | std::string propertyName; //!< D-Bus property name |
| 128 | std::string propertyType; //!< D-Bus property type |
| 129 | }; |
| 130 | |
| 131 | using PropertyValue = |
| 132 | std::variant<bool, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, |
| 133 | uint64_t, double, std::string>; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 134 | using DbusProp = std::string; |
| 135 | using DbusChangedProps = std::map<DbusProp, PropertyValue>; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * @brief The interface for DBusHandler |
| 139 | */ |
| 140 | class DBusHandlerInterface |
| 141 | { |
| 142 | public: |
| 143 | virtual ~DBusHandlerInterface() = default; |
| 144 | |
| 145 | virtual void setDbusProperty(const DBusMapping& dBusMap, |
| 146 | const PropertyValue& value) const = 0; |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 147 | |
| 148 | virtual PropertyValue |
| 149 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 150 | const char* dbusInterface) const = 0; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 151 | }; |
| 152 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 153 | /** |
| 154 | * @class DBusHandler |
| 155 | * |
| 156 | * Wrapper class to handle the D-Bus calls |
| 157 | * |
| 158 | * This class contains the APIs to handle the D-Bus calls |
| 159 | * to cater the request from pldm requester. |
| 160 | * A class is created to mock the apis in the test cases |
| 161 | */ |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 162 | class DBusHandler : public DBusHandlerInterface |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 163 | { |
| 164 | public: |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 165 | /** @brief Get the bus connection. */ |
| 166 | static auto& getBus() |
| 167 | { |
| 168 | static auto bus = sdbusplus::bus::new_default(); |
| 169 | return bus; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @brief Get the DBUS Service name for the input dbus path |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 174 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 175 | * @param[in] path - DBUS object path |
| 176 | * @param[in] interface - DBUS Interface |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 177 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 178 | * @return std::string - the dbus service name |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 179 | * |
| 180 | * @throw sdbusplus::exception::SdBusError when it fails |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 181 | */ |
| 182 | std::string getService(const char* path, const char* interface) const; |
| 183 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 184 | /** @brief Get property(type: variant) from the requested dbus |
| 185 | * |
| 186 | * @param[in] objPath - The Dbus object path |
| 187 | * @param[in] dbusProp - The property name to get |
| 188 | * @param[in] dbusInterface - The Dbus interface |
| 189 | * |
| 190 | * @return The value of the property(type: variant) |
| 191 | * |
| 192 | * @throw sdbusplus::exception::SdBusError when it fails |
| 193 | */ |
| 194 | PropertyValue |
| 195 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 196 | const char* dbusInterface) const override; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 197 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 198 | /** @brief The template function to get property from the requested dbus |
| 199 | * path |
| 200 | * |
| 201 | * @tparam Property - Excepted type of the property on dbus |
| 202 | * |
| 203 | * @param[in] objPath - The Dbus object path |
| 204 | * @param[in] dbusProp - The property name to get |
| 205 | * @param[in] dbusInterface - The Dbus interface |
| 206 | * |
| 207 | * @return The value of the property |
| 208 | * |
| 209 | * @throw sdbusplus::exception::SdBusError when dbus request fails |
| 210 | * std::bad_variant_access when \p Property and property on dbus do |
| 211 | * not match |
| 212 | */ |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 213 | template <typename Property> |
| 214 | auto getDbusProperty(const char* objPath, const char* dbusProp, |
| 215 | const char* dbusInterface) |
| 216 | { |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 217 | auto VariantValue = |
| 218 | getDbusPropertyVariant(objPath, dbusProp, dbusInterface); |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 219 | return std::get<Property>(VariantValue); |
| 220 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 221 | |
| 222 | /** @brief Set Dbus property |
| 223 | * |
| 224 | * @param[in] dBusMap - Object path, property name, interface and property |
| 225 | * type for the D-Bus object |
| 226 | * @param[in] value - The value to be set |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 227 | * |
| 228 | * @throw sdbusplus::exception::SdBusError when it fails |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 229 | */ |
| 230 | void setDbusProperty(const DBusMapping& dBusMap, |
| 231 | const PropertyValue& value) const override; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 232 | }; |
| 233 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 234 | /** @brief Fetch parent D-Bus object based on pathname |
| 235 | * |
| 236 | * @param[in] dbusObj - child D-Bus object |
| 237 | * |
| 238 | * @return std::string - the parent D-Bus object path |
| 239 | */ |
| 240 | inline std::string findParent(const std::string& dbusObj) |
| 241 | { |
| 242 | fs::path p(dbusObj); |
| 243 | return p.parent_path().string(); |
| 244 | } |
| 245 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 246 | /** @brief Read (static) MCTP EID of host firmware from a file |
| 247 | * |
| 248 | * @return uint8_t - MCTP EID |
| 249 | */ |
| 250 | uint8_t readHostEID(); |
| 251 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 252 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 253 | } // namespace pldm |