blob: 75a1376d28e2d8b1b7a781f6b260517db21d9a4b [file] [log] [blame]
Brad Bishop613a5b32017-01-05 20:58:13 -05001/**
2 * Copyright © 2016 IBM 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#include <cstdlib>
Brad Bishop08379a32017-03-06 21:28:46 -050017#include <experimental/filesystem>
Brad Bishop613a5b32017-01-05 20:58:13 -050018#include <memory>
Brad Bishop4db64422017-02-16 11:33:32 -050019#include <phosphor-logging/log.hpp>
Brad Bishop613a5b32017-01-05 20:58:13 -050020#include "sysfs.hpp"
21#include "util.hpp"
Brad Bishop613a5b32017-01-05 20:58:13 -050022
23std::string findHwmon(const std::string& ofNode)
24{
Brad Bishop08379a32017-03-06 21:28:46 -050025 namespace fs = std::experimental::filesystem;
Brad Bishop613a5b32017-01-05 20:58:13 -050026 static constexpr auto hwmonRoot = "/sys/class/hwmon";
Brad Bishop08379a32017-03-06 21:28:46 -050027 static constexpr auto ofRoot = "/sys/firmware/devicetree/base";
Brad Bishop613a5b32017-01-05 20:58:13 -050028
Brad Bishop08379a32017-03-06 21:28:46 -050029 fs::path fullOfPath{ofRoot};
30 fullOfPath /= ofNode;
Brad Bishop613a5b32017-01-05 20:58:13 -050031
Brad Bishop08379a32017-03-06 21:28:46 -050032 for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot))
Brad Bishop613a5b32017-01-05 20:58:13 -050033 {
Brad Bishop08379a32017-03-06 21:28:46 -050034 auto path = hwmonInst.path();
35 path /= "of_node";
Brad Bishop613a5b32017-01-05 20:58:13 -050036
Brad Bishop08379a32017-03-06 21:28:46 -050037 if (fs::canonical(path) != fullOfPath)
Brad Bishop613a5b32017-01-05 20:58:13 -050038 {
39 continue;
40 }
41
Brad Bishop08379a32017-03-06 21:28:46 -050042 return hwmonInst.path();
Brad Bishop613a5b32017-01-05 20:58:13 -050043 }
44
45 return std::string();
46}
47
Brad Bishop4db64422017-02-16 11:33:32 -050048int readSysfsWithCallout(const std::string& root,
49 const std::string& instance,
50 const std::string& type,
51 const std::string& id,
52 const std::string& sensor)
53{
Brad Bishop5ec68ab2017-03-27 13:41:02 -040054 namespace fs = std::experimental::filesystem;
55
Brad Bishop4db64422017-02-16 11:33:32 -050056 int value = 0;
Brad Bishop5ec68ab2017-03-27 13:41:02 -040057 std::ifstream ifs;
58 fs::path instancePath{root};
59 instancePath /= instance;
Brad Bishop4db64422017-02-16 11:33:32 -050060 std::string fullPath = make_sysfs_path(instancePath,
61 type, id, sensor);
Brad Bishop4db64422017-02-16 11:33:32 -050062
63 ifs.exceptions(std::ifstream::failbit
64 | std::ifstream::badbit
65 | std::ifstream::eofbit);
66 try
67 {
68 ifs.open(fullPath);
69 ifs >> value;
70 }
71 catch (const std::exception& e)
72 {
73 // Too many GCC bugs (53984, 66145) to do
74 // this the right way...
Brad Bishop4db64422017-02-16 11:33:32 -050075
76 // errno should still reflect the error from the failing open
77 // or read system calls that got us here.
78 auto rc = errno;
Brad Bishop5ec68ab2017-03-27 13:41:02 -040079 instancePath /= "device";
Brad Bishop4db64422017-02-16 11:33:32 -050080 phosphor::logging::log<phosphor::logging::level::ERR>(
81 strerror(rc),
Brad Bishop5ec68ab2017-03-27 13:41:02 -040082 phosphor::logging::entry(
83 "CALLOUT_DEVICE_PATH=%s",
84 fs::canonical(instancePath).c_str()),
Brad Bishop4db64422017-02-16 11:33:32 -050085 phosphor::logging::entry("CALLOUT_ERRNO=%d", rc));
86 exit(EXIT_FAILURE);
87 }
88
89 return value;
90}
91
Brad Bishop613a5b32017-01-05 20:58:13 -050092// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4