blob: 3faa7e1dcd5a23636f58ef42f0771a1c71bd08ea [file] [log] [blame]
Gunnar Mills72639152017-06-22 15:06:21 -05001#pragma once
Gunnar Mills5f101102017-06-29 13:07:39 -05002#include <string>
3#include <libevdev/libevdev.h>
4#include "file.hpp"
Gunnar Mills72639152017-06-22 15:06:21 -05005
6namespace phosphor
7{
8namespace gpio
9{
10namespace presence
11{
12
Gunnar Mills5f101102017-06-29 13:07:39 -050013/* Need a custom deleter for freeing up evdev struct */
14struct FreeEvDev
15{
16 void operator()(struct libevdev* device) const
17 {
18 libevdev_free(device);
19 }
20};
21using EvdevPtr = std::unique_ptr<struct libevdev, FreeEvDev>;
22
23/** @class Presence
24 * @brief Responsible for determining and monitoring presence of
25 * inventory items and updating D-Bus accordingly.
26 */
27class Presence
28{
29
30 public:
31 Presence() = delete;
32 ~Presence() = default;
33 Presence(const Presence&) = delete;
34 Presence& operator=(const Presence&) = delete;
35 Presence(Presence&&) = delete;
36 Presence& operator=(Presence&&) = delete;
37
38 /** @brief Constructs Presence object.
39 *
40 * @param[in] inventory - Object path under inventory
41 to display this inventory item
42 * @param[in] path - Device path to read for GPIO pin state
43 to determine presence of inventory item
44 * @param[in] key - GPIO key to monitor
45 * @param[in] name - Pretty name of the inventory item
46 */
47 Presence(const std::string& inventory,
48 const std::string& path,
49 const unsigned int key,
50 const std::string& name) :
51 inventory(inventory),
52 path(path),
53 key(key),
54 name(name),
55 fd(openDevice())
56 {
57 initEvDev();
58 determinePresence();
59 }
60
61 private:
62 /**
63 * @brief Read the GPIO device to determine initial presence and set
64 * present property at D-Bus path.
65 **/
66 void determinePresence();
67
68 /** @brief Object path under inventory to display this inventory item */
69 const std::string inventory;
70
71 /** @brief Device path to read for GPIO pin state
72 to determine presence of inventory item */
73 const std::string path;
74
75 /** @brief GPIO key to monitor */
76 const unsigned int key;
77
78 /** @brief Pretty name of the inventory item*/
79 const std::string name;
80
81 /** @brief Event structure */
82 EvdevPtr devicePtr;
83
84 /** @brief Opens the device and populates the descriptor */
85 int openDevice();
86
87 /** @brief File descriptor manager */
88 FileDescriptor fd;
89
90 /** @brief Initializes evdev handle with the fd */
91 void initEvDev();
92};
93
Gunnar Mills72639152017-06-22 15:06:21 -050094} // namespace presence
95} // namespace gpio
96} // namespace phosphor
97