Commit e705789f authored by Michael Ott's avatar Michael Ott

Fix casting bug in LenovoXCC::readSingleEnergyRaw(), code cleanup in LenovoXCC

parent 23c0e3df
......@@ -90,22 +90,14 @@ int LenovoXCC::getBulkPower(std::vector<reading_t> &readings) {
}
if ((len == 208) && (buf[0] == 0x32) && (buf[1] == 0x00)) {
uint32_t ts1;
uint16_t ts2;
memcpy(&ts1, &buf[2], sizeof(ts1));
memcpy(&ts2, &buf[6], sizeof(ts2));
uint64_t timeStamp = S_TO_NS(ts1);
if (ts2 < 1000) {
timeStamp += MS_TO_NS(ts2);
}
timeStamp += _drift;
uint64_t timeStamp = extractTimestamp(&buf[2]) + _drift;
reading_t r = {0,0};
readings.resize(100, r);
uint16_t *buf16 = (uint16_t *)&buf[8];
for (int i=0; i<100; i++) {
reading_t r;
r.timestamp = timeStamp + MS_TO_NS(i * 10);
r.value = buf16[i];
readings.push_back(r);
readings[i].timestamp = timeStamp + MS_TO_NS(i * 10);
readings[i].value = buf16[i];
}
return 0;
}
......@@ -127,30 +119,20 @@ int LenovoXCC::getBulkEnergy(std::vector<reading_t> &readings) {
}
if ((len == 212) && (buf[0] == 0x32) && (buf[1] == 0x00)) {
uint32_t ts1;
uint16_t ts2;
memcpy(&ts1, &buf[2], sizeof(ts1));
memcpy(&ts2, &buf[6], sizeof(ts2));
uint64_t timeStamp = S_TO_NS(ts1);
if (ts2 < 1000) {
timeStamp += MS_TO_NS(ts2);
}
timeStamp += _drift;
uint64_t timeStamp = extractTimestamp(&buf[2]) + _drift;
uint32_t baseEnergy;
memcpy(&baseEnergy, &buf[8], sizeof(baseEnergy));
reading_t r;
r.timestamp = timeStamp;
r.value = baseEnergy;
readings.push_back(r);
reading_t r = {0,0};
readings.resize(101, r); // This is really 101!
readings[0].timestamp = timeStamp;
readings[0].value = baseEnergy;
uint16_t *buf16 = (uint16_t *)&buf[12];
for (int i=1; i<=100; i++) {
reading_t r;
r.timestamp = timeStamp + MS_TO_NS(i * 10);
r.value = baseEnergy + buf16[i-1];
readings.push_back(r);
for (int i=1; i<101; i++) {
readings[i].timestamp = timeStamp + MS_TO_NS(i * 10);
readings[i].value = baseEnergy + buf16[i-1];
}
return 0;
}
......@@ -159,7 +141,18 @@ int LenovoXCC::getBulkEnergy(std::vector<reading_t> &readings) {
return 1;
}
uint64_t LenovoXCC::extractTimestamp(const uint8_t *buf) {
uint64_t timestamp = 0;
uint32_t ts1;
uint16_t ts2;
memcpy(&ts1, &buf[0], sizeof(ts1));
memcpy(&ts2, &buf[4], sizeof(ts2));
timestamp = S_TO_NS(ts1);
if (ts2 < 1000) {
timestamp += MS_TO_NS(ts2);
}
return timestamp;
}
int LenovoXCC::readSingleEnergyRaw(uint64_t &timeStamp, uint64_t &energy) {
uint8_t buf[256];
......@@ -173,20 +166,13 @@ int LenovoXCC::readSingleEnergyRaw(uint64_t &timeStamp, uint64_t &energy) {
}
if ((len == 16) && (buf[0] == 0x32) && (buf[1] == 0x00)) {
uint32_t ts1;
uint16_t ts2;
memcpy(&ts1, &buf[10], sizeof(ts1));
memcpy(&ts2, &buf[14], sizeof(ts2));
timeStamp = S_TO_NS(ts1);
if (ts2 < 1000) {
timeStamp += MS_TO_NS(ts2);
}
timeStamp = extractTimestamp(&buf[10]);
uint32_t joules;
uint16_t mJoules;
memcpy(&joules, &buf[4], sizeof(joules));
memcpy(&mJoules, &buf[8], sizeof(mJoules));
energy = ((uint32_t)joules) * 1000;
energy = ((uint64_t)joules) * 1000;
if (mJoules < 1000) {
energy += mJoules;
}
......@@ -290,21 +276,10 @@ uint64_t LenovoXCC::readDatastoreTimestamp() {
return 0;
}
if (len >= 20) {
uint32_t ts1;
uint16_t ts2;
uint64_t ts;
uint64_t index;
memcpy(&ts1, &buf[7], sizeof(ts1));
memcpy(&ts2, &buf[11], sizeof(ts2));
memcpy(&index, &buf[15], sizeof(index));
ts = (uint64_t)ts1 * 1000 + index * 10;
if (ts2 < 1000) {
ts += ts2;
}
ts = MS_TO_NS(ts);
return ts;
if ((len == 23) && (buf[0] == 0x90) && (buf[1] == 0x00)){
uint64_t startedIndex;
memcpy(&startedIndex, &buf[15], sizeof(startedIndex));
return extractTimestamp(&buf[7]) + MS_TO_NS(startedIndex * 10);
} else {
return 0;
}
......
......@@ -25,12 +25,13 @@ class LenovoXCC {
int getBulkEnergy(std::vector<reading_t> &readings);
private:
uint64_t extractTimestamp(const uint8_t *buf);
int readSingleEnergyRaw(uint64_t &timeStamp, uint64_t &energy);
int getDrift();
int openDatastore();
int closeDatastore();
uint64_t readDatastoreTimestamp();
int readDatastoreRange(uint32_t offset, uint16_t count, uint8_t *buf, uint16_t bufLen);
int readSingleEnergyRaw(uint64_t &timeStamp, uint64_t &energy);
int getDrift();
IPMIHost *_host;
uint32_t _handle;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment