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 #pragma once
18 #include "common.hpp"
19 #include "gpio.hpp"
20 #include "xyz/openbmc_project/Chassis/Buttons/ID/server.hpp"
21 #include "xyz/openbmc_project/Chassis/Common/error.hpp"
22 
23 #include <unistd.h>
24 
25 #include <phosphor-logging/elog-errors.hpp>
26 
27 const static constexpr char* ID_BUTTON = "ID_BTN";
28 
29 struct IDButton
30     : sdbusplus::server::object::object<
31           sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>
32 {
33 
34     IDButton(sdbusplus::bus::bus& bus, const char* path, EventPtr& event,
35              sd_event_io_handler_t handler = IDButton::EventHandler) :
36         sdbusplus::server::object::object<
37             sdbusplus::xyz::openbmc_project::Chassis::Buttons::server::ID>(
38             bus, path),
39         fd(-1), bus(bus), event(event), callbackHandler(handler)
40     {
41 
42         int ret = -1;
43 
44         // config gpio
45         ret = ::configGpio(ID_BUTTON, &fd, bus);
46         if (ret < 0)
47         {
48             phosphor::logging::log<phosphor::logging::level::ERR>(
49                 "ID_BUTTON: failed to config GPIO");
50             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
51                 IOError();
52         }
53 
54         ret = sd_event_add_io(event.get(), nullptr, fd, EPOLLPRI,
55                               callbackHandler, this);
56         if (ret < 0)
57         {
58             phosphor::logging::log<phosphor::logging::level::ERR>(
59                 "ID_BUTTON: failed to add to event loop");
60             ::closeGpio(fd);
61             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
62                 IOError();
63         }
64     }
65 
66     ~IDButton()
67     {
68         ::closeGpio(fd);
69     }
70 
71     void simPress() override;
72 
73     static const char* getGpioName()
74     {
75         return ID_BUTTON;
76     }
77 
78     static int EventHandler(sd_event_source* es, int fd, uint32_t revents,
79                             void* userdata)
80     {
81 
82         int n = -1;
83         char buf = '0';
84 
85         if (!userdata)
86         {
87             phosphor::logging::log<phosphor::logging::level::ERR>(
88                 "ID_BUTTON: userdata null!");
89             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
90                 IOError();
91         }
92 
93         IDButton* idButton = static_cast<IDButton*>(userdata);
94 
95         if (!idButton)
96         {
97             phosphor::logging::log<phosphor::logging::level::ERR>(
98                 "ID_BUTTON: null pointer!");
99             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
100                 IOError();
101         }
102 
103         n = ::lseek(fd, 0, SEEK_SET);
104 
105         if (n < 0)
106         {
107             phosphor::logging::log<phosphor::logging::level::ERR>(
108                 "ID_BUTTON: lseek error!");
109             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
110                 IOError();
111         }
112 
113         n = ::read(fd, &buf, sizeof(buf));
114         if (n < 0)
115         {
116             phosphor::logging::log<phosphor::logging::level::ERR>(
117                 "ID_BUTTON: read error!");
118             throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error::
119                 IOError();
120         }
121 
122         if (buf == '0')
123         {
124             phosphor::logging::log<phosphor::logging::level::DEBUG>(
125                 "ID_BUTTON: pressed");
126             // emit pressed signal
127             idButton->pressed();
128         }
129         else
130         {
131             phosphor::logging::log<phosphor::logging::level::DEBUG>(
132                 "ID_BUTTON: released");
133             // released
134             idButton->released();
135         }
136 
137         return 0;
138     }
139 
140   private:
141     int fd;
142     sdbusplus::bus::bus& bus;
143     EventPtr& event;
144     sd_event_io_handler_t callbackHandler;
145 };
146