blob: f94503479c4a2eb189bf0d0b2f5e92a7502c16c2 [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 Jefferyc41bf5b2018-05-25 16:39:22 +093021
22#include <algorithm>
23#include <iostream>
24#include <string>
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053025
26static void ExitWithError(const char* err, char** argv)
27{
28 phosphor::led::ArgumentParser::usage(argv);
29 std::cerr << std::endl;
30 std::cerr << "ERROR: " << err << std::endl;
31 exit(-1);
32}
33
34int main(int argc, char** argv)
35{
36 // Read arguments.
37 auto options = phosphor::led::ArgumentParser(argc, argv);
38
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053039 // Parse out Path argument.
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053040 auto path = std::move((options)["path"]);
41 if (path == phosphor::led::ArgumentParser::empty_string)
42 {
43 ExitWithError("Path not specified.", argv);
44 }
45
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +053046 // If the LED has a hyphen in the name like: "one-two", then it gets passed
47 // as /one/two/ as opposed to /one-two to the service file. There is a
48 // change needed in systemd to solve this issue and hence putting in this
49 // work-around.
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053050
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +053051 // Since this application always gets invoked as part of a udev rule,
52 // it is always guaranteed to get /sys/class/leds/one/two
53 // and we can go beyond leds/ to get the actual led name.
54 // Refer: systemd/systemd#5072
55
56 // On an error, this throws an exception and terminates.
57 auto name = path.substr(strlen(DEVPATH));
58
59 // LED names may have a hyphen and that would be an issue for
60 // dbus paths and hence need to convert them to underscores.
61 std::replace(name.begin(), name.end(), '/', '-');
62 path = std::move(DEVPATH + name);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053063
Vishwanatha Subbanna413fd342017-03-30 17:25:18 +053064 // Convert to lowercase just in case some are not and that
65 // we follow lowercase all over
66 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
67
68 // LED names may have a hyphen and that would be an issue for
69 // dbus paths and hence need to convert them to underscores.
70 std::replace(name.begin(), name.end(), '-', '_');
71
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053072 // Unique bus name representing a single LED.
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093073 auto busName = std::string(BUSNAME) + '.' + name;
74 auto objPath = std::string(OBJPATH) + '/' + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053075
76 // Get a handle to system dbus.
77 auto bus = std::move(sdbusplus::bus::new_default());
78
79 // Add systemd object manager.
80 sdbusplus::server::manager::manager(bus, objPath.c_str());
81
82 // Create the Physical LED objects for directing actions.
83 // Need to save this else sdbusplus destructor will wipe this off.
Vishwanatha Subbannae0891732017-03-10 15:27:23 +053084 phosphor::led::Physical led(bus, objPath, path);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053085
86 /** @brief Claim the bus */
87 bus.request_name(busName.c_str());
88
89 /** @brief Wait for client requests */
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093090 while (true)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053091 {
92 // Handle dbus message / signals discarding unhandled
93 bus.process_discard();
94 bus.wait();
95 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053096 return 0;
97}