xref: /openbmc/phosphor-hwmon/sysfs.cpp (revision 613a5b37b11a272da503fcf6ac0c734a6e342442)
1*613a5b37SBrad Bishop /**
2*613a5b37SBrad Bishop  * Copyright © 2016 IBM Corporation
3*613a5b37SBrad Bishop  *
4*613a5b37SBrad Bishop  * Licensed under the Apache License, Version 2.0 (the "License");
5*613a5b37SBrad Bishop  * you may not use this file except in compliance with the License.
6*613a5b37SBrad Bishop  * You may obtain a copy of the License at
7*613a5b37SBrad Bishop  *
8*613a5b37SBrad Bishop  *     http://www.apache.org/licenses/LICENSE-2.0
9*613a5b37SBrad Bishop  *
10*613a5b37SBrad Bishop  * Unless required by applicable law or agreed to in writing, software
11*613a5b37SBrad Bishop  * distributed under the License is distributed on an "AS IS" BASIS,
12*613a5b37SBrad Bishop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*613a5b37SBrad Bishop  * See the License for the specific language governing permissions and
14*613a5b37SBrad Bishop  * limitations under the License.
15*613a5b37SBrad Bishop  */
16*613a5b37SBrad Bishop #include <cstdlib>
17*613a5b37SBrad Bishop #include <memory>
18*613a5b37SBrad Bishop #include "sysfs.hpp"
19*613a5b37SBrad Bishop #include "util.hpp"
20*613a5b37SBrad Bishop #include "directory.hpp"
21*613a5b37SBrad Bishop 
22*613a5b37SBrad Bishop std::string findHwmon(const std::string& ofNode)
23*613a5b37SBrad Bishop {
24*613a5b37SBrad Bishop     static constexpr auto hwmonRoot = "/sys/class/hwmon";
25*613a5b37SBrad Bishop 
26*613a5b37SBrad Bishop     std::string fullOfPath{"/sys/firmware/devicetree/base"};
27*613a5b37SBrad Bishop     fullOfPath.append(ofNode);
28*613a5b37SBrad Bishop 
29*613a5b37SBrad Bishop     std::string hwmonInst;
30*613a5b37SBrad Bishop     Directory d(hwmonRoot);
31*613a5b37SBrad Bishop 
32*613a5b37SBrad Bishop     while (d.next(hwmonInst))
33*613a5b37SBrad Bishop     {
34*613a5b37SBrad Bishop         std::string hwmonPath{hwmonRoot};
35*613a5b37SBrad Bishop         hwmonPath.append(1, '/');
36*613a5b37SBrad Bishop         hwmonPath.append(hwmonInst);
37*613a5b37SBrad Bishop         std::string path{hwmonPath};
38*613a5b37SBrad Bishop         path.append(1, '/');
39*613a5b37SBrad Bishop         path.append("of_node");
40*613a5b37SBrad Bishop 
41*613a5b37SBrad Bishop         auto real = std::unique_ptr<char, phosphor::utility::Free<char>>(realpath(
42*613a5b37SBrad Bishop                         path.c_str(), nullptr));
43*613a5b37SBrad Bishop         if (!real || real.get() != fullOfPath)
44*613a5b37SBrad Bishop         {
45*613a5b37SBrad Bishop             continue;
46*613a5b37SBrad Bishop         }
47*613a5b37SBrad Bishop 
48*613a5b37SBrad Bishop         return hwmonPath;
49*613a5b37SBrad Bishop     }
50*613a5b37SBrad Bishop 
51*613a5b37SBrad Bishop     return std::string();
52*613a5b37SBrad Bishop }
53*613a5b37SBrad Bishop 
54*613a5b37SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
55