blob: afe476e4fec9b3edc5c03fb9e6c48f15c7b77bb6 [file] [log] [blame]
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +05301/**
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 Subbanna835571e2016-11-30 11:29:30 +053017#include "argument.hpp"
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053018#include "physical.hpp"
Andrew Jeffery42e02d32018-05-24 13:34:05 +093019#include "sysfs.hpp"
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093020
Alexander Soldatov97ddb722019-04-16 09:10:00 +030021#include <boost/algorithm/string.hpp>
George Liu61b90632020-06-22 10:55:13 +080022
23#include <algorithm>
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093024#include <iostream>
25#include <string>
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053026
Andrew Jefferybd0b9e72023-02-06 10:38:45 +103027static void exitWithError(const char* err, char** argv)
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053028{
29 phosphor::led::ArgumentParser::usage(argv);
30 std::cerr << std::endl;
31 std::cerr << "ERROR: " << err << std::endl;
32 exit(-1);
33}
34
Alexander Soldatov97ddb722019-04-16 09:10:00 +030035struct 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 */
50void 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 */
71std::string getDbusName(const LedDescr& ledDescr)
72{
73 std::vector<std::string> words;
74 words.emplace_back(ledDescr.devicename);
75 if (!ledDescr.function.empty())
Andrew Jeffery20aef9a2023-02-06 10:41:07 +103076 {
Alexander Soldatov97ddb722019-04-16 09:10:00 +030077 words.emplace_back(ledDescr.function);
Andrew Jeffery20aef9a2023-02-06 10:41:07 +103078 }
Alexander Soldatov97ddb722019-04-16 09:10:00 +030079 if (!ledDescr.color.empty())
Andrew Jeffery20aef9a2023-02-06 10:41:07 +103080 {
Alexander Soldatov97ddb722019-04-16 09:10:00 +030081 words.emplace_back(ledDescr.color);
Andrew Jeffery20aef9a2023-02-06 10:41:07 +103082 }
Alexander Soldatov97ddb722019-04-16 09:10:00 +030083 return boost::join(words, "_");
84}
85
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053086int main(int argc, char** argv)
87{
George Liu45eba6f2021-05-18 14:32:20 +080088 namespace fs = std::filesystem;
Andrew Jefferye2dc00a2023-02-06 10:44:03 +103089 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 Jeffery42e02d32018-05-24 13:34:05 +093092
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053093 // Read arguments.
94 auto options = phosphor::led::ArgumentParser(argc, argv);
95
Andrew Jefferycb14f2d2023-02-06 14:19:37 +103096 // FIXME: https://bugs.llvm.org/show_bug.cgi?id=41141
97 // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
98
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053099 // Parse out Path argument.
Andrew Jeffery444ee362023-02-06 10:47:54 +1030100 if (options["path"].empty())
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +0530101 {
Andrew Jefferybd0b9e72023-02-06 10:38:45 +1030102 exitWithError("Path not specified.", argv);
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +0530103 }
104
Andrew Jeffery444ee362023-02-06 10:47:54 +1030105 auto path = options["path"];
106
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +0530107 // If the LED has a hyphen in the name like: "one-two", then it gets passed
108 // as /one/two/ as opposed to /one-two to the service file. There is a
109 // change needed in systemd to solve this issue and hence putting in this
110 // work-around.
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530111
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +0530112 // Since this application always gets invoked as part of a udev rule,
113 // it is always guaranteed to get /sys/class/leds/one/two
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300114 // and we can go beyond leds/ to get the actual LED name.
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +0530115 // Refer: systemd/systemd#5072
116
117 // On an error, this throws an exception and terminates.
Andrew Jefferye2dc00a2023-02-06 10:44:03 +1030118 auto name = path.substr(strlen(devParent));
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +0530119
120 // LED names may have a hyphen and that would be an issue for
121 // dbus paths and hence need to convert them to underscores.
122 std::replace(name.begin(), name.end(), '/', '-');
Andrew Jefferye2dc00a2023-02-06 10:44:03 +1030123 path = devParent + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530124
Vishwanatha Subbanna413fd342017-03-30 17:25:18 +0530125 // Convert to lowercase just in case some are not and that
126 // we follow lowercase all over
127 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
128
129 // LED names may have a hyphen and that would be an issue for
130 // dbus paths and hence need to convert them to underscores.
131 std::replace(name.begin(), name.end(), '-', '_');
132
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300133 // Convert LED name in sysfs into DBus name
134 LedDescr ledDescr;
135 getLedDescr(name, ledDescr);
Andrew Jefferycb14f2d2023-02-06 14:19:37 +1030136 // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300137 name = getDbusName(ledDescr);
138
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530139 // Unique bus name representing a single LED.
Andrew Jefferye2dc00a2023-02-06 10:44:03 +1030140 auto busName = std::string(busParent) + '.' + name;
141 auto objPath = std::string(objParent) + '/' + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530142
143 // Get a handle to system dbus.
Andrew Jefferycd3b05e2018-05-24 13:05:30 +0930144 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530145
Andrew Jefferyb754cf02023-02-06 11:54:12 +1030146 sdbusplus::server::manager_t manager{bus, objPath.c_str()};
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530147
148 // Create the Physical LED objects for directing actions.
149 // Need to save this else sdbusplus destructor will wipe this off.
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930150 phosphor::led::SysfsLed sled{fs::path(path)};
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300151 phosphor::led::Physical led(bus, objPath, sled, ledDescr.color);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530152
153 /** @brief Claim the bus */
154 bus.request_name(busName.c_str());
155
156 /** @brief Wait for client requests */
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +0930157 while (true)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530158 {
159 // Handle dbus message / signals discarding unhandled
160 bus.process_discard();
161 bus.wait();
162 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +0530163 return 0;
164}