xref: /openbmc/phosphor-hwmon/sysfs.cpp (revision 613a5b37)
1 /**
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>
17 #include <memory>
18 #include "sysfs.hpp"
19 #include "util.hpp"
20 #include "directory.hpp"
21 
22 std::string findHwmon(const std::string& ofNode)
23 {
24     static constexpr auto hwmonRoot = "/sys/class/hwmon";
25 
26     std::string fullOfPath{"/sys/firmware/devicetree/base"};
27     fullOfPath.append(ofNode);
28 
29     std::string hwmonInst;
30     Directory d(hwmonRoot);
31 
32     while (d.next(hwmonInst))
33     {
34         std::string hwmonPath{hwmonRoot};
35         hwmonPath.append(1, '/');
36         hwmonPath.append(hwmonInst);
37         std::string path{hwmonPath};
38         path.append(1, '/');
39         path.append("of_node");
40 
41         auto real = std::unique_ptr<char, phosphor::utility::Free<char>>(realpath(
42                         path.c_str(), nullptr));
43         if (!real || real.get() != fullOfPath)
44         {
45             continue;
46         }
47 
48         return hwmonPath;
49     }
50 
51     return std::string();
52 }
53 
54 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
55