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