Willy Tu | de54f48 | 2021-01-26 15:59:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel 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 | |
| 17 | #include <boost/algorithm/string.hpp> |
| 18 | #include <boost/bimap.hpp> |
| 19 | #include <boost/container/flat_map.hpp> |
| 20 | #include <cstdio> |
| 21 | #include <cstring> |
| 22 | #include <exception> |
| 23 | #include <filesystem> |
| 24 | #include <ipmid/api.hpp> |
| 25 | #include <ipmid/types.hpp> |
| 26 | #include <map> |
| 27 | #include <phosphor-logging/log.hpp> |
| 28 | #include <sdbusplus/bus/match.hpp> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | #pragma once |
| 33 | |
| 34 | static constexpr bool debug = false; |
| 35 | |
| 36 | struct CmpStrVersion |
| 37 | { |
| 38 | bool operator()(std::string a, std::string b) const |
| 39 | { |
| 40 | return strverscmp(a.c_str(), b.c_str()) < 0; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | using SensorSubTree = boost::container::flat_map< |
| 45 | std::string, |
| 46 | boost::container::flat_map<std::string, std::vector<std::string>>, |
| 47 | CmpStrVersion>; |
| 48 | |
| 49 | using SensorNumMap = boost::bimap<int, std::string>; |
| 50 | |
| 51 | static constexpr uint16_t maxSensorsPerLUN = 255; |
| 52 | static constexpr uint16_t maxIPMISensors = (maxSensorsPerLUN * 3); |
| 53 | static constexpr uint16_t lun1Sensor0 = 0x100; |
| 54 | static constexpr uint16_t lun3Sensor0 = 0x300; |
| 55 | static constexpr uint16_t invalidSensorNumber = 0xFFFF; |
| 56 | static constexpr uint8_t reservedSensorNumber = 0xFF; |
| 57 | |
| 58 | namespace details |
| 59 | { |
| 60 | bool getSensorSubtree(std::shared_ptr<SensorSubTree>& subtree); |
| 61 | |
| 62 | bool getSensorNumMap(std::shared_ptr<SensorNumMap>& sensorNumMap); |
| 63 | } // namespace details |
| 64 | |
| 65 | bool getSensorSubtree(SensorSubTree& subtree); |
| 66 | |
| 67 | struct CmpStr |
| 68 | { |
| 69 | bool operator()(const char* a, const char* b) const |
| 70 | { |
| 71 | return std::strcmp(a, b) < 0; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | enum class SensorTypeCodes : uint8_t |
| 76 | { |
| 77 | reserved = 0x0, |
| 78 | temperature = 0x1, |
| 79 | voltage = 0x2, |
| 80 | current = 0x3, |
| 81 | fan = 0x4, |
| 82 | other = 0xB, |
| 83 | }; |
| 84 | |
| 85 | const static boost::container::flat_map<const char*, SensorTypeCodes, CmpStr> |
| 86 | sensorTypes{{{"temperature", SensorTypeCodes::temperature}, |
| 87 | {"voltage", SensorTypeCodes::voltage}, |
| 88 | {"current", SensorTypeCodes::current}, |
| 89 | {"fan_tach", SensorTypeCodes::fan}, |
| 90 | {"fan_pwm", SensorTypeCodes::fan}, |
| 91 | {"power", SensorTypeCodes::other}}}; |
| 92 | |
| 93 | std::string getSensorTypeStringFromPath(const std::string& path); |
| 94 | |
| 95 | uint8_t getSensorTypeFromPath(const std::string& path); |
| 96 | |
| 97 | uint16_t getSensorNumberFromPath(const std::string& path); |
| 98 | |
| 99 | uint8_t getSensorEventTypeFromPath(const std::string& path); |
| 100 | |
| 101 | std::string getPathFromSensorNumber(uint16_t sensorNum); |
| 102 | |
| 103 | namespace ipmi |
| 104 | { |
| 105 | std::map<std::string, std::vector<std::string>> |
| 106 | getObjectInterfaces(const char* path); |
| 107 | |
| 108 | std::map<std::string, Value> getEntityManagerProperties(const char* path, |
| 109 | const char* interface); |
| 110 | |
| 111 | const std::string* getSensorConfigurationInterface( |
| 112 | const std::map<std::string, std::vector<std::string>>& |
| 113 | sensorInterfacesResponse); |
| 114 | |
| 115 | void updateIpmiFromAssociation(const std::string& path, |
| 116 | const DbusInterfaceMap& sensorMap, |
| 117 | uint8_t& entityId, uint8_t& entityInstance); |
| 118 | } // namespace ipmi |