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