Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame^] | 1 | #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 Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 7 | #include "gpio_presence.hpp" |
| 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace gpio |
| 12 | { |
| 13 | namespace presence |
| 14 | { |
| 15 | |
Gunnar Mills | 5f10110 | 2017-06-29 13:07:39 -0500 | [diff] [blame^] | 16 | using namespace phosphor::logging; |
| 17 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 18 | |
| 19 | // Populate the file descriptor for passed in device |
| 20 | int 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 |
| 36 | void 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 | |
| 57 | void 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 Mills | 7263915 | 2017-06-22 15:06:21 -0500 | [diff] [blame] | 71 | } // namespace presence |
| 72 | } // namespace gpio |
| 73 | } // namespace phosphor |
| 74 | |