xref: /openbmc/phosphor-hwmon/sensorset.cpp (revision 5906173a)
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 "sensorset.hpp"
17 
18 #include "hwmon.hpp"
19 
20 #include <filesystem>
21 #include <iostream>
22 #include <regex>
23 
24 // TODO: Issue#2 - STL regex generates really bloated code.  Use POSIX regex
25 //       interfaces instead.
26 static const std::regex sensors_regex = std::regex(
27     "^(fan|in|temp|power|energy|curr)([0-9]+)_([a-z]*)", std::regex::extended);
28 static const auto sensor_regex_match_count = 4;
29 
30 SensorSet::SensorSet(const std::string& path)
31 {
32     namespace fs = std::filesystem;
33 
34     for (const auto& file : fs::directory_iterator(path))
35     {
36         std::smatch match;
37         auto fileName = file.path().filename();
38         std::regex_search(fileName.native(), match, sensors_regex);
39 
40         if (match.size() != sensor_regex_match_count)
41         {
42             continue;
43         }
44 
45         if (match[3] == hwmon::entry::label)
46         {
47             continue;
48         }
49 
50         _container[make_pair(match[1], match[2])].emplace(match[3]);
51     }
52 }
53 
54 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
55