blob: d761f450b65cdc187aefdd1047d145060f54f61d [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
George Liu5b3542e2023-10-31 17:27:12 +080033using EventPathList =
34 boost::container::flat_map<std::string, std::vector<std::string>>;
35using GroupEventPathList =
36 boost::container::flat_map<std::string, EventPathList>;
37
Yong Libf8b1da2020-04-15 16:32:50 +080038class PSUSubEvent : public std::enable_shared_from_this<PSUSubEvent>
Cheng C Yang58b2b532019-05-31 00:19:45 +080039{
40 public:
41 PSUSubEvent(std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface,
Yong Li3046a022020-04-03 13:01:02 +080042 const std::string& path,
43 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080044 boost::asio::io_context& io, const PowerState& powerState,
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000045 const std::string& groupEventName, const std::string& eventName,
Cheng C Yang58b2b532019-05-31 00:19:45 +080046 std::shared_ptr<std::set<std::string>> asserts,
47 std::shared_ptr<std::set<std::string>> combineEvent,
Lei YU7170a232021-02-04 16:19:27 +080048 std::shared_ptr<bool> state, const std::string& psuName,
49 double pollRate);
Cheng C Yang58b2b532019-05-31 00:19:45 +080050 ~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;
Ed Tanous201a1012024-04-03 18:07:28 -070056 void setupRead();
Cheng C Yang58b2b532019-05-31 00:19:45 +080057
58 private:
59 int value = 0;
Ed Tanousb429f312022-06-27 16:09:53 -070060 size_t errCount{0};
Cheng C Yang58b2b532019-05-31 00:19:45 +080061 std::string path;
62 std::string eventName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050063
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000064 PowerState readState;
Ed Tanous9b4a20e2022-09-06 08:47:11 -070065 boost::asio::steady_timer waitTimer;
Ed Tanous16966b52021-09-15 15:06:59 -070066 std::shared_ptr<std::array<char, 128>> buffer;
Konstantin Aladyshevc7a1ae62021-04-30 08:50:43 +000067 void restartRead();
Ed Tanous16966b52021-09-15 15:06:59 -070068 void handleResponse(const boost::system::error_code& err,
69 size_t bytesTransferred);
Cheng C Yang58b2b532019-05-31 00:19:45 +080070 void updateValue(const int& newValue);
Ed Tanous16966b52021-09-15 15:06:59 -070071 boost::asio::random_access_file inputDev;
Cheng C Yang5665db22019-07-12 00:57:30 +080072 std::string psuName;
Brad Bishopfeb19ef2019-11-07 18:02:16 -050073 std::string groupEventName;
Cheng C Yang5665db22019-07-12 00:57:30 +080074 std::string fanName;
Cheng C Yang9b53a622019-08-27 22:13:58 +080075 std::string assertMessage;
76 std::string deassertMessage;
Yong Li3046a022020-04-03 13:01:02 +080077 std::shared_ptr<sdbusplus::asio::connection> systemBus;
Lei YU7170a232021-02-04 16:19:27 +080078 unsigned int eventPollMs = defaultEventPollMs;
79 static constexpr unsigned int defaultEventPollMs = 1000;
80 static constexpr size_t warnAfterErrorCount = 10;
Cheng C Yang58b2b532019-05-31 00:19:45 +080081};
82
83class PSUCombineEvent
84{
85 public:
George Liu5b3542e2023-10-31 17:27:12 +080086 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);
Cheng C Yang58b2b532019-05-31 00:19:45 +080092 ~PSUCombineEvent();
93
94 sdbusplus::asio::object_server& objServer;
95 std::shared_ptr<sdbusplus::asio::dbus_interface> eventInterface;
96 boost::container::flat_map<std::string,
Yong Libf8b1da2020-04-15 16:32:50 +080097 std::vector<std::shared_ptr<PSUSubEvent>>>
Cheng C Yang58b2b532019-05-31 00:19:45 +080098 events;
99 std::vector<std::shared_ptr<std::set<std::string>>> asserts;
100 std::vector<std::shared_ptr<bool>> states;
101};