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