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 | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 8 | #include <sdbusplus/server.hpp> |
| 9 | #include <string> |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 10 | #include <variant> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "libpldm/base.h" |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 14 | |
| 15 | namespace pldm |
| 16 | { |
| 17 | namespace responder |
| 18 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 19 | namespace utils |
| 20 | { |
| 21 | |
| 22 | /** @struct CustomFD |
| 23 | * |
| 24 | * RAII wrapper for file descriptor. |
| 25 | */ |
| 26 | struct CustomFD |
| 27 | { |
| 28 | CustomFD(const CustomFD&) = delete; |
| 29 | CustomFD& operator=(const CustomFD&) = delete; |
| 30 | CustomFD(CustomFD&&) = delete; |
| 31 | CustomFD& operator=(CustomFD&&) = delete; |
| 32 | |
| 33 | CustomFD(int fd) : fd(fd) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | ~CustomFD() |
| 38 | { |
| 39 | if (fd >= 0) |
| 40 | { |
| 41 | close(fd); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | int operator()() const |
| 46 | { |
| 47 | return fd; |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | int fd = -1; |
| 52 | }; |
| 53 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 54 | /** @brief Calculate the pad for PLDM data |
| 55 | * |
| 56 | * @param[in] data - Length of the data |
| 57 | * @return - uint8_t - number of pad bytes |
| 58 | */ |
| 59 | uint8_t getNumPadBytes(uint32_t data); |
| 60 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 61 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * @brief Get the DBUS Service name for the input dbus path |
| 65 | * @param[in] bus - DBUS Bus Object |
| 66 | * @param[in] path - DBUS object path |
| 67 | * @param[in] interface - DBUS Interface |
| 68 | * @return std::string - the dbus service name |
| 69 | */ |
| 70 | std::string getService(sdbusplus::bus::bus& bus, const std::string& path, |
| 71 | const std::string& interface); |
| 72 | |
| 73 | /** @brief Convert any Decimal number to BCD |
| 74 | * |
| 75 | * @tparam[in] decimal - Decimal number |
| 76 | * @return Corresponding BCD number |
| 77 | */ |
| 78 | template <typename T> |
| 79 | T decimalToBcd(T decimal) |
| 80 | { |
| 81 | T bcd = 0; |
| 82 | T rem = 0; |
| 83 | auto cnt = 0; |
| 84 | |
| 85 | while (decimal) |
| 86 | { |
| 87 | rem = decimal % 10; |
| 88 | bcd = bcd + (rem << cnt); |
| 89 | decimal = decimal / 10; |
| 90 | cnt += 4; |
| 91 | } |
| 92 | |
| 93 | return bcd; |
| 94 | } |
| 95 | |
Sampa Misra | a2fa070 | 2019-05-31 01:28:55 -0500 | [diff] [blame] | 96 | constexpr auto dbusProperties = "org.freedesktop.DBus.Properties"; |
| 97 | |
| 98 | /** |
| 99 | * @class DBusHandler |
| 100 | * |
| 101 | * Wrapper class to handle the D-Bus calls |
| 102 | * |
| 103 | * This class contains the APIs to handle the D-Bus calls |
| 104 | * to cater the request from pldm requester. |
| 105 | * A class is created to mock the apis in the test cases |
| 106 | */ |
| 107 | class DBusHandler |
| 108 | { |
| 109 | public: |
| 110 | /** @brief API to set a D-Bus property |
| 111 | * |
| 112 | * @param[in] objPath - Object path for the D-Bus object |
| 113 | * @param[in] dbusProp - The D-Bus property |
| 114 | * @param[in] dbusInterface - The D-Bus interface |
| 115 | * @param[in] value - The value to be set |
| 116 | * failure |
| 117 | */ |
| 118 | template <typename T> |
| 119 | void setDbusProperty(const char* objPath, const char* dbusProp, |
| 120 | const char* dbusInterface, |
| 121 | const std::variant<T>& value) const |
| 122 | { |
| 123 | auto bus = sdbusplus::bus::new_default(); |
| 124 | auto service = getService(bus, objPath, dbusInterface); |
| 125 | auto method = bus.new_method_call(service.c_str(), objPath, |
| 126 | dbusProperties, "Set"); |
| 127 | method.append(dbusInterface, dbusProp, value); |
| 128 | bus.call_noreply(method); |
| 129 | } |
| 130 | }; |
| 131 | |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 132 | } // namespace responder |
| 133 | } // namespace pldm |