blob: e3f4e9c2cfc3db5d34bdf934266f0e4b51fcd60f [file] [log] [blame]
Brad Bishop29dbfa62016-12-19 13:39:57 -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 */
Matthew Barth6292aee2016-10-06 10:15:48 -050016#include "sensorset.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -070017
Brad Bishop6e8f6232017-01-04 15:39:03 -050018#include "hwmon.hpp"
Patrick Williams3667cf32015-10-20 22:39:11 -050019
Patrick Venture9e997b42019-03-08 13:42:10 -080020#include <filesystem>
Patrick Venture043d3232018-08-31 10:10:53 -070021#include <iostream>
22#include <regex>
23
Patrick Williams3667cf32015-10-20 22:39:11 -050024// TODO: Issue#2 - STL regex generates really bloated code. Use POSIX regex
25// interfaces instead.
Patrick Venture043d3232018-08-31 10:10:53 -070026static const std::regex sensors_regex = std::regex(
27 "^(fan|in|temp|power|energy|curr)([0-9]+)_([a-z]*)", std::regex::extended);
Patrick Williams3667cf32015-10-20 22:39:11 -050028static const auto sensor_regex_match_count = 4;
29
30SensorSet::SensorSet(const std::string& path)
31{
Patrick Venture9e997b42019-03-08 13:42:10 -080032 namespace fs = std::filesystem;
Patrick Williams3667cf32015-10-20 22:39:11 -050033
Brad Bishop08379a32017-03-06 21:28:46 -050034 for (const auto& file : fs::directory_iterator(path))
Patrick Williams3667cf32015-10-20 22:39:11 -050035 {
36 std::smatch match;
Brad Bishop08379a32017-03-06 21:28:46 -050037 auto fileName = file.path().filename();
38 std::regex_search(fileName.native(), match, sensors_regex);
Patrick Williams3667cf32015-10-20 22:39:11 -050039
Brad Bishop6bb97a92016-12-19 13:06:40 -050040 if (match.size() != sensor_regex_match_count)
41 {
42 continue;
43 }
Patrick Williams3667cf32015-10-20 22:39:11 -050044
Brad Bishop6e8f6232017-01-04 15:39:03 -050045 if (match[3] == hwmon::entry::label)
46 {
47 continue;
48 }
49
Patrick Ventured0f50972018-12-19 14:43:12 -080050 _container[make_pair(match[1], match[2])].emplace(match[3]);
Patrick Williams3667cf32015-10-20 22:39:11 -050051 }
52}
Brad Bishop03476f12016-12-19 13:09:12 -050053
54// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4