blob: 710e4aeabb9d1b0224befd3898fe2e7555475bda [file] [log] [blame]
Gunnar Mills5f101102017-06-29 13:07:39 -05001#include <libevdev/libevdev.h>
2#include <fcntl.h>
3#include <phosphor-logging/elog.hpp>
4#include <phosphor-logging/log.hpp>
5#include <phosphor-logging/elog-errors.hpp>
6#include "xyz/openbmc_project/Common/error.hpp"
Gunnar Mills72639152017-06-22 15:06:21 -05007#include "gpio_presence.hpp"
8
9namespace phosphor
10{
11namespace gpio
12{
13namespace presence
14{
15
Gunnar Mills5f101102017-06-29 13:07:39 -050016using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
18
19// Populate the file descriptor for passed in device
20int Presence::openDevice()
21{
22 using namespace phosphor::logging;
23
24 auto fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
25 if (fd < 0)
26 {
27 log<level::ERR>("Failed to open device path",
28 entry("DEVICEPATH=%s", path.c_str()),
29 entry("ERRNO=%d", errno));
30 elog<InternalFailure>();
31 }
32 return fd;
33}
34
35// Initializes the event device with the fd
36void Presence::initEvDev()
37{
38 if (devicePtr)
39 {
40 // Init can be done only once per device
41 return;
42 }
43
44 struct libevdev* evdev = nullptr;
45 auto rc = libevdev_new_from_fd((fd)(), &evdev);
46 if (rc < 0)
47 {
48 log<level::ERR>("Failed to initialize evdev");
49 elog<InternalFailure>();
50 return;
51 }
52
53 // Packing in the unique_ptr
54 devicePtr.reset(evdev);
55}
56
57void Presence::determinePresence()
58{
59 auto value = static_cast<int>(0);
60 auto fetch_rc = libevdev_fetch_event_value(devicePtr.get(), EV_KEY,
61 key, &value);
62 if (0 == fetch_rc)
63 {
64 log<level::ERR>("Device does not support event type",
65 entry("KEYCODE=%d", key));
66 elog<InternalFailure>();
67 return;
68 }
69}
70
Gunnar Mills72639152017-06-22 15:06:21 -050071} // namespace presence
72} // namespace gpio
73} // namespace phosphor
74