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