temperature_feature.cc
Go to the documentation of this file.
1 // BSD License
2 //
3 // Copyright (c) 2021, Ascent Robotics, Inc.
4 // All rights reserved.
5 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of Ascent Robotics, Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 
30 // Author Thomas Kostas/thomas.kostas@ascent.ai
31 
33 
34 #include <optional>
35 
36 #include <sensors/sensors.h>
37 
38 namespace
39 {
40 std::optional<double> read_subfeature(const sensors_chip_name* chip_name,
41  const sensors_subfeature* subfeature)
42 {
43  double value_read;
44  int return_value =
45  sensors_get_value(chip_name, subfeature->number, &value_read);
46  if (return_value < 0)
47  {
48  return std::nullopt;
49  }
50  else
51  {
52  return value_read;
53  }
54 }
55 } // namespace
56 
58 {
59 std::optional<TemperatureFeature>
60 TemperatureFeature::make_temp_feature(const sensors_chip_name* chip_name,
61  const sensors_feature* feature,
62  double default_critical_temp,
63  double defaut_max_temp)
64 {
65  if (feature->type == SENSORS_FEATURE_TEMP)
66  {
67  return { TemperatureFeature(
68  chip_name, feature, default_critical_temp, defaut_max_temp) };
69  }
70  else
71  {
72  return std::nullopt;
73  }
74 }
75 
76 TemperatureFeature::TemperatureFeature(const sensors_chip_name* chip_name,
77  const sensors_feature* feature,
78  double default_critical_temp,
79  double defaut_max_temp)
80 {
81  sensors_subfeature const* subfeature;
82  int subf_num = 0;
83  label_ = std::string(sensors_get_label(chip_name, feature));
84  while ((subfeature = sensors_get_all_subfeatures(
85  chip_name, feature, &subf_num)) != nullptr)
86  {
87  // A not readable feature has no interrest for us
88  if (subfeature->flags & SENSORS_MODE_R)
89  {
90  if (subfeature->type == SENSORS_SUBFEATURE_TEMP_INPUT)
91  {
92  input_temp_subfeature_ = subfeature;
93  }
94  else if (subfeature->type == SENSORS_SUBFEATURE_TEMP_CRIT)
95  {
96  auto crit_read = read_subfeature(chip_name, subfeature);
97  crit_temp_ = crit_read.value_or(default_critical_temp);
98  }
99  else if (subfeature->type == SENSORS_SUBFEATURE_TEMP_MAX)
100  {
101  auto max_read = read_subfeature(chip_name, subfeature);
102  max_temp_ = max_read.value_or(defaut_max_temp);
103  }
104  }
105  }
106 }
107 
108 std::string TemperatureFeature::get_label() const
109 {
110  return label_;
111 }
112 
114  const sensors_chip_name* chip_name) const
115 {
116  temperature_info ret_info;
117  if (auto temp_read = read_subfeature(chip_name, input_temp_subfeature_))
118  {
119  ret_info.temperature_ = temp_read.value();
120  ret_info.is_ok_ = (temp_read.value() < max_temp_);
121  ret_info.label_ = label_;
122  }
123  return ret_info;
124 }
125 
126 } // namespace cpu_temperature_diagnostics
TemperatureFeature(const sensors_chip_name *chip_name, const sensors_feature *feature, double default_critical_temp, double defaut_max_temp)
temperature_info read_temperature_info(const sensors_chip_name *chip_name) const
static std::optional< TemperatureFeature > make_temp_feature(const sensors_chip_name *chip_name, const sensors_feature *feature, double default_critical_temp=100, double defaut_max_temp=85)


cpu_temperature_diagnostics
Author(s):
autogenerated on Mon Feb 28 2022 22:08:46