blob: f87c59a13f41a2b506c3ab7ded1f1c83de1aff67 [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
Anwaar Hadi1f0193f2025-05-13 21:05:12 +000020#include <phosphor-logging/lg2.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -050021
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 Spinler8168d282018-01-18 13:24:06 -060031bool RecordManager::add(const std::vector<uint8_t>& rawRecord)
32{
33 if (rawRecord.size() == 0)
34 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050035 // The PS has no data - either the power supply just started up,
36 // or it just got a SYNC. Clear the history.
Matt Spinler8168d282018-01-18 13:24:06 -060037 records.clear();
38 return true;
39 }
40
41 try
42 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050043 // Peek at the ID to see if more processing is needed.
Matt Spinler8168d282018-01-18 13:24:06 -060044 auto id = getRawRecordID(rawRecord);
45
46 if (!records.empty())
47 {
48 auto previousID = std::get<recIDPos>(records.front());
49
Matt Spinlerf0f02b92018-10-25 16:12:43 -050050 // Already have this record. Done.
Matt Spinler8168d282018-01-18 13:24:06 -060051 if (previousID == id)
52 {
53 return false;
54 }
55
Matt Spinlerf0f02b92018-10-25 16:12:43 -050056 // Check that the sequence ID is in order.
57 // If not, clear out current list.
Matt Spinler8168d282018-01-18 13:24:06 -060058 if ((previousID + 1) != id)
59 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050060 // 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.
Patrick Williams48781ae2023-05-10 07:50:50 -050063 auto rolledOver = (previousID == lastSequenceID) &&
64 (id == FIRST_SEQUENCE_ID);
Matt Spinler8168d282018-01-18 13:24:06 -060065
66 if (!rolledOver)
67 {
68 if (id != FIRST_SEQUENCE_ID)
69 {
Anwaar Hadi1f0193f2025-05-13 21:05:12 +000070 lg2::info(
Matt Spinlerf0f02b92018-10-25 16:12:43 -050071 "Noncontiguous INPUT_HISTORY sequence ID "
Anwaar Hadi1f0193f2025-05-13 21:05:12 +000072 "found. Clearing old entries. OLD_ID={OLD_ID}, "
73 "NEW_ID={NEW_ID}",
74 "OLD_ID", previousID, "NEW_ID", id);
Matt Spinler8168d282018-01-18 13:24:06 -060075 }
76 records.clear();
77 }
78 }
79 }
80
Jayanth Othayothf9886b92024-12-07 01:26:08 -060081#pragma GCC diagnostic push
82#pragma GCC diagnostic ignored "-Wpessimizing-move"
83#ifdef __clang__
84#pragma clang diagnostic ignored "-Wpessimizing-move"
85#endif
Matt Spinler8168d282018-01-18 13:24:06 -060086 records.push_front(std::move(createRecord(rawRecord)));
Jayanth Othayothf9886b92024-12-07 01:26:08 -060087#pragma GCC diagnostic pop
Matt Spinler8168d282018-01-18 13:24:06 -060088
Matt Spinlerf0f02b92018-10-25 16:12:43 -050089 // If no more should be stored, prune the oldest
Matt Spinler8168d282018-01-18 13:24:06 -060090 if (records.size() > maxRecords)
91 {
92 records.pop_back();
93 }
94 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050095 catch (const InvalidRecordException& e)
Matt Spinler8168d282018-01-18 13:24:06 -060096 {
97 return false;
98 }
99
100 return true;
101}
102
Matt Spinlerc3414382018-01-18 13:49:16 -0600103auto RecordManager::getAverageRecords() -> DBusRecordList
104{
105 DBusRecordList list;
106
107 for (const auto& r : records)
108 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500109 list.emplace_back(std::get<recTimePos>(r), std::get<recAvgPos>(r));
Matt Spinlerc3414382018-01-18 13:49:16 -0600110 }
111
112 return list;
113}
114
115auto RecordManager::getMaximumRecords() -> DBusRecordList
116{
117 DBusRecordList list;
118
119 for (const auto& r : records)
120 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500121 list.emplace_back(std::get<recTimePos>(r), std::get<recMaxPos>(r));
Matt Spinlerc3414382018-01-18 13:49:16 -0600122 }
123
124 return list;
125}
126
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500127size_t RecordManager::getRawRecordID(const std::vector<uint8_t>& data) const
Matt Spinler28fa1b62018-01-18 13:15:02 -0600128{
129 if (data.size() != RAW_RECORD_SIZE)
130 {
Anwaar Hadi1f0193f2025-05-13 21:05:12 +0000131 lg2::error("Invalid INPUT_HISTORY size {SIZE}", "SIZE", data.size());
Matt Spinler28fa1b62018-01-18 13:15:02 -0600132 throw InvalidRecordException{};
133 }
134
135 return data[RAW_RECORD_ID_OFFSET];
136}
137
138Record RecordManager::createRecord(const std::vector<uint8_t>& data)
139{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500140 // The raw record format is:
Matt Spinler28fa1b62018-01-18 13:15:02 -0600141 // 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>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500150 std::chrono::system_clock::now().time_since_epoch())
151 .count();
Matt Spinler28fa1b62018-01-18 13:15:02 -0600152
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
Matt Spinlere710d182018-01-18 13:06:17 -0600162int64_t RecordManager::linearToInteger(uint16_t data)
163{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500164 // The exponent is the first 5 bits, followed by 11 bits of mantissa.
Matt Spinlere710d182018-01-18 13:06:17 -0600165 int8_t exponent = (data & 0xF800) >> 11;
166 int16_t mantissa = (data & 0x07FF);
167
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500168 // If exponent's MSB on, then it's negative.
169 // Convert from two's complement.
Matt Spinlere710d182018-01-18 13:06:17 -0600170 if (exponent & 0x10)
171 {
172 exponent = (~exponent) & 0x1F;
173 exponent = (exponent + 1) * -1;
174 }
175
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500176 // If mantissa's MSB on, then it's negative.
177 // Convert from two's complement.
Matt Spinlere710d182018-01-18 13:06:17 -0600178 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}
Matt Spinlerd7abf362018-01-18 12:40:02 -0600187
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500188} // namespace history
189} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800190} // namespace phosphor