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