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 
21*673b981aSAlexander Hansen #include <algorithm>
22*673b981aSAlexander Hansen 
23b6779846SJayashree Dhanapal namespace phosphor
24b6779846SJayashree Dhanapal {
25b6779846SJayashree Dhanapal namespace led
26b6779846SJayashree Dhanapal {
27b6779846SJayashree Dhanapal namespace sysfs
28b6779846SJayashree Dhanapal {
29b6779846SJayashree Dhanapal namespace interface
30b6779846SJayashree Dhanapal {
31b6779846SJayashree Dhanapal 
32b6779846SJayashree Dhanapal InternalInterface::InternalInterface(sdbusplus::bus_t& bus, const char* path) :
33b6779846SJayashree Dhanapal     bus(bus), serverInterface(bus, path, internalInterface, vtable.data(), this)
34b6779846SJayashree Dhanapal {}
35b6779846SJayashree Dhanapal 
36b6779846SJayashree Dhanapal std::string InternalInterface::getDbusName(const LedDescr& ledDescr)
37b6779846SJayashree Dhanapal {
38b6779846SJayashree Dhanapal     std::vector<std::string> words;
3924d124f9SAlexander Hansen     if (ledDescr.devicename.has_value())
40b6779846SJayashree Dhanapal     {
4124d124f9SAlexander Hansen         words.emplace_back(ledDescr.devicename.value());
4224d124f9SAlexander Hansen     }
4324d124f9SAlexander Hansen     if (ledDescr.function.has_value())
4424d124f9SAlexander Hansen     {
4524d124f9SAlexander Hansen         words.emplace_back(ledDescr.function.value());
46b6779846SJayashree Dhanapal     }
47b6779846SJayashree Dhanapal 
4824d124f9SAlexander Hansen     if (ledDescr.color.has_value())
49b6779846SJayashree Dhanapal     {
5024d124f9SAlexander Hansen         words.emplace_back(ledDescr.color.value());
51b6779846SJayashree Dhanapal     }
52b6779846SJayashree Dhanapal 
53b6779846SJayashree Dhanapal     std::string s = boost::join(words, "_");
54b6779846SJayashree Dhanapal 
55*673b981aSAlexander Hansen     // we assume the string to be a correct dbus name besides
56*673b981aSAlexander Hansen     // this detail
57*673b981aSAlexander Hansen     std::replace(s.begin(), s.end(), '-', '_');
58b6779846SJayashree Dhanapal 
59*673b981aSAlexander Hansen     return s;
60b6779846SJayashree Dhanapal }
61b6779846SJayashree Dhanapal 
62b6779846SJayashree Dhanapal void InternalInterface::createLEDPath(const std::string& ledName)
63b6779846SJayashree Dhanapal {
64b6779846SJayashree Dhanapal     std::string name;
65b6779846SJayashree Dhanapal     std::string path = devParent + ledName;
66b6779846SJayashree Dhanapal 
67b6779846SJayashree Dhanapal     if (!std::filesystem::exists(fs::path(path)))
68b6779846SJayashree Dhanapal     {
69b6779846SJayashree Dhanapal         lg2::error("No such directory {PATH}", "PATH", path);
70b6779846SJayashree Dhanapal         return;
71b6779846SJayashree Dhanapal     }
72b6779846SJayashree Dhanapal 
7324d124f9SAlexander Hansen     auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
7424d124f9SAlexander Hansen 
75b6779846SJayashree Dhanapal     // Convert LED name in sysfs into DBus name
7624d124f9SAlexander Hansen     const LedDescr ledDescr = sled->getLedDescr();
7724d124f9SAlexander Hansen 
78b6779846SJayashree Dhanapal     name = getDbusName(ledDescr);
79b6779846SJayashree Dhanapal 
80b6779846SJayashree Dhanapal     lg2::debug("LED {NAME} receives dbus name {DBUSNAME}", "NAME", ledName,
81b6779846SJayashree Dhanapal                "DBUSNAME", name);
82b6779846SJayashree Dhanapal 
83b6779846SJayashree Dhanapal     // Unique path name representing a single LED.
84*673b981aSAlexander Hansen     std::string objPath = std::string(physParent) + "/" + name;
85b6779846SJayashree Dhanapal 
86b6779846SJayashree Dhanapal     if (leds.contains(objPath))
87b6779846SJayashree Dhanapal     {
88b6779846SJayashree Dhanapal         return;
89b6779846SJayashree Dhanapal     }
90b6779846SJayashree Dhanapal 
91b6779846SJayashree Dhanapal     leds.emplace(objPath, std::make_unique<phosphor::led::Physical>(
9224d124f9SAlexander Hansen                               bus, objPath, std::move(sled),
9324d124f9SAlexander Hansen                               ledDescr.color.value_or("")));
94b6779846SJayashree Dhanapal }
95b6779846SJayashree Dhanapal 
96b6779846SJayashree Dhanapal void InternalInterface::addLED(const std::string& name)
97b6779846SJayashree Dhanapal {
98b6779846SJayashree Dhanapal     createLEDPath(name);
99b6779846SJayashree Dhanapal }
100b6779846SJayashree Dhanapal 
101b6779846SJayashree Dhanapal // NOLINTNEXTLINE(readability-convert-member-functions-to-static)
102b6779846SJayashree Dhanapal void InternalInterface::removeLED(const std::string& name)
103b6779846SJayashree Dhanapal {
104b6779846SJayashree Dhanapal     lg2::debug("RemoveLED is not configured {NAME}", "NAME", name);
105b6779846SJayashree Dhanapal }
106b6779846SJayashree Dhanapal 
107b6779846SJayashree Dhanapal int InternalInterface::addLedConfigure(sd_bus_message* msg, void* context,
108b6779846SJayashree Dhanapal                                        sd_bus_error* error)
109b6779846SJayashree Dhanapal {
110b6779846SJayashree Dhanapal     if (msg == nullptr && context == nullptr)
111b6779846SJayashree Dhanapal     {
112b6779846SJayashree Dhanapal         lg2::error("Unable to configure addLed");
113b6779846SJayashree Dhanapal         return -EINVAL;
114b6779846SJayashree Dhanapal     }
115b6779846SJayashree Dhanapal 
116b6779846SJayashree Dhanapal     try
117b6779846SJayashree Dhanapal     {
118b6779846SJayashree Dhanapal         auto message = sdbusplus::message_t(msg);
119b6779846SJayashree Dhanapal         auto ledName = message.unpack<std::string>();
120b6779846SJayashree Dhanapal 
121b6779846SJayashree Dhanapal         auto* self = static_cast<InternalInterface*>(context);
122b6779846SJayashree Dhanapal         self->addLED(ledName);
123b6779846SJayashree Dhanapal 
124b6779846SJayashree Dhanapal         auto reply = message.new_method_return();
125b6779846SJayashree Dhanapal         reply.method_return();
126b6779846SJayashree Dhanapal     }
127b6779846SJayashree Dhanapal     catch (const sdbusplus::exception_t& e)
128b6779846SJayashree Dhanapal     {
129b6779846SJayashree Dhanapal         return sd_bus_error_set(error, e.name(), e.description());
130b6779846SJayashree Dhanapal     }
131b6779846SJayashree Dhanapal 
132b6779846SJayashree Dhanapal     return 1;
133b6779846SJayashree Dhanapal }
134b6779846SJayashree Dhanapal 
135b6779846SJayashree Dhanapal int InternalInterface::removeLedConfigure(sd_bus_message* msg, void* context,
136b6779846SJayashree Dhanapal                                           sd_bus_error* error)
137b6779846SJayashree Dhanapal {
138b6779846SJayashree Dhanapal     if (msg == nullptr && context == nullptr)
139b6779846SJayashree Dhanapal     {
140b6779846SJayashree Dhanapal         lg2::error("Unable to configure removeLed");
141b6779846SJayashree Dhanapal         return -EINVAL;
142b6779846SJayashree Dhanapal     }
143b6779846SJayashree Dhanapal 
144b6779846SJayashree Dhanapal     try
145b6779846SJayashree Dhanapal     {
146b6779846SJayashree Dhanapal         auto message = sdbusplus::message_t(msg);
147b6779846SJayashree Dhanapal         auto ledName = message.unpack<std::string>();
148b6779846SJayashree Dhanapal 
149b6779846SJayashree Dhanapal         auto* self = static_cast<InternalInterface*>(context);
150b6779846SJayashree Dhanapal         self->removeLED(ledName);
151b6779846SJayashree Dhanapal 
152b6779846SJayashree Dhanapal         auto reply = message.new_method_return();
153b6779846SJayashree Dhanapal         reply.method_return();
154b6779846SJayashree Dhanapal     }
155b6779846SJayashree Dhanapal     catch (const sdbusplus::exception_t& e)
156b6779846SJayashree Dhanapal     {
157b6779846SJayashree Dhanapal         return sd_bus_error_set(error, e.name(), e.description());
158b6779846SJayashree Dhanapal     }
159b6779846SJayashree Dhanapal 
160b6779846SJayashree Dhanapal     return 1;
161b6779846SJayashree Dhanapal }
162b6779846SJayashree Dhanapal 
163b6779846SJayashree Dhanapal const std::array<sdbusplus::vtable::vtable_t, 4> InternalInterface::vtable = {
164b6779846SJayashree Dhanapal     sdbusplus::vtable::start(),
165b6779846SJayashree Dhanapal     // AddLed method takes a string parameter and returns void
166b6779846SJayashree Dhanapal     sdbusplus::vtable::method("AddLED", "s", "", addLedConfigure),
167b6779846SJayashree Dhanapal     // RemoveLed method takes a string parameter and returns void
168b6779846SJayashree Dhanapal     sdbusplus::vtable::method("RemoveLED", "s", "", removeLedConfigure),
169b6779846SJayashree Dhanapal     sdbusplus::vtable::end()};
170b6779846SJayashree Dhanapal 
171b6779846SJayashree Dhanapal } // namespace interface
172b6779846SJayashree Dhanapal } // namespace sysfs
173b6779846SJayashree Dhanapal } // namespace led
174b6779846SJayashree Dhanapal } // namespace phosphor
175