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 "argument.hpp"
18 #include "physical.hpp"
19 #include "sysfs.hpp"
20 
21 #include <boost/algorithm/string.hpp>
22 
23 #include <algorithm>
24 #include <iostream>
25 #include <string>
26 
27 static void ExitWithError(const char* err, char** argv)
28 {
29     phosphor::led::ArgumentParser::usage(argv);
30     std::cerr << std::endl;
31     std::cerr << "ERROR: " << err << std::endl;
32     exit(-1);
33 }
34 
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())
76         words.emplace_back(ledDescr.function);
77     if (!ledDescr.color.empty())
78         words.emplace_back(ledDescr.color);
79     return boost::join(words, "_");
80 }
81 
82 int main(int argc, char** argv)
83 {
84     namespace fs = std::filesystem;
85     static constexpr auto BUSNAME = "xyz.openbmc_project.LED.Controller";
86     static constexpr auto OBJPATH = "/xyz/openbmc_project/led/physical";
87     static constexpr auto DEVPATH = "/sys/class/leds/";
88 
89     // Read arguments.
90     auto options = phosphor::led::ArgumentParser(argc, argv);
91 
92     // Parse out Path argument.
93     auto path = std::move((options)["path"]);
94     if (path == phosphor::led::ArgumentParser::empty_string)
95     {
96         ExitWithError("Path not specified.", argv);
97     }
98 
99     // If the LED has a hyphen in the name like: "one-two", then it gets passed
100     // as /one/two/ as opposed to /one-two to the service file. There is a
101     // change needed in systemd to solve this issue and hence putting in this
102     // work-around.
103 
104     // Since this application always gets invoked as part of a udev rule,
105     // it is always guaranteed to get /sys/class/leds/one/two
106     // and we can go beyond leds/ to get the actual LED name.
107     // Refer: systemd/systemd#5072
108 
109     // On an error, this throws an exception and terminates.
110     auto name = path.substr(strlen(DEVPATH));
111 
112     // LED names may have a hyphen and that would be an issue for
113     // dbus paths and hence need to convert them to underscores.
114     std::replace(name.begin(), name.end(), '/', '-');
115     path = DEVPATH + name;
116 
117     // Convert to lowercase just in case some are not and that
118     // we follow lowercase all over
119     std::transform(name.begin(), name.end(), name.begin(), ::tolower);
120 
121     // LED names may have a hyphen and that would be an issue for
122     // dbus paths and hence need to convert them to underscores.
123     std::replace(name.begin(), name.end(), '-', '_');
124 
125     // Convert LED name in sysfs into DBus name
126     LedDescr ledDescr;
127     getLedDescr(name, ledDescr);
128     name = getDbusName(ledDescr);
129 
130     // Unique bus name representing a single LED.
131     auto busName = std::string(BUSNAME) + '.' + name;
132     auto objPath = std::string(OBJPATH) + '/' + name;
133 
134     // Get a handle to system dbus.
135     auto bus = sdbusplus::bus::new_default();
136 
137     // Add systemd object manager.
138     sdbusplus::server::manager_t(bus, objPath.c_str());
139 
140     // Create the Physical LED objects for directing actions.
141     // Need to save this else sdbusplus destructor will wipe this off.
142     phosphor::led::SysfsLed sled{fs::path(path)};
143     phosphor::led::Physical led(bus, objPath, sled, ledDescr.color);
144 
145     /** @brief Claim the bus */
146     bus.request_name(busName.c_str());
147 
148     /** @brief Wait for client requests */
149     while (true)
150     {
151         // Handle dbus message / signals discarding unhandled
152         bus.process_discard();
153         bus.wait();
154     }
155     return 0;
156 }
157