Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 1 | /** |
| 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 Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 18 | #include <string> |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 19 | #include "argument.hpp" |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 20 | #include "physical.hpp" |
| 21 | #include "config.h" |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 22 | |
| 23 | static 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 | |
| 31 | int main(int argc, char** argv) |
| 32 | { |
| 33 | // Read arguments. |
| 34 | auto options = phosphor::led::ArgumentParser(argc, argv); |
| 35 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 36 | // Parse out Path argument. |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 37 | auto path = std::move((options)["path"]); |
| 38 | if (path == phosphor::led::ArgumentParser::empty_string) |
| 39 | { |
| 40 | ExitWithError("Path not specified.", argv); |
| 41 | } |
| 42 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 43 | // 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. |
Vishwanatha Subbanna | e089173 | 2017-03-10 15:27:23 +0530 | [diff] [blame^] | 65 | phosphor::led::Physical led(bus, objPath, path); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 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 Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 77 | return 0; |
| 78 | } |