blob: 917b8ec08704dad1ef3aa7660df0aa9a19e5c9a1 [file] [log] [blame]
Jayashree Dhanapalb6779842022-10-07 13:34:16 +05301/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "internal_interface.hpp"
18
19#include <sdbusplus/message.hpp>
20
21namespace phosphor
22{
23namespace led
24{
25namespace sysfs
26{
27namespace interface
28{
29
30InternalInterface::InternalInterface(sdbusplus::bus_t& bus, const char* path) :
31 bus(bus), serverInterface(bus, path, internalInterface, vtable.data(), this)
32{}
33
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053034std::string InternalInterface::getDbusName(const LedDescr& ledDescr)
35{
36 std::vector<std::string> words;
Alexander Hansen24d124f2024-07-16 13:49:55 +020037 if (ledDescr.devicename.has_value())
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053038 {
Alexander Hansen24d124f2024-07-16 13:49:55 +020039 words.emplace_back(ledDescr.devicename.value());
40 }
41 if (ledDescr.function.has_value())
42 {
43 words.emplace_back(ledDescr.function.value());
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053044 }
45
Alexander Hansen24d124f2024-07-16 13:49:55 +020046 if (ledDescr.color.has_value())
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053047 {
Alexander Hansen24d124f2024-07-16 13:49:55 +020048 words.emplace_back(ledDescr.color.value());
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053049 }
50
51 std::string s = boost::join(words, "_");
52
53 sdbusplus::message::object_path path(s);
54
55 return path.str;
56}
57
58void InternalInterface::createLEDPath(const std::string& ledName)
59{
60 std::string name;
61 std::string path = devParent + ledName;
62
63 if (!std::filesystem::exists(fs::path(path)))
64 {
65 lg2::error("No such directory {PATH}", "PATH", path);
66 return;
67 }
68
Alexander Hansen24d124f2024-07-16 13:49:55 +020069 auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
70
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053071 // Convert LED name in sysfs into DBus name
Alexander Hansen24d124f2024-07-16 13:49:55 +020072 const LedDescr ledDescr = sled->getLedDescr();
73
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053074 name = getDbusName(ledDescr);
75
76 lg2::debug("LED {NAME} receives dbus name {DBUSNAME}", "NAME", ledName,
77 "DBUSNAME", name);
78
79 // Unique path name representing a single LED.
80 sdbusplus::message::object_path objPath = std::string(physParent);
81 objPath /= name;
82
83 if (leds.contains(objPath))
84 {
85 return;
86 }
87
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053088 leds.emplace(objPath, std::make_unique<phosphor::led::Physical>(
Alexander Hansen24d124f2024-07-16 13:49:55 +020089 bus, objPath, std::move(sled),
90 ledDescr.color.value_or("")));
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053091}
92
93void InternalInterface::addLED(const std::string& name)
94{
95 createLEDPath(name);
96}
97
98// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
99void InternalInterface::removeLED(const std::string& name)
100{
101 lg2::debug("RemoveLED is not configured {NAME}", "NAME", name);
102}
103
104int InternalInterface::addLedConfigure(sd_bus_message* msg, void* context,
105 sd_bus_error* error)
106{
107 if (msg == nullptr && context == nullptr)
108 {
109 lg2::error("Unable to configure addLed");
110 return -EINVAL;
111 }
112
113 try
114 {
115 auto message = sdbusplus::message_t(msg);
116 auto ledName = message.unpack<std::string>();
117
118 auto* self = static_cast<InternalInterface*>(context);
119 self->addLED(ledName);
120
121 auto reply = message.new_method_return();
122 reply.method_return();
123 }
124 catch (const sdbusplus::exception_t& e)
125 {
126 return sd_bus_error_set(error, e.name(), e.description());
127 }
128
129 return 1;
130}
131
132int InternalInterface::removeLedConfigure(sd_bus_message* msg, void* context,
133 sd_bus_error* error)
134{
135 if (msg == nullptr && context == nullptr)
136 {
137 lg2::error("Unable to configure removeLed");
138 return -EINVAL;
139 }
140
141 try
142 {
143 auto message = sdbusplus::message_t(msg);
144 auto ledName = message.unpack<std::string>();
145
146 auto* self = static_cast<InternalInterface*>(context);
147 self->removeLED(ledName);
148
149 auto reply = message.new_method_return();
150 reply.method_return();
151 }
152 catch (const sdbusplus::exception_t& e)
153 {
154 return sd_bus_error_set(error, e.name(), e.description());
155 }
156
157 return 1;
158}
159
160const std::array<sdbusplus::vtable::vtable_t, 4> InternalInterface::vtable = {
161 sdbusplus::vtable::start(),
162 // AddLed method takes a string parameter and returns void
163 sdbusplus::vtable::method("AddLED", "s", "", addLedConfigure),
164 // RemoveLed method takes a string parameter and returns void
165 sdbusplus::vtable::method("RemoveLED", "s", "", removeLedConfigure),
166 sdbusplus::vtable::end()};
167
168} // namespace interface
169} // namespace sysfs
170} // namespace led
171} // namespace phosphor