blob: 47d2dc786c7bcd8722dda367e161a5b856aa9e37 [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
Gunnar Mills80292bb2017-07-05 16:34:51 -050030 using Property = std::string;
31 using Value = sdbusplus::message::variant<bool, std::string>;
32 // Association between property and its value
33 using PropertyMap = std::map<Property, Value>;
34 using Interface = std::string;
35 // Association between interface and the D-Bus property
36 using InterfaceMap = std::map<Interface, PropertyMap>;
37 using Object = sdbusplus::message::object_path;
38 // Association between object and the interface
39 using ObjectMap = std::map<Object, InterfaceMap>;
40
Gunnar Mills5f101102017-06-29 13:07:39 -050041 public:
42 Presence() = delete;
43 ~Presence() = default;
44 Presence(const Presence&) = delete;
45 Presence& operator=(const Presence&) = delete;
46 Presence(Presence&&) = delete;
47 Presence& operator=(Presence&&) = delete;
48
49 /** @brief Constructs Presence object.
50 *
Gunnar Mills80292bb2017-07-05 16:34:51 -050051 * @param[in] bus - D-Bus bus Object
Gunnar Mills5f101102017-06-29 13:07:39 -050052 * @param[in] inventory - Object path under inventory
53 to display this inventory item
54 * @param[in] path - Device path to read for GPIO pin state
55 to determine presence of inventory item
56 * @param[in] key - GPIO key to monitor
57 * @param[in] name - Pretty name of the inventory item
58 */
Gunnar Mills80292bb2017-07-05 16:34:51 -050059 Presence(sdbusplus::bus::bus& bus,
60 const std::string& inventory,
Gunnar Mills5f101102017-06-29 13:07:39 -050061 const std::string& path,
62 const unsigned int key,
63 const std::string& name) :
Gunnar Mills80292bb2017-07-05 16:34:51 -050064 bus(bus),
Gunnar Mills5f101102017-06-29 13:07:39 -050065 inventory(inventory),
66 path(path),
67 key(key),
68 name(name),
69 fd(openDevice())
70 {
71 initEvDev();
72 determinePresence();
73 }
74
75 private:
76 /**
Gunnar Mills80292bb2017-07-05 16:34:51 -050077 * @brief Update the present property for the inventory item.
78 *
79 * @param[in] present - What the present property should be set to.
80 */
81 void updateInventory(bool present);
82
83 /**
84 * @brief Construct the inventory object map for the inventory item.
85 *
86 * @param[in] present - What the present property should be set to.
87 *
88 * @return The inventory object map to update inventory
89 */
90 ObjectMap getObjectMap(bool present);
91
92 /** @brief Connection for sdbusplus bus */
93 sdbusplus::bus::bus& bus;
94
95 /**
Gunnar Mills5f101102017-06-29 13:07:39 -050096 * @brief Read the GPIO device to determine initial presence and set
97 * present property at D-Bus path.
98 **/
99 void determinePresence();
100
101 /** @brief Object path under inventory to display this inventory item */
102 const std::string inventory;
103
104 /** @brief Device path to read for GPIO pin state
105 to determine presence of inventory item */
106 const std::string path;
107
108 /** @brief GPIO key to monitor */
109 const unsigned int key;
110
111 /** @brief Pretty name of the inventory item*/
112 const std::string name;
113
114 /** @brief Event structure */
115 EvdevPtr devicePtr;
116
117 /** @brief Opens the device and populates the descriptor */
118 int openDevice();
119
120 /** @brief File descriptor manager */
121 FileDescriptor fd;
122
123 /** @brief Initializes evdev handle with the fd */
124 void initEvDev();
125};
126
Gunnar Mills80292bb2017-07-05 16:34:51 -0500127/**
128 * @brief Get the service name from the mapper for the
129 * interface and path passed in.
130 *
131 * @param[in] path - The D-Bus path name
132 * @param[in] interface - The D-Bus interface name
133 * @param[in] bus - The D-Bus bus object
134 *
135 * @return The service name
136 */
137std::string getService(const std::string& path,
138 const std::string& interface,
139 sdbusplus::bus::bus& bus);
140
Gunnar Mills72639152017-06-22 15:06:21 -0500141} // namespace presence
142} // namespace gpio
143} // namespace phosphor
144