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>; |
| 134 | |
| 135 | /** |
| 136 | * @brief The interface for DBusHandler |
| 137 | */ |
| 138 | class DBusHandlerInterface |
| 139 | { |
| 140 | public: |
| 141 | virtual ~DBusHandlerInterface() = default; |
| 142 | |
| 143 | virtual void setDbusProperty(const DBusMapping& dBusMap, |
| 144 | const PropertyValue& value) const = 0; |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 145 | |
| 146 | virtual PropertyValue |
| 147 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 148 | const char* dbusInterface) const = 0; |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 149 | }; |
| 150 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 151 | /** |
| 152 | * @class DBusHandler |
| 153 | * |
| 154 | * Wrapper class to handle the D-Bus calls |
| 155 | * |
| 156 | * This class contains the APIs to handle the D-Bus calls |
| 157 | * to cater the request from pldm requester. |
| 158 | * A class is created to mock the apis in the test cases |
| 159 | */ |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 160 | class DBusHandler : public DBusHandlerInterface |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 161 | { |
| 162 | public: |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 163 | /** @brief Get the bus connection. */ |
| 164 | static auto& getBus() |
| 165 | { |
| 166 | static auto bus = sdbusplus::bus::new_default(); |
| 167 | return bus; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @brief Get the DBUS Service name for the input dbus path |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 172 | * |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 173 | * @param[in] path - DBUS object path |
| 174 | * @param[in] interface - DBUS Interface |
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 | * @return std::string - the dbus service name |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 177 | * |
| 178 | * @throw sdbusplus::exception::SdBusError when it fails |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 179 | */ |
| 180 | std::string getService(const char* path, const char* interface) const; |
| 181 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 182 | /** @brief Get property(type: variant) from the requested dbus |
| 183 | * |
| 184 | * @param[in] objPath - The Dbus object path |
| 185 | * @param[in] dbusProp - The property name to get |
| 186 | * @param[in] dbusInterface - The Dbus interface |
| 187 | * |
| 188 | * @return The value of the property(type: variant) |
| 189 | * |
| 190 | * @throw sdbusplus::exception::SdBusError when it fails |
| 191 | */ |
| 192 | PropertyValue |
| 193 | getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 194 | const char* dbusInterface) const override; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 195 | |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 196 | /** @brief The template function to get property from the requested dbus |
| 197 | * path |
| 198 | * |
| 199 | * @tparam Property - Excepted type of the property on dbus |
| 200 | * |
| 201 | * @param[in] objPath - The Dbus object path |
| 202 | * @param[in] dbusProp - The property name to get |
| 203 | * @param[in] dbusInterface - The Dbus interface |
| 204 | * |
| 205 | * @return The value of the property |
| 206 | * |
| 207 | * @throw sdbusplus::exception::SdBusError when dbus request fails |
| 208 | * std::bad_variant_access when \p Property and property on dbus do |
| 209 | * not match |
| 210 | */ |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 211 | template <typename Property> |
| 212 | auto getDbusProperty(const char* objPath, const char* dbusProp, |
| 213 | const char* dbusInterface) |
| 214 | { |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 215 | auto VariantValue = |
| 216 | getDbusPropertyVariant(objPath, dbusProp, dbusInterface); |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 217 | return std::get<Property>(VariantValue); |
| 218 | } |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 219 | |
| 220 | /** @brief Set Dbus property |
| 221 | * |
| 222 | * @param[in] dBusMap - Object path, property name, interface and property |
| 223 | * type for the D-Bus object |
| 224 | * @param[in] value - The value to be set |
John Wang | 9e24242 | 2020-03-05 08:37:50 +0800 | [diff] [blame] | 225 | * |
| 226 | * @throw sdbusplus::exception::SdBusError when it fails |
George Liu | 1e44c73 | 2020-02-28 20:20:06 +0800 | [diff] [blame] | 227 | */ |
| 228 | void setDbusProperty(const DBusMapping& dBusMap, |
| 229 | const PropertyValue& value) const override; |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 230 | }; |
| 231 | |
Deepak Kodihalli | 3cd6181 | 2020-03-10 06:38:45 -0500 | [diff] [blame] | 232 | /** @brief Fetch parent D-Bus object based on pathname |
| 233 | * |
| 234 | * @param[in] dbusObj - child D-Bus object |
| 235 | * |
| 236 | * @return std::string - the parent D-Bus object path |
| 237 | */ |
| 238 | inline std::string findParent(const std::string& dbusObj) |
| 239 | { |
| 240 | fs::path p(dbusObj); |
| 241 | return p.parent_path().string(); |
| 242 | } |
| 243 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 244 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 245 | } // namespace pldm |