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