blob: 1fcd46341182cb1ddd515d7741d93ab54bc75703 [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
30size_t RecordManager::getRawRecordID(
31 const std::vector<uint8_t>& data) const
32{
33 if (data.size() != RAW_RECORD_SIZE)
34 {
35 log<level::ERR>("Invalid INPUT_HISTORY size",
36 entry("SIZE=%d", data.size()));
37 throw InvalidRecordException{};
38 }
39
40 return data[RAW_RECORD_ID_OFFSET];
41}
42
43Record RecordManager::createRecord(const std::vector<uint8_t>& data)
44{
45 //The raw record format is:
46 // 0xAABBCCDDEE
47 //
48 // where:
49 // 0xAA = sequence ID
50 // 0xBBCC = average power in linear format (0xCC = MSB)
51 // 0xDDEE = maximum power in linear format (0xEE = MSB)
52 auto id = getRawRecordID(data);
53
54 auto time = std::chrono::duration_cast<std::chrono::milliseconds>(
55 std::chrono::system_clock::now().time_since_epoch()).count();
56
57 auto val = static_cast<uint16_t>(data[2]) << 8 | data[1];
58 auto averagePower = linearToInteger(val);
59
60 val = static_cast<uint16_t>(data[4]) << 8 | data[3];
61 auto maxPower = linearToInteger(val);
62
63 return Record{id, time, averagePower, maxPower};
64}
65
Matt Spinlere710d182018-01-18 13:06:17 -060066int64_t RecordManager::linearToInteger(uint16_t data)
67{
68 //The exponent is the first 5 bits, followed by 11 bits of mantissa.
69 int8_t exponent = (data & 0xF800) >> 11;
70 int16_t mantissa = (data & 0x07FF);
71
72 //If exponent's MSB on, then it's negative.
73 //Convert from two's complement.
74 if (exponent & 0x10)
75 {
76 exponent = (~exponent) & 0x1F;
77 exponent = (exponent + 1) * -1;
78 }
79
80 //If mantissa's MSB on, then it's negative.
81 //Convert from two's complement.
82 if (mantissa & 0x400)
83 {
84 mantissa = (~mantissa) & 0x07FF;
85 mantissa = (mantissa + 1) * -1;
86 }
87
88 auto value = static_cast<float>(mantissa) * pow(2, exponent);
89 return value;
90}
Matt Spinlerd7abf362018-01-18 12:40:02 -060091
92}
93}
94}