blob: ad99bf7f419396c58510d5f640edb4ec964310d9 [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>
Matthew Barth048ac872017-03-09 14:36:08 -060019#include <phosphor-logging/elog.hpp>
20#include <phosphor-logging/elog-errors.hpp>
21#include <xyz/openbmc_project/Control/Device/error.hpp>
Matthew Barth4e1f30f2017-03-21 16:13:27 -050022#include <xyz/openbmc_project/Sensor/Device/error.hpp>
Brad Bishop613a5b32017-01-05 20:58:13 -050023#include "sysfs.hpp"
24#include "util.hpp"
Brandon Wyman4eb98582017-05-24 14:24:00 -050025#include <fstream>
Brad Bishop613a5b32017-01-05 20:58:13 -050026
Matthew Barth048ac872017-03-09 14:36:08 -060027using namespace phosphor::logging;
28
Brad Bishop613a5b32017-01-05 20:58:13 -050029std::string findHwmon(const std::string& ofNode)
30{
Brad Bishop08379a32017-03-06 21:28:46 -050031 namespace fs = std::experimental::filesystem;
Brad Bishop613a5b32017-01-05 20:58:13 -050032 static constexpr auto hwmonRoot = "/sys/class/hwmon";
Brad Bishop08379a32017-03-06 21:28:46 -050033 static constexpr auto ofRoot = "/sys/firmware/devicetree/base";
Brad Bishop613a5b32017-01-05 20:58:13 -050034
Brad Bishop08379a32017-03-06 21:28:46 -050035 fs::path fullOfPath{ofRoot};
36 fullOfPath /= ofNode;
Brandon Wyman4eb98582017-05-24 14:24:00 -050037 fs::path fullOfPathPhandle{fullOfPath};
38 fullOfPathPhandle /= "phandle";
Brad Bishop613a5b32017-01-05 20:58:13 -050039
Brad Bishop08379a32017-03-06 21:28:46 -050040 for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot))
Brad Bishop613a5b32017-01-05 20:58:13 -050041 {
Brad Bishop08379a32017-03-06 21:28:46 -050042 auto path = hwmonInst.path();
43 path /= "of_node";
Brad Bishop613a5b32017-01-05 20:58:13 -050044
Brad Bishop08379a32017-03-06 21:28:46 -050045 if (fs::canonical(path) != fullOfPath)
Brad Bishop613a5b32017-01-05 20:58:13 -050046 {
Brandon Wyman4eb98582017-05-24 14:24:00 -050047 // Try to find HWMON instance via phandle values.
48 // Used for IIO device drivers.
49 path /= "io-channels";
50 if (fs::exists(path) && fs::exists(fullOfPathPhandle))
51 {
52 std::ifstream ioChannelsFile(path);
53 std::ifstream pHandleFile(fullOfPathPhandle);
54
55 uint32_t ioChannelsValue;
56 uint32_t pHandleValue;
57
58 try
59 {
60 ioChannelsFile.read(reinterpret_cast<char*>(&ioChannelsValue),
61 sizeof(ioChannelsValue));
62 pHandleFile.read(reinterpret_cast<char*>(&pHandleValue),
63 sizeof(pHandleValue));
64
65 if (ioChannelsValue == pHandleValue)
66 {
67 return hwmonInst.path();
68 }
69 }
70 catch (const std::exception& e)
71 {
72 log<level::INFO>(e.what());
73 }
74
75
76 }
Brad Bishop613a5b32017-01-05 20:58:13 -050077 continue;
78 }
79
Brad Bishop08379a32017-03-06 21:28:46 -050080 return hwmonInst.path();
Brad Bishop613a5b32017-01-05 20:58:13 -050081 }
82
83 return std::string();
84}
85
Brad Bishop4db64422017-02-16 11:33:32 -050086int readSysfsWithCallout(const std::string& root,
87 const std::string& instance,
88 const std::string& type,
89 const std::string& id,
90 const std::string& sensor)
91{
Brad Bishop5ec68ab2017-03-27 13:41:02 -040092 namespace fs = std::experimental::filesystem;
93
Brad Bishop4db64422017-02-16 11:33:32 -050094 int value = 0;
Brad Bishop5ec68ab2017-03-27 13:41:02 -040095 std::ifstream ifs;
96 fs::path instancePath{root};
97 instancePath /= instance;
Brad Bishop4db64422017-02-16 11:33:32 -050098 std::string fullPath = make_sysfs_path(instancePath,
99 type, id, sensor);
Brad Bishop4db64422017-02-16 11:33:32 -0500100
101 ifs.exceptions(std::ifstream::failbit
102 | std::ifstream::badbit
103 | std::ifstream::eofbit);
104 try
105 {
106 ifs.open(fullPath);
107 ifs >> value;
108 }
109 catch (const std::exception& e)
110 {
111 // Too many GCC bugs (53984, 66145) to do
112 // this the right way...
Brad Bishop4db64422017-02-16 11:33:32 -0500113
114 // errno should still reflect the error from the failing open
115 // or read system calls that got us here.
116 auto rc = errno;
Brad Bishop5ec68ab2017-03-27 13:41:02 -0400117 instancePath /= "device";
Matthew Barth4e1f30f2017-03-21 16:13:27 -0500118 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
Marri Devender Rao05711eb2017-04-15 06:47:11 -0500119 report<ReadFailure>(
120 xyz::openbmc_project::Sensor::Device::
121 ReadFailure::CALLOUT_ERRNO(rc),
122 xyz::openbmc_project::Sensor::Device::
123 ReadFailure::CALLOUT_DEVICE_PATH(
124 fs::canonical(instancePath).c_str()));
125
Brad Bishop4db64422017-02-16 11:33:32 -0500126 exit(EXIT_FAILURE);
127 }
128
129 return value;
130}
131
Matthew Barth048ac872017-03-09 14:36:08 -0600132uint64_t writeSysfsWithCallout(const uint64_t& value,
133 const std::string& root,
134 const std::string& instance,
135 const std::string& type,
136 const std::string& id,
137 const std::string& sensor)
138{
139 namespace fs = std::experimental::filesystem;
140
141 std::string valueStr = std::to_string(value);
142 std::ofstream ofs;
143 fs::path instancePath{root};
144 instancePath /= instance;
145 std::string fullPath = make_sysfs_path(instancePath,
146 type, id, sensor);
147
148 ofs.exceptions(std::ofstream::failbit
149 | std::ofstream::badbit
150 | std::ofstream::eofbit);
151 try
152 {
153 ofs.open(fullPath);
154 ofs << valueStr;
155 }
156 catch (const std::exception& e)
157 {
158 // errno should still reflect the error from the failing open
159 // or write system calls that got us here.
160 auto rc = errno;
161 instancePath /= "device";
162 using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error;
Marri Devender Rao05711eb2017-04-15 06:47:11 -0500163 report<WriteFailure>(
164 xyz::openbmc_project::Control::Device::
165 WriteFailure::CALLOUT_ERRNO(rc),
166 xyz::openbmc_project::Control::Device::
167 WriteFailure::CALLOUT_DEVICE_PATH(
168 fs::canonical(instancePath).c_str()));
169
Matthew Barth048ac872017-03-09 14:36:08 -0600170 exit(EXIT_FAILURE);
171 }
172
173 return value;
174}
175
Brad Bishop613a5b32017-01-05 20:58:13 -0500176// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4