blob: c3f8984df7a5e72122ebfaa4657f030437a61713 [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 Spinler28fa1b62018-01-18 13:15:02 -060097size_t RecordManager::getRawRecordID(
98 const std::vector<uint8_t>& data) const
99{
100 if (data.size() != RAW_RECORD_SIZE)
101 {
102 log<level::ERR>("Invalid INPUT_HISTORY size",
103 entry("SIZE=%d", data.size()));
104 throw InvalidRecordException{};
105 }
106
107 return data[RAW_RECORD_ID_OFFSET];
108}
109
110Record RecordManager::createRecord(const std::vector<uint8_t>& data)
111{
112 //The raw record format is:
113 // 0xAABBCCDDEE
114 //
115 // where:
116 // 0xAA = sequence ID
117 // 0xBBCC = average power in linear format (0xCC = MSB)
118 // 0xDDEE = maximum power in linear format (0xEE = MSB)
119 auto id = getRawRecordID(data);
120
121 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(
122 std::chrono::system_clock::now().time_since_epoch()).count();
123
124 auto val = static_cast<uint16_t>(data[2]) << 8 | data[1];
125 auto averagePower = linearToInteger(val);
126
127 val = static_cast<uint16_t>(data[4]) << 8 | data[3];
128 auto maxPower = linearToInteger(val);
129
130 return Record{id, time, averagePower, maxPower};
131}
132
Matt Spinlere710d182018-01-18 13:06:17 -0600133int64_t RecordManager::linearToInteger(uint16_t data)
134{
135 //The exponent is the first 5 bits, followed by 11 bits of mantissa.
136 int8_t exponent = (data & 0xF800) >> 11;
137 int16_t mantissa = (data & 0x07FF);
138
139 //If exponent's MSB on, then it's negative.
140 //Convert from two's complement.
141 if (exponent & 0x10)
142 {
143 exponent = (~exponent) & 0x1F;
144 exponent = (exponent + 1) * -1;
145 }
146
147 //If mantissa's MSB on, then it's negative.
148 //Convert from two's complement.
149 if (mantissa & 0x400)
150 {
151 mantissa = (~mantissa) & 0x07FF;
152 mantissa = (mantissa + 1) * -1;
153 }
154
155 auto value = static_cast<float>(mantissa) * pow(2, exponent);
156 return value;
157}
Matt Spinlerd7abf362018-01-18 12:40:02 -0600158
159}
160}
161}