blob: 88a8390cbf6950747f074b5ad3ccdf6bc83dc7c1 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 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#include <Utils.hpp>
18#include <boost/algorithm/string/predicate.hpp>
James Feist24f02f22019-04-15 11:05:39 -070019#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070020#include <fstream>
21#include <regex>
22#include <sdbusplus/asio/connection.hpp>
James Feist82bac4c2019-03-11 11:16:53 -070023#include <sdbusplus/asio/object_server.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <sdbusplus/bus/match.hpp>
25
James Feistcf3bce62019-01-08 10:07:19 -080026namespace fs = std::filesystem;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070027const static constexpr char* powerInterfaceName =
James Feist6714a252018-09-10 15:26:18 -070028 "xyz.openbmc_project.Chassis.Control.Power";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070029const static constexpr char* powerObjectName =
James Feist6714a252018-09-10 15:26:18 -070030 "/xyz/openbmc_project/Chassis/Control/Power0";
31
James Feist6ef20402019-01-07 16:45:08 -080032static bool powerStatusOn = false;
James Feistfc94b212019-02-06 16:14:51 -080033static bool biosHasPost = false;
James Feist6ef20402019-01-07 16:45:08 -080034static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
James Feist71d31b22019-01-02 16:57:54 -080035
James Feist6714a252018-09-10 15:26:18 -070036bool getSensorConfiguration(
37 const std::string& type,
38 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
39 ManagedObjectType& resp, bool useCache)
40{
41 static ManagedObjectType managedObj;
42
43 if (!useCache)
44 {
45 managedObj.clear();
46 sdbusplus::message::message getManagedObjects =
47 dbusConnection->new_method_call(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070048 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
James Feist6714a252018-09-10 15:26:18 -070049 "GetManagedObjects");
50 bool err = false;
51 try
52 {
53 sdbusplus::message::message reply =
54 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -070055 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -070056 }
57 catch (const sdbusplus::exception::exception&)
58 {
59 err = true;
60 }
61
62 if (err)
63 {
64 std::cerr << "Error communicating to entity manager\n";
65 return false;
66 }
67 }
68 for (const auto& pathPair : managedObj)
69 {
70 std::vector<boost::container::flat_map<std::string, BasicVariantType>>
71 sensorData;
72 bool correctType = false;
73 for (const auto& entry : pathPair.second)
74 {
75 if (boost::starts_with(entry.first, type))
76 {
77 correctType = true;
78 break;
79 }
80 }
81 if (correctType)
82 {
83 resp.emplace(pathPair);
84 }
85 }
86 return true;
87}
88
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089bool findFiles(const fs::path dirPath, const std::string& matchString,
90 std::vector<fs::path>& foundPaths, unsigned int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -070091{
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 if (!fs::exists(dirPath))
James Feist6714a252018-09-10 15:26:18 -070093 return false;
94
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 std::regex search(matchString);
James Feist6714a252018-09-10 15:26:18 -070096 std::smatch match;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 for (auto& p : fs::recursive_directory_iterator(dirPath))
James Feist6714a252018-09-10 15:26:18 -070098 {
99 std::string path = p.path().string();
100 if (!is_directory(p))
101 {
102 if (std::regex_search(path, match, search))
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 foundPaths.emplace_back(p.path());
James Feist6714a252018-09-10 15:26:18 -0700104 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 else if (is_symlink(p) && symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700106 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107 findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1);
James Feist6714a252018-09-10 15:26:18 -0700108 }
109 }
110 return true;
111}
112
James Feist71d31b22019-01-02 16:57:54 -0800113bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700114{
James Feist71d31b22019-01-02 16:57:54 -0800115 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700116 {
James Feist71d31b22019-01-02 16:57:54 -0800117 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700118 }
James Feist71d31b22019-01-02 16:57:54 -0800119 return powerStatusOn;
120}
121
James Feistfc94b212019-02-06 16:14:51 -0800122bool hasBiosPost(void)
123{
124 if (!powerMatch)
125 {
126 throw std::runtime_error("Power Match Not Created");
127 }
128 return biosHasPost;
129}
130
James Feist71d31b22019-01-02 16:57:54 -0800131void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
132{
James Feist6714a252018-09-10 15:26:18 -0700133 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800134 // cache the correct value
James Feist6714a252018-09-10 15:26:18 -0700135 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feist3f0e8762018-11-27 11:30:42 -0800136 [](sdbusplus::message::message& message) {
James Feist6714a252018-09-10 15:26:18 -0700137 std::string objectName;
James Feist3eb82622019-02-08 13:10:22 -0800138 boost::container::flat_map<std::string, std::variant<int32_t, bool>>
James Feist6714a252018-09-10 15:26:18 -0700139 values;
140 message.read(objectName, values);
141 auto findPgood = values.find("pgood");
142 if (findPgood != values.end())
143 {
James Feist3eb82622019-02-08 13:10:22 -0800144 powerStatusOn = std::get<int32_t>(findPgood->second);
James Feist6714a252018-09-10 15:26:18 -0700145 }
James Feistfc94b212019-02-06 16:14:51 -0800146 auto findPostComplete = values.find("post_complete");
147 if (findPostComplete != values.end())
148 {
James Feist3eb82622019-02-08 13:10:22 -0800149 biosHasPost = std::get<bool>(findPostComplete->second);
James Feistfc94b212019-02-06 16:14:51 -0800150 }
James Feist6714a252018-09-10 15:26:18 -0700151 };
152
153 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
154 static_cast<sdbusplus::bus::bus&>(*conn),
155 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
156 "namespace='/xyz/openbmc_project/Chassis/Control/"
James Feiste86810b2019-02-05 13:13:06 -0800157 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist6714a252018-09-10 15:26:18 -0700158 eventHandler);
159
160 conn->async_method_call(
James Feist3eb82622019-02-08 13:10:22 -0800161 [](boost::system::error_code ec, const std::variant<int32_t>& pgood) {
James Feist6714a252018-09-10 15:26:18 -0700162 if (ec)
163 {
James Feistc9445d82019-04-04 08:40:21 -0700164 // we commonly come up before power control, we'll capture the
165 // property change later
James Feist6714a252018-09-10 15:26:18 -0700166 return;
167 }
James Feist3eb82622019-02-08 13:10:22 -0800168 powerStatusOn = std::get<int32_t>(pgood);
James Feist6714a252018-09-10 15:26:18 -0700169 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties",
James Feistdc6c55f2018-10-31 12:53:20 -0700171 "Get", powerInterfaceName, "pgood");
James Feistfc94b212019-02-06 16:14:51 -0800172
173 conn->async_method_call(
174 [](boost::system::error_code ec,
James Feist3eb82622019-02-08 13:10:22 -0800175 const std::variant<int32_t>& postComplete) {
James Feistfc94b212019-02-06 16:14:51 -0800176 if (ec)
177 {
James Feistcee4ef22019-04-04 11:04:08 -0700178 // we commonly come up before power control, we'll capture the
179 // property change later
James Feistfc94b212019-02-06 16:14:51 -0800180 return;
181 }
James Feist3eb82622019-02-08 13:10:22 -0800182 biosHasPost = std::get<int32_t>(postComplete);
James Feistfc94b212019-02-06 16:14:51 -0800183 },
184 powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties",
185 "Get", powerInterfaceName, "post_complete");
James Feist3f0e8762018-11-27 11:30:42 -0800186}
James Feist87d713a2018-12-06 16:06:24 -0800187
188// replaces limits if MinReading and MaxReading are found.
189void findLimits(std::pair<double, double>& limits,
190 const SensorBaseConfiguration* data)
191{
192 if (!data)
193 {
194 return;
195 }
196 auto maxFind = data->second.find("MaxReading");
197 auto minFind = data->second.find("MinReading");
198
199 if (minFind != data->second.end())
200 {
James Feist3eb82622019-02-08 13:10:22 -0800201 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800202 }
203 if (maxFind != data->second.end())
204 {
James Feist3eb82622019-02-08 13:10:22 -0800205 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800206 }
James Feistfc94b212019-02-06 16:14:51 -0800207}
James Feist82bac4c2019-03-11 11:16:53 -0700208
209void createAssociation(
210 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
211 const std::string& path)
212{
213 if (association)
214 {
James Feistd2543f82019-04-09 11:10:06 -0700215 std::filesystem::path p(path);
216
James Feist82bac4c2019-03-11 11:16:53 -0700217 using Association = std::tuple<std::string, std::string, std::string>;
218 std::vector<Association> associations;
James Feistd2543f82019-04-09 11:10:06 -0700219 associations.push_back(
220 Association("inventory", "sensors", p.parent_path().string()));
James Feist82bac4c2019-03-11 11:16:53 -0700221 association->register_property("associations", associations);
222 association->initialize();
223 }
224}