blob: 656c9bfd47bddfb7a9525306f3f37b6e6572cb03 [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 Subbanna835571e2016-11-30 11:29:30 +053019#include "argument.hpp"
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053020#include "physical.hpp"
21#include "config.h"
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053022
23static void ExitWithError(const char* err, char** argv)
24{
25 phosphor::led::ArgumentParser::usage(argv);
26 std::cerr << std::endl;
27 std::cerr << "ERROR: " << err << std::endl;
28 exit(-1);
29}
30
31int main(int argc, char** argv)
32{
33 // Read arguments.
34 auto options = phosphor::led::ArgumentParser(argc, argv);
35
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053036 // Parse out Path argument.
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053037 auto path = std::move((options)["path"]);
38 if (path == phosphor::led::ArgumentParser::empty_string)
39 {
40 ExitWithError("Path not specified.", argv);
41 }
42
Vishwanatha Subbanna75b55102016-11-30 14:20:53 +053043 // Extract the name of LED from path.
44 auto index = path.rfind("/");
45 if (index == std::string::npos)
46 {
47 throw std::runtime_error("No Led in " + path);
48 }
49
50 // Remove the leading "/"
51 auto name = path.substr(index + 1);
52
53 // Unique bus name representing a single LED.
54 auto busName = std::string(BUSNAME) + '.' + name;
55 auto objPath = std::string(OBJPATH) + '/' + name;
56
57 // Get a handle to system dbus.
58 auto bus = std::move(sdbusplus::bus::new_default());
59
60 // Add systemd object manager.
61 sdbusplus::server::manager::manager(bus, objPath.c_str());
62
63 // Create the Physical LED objects for directing actions.
64 // Need to save this else sdbusplus destructor will wipe this off.
65 auto obj = phosphor::led::Physical(bus, objPath, path);
66
67 /** @brief Claim the bus */
68 bus.request_name(busName.c_str());
69
70 /** @brief Wait for client requests */
71 while(true)
72 {
73 // Handle dbus message / signals discarding unhandled
74 bus.process_discard();
75 bus.wait();
76 }
Vishwanatha Subbanna835571e2016-11-30 11:29:30 +053077 return 0;
78}