blob: 3ec1cac932edae73d621e991bebff77357d13d07 [file] [log] [blame]
Patrick Venturedace6802018-11-01 16:52:10 -07001#include "evdev.hpp"
2
3#include "xyz/openbmc_project/Common/error.hpp"
4
Gunnar Mills835dfb82017-07-26 16:15:21 -05005#include <fcntl.h>
Patrick Venturedace6802018-11-01 16:52:10 -07006#include <libevdev/libevdev.h>
7
8#include <phosphor-logging/elog-errors.hpp>
Gunnar Mills835dfb82017-07-26 16:15:21 -05009#include <phosphor-logging/elog.hpp>
George Liu2a8848c2023-08-01 13:49:28 +080010#include <phosphor-logging/lg2.hpp>
Gunnar Mills835dfb82017-07-26 16:15:21 -050011
12namespace phosphor
13{
14namespace gpio
15{
16
17using namespace phosphor::logging;
18using namespace sdbusplus::xyz::openbmc_project::Common::Error;
19
20// Populate the file descriptor for passed in device
21int Evdev::openDevice()
22{
Gunnar Mills835dfb82017-07-26 16:15:21 -050023 auto fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
24 if (fd < 0)
25 {
George Liu2a8848c2023-08-01 13:49:28 +080026 lg2::error("Failed to open {DEVICEPATH}: {ERRNO}", "DEVICEPATH", path,
27 "ERRNO", errno);
Gunnar Mills835dfb82017-07-26 16:15:21 -050028 elog<InternalFailure>();
29 }
30 return fd;
31}
32
33// Initializes the event device with the fd
34void Evdev::initEvDev()
35{
36 if (devicePtr)
37 {
38 // Init can be done only once per device
39 return;
40 }
41
42 struct libevdev* evdev = nullptr;
43 auto rc = libevdev_new_from_fd((fd)(), &evdev);
44 if (rc < 0)
45 {
George Liu2a8848c2023-08-01 13:49:28 +080046 lg2::error("Failed to initialize evdev");
Gunnar Mills835dfb82017-07-26 16:15:21 -050047 elog<InternalFailure>();
48 return;
49 }
50
51 // Packing in the unique_ptr
52 devicePtr.reset(evdev);
53}
54
55// Attaches the FD to event loop and registers the callback handler
56void Evdev::registerCallback()
57{
58 decltype(eventSource.get()) sourcePtr = nullptr;
Patrick Venturedace6802018-11-01 16:52:10 -070059 auto rc = sd_event_add_io(event.get(), &sourcePtr, (fd)(), EPOLLIN,
60 callbackHandler, this);
Gunnar Mills835dfb82017-07-26 16:15:21 -050061 eventSource.reset(sourcePtr);
62
63 if (rc < 0)
64 {
George Liu2a8848c2023-08-01 13:49:28 +080065 lg2::error("Failed to register callback handler: {RC}", "RC", rc);
Gunnar Mills835dfb82017-07-26 16:15:21 -050066 elog<InternalFailure>();
67 }
68}
69
70} // namespace gpio
71} // namespace phosphor