blob: 82430a7894eaef70fb67bbd77306669dfabb19a2 [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
23#include <algorithm>
24#include <iostream>
25#include <string>
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053026
27static 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
35int main(int argc, char** argv)
36{
Andrew Jeffery42e02d32018-05-24 13:34:05 +093037 namespace fs = std::experimental::filesystem;
38
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053039 // Read arguments.
40 auto options = phosphor::led::ArgumentParser(argc, argv);
41
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053042 // Parse out Path argument.
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053043 auto path = std::move((options)["path"]);
44 if (path == phosphor::led::ArgumentParser::empty_string)
45 {
46 ExitWithError("Path not specified.", argv);
47 }
48
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +053049 // If the LED has a hyphen in the name like: "one-two", then it gets passed
50 // as /one/two/ as opposed to /one-two to the service file. There is a
51 // change needed in systemd to solve this issue and hence putting in this
52 // work-around.
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053053
Vishwanatha Subbannaf9de54b2017-05-25 21:06:33 +053054 // Since this application always gets invoked as part of a udev rule,
55 // it is always guaranteed to get /sys/class/leds/one/two
56 // and we can go beyond leds/ to get the actual led name.
57 // Refer: systemd/systemd#5072
58
59 // On an error, this throws an exception and terminates.
60 auto name = path.substr(strlen(DEVPATH));
61
62 // LED names may have a hyphen and that would be an issue for
63 // dbus paths and hence need to convert them to underscores.
64 std::replace(name.begin(), name.end(), '/', '-');
Andrew Jefferycd3b05e2018-05-24 13:05:30 +093065 path = DEVPATH + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053066
Vishwanatha Subbanna413fd342017-03-30 17:25:18 +053067 // Convert to lowercase just in case some are not and that
68 // we follow lowercase all over
69 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
70
71 // LED names may have a hyphen and that would be an issue for
72 // dbus paths and hence need to convert them to underscores.
73 std::replace(name.begin(), name.end(), '-', '_');
74
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053075 // Unique bus name representing a single LED.
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093076 auto busName = std::string(BUSNAME) + '.' + name;
77 auto objPath = std::string(OBJPATH) + '/' + name;
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053078
79 // Get a handle to system dbus.
Andrew Jefferycd3b05e2018-05-24 13:05:30 +093080 auto bus = sdbusplus::bus::new_default();
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053081
82 // Add systemd object manager.
83 sdbusplus::server::manager::manager(bus, objPath.c_str());
84
85 // Create the Physical LED objects for directing actions.
86 // Need to save this else sdbusplus destructor will wipe this off.
Andrew Jeffery42e02d32018-05-24 13:34:05 +093087 phosphor::led::SysfsLed sled{fs::path(path)};
88 phosphor::led::Physical led(bus, objPath, sled);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053089
90 /** @brief Claim the bus */
91 bus.request_name(busName.c_str());
92
93 /** @brief Wait for client requests */
Andrew Jefferyc41bf5b2018-05-25 16:39:22 +093094 while (true)
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053095 {
96 // Handle dbus message / signals discarding unhandled
97 bus.process_discard();
98 bus.wait();
99 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +0530100 return 0;
101}