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 17 #include "physical.hpp" 18 #include "sysfs.hpp" 19 20 #include <CLI/CLI.hpp> 21 #include <boost/algorithm/string.hpp> 22 23 #include <algorithm> 24 #include <iostream> 25 #include <string> 26 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; 45 // FIXME: https://bugs.llvm.org/show_bug.cgi?id=41141 46 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) 47 boost::split(words, name, boost::is_any_of(":")); 48 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks) 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()) 71 { 72 words.emplace_back(ledDescr.function); 73 } 74 if (!ledDescr.color.empty()) 75 { 76 words.emplace_back(ledDescr.color); 77 } 78 return boost::join(words, "_"); 79 } 80 81 int main(int argc, char** argv) 82 { 83 namespace fs = std::filesystem; 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/"; 87 88 CLI::App app{"Control and Drive the physical LEDs"}; 89 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(); 95 96 // Parse out Path argument. 97 try 98 { 99 app.parse(argc, argv); 100 } 101 catch (const CLI::Error& e) 102 { 103 return app.exit(e); 104 } 105 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. 110 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 113 // and we can go beyond leds/ to get the actual LED name. 114 // Refer: systemd/systemd#5072 115 116 // On an error, this throws an exception and terminates. 117 auto name = path.substr(strlen(devParent)); 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(), '/', '-'); 122 path = devParent + name; 123 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 132 // Convert LED name in sysfs into DBus name 133 LedDescr ledDescr; 134 getLedDescr(name, ledDescr); 135 136 name = getDbusName(ledDescr); 137 138 // Unique bus name representing a single LED. 139 auto busName = std::string(busParent) + '.' + name; 140 auto objPath = std::string(objParent) + '/' + name; 141 142 // Get a handle to system dbus. 143 auto bus = sdbusplus::bus::new_default(); 144 145 sdbusplus::server::manager_t manager{bus, objPath.c_str()}; 146 147 // Create the Physical LED objects for directing actions. 148 // Need to save this else sdbusplus destructor will wipe this off. 149 phosphor::led::SysfsLed sled{fs::path(path)}; 150 phosphor::led::Physical led(bus, objPath, sled, ledDescr.color); 151 152 /** @brief Claim the bus */ 153 bus.request_name(busName.c_str()); 154 155 /** @brief Wait for client requests */ 156 while (true) 157 { 158 // Handle dbus message / signals discarding unhandled 159 bus.process_discard(); 160 bus.wait(); 161 } 162 return 0; 163 } 164