blob: e7b5c0bc41acc362d88e4e4964465c6a0395701d [file] [log] [blame]
Cheng C Yang58b2b532019-05-31 00:19:45 +08001/*
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
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000019#include <Utils.hpp>
James Feist8086aba2020-08-25 16:00:59 -070020#include <boost/asio/deadline_timer.hpp>
21#include <boost/asio/io_service.hpp>
22#include <boost/asio/streambuf.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -070023#include <boost/container/flat_map.hpp>
Cheng C Yang58b2b532019-05-31 00:19:45 +080024#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025
26#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070027#include <set>
28#include <string>
29#include <vector>
Cheng C Yang58b2b532019-05-31 00:19:45 +080030
Yong Libf8b1da2020-04-15 16:32:50 +080031class PSUSubEvent : public std::enable_shared_from_this<PSUSubEvent>
Cheng C Yang58b2b532019-05-31 00:19:45 +080032{
33 public:
34 PSUSubEvent(std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
Yong Li3046a022020-04-03 13:01:02 +080035 const std::string& path,
36 std::shared_ptr<sdbusplus::asio::connection>& conn,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000037 boost::asio::io_service& io, const PowerState& readState,
38 const std::string& groupEventName, const std::string& eventName,
Cheng C Yang58b2b532019-05-31 00:19:45 +080039 std::shared_ptr<std::set<std::string>> asserts,
40 std::shared_ptr<std::set<std::string>> combineEvent,
Lei YU7170a232021-02-04 16:19:27 +080041 std::shared_ptr<bool> state, const std::string& psuName,
42 double pollRate);
Cheng C Yang58b2b532019-05-31 00:19:45 +080043 ~PSUSubEvent();
44
45 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
46 std::shared_ptr<std::set<std::string>> asserts;
47 std::shared_ptr<std::set<std::string>> combineEvent;
48 std::shared_ptr<bool> assertState;
Yong Libf8b1da2020-04-15 16:32:50 +080049 void setupRead(void);
Cheng C Yang58b2b532019-05-31 00:19:45 +080050
51 private:
52 int value = 0;
Cheng C Yanga97f1342020-02-11 15:10:41 +080053 int fd;
Ed Tanousb429f312022-06-27 16:09:53 -070054 size_t errCount{0};
Cheng C Yang58b2b532019-05-31 00:19:45 +080055 std::string path;
56 std::string eventName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050057
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000058 PowerState readState;
Cheng C Yang58b2b532019-05-31 00:19:45 +080059 boost::asio::deadline_timer waitTimer;
Yong Libf8b1da2020-04-15 16:32:50 +080060 std::shared_ptr<boost::asio::streambuf> readBuf;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000061 void restartRead();
Cheng C Yang58b2b532019-05-31 00:19:45 +080062 void handleResponse(const boost::system::error_code& err);
63 void updateValue(const int& newValue);
64 boost::asio::posix::stream_descriptor inputDev;
Cheng C Yang5665db22019-07-12 00:57:30 +080065 std::string psuName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050066 std::string groupEventName;
Cheng C Yang5665db22019-07-12 00:57:30 +080067 std::string fanName;
Cheng C Yang9b53a622019-08-27 22:13:58 +080068 std::string assertMessage;
69 std::string deassertMessage;
Yong Li3046a022020-04-03 13:01:02 +080070 std::shared_ptr<sdbusplus::asio::connection> systemBus;
Lei YU7170a232021-02-04 16:19:27 +080071 unsigned int eventPollMs = defaultEventPollMs;
72 static constexpr unsigned int defaultEventPollMs = 1000;
73 static constexpr size_t warnAfterErrorCount = 10;
Cheng C Yang58b2b532019-05-31 00:19:45 +080074};
75
76class PSUCombineEvent
77{
78 public:
79 PSUCombineEvent(
80 sdbusplus::asio::object_server& objectSever,
Yong Li3046a022020-04-03 13:01:02 +080081 std::shared_ptr<sdbusplus::asio::connection>& conn,
Cheng C Yang58b2b532019-05-31 00:19:45 +080082 boost::asio::io_service& io, const std::string& psuName,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000083 const PowerState& powerState,
Cheng C Yang58b2b532019-05-31 00:19:45 +080084 boost::container::flat_map<std::string, std::vector<std::string>>&
85 eventPathList,
Cheng C Yang202a1ff2020-01-09 09:34:22 +080086 boost::container::flat_map<
87 std::string,
88 boost::container::flat_map<std::string, std::vector<std::string>>>&
89 groupEventPathList,
Lei YU7170a232021-02-04 16:19:27 +080090 const std::string& combineEventName, double pollRate);
Cheng C Yang58b2b532019-05-31 00:19:45 +080091 ~PSUCombineEvent();
92
93 sdbusplus::asio::object_server& objServer;
94 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
95 boost::container::flat_map<std::string,
Yong Libf8b1da2020-04-15 16:32:50 +080096 std::vector<std::shared_ptr<PSUSubEvent>>>
Cheng C Yang58b2b532019-05-31 00:19:45 +080097 events;
98 std::vector<std::shared_ptr<std::set<std::string>>> asserts;
99 std::vector<std::shared_ptr<bool>> states;
100};