Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 1 | /** |
| 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 Hansen | 673b981 | 2024-08-01 15:21:34 +0200 | [diff] [blame^] | 21 | #include <algorithm> |
| 22 | |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 23 | namespace phosphor |
| 24 | { |
| 25 | namespace led |
| 26 | { |
| 27 | namespace sysfs |
| 28 | { |
| 29 | namespace interface |
| 30 | { |
| 31 | |
| 32 | InternalInterface::InternalInterface(sdbusplus::bus_t& bus, const char* path) : |
| 33 | bus(bus), serverInterface(bus, path, internalInterface, vtable.data(), this) |
| 34 | {} |
| 35 | |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 36 | std::string InternalInterface::getDbusName(const LedDescr& ledDescr) |
| 37 | { |
| 38 | std::vector<std::string> words; |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 39 | if (ledDescr.devicename.has_value()) |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 40 | { |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 41 | words.emplace_back(ledDescr.devicename.value()); |
| 42 | } |
| 43 | if (ledDescr.function.has_value()) |
| 44 | { |
| 45 | words.emplace_back(ledDescr.function.value()); |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 46 | } |
| 47 | |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 48 | if (ledDescr.color.has_value()) |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 49 | { |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 50 | words.emplace_back(ledDescr.color.value()); |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | std::string s = boost::join(words, "_"); |
| 54 | |
Alexander Hansen | 673b981 | 2024-08-01 15:21:34 +0200 | [diff] [blame^] | 55 | // we assume the string to be a correct dbus name besides |
| 56 | // this detail |
| 57 | std::replace(s.begin(), s.end(), '-', '_'); |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 58 | |
Alexander Hansen | 673b981 | 2024-08-01 15:21:34 +0200 | [diff] [blame^] | 59 | return s; |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void 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 Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 73 | auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path)); |
| 74 | |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 75 | // Convert LED name in sysfs into DBus name |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 76 | const LedDescr ledDescr = sled->getLedDescr(); |
| 77 | |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 78 | 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 Hansen | 673b981 | 2024-08-01 15:21:34 +0200 | [diff] [blame^] | 84 | std::string objPath = std::string(physParent) + "/" + name; |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 85 | |
| 86 | if (leds.contains(objPath)) |
| 87 | { |
| 88 | return; |
| 89 | } |
| 90 | |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 91 | leds.emplace(objPath, std::make_unique<phosphor::led::Physical>( |
Alexander Hansen | 24d124f | 2024-07-16 13:49:55 +0200 | [diff] [blame] | 92 | bus, objPath, std::move(sled), |
| 93 | ledDescr.color.value_or(""))); |
Jayashree Dhanapal | b677984 | 2022-10-07 13:34:16 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void InternalInterface::addLED(const std::string& name) |
| 97 | { |
| 98 | createLEDPath(name); |
| 99 | } |
| 100 | |
| 101 | // NOLINTNEXTLINE(readability-convert-member-functions-to-static) |
| 102 | void InternalInterface::removeLED(const std::string& name) |
| 103 | { |
| 104 | lg2::debug("RemoveLED is not configured {NAME}", "NAME", name); |
| 105 | } |
| 106 | |
| 107 | int 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 | |
| 135 | int 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 | |
| 163 | const 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 |