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 | |
| 7 | #include <sdbusplus/server.hpp> |
| 8 | #include <string> |
| 9 | |
| 10 | namespace pldm |
| 11 | { |
| 12 | namespace responder |
| 13 | { |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 14 | namespace utils |
| 15 | { |
| 16 | |
| 17 | /** @struct CustomFD |
| 18 | * |
| 19 | * RAII wrapper for file descriptor. |
| 20 | */ |
| 21 | struct CustomFD |
| 22 | { |
| 23 | CustomFD(const CustomFD&) = delete; |
| 24 | CustomFD& operator=(const CustomFD&) = delete; |
| 25 | CustomFD(CustomFD&&) = delete; |
| 26 | CustomFD& operator=(CustomFD&&) = delete; |
| 27 | |
| 28 | CustomFD(int fd) : fd(fd) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | ~CustomFD() |
| 33 | { |
| 34 | if (fd >= 0) |
| 35 | { |
| 36 | close(fd); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | int operator()() const |
| 41 | { |
| 42 | return fd; |
| 43 | } |
| 44 | |
| 45 | private: |
| 46 | int fd = -1; |
| 47 | }; |
| 48 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 49 | /** @brief Calculate the pad for PLDM data |
| 50 | * |
| 51 | * @param[in] data - Length of the data |
| 52 | * @return - uint8_t - number of pad bytes |
| 53 | */ |
| 54 | uint8_t getNumPadBytes(uint32_t data); |
| 55 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 56 | } // namespace utils |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * @brief Get the DBUS Service name for the input dbus path |
| 60 | * @param[in] bus - DBUS Bus Object |
| 61 | * @param[in] path - DBUS object path |
| 62 | * @param[in] interface - DBUS Interface |
| 63 | * @return std::string - the dbus service name |
| 64 | */ |
| 65 | std::string getService(sdbusplus::bus::bus& bus, const std::string& path, |
| 66 | const std::string& interface); |
| 67 | |
| 68 | /** @brief Convert any Decimal number to BCD |
| 69 | * |
| 70 | * @tparam[in] decimal - Decimal number |
| 71 | * @return Corresponding BCD number |
| 72 | */ |
| 73 | template <typename T> |
| 74 | T decimalToBcd(T decimal) |
| 75 | { |
| 76 | T bcd = 0; |
| 77 | T rem = 0; |
| 78 | auto cnt = 0; |
| 79 | |
| 80 | while (decimal) |
| 81 | { |
| 82 | rem = decimal % 10; |
| 83 | bcd = bcd + (rem << cnt); |
| 84 | decimal = decimal / 10; |
| 85 | cnt += 4; |
| 86 | } |
| 87 | |
| 88 | return bcd; |
| 89 | } |
| 90 | |
| 91 | } // namespace responder |
| 92 | } // namespace pldm |