Matt Spinler | e73446e | 2017-04-10 13:55:52 -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 <sdbusplus/bus.hpp> |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 17 | #include <phosphor-logging/log.hpp> |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 18 | #include "argument.hpp" |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 19 | #include "manager.hpp" |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 20 | #include "event.hpp" |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame^] | 21 | #include "sdbusplus.hpp" |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 22 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 23 | using namespace phosphor::fan::control; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 24 | using namespace phosphor::logging; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 25 | |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 26 | int main(int argc, char* argv[]) |
| 27 | { |
| 28 | auto bus = sdbusplus::bus::new_default(); |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 29 | sd_event* events = nullptr; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 30 | phosphor::fan::util::ArgumentParser args(argc, argv); |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 31 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 32 | if (argc != 2) |
| 33 | { |
| 34 | args.usage(argv); |
| 35 | exit(-1); |
| 36 | } |
| 37 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 38 | Mode mode; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 39 | |
| 40 | if (args["init"] == "true") |
| 41 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 42 | mode = Mode::init; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 43 | } |
| 44 | else if (args["control"] == "true") |
| 45 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 46 | mode = Mode::control; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 47 | } |
| 48 | else |
| 49 | { |
| 50 | args.usage(argv); |
| 51 | exit(-1); |
| 52 | } |
| 53 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 54 | auto r = sd_event_default(&events); |
| 55 | if (r < 0) |
| 56 | { |
| 57 | log<level::ERR>("Failed call to sd_event_default()", |
| 58 | entry("ERROR=%s", strerror(-r))); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | phosphor::fan::event::EventPtr eventPtr{events}; |
| 63 | |
| 64 | //Attach the event object to the bus object so we can |
| 65 | //handle both sd_events (for the timers) and dbus signals. |
| 66 | bus.attach_event(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 67 | |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame^] | 68 | try |
| 69 | { |
| 70 | Manager manager(bus, eventPtr, mode); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 71 | |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame^] | 72 | //Init mode will just set fans to max and delay |
| 73 | if (mode == Mode::init) |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 74 | { |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame^] | 75 | manager.doInit(); |
| 76 | return 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 77 | } |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame^] | 78 | else |
| 79 | { |
| 80 | r = sd_event_loop(eventPtr.get()); |
| 81 | if (r < 0) |
| 82 | { |
| 83 | log<level::ERR>("Failed call to sd_event_loop", |
| 84 | entry("ERROR=%s", strerror(-r))); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | //Log the useful metadata on these exceptions and let the app |
| 89 | //return -1 so it is restarted without a core dump. |
| 90 | catch (phosphor::fan::util::DBusServiceError& e) |
| 91 | { |
| 92 | log<level::ERR>("Uncaught DBus service lookup failure exception", |
| 93 | entry("PATH=%s", e.path.c_str()), |
| 94 | entry("INTERFACE=%s", e.interface.c_str())); |
| 95 | } |
| 96 | catch (phosphor::fan::util::DBusMethodError& e) |
| 97 | { |
| 98 | log<level::ERR>("Uncaught DBus method failure exception", |
| 99 | entry("BUSNAME=%s", e.busName.c_str()), |
| 100 | entry("PATH=%s", e.path.c_str()), |
| 101 | entry("INTERFACE=%s", e.interface.c_str()), |
| 102 | entry("METHOD=%s", e.method.c_str())); |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 105 | return -1; |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 106 | } |