1b6779846SJayashree Dhanapal /**
2b6779846SJayashree Dhanapal  * Copyright © 2020 IBM Corporation
3b6779846SJayashree Dhanapal  *
4b6779846SJayashree Dhanapal  * Licensed under the Apache License, Version 2.0 (the "License");
5b6779846SJayashree Dhanapal  * you may not use this file except in compliance with the License.
6b6779846SJayashree Dhanapal  * You may obtain a copy of the License at
7b6779846SJayashree Dhanapal  *
8b6779846SJayashree Dhanapal  *     http://www.apache.org/licenses/LICENSE-2.0
9b6779846SJayashree Dhanapal  *
10b6779846SJayashree Dhanapal  * Unless required by applicable law or agreed to in writing, software
11b6779846SJayashree Dhanapal  * distributed under the License is distributed on an "AS IS" BASIS,
12b6779846SJayashree Dhanapal  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b6779846SJayashree Dhanapal  * See the License for the specific language governing permissions and
14b6779846SJayashree Dhanapal  * limitations under the License.
15b6779846SJayashree Dhanapal  */
16b6779846SJayashree Dhanapal 
17b6779846SJayashree Dhanapal #include "internal_interface.hpp"
18b6779846SJayashree Dhanapal 
19b6779846SJayashree Dhanapal #include <sdbusplus/message.hpp>
20b6779846SJayashree Dhanapal 
21673b981aSAlexander Hansen #include <algorithm>
22a16bef7dSAlexander Hansen #include <iterator>
23a16bef7dSAlexander Hansen #include <numeric>
24673b981aSAlexander Hansen 
25b6779846SJayashree Dhanapal namespace phosphor
26b6779846SJayashree Dhanapal {
27b6779846SJayashree Dhanapal namespace led
28b6779846SJayashree Dhanapal {
29b6779846SJayashree Dhanapal namespace sysfs
30b6779846SJayashree Dhanapal {
31b6779846SJayashree Dhanapal namespace interface
32b6779846SJayashree Dhanapal {
33b6779846SJayashree Dhanapal 
InternalInterface(sdbusplus::bus_t & bus,const char * path)34b6779846SJayashree Dhanapal InternalInterface::InternalInterface(sdbusplus::bus_t& bus, const char* path) :
35b6779846SJayashree Dhanapal     bus(bus), serverInterface(bus, path, internalInterface, vtable.data(), this)
36b6779846SJayashree Dhanapal {}
37b6779846SJayashree Dhanapal 
getDbusName(const LedDescr & ledDescr)38b6779846SJayashree Dhanapal std::string InternalInterface::getDbusName(const LedDescr& ledDescr)
39b6779846SJayashree Dhanapal {
40b6779846SJayashree Dhanapal     std::vector<std::string> words;
4124d124f9SAlexander Hansen     if (ledDescr.devicename.has_value())
42b6779846SJayashree Dhanapal     {
4324d124f9SAlexander Hansen         words.emplace_back(ledDescr.devicename.value());
4424d124f9SAlexander Hansen     }
4524d124f9SAlexander Hansen     if (ledDescr.function.has_value())
4624d124f9SAlexander Hansen     {
4724d124f9SAlexander Hansen         words.emplace_back(ledDescr.function.value());
48b6779846SJayashree Dhanapal     }
49b6779846SJayashree Dhanapal 
5024d124f9SAlexander Hansen     if (ledDescr.color.has_value())
51b6779846SJayashree Dhanapal     {
5224d124f9SAlexander Hansen         words.emplace_back(ledDescr.color.value());
53b6779846SJayashree Dhanapal     }
54b6779846SJayashree Dhanapal 
55*db82c0f8SPatrick Williams     std::string s =
56*db82c0f8SPatrick Williams         std::accumulate(std::next(words.begin()), words.end(), words[0],
57a16bef7dSAlexander Hansen                         [](std::string a, const std::string& b) {
58a16bef7dSAlexander Hansen                             return std::move(a) + "_" + b;
59a16bef7dSAlexander Hansen                         });
60b6779846SJayashree Dhanapal 
61673b981aSAlexander Hansen     // we assume the string to be a correct dbus name besides
62673b981aSAlexander Hansen     // this detail
63673b981aSAlexander Hansen     std::replace(s.begin(), s.end(), '-', '_');
64b6779846SJayashree Dhanapal 
65673b981aSAlexander Hansen     return s;
66b6779846SJayashree Dhanapal }
67b6779846SJayashree Dhanapal 
createLEDPath(const std::string & ledName)68b6779846SJayashree Dhanapal void InternalInterface::createLEDPath(const std::string& ledName)
69b6779846SJayashree Dhanapal {
70b6779846SJayashree Dhanapal     std::string name;
71b6779846SJayashree Dhanapal     std::string path = devParent + ledName;
72b6779846SJayashree Dhanapal 
73b6779846SJayashree Dhanapal     if (!std::filesystem::exists(fs::path(path)))
74b6779846SJayashree Dhanapal     {
75b6779846SJayashree Dhanapal         lg2::error("No such directory {PATH}", "PATH", path);
76b6779846SJayashree Dhanapal         return;
77b6779846SJayashree Dhanapal     }
78b6779846SJayashree Dhanapal 
7924d124f9SAlexander Hansen     auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
8024d124f9SAlexander Hansen 
81b6779846SJayashree Dhanapal     // Convert LED name in sysfs into DBus name
8224d124f9SAlexander Hansen     const LedDescr ledDescr = sled->getLedDescr();
8324d124f9SAlexander Hansen 
84b6779846SJayashree Dhanapal     name = getDbusName(ledDescr);
85b6779846SJayashree Dhanapal 
86b6779846SJayashree Dhanapal     lg2::debug("LED {NAME} receives dbus name {DBUSNAME}", "NAME", ledName,
87b6779846SJayashree Dhanapal                "DBUSNAME", name);
88b6779846SJayashree Dhanapal 
89b6779846SJayashree Dhanapal     // Unique path name representing a single LED.
90673b981aSAlexander Hansen     std::string objPath = std::string(physParent) + "/" + name;
91b6779846SJayashree Dhanapal 
92b6779846SJayashree Dhanapal     if (leds.contains(objPath))
93b6779846SJayashree Dhanapal     {
94b6779846SJayashree Dhanapal         return;
95b6779846SJayashree Dhanapal     }
96b6779846SJayashree Dhanapal 
97b6779846SJayashree Dhanapal     leds.emplace(objPath, std::make_unique<phosphor::led::Physical>(
9824d124f9SAlexander Hansen                               bus, objPath, std::move(sled),
9924d124f9SAlexander Hansen                               ledDescr.color.value_or("")));
100b6779846SJayashree Dhanapal }
101b6779846SJayashree Dhanapal 
addLED(const std::string & name)102b6779846SJayashree Dhanapal void InternalInterface::addLED(const std::string& name)
103b6779846SJayashree Dhanapal {
104b6779846SJayashree Dhanapal     createLEDPath(name);
105b6779846SJayashree Dhanapal }
106b6779846SJayashree Dhanapal 
107b6779846SJayashree Dhanapal // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
removeLED(const std::string & name)108b6779846SJayashree Dhanapal void InternalInterface::removeLED(const std::string& name)
109b6779846SJayashree Dhanapal {
110b6779846SJayashree Dhanapal     lg2::debug("RemoveLED is not configured {NAME}", "NAME", name);
111b6779846SJayashree Dhanapal }
112b6779846SJayashree Dhanapal 
addLedConfigure(sd_bus_message * msg,void * context,sd_bus_error * error)113b6779846SJayashree Dhanapal int InternalInterface::addLedConfigure(sd_bus_message* msg, void* context,
114b6779846SJayashree Dhanapal                                        sd_bus_error* error)
115b6779846SJayashree Dhanapal {
116b6779846SJayashree Dhanapal     if (msg == nullptr && context == nullptr)
117b6779846SJayashree Dhanapal     {
118b6779846SJayashree Dhanapal         lg2::error("Unable to configure addLed");
119b6779846SJayashree Dhanapal         return -EINVAL;
120b6779846SJayashree Dhanapal     }
121b6779846SJayashree Dhanapal 
122b6779846SJayashree Dhanapal     try
123b6779846SJayashree Dhanapal     {
124b6779846SJayashree Dhanapal         auto message = sdbusplus::message_t(msg);
125b6779846SJayashree Dhanapal         auto ledName = message.unpack<std::string>();
126b6779846SJayashree Dhanapal 
127b6779846SJayashree Dhanapal         auto* self = static_cast<InternalInterface*>(context);
128b6779846SJayashree Dhanapal         self->addLED(ledName);
129b6779846SJayashree Dhanapal 
130b6779846SJayashree Dhanapal         auto reply = message.new_method_return();
131b6779846SJayashree Dhanapal         reply.method_return();
132b6779846SJayashree Dhanapal     }
133b6779846SJayashree Dhanapal     catch (const sdbusplus::exception_t& e)
134b6779846SJayashree Dhanapal     {
135b6779846SJayashree Dhanapal         return sd_bus_error_set(error, e.name(), e.description());
136b6779846SJayashree Dhanapal     }
137b6779846SJayashree Dhanapal 
138b6779846SJayashree Dhanapal     return 1;
139b6779846SJayashree Dhanapal }
140b6779846SJayashree Dhanapal 
removeLedConfigure(sd_bus_message * msg,void * context,sd_bus_error * error)141b6779846SJayashree Dhanapal int InternalInterface::removeLedConfigure(sd_bus_message* msg, void* context,
142b6779846SJayashree Dhanapal                                           sd_bus_error* error)
143b6779846SJayashree Dhanapal {
144b6779846SJayashree Dhanapal     if (msg == nullptr && context == nullptr)
145b6779846SJayashree Dhanapal     {
146b6779846SJayashree Dhanapal         lg2::error("Unable to configure removeLed");
147b6779846SJayashree Dhanapal         return -EINVAL;
148b6779846SJayashree Dhanapal     }
149b6779846SJayashree Dhanapal 
150b6779846SJayashree Dhanapal     try
151b6779846SJayashree Dhanapal     {
152b6779846SJayashree Dhanapal         auto message = sdbusplus::message_t(msg);
153b6779846SJayashree Dhanapal         auto ledName = message.unpack<std::string>();
154b6779846SJayashree Dhanapal 
155b6779846SJayashree Dhanapal         auto* self = static_cast<InternalInterface*>(context);
156b6779846SJayashree Dhanapal         self->removeLED(ledName);
157b6779846SJayashree Dhanapal 
158b6779846SJayashree Dhanapal         auto reply = message.new_method_return();
159b6779846SJayashree Dhanapal         reply.method_return();
160b6779846SJayashree Dhanapal     }
161b6779846SJayashree Dhanapal     catch (const sdbusplus::exception_t& e)
162b6779846SJayashree Dhanapal     {
163b6779846SJayashree Dhanapal         return sd_bus_error_set(error, e.name(), e.description());
164b6779846SJayashree Dhanapal     }
165b6779846SJayashree Dhanapal 
166b6779846SJayashree Dhanapal     return 1;
167b6779846SJayashree Dhanapal }
168b6779846SJayashree Dhanapal 
169b6779846SJayashree Dhanapal const std::array<sdbusplus::vtable::vtable_t, 4> InternalInterface::vtable = {
170b6779846SJayashree Dhanapal     sdbusplus::vtable::start(),
171b6779846SJayashree Dhanapal     // AddLed method takes a string parameter and returns void
172b6779846SJayashree Dhanapal     sdbusplus::vtable::method("AddLED", "s", "", addLedConfigure),
173b6779846SJayashree Dhanapal     // RemoveLed method takes a string parameter and returns void
174b6779846SJayashree Dhanapal     sdbusplus::vtable::method("RemoveLED", "s", "", removeLedConfigure),
175b6779846SJayashree Dhanapal     sdbusplus::vtable::end()};
176b6779846SJayashree Dhanapal 
177b6779846SJayashree Dhanapal } // namespace interface
178b6779846SJayashree Dhanapal } // namespace sysfs
179b6779846SJayashree Dhanapal } // namespace led
180b6779846SJayashree Dhanapal } // namespace phosphor
181