blob: c52f88f5223977267141b9f5a9e2e05545c821b4 [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>
19#include <experimental/filesystem>
20#include <fstream>
21#include <regex>
22#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/bus/match.hpp>
24
25namespace fs = std::experimental::filesystem;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070026const static constexpr char* powerInterfaceName =
James Feist6714a252018-09-10 15:26:18 -070027 "xyz.openbmc_project.Chassis.Control.Power";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070028const static constexpr char* powerObjectName =
James Feist6714a252018-09-10 15:26:18 -070029 "/xyz/openbmc_project/Chassis/Control/Power0";
30
James Feist71d31b22019-01-02 16:57:54 -080031bool powerStatusOn = false;
32std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
33
James Feist6714a252018-09-10 15:26:18 -070034bool getSensorConfiguration(
35 const std::string& type,
36 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
37 ManagedObjectType& resp, bool useCache)
38{
39 static ManagedObjectType managedObj;
40
41 if (!useCache)
42 {
43 managedObj.clear();
44 sdbusplus::message::message getManagedObjects =
45 dbusConnection->new_method_call(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
James Feist6714a252018-09-10 15:26:18 -070047 "GetManagedObjects");
48 bool err = false;
49 try
50 {
51 sdbusplus::message::message reply =
52 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -070053 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -070054 }
55 catch (const sdbusplus::exception::exception&)
56 {
57 err = true;
58 }
59
60 if (err)
61 {
62 std::cerr << "Error communicating to entity manager\n";
63 return false;
64 }
65 }
66 for (const auto& pathPair : managedObj)
67 {
68 std::vector<boost::container::flat_map<std::string, BasicVariantType>>
69 sensorData;
70 bool correctType = false;
71 for (const auto& entry : pathPair.second)
72 {
73 if (boost::starts_with(entry.first, type))
74 {
75 correctType = true;
76 break;
77 }
78 }
79 if (correctType)
80 {
81 resp.emplace(pathPair);
82 }
83 }
84 return true;
85}
86
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087bool findFiles(const fs::path dirPath, const std::string& matchString,
88 std::vector<fs::path>& foundPaths, unsigned int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -070089{
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090 if (!fs::exists(dirPath))
James Feist6714a252018-09-10 15:26:18 -070091 return false;
92
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 std::regex search(matchString);
James Feist6714a252018-09-10 15:26:18 -070094 std::smatch match;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 for (auto& p : fs::recursive_directory_iterator(dirPath))
James Feist6714a252018-09-10 15:26:18 -070096 {
97 std::string path = p.path().string();
98 if (!is_directory(p))
99 {
100 if (std::regex_search(path, match, search))
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 foundPaths.emplace_back(p.path());
James Feist6714a252018-09-10 15:26:18 -0700102 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 else if (is_symlink(p) && symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700104 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1);
James Feist6714a252018-09-10 15:26:18 -0700106 }
107 }
108 return true;
109}
110
James Feist71d31b22019-01-02 16:57:54 -0800111bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700112{
James Feist71d31b22019-01-02 16:57:54 -0800113 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feist71d31b22019-01-02 16:57:54 -0800115 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700116 }
James Feist71d31b22019-01-02 16:57:54 -0800117 return powerStatusOn;
118}
119
120void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
121{
James Feist6714a252018-09-10 15:26:18 -0700122
123 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800124 // cache the correct value
James Feist6714a252018-09-10 15:26:18 -0700125 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feist3f0e8762018-11-27 11:30:42 -0800126 [](sdbusplus::message::message& message) {
James Feist6714a252018-09-10 15:26:18 -0700127 std::string objectName;
128 boost::container::flat_map<std::string,
129 sdbusplus::message::variant<int32_t>>
130 values;
131 message.read(objectName, values);
132 auto findPgood = values.find("pgood");
133 if (findPgood != values.end())
134 {
135 powerStatusOn = sdbusplus::message::variant_ns::get<int32_t>(
136 findPgood->second);
137 }
138 };
139
140 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
141 static_cast<sdbusplus::bus::bus&>(*conn),
142 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
143 "namespace='/xyz/openbmc_project/Chassis/Control/"
144 "power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
145 eventHandler);
146
147 conn->async_method_call(
James Feist3f0e8762018-11-27 11:30:42 -0800148 [](boost::system::error_code ec,
149 const sdbusplus::message::variant<int32_t>& pgood) {
James Feist6714a252018-09-10 15:26:18 -0700150 if (ec)
151 {
152 std::cerr << "Error getting initial power status\n";
153 return;
154 }
155 powerStatusOn = sdbusplus::message::variant_ns::get<int32_t>(pgood);
156 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700157 powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties",
James Feistdc6c55f2018-10-31 12:53:20 -0700158 "Get", powerInterfaceName, "pgood");
James Feist3f0e8762018-11-27 11:30:42 -0800159}
James Feist87d713a2018-12-06 16:06:24 -0800160
161// replaces limits if MinReading and MaxReading are found.
162void findLimits(std::pair<double, double>& limits,
163 const SensorBaseConfiguration* data)
164{
165 if (!data)
166 {
167 return;
168 }
169 auto maxFind = data->second.find("MaxReading");
170 auto minFind = data->second.find("MinReading");
171
172 if (minFind != data->second.end())
173 {
174 limits.first = sdbusplus::message::variant_ns::visit(
175 VariantToDoubleVisitor(), minFind->second);
176 }
177 if (maxFind != data->second.end())
178 {
179 limits.second = sdbusplus::message::variant_ns::visit(
180 VariantToDoubleVisitor(), maxFind->second);
181 }
182}