From 8044488bed9de320ade44f2b874af9ca6ff854a7 Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Mon, 22 Oct 2018 17:23:13 -0400 Subject: [PATCH] Make a blank settings interface if one does not exist because segfaulting is bad --- api/icsneoc/icsneoc.cpp | 18 ------------------ device/device.cpp | 4 ++-- device/idevicesettings.cpp | 11 +++++++---- include/icsneo/device/device.h | 1 + include/icsneo/device/idevicesettings.h | 7 ++++++- 5 files changed, 16 insertions(+), 25 deletions(-) diff --git a/api/icsneoc/icsneoc.cpp b/api/icsneoc/icsneoc.cpp index 2015eb3..8bf6552 100644 --- a/api/icsneoc/icsneoc.cpp +++ b/api/icsneoc/icsneoc.cpp @@ -216,9 +216,6 @@ bool icsneo_settingsRefresh(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) return false; - if(!device->device->settings) // Settings are not available for this device - return false; - return device->device->settings->refresh(); } @@ -226,9 +223,6 @@ bool icsneo_settingsApply(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) return false; - if(!device->device->settings) // Settings are not available for this device - return false; - return device->device->settings->apply(); } @@ -236,9 +230,6 @@ bool icsneo_settingsApplyTemporary(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) return false; - if(!device->device->settings) // Settings are not available for this device - return false; - return device->device->settings->apply(true); } @@ -246,9 +237,6 @@ bool icsneo_settingsApplyDefaults(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) return false; - if(!device->device->settings) // Settings are not available for this device - return false; - return device->device->settings->applyDefaults(); } @@ -256,9 +244,6 @@ bool icsneo_settingsApplyDefaultsTemporary(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) return false; - if(!device->device->settings) // Settings are not available for this device - return false; - return device->device->settings->applyDefaults(true); } @@ -266,9 +251,6 @@ bool icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newB if(!icsneo_isValidNeoDevice(device)) return false; - if(!device->device->settings) // Settings are not available for this device - return false; - return device->device->settings->setBaudrateFor(netid, newBaudrate); } diff --git a/device/device.cpp b/device/device.cpp index a7a7f36..71d3df5 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -145,10 +145,10 @@ bool Device::open() { return false; } - bool settingsNecessary = bool(settings); // Check if the shared_ptr exists + bool settingsNecessary = !settings->disabled; if(settingsNecessary) { settings->refresh(); - if(!settings || !settings->ok()) + if(!settings->ok()) return false; } diff --git a/device/idevicesettings.cpp b/device/idevicesettings.cpp index 0c81e1a..7637c2f 100644 --- a/device/idevicesettings.cpp +++ b/device/idevicesettings.cpp @@ -43,6 +43,9 @@ uint16_t IDeviceSettings::CalculateGSChecksum(const std::vector& settin } bool IDeviceSettings::refresh(bool ignoreChecksum) { + if(disabled) + return false; + std::vector rxSettings; bool ret = com->getSettingsSync(rxSettings); if(!ret) @@ -86,7 +89,7 @@ bool IDeviceSettings::refresh(bool ignoreChecksum) { } bool IDeviceSettings::apply(bool temporary) { - if(readonly) + if(disabled || readonly) return false; std::vector bytestream; @@ -135,7 +138,7 @@ bool IDeviceSettings::apply(bool temporary) { } bool IDeviceSettings::applyDefaults(bool temporary) { - if(readonly) + if(disabled || readonly) return false; com->sendCommand(Command::SetDefaultSettings); @@ -178,7 +181,7 @@ bool IDeviceSettings::applyDefaults(bool temporary) { } bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { - if(readonly) + if(disabled || readonly) return false; switch(net.getType()) { @@ -201,7 +204,7 @@ bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { } template bool IDeviceSettings::setStructure(const T& newStructure) { - if(readonly) + if(disabled || readonly) return false; if(sizeof(T) != structSize) diff --git a/include/icsneo/device/device.h b/include/icsneo/device/device.h index 20513a6..d6bb2e5 100644 --- a/include/icsneo/device/device.h +++ b/include/icsneo/device/device.h @@ -21,6 +21,7 @@ public: Device(neodevice_t neodevice = { 0 }) { data = neodevice; data.device = this; + settings = std::unique_ptr(new IDeviceSettings(nullptr)); } virtual ~Device() { disableMessagePolling(); diff --git a/include/icsneo/device/idevicesettings.h b/include/icsneo/device/idevicesettings.h index b47102d..ab67f38 100644 --- a/include/icsneo/device/idevicesettings.h +++ b/include/icsneo/device/idevicesettings.h @@ -281,9 +281,13 @@ public: static constexpr uint16_t GS_VERSION = 5; static uint16_t CalculateGSChecksum(const std::vector& settings); + // Parameter createInoperableSettings exists because it is serving as a warning that you probably don't want to do this + typedef void* warn_t; + IDeviceSettings(warn_t createInoperableSettings) : disabled(true), readonly(true), structSize(0) { (void)createInoperableSettings; } + IDeviceSettings(std::shared_ptr com, size_t size) : com(com), structSize(size) {} virtual ~IDeviceSettings() {} - bool ok() { return settingsLoaded; } + bool ok() { return !disabled && settingsLoaded; } bool refresh(bool ignoreChecksum = false); // Get from device @@ -303,6 +307,7 @@ public: uint8_t getEnumValueForBaudrate(uint32_t baudrate); + bool disabled = false; bool readonly = false; protected: std::shared_ptr com;