Make a blank settings interface if one does not exist because segfaulting is bad

pull/4/head
Paul Hollinsky 2018-10-22 17:23:13 -04:00
parent 6456d4e261
commit 8044488bed
5 changed files with 16 additions and 25 deletions

View File

@ -216,9 +216,6 @@ bool icsneo_settingsRefresh(const neodevice_t* device) {
if(!icsneo_isValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->settings) // Settings are not available for this device
return false;
return device->device->settings->refresh(); return device->device->settings->refresh();
} }
@ -226,9 +223,6 @@ bool icsneo_settingsApply(const neodevice_t* device) {
if(!icsneo_isValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->settings) // Settings are not available for this device
return false;
return device->device->settings->apply(); return device->device->settings->apply();
} }
@ -236,9 +230,6 @@ bool icsneo_settingsApplyTemporary(const neodevice_t* device) {
if(!icsneo_isValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->settings) // Settings are not available for this device
return false;
return device->device->settings->apply(true); return device->device->settings->apply(true);
} }
@ -246,9 +237,6 @@ bool icsneo_settingsApplyDefaults(const neodevice_t* device) {
if(!icsneo_isValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->settings) // Settings are not available for this device
return false;
return device->device->settings->applyDefaults(); return device->device->settings->applyDefaults();
} }
@ -256,9 +244,6 @@ bool icsneo_settingsApplyDefaultsTemporary(const neodevice_t* device) {
if(!icsneo_isValidNeoDevice(device)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->settings) // Settings are not available for this device
return false;
return device->device->settings->applyDefaults(true); 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)) if(!icsneo_isValidNeoDevice(device))
return false; return false;
if(!device->device->settings) // Settings are not available for this device
return false;
return device->device->settings->setBaudrateFor(netid, newBaudrate); return device->device->settings->setBaudrateFor(netid, newBaudrate);
} }

View File

@ -145,10 +145,10 @@ bool Device::open() {
return false; return false;
} }
bool settingsNecessary = bool(settings); // Check if the shared_ptr exists bool settingsNecessary = !settings->disabled;
if(settingsNecessary) { if(settingsNecessary) {
settings->refresh(); settings->refresh();
if(!settings || !settings->ok()) if(!settings->ok())
return false; return false;
} }

View File

@ -43,6 +43,9 @@ uint16_t IDeviceSettings::CalculateGSChecksum(const std::vector<uint8_t>& settin
} }
bool IDeviceSettings::refresh(bool ignoreChecksum) { bool IDeviceSettings::refresh(bool ignoreChecksum) {
if(disabled)
return false;
std::vector<uint8_t> rxSettings; std::vector<uint8_t> rxSettings;
bool ret = com->getSettingsSync(rxSettings); bool ret = com->getSettingsSync(rxSettings);
if(!ret) if(!ret)
@ -86,7 +89,7 @@ bool IDeviceSettings::refresh(bool ignoreChecksum) {
} }
bool IDeviceSettings::apply(bool temporary) { bool IDeviceSettings::apply(bool temporary) {
if(readonly) if(disabled || readonly)
return false; return false;
std::vector<uint8_t> bytestream; std::vector<uint8_t> bytestream;
@ -135,7 +138,7 @@ bool IDeviceSettings::apply(bool temporary) {
} }
bool IDeviceSettings::applyDefaults(bool temporary) { bool IDeviceSettings::applyDefaults(bool temporary) {
if(readonly) if(disabled || readonly)
return false; return false;
com->sendCommand(Command::SetDefaultSettings); com->sendCommand(Command::SetDefaultSettings);
@ -178,7 +181,7 @@ bool IDeviceSettings::applyDefaults(bool temporary) {
} }
bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) {
if(readonly) if(disabled || readonly)
return false; return false;
switch(net.getType()) { switch(net.getType()) {
@ -201,7 +204,7 @@ bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) {
} }
template<typename T> bool IDeviceSettings::setStructure(const T& newStructure) { template<typename T> bool IDeviceSettings::setStructure(const T& newStructure) {
if(readonly) if(disabled || readonly)
return false; return false;
if(sizeof(T) != structSize) if(sizeof(T) != structSize)

View File

@ -21,6 +21,7 @@ public:
Device(neodevice_t neodevice = { 0 }) { Device(neodevice_t neodevice = { 0 }) {
data = neodevice; data = neodevice;
data.device = this; data.device = this;
settings = std::unique_ptr<IDeviceSettings>(new IDeviceSettings(nullptr));
} }
virtual ~Device() { virtual ~Device() {
disableMessagePolling(); disableMessagePolling();

View File

@ -281,9 +281,13 @@ public:
static constexpr uint16_t GS_VERSION = 5; static constexpr uint16_t GS_VERSION = 5;
static uint16_t CalculateGSChecksum(const std::vector<uint8_t>& settings); static uint16_t CalculateGSChecksum(const std::vector<uint8_t>& 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<Communication> com, size_t size) : com(com), structSize(size) {} IDeviceSettings(std::shared_ptr<Communication> com, size_t size) : com(com), structSize(size) {}
virtual ~IDeviceSettings() {} virtual ~IDeviceSettings() {}
bool ok() { return settingsLoaded; } bool ok() { return !disabled && settingsLoaded; }
bool refresh(bool ignoreChecksum = false); // Get from device bool refresh(bool ignoreChecksum = false); // Get from device
@ -303,6 +307,7 @@ public:
uint8_t getEnumValueForBaudrate(uint32_t baudrate); uint8_t getEnumValueForBaudrate(uint32_t baudrate);
bool disabled = false;
bool readonly = false; bool readonly = false;
protected: protected:
std::shared_ptr<Communication> com; std::shared_ptr<Communication> com;