xref: /openbmc/dbus-sensors/src/psu/PSUEvent.hpp (revision d7be555e)
1 /*
2 // Copyright (c) 2019 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 
17 #pragma once
18 
19 #include "Utils.hpp"
20 
21 #include <boost/asio/io_context.hpp>
22 #include <boost/asio/random_access_file.hpp>
23 #include <boost/asio/steady_timer.hpp>
24 #include <boost/container/flat_map.hpp>
25 #include <sdbusplus/asio/object_server.hpp>
26 
27 #include <array>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <vector>
32 
33 using EventPathList =
34     boost::container::flat_map<std::string, std::vector<std::string>>;
35 using GroupEventPathList =
36     boost::container::flat_map<std::string, EventPathList>;
37 
38 class PSUSubEvent : public std::enable_shared_from_this<PSUSubEvent>
39 {
40   public:
41     PSUSubEvent(std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
42                 const std::string& path,
43                 std::shared_ptr<sdbusplus::asio::connection>& conn,
44                 boost::asio::io_context& io, const PowerState& powerState,
45                 const std::string& groupEventName, const std::string& eventName,
46                 std::shared_ptr<std::set<std::string>> asserts,
47                 std::shared_ptr<std::set<std::string>> combineEvent,
48                 std::shared_ptr<bool> state, const std::string& psuName,
49                 double pollRate);
50     ~PSUSubEvent();
51 
52     std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
53     std::shared_ptr<std::set<std::string>> asserts;
54     std::shared_ptr<std::set<std::string>> combineEvent;
55     std::shared_ptr<bool> assertState;
56     void setupRead();
57 
58   private:
59     int value = 0;
60     size_t errCount{0};
61     std::string path;
62     std::string eventName;
63 
64     PowerState readState;
65     boost::asio::steady_timer waitTimer;
66     std::shared_ptr<std::array<char, 128>> buffer;
67     void restartRead();
68     void handleResponse(const boost::system::error_code& err,
69                         size_t bytesTransferred);
70     void updateValue(const int& newValue);
71     boost::asio::random_access_file inputDev;
72     std::string psuName;
73     std::string groupEventName;
74     std::string fanName;
75     std::string assertMessage;
76     std::string deassertMessage;
77     std::shared_ptr<sdbusplus::asio::connection> systemBus;
78     unsigned int eventPollMs = defaultEventPollMs;
79     static constexpr unsigned int defaultEventPollMs = 1000;
80     static constexpr size_t warnAfterErrorCount = 10;
81 };
82 
83 class PSUCombineEvent
84 {
85   public:
86     PSUCombineEvent(sdbusplus::asio::object_server& objectServer,
87                     std::shared_ptr<sdbusplus::asio::connection>& conn,
88                     boost::asio::io_context& io, const std::string& psuName,
89                     const PowerState& powerState, EventPathList& eventPathList,
90                     GroupEventPathList& groupEventPathList,
91                     const std::string& combineEventName, double pollRate);
92     ~PSUCombineEvent();
93 
94     sdbusplus::asio::object_server& objServer;
95     std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
96     boost::container::flat_map<std::string,
97                                std::vector<std::shared_ptr<PSUSubEvent>>>
98         events;
99     std::vector<std::shared_ptr<std::set<std::string>>> asserts;
100     std::vector<std::shared_ptr<bool>> states;
101 };
102