Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright © 2017 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 | #include <iostream> |
| 17 | #include <phosphor-logging/log.hpp> |
| 18 | #include <systemd/sd-daemon.h> |
| 19 | #include "argument.hpp" |
| 20 | #include "event.hpp" |
| 21 | |
| 22 | using namespace witherspoon::power; |
| 23 | using namespace phosphor::logging; |
| 24 | |
| 25 | int main(int argc, char* argv[]) |
| 26 | { |
| 27 | auto rc = -1; |
| 28 | auto options = ArgumentParser(argc, argv); |
| 29 | |
| 30 | auto objpath = (options)["path"]; |
| 31 | if (argc < 2) |
| 32 | { |
| 33 | std::cerr << std::endl << "Too few arguments" << std::endl; |
| 34 | options.usage(argv); |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | if (objpath == ArgumentParser::emptyString) |
| 39 | { |
| 40 | log<level::ERR>("Device monitoring path argument required"); |
| 41 | return -2; |
| 42 | } |
| 43 | |
| 44 | auto bus = sdbusplus::bus::new_default(); |
| 45 | sd_event* events = nullptr; |
| 46 | |
| 47 | auto r = sd_event_default(&events); |
| 48 | if (r < 0) |
| 49 | { |
| 50 | log<level::ERR>("Failed call to sd_event_default()", |
| 51 | entry("ERROR=%s", strerror(-r))); |
| 52 | return -3; |
| 53 | } |
| 54 | |
| 55 | witherspoon::power::event::Event eventPtr{events}; |
| 56 | |
| 57 | //Attach the event object to the bus object so we can |
| 58 | //handle both sd_events (for the timers) and dbus signals. |
| 59 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 60 | |
| 61 | r = sd_event_loop(eventPtr.get()); |
| 62 | if (r < 0) |
| 63 | { |
| 64 | log<level::ERR>("Failed call to sd_event_loop", |
| 65 | entry("ERROR=%s", strerror(-r))); |
| 66 | } |
| 67 | |
| 68 | rc = 0; |
| 69 | |
| 70 | return rc; |
| 71 | } |