blob: 53e8fe31d9566abb040c7160d952240596d0183f [file] [log] [blame]
Matt Spinlerd7abf362018-01-18 12:40:02 -06001/**
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
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018#include <math.h>
19
Matt Spinlerf0f02b92018-10-25 16:12:43 -050020#include <phosphor-logging/log.hpp>
21
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060022#include <chrono>
23
Lei YUab093322019-10-09 16:43:22 +080024namespace phosphor
Matt Spinlerd7abf362018-01-18 12:40:02 -060025{
26namespace power
27{
28namespace history
29{
30
Matt Spinler28fa1b62018-01-18 13:15:02 -060031using namespace phosphor::logging;
32
Matt Spinler8168d282018-01-18 13:24:06 -060033bool RecordManager::add(const std::vector<uint8_t>& rawRecord)
34{
35 if (rawRecord.size() == 0)
36 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050037 // The PS has no data - either the power supply just started up,
38 // or it just got a SYNC. Clear the history.
Matt Spinler8168d282018-01-18 13:24:06 -060039 records.clear();
40 return true;
41 }
42
43 try
44 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050045 // Peek at the ID to see if more processing is needed.
Matt Spinler8168d282018-01-18 13:24:06 -060046 auto id = getRawRecordID(rawRecord);
47
48 if (!records.empty())
49 {
50 auto previousID = std::get<recIDPos>(records.front());
51
Matt Spinlerf0f02b92018-10-25 16:12:43 -050052 // Already have this record. Done.
Matt Spinler8168d282018-01-18 13:24:06 -060053 if (previousID == id)
54 {
55 return false;
56 }
57
Matt Spinlerf0f02b92018-10-25 16:12:43 -050058 // Check that the sequence ID is in order.
59 // If not, clear out current list.
Matt Spinler8168d282018-01-18 13:24:06 -060060 if ((previousID + 1) != id)
61 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050062 // If it just rolled over from 0xFF to 0x00, then no
63 // need to clear. If we see a 0 seemingly out of nowhere,
64 // then it was a sync so clear the old records.
Matt Spinler8168d282018-01-18 13:24:06 -060065 auto rolledOver =
Matt Spinlerf0f02b92018-10-25 16:12:43 -050066 (previousID == lastSequenceID) && (id == FIRST_SEQUENCE_ID);
Matt Spinler8168d282018-01-18 13:24:06 -060067
68 if (!rolledOver)
69 {
70 if (id != FIRST_SEQUENCE_ID)
71 {
72 log<level::INFO>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -050073 "Noncontiguous INPUT_HISTORY sequence ID "
74 "found. Clearing old entries",
75 entry("OLD_ID=%ld", previousID),
76 entry("NEW_ID=%ld", id));
Matt Spinler8168d282018-01-18 13:24:06 -060077 }
78 records.clear();
79 }
80 }
81 }
82
83 records.push_front(std::move(createRecord(rawRecord)));
84
Matt Spinlerf0f02b92018-10-25 16:12:43 -050085 // If no more should be stored, prune the oldest
Matt Spinler8168d282018-01-18 13:24:06 -060086 if (records.size() > maxRecords)
87 {
88 records.pop_back();
89 }
90 }
91 catch (InvalidRecordException& e)
92 {
93 return false;
94 }
95
96 return true;
97}
98
Matt Spinlerc3414382018-01-18 13:49:16 -060099auto RecordManager::getAverageRecords() -> DBusRecordList
100{
101 DBusRecordList list;
102
103 for (const auto& r : records)
104 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500105 list.emplace_back(std::get<recTimePos>(r), std::get<recAvgPos>(r));
Matt Spinlerc3414382018-01-18 13:49:16 -0600106 }
107
108 return list;
109}
110
111auto RecordManager::getMaximumRecords() -> DBusRecordList
112{
113 DBusRecordList list;
114
115 for (const auto& r : records)
116 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500117 list.emplace_back(std::get<recTimePos>(r), std::get<recMaxPos>(r));
Matt Spinlerc3414382018-01-18 13:49:16 -0600118 }
119
120 return list;
121}
122
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500123size_t RecordManager::getRawRecordID(const std::vector<uint8_t>& data) const
Matt Spinler28fa1b62018-01-18 13:15:02 -0600124{
125 if (data.size() != RAW_RECORD_SIZE)
126 {
127 log<level::ERR>("Invalid INPUT_HISTORY size",
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500128 entry("SIZE=%d", data.size()));
Matt Spinler28fa1b62018-01-18 13:15:02 -0600129 throw InvalidRecordException{};
130 }
131
132 return data[RAW_RECORD_ID_OFFSET];
133}
134
135Record RecordManager::createRecord(const std::vector<uint8_t>& data)
136{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500137 // The raw record format is:
Matt Spinler28fa1b62018-01-18 13:15:02 -0600138 // 0xAABBCCDDEE
139 //
140 // where:
141 // 0xAA = sequence ID
142 // 0xBBCC = average power in linear format (0xCC = MSB)
143 // 0xDDEE = maximum power in linear format (0xEE = MSB)
144 auto id = getRawRecordID(data);
145
146 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500147 std::chrono::system_clock::now().time_since_epoch())
148 .count();
Matt Spinler28fa1b62018-01-18 13:15:02 -0600149
150 auto val = static_cast<uint16_t>(data[2]) << 8 | data[1];
151 auto averagePower = linearToInteger(val);
152
153 val = static_cast<uint16_t>(data[4]) << 8 | data[3];
154 auto maxPower = linearToInteger(val);
155
156 return Record{id, time, averagePower, maxPower};
157}
158
Matt Spinlere710d182018-01-18 13:06:17 -0600159int64_t RecordManager::linearToInteger(uint16_t data)
160{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500161 // The exponent is the first 5 bits, followed by 11 bits of mantissa.
Matt Spinlere710d182018-01-18 13:06:17 -0600162 int8_t exponent = (data & 0xF800) >> 11;
163 int16_t mantissa = (data & 0x07FF);
164
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500165 // If exponent's MSB on, then it's negative.
166 // Convert from two's complement.
Matt Spinlere710d182018-01-18 13:06:17 -0600167 if (exponent & 0x10)
168 {
169 exponent = (~exponent) & 0x1F;
170 exponent = (exponent + 1) * -1;
171 }
172
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500173 // If mantissa's MSB on, then it's negative.
174 // Convert from two's complement.
Matt Spinlere710d182018-01-18 13:06:17 -0600175 if (mantissa & 0x400)
176 {
177 mantissa = (~mantissa) & 0x07FF;
178 mantissa = (mantissa + 1) * -1;
179 }
180
181 auto value = static_cast<float>(mantissa) * pow(2, exponent);
182 return value;
183}
Matt Spinlerd7abf362018-01-18 12:40:02 -0600184
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500185} // namespace history
186} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800187} // namespace phosphor