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 | */ |
Matthew Barth | f8ae7a5 | 2021-03-05 10:23:43 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
| 18 | #ifndef CONTROL_USE_JSON |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 19 | #include "argument.hpp" |
Matt Spinler | e10416e | 2017-04-10 14:15:53 -0500 | [diff] [blame] | 20 | #include "manager.hpp" |
Matthew Barth | f8ae7a5 | 2021-03-05 10:23:43 -0600 | [diff] [blame] | 21 | #else |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 22 | #include "json/manager.hpp" |
| 23 | #endif |
Matthew Barth | f8ae7a5 | 2021-03-05 10:23:43 -0600 | [diff] [blame] | 24 | #include "sdbusplus.hpp" |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 25 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
| 27 | #include <sdbusplus/bus.hpp> |
| 28 | #include <sdeventplus/event.hpp> |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 29 | #include <sdeventplus/source/signal.hpp> |
| 30 | #include <stdplus/signal.hpp> |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 31 | |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 32 | using namespace phosphor::fan::control; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 33 | using namespace phosphor::logging; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 34 | |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 35 | int main(int argc, char* argv[]) |
| 36 | { |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 37 | auto event = sdeventplus::Event::get_default(); |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 38 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 39 | #ifndef CONTROL_USE_JSON |
| 40 | phosphor::fan::util::ArgumentParser args(argc, argv); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 41 | if (argc != 2) |
| 42 | { |
| 43 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 44 | return 1; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 47 | Mode mode; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 48 | |
| 49 | if (args["init"] == "true") |
| 50 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 51 | mode = Mode::init; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 52 | } |
| 53 | else if (args["control"] == "true") |
| 54 | { |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 55 | mode = Mode::control; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 56 | } |
| 57 | else |
| 58 | { |
| 59 | args.usage(argv); |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 60 | return 1; |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 61 | } |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 62 | #endif |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 63 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 64 | // Attach the event object to the bus object so we can |
| 65 | // handle both sd_events (for the timers) and dbus signals. |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 66 | phosphor::fan::util::SDBusPlus::getBus().attach_event( |
| 67 | event.get(), SD_EVENT_PRIORITY_NORMAL); |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 68 | |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 69 | try |
| 70 | { |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 71 | #ifdef CONTROL_USE_JSON |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 72 | json::Manager manager(event); |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 73 | |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame^] | 74 | // Handle loading fan control's config file(s) |
| 75 | phosphor::fan::JsonConfig config( |
| 76 | std::bind(&json::Manager::load, &manager)); |
| 77 | |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 78 | // Enable SIGHUP handling to reload JSON configs |
| 79 | stdplus::signal::block(SIGHUP); |
| 80 | sdeventplus::source::Signal signal( |
| 81 | event, SIGHUP, |
| 82 | std::bind(&json::Manager::sighupHandler, &manager, |
| 83 | std::placeholders::_1, std::placeholders::_2)); |
| 84 | |
| 85 | phosphor::fan::util::SDBusPlus::getBus().request_name(CONTROL_BUSNAME); |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 86 | #else |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 87 | Manager manager(phosphor::fan::util::SDBusPlus::getBus(), event, mode); |
Matt Spinler | ee7f642 | 2017-05-09 11:03:14 -0500 | [diff] [blame] | 88 | |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 89 | // Init mode will just set fans to max and delay |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 90 | if (mode == Mode::init) |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 91 | { |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 92 | manager.doInit(event); |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 93 | return 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 94 | } |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 95 | #endif |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 96 | return event.loop(); |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 97 | } |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 98 | // Log the useful metadata on these exceptions and let the app |
| 99 | // return 1 so it is restarted without a core dump. |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 100 | catch (phosphor::fan::util::DBusServiceError& e) |
| 101 | { |
| 102 | log<level::ERR>("Uncaught DBus service lookup failure exception", |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 103 | entry("PATH=%s", e.path.c_str()), |
| 104 | entry("INTERFACE=%s", e.interface.c_str())); |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 105 | } |
| 106 | catch (phosphor::fan::util::DBusMethodError& e) |
| 107 | { |
| 108 | log<level::ERR>("Uncaught DBus method failure exception", |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 109 | entry("BUSNAME=%s", e.busName.c_str()), |
| 110 | entry("PATH=%s", e.path.c_str()), |
| 111 | entry("INTERFACE=%s", e.interface.c_str()), |
| 112 | entry("METHOD=%s", e.method.c_str())); |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 113 | } |
Matthew Barth | 88923a0 | 2018-05-11 10:14:44 -0500 | [diff] [blame] | 114 | catch (phosphor::fan::util::DBusPropertyError& e) |
| 115 | { |
| 116 | log<level::ERR>("Uncaught DBus property access failure exception", |
Matthew Barth | 3e1bb27 | 2020-05-26 11:09:21 -0500 | [diff] [blame] | 117 | entry("BUSNAME=%s", e.busName.c_str()), |
| 118 | entry("PATH=%s", e.path.c_str()), |
| 119 | entry("INTERFACE=%s", e.interface.c_str()), |
| 120 | entry("PROPERTY=%s", e.property.c_str())); |
Matthew Barth | 88923a0 | 2018-05-11 10:14:44 -0500 | [diff] [blame] | 121 | } |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 122 | |
William A. Kennington III | 3e78106 | 2018-10-19 17:18:34 -0700 | [diff] [blame] | 123 | return 1; |
Matt Spinler | e73446e | 2017-04-10 13:55:52 -0500 | [diff] [blame] | 124 | } |