blob: 3e066cf7149faebd2237f004e70e35a3e9306f89 [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>
Cheng C Yang58b2b532019-05-31 00:19:45 +080025#include <sdbusplus/asio/object_server.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026
Ed Tanous16966b52021-09-15 15:06:59 -070027#include <array>
James Feist38fb5982020-05-28 10:09:54 -070028#include <memory>
Patrick Venturefd6ba732019-10-31 14:27:39 -070029#include <set>
30#include <string>
31#include <vector>
Cheng C Yang58b2b532019-05-31 00:19:45 +080032
Yong Libf8b1da2020-04-15 16:32:50 +080033class PSUSubEvent : public std::enable_shared_from_this<PSUSubEvent>
Cheng C Yang58b2b532019-05-31 00:19:45 +080034{
35 public:
36 PSUSubEvent(std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
Yong Li3046a022020-04-03 13:01:02 +080037 const std::string& path,
38 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080039 boost::asio::io_context& io, const PowerState& powerState,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000040 const std::string& groupEventName, const std::string& eventName,
Cheng C Yang58b2b532019-05-31 00:19:45 +080041 std::shared_ptr<std::set<std::string>> asserts,
42 std::shared_ptr<std::set<std::string>> combineEvent,
Lei YU7170a232021-02-04 16:19:27 +080043 std::shared_ptr<bool> state, const std::string& psuName,
44 double pollRate);
Cheng C Yang58b2b532019-05-31 00:19:45 +080045 ~PSUSubEvent();
46
47 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
48 std::shared_ptr<std::set<std::string>> asserts;
49 std::shared_ptr<std::set<std::string>> combineEvent;
50 std::shared_ptr<bool> assertState;
Yong Libf8b1da2020-04-15 16:32:50 +080051 void setupRead(void);
Cheng C Yang58b2b532019-05-31 00:19:45 +080052
53 private:
54 int value = 0;
Ed Tanousb429f312022-06-27 16:09:53 -070055 size_t errCount{0};
Cheng C Yang58b2b532019-05-31 00:19:45 +080056 std::string path;
57 std::string eventName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050058
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000059 PowerState readState;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070060 boost::asio::steady_timer waitTimer;
Ed Tanous16966b52021-09-15 15:06:59 -070061 std::shared_ptr<std::array<char, 128>> buffer;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000062 void restartRead();
Ed Tanous16966b52021-09-15 15:06:59 -070063 void handleResponse(const boost::system::error_code& err,
64 size_t bytesTransferred);
Cheng C Yang58b2b532019-05-31 00:19:45 +080065 void updateValue(const int& newValue);
Ed Tanous16966b52021-09-15 15:06:59 -070066 boost::asio::random_access_file inputDev;
Cheng C Yang5665db22019-07-12 00:57:30 +080067 std::string psuName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050068 std::string groupEventName;
Cheng C Yang5665db22019-07-12 00:57:30 +080069 std::string fanName;
Cheng C Yang9b53a622019-08-27 22:13:58 +080070 std::string assertMessage;
71 std::string deassertMessage;
Yong Li3046a022020-04-03 13:01:02 +080072 std::shared_ptr<sdbusplus::asio::connection> systemBus;
Lei YU7170a232021-02-04 16:19:27 +080073 unsigned int eventPollMs = defaultEventPollMs;
74 static constexpr unsigned int defaultEventPollMs = 1000;
75 static constexpr size_t warnAfterErrorCount = 10;
Cheng C Yang58b2b532019-05-31 00:19:45 +080076};
77
78class PSUCombineEvent
79{
80 public:
81 PSUCombineEvent(
Ed Tanous2049bd22022-07-09 07:20:26 -070082 sdbusplus::asio::object_server& objectServer,
Yong Li3046a022020-04-03 13:01:02 +080083 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080084 boost::asio::io_context& io, const std::string& psuName,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000085 const PowerState& powerState,
Cheng C Yang58b2b532019-05-31 00:19:45 +080086 boost::container::flat_map<std::string, std::vector<std::string>>&
87 eventPathList,
Cheng C Yang202a1ff2020-01-09 09:34:22 +080088 boost::container::flat_map<
89 std::string,
90 boost::container::flat_map<std::string, std::vector<std::string>>>&
91 groupEventPathList,
Lei YU7170a232021-02-04 16:19:27 +080092 const std::string& combineEventName, double pollRate);
Cheng C Yang58b2b532019-05-31 00:19:45 +080093 ~PSUCombineEvent();
94
95 sdbusplus::asio::object_server& objServer;
96 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
97 boost::container::flat_map<std::string,
Yong Libf8b1da2020-04-15 16:32:50 +080098 std::vector<std::shared_ptr<PSUSubEvent>>>
Cheng C Yang58b2b532019-05-31 00:19:45 +080099 events;
100 std::vector<std::shared_ptr<std::set<std::string>>> asserts;
101 std::vector<std::shared_ptr<bool>> states;
102};