mirror of
https://github.com/intrepidcs/libicsneo.git
synced 2026-08-05 09:28:40 +02:00
Allow returning to the default settings
This commit is contained in:
@@ -82,8 +82,7 @@ void IDeviceSettings::refresh(bool ignoreChecksum) {
|
||||
}
|
||||
}
|
||||
|
||||
bool IDeviceSettings::send() {
|
||||
constexpr uint16_t GS_VERSION = 5;
|
||||
bool IDeviceSettings::apply(bool temporary) {
|
||||
std::vector<uint8_t> bytestream;
|
||||
bytestream.resize(7 + structSize);
|
||||
bytestream[0] = 0x00;
|
||||
@@ -114,21 +113,58 @@ bool IDeviceSettings::send() {
|
||||
|
||||
com->sendCommand(Command::SetSettings, bytestream);
|
||||
msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SetSettings), std::chrono::milliseconds(1000));
|
||||
if(!msg || msg->data[0] != 1) {
|
||||
refresh();
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!temporary) {
|
||||
com->sendCommand(Command::SaveSettings);
|
||||
msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SaveSettings), std::chrono::milliseconds(5000));
|
||||
}
|
||||
|
||||
refresh(); // Refresh our buffer with what the device has, whether we were successful or not
|
||||
|
||||
return (msg && msg->data[0] == 1); // Device sends 0x01 for success
|
||||
}
|
||||
|
||||
bool IDeviceSettings::commit() {
|
||||
if(!send())
|
||||
bool IDeviceSettings::applyDefaults(bool temporary) {
|
||||
com->sendCommand(Command::SetDefaultSettings);
|
||||
std::shared_ptr<Message> msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SetDefaultSettings), std::chrono::milliseconds(1000));
|
||||
if(!msg || msg->data[0] != 1) {
|
||||
refresh();
|
||||
return false;
|
||||
}
|
||||
|
||||
com->sendCommand(Command::SaveSettings);
|
||||
std::shared_ptr<Message> msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SaveSettings), std::chrono::milliseconds(5000));
|
||||
refresh(true); // Refresh ignoring checksum
|
||||
// The device might modify the settings once they are applied, however in this case it does not update the checksum
|
||||
// We refresh to get these updates, update the checksum, and send it back so it's all in sync
|
||||
std::vector<uint8_t> bytestream;
|
||||
bytestream.resize(7 + structSize);
|
||||
bytestream[0] = 0x00;
|
||||
bytestream[1] = GS_VERSION;
|
||||
bytestream[2] = GS_VERSION >> 8;
|
||||
bytestream[3] = (uint8_t)structSize;
|
||||
bytestream[4] = (uint8_t)(structSize >> 8);
|
||||
uint16_t gs_checksum = CalculateGSChecksum(settings);
|
||||
bytestream[5] = (uint8_t)gs_checksum;
|
||||
bytestream[6] = (uint8_t)(gs_checksum >> 8);
|
||||
memcpy(bytestream.data() + 7, getRawStructurePointer(), structSize);
|
||||
|
||||
refresh(); // Refresh our buffer with what the device has, whether we were successful or not
|
||||
com->sendCommand(Command::SetSettings, bytestream);
|
||||
msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SetSettings), std::chrono::milliseconds(1000));
|
||||
if(!msg || msg->data[0] != 1) {
|
||||
refresh();
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!temporary) {
|
||||
com->sendCommand(Command::SaveSettings);
|
||||
msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SaveSettings), std::chrono::milliseconds(5000));
|
||||
}
|
||||
|
||||
refresh(); // Refresh our buffer with what the device has, whether we were successful or not
|
||||
|
||||
return (msg && msg->data[0] == 1); // Device sends 0x01 for success
|
||||
}
|
||||
|
||||
|
||||
@@ -278,6 +278,7 @@ namespace icsneo {
|
||||
|
||||
class IDeviceSettings {
|
||||
public:
|
||||
static constexpr uint16_t GS_VERSION = 5;
|
||||
static uint16_t CalculateGSChecksum(const std::vector<uint8_t>& settings);
|
||||
|
||||
IDeviceSettings(std::shared_ptr<Communication> com, size_t size) : com(com), structSize(size) {}
|
||||
@@ -285,8 +286,10 @@ public:
|
||||
bool ok() { return settingsLoaded; }
|
||||
|
||||
void refresh(bool ignoreChecksum = false); // Get from device
|
||||
bool send(); // Send to device, device keeps settings in volatile RAM until power cycle
|
||||
bool commit(); // Send to device, device keeps settings in EEPROM until next commit
|
||||
|
||||
// Send to device, if temporary device keeps settings in volatile RAM until power cycle, otherwise saved to EEPROM
|
||||
bool apply(bool temporary = false);
|
||||
bool applyDefaults(bool temporary = false);
|
||||
|
||||
virtual bool setBaudrateFor(Network net, uint32_t baudrate);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user