Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 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 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 17 | #include "physical.hpp" |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 18 | #include "sysfs.hpp" |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 19 | |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 20 | #include <CLI/CLI.hpp> |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 21 | #include <boost/algorithm/string.hpp> |
George Liu | 61b9063 | 2020-06-22 10:55:13 +0800 | [diff] [blame] | 22 | |
| 23 | #include <algorithm> |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 24 | #include <iostream> |
| 25 | #include <string> |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 26 | |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 27 | struct LedDescr |
| 28 | { |
| 29 | std::string devicename; |
| 30 | std::string color; |
| 31 | std::string function; |
| 32 | }; |
| 33 | |
| 34 | /** @brief parse LED name in sysfs |
| 35 | * Parse sysfs LED name in format "devicename:colour:function" |
| 36 | * or "devicename:colour" or "devicename" and sets corresponding |
| 37 | * fields in LedDescr struct. |
| 38 | * |
| 39 | * @param[in] name - LED name in sysfs |
| 40 | * @param[out] ledDescr - LED description |
| 41 | */ |
| 42 | void getLedDescr(const std::string& name, LedDescr& ledDescr) |
| 43 | { |
| 44 | std::vector<std::string> words; |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 45 | // FIXME: https://bugs.llvm.org/show_bug.cgi?id=41141 |
| 46 | // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 47 | boost::split(words, name, boost::is_any_of(":")); |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 48 | // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 49 | try |
| 50 | { |
| 51 | ledDescr.devicename = words.at(0); |
| 52 | ledDescr.color = words.at(1); |
| 53 | ledDescr.function = words.at(2); |
| 54 | } |
| 55 | catch (const std::out_of_range&) |
| 56 | { |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** @brief generates LED DBus name from LED description |
| 62 | * |
| 63 | * @param[in] name - LED description |
| 64 | * @return - DBus LED name |
| 65 | */ |
| 66 | std::string getDbusName(const LedDescr& ledDescr) |
| 67 | { |
| 68 | std::vector<std::string> words; |
| 69 | words.emplace_back(ledDescr.devicename); |
| 70 | if (!ledDescr.function.empty()) |
Andrew Jeffery | 20aef9a | 2023-02-06 10:41:07 +1030 | [diff] [blame] | 71 | { |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 72 | words.emplace_back(ledDescr.function); |
Andrew Jeffery | 20aef9a | 2023-02-06 10:41:07 +1030 | [diff] [blame] | 73 | } |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 74 | if (!ledDescr.color.empty()) |
Andrew Jeffery | 20aef9a | 2023-02-06 10:41:07 +1030 | [diff] [blame] | 75 | { |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 76 | words.emplace_back(ledDescr.color); |
Andrew Jeffery | 20aef9a | 2023-02-06 10:41:07 +1030 | [diff] [blame] | 77 | } |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 78 | return boost::join(words, "_"); |
| 79 | } |
| 80 | |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 81 | int main(int argc, char** argv) |
| 82 | { |
George Liu | 45eba6f | 2021-05-18 14:32:20 +0800 | [diff] [blame] | 83 | namespace fs = std::filesystem; |
Andrew Jeffery | e2dc00a | 2023-02-06 10:44:03 +1030 | [diff] [blame] | 84 | static constexpr auto busParent = "xyz.openbmc_project.LED.Controller"; |
| 85 | static constexpr auto objParent = "/xyz/openbmc_project/led/physical"; |
| 86 | static constexpr auto devParent = "/sys/class/leds/"; |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 87 | |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 88 | CLI::App app{"Control and Drive the physical LEDs"}; |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 89 | |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 90 | // Read arguments. |
| 91 | std::string path{}; |
| 92 | app.add_option("-p,--path", path, |
| 93 | "absolute path of LED in sysfs; like /sys/class/leds/<name>") |
| 94 | ->required(); |
Andrew Jeffery | cb14f2d | 2023-02-06 14:19:37 +1030 | [diff] [blame] | 95 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 96 | // Parse out Path argument. |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 97 | try |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 98 | { |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 99 | app.parse(argc, argv); |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 100 | } |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 101 | catch (const CLI::Error& e) |
| 102 | { |
| 103 | return app.exit(e); |
| 104 | } |
Andrew Jeffery | 444ee36 | 2023-02-06 10:47:54 +1030 | [diff] [blame] | 105 | |
Vishwanatha Subbanna | f9de54b | 2017-05-25 21:06:33 +0530 | [diff] [blame] | 106 | // If the LED has a hyphen in the name like: "one-two", then it gets passed |
| 107 | // as /one/two/ as opposed to /one-two to the service file. There is a |
| 108 | // change needed in systemd to solve this issue and hence putting in this |
| 109 | // work-around. |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 110 | |
Vishwanatha Subbanna | f9de54b | 2017-05-25 21:06:33 +0530 | [diff] [blame] | 111 | // Since this application always gets invoked as part of a udev rule, |
| 112 | // it is always guaranteed to get /sys/class/leds/one/two |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 113 | // and we can go beyond leds/ to get the actual LED name. |
Vishwanatha Subbanna | f9de54b | 2017-05-25 21:06:33 +0530 | [diff] [blame] | 114 | // Refer: systemd/systemd#5072 |
| 115 | |
| 116 | // On an error, this throws an exception and terminates. |
Andrew Jeffery | e2dc00a | 2023-02-06 10:44:03 +1030 | [diff] [blame] | 117 | auto name = path.substr(strlen(devParent)); |
Vishwanatha Subbanna | f9de54b | 2017-05-25 21:06:33 +0530 | [diff] [blame] | 118 | |
| 119 | // LED names may have a hyphen and that would be an issue for |
| 120 | // dbus paths and hence need to convert them to underscores. |
| 121 | std::replace(name.begin(), name.end(), '/', '-'); |
Andrew Jeffery | e2dc00a | 2023-02-06 10:44:03 +1030 | [diff] [blame] | 122 | path = devParent + name; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 123 | |
Vishwanatha Subbanna | 413fd34 | 2017-03-30 17:25:18 +0530 | [diff] [blame] | 124 | // Convert to lowercase just in case some are not and that |
| 125 | // we follow lowercase all over |
| 126 | std::transform(name.begin(), name.end(), name.begin(), ::tolower); |
| 127 | |
| 128 | // LED names may have a hyphen and that would be an issue for |
| 129 | // dbus paths and hence need to convert them to underscores. |
| 130 | std::replace(name.begin(), name.end(), '-', '_'); |
| 131 | |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 132 | // Convert LED name in sysfs into DBus name |
| 133 | LedDescr ledDescr; |
| 134 | getLedDescr(name, ledDescr); |
George Liu | eaa1c89 | 2023-08-03 16:19:51 +0800 | [diff] [blame] | 135 | |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 136 | name = getDbusName(ledDescr); |
| 137 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 138 | // Unique bus name representing a single LED. |
Andrew Jeffery | e2dc00a | 2023-02-06 10:44:03 +1030 | [diff] [blame] | 139 | auto busName = std::string(busParent) + '.' + name; |
| 140 | auto objPath = std::string(objParent) + '/' + name; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 141 | |
| 142 | // Get a handle to system dbus. |
Andrew Jeffery | cd3b05e | 2018-05-24 13:05:30 +0930 | [diff] [blame] | 143 | auto bus = sdbusplus::bus::new_default(); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 144 | |
Andrew Jeffery | b754cf0 | 2023-02-06 11:54:12 +1030 | [diff] [blame] | 145 | sdbusplus::server::manager_t manager{bus, objPath.c_str()}; |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 146 | |
| 147 | // Create the Physical LED objects for directing actions. |
| 148 | // Need to save this else sdbusplus destructor will wipe this off. |
Andrew Jeffery | 42e02d3 | 2018-05-24 13:34:05 +0930 | [diff] [blame] | 149 | phosphor::led::SysfsLed sled{fs::path(path)}; |
Alexander Soldatov | 97ddb72 | 2019-04-16 09:10:00 +0300 | [diff] [blame] | 150 | phosphor::led::Physical led(bus, objPath, sled, ledDescr.color); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 151 | |
| 152 | /** @brief Claim the bus */ |
| 153 | bus.request_name(busName.c_str()); |
| 154 | |
| 155 | /** @brief Wait for client requests */ |
Andrew Jeffery | c41bf5b | 2018-05-25 16:39:22 +0930 | [diff] [blame] | 156 | while (true) |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 157 | { |
| 158 | // Handle dbus message / signals discarding unhandled |
| 159 | bus.process_discard(); |
| 160 | bus.wait(); |
| 161 | } |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 162 | return 0; |
| 163 | } |