1*de54f486SWilly Tu /* 2*de54f486SWilly Tu // Copyright (c) 2018 Intel Corporation 3*de54f486SWilly Tu // 4*de54f486SWilly Tu // Licensed under the Apache License, Version 2.0 (the "License"); 5*de54f486SWilly Tu // you may not use this file except in compliance with the License. 6*de54f486SWilly Tu // You may obtain a copy of the License at 7*de54f486SWilly Tu // 8*de54f486SWilly Tu // http://www.apache.org/licenses/LICENSE-2.0 9*de54f486SWilly Tu // 10*de54f486SWilly Tu // Unless required by applicable law or agreed to in writing, software 11*de54f486SWilly Tu // distributed under the License is distributed on an "AS IS" BASIS, 12*de54f486SWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*de54f486SWilly Tu // See the License for the specific language governing permissions and 14*de54f486SWilly Tu // limitations under the License. 15*de54f486SWilly Tu */ 16*de54f486SWilly Tu 17*de54f486SWilly Tu #include <boost/algorithm/string.hpp> 18*de54f486SWilly Tu #include <boost/bimap.hpp> 19*de54f486SWilly Tu #include <boost/container/flat_map.hpp> 20*de54f486SWilly Tu #include <cstdio> 21*de54f486SWilly Tu #include <cstring> 22*de54f486SWilly Tu #include <exception> 23*de54f486SWilly Tu #include <filesystem> 24*de54f486SWilly Tu #include <ipmid/api.hpp> 25*de54f486SWilly Tu #include <ipmid/types.hpp> 26*de54f486SWilly Tu #include <map> 27*de54f486SWilly Tu #include <phosphor-logging/log.hpp> 28*de54f486SWilly Tu #include <sdbusplus/bus/match.hpp> 29*de54f486SWilly Tu #include <string> 30*de54f486SWilly Tu #include <vector> 31*de54f486SWilly Tu 32*de54f486SWilly Tu #pragma once 33*de54f486SWilly Tu 34*de54f486SWilly Tu static constexpr bool debug = false; 35*de54f486SWilly Tu 36*de54f486SWilly Tu struct CmpStrVersion 37*de54f486SWilly Tu { 38*de54f486SWilly Tu bool operator()(std::string a, std::string b) const 39*de54f486SWilly Tu { 40*de54f486SWilly Tu return strverscmp(a.c_str(), b.c_str()) < 0; 41*de54f486SWilly Tu } 42*de54f486SWilly Tu }; 43*de54f486SWilly Tu 44*de54f486SWilly Tu using SensorSubTree = boost::container::flat_map< 45*de54f486SWilly Tu std::string, 46*de54f486SWilly Tu boost::container::flat_map<std::string, std::vector<std::string>>, 47*de54f486SWilly Tu CmpStrVersion>; 48*de54f486SWilly Tu 49*de54f486SWilly Tu using SensorNumMap = boost::bimap<int, std::string>; 50*de54f486SWilly Tu 51*de54f486SWilly Tu static constexpr uint16_t maxSensorsPerLUN = 255; 52*de54f486SWilly Tu static constexpr uint16_t maxIPMISensors = (maxSensorsPerLUN * 3); 53*de54f486SWilly Tu static constexpr uint16_t lun1Sensor0 = 0x100; 54*de54f486SWilly Tu static constexpr uint16_t lun3Sensor0 = 0x300; 55*de54f486SWilly Tu static constexpr uint16_t invalidSensorNumber = 0xFFFF; 56*de54f486SWilly Tu static constexpr uint8_t reservedSensorNumber = 0xFF; 57*de54f486SWilly Tu 58*de54f486SWilly Tu namespace details 59*de54f486SWilly Tu { 60*de54f486SWilly Tu bool getSensorSubtree(std::shared_ptr<SensorSubTree>& subtree); 61*de54f486SWilly Tu 62*de54f486SWilly Tu bool getSensorNumMap(std::shared_ptr<SensorNumMap>& sensorNumMap); 63*de54f486SWilly Tu } // namespace details 64*de54f486SWilly Tu 65*de54f486SWilly Tu bool getSensorSubtree(SensorSubTree& subtree); 66*de54f486SWilly Tu 67*de54f486SWilly Tu struct CmpStr 68*de54f486SWilly Tu { 69*de54f486SWilly Tu bool operator()(const char* a, const char* b) const 70*de54f486SWilly Tu { 71*de54f486SWilly Tu return std::strcmp(a, b) < 0; 72*de54f486SWilly Tu } 73*de54f486SWilly Tu }; 74*de54f486SWilly Tu 75*de54f486SWilly Tu enum class SensorTypeCodes : uint8_t 76*de54f486SWilly Tu { 77*de54f486SWilly Tu reserved = 0x0, 78*de54f486SWilly Tu temperature = 0x1, 79*de54f486SWilly Tu voltage = 0x2, 80*de54f486SWilly Tu current = 0x3, 81*de54f486SWilly Tu fan = 0x4, 82*de54f486SWilly Tu other = 0xB, 83*de54f486SWilly Tu }; 84*de54f486SWilly Tu 85*de54f486SWilly Tu const static boost::container::flat_map<const char*, SensorTypeCodes, CmpStr> 86*de54f486SWilly Tu sensorTypes{{{"temperature", SensorTypeCodes::temperature}, 87*de54f486SWilly Tu {"voltage", SensorTypeCodes::voltage}, 88*de54f486SWilly Tu {"current", SensorTypeCodes::current}, 89*de54f486SWilly Tu {"fan_tach", SensorTypeCodes::fan}, 90*de54f486SWilly Tu {"fan_pwm", SensorTypeCodes::fan}, 91*de54f486SWilly Tu {"power", SensorTypeCodes::other}}}; 92*de54f486SWilly Tu 93*de54f486SWilly Tu std::string getSensorTypeStringFromPath(const std::string& path); 94*de54f486SWilly Tu 95*de54f486SWilly Tu uint8_t getSensorTypeFromPath(const std::string& path); 96*de54f486SWilly Tu 97*de54f486SWilly Tu uint16_t getSensorNumberFromPath(const std::string& path); 98*de54f486SWilly Tu 99*de54f486SWilly Tu uint8_t getSensorEventTypeFromPath(const std::string& path); 100*de54f486SWilly Tu 101*de54f486SWilly Tu std::string getPathFromSensorNumber(uint16_t sensorNum); 102*de54f486SWilly Tu 103*de54f486SWilly Tu namespace ipmi 104*de54f486SWilly Tu { 105*de54f486SWilly Tu std::map<std::string, std::vector<std::string>> 106*de54f486SWilly Tu getObjectInterfaces(const char* path); 107*de54f486SWilly Tu 108*de54f486SWilly Tu std::map<std::string, Value> getEntityManagerProperties(const char* path, 109*de54f486SWilly Tu const char* interface); 110*de54f486SWilly Tu 111*de54f486SWilly Tu const std::string* getSensorConfigurationInterface( 112*de54f486SWilly Tu const std::map<std::string, std::vector<std::string>>& 113*de54f486SWilly Tu sensorInterfacesResponse); 114*de54f486SWilly Tu 115*de54f486SWilly Tu void updateIpmiFromAssociation(const std::string& path, 116*de54f486SWilly Tu const DbusInterfaceMap& sensorMap, 117*de54f486SWilly Tu uint8_t& entityId, uint8_t& entityInstance); 118*de54f486SWilly Tu } // namespace ipmi 119