finally works

This commit is contained in:
2023-04-06 22:35:23 +02:00
parent e4530544c2
commit 2968856d71
12 changed files with 184 additions and 154 deletions

View File

@ -27,54 +27,56 @@ void BpxBatteryHandler::doStartUp() {
void BpxBatteryHandler::doShutDown() {
// Perform a COM check on reboot
state = States::CHECK_COM;
setMode(MODE_OFF);
}
ReturnValue_t BpxBatteryHandler::buildNormalDeviceCommand(DeviceCommandId_t* id) {
*id = BpxBattery::GET_HK;
*id = bpxBat::GET_HK;
return buildCommandFromCommand(*id, nullptr, 0);
}
ReturnValue_t BpxBatteryHandler::buildTransitionDeviceCommand(DeviceCommandId_t* id) {
if (state == States::CHECK_COM) {
*id = BpxBattery::PING;
*id = bpxBat::PING;
return buildCommandFromCommand(*id, nullptr, 0);
}
return returnvalue::OK;
}
void BpxBatteryHandler::fillCommandAndReplyMap() {
using namespace BpxBattery;
using namespace bpxBat;
insertInCommandAndReplyMap(GET_HK, 1, &hkSet, GET_HK_REPLY_LEN);
insertInCommandAndReplyMap(BpxBattery::PING, 1, nullptr, PING_REPLY_LEN);
insertInCommandAndReplyMap(BpxBattery::REBOOT, 1, nullptr, 0);
insertInCommandAndReplyMap(BpxBattery::RESET_COUNTERS, 1, nullptr, EMPTY_REPLY_LEN);
insertInCommandAndReplyMap(BpxBattery::CONFIG_CMD, 1, nullptr, EMPTY_REPLY_LEN);
insertInCommandAndReplyMap(BpxBattery::CONFIG_GET, 1, &cfgSet, CONFIG_GET_REPLY_LEN);
insertInCommandAndReplyMap(bpxBat::PING, 1, nullptr, PING_REPLY_LEN);
insertInCommandAndReplyMap(bpxBat::REBOOT, 1, nullptr, 0);
insertInCommandAndReplyMap(bpxBat::RESET_COUNTERS, 1, nullptr, EMPTY_REPLY_LEN);
insertInCommandAndReplyMap(bpxBat::CONFIG_CMD, 1, nullptr, EMPTY_REPLY_LEN);
insertInCommandAndReplyMap(bpxBat::CONFIG_GET, 1, &cfgSet, CONFIG_GET_REPLY_LEN);
}
ReturnValue_t BpxBatteryHandler::buildCommandFromCommand(DeviceCommandId_t deviceCommand,
const uint8_t* commandData,
size_t commandDataLen) {
switch (deviceCommand) {
case (BpxBattery::GET_HK): {
cmdBuf[0] = BpxBattery::PORT_GET_HK;
case (bpxBat::GET_HK): {
cmdBuf[0] = bpxBat::PORT_GET_HK;
this->rawPacketLen = 1;
break;
}
case (BpxBattery::PING): {
case (bpxBat::PING): {
if (commandDataLen == 1 and commandData != nullptr) {
sentPingByte = commandData[0];
} else {
sentPingByte = BpxBattery::DEFAULT_PING_SENT_BYTE;
sentPingByte = bpxBat::DEFAULT_PING_SENT_BYTE;
}
cmdBuf[0] = BpxBattery::PORT_PING;
cmdBuf[0] = bpxBat::PORT_PING;
cmdBuf[1] = sentPingByte;
this->rawPacketLen = 2;
break;
}
case (BpxBattery::REBOOT): {
cmdBuf[0] = BpxBattery::PORT_REBOOT;
case (bpxBat::REBOOT): {
sif::info << "BPX BATT: Executing reboot command" << std::endl;
cmdBuf[0] = bpxBat::PORT_REBOOT;
cmdBuf[1] = 0x80;
cmdBuf[2] = 0x07;
cmdBuf[3] = 0x80;
@ -85,26 +87,26 @@ ReturnValue_t BpxBatteryHandler::buildCommandFromCommand(DeviceCommandId_t devic
triggerEvent(DeviceHandlerIF::DEVICE_WANTS_HARD_REBOOT);
break;
}
case (BpxBattery::RESET_COUNTERS): {
cmdBuf[0] = BpxBattery::PORT_RESET_COUNTERS;
cmdBuf[1] = BpxBattery::RESET_COUNTERS_MAGIC_VALUE;
case (bpxBat::RESET_COUNTERS): {
cmdBuf[0] = bpxBat::PORT_RESET_COUNTERS;
cmdBuf[1] = bpxBat::RESET_COUNTERS_MAGIC_VALUE;
this->rawPacketLen = 2;
break;
}
case (BpxBattery::CONFIG_CMD): {
cmdBuf[0] = BpxBattery::PORT_CONFIG_CMD;
case (bpxBat::CONFIG_CMD): {
cmdBuf[0] = bpxBat::PORT_CONFIG_CMD;
// Needs to be set to 0x01 according to datasheet
cmdBuf[1] = 0x01;
this->rawPacketLen = 2;
break;
}
case (BpxBattery::CONFIG_GET): {
cmdBuf[0] = BpxBattery::PORT_CONFIG_GET;
case (bpxBat::CONFIG_GET): {
cmdBuf[0] = bpxBat::PORT_CONFIG_GET;
this->rawPacketLen = 1;
break;
}
case (BpxBattery::CONFIG_SET): {
cmdBuf[0] = BpxBattery::PORT_CONFIG_SET;
case (bpxBat::CONFIG_SET): {
cmdBuf[0] = bpxBat::PORT_CONFIG_SET;
if (commandDataLen != 3) {
return DeviceHandlerIF::INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
@ -114,8 +116,8 @@ ReturnValue_t BpxBatteryHandler::buildCommandFromCommand(DeviceCommandId_t devic
this->rawPacketLen = 4;
break;
}
case (BpxBattery::MAN_HEAT_ON): {
cmdBuf[0] = BpxBattery::PORT_MAN_HEAT_ON;
case (bpxBat::MAN_HEAT_ON): {
cmdBuf[0] = bpxBat::PORT_MAN_HEAT_ON;
if (commandDataLen != 2) {
return DeviceHandlerIF::INVALID_NUMBER_OR_LENGTH_OF_PARAMETERS;
}
@ -125,8 +127,8 @@ ReturnValue_t BpxBatteryHandler::buildCommandFromCommand(DeviceCommandId_t devic
this->rawPacketLen = 3;
break;
}
case (BpxBattery::MAN_HEAT_OFF): {
cmdBuf[0] = BpxBattery::PORT_MAN_HEAT_OFF;
case (bpxBat::MAN_HEAT_OFF): {
cmdBuf[0] = bpxBat::PORT_MAN_HEAT_OFF;
this->rawPacketLen = 1;
break;
}
@ -142,35 +144,35 @@ ReturnValue_t BpxBatteryHandler::buildCommandFromCommand(DeviceCommandId_t devic
ReturnValue_t BpxBatteryHandler::scanForReply(const uint8_t* start, size_t remainingSize,
DeviceCommandId_t* foundId, size_t* foundLen) {
using namespace BpxBattery;
using namespace bpxBat;
switch (lastCmd) {
case (BpxBattery::GET_HK): {
case (bpxBat::GET_HK): {
if (remainingSize != GET_HK_REPLY_LEN) {
return DeviceHandlerIF::LENGTH_MISSMATCH;
}
break;
}
case (BpxBattery::PING):
case (BpxBattery::MAN_HEAT_ON):
case (BpxBattery::MAN_HEAT_OFF): {
case (bpxBat::PING):
case (bpxBat::MAN_HEAT_ON):
case (bpxBat::MAN_HEAT_OFF): {
if (remainingSize != PING_REPLY_LEN) {
return DeviceHandlerIF::LENGTH_MISSMATCH;
}
break;
}
case (BpxBattery::REBOOT): {
case (bpxBat::REBOOT): {
// Ignore
break;
}
case (BpxBattery::RESET_COUNTERS):
case (BpxBattery::CONFIG_CMD):
case (BpxBattery::CONFIG_SET): {
case (bpxBat::RESET_COUNTERS):
case (bpxBat::CONFIG_CMD):
case (bpxBat::CONFIG_SET): {
if (remainingSize != EMPTY_REPLY_LEN) {
return DeviceHandlerIF::LENGTH_MISSMATCH;
}
break;
}
case (BpxBattery::CONFIG_GET): {
case (bpxBat::CONFIG_GET): {
if (remainingSize != CONFIG_GET_REPLY_LEN) {
return DeviceHandlerIF::LENGTH_MISSMATCH;
}
@ -187,11 +189,11 @@ ReturnValue_t BpxBatteryHandler::scanForReply(const uint8_t* start, size_t remai
}
ReturnValue_t BpxBatteryHandler::interpretDeviceReply(DeviceCommandId_t id, const uint8_t* packet) {
if (id != BpxBattery::REBOOT and packet[1] != 0) {
if (id != bpxBat::REBOOT and packet[1] != 0) {
return DeviceHandlerIF::DEVICE_REPORTED_ERROR;
}
switch (id) {
case (BpxBattery::GET_HK): {
case (bpxBat::GET_HK): {
PoolReadGuard rg(&hkSet);
ReturnValue_t result = hkSet.parseRawHk(packet + 2, 21);
hkSet.setValidity(true, true);
@ -213,7 +215,7 @@ ReturnValue_t BpxBatteryHandler::interpretDeviceReply(DeviceCommandId_t id, cons
}
break;
}
case (BpxBattery::PING): {
case (bpxBat::PING): {
if (packet[2] != sentPingByte) {
return DeviceHandlerIF::INVALID_DATA;
}
@ -222,19 +224,19 @@ ReturnValue_t BpxBatteryHandler::interpretDeviceReply(DeviceCommandId_t id, cons
}
break;
}
case (BpxBattery::RESET_COUNTERS):
case (BpxBattery::CONFIG_CMD):
case (BpxBattery::CONFIG_SET): {
case (bpxBat::RESET_COUNTERS):
case (bpxBat::CONFIG_CMD):
case (bpxBat::CONFIG_SET): {
break;
}
case (BpxBattery::MAN_HEAT_ON):
case (BpxBattery::MAN_HEAT_OFF): {
case (bpxBat::MAN_HEAT_ON):
case (bpxBat::MAN_HEAT_OFF): {
if (packet[2] != 0x01) {
return DeviceHandlerIF::DEVICE_DID_NOT_EXECUTE;
}
break;
}
case (BpxBattery::CONFIG_GET): {
case (bpxBat::CONFIG_GET): {
PoolReadGuard rg(&cfgSet);
ReturnValue_t result = cfgSet.parseRawHk(packet + 2, 3);
if (result != returnvalue::OK) {
@ -243,7 +245,7 @@ ReturnValue_t BpxBatteryHandler::interpretDeviceReply(DeviceCommandId_t id, cons
cfgSet.setValidity(true, true);
break;
}
case (BpxBattery::REBOOT): {
case (bpxBat::REBOOT): {
break;
}
default: {
@ -257,20 +259,20 @@ uint32_t BpxBatteryHandler::getTransitionDelayMs(Mode_t modeFrom, Mode_t modeTo)
ReturnValue_t BpxBatteryHandler::initializeLocalDataPool(localpool::DataPool& localDataPoolMap,
LocalDataPoolManager& poolManager) {
localDataPoolMap.emplace(BpxBattery::BATT_TEMP_1, &battTemp1);
localDataPoolMap.emplace(BpxBattery::BATT_TEMP_2, &battTemp2);
localDataPoolMap.emplace(BpxBattery::BATT_TEMP_3, &battTemp3);
localDataPoolMap.emplace(BpxBattery::BATT_TEMP_4, &battTemp4);
localDataPoolMap.emplace(BpxBattery::CHARGE_CURRENT, &chargeCurrent);
localDataPoolMap.emplace(BpxBattery::DISCHARGE_CURRENT, &dischargeCurrent);
localDataPoolMap.emplace(BpxBattery::HEATER_CURRENT, &heaterCurrent);
localDataPoolMap.emplace(BpxBattery::BATT_VOLTAGE, &battVolt);
localDataPoolMap.emplace(BpxBattery::REBOOT_COUNTER, &rebootCounter);
localDataPoolMap.emplace(BpxBattery::BOOTCAUSE, &bootCause);
localDataPoolMap.emplace(bpxBat::BATT_TEMP_1, &battTemp1);
localDataPoolMap.emplace(bpxBat::BATT_TEMP_2, &battTemp2);
localDataPoolMap.emplace(bpxBat::BATT_TEMP_3, &battTemp3);
localDataPoolMap.emplace(bpxBat::BATT_TEMP_4, &battTemp4);
localDataPoolMap.emplace(bpxBat::CHARGE_CURRENT, &chargeCurrent);
localDataPoolMap.emplace(bpxBat::DISCHARGE_CURRENT, &dischargeCurrent);
localDataPoolMap.emplace(bpxBat::HEATER_CURRENT, &heaterCurrent);
localDataPoolMap.emplace(bpxBat::BATT_VOLTAGE, &battVolt);
localDataPoolMap.emplace(bpxBat::REBOOT_COUNTER, &rebootCounter);
localDataPoolMap.emplace(bpxBat::BOOTCAUSE, &bootCause);
localDataPoolMap.emplace(BpxBattery::BATTERY_HEATER_MODE, &battheatMode);
localDataPoolMap.emplace(BpxBattery::BATTHEAT_LOW_LIMIT, &battheatLow);
localDataPoolMap.emplace(BpxBattery::BATTHEAT_HIGH_LIMIT, &battheatHigh);
localDataPoolMap.emplace(bpxBat::BATTERY_HEATER_MODE, &battheatMode);
localDataPoolMap.emplace(bpxBat::BATTHEAT_LOW_LIMIT, &battheatLow);
localDataPoolMap.emplace(bpxBat::BATTHEAT_HIGH_LIMIT, &battheatHigh);
poolManager.subscribeForRegularPeriodicPacket(
subdp::RegularHkPeriodicParams(hkSet.getSid(), enableHkSets, 20.0));