blob: 986dbe69951a5d9ce8f69b185a83cdf54904fc73 [file] [log] [blame]
Gunnar Mills72639152017-06-22 15:06:21 -05001#pragma once
Gunnar Mills5f101102017-06-29 13:07:39 -05002#include <string>
Gunnar Mills765725e2017-07-06 14:17:44 -05003#include <systemd/sd-event.h>
Gunnar Mills835dfb82017-07-26 16:15:21 -05004#include "evdev.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/** @class Presence
Gunnar Mills835dfb82017-07-26 16:15:21 -050014 * @brief Responsible for determining and monitoring presence,
15 * by monitoring GPIO state changes, of inventory items and
16 * updating D-Bus accordingly.
Gunnar Mills5f101102017-06-29 13:07:39 -050017 */
Gunnar Mills835dfb82017-07-26 16:15:21 -050018class Presence : public Evdev
Gunnar Mills5f101102017-06-29 13:07:39 -050019{
20
Gunnar Mills80292bb2017-07-05 16:34:51 -050021 using Property = std::string;
22 using Value = sdbusplus::message::variant<bool, std::string>;
23 // Association between property and its value
24 using PropertyMap = std::map<Property, Value>;
25 using Interface = std::string;
26 // Association between interface and the D-Bus property
27 using InterfaceMap = std::map<Interface, PropertyMap>;
28 using Object = sdbusplus::message::object_path;
29 // Association between object and the interface
30 using ObjectMap = std::map<Object, InterfaceMap>;
31
Gunnar Mills5f101102017-06-29 13:07:39 -050032 public:
33 Presence() = delete;
34 ~Presence() = default;
35 Presence(const Presence&) = delete;
36 Presence& operator=(const Presence&) = delete;
37 Presence(Presence&&) = delete;
38 Presence& operator=(Presence&&) = delete;
39
40 /** @brief Constructs Presence object.
41 *
Gunnar Mills80292bb2017-07-05 16:34:51 -050042 * @param[in] bus - D-Bus bus Object
Gunnar Mills5f101102017-06-29 13:07:39 -050043 * @param[in] inventory - Object path under inventory
44 to display this inventory item
45 * @param[in] path - Device path to read for GPIO pin state
46 to determine presence of inventory item
47 * @param[in] key - GPIO key to monitor
48 * @param[in] name - Pretty name of the inventory item
Gunnar Mills765725e2017-07-06 14:17:44 -050049 * @param[in] event - sd_event handler
50 * @param[in] handler - IO callback handler. Defaults to one in this
51 * class
Gunnar Mills5f101102017-06-29 13:07:39 -050052 */
Gunnar Mills80292bb2017-07-05 16:34:51 -050053 Presence(sdbusplus::bus::bus& bus,
54 const std::string& inventory,
Gunnar Mills5f101102017-06-29 13:07:39 -050055 const std::string& path,
56 const unsigned int key,
Gunnar Mills765725e2017-07-06 14:17:44 -050057 const std::string& name,
58 EventPtr& event,
59 sd_event_io_handler_t handler = Presence::processEvents) :
Gunnar Mills835dfb82017-07-26 16:15:21 -050060 Evdev(path, key, event, handler, true),
Gunnar Mills80292bb2017-07-05 16:34:51 -050061 bus(bus),
Gunnar Mills5f101102017-06-29 13:07:39 -050062 inventory(inventory),
Gunnar Mills835dfb82017-07-26 16:15:21 -050063 name(name)
Gunnar Mills5f101102017-06-29 13:07:39 -050064 {
Gunnar Mills5f101102017-06-29 13:07:39 -050065 determinePresence();
66 }
67
Gunnar Mills765725e2017-07-06 14:17:44 -050068 /** @brief Callback handler when the FD has some activity on it
69 *
70 * @param[in] es - Populated event source
71 * @param[in] fd - Associated File descriptor
72 * @param[in] revents - Type of event
73 * @param[in] userData - User data that was passed during registration
74 *
75 * @return - 0 or positive number on success and negative
76 * errno otherwise
77 */
78 static int processEvents(sd_event_source* es, int fd,
79 uint32_t revents, void* userData);
80
Gunnar Mills5f101102017-06-29 13:07:39 -050081 private:
82 /**
Gunnar Mills80292bb2017-07-05 16:34:51 -050083 * @brief Update the present property for the inventory item.
84 *
85 * @param[in] present - What the present property should be set to.
86 */
87 void updateInventory(bool present);
88
89 /**
90 * @brief Construct the inventory object map for the inventory item.
91 *
92 * @param[in] present - What the present property should be set to.
93 *
94 * @return The inventory object map to update inventory
95 */
96 ObjectMap getObjectMap(bool present);
97
98 /** @brief Connection for sdbusplus bus */
99 sdbusplus::bus::bus& bus;
100
101 /**
Gunnar Mills5f101102017-06-29 13:07:39 -0500102 * @brief Read the GPIO device to determine initial presence and set
103 * present property at D-Bus path.
104 **/
105 void determinePresence();
106
107 /** @brief Object path under inventory to display this inventory item */
108 const std::string inventory;
109
Gunnar Mills5f101102017-06-29 13:07:39 -0500110 /** @brief Pretty name of the inventory item*/
111 const std::string name;
112
Gunnar Mills765725e2017-07-06 14:17:44 -0500113 /** @brief Analyzes the GPIO event and update present property*/
114 void analyzeEvent();
Gunnar Mills5f101102017-06-29 13:07:39 -0500115};
116
Gunnar Mills80292bb2017-07-05 16:34:51 -0500117/**
118 * @brief Get the service name from the mapper for the
119 * interface and path passed in.
120 *
121 * @param[in] path - The D-Bus path name
122 * @param[in] interface - The D-Bus interface name
123 * @param[in] bus - The D-Bus bus object
124 *
125 * @return The service name
126 */
127std::string getService(const std::string& path,
128 const std::string& interface,
129 sdbusplus::bus::bus& bus);
130
Gunnar Mills72639152017-06-22 15:06:21 -0500131} // namespace presence
132} // namespace gpio
133} // namespace phosphor
134