1de54f486SWilly Tu /*
2de54f486SWilly Tu // Copyright (c) 2018 Intel Corporation
3de54f486SWilly Tu //
4de54f486SWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
5de54f486SWilly Tu // you may not use this file except in compliance with the License.
6de54f486SWilly Tu // You may obtain a copy of the License at
7de54f486SWilly Tu //
8de54f486SWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
9de54f486SWilly Tu //
10de54f486SWilly Tu // Unless required by applicable law or agreed to in writing, software
11de54f486SWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
12de54f486SWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13de54f486SWilly Tu // See the License for the specific language governing permissions and
14de54f486SWilly Tu // limitations under the License.
15de54f486SWilly Tu */
16de54f486SWilly Tu 
17de54f486SWilly Tu #include <boost/algorithm/string.hpp>
18de54f486SWilly Tu #include <boost/bimap.hpp>
19de54f486SWilly Tu #include <boost/container/flat_map.hpp>
20de54f486SWilly Tu #include <cstdio>
21de54f486SWilly Tu #include <cstring>
22de54f486SWilly Tu #include <exception>
23de54f486SWilly Tu #include <filesystem>
24de54f486SWilly Tu #include <ipmid/api.hpp>
25de54f486SWilly Tu #include <ipmid/types.hpp>
26de54f486SWilly Tu #include <map>
27de54f486SWilly Tu #include <phosphor-logging/log.hpp>
28de54f486SWilly Tu #include <sdbusplus/bus/match.hpp>
29de54f486SWilly Tu #include <string>
30de54f486SWilly Tu #include <vector>
31de54f486SWilly Tu 
32de54f486SWilly Tu #pragma once
33de54f486SWilly Tu 
34de54f486SWilly Tu static constexpr bool debug = false;
35de54f486SWilly Tu 
36de54f486SWilly Tu struct CmpStrVersion
37de54f486SWilly Tu {
38de54f486SWilly Tu     bool operator()(std::string a, std::string b) const
39de54f486SWilly Tu     {
40de54f486SWilly Tu         return strverscmp(a.c_str(), b.c_str()) < 0;
41de54f486SWilly Tu     }
42de54f486SWilly Tu };
43de54f486SWilly Tu 
44de54f486SWilly Tu using SensorSubTree = boost::container::flat_map<
45de54f486SWilly Tu     std::string,
46de54f486SWilly Tu     boost::container::flat_map<std::string, std::vector<std::string>>,
47de54f486SWilly Tu     CmpStrVersion>;
48de54f486SWilly Tu 
49de54f486SWilly Tu using SensorNumMap = boost::bimap<int, std::string>;
50de54f486SWilly Tu 
51de54f486SWilly Tu static constexpr uint16_t maxSensorsPerLUN = 255;
52de54f486SWilly Tu static constexpr uint16_t maxIPMISensors = (maxSensorsPerLUN * 3);
53de54f486SWilly Tu static constexpr uint16_t lun1Sensor0 = 0x100;
54de54f486SWilly Tu static constexpr uint16_t lun3Sensor0 = 0x300;
55de54f486SWilly Tu static constexpr uint16_t invalidSensorNumber = 0xFFFF;
56de54f486SWilly Tu static constexpr uint8_t reservedSensorNumber = 0xFF;
57de54f486SWilly Tu 
58de54f486SWilly Tu namespace details
59de54f486SWilly Tu {
60a55c953dSJosh Lehan // Enable/disable the logging of stats instrumentation
61a55c953dSJosh Lehan static constexpr bool enableInstrumentation = false;
62a55c953dSJosh Lehan 
63a55c953dSJosh Lehan class IPMIStatsEntry
64a55c953dSJosh Lehan {
65a55c953dSJosh Lehan   private:
66a55c953dSJosh Lehan     int numReadings = 0;
67a55c953dSJosh Lehan     int numMissings = 0;
68a55c953dSJosh Lehan     int numStreakRead = 0;
69a55c953dSJosh Lehan     int numStreakMiss = 0;
70a55c953dSJosh Lehan     double minValue = 0.0;
71a55c953dSJosh Lehan     double maxValue = 0.0;
72a55c953dSJosh Lehan     std::string sensorName;
73a55c953dSJosh Lehan 
74a55c953dSJosh Lehan   public:
75a55c953dSJosh Lehan     const std::string& getName(void) const
76a55c953dSJosh Lehan     {
77a55c953dSJosh Lehan         return sensorName;
78a55c953dSJosh Lehan     }
79a55c953dSJosh Lehan 
80a55c953dSJosh Lehan     void updateName(std::string_view name)
81a55c953dSJosh Lehan     {
82a55c953dSJosh Lehan         sensorName = name;
83a55c953dSJosh Lehan     }
84a55c953dSJosh Lehan 
85a55c953dSJosh Lehan     // Returns true if this is the first successful reading
86a55c953dSJosh Lehan     // This is so the caller can log the coefficients used
87a55c953dSJosh Lehan     bool updateReading(double reading, int raw)
88a55c953dSJosh Lehan     {
89a55c953dSJosh Lehan         if constexpr (!enableInstrumentation)
90a55c953dSJosh Lehan         {
91a55c953dSJosh Lehan             return false;
92a55c953dSJosh Lehan         }
93a55c953dSJosh Lehan 
94a55c953dSJosh Lehan         bool first = ((numReadings == 0) && (numMissings == 0));
95a55c953dSJosh Lehan 
96a55c953dSJosh Lehan         // Sensors can use "nan" to indicate unavailable reading
97a55c953dSJosh Lehan         if (!(std::isfinite(reading)))
98a55c953dSJosh Lehan         {
99a55c953dSJosh Lehan             // Only show this if beginning a new streak
100a55c953dSJosh Lehan             if (numStreakMiss == 0)
101a55c953dSJosh Lehan             {
102a55c953dSJosh Lehan                 std::cerr << "IPMI sensor " << sensorName
103a55c953dSJosh Lehan                           << ": Missing reading, byte=" << raw
104a55c953dSJosh Lehan                           << ", Reading counts good=" << numReadings
105a55c953dSJosh Lehan                           << " miss=" << numMissings
106a55c953dSJosh Lehan                           << ", Prior good streak=" << numStreakRead << "\n";
107a55c953dSJosh Lehan             }
108a55c953dSJosh Lehan 
109a55c953dSJosh Lehan             numStreakRead = 0;
110a55c953dSJosh Lehan             ++numMissings;
111a55c953dSJosh Lehan             ++numStreakMiss;
112a55c953dSJosh Lehan 
113a55c953dSJosh Lehan             return first;
114a55c953dSJosh Lehan         }
115a55c953dSJosh Lehan 
116a55c953dSJosh Lehan         // Only show this if beginning a new streak and not the first time
117a55c953dSJosh Lehan         if ((numStreakRead == 0) && (numReadings != 0))
118a55c953dSJosh Lehan         {
119a55c953dSJosh Lehan             std::cerr << "IPMI sensor " << sensorName
120a55c953dSJosh Lehan                       << ": Recovered reading, value=" << reading
121a55c953dSJosh Lehan                       << " byte=" << raw
122a55c953dSJosh Lehan                       << ", Reading counts good=" << numReadings
123a55c953dSJosh Lehan                       << " miss=" << numMissings
124a55c953dSJosh Lehan                       << ", Prior miss streak=" << numStreakMiss << "\n";
125a55c953dSJosh Lehan         }
126a55c953dSJosh Lehan 
127a55c953dSJosh Lehan         // Initialize min/max if the first successful reading
128a55c953dSJosh Lehan         if (numReadings == 0)
129a55c953dSJosh Lehan         {
130a55c953dSJosh Lehan             std::cerr << "IPMI sensor " << sensorName
131a55c953dSJosh Lehan                       << ": First reading, value=" << reading << " byte=" << raw
132a55c953dSJosh Lehan                       << "\n";
133a55c953dSJosh Lehan 
134a55c953dSJosh Lehan             minValue = reading;
135a55c953dSJosh Lehan             maxValue = reading;
136a55c953dSJosh Lehan         }
137a55c953dSJosh Lehan 
138a55c953dSJosh Lehan         numStreakMiss = 0;
139a55c953dSJosh Lehan         ++numReadings;
140a55c953dSJosh Lehan         ++numStreakRead;
141a55c953dSJosh Lehan 
142a55c953dSJosh Lehan         // Only provide subsequent output if new min/max established
143a55c953dSJosh Lehan         if (reading < minValue)
144a55c953dSJosh Lehan         {
145a55c953dSJosh Lehan             std::cerr << "IPMI sensor " << sensorName
146a55c953dSJosh Lehan                       << ": Lowest reading, value=" << reading
147a55c953dSJosh Lehan                       << " byte=" << raw << "\n";
148a55c953dSJosh Lehan 
149a55c953dSJosh Lehan             minValue = reading;
150a55c953dSJosh Lehan         }
151a55c953dSJosh Lehan 
152a55c953dSJosh Lehan         if (reading > maxValue)
153a55c953dSJosh Lehan         {
154a55c953dSJosh Lehan             std::cerr << "IPMI sensor " << sensorName
155a55c953dSJosh Lehan                       << ": Highest reading, value=" << reading
156a55c953dSJosh Lehan                       << " byte=" << raw << "\n";
157a55c953dSJosh Lehan 
158a55c953dSJosh Lehan             maxValue = reading;
159a55c953dSJosh Lehan         }
160a55c953dSJosh Lehan 
161a55c953dSJosh Lehan         return first;
162a55c953dSJosh Lehan     }
163a55c953dSJosh Lehan };
164a55c953dSJosh Lehan 
165a55c953dSJosh Lehan class IPMIStatsTable
166a55c953dSJosh Lehan {
167a55c953dSJosh Lehan   private:
168a55c953dSJosh Lehan     std::vector<IPMIStatsEntry> entries;
169a55c953dSJosh Lehan 
170a55c953dSJosh Lehan   private:
171a55c953dSJosh Lehan     void padEntries(size_t index)
172a55c953dSJosh Lehan     {
173a55c953dSJosh Lehan         char hexbuf[16];
174a55c953dSJosh Lehan 
175a55c953dSJosh Lehan         // Pad vector until entries[index] becomes a valid index
176a55c953dSJosh Lehan         while (entries.size() <= index)
177a55c953dSJosh Lehan         {
178a55c953dSJosh Lehan             // As name not known yet, use human-readable hex as name
179a55c953dSJosh Lehan             IPMIStatsEntry newEntry;
180a55c953dSJosh Lehan             sprintf(hexbuf, "0x%02zX", entries.size());
181a55c953dSJosh Lehan             newEntry.updateName(hexbuf);
182a55c953dSJosh Lehan 
183a55c953dSJosh Lehan             entries.push_back(std::move(newEntry));
184a55c953dSJosh Lehan         }
185a55c953dSJosh Lehan     }
186a55c953dSJosh Lehan 
187a55c953dSJosh Lehan   public:
188a55c953dSJosh Lehan     void wipeTable(void)
189a55c953dSJosh Lehan     {
190a55c953dSJosh Lehan         entries.clear();
191a55c953dSJosh Lehan     }
192a55c953dSJosh Lehan 
193a55c953dSJosh Lehan     const std::string& getName(size_t index)
194a55c953dSJosh Lehan     {
195a55c953dSJosh Lehan         padEntries(index);
196a55c953dSJosh Lehan         return entries[index].getName();
197a55c953dSJosh Lehan     }
198a55c953dSJosh Lehan 
199a55c953dSJosh Lehan     void updateName(size_t index, std::string_view name)
200a55c953dSJosh Lehan     {
201a55c953dSJosh Lehan         padEntries(index);
202a55c953dSJosh Lehan         entries[index].updateName(name);
203a55c953dSJosh Lehan     }
204a55c953dSJosh Lehan 
205a55c953dSJosh Lehan     bool updateReading(size_t index, double reading, int raw)
206a55c953dSJosh Lehan     {
207a55c953dSJosh Lehan         padEntries(index);
208a55c953dSJosh Lehan         return entries[index].updateReading(reading, raw);
209a55c953dSJosh Lehan     }
210a55c953dSJosh Lehan };
211a55c953dSJosh Lehan 
212d2afd054SHao Jiang // Store information for threshold sensors and they are not used by VR
213d2afd054SHao Jiang // sensors. These objects are global singletons, used from a variety of places.
214a55c953dSJosh Lehan inline IPMIStatsTable sdrStatsTable;
215a55c953dSJosh Lehan 
2169a5b51e3SHao Jiang /**
2179a5b51e3SHao Jiang  * Search ObjectMapper for sensors and update them to subtree.
2189a5b51e3SHao Jiang  *
2199a5b51e3SHao Jiang  * The function will search for sensors under either
2209a5b51e3SHao Jiang  * /xyz/openbmc_project/sensors or /xyz/openbmc_project/extsensors. It will
2219a5b51e3SHao Jiang  * optionally search VR typed sensors under /xyz/openbmc_project/vr
2229a5b51e3SHao Jiang  *
2239a5b51e3SHao Jiang  * @return the updated amount of times any of "sensors" or "extsensors" sensor
2249a5b51e3SHao Jiang  * paths updated successfully, previous amount if all failed. The "vr"
2259a5b51e3SHao Jiang  * sensor path is optional, and does not participate in the return value.
2269a5b51e3SHao Jiang  */
227a8b5b26dSKuiying Wang uint16_t getSensorSubtree(std::shared_ptr<SensorSubTree>& subtree);
228de54f486SWilly Tu 
229de54f486SWilly Tu bool getSensorNumMap(std::shared_ptr<SensorNumMap>& sensorNumMap);
230de54f486SWilly Tu } // namespace details
231de54f486SWilly Tu 
232de54f486SWilly Tu bool getSensorSubtree(SensorSubTree& subtree);
233de54f486SWilly Tu 
234de54f486SWilly Tu struct CmpStr
235de54f486SWilly Tu {
236de54f486SWilly Tu     bool operator()(const char* a, const char* b) const
237de54f486SWilly Tu     {
238de54f486SWilly Tu         return std::strcmp(a, b) < 0;
239de54f486SWilly Tu     }
240de54f486SWilly Tu };
241de54f486SWilly Tu 
2422b42d7eeSScron Chang static constexpr size_t sensorTypeCodes = 0;
2432b42d7eeSScron Chang static constexpr size_t sensorEventTypeCodes = 1;
2442b42d7eeSScron Chang 
245de54f486SWilly Tu enum class SensorTypeCodes : uint8_t
246de54f486SWilly Tu {
247de54f486SWilly Tu     reserved = 0x0,
248de54f486SWilly Tu     temperature = 0x1,
249de54f486SWilly Tu     voltage = 0x2,
250de54f486SWilly Tu     current = 0x3,
251de54f486SWilly Tu     fan = 0x4,
252de54f486SWilly Tu     other = 0xB,
253*b8e5b164SScron Chang     memory = 0x0c,
254*b8e5b164SScron Chang     power_unit = 0x09,
255*b8e5b164SScron Chang     buttons = 0x14,
256*b8e5b164SScron Chang     watchdog2 = 0x23,
257de54f486SWilly Tu };
258de54f486SWilly Tu 
2592b42d7eeSScron Chang enum class SensorEventTypeCodes : uint8_t
2602b42d7eeSScron Chang {
2612b42d7eeSScron Chang     unspecified = 0x00,
2622b42d7eeSScron Chang     threshold = 0x01,
2632b42d7eeSScron Chang     sensorSpecified = 0x6f
2642b42d7eeSScron Chang };
2652b42d7eeSScron Chang 
2662b42d7eeSScron Chang const static boost::container::flat_map<
2672b42d7eeSScron Chang     const char*, std::pair<SensorTypeCodes, SensorEventTypeCodes>, CmpStr>
2682b42d7eeSScron Chang     sensorTypes{
2692b42d7eeSScron Chang         {{"temperature", std::make_pair(SensorTypeCodes::temperature,
2702b42d7eeSScron Chang                                         SensorEventTypeCodes::threshold)},
2712b42d7eeSScron Chang          {"voltage", std::make_pair(SensorTypeCodes::voltage,
2722b42d7eeSScron Chang                                     SensorEventTypeCodes::threshold)},
2732b42d7eeSScron Chang          {"current", std::make_pair(SensorTypeCodes::current,
2742b42d7eeSScron Chang                                     SensorEventTypeCodes::threshold)},
2752b42d7eeSScron Chang          {"fan_tach", std::make_pair(SensorTypeCodes::fan,
2762b42d7eeSScron Chang                                      SensorEventTypeCodes::threshold)},
2772b42d7eeSScron Chang          {"fan_pwm", std::make_pair(SensorTypeCodes::fan,
2782b42d7eeSScron Chang                                     SensorEventTypeCodes::threshold)},
2792b42d7eeSScron Chang          {"power", std::make_pair(SensorTypeCodes::other,
280*b8e5b164SScron Chang                                   SensorEventTypeCodes::threshold)},
281*b8e5b164SScron Chang          {"memory", std::make_pair(SensorTypeCodes::memory,
282*b8e5b164SScron Chang                                    SensorEventTypeCodes::sensorSpecified)},
283*b8e5b164SScron Chang          {"state", std::make_pair(SensorTypeCodes::power_unit,
284*b8e5b164SScron Chang                                   SensorEventTypeCodes::sensorSpecified)},
285*b8e5b164SScron Chang          {"buttons", std::make_pair(SensorTypeCodes::buttons,
286*b8e5b164SScron Chang                                     SensorEventTypeCodes::sensorSpecified)},
287*b8e5b164SScron Chang          {"watchdog", std::make_pair(SensorTypeCodes::watchdog2,
288*b8e5b164SScron Chang                                      SensorEventTypeCodes::sensorSpecified)}}};
289de54f486SWilly Tu 
290de54f486SWilly Tu std::string getSensorTypeStringFromPath(const std::string& path);
291de54f486SWilly Tu 
292de54f486SWilly Tu uint8_t getSensorTypeFromPath(const std::string& path);
293de54f486SWilly Tu 
294de54f486SWilly Tu uint16_t getSensorNumberFromPath(const std::string& path);
295de54f486SWilly Tu 
296de54f486SWilly Tu uint8_t getSensorEventTypeFromPath(const std::string& path);
297de54f486SWilly Tu 
298de54f486SWilly Tu std::string getPathFromSensorNumber(uint16_t sensorNum);
299de54f486SWilly Tu 
300de54f486SWilly Tu namespace ipmi
301de54f486SWilly Tu {
302de54f486SWilly Tu std::map<std::string, std::vector<std::string>>
303de54f486SWilly Tu     getObjectInterfaces(const char* path);
304de54f486SWilly Tu 
305de54f486SWilly Tu std::map<std::string, Value> getEntityManagerProperties(const char* path,
306de54f486SWilly Tu                                                         const char* interface);
307de54f486SWilly Tu 
308de54f486SWilly Tu const std::string* getSensorConfigurationInterface(
309de54f486SWilly Tu     const std::map<std::string, std::vector<std::string>>&
310de54f486SWilly Tu         sensorInterfacesResponse);
311de54f486SWilly Tu 
312de54f486SWilly Tu void updateIpmiFromAssociation(const std::string& path,
313de54f486SWilly Tu                                const DbusInterfaceMap& sensorMap,
314de54f486SWilly Tu                                uint8_t& entityId, uint8_t& entityInstance);
315de54f486SWilly Tu } // namespace ipmi
316