summaryrefslogtreecommitdiffstats
path: root/manager.hpp
blob: 1a75f8c52431df6e2e08bcaa7360d0c178559882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once

#include <map>
#include <set>
#include <sdbusplus/bus.hpp>
#include "ledlayout.hpp"
namespace phosphor
{
namespace led
{

/** @brief Physical LED dbus constructs */
constexpr auto PHY_LED_PATH = "/xyz/openbmc_project/led/physical/";
constexpr auto PHY_LED_IFACE = "xyz.openbmc_project.Led.Physical";
constexpr auto DBUS_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";

/** @class Manager
 *  @brief Manages group of LEDs and applies action on the elements of group
 */
class Manager
{
    public:
        /** @brief Only need the default Manager */
        Manager() = delete;
        ~Manager() = default;
        Manager(const Manager&) = delete;
        Manager& operator=(const Manager&) = delete;
        Manager(Manager&&) = delete;
        Manager& operator=(Manager&&) = delete;

        /** @brief For finding intersection */
        static bool ledComp(const phosphor::led::Layout::LedAction& left,
                            const phosphor::led::Layout::LedAction& right)
        {
            return left.name < right.name;
        }

        using group = std::set<phosphor::led::Layout::LedAction>;
        using LedLayout = std::map<std::string, group>;

        /** @brief static global map constructed at compile time */
        const LedLayout& ledMap;

        /** @brief Refer the user supplied LED layout and sdbusplus handler
         *
         *  @param [in] bus       - sdbusplus handler
         *  @param [in] LedLayout - LEDs group layout
         */
        Manager(sdbusplus::bus::bus& bus,
                const LedLayout& ledLayout)
                : ledMap(ledLayout),
                  bus(bus)
        {
            // Nothing here
        }

        /** @brief Given a group name, applies the action on the group
         *
         *  @param[in]  path          -  dbus path of group
         *  @param[in]  assert        -  Could be true or false
         *  @param[in]  ledsAssert    -  LEDs that are to be asserted newly
         *  @param[in]  ledsDeAssert  -  LEDs that are to be Deasserted
         *  @param[in]  ledsUpdate    -  LEDs that need a transition between
         *                               different types of asserted states.
         *
         *  @return                   -  Success or exception thrown
         */
        bool setGroupState(const std::string& path, bool assert,
                           group& ledsAssert, group& ledsDeAssert,
                           group& ledsUpdate);

        /** @brief Finds the set of LEDs to operate on and executes action
         *
         *  @param[in]  ledsAssert    -  LEDs that are to be asserted newly
         *  @param[in]  ledsDeAssert  -  LEDs that are to be Deasserted
         *  @param[in]  ledsUpdate    -  LEDs that need a transition between
         *                               different types of asserted states.
         *
         *  @return: None
         */
        void driveLEDs(group& ledsAssert, group& ledsDeAssert,
                       group& ledsUpdate);

    private:
        /** @brief sdbusplus handler */
        sdbusplus::bus::bus& bus;

        /** Map of physical LED path to service name */
        std::map<std::string, std::string> phyLeds {};

        /** @brief Pointers to groups that are in asserted state */
        std::set<const group*> assertedGroups;

        /** @brief Contains the LEDs that are in asserted state */
        group currentState;

        /** @brief Returns action string based on enum
         *
         *  @param[in]  action - Action enum
         *
         *  @return string equivalent of the passed in enumeration
         */
        static std::string getPhysicalAction(Layout::Action action);

        /** @brief Chooses appropriate action to be triggered on physical LED
         *  and calls into function that applies the actual action.
         *
         *  @param[in]  objPath   -  dbus object path
         *  @param[in]  action    -  Intended action to be triggered
         *  @param[in]  dutyOn    -  Duty Cycle ON percentage
         */
        void drivePhysicalLED(const std::string& objPath,
                              Layout::Action action,
                              uint8_t dutyOn);

        /** @brief Makes a dbus call to a passed in service name.
         *  This is now the physical LED controller
         *
         *  @param[in]  service   -  dbus service name
         *  @param[in]  objPath   -  dbus object path
         *  @param[in]  property  -  property to be written to
         *  @param[in]  value     -  Value to write
         */
        template <typename T>
        void drivePhysicalLED(const std::string& service,
                              const std::string& objPath,
                              const std::string& property,
                              const T& value)
        {
            sdbusplus::message::variant<T> data = value;

            auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
                                              DBUS_PROPERTY_IFACE, "Set");
            method.append(PHY_LED_IFACE);
            method.append(property);
            method.append(data);

            // There will be exceptions going forward and hence don't need a
            // response
            bus.call_noreply(method);
            return;
        }

        /** @brief Populates map of Physical LED paths to service name */
        void populateObjectMap();
};

} // namespace led
} // namespace phosphor
OpenPOWER on IntegriCloud