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>
17 #include <phosphor-logging/log.hpp>
18 #include "argument.hpp"
19 #include "manager.hpp"
20 #include "event.hpp"
21 
22 using namespace phosphor::fan::control;
23 using namespace phosphor::logging;
24 
25 int main(int argc, char* argv[])
26 {
27     auto bus = sdbusplus::bus::new_default();
28     sd_event* events = nullptr;
29     phosphor::fan::util::ArgumentParser args(argc, argv);
30 
31     if (argc != 2)
32     {
33         args.usage(argv);
34         exit(-1);
35     }
36 
37     Mode mode;
38 
39     if (args["init"] == "true")
40     {
41         mode = Mode::init;
42     }
43     else if (args["control"] == "true")
44     {
45         mode = Mode::control;
46     }
47     else
48     {
49         args.usage(argv);
50         exit(-1);
51     }
52 
53     auto r = sd_event_default(&events);
54     if (r < 0)
55     {
56         log<level::ERR>("Failed call to sd_event_default()",
57                         entry("ERROR=%s", strerror(-r)));
58         return -1;
59     }
60 
61     phosphor::fan::event::EventPtr eventPtr{events};
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(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
66 
67     Manager manager(bus, eventPtr, mode);
68 
69     //Init mode will just set fans to max and delay
70     if (mode == Mode::init)
71     {
72         manager.doInit();
73         return 0;
74     }
75     else
76     {
77         r = sd_event_loop(eventPtr.get());
78         if (r < 0)
79         {
80             log<level::ERR>("Failed call to sd_event_loop",
81                             entry("ERROR=%s", strerror(-r)));
82         }
83     }
84 
85     return -1;
86 }
87