Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <map> |
| 4 | #include <set> |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 6 | #include "ledlayout.hpp" |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 7 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace led |
| 11 | { |
| 12 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 13 | /** @brief Physical LED dbus constructs */ |
| 14 | constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/"; |
| 15 | constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical"; |
| 16 | constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 17 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 18 | /** @class Manager |
| 19 | * @brief Manages group of LEDs and applies action on the elements of group |
| 20 | */ |
| 21 | class Manager |
| 22 | { |
| 23 | public: |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 24 | /** @brief Only need the default Manager */ |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 25 | Manager() = delete; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 26 | ~Manager() = default; |
| 27 | Manager(const Manager&) = delete; |
| 28 | Manager& operator=(const Manager&) = delete; |
| 29 | Manager(Manager&&) = delete; |
| 30 | Manager& operator=(Manager&&) = delete; |
| 31 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 32 | /** @brief Special comparator for finding set difference */ |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 33 | static bool ledComp(const phosphor::led::Layout::LedAction& left, |
| 34 | const phosphor::led::Layout::LedAction& right) |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 35 | { |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 36 | // Example : |
| 37 | // If FIRST_1 is {fan0, 1, 1} and FIRST_2 is {fan0, 2, 2}, |
| 38 | // with default priority of Blink, this comparator would return |
| 39 | // false. But considering the priority, this comparator would need |
| 40 | // to return true so that we consider appropriate set and in |
| 41 | // this case its {fan0, 1, 1} |
| 42 | if (left.name == right.name) |
| 43 | { |
| 44 | if (left.action == right.action) |
| 45 | { |
| 46 | return false; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | return true; |
| 51 | } |
| 52 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 53 | return left.name < right.name; |
| 54 | } |
| 55 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 56 | /** @brief Comparator for finding LEDs to be DeAsserted */ |
| 57 | static bool ledLess(const phosphor::led::Layout::LedAction& left, |
| 58 | const phosphor::led::Layout::LedAction& right) |
| 59 | { |
| 60 | return left.name < right.name; |
| 61 | } |
| 62 | |
| 63 | /** @brief Comparator for helping unique_copy */ |
| 64 | static bool ledEqual(const phosphor::led::Layout::LedAction& left, |
| 65 | const phosphor::led::Layout::LedAction& right) |
| 66 | { |
| 67 | return left.name == right.name; |
| 68 | } |
| 69 | |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 70 | using group = std::set<phosphor::led::Layout::LedAction>; |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 71 | using LedLayout = std::map<std::string, group>; |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 72 | |
| 73 | /** @brief static global map constructed at compile time */ |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 74 | const LedLayout& ledMap; |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 75 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 76 | /** @brief Refer the user supplied LED layout and sdbusplus handler |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 77 | * |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 78 | * @param [in] bus - sdbusplus handler |
| 79 | * @param [in] LedLayout - LEDs group layout |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 80 | */ |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 81 | Manager(sdbusplus::bus::bus& bus, |
| 82 | const LedLayout& ledLayout) |
| 83 | : ledMap(ledLayout), |
| 84 | bus(bus) |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 85 | { |
| 86 | // Nothing here |
| 87 | } |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 88 | |
| 89 | /** @brief Given a group name, applies the action on the group |
| 90 | * |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 91 | * @param[in] path - dbus path of group |
| 92 | * @param[in] assert - Could be true or false |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 93 | * @param[in] ledsAssert - LEDs that are to be asserted new |
| 94 | * or to a different state |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 95 | * @param[in] ledsDeAssert - LEDs that are to be Deasserted |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 96 | * |
| 97 | * @return - Success or exception thrown |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 98 | */ |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 99 | bool setGroupState(const std::string& path, bool assert, |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 100 | group& ledsAssert, group& ledsDeAssert); |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 101 | |
| 102 | /** @brief Finds the set of LEDs to operate on and executes action |
| 103 | * |
| 104 | * @param[in] ledsAssert - LEDs that are to be asserted newly |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 105 | * or to a different state |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 106 | * @param[in] ledsDeAssert - LEDs that are to be Deasserted |
Vishwanatha Subbanna | ed49073 | 2016-12-20 15:59:29 +0530 | [diff] [blame] | 107 | * |
| 108 | * @return: None |
| 109 | */ |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 110 | void driveLEDs(group& ledsAssert, group& ledsDeAssert); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 111 | |
| 112 | private: |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 113 | /** @brief sdbusplus handler */ |
| 114 | sdbusplus::bus::bus& bus; |
| 115 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 116 | /** Map of physical LED path to service name */ |
| 117 | std::map<std::string, std::string> phyLeds {}; |
| 118 | |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 119 | /** @brief Pointers to groups that are in asserted state */ |
| 120 | std::set<const group*> assertedGroups; |
| 121 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 122 | /** @brief Contains the highest priority actions for all |
| 123 | * asserted LEDs. |
| 124 | */ |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 125 | group currentState; |
| 126 | |
Vishwanatha Subbanna | 4b000d8 | 2017-05-03 18:44:16 +0530 | [diff] [blame] | 127 | /** @brief Contains the set of all actions for asserted LEDs */ |
| 128 | group combinedState; |
| 129 | |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 130 | /** @brief Returns action string based on enum |
| 131 | * |
| 132 | * @param[in] action - Action enum |
| 133 | * |
| 134 | * @return string equivalent of the passed in enumeration |
| 135 | */ |
Vishwanatha Subbanna | 4fa9248 | 2017-03-10 14:39:20 +0530 | [diff] [blame] | 136 | static std::string getPhysicalAction(Layout::Action action); |
Vishwanatha Subbanna | 11ca8f9 | 2017-02-27 19:33:45 +0530 | [diff] [blame] | 137 | |
| 138 | /** @brief Chooses appropriate action to be triggered on physical LED |
| 139 | * and calls into function that applies the actual action. |
| 140 | * |
| 141 | * @param[in] objPath - dbus object path |
| 142 | * @param[in] action - Intended action to be triggered |
| 143 | * @param[in] dutyOn - Duty Cycle ON percentage |
| 144 | */ |
| 145 | void drivePhysicalLED(const std::string& objPath, |
| 146 | Layout::Action action, |
| 147 | uint8_t dutyOn); |
| 148 | |
| 149 | /** @brief Makes a dbus call to a passed in service name. |
| 150 | * This is now the physical LED controller |
| 151 | * |
| 152 | * @param[in] service - dbus service name |
| 153 | * @param[in] objPath - dbus object path |
| 154 | * @param[in] property - property to be written to |
| 155 | * @param[in] value - Value to write |
| 156 | */ |
| 157 | template <typename T> |
| 158 | void drivePhysicalLED(const std::string& service, |
| 159 | const std::string& objPath, |
| 160 | const std::string& property, |
| 161 | const T& value) |
| 162 | { |
| 163 | sdbusplus::message::variant<T> data = value; |
| 164 | |
| 165 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 166 | DBUS_PROPERTY_IFACE, "Set"); |
| 167 | method.append(PHY_LED_IFACE); |
| 168 | method.append(property); |
| 169 | method.append(data); |
| 170 | |
| 171 | // There will be exceptions going forward and hence don't need a |
| 172 | // response |
| 173 | bus.call_noreply(method); |
| 174 | return; |
| 175 | } |
| 176 | |
Vishwanatha Subbanna | dcc3f38 | 2017-03-24 20:15:02 +0530 | [diff] [blame] | 177 | /** @brief Populates map of Physical LED paths to service name */ |
| 178 | void populateObjectMap(); |
Vishwanatha Subbanna | 4c8c72b | 2016-11-29 23:02:06 +0530 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | } // namespace led |
| 182 | } // namespace phosphor |