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> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 8 | #include <iostream> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 9 | #include <sdbusplus/server.hpp> |
| 10 | #include <string> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 11 | #include <variant> |
| 12 | #include <vector> |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Logging/Entry/server.hpp> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 14 | |
| 15 | #include "libpldm/base.h" |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 16 | #include "libpldm/bios.h" |
| 17 | #include "libpldm/platform.h" |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 18 | |
| 19 | namespace pldm |
| 20 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 21 | namespace utils |
| 22 | { |
| 23 | |
| 24 | /** @struct CustomFD |
| 25 | * |
| 26 | * RAII wrapper for file descriptor. |
| 27 | */ |
| 28 | struct CustomFD |
| 29 | { |
| 30 | CustomFD(const CustomFD&) = delete; |
| 31 | CustomFD& operator=(const CustomFD&) = delete; |
| 32 | CustomFD(CustomFD&&) = delete; |
| 33 | CustomFD& operator=(CustomFD&&) = delete; |
| 34 | |
| 35 | CustomFD(int fd) : fd(fd) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | ~CustomFD() |
| 40 | { |
| 41 | if (fd >= 0) |
| 42 | { |
| 43 | close(fd); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | int operator()() const |
| 48 | { |
| 49 | return fd; |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | int fd = -1; |
| 54 | }; |
| 55 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 56 | /** @brief Calculate the pad for PLDM data |
| 57 | * |
| 58 | * @param[in] data - Length of the data |
| 59 | * @return - uint8_t - number of pad bytes |
| 60 | */ |
| 61 | uint8_t getNumPadBytes(uint32_t data); |
| 62 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 63 | /** @brief Convert uint64 to date |
| 64 | * |
| 65 | * @param[in] data - time date of uint64 |
| 66 | * @param[out] year - year number in dec |
| 67 | * @param[out] month - month number in dec |
| 68 | * @param[out] day - day of the month in dec |
| 69 | * @param[out] hour - number of hours in dec |
| 70 | * @param[out] min - number of minutes in dec |
| 71 | * @param[out] sec - number of seconds in dec |
| 72 | * @return true if decode success, false if decode faild |
| 73 | */ |
| 74 | bool uintToDate(uint64_t data, uint16_t* year, uint8_t* month, uint8_t* day, |
| 75 | uint8_t* hour, uint8_t* min, uint8_t* sec); |
| 76 | |
| 77 | /** @brief Convert effecter data to structure of set_effecter_state_field |
| 78 | * |
| 79 | * @param[in] effecterData - the date of effecter |
| 80 | * @param[out] effecter_id - a handle that is used to identify and access the |
| 81 | * effecter |
| 82 | * @param[out] stateField - structure of set_effecter_state_field |
| 83 | */ |
| 84 | bool decodeEffecterData(const std::vector<uint8_t>& effecterData, |
| 85 | uint16_t& effecter_id, |
| 86 | std::vector<set_effecter_state_field>& stateField); |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 87 | |
| 88 | /** |
Sampa Misra | aa8ae72 | 2019-12-12 03:20:40 -0600 | [diff] [blame] | 89 | * @brief creates an error log |
| 90 | * @param[in] errorMsg - the error message |
| 91 | */ |
| 92 | void reportError(const char* errorMsg); |
| 93 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 94 | /** @brief Convert any Decimal number to BCD |
| 95 | * |
| 96 | * @tparam[in] decimal - Decimal number |
| 97 | * @return Corresponding BCD number |
| 98 | */ |
| 99 | template <typename T> |
| 100 | T decimalToBcd(T decimal) |
| 101 | { |
| 102 | T bcd = 0; |
| 103 | T rem = 0; |
| 104 | auto cnt = 0; |
| 105 | |
| 106 | while (decimal) |
| 107 | { |
| 108 | rem = decimal % 10; |
| 109 | bcd = bcd + (rem << cnt); |
| 110 | decimal = decimal / 10; |
| 111 | cnt += 4; |
| 112 | } |
| 113 | |
| 114 | return bcd; |
| 115 | } |
| 116 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 117 | constexpr auto dbusProperties = "org.freedesktop.DBus.Properties"; |
| 118 | |
| 119 | /** |
| 120 | * @class DBusHandler |
| 121 | * |
| 122 | * Wrapper class to handle the D-Bus calls |
| 123 | * |
| 124 | * This class contains the APIs to handle the D-Bus calls |
| 125 | * to cater the request from pldm requester. |
| 126 | * A class is created to mock the apis in the test cases |
| 127 | */ |
| 128 | class DBusHandler |
| 129 | { |
| 130 | public: |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 131 | /** @brief Get the bus connection. */ |
| 132 | static auto& getBus() |
| 133 | { |
| 134 | static auto bus = sdbusplus::bus::new_default(); |
| 135 | return bus; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @brief Get the DBUS Service name for the input dbus path |
| 140 | * @param[in] path - DBUS object path |
| 141 | * @param[in] interface - DBUS Interface |
| 142 | * @return std::string - the dbus service name |
| 143 | */ |
| 144 | std::string getService(const char* path, const char* interface) const; |
| 145 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 146 | /** @brief API to set a D-Bus property |
| 147 | * |
| 148 | * @param[in] objPath - Object path for the D-Bus object |
| 149 | * @param[in] dbusProp - The D-Bus property |
| 150 | * @param[in] dbusInterface - The D-Bus interface |
| 151 | * @param[in] value - The value to be set |
| 152 | * failure |
| 153 | */ |
| 154 | template <typename T> |
| 155 | void setDbusProperty(const char* objPath, const char* dbusProp, |
| 156 | const char* dbusInterface, |
| 157 | const std::variant<T>& value) const |
| 158 | { |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 159 | auto& bus = DBusHandler::getBus(); |
| 160 | auto service = getService(objPath, dbusInterface); |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 161 | auto method = bus.new_method_call(service.c_str(), objPath, |
| 162 | dbusProperties, "Set"); |
| 163 | method.append(dbusInterface, dbusProp, value); |
| 164 | bus.call_noreply(method); |
| 165 | } |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 166 | |
| 167 | template <typename Variant> |
| 168 | auto getDbusPropertyVariant(const char* objPath, const char* dbusProp, |
| 169 | const char* dbusInterface) |
| 170 | { |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 171 | Variant value; |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 172 | auto& bus = DBusHandler::getBus(); |
| 173 | auto service = getService(objPath, dbusInterface); |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 174 | auto method = bus.new_method_call(service.c_str(), objPath, |
| 175 | dbusProperties, "Get"); |
| 176 | method.append(dbusInterface, dbusProp); |
George Liu | 0e02c32 | 2020-01-01 09:41:51 +0800 | [diff] [blame] | 177 | auto reply = bus.call(method); |
| 178 | reply.read(value); |
| 179 | |
John Wang | 92b3c97 | 2019-10-17 11:06:41 +0800 | [diff] [blame] | 180 | return value; |
| 181 | } |
| 182 | |
| 183 | template <typename Property> |
| 184 | auto getDbusProperty(const char* objPath, const char* dbusProp, |
| 185 | const char* dbusInterface) |
| 186 | { |
| 187 | auto VariantValue = getDbusPropertyVariant<std::variant<Property>>( |
| 188 | objPath, dbusProp, dbusInterface); |
| 189 | return std::get<Property>(VariantValue); |
| 190 | } |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 191 | }; |
| 192 | |
George Liu | 8340957 | 2019-12-24 18:42:54 +0800 | [diff] [blame] | 193 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 194 | } // namespace pldm |