blob: c990632bb7a51b9536c32d0a50846e8f4413b462 [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
Alexander Hansen673b9812024-08-01 15:21:34 +020021#include <algorithm>
22
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053023namespace phosphor
24{
25namespace led
26{
27namespace sysfs
28{
29namespace interface
30{
31
32InternalInterface::InternalInterface(sdbusplus::bus_t& bus, const char* path) :
33 bus(bus), serverInterface(bus, path, internalInterface, vtable.data(), this)
34{}
35
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053036std::string InternalInterface::getDbusName(const LedDescr& ledDescr)
37{
38 std::vector<std::string> words;
Alexander Hansen24d124f2024-07-16 13:49:55 +020039 if (ledDescr.devicename.has_value())
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053040 {
Alexander Hansen24d124f2024-07-16 13:49:55 +020041 words.emplace_back(ledDescr.devicename.value());
42 }
43 if (ledDescr.function.has_value())
44 {
45 words.emplace_back(ledDescr.function.value());
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053046 }
47
Alexander Hansen24d124f2024-07-16 13:49:55 +020048 if (ledDescr.color.has_value())
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053049 {
Alexander Hansen24d124f2024-07-16 13:49:55 +020050 words.emplace_back(ledDescr.color.value());
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053051 }
52
53 std::string s = boost::join(words, "_");
54
Alexander Hansen673b9812024-08-01 15:21:34 +020055 // we assume the string to be a correct dbus name besides
56 // this detail
57 std::replace(s.begin(), s.end(), '-', '_');
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053058
Alexander Hansen673b9812024-08-01 15:21:34 +020059 return s;
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053060}
61
62void InternalInterface::createLEDPath(const std::string& ledName)
63{
64 std::string name;
65 std::string path = devParent + ledName;
66
67 if (!std::filesystem::exists(fs::path(path)))
68 {
69 lg2::error("No such directory {PATH}", "PATH", path);
70 return;
71 }
72
Alexander Hansen24d124f2024-07-16 13:49:55 +020073 auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
74
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053075 // Convert LED name in sysfs into DBus name
Alexander Hansen24d124f2024-07-16 13:49:55 +020076 const LedDescr ledDescr = sled->getLedDescr();
77
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053078 name = getDbusName(ledDescr);
79
80 lg2::debug("LED {NAME} receives dbus name {DBUSNAME}", "NAME", ledName,
81 "DBUSNAME", name);
82
83 // Unique path name representing a single LED.
Alexander Hansen673b9812024-08-01 15:21:34 +020084 std::string objPath = std::string(physParent) + "/" + name;
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053085
86 if (leds.contains(objPath))
87 {
88 return;
89 }
90
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053091 leds.emplace(objPath, std::make_unique<phosphor::led::Physical>(
Alexander Hansen24d124f2024-07-16 13:49:55 +020092 bus, objPath, std::move(sled),
93 ledDescr.color.value_or("")));
Jayashree Dhanapalb6779842022-10-07 13:34:16 +053094}
95
96void InternalInterface::addLED(const std::string& name)
97{
98 createLEDPath(name);
99}
100
101// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
102void InternalInterface::removeLED(const std::string& name)
103{
104 lg2::debug("RemoveLED is not configured {NAME}", "NAME", name);
105}
106
107int InternalInterface::addLedConfigure(sd_bus_message* msg, void* context,
108 sd_bus_error* error)
109{
110 if (msg == nullptr && context == nullptr)
111 {
112 lg2::error("Unable to configure addLed");
113 return -EINVAL;
114 }
115
116 try
117 {
118 auto message = sdbusplus::message_t(msg);
119 auto ledName = message.unpack<std::string>();
120
121 auto* self = static_cast<InternalInterface*>(context);
122 self->addLED(ledName);
123
124 auto reply = message.new_method_return();
125 reply.method_return();
126 }
127 catch (const sdbusplus::exception_t& e)
128 {
129 return sd_bus_error_set(error, e.name(), e.description());
130 }
131
132 return 1;
133}
134
135int InternalInterface::removeLedConfigure(sd_bus_message* msg, void* context,
136 sd_bus_error* error)
137{
138 if (msg == nullptr && context == nullptr)
139 {
140 lg2::error("Unable to configure removeLed");
141 return -EINVAL;
142 }
143
144 try
145 {
146 auto message = sdbusplus::message_t(msg);
147 auto ledName = message.unpack<std::string>();
148
149 auto* self = static_cast<InternalInterface*>(context);
150 self->removeLED(ledName);
151
152 auto reply = message.new_method_return();
153 reply.method_return();
154 }
155 catch (const sdbusplus::exception_t& e)
156 {
157 return sd_bus_error_set(error, e.name(), e.description());
158 }
159
160 return 1;
161}
162
163const std::array<sdbusplus::vtable::vtable_t, 4> InternalInterface::vtable = {
164 sdbusplus::vtable::start(),
165 // AddLed method takes a string parameter and returns void
166 sdbusplus::vtable::method("AddLED", "s", "", addLedConfigure),
167 // RemoveLed method takes a string parameter and returns void
168 sdbusplus::vtable::method("RemoveLED", "s", "", removeLedConfigure),
169 sdbusplus::vtable::end()};
170
171} // namespace interface
172} // namespace sysfs
173} // namespace led
174} // namespace phosphor