blob: 39c7ed6ed17a13cc0247cf5225a4d011f4d736d9 [file] [log] [blame]
Sampa Misra032bd502019-03-06 05:03:22 -06001#pragma once
2
3#include <stdint.h>
4#include <systemd/sd-bus.h>
Jinu Joy Thomasf666db12019-05-29 05:22:31 -05005#include <unistd.h>
Sampa Misra032bd502019-03-06 05:03:22 -06006
7#include <sdbusplus/server.hpp>
8#include <string>
9
10namespace pldm
11{
12namespace responder
13{
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050014namespace utils
15{
16
17/** @struct CustomFD
18 *
19 * RAII wrapper for file descriptor.
20 */
21struct 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
49} // namespace utils
Sampa Misra032bd502019-03-06 05:03:22 -060050
51/**
52 * @brief Get the DBUS Service name for the input dbus path
53 * @param[in] bus - DBUS Bus Object
54 * @param[in] path - DBUS object path
55 * @param[in] interface - DBUS Interface
56 * @return std::string - the dbus service name
57 */
58std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
59 const std::string& interface);
60
61/** @brief Convert any Decimal number to BCD
62 *
63 * @tparam[in] decimal - Decimal number
64 * @return Corresponding BCD number
65 */
66template <typename T>
67T decimalToBcd(T decimal)
68{
69 T bcd = 0;
70 T rem = 0;
71 auto cnt = 0;
72
73 while (decimal)
74 {
75 rem = decimal % 10;
76 bcd = bcd + (rem << cnt);
77 decimal = decimal / 10;
78 cnt += 4;
79 }
80
81 return bcd;
82}
83
84} // namespace responder
85} // namespace pldm