xref: /openbmc/dbus-sensors/src/psu/PSUEvent.hpp (revision 18b6186e531ae37dd22b634c6530f793528473f4)
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/connection.hpp>
26 #include <sdbusplus/asio/object_server.hpp>
27 
28 #include <array>
29 #include <cstddef>
30 #include <memory>
31 #include <set>
32 #include <string>
33 #include <vector>
34 
35 using EventPathList =
36     boost::container::flat_map<std::string, std::vector<std::string>>;
37 using GroupEventPathList =
38     boost::container::flat_map<std::string, EventPathList>;
39 
40 class PSUSubEvent : public std::enable_shared_from_this<PSUSubEvent>
41 {
42   public:
43     PSUSubEvent(std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
44                 const std::string& path,
45                 std::shared_ptr<sdbusplus::asio::connection>& conn,
46                 boost::asio::io_context& io, const PowerState& powerState,
47                 const std::string& groupEventName, const std::string& eventName,
48                 std::shared_ptr<std::set<std::string>> asserts,
49                 std::shared_ptr<std::set<std::string>> combineEvent,
50                 std::shared_ptr<bool> state, const std::string& psuName,
51                 double pollRate);
52     ~PSUSubEvent();
53 
54     std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
55     std::shared_ptr<std::set<std::string>> asserts;
56     std::shared_ptr<std::set<std::string>> combineEvent;
57     std::shared_ptr<bool> assertState;
58     void setupRead();
59 
60   private:
61     int value = 0;
62     size_t errCount{0};
63     std::string path;
64     std::string eventName;
65 
66     PowerState readState;
67     boost::asio::steady_timer waitTimer;
68     std::shared_ptr<std::array<char, 128>> buffer;
69     void restartRead();
70     void handleResponse(const boost::system::error_code& err,
71                         size_t bytesTransferred);
72     void updateValue(const int& newValue);
73     boost::asio::random_access_file inputDev;
74     std::string psuName;
75     std::string groupEventName;
76     std::string fanName;
77     std::string assertMessage;
78     std::string deassertMessage;
79     std::shared_ptr<sdbusplus::asio::connection> systemBus;
80     unsigned int eventPollMs = defaultEventPollMs;
81     static constexpr unsigned int defaultEventPollMs = 1000;
82     static constexpr size_t warnAfterErrorCount = 10;
83 };
84 
85 class PSUCombineEvent
86 {
87   public:
88     PSUCombineEvent(sdbusplus::asio::object_server& objectServer,
89                     std::shared_ptr<sdbusplus::asio::connection>& conn,
90                     boost::asio::io_context& io, const std::string& psuName,
91                     const PowerState& powerState, EventPathList& eventPathList,
92                     GroupEventPathList& groupEventPathList,
93                     const std::string& combineEventName, double pollRate);
94     ~PSUCombineEvent();
95 
96     sdbusplus::asio::object_server& objServer;
97     std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
98     boost::container::flat_map<std::string,
99                                std::vector<std::shared_ptr<PSUSubEvent>>>
100         events;
101     std::vector<std::shared_ptr<std::set<std::string>>> asserts;
102     std::vector<std::shared_ptr<bool>> states;
103 };
104