blob: 75c89f2c42da799326b26d74a04aec0d5c773ebc [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103019#include "Utils.hpp"
20
Ed Tanous1f978632023-02-28 18:16:39 -080021#include <boost/asio/io_context.hpp>
Ed Tanous16966b52021-09-15 15:06:59 -070022#include <boost/asio/random_access_file.hpp>
Ed Tanous9b4a20e2022-09-06 08:47:11 -070023#include <boost/asio/steady_timer.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -070024#include <boost/container/flat_map.hpp>
Ed Tanous18b61862025-01-30 10:56:28 -080025#include <sdbusplus/asio/connection.hpp>
Cheng C Yang58b2b532019-05-31 00:19:45 +080026#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070027
Ed Tanous16966b52021-09-15 15:06:59 -070028#include <array>
Ed Tanous18b61862025-01-30 10:56:28 -080029#include <cstddef>
James Feist38fb5982020-05-28 10:09:54 -070030#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070031#include <set>
32#include <string>
33#include <vector>
Cheng C Yang58b2b532019-05-31 00:19:45 +080034
George Liu5b3542e2023-10-31 17:27:12 +080035using EventPathList =
36 boost::container::flat_map<std::string, std::vector<std::string>>;
37using GroupEventPathList =
38 boost::container::flat_map<std::string, EventPathList>;
39
Yong Libf8b1da2020-04-15 16:32:50 +080040class PSUSubEvent : public std::enable_shared_from_this<PSUSubEvent>
Cheng C Yang58b2b532019-05-31 00:19:45 +080041{
42 public:
43 PSUSubEvent(std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
Yong Li3046a022020-04-03 13:01:02 +080044 const std::string& path,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080046 boost::asio::io_context& io, const PowerState& powerState,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000047 const std::string& groupEventName, const std::string& eventName,
Cheng C Yang58b2b532019-05-31 00:19:45 +080048 std::shared_ptr<std::set<std::string>> asserts,
49 std::shared_ptr<std::set<std::string>> combineEvent,
Lei YU7170a232021-02-04 16:19:27 +080050 std::shared_ptr<bool> state, const std::string& psuName,
51 double pollRate);
Cheng C Yang58b2b532019-05-31 00:19:45 +080052 ~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;
Ed Tanous201a1012024-04-03 18:07:28 -070058 void setupRead();
Cheng C Yang58b2b532019-05-31 00:19:45 +080059
60 private:
61 int value = 0;
Ed Tanousb429f312022-06-27 16:09:53 -070062 size_t errCount{0};
Cheng C Yang58b2b532019-05-31 00:19:45 +080063 std::string path;
64 std::string eventName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050065
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000066 PowerState readState;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070067 boost::asio::steady_timer waitTimer;
Ed Tanous16966b52021-09-15 15:06:59 -070068 std::shared_ptr<std::array<char, 128>> buffer;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000069 void restartRead();
Ed Tanous16966b52021-09-15 15:06:59 -070070 void handleResponse(const boost::system::error_code& err,
71 size_t bytesTransferred);
Cheng C Yang58b2b532019-05-31 00:19:45 +080072 void updateValue(const int& newValue);
Ed Tanous16966b52021-09-15 15:06:59 -070073 boost::asio::random_access_file inputDev;
Cheng C Yang5665db22019-07-12 00:57:30 +080074 std::string psuName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050075 std::string groupEventName;
Cheng C Yang5665db22019-07-12 00:57:30 +080076 std::string fanName;
Cheng C Yang9b53a622019-08-27 22:13:58 +080077 std::string assertMessage;
78 std::string deassertMessage;
Yong Li3046a022020-04-03 13:01:02 +080079 std::shared_ptr<sdbusplus::asio::connection> systemBus;
Lei YU7170a232021-02-04 16:19:27 +080080 unsigned int eventPollMs = defaultEventPollMs;
81 static constexpr unsigned int defaultEventPollMs = 1000;
82 static constexpr size_t warnAfterErrorCount = 10;
Cheng C Yang58b2b532019-05-31 00:19:45 +080083};
84
85class PSUCombineEvent
86{
87 public:
George Liu5b3542e2023-10-31 17:27:12 +080088 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);
Cheng C Yang58b2b532019-05-31 00:19:45 +080094 ~PSUCombineEvent();
95
96 sdbusplus::asio::object_server& objServer;
97 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
98 boost::container::flat_map<std::string,
Yong Libf8b1da2020-04-15 16:32:50 +080099 std::vector<std::shared_ptr<PSUSubEvent>>>
Cheng C Yang58b2b532019-05-31 00:19:45 +0800100 events;
101 std::vector<std::shared_ptr<std::set<std::string>>> asserts;
102 std::vector<std::shared_ptr<bool>> states;
103};