xref: /openbmc/phosphor-buttons/src/main.cpp (revision f654267d)
1 /*
2 // Copyright (c) 2018 Intel 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 
17 #include "id_button.hpp"
18 #include "power_button.hpp"
19 #include "reset_button.hpp"
20 
21 int main(int argc, char* argv[])
22 {
23     int ret = 0;
24 
25     phosphor::logging::log<phosphor::logging::level::INFO>(
26         "Start power button service...");
27 
28     sd_event* event = nullptr;
29     ret = sd_event_default(&event);
30     if (ret < 0)
31     {
32         phosphor::logging::log<phosphor::logging::level::ERR>(
33             "Error creating a default sd_event handler");
34         return ret;
35     }
36     EventPtr eventP{event};
37     event = nullptr;
38 
39     sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
40     sdbusplus::server::manager::manager objManager{
41         bus, "/xyz/openbmc_project/Chassis/Buttons"};
42 
43     bus.request_name("xyz.openbmc_project.Chassis.Buttons");
44 
45     std::unique_ptr<PowerButton> pb;
46     if (hasGpio<PowerButton>())
47     {
48         pb = std::make_unique<PowerButton>(bus, POWER_DBUS_OBJECT_NAME, eventP);
49     }
50 
51     std::unique_ptr<ResetButton> rb;
52     if (hasGpio<ResetButton>())
53     {
54         rb = std::make_unique<ResetButton>(bus, RESET_DBUS_OBJECT_NAME, eventP);
55     }
56 
57     std::unique_ptr<IDButton> ib;
58     if (hasGpio<IDButton>())
59     {
60         ib = std::make_unique<IDButton>(bus, ID_DBUS_OBJECT_NAME, eventP);
61     }
62 
63     try
64     {
65         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
66         ret = sd_event_loop(eventP.get());
67         if (ret < 0)
68         {
69             phosphor::logging::log<phosphor::logging::level::ERR>(
70                 "Error occurred during the sd_event_loop",
71                 phosphor::logging::entry("RET=%d", ret));
72         }
73     }
74     catch (std::exception& e)
75     {
76         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
77         ret = -1;
78     }
79     return ret;
80 }
81