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