blob: ec6e9179510f6757f0354654226691214fcceebb [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 */
Matt Spinler28fa1b62018-01-18 13:15:02 -060016#include <chrono>
Matt Spinlere710d182018-01-18 13:06:17 -060017#include <math.h>
Matt Spinler28fa1b62018-01-18 13:15:02 -060018#include <phosphor-logging/log.hpp>
Matt Spinlerd7abf362018-01-18 12:40:02 -060019#include "record_manager.hpp"
20
21namespace witherspoon
22{
23namespace power
24{
25namespace history
26{
27
Matt Spinler28fa1b62018-01-18 13:15:02 -060028using namespace phosphor::logging;
29
Matt Spinler8168d282018-01-18 13:24:06 -060030bool RecordManager::add(const std::vector<uint8_t>& rawRecord)
31{
32 if (rawRecord.size() == 0)
33 {
34 //The PS has no data - either the power supply just started up,
35 //or it just got a SYNC. Clear the history.
36 records.clear();
37 return true;
38 }
39
40 try
41 {
42 //Peek at the ID to see if more processing is needed.
43 auto id = getRawRecordID(rawRecord);
44
45 if (!records.empty())
46 {
47 auto previousID = std::get<recIDPos>(records.front());
48
49 //Already have this record. Done.
50 if (previousID == id)
51 {
52 return false;
53 }
54
55 //Check that the sequence ID is in order.
56 //If not, clear out current list.
57 if ((previousID + 1) != id)
58 {
59 //If it just rolled over from 0xFF to 0x00, then no
60 //need to clear. If we see a 0 seemingly out of nowhere,
61 //then it was a sync so clear the old records.
62 auto rolledOver =
63 (previousID == lastSequenceID) &&
64 (id == FIRST_SEQUENCE_ID);
65
66 if (!rolledOver)
67 {
68 if (id != FIRST_SEQUENCE_ID)
69 {
70 log<level::INFO>(
71 "Noncontiguous INPUT_HISTORY sequence ID "
72 "found. Clearing old entries",
73 entry("OLD_ID=%ld", previousID),
74 entry("NEW_ID=%ld", id));
75 }
76 records.clear();
77 }
78 }
79 }
80
81 records.push_front(std::move(createRecord(rawRecord)));
82
83 //If no more should be stored, prune the oldest
84 if (records.size() > maxRecords)
85 {
86 records.pop_back();
87 }
88 }
89 catch (InvalidRecordException& e)
90 {
91 return false;
92 }
93
94 return true;
95}
96
Matt Spinlerc3414382018-01-18 13:49:16 -060097auto RecordManager::getAverageRecords() -> DBusRecordList
98{
99 DBusRecordList list;
100
101 for (const auto& r : records)
102 {
103 list.emplace_back(std::get<recTimePos>(r),
104 std::get<recAvgPos>(r));
105 }
106
107 return list;
108}
109
110auto RecordManager::getMaximumRecords() -> DBusRecordList
111{
112 DBusRecordList list;
113
114 for (const auto& r : records)
115 {
116 list.emplace_back(std::get<recTimePos>(r),
117 std::get<recMaxPos>(r));
118 }
119
120 return list;
121}
122
Matt Spinler28fa1b62018-01-18 13:15:02 -0600123size_t RecordManager::getRawRecordID(
124 const std::vector<uint8_t>& data) const
125{
126 if (data.size() != RAW_RECORD_SIZE)
127 {
128 log<level::ERR>("Invalid INPUT_HISTORY size",
129 entry("SIZE=%d", data.size()));
130 throw InvalidRecordException{};
131 }
132
133 return data[RAW_RECORD_ID_OFFSET];
134}
135
136Record RecordManager::createRecord(const std::vector<uint8_t>& data)
137{
138 //The raw record format is:
139 // 0xAABBCCDDEE
140 //
141 // where:
142 // 0xAA = sequence ID
143 // 0xBBCC = average power in linear format (0xCC = MSB)
144 // 0xDDEE = maximum power in linear format (0xEE = MSB)
145 auto id = getRawRecordID(data);
146
147 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(
148 std::chrono::system_clock::now().time_since_epoch()).count();
149
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{
161 //The exponent is the first 5 bits, followed by 11 bits of mantissa.
162 int8_t exponent = (data & 0xF800) >> 11;
163 int16_t mantissa = (data & 0x07FF);
164
165 //If exponent's MSB on, then it's negative.
166 //Convert from two's complement.
167 if (exponent & 0x10)
168 {
169 exponent = (~exponent) & 0x1F;
170 exponent = (exponent + 1) * -1;
171 }
172
173 //If mantissa's MSB on, then it's negative.
174 //Convert from two's complement.
175 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
185}
186}
187}