xref: /openbmc/phosphor-fan-presence/control/main.cpp (revision f8ae7a5e6bc65fc41a29ae465cd7e4415c4eb9dd)
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 
26 #include <phosphor-logging/log.hpp>
27 #include <sdbusplus/bus.hpp>
28 #include <sdeventplus/event.hpp>
29 
30 using namespace phosphor::fan::control;
31 using namespace phosphor::logging;
32 
33 int main(int argc, char* argv[])
34 {
35     auto event = sdeventplus::Event::get_default();
36     auto bus = sdbusplus::bus::new_default();
37 
38 #ifndef CONTROL_USE_JSON
39     phosphor::fan::util::ArgumentParser args(argc, argv);
40     if (argc != 2)
41     {
42         args.usage(argv);
43         return 1;
44     }
45 
46     Mode mode;
47 
48     if (args["init"] == "true")
49     {
50         mode = Mode::init;
51     }
52     else if (args["control"] == "true")
53     {
54         mode = Mode::control;
55     }
56     else
57     {
58         args.usage(argv);
59         return 1;
60     }
61 #endif
62 
63     // Attach the event object to the bus object so we can
64     // handle both sd_events (for the timers) and dbus signals.
65     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
66 
67     try
68     {
69 #ifdef CONTROL_USE_JSON
70         json::Manager manager(bus, event);
71 #else
72         Manager manager(bus, event, mode);
73 
74         // Init mode will just set fans to max and delay
75         if (mode == Mode::init)
76         {
77             manager.doInit(event);
78             return 0;
79         }
80 #endif
81         return event.loop();
82     }
83     // Log the useful metadata on these exceptions and let the app
84     // return 1 so it is restarted without a core dump.
85     catch (phosphor::fan::util::DBusServiceError& e)
86     {
87         log<level::ERR>("Uncaught DBus service lookup failure exception",
88                         entry("PATH=%s", e.path.c_str()),
89                         entry("INTERFACE=%s", e.interface.c_str()));
90     }
91     catch (phosphor::fan::util::DBusMethodError& e)
92     {
93         log<level::ERR>("Uncaught DBus method failure exception",
94                         entry("BUSNAME=%s", e.busName.c_str()),
95                         entry("PATH=%s", e.path.c_str()),
96                         entry("INTERFACE=%s", e.interface.c_str()),
97                         entry("METHOD=%s", e.method.c_str()));
98     }
99     catch (phosphor::fan::util::DBusPropertyError& e)
100     {
101         log<level::ERR>("Uncaught DBus property access failure exception",
102                         entry("BUSNAME=%s", e.busName.c_str()),
103                         entry("PATH=%s", e.path.c_str()),
104                         entry("INTERFACE=%s", e.interface.c_str()),
105                         entry("PROPERTY=%s", e.property.c_str()));
106     }
107 
108     return 1;
109 }
110