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 | 413fd34 | 2017-03-30 17:25:18 +0530 | [diff] [blame] | 19 | #include <algorithm> |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 20 | #include "argument.hpp" |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 21 | #include "physical.hpp" |
| 22 | #include "config.h" |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 23 | |
| 24 | static 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 | |
| 32 | int main(int argc, char** argv) |
| 33 | { |
| 34 | // Read arguments. |
| 35 | auto options = phosphor::led::ArgumentParser(argc, argv); |
| 36 | |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 37 | // Parse out Path argument. |
Vishwanatha Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 38 | auto path = std::move((options)["path"]); |
| 39 | if (path == phosphor::led::ArgumentParser::empty_string) |
| 40 | { |
| 41 | ExitWithError("Path not specified.", argv); |
| 42 | } |
| 43 | |
Vishwanatha Subbanna | f9de54b | 2017-05-25 21:06:33 +0530 | [diff] [blame] | 44 | // 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 Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 48 | |
Vishwanatha Subbanna | f9de54b | 2017-05-25 21:06:33 +0530 | [diff] [blame] | 49 | // 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 Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 61 | |
Vishwanatha Subbanna | 413fd34 | 2017-03-30 17:25:18 +0530 | [diff] [blame] | 62 | // 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 Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 70 | // 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 Subbanna | e089173 | 2017-03-10 15:27:23 +0530 | [diff] [blame] | 82 | phosphor::led::Physical led(bus, objPath, path); |
Vishwanatha Subbanna | 75b5510 | 2016-11-30 14:20:53 +0530 | [diff] [blame] | 83 | |
| 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 Subbanna | 835571e | 2016-11-30 11:29:30 +0530 | [diff] [blame] | 94 | return 0; |
| 95 | } |