blob: 25d530d9acbc45489026ce4c1b4cbd991a5b0e4c [file] [log] [blame]
Jayashree Dhanapalb6779842022-10-07 13:34:16 +05301#pragma once
2
3#include "physical.hpp"
4
5#include <boost/algorithm/string.hpp>
6#include <phosphor-logging/lg2.hpp>
7#include <sdbusplus/bus.hpp>
8#include <sdbusplus/server/interface.hpp>
9#include <sdbusplus/vtable.hpp>
10
11#include <unordered_map>
12
13static constexpr auto busName = "xyz.openbmc_project.LED.Controller";
14static constexpr auto ledPath = "/xyz/openbmc_project/led";
15static constexpr auto physParent = "/xyz/openbmc_project/led/physical";
16static constexpr auto devParent = "/sys/class/leds/";
17static constexpr auto internalInterface =
18 "xyz.openbmc_project.Led.Sysfs.Internal";
19static constexpr auto ledAddMethod = "AddLED";
20
21namespace phosphor
22{
23namespace led
24{
25namespace sysfs
26{
27namespace interface
28{
29
30class InternalInterface
31{
32 public:
33 InternalInterface() = delete;
34 InternalInterface(const InternalInterface&) = delete;
35 InternalInterface& operator=(const InternalInterface&) = delete;
36 InternalInterface(InternalInterface&&) = delete;
37 InternalInterface& operator=(InternalInterface&&) = delete;
38 virtual ~InternalInterface() = default;
39
40 /**
41 * @brief Construct a class to put object onto bus at a dbus path.
42 *
43 * @param[in] bus - D-Bus object.
44 * @param[in] path - D-Bus Path.
45 */
46
47 InternalInterface(sdbusplus::bus_t& bus, const char* path);
48
49 /**
50 * @brief Implementation for the AddLed method to add
51 * the LED name to dbus path.
52 *
53 * @param[in] name - LED name to add.
54 */
55
56 void addLED(const std::string& name);
57
58 /**
59 * @brief Implementation for the RemoveLed method to remove
60 * the LED name to dbus path.
61 *
62 * @param[in] name - LED name to remove.
63 */
64
65 void removeLED(const std::string& name);
66
67 private:
68 /**
69 * @brief Unordered map to declare the sysfs LEDs
70 */
71
72 std::unordered_map<std::string, std::unique_ptr<phosphor::led::Physical>>
73 leds;
74
75 /**
76 * @brief Structure to define the LED sysfs name
77 */
78
79 struct LedDescr
80 {
81 std::string devicename;
82 std::string color;
83 std::string function;
84 };
85
86 /**
87 * @brief sdbusplus D-Bus connection.
88 */
89
90 sdbusplus::bus_t& bus;
91
92 /**
93 * @brief Systemd bus callback for the AddLed method.
94 */
95
96 static int addLedConfigure(sd_bus_message* msg, void* context,
97 sd_bus_error* error);
98
99 /**
100 * @brief Systemd bus callback for the RemoveLed method.
101 */
102
103 static int removeLedConfigure(sd_bus_message* msg, void* context,
104 sd_bus_error* error);
105
106 /**
107 * @brief Systemd vtable structure that contains all the
108 * methods, signals, and properties of this interface with their
109 * respective systemd attributes
110 */
111
112 static const std::array<sdbusplus::vtable::vtable_t, 4> vtable;
113
114 /**
115 * @brief Support for the dbus based instance of this interface.
116 */
117
118 sdbusplus::server::interface_t serverInterface;
119
120 /**
121 * @brief Implementation to create a dbus path for LED.
122 *
123 * @param[in] name - LED name.
124 */
125
126 void createLEDPath(const std::string& ledName);
127
128 /** @brief Parse LED name in sysfs
129 *
130 * Parse sysfs LED name in format "devicename:colour:function"
131 * or "devicename:colour" or "devicename" and sets corresponding
132 * fields in LedDescr struct.
133 *
134 * @param[in] name - LED name in sysfs
135 * @param[out] ledDescr - LED description
136 */
137
138 static void getLedDescr(const std::string& name, LedDescr& ledDescr);
139
140 /** @brief Generates LED DBus name from LED description
141 *
142 * @param[in] name - LED description
143 * @return - DBus LED name
144 */
145
146 static std::string getDbusName(const LedDescr& ledDescr);
147};
148
149} // namespace interface
150} // namespace sysfs
151} // namespace led
152} // namespace phosphor