From d86f15ab4c839fc4b85cec9bf32e2e47e24a79a5 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Thu, 4 Oct 2018 12:31:04 -0400 Subject: [PATCH] Fix settings checksum issues --- device/idevicesettings.cpp | 24 +++++++++++++++++++----- device/include/idevicesettings.h | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/device/idevicesettings.cpp b/device/idevicesettings.cpp index 686706e..0afbc50 100644 --- a/device/idevicesettings.cpp +++ b/device/idevicesettings.cpp @@ -42,7 +42,7 @@ uint16_t IDeviceSettings::CalculateGSChecksum(const std::vector& settin return gs_crc; } -void IDeviceSettings::refresh() { +void IDeviceSettings::refresh(bool ignoreChecksum) { std::vector rxSettings; bool ret = com->getSettingsSync(rxSettings); if(ret) { @@ -67,7 +67,7 @@ void IDeviceSettings::refresh() { return; } - if(gs_chksum != CalculateGSChecksum(rxSettings)) { + if(!ignoreChecksum && gs_chksum != CalculateGSChecksum(rxSettings)) { std::cout << "Checksum mismatch while reading settings" << std::endl; return; } @@ -98,8 +98,23 @@ bool IDeviceSettings::send() { com->sendCommand(Command::SetSettings, bytestream); std::shared_ptr msg = com->waitForMessageSync(std::make_shared(Command::SetSettings), std::chrono::milliseconds(1000)); - if(!msg || msg->data[0] != 1) - refresh(); // Refr + if(!msg || msg->data[0] != 1) { // We did not receive a response + refresh(); // Attempt to get the settings from the device so we're up to date if possible + return false; + } + + 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 + gs_checksum = CalculateGSChecksum(settings); + bytestream[4] = (uint8_t)gs_checksum; + bytestream[5] = (uint8_t)(gs_checksum >> 8); + memcpy(bytestream.data() + 6, getRawStructurePointer(), structSize); + + com->sendCommand(Command::SetSettings, bytestream); + msg = com->waitForMessageSync(std::make_shared(Command::SetSettings), std::chrono::milliseconds(1000)); + + 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 } @@ -111,7 +126,6 @@ bool IDeviceSettings::commit() { com->sendCommand(Command::SaveSettings); std::shared_ptr msg = com->waitForMessageSync(std::make_shared(Command::SaveSettings), std::chrono::milliseconds(5000)); - if(!msg || msg->data[0] != 1) 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 diff --git a/device/include/idevicesettings.h b/device/include/idevicesettings.h index 2673224..634f072 100644 --- a/device/include/idevicesettings.h +++ b/device/include/idevicesettings.h @@ -284,7 +284,7 @@ public: virtual ~IDeviceSettings() {} bool ok() { return settingsLoaded; } - void refresh(); // Get from device + 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