1 /** 2 * Copyright © 2017 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 */ 16 #include "record_manager.hpp" 17 18 #include <math.h> 19 20 #include <phosphor-logging/lg2.hpp> 21 22 #include <chrono> 23 24 namespace phosphor 25 { 26 namespace power 27 { 28 namespace history 29 { 30 31 bool RecordManager::add(const std::vector<uint8_t>& rawRecord) 32 { 33 if (rawRecord.size() == 0) 34 { 35 // The PS has no data - either the power supply just started up, 36 // or it just got a SYNC. Clear the history. 37 records.clear(); 38 return true; 39 } 40 41 try 42 { 43 // Peek at the ID to see if more processing is needed. 44 auto id = getRawRecordID(rawRecord); 45 46 if (!records.empty()) 47 { 48 auto previousID = std::get<recIDPos>(records.front()); 49 50 // Already have this record. Done. 51 if (previousID == id) 52 { 53 return false; 54 } 55 56 // Check that the sequence ID is in order. 57 // If not, clear out current list. 58 if ((previousID + 1) != id) 59 { 60 // If it just rolled over from 0xFF to 0x00, then no 61 // need to clear. If we see a 0 seemingly out of nowhere, 62 // then it was a sync so clear the old records. 63 auto rolledOver = (previousID == lastSequenceID) && 64 (id == FIRST_SEQUENCE_ID); 65 66 if (!rolledOver) 67 { 68 if (id != FIRST_SEQUENCE_ID) 69 { 70 lg2::info( 71 "Noncontiguous INPUT_HISTORY sequence ID " 72 "found. Clearing old entries. OLD_ID={OLD_ID}, " 73 "NEW_ID={NEW_ID}", 74 "OLD_ID", previousID, "NEW_ID", id); 75 } 76 records.clear(); 77 } 78 } 79 } 80 81 #pragma GCC diagnostic push 82 #pragma GCC diagnostic ignored "-Wpessimizing-move" 83 #ifdef __clang__ 84 #pragma clang diagnostic ignored "-Wpessimizing-move" 85 #endif 86 records.push_front(std::move(createRecord(rawRecord))); 87 #pragma GCC diagnostic pop 88 89 // If no more should be stored, prune the oldest 90 if (records.size() > maxRecords) 91 { 92 records.pop_back(); 93 } 94 } 95 catch (const InvalidRecordException& e) 96 { 97 return false; 98 } 99 100 return true; 101 } 102 103 auto RecordManager::getAverageRecords() -> DBusRecordList 104 { 105 DBusRecordList list; 106 107 for (const auto& r : records) 108 { 109 list.emplace_back(std::get<recTimePos>(r), std::get<recAvgPos>(r)); 110 } 111 112 return list; 113 } 114 115 auto RecordManager::getMaximumRecords() -> DBusRecordList 116 { 117 DBusRecordList list; 118 119 for (const auto& r : records) 120 { 121 list.emplace_back(std::get<recTimePos>(r), std::get<recMaxPos>(r)); 122 } 123 124 return list; 125 } 126 127 size_t RecordManager::getRawRecordID(const std::vector<uint8_t>& data) const 128 { 129 if (data.size() != RAW_RECORD_SIZE) 130 { 131 lg2::error("Invalid INPUT_HISTORY size {SIZE}", "SIZE", data.size()); 132 throw InvalidRecordException{}; 133 } 134 135 return data[RAW_RECORD_ID_OFFSET]; 136 } 137 138 Record RecordManager::createRecord(const std::vector<uint8_t>& data) 139 { 140 // The raw record format is: 141 // 0xAABBCCDDEE 142 // 143 // where: 144 // 0xAA = sequence ID 145 // 0xBBCC = average power in linear format (0xCC = MSB) 146 // 0xDDEE = maximum power in linear format (0xEE = MSB) 147 auto id = getRawRecordID(data); 148 149 auto time = std::chrono::duration_cast<std::chrono::milliseconds>( 150 std::chrono::system_clock::now().time_since_epoch()) 151 .count(); 152 153 auto val = static_cast<uint16_t>(data[2]) << 8 | data[1]; 154 auto averagePower = linearToInteger(val); 155 156 val = static_cast<uint16_t>(data[4]) << 8 | data[3]; 157 auto maxPower = linearToInteger(val); 158 159 return Record{id, time, averagePower, maxPower}; 160 } 161 162 int64_t RecordManager::linearToInteger(uint16_t data) 163 { 164 // The exponent is the first 5 bits, followed by 11 bits of mantissa. 165 int8_t exponent = (data & 0xF800) >> 11; 166 int16_t mantissa = (data & 0x07FF); 167 168 // If exponent's MSB on, then it's negative. 169 // Convert from two's complement. 170 if (exponent & 0x10) 171 { 172 exponent = (~exponent) & 0x1F; 173 exponent = (exponent + 1) * -1; 174 } 175 176 // If mantissa's MSB on, then it's negative. 177 // Convert from two's complement. 178 if (mantissa & 0x400) 179 { 180 mantissa = (~mantissa) & 0x07FF; 181 mantissa = (mantissa + 1) * -1; 182 } 183 184 auto value = static_cast<float>(mantissa) * pow(2, exponent); 185 return value; 186 } 187 188 } // namespace history 189 } // namespace power 190 } // namespace phosphor 191