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 "argument.hpp"
17 #include "fan.hpp"
18 #include "fan_defs.hpp"
19 #include "trust_manager.hpp"
20 
21 #include <phosphor-logging/log.hpp>
22 #include <sdbusplus/bus.hpp>
23 #include <sdeventplus/event.hpp>
24 
25 using namespace phosphor::fan::monitor;
26 using namespace phosphor::logging;
27 
28 int main(int argc, char* argv[])
29 {
30     auto event = sdeventplus::Event::get_default();
31     auto bus = sdbusplus::bus::new_default();
32     std::vector<std::unique_ptr<Fan>> fans;
33     phosphor::fan::util::ArgumentParser args(argc, argv);
34 
35     if (argc != 2)
36     {
37         args.usage(argv);
38         return 1;
39     }
40 
41     Mode mode;
42     if (args["init"] == "true")
43     {
44         mode = Mode::init;
45     }
46     else if (args["monitor"] == "true")
47     {
48         mode = Mode::monitor;
49     }
50     else
51     {
52         args.usage(argv);
53         return 1;
54     }
55 
56     std::unique_ptr<phosphor::fan::trust::Manager> trust =
57         std::make_unique<phosphor::fan::trust::Manager>(trustGroups);
58 
59     // Attach the event object to the bus object so we can
60     // handle both sd_events (for the timers) and dbus signals.
61     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
62 
63     for (const auto& fanDef : fanDefinitions)
64     {
65         // Check if a condition exists on the fan
66         auto condition = std::get<conditionField>(fanDef);
67         if (condition)
68         {
69             // Condition exists, skip adding fan if it fails
70             if (!(*condition)(bus))
71             {
72                 continue;
73             }
74         }
75         fans.emplace_back(
76             std::make_unique<Fan>(mode, bus, event, trust, fanDef));
77     }
78 
79     if (mode == Mode::init)
80     {
81         // Fans were initialized to be functional, exit
82         return 0;
83     }
84 
85     return event.loop();
86 }
87