blob: d5728da5b1d69245fdbce29c8843cf225c32cd0c [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 Subbanna75b55102016-11-30 14:20:53 +053044 // Extract the name of LED from path.
45 auto index = path.rfind("/");
46 if (index == std::string::npos)
47 {
48 throw std::runtime_error("No Led in " + path);
49 }
50
51 // Remove the leading "/"
52 auto name = path.substr(index + 1);
53
Vishwanatha Subbanna413fd342017-03-30 17:25:18 +053054 // Convert to lowercase just in case some are not and that
55 // we follow lowercase all over
56 std::transform(name.begin(), name.end(), name.begin(), ::tolower);
57
58 // LED names may have a hyphen and that would be an issue for
59 // dbus paths and hence need to convert them to underscores.
60 std::replace(name.begin(), name.end(), '-', '_');
61
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053062 // Unique bus name representing a single LED.
63 auto busName = std::string(BUSNAME) + '.' + name;
64 auto objPath = std::string(OBJPATH) + '/' + name;
65
66 // Get a handle to system dbus.
67 auto bus = std::move(sdbusplus::bus::new_default());
68
69 // Add systemd object manager.
70 sdbusplus::server::manager::manager(bus, objPath.c_str());
71
72 // Create the Physical LED objects for directing actions.
73 // Need to save this else sdbusplus destructor will wipe this off.
Vishwanatha Subbannae0891732017-03-10 15:27:23 +053074 phosphor::led::Physical led(bus, objPath, path);
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053075
76 /** @brief Claim the bus */
77 bus.request_name(busName.c_str());
78
79 /** @brief Wait for client requests */
80 while(true)
81 {
82 // Handle dbus message / signals discarding unhandled
83 bus.process_discard();
84 bus.wait();
85 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053086 return 0;
87}