blob: 685a4fcee61a1b79491816715fbf2b69587b20fe [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
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093017#include "config.h"
18
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053019#include "argument.hpp"
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053020#include "physical.hpp"
Andrew Jeffery42e02d32018-05-24 13:34:05 +093021#include "sysfs.hpp"
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093022
Alexander Soldatov97ddb722019-04-16 09:10:00 +030023#include <boost/algorithm/string.hpp>
George Liu61b90632020-06-22 10:55:13 +080024
25#include <algorithm>
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093026#include <iostream>
27#include <string>
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053028
29static void ExitWithError(const char* err, char** argv)
30{
31 phosphor::led::ArgumentParser::usage(argv);
32 std::cerr << std::endl;
33 std::cerr << "ERROR: " << err << std::endl;
34 exit(-1);
35}
36
Alexander Soldatov97ddb722019-04-16 09:10:00 +030037struct LedDescr
38{
39 std::string devicename;
40 std::string color;
41 std::string function;
42};
43
44/** @brief parse LED name in sysfs
45 * Parse sysfs LED name in format "devicename:colour:function"
46 * or "devicename:colour" or "devicename" and sets corresponding
47 * fields in LedDescr struct.
48 *
49 * @param[in] name - LED name in sysfs
50 * @param[out] ledDescr - LED description
51 */
52void getLedDescr(const std::string& name, LedDescr& ledDescr)
53{
54 std::vector<std::string> words;
55 boost::split(words, name, boost::is_any_of(":"));
56 try
57 {
58 ledDescr.devicename = words.at(0);
59 ledDescr.color = words.at(1);
60 ledDescr.function = words.at(2);
61 }
62 catch (const std::out_of_range&)
63 {
64 return;
65 }
66}
67
68/** @brief generates LED DBus name from LED description
69 *
70 * @param[in] name - LED description
71 * @return - DBus LED name
72 */
73std::string getDbusName(const LedDescr& ledDescr)
74{
75 std::vector<std::string> words;
76 words.emplace_back(ledDescr.devicename);
77 if (!ledDescr.function.empty())
78 words.emplace_back(ledDescr.function);
79 if (!ledDescr.color.empty())
80 words.emplace_back(ledDescr.color);
81 return boost::join(words, "_");
82}
83
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053084int main(int argc, char** argv)
85{
Andrew Jeffery42e02d32018-05-24 13:34:05 +093086 namespace fs = std::experimental::filesystem;
87
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053088 // Read arguments.
89 auto options = phosphor::led::ArgumentParser(argc, argv);
90
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053091 // Parse out Path argument.
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053092 auto path = std::move((options)["path"]);
93 if (path == phosphor::led::ArgumentParser::empty_string)
94 {
95 ExitWithError("Path not specified.", argv);
96 }
97
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +053098 // If the LED has a hyphen in the name like: "one-two", then it gets passed
99 // as /one/two/ as opposed to /one-two to the service file. There is a
100 // change needed in systemd to solve this issue and hence putting in this
101 // work-around.
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530102
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +0530103 // Since this application always gets invoked as part of a udev rule,
104 // it is always guaranteed to get /sys/class/leds/one/two
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300105 // and we can go beyond leds/ to get the actual LED name.
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +0530106 // Refer: systemd/systemd#5072
107
108 // On an error, this throws an exception and terminates.
109 auto name = path.substr(strlen(DEVPATH));
110
111 // LED names may have a hyphen and that would be an issue for
112 // dbus paths and hence need to convert them to underscores.
113 std::replace(name.begin(), name.end(), '/', '-');
Andrew Jefferycd3b05e2018-05-24 13:05:30 +0930114 path = DEVPATH + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530115
Vishwanatha Subbanna413fd342017-03-30 17:25:18 +0530116 // Convert to lowercase just in case some are not and that
117 // we follow lowercase all over
118 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
119
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(), '-', '_');
123
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300124 // Convert LED name in sysfs into DBus name
125 LedDescr ledDescr;
126 getLedDescr(name, ledDescr);
127 name = getDbusName(ledDescr);
128
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530129 // Unique bus name representing a single LED.
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +0930130 auto busName = std::string(BUSNAME) + '.' + name;
131 auto objPath = std::string(OBJPATH) + '/' + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530132
133 // Get a handle to system dbus.
Andrew Jefferycd3b05e2018-05-24 13:05:30 +0930134 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530135
136 // Add systemd object manager.
137 sdbusplus::server::manager::manager(bus, objPath.c_str());
138
139 // Create the Physical LED objects for directing actions.
140 // Need to save this else sdbusplus destructor will wipe this off.
Andrew Jeffery42e02d32018-05-24 13:34:05 +0930141 phosphor::led::SysfsLed sled{fs::path(path)};
Alexander Soldatov97ddb722019-04-16 09:10:00 +0300142 phosphor::led::Physical led(bus, objPath, sled, ledDescr.color);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530143
144 /** @brief Claim the bus */
145 bus.request_name(busName.c_str());
146
147 /** @brief Wait for client requests */
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +0930148 while (true)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +0530149 {
150 // Handle dbus message / signals discarding unhandled
151 bus.process_discard();
152 bus.wait();
153 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +0530154 return 0;
155}