Major improvements to the settings API

* Allow the raw structure to be manipulated from C and Legacy APIs

 * Structure is now split between what's on the device and what's on the client so changes will not be visible from read methods until apply()

 * Allow devices to connect which have slightly different firmware versions than the settings structure
This commit is contained in:
Paul Hollinsky
2018-12-21 20:24:11 -05:00
parent 151b2ce266
commit e124ad28f4
13 changed files with 282 additions and 74 deletions
+24 -10
View File
@@ -302,21 +302,33 @@ public:
virtual bool setFDBaudrateFor(Network net, int64_t baudrate);
virtual const CAN_SETTINGS* getCANSettingsFor(Network net) const { (void)net; return nullptr; }
CAN_SETTINGS* getCANSettingsFor(Network net) {
return const_cast<CAN_SETTINGS*>(static_cast<const IDeviceSettings*>(this)->getCANSettingsFor(net));
CAN_SETTINGS* getMutableCANSettingsFor(Network net) {
if(disabled || readonly)
return nullptr;
const uint8_t* offset = (const uint8_t*)getCANSettingsFor(net);
if(offset == nullptr)
return nullptr;
return static_cast<CAN_SETTINGS*>((void*)(settings.data() + (offset - settingsInDeviceRAM.data())));
}
virtual const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const { (void)net; return nullptr; }
CANFD_SETTINGS* getCANFDSettingsFor(Network net) {
return const_cast<CANFD_SETTINGS*>(static_cast<const IDeviceSettings*>(this)->getCANFDSettingsFor(net));
CANFD_SETTINGS* getMutableCANFDSettingsFor(Network net) {
if(disabled || readonly)
return nullptr;
const uint8_t* offset = (const uint8_t*)getCANFDSettingsFor(net);
if(offset == nullptr)
return nullptr;
return static_cast<CANFD_SETTINGS*>((void*)(settings.data() + (offset - settingsInDeviceRAM.data())));
}
const void* getRawStructurePointer() const { return settings.data(); }
void* getRawStructurePointer() { return settings.data(); }
const void* getRawStructurePointer() const { return settingsInDeviceRAM.data(); }
void* getMutableRawStructurePointer() { return settings.data(); }
template<typename T> const T* getStructurePointer() const { return static_cast<const T*>(getRawStructurePointer()); }
template<typename T> T* getStructurePointer() { return static_cast<T*>(getRawStructurePointer()); }
template<typename T> T getStructureCopy() const { return *getStructurePointer<T>(); }
template<typename T> bool setStructure(const T& newStructure);
template<typename T> T* getMutableStructurePointer() { return static_cast<T*>(getMutableRawStructurePointer()); }
template<typename T> T getStructure() const { return *getStructurePointer<T>(); }
template<typename T> bool applyStructure(const T& newStructure);
const size_t& getSize() const { return structSize; }
bool disabled = false;
bool readonly = false;
@@ -325,7 +337,9 @@ protected:
device_errorhandler_t err;
size_t structSize;
bool settingsLoaded = false;
std::vector<uint8_t> settings;
std::vector<uint8_t> settings; // For writing settings to, calling apply() should copy over to device RAM (and EEPROM)
std::vector<uint8_t> settingsInDeviceRAM; // For reading settings from
// Parameter createInoperableSettings exists because it is serving as a warning that you probably don't want to do this
typedef void* warn_t;
@@ -102,6 +102,8 @@ public:
NeoVIFIRESettings(std::shared_ptr<Communication> com) : IDeviceSettings(com, sizeof(neovifire_settings_t)) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<neovifire_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -116,6 +116,8 @@ public:
NeoVIFIRE2Settings(std::shared_ptr<Communication> com) : IDeviceSettings(com, sizeof(neovifire2_settings_t)) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<neovifire2_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -139,6 +141,8 @@ public:
}
const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override {
auto cfg = getStructurePointer<neovifire2_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->canfd1);
@@ -38,6 +38,8 @@ public:
ValueCAN3Settings(std::shared_ptr<Communication> com) : IDeviceSettings(com, sizeof(valuecan3_settings_t)) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan3_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -12,6 +12,8 @@ public:
ValueCAN4_1Settings(std::shared_ptr<Communication> com) : ValueCAN4_1_2Settings(com) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_1_2_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -13,6 +13,8 @@ public:
ValueCAN4_2ELSettings(std::shared_ptr<Communication> com) : ValueCAN4_4_2ELSettings(com) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_4_2el_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -24,6 +26,8 @@ public:
}
const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_4_2el_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->canfd1);
@@ -12,6 +12,8 @@ public:
ValueCAN4_2Settings(std::shared_ptr<Communication> com) : ValueCAN4_1_2Settings(com) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_1_2_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -23,6 +25,8 @@ public:
}
const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_1_2_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->canfd1);
@@ -13,6 +13,8 @@ public:
ValueCAN4_4Settings(std::shared_ptr<Communication> com) : ValueCAN4_4_2ELSettings(com) {}
const CAN_SETTINGS* getCANSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_4_2el_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->can1);
@@ -28,6 +30,8 @@ public:
}
const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override {
auto cfg = getStructurePointer<valuecan4_4_2el_settings_t>();
if(cfg == nullptr)
return nullptr;
switch(net.getNetID()) {
case Network::NetID::HSCAN:
return &(cfg->canfd1);
+58
View File
@@ -357,6 +357,52 @@ extern bool DLLExport icsneo_settingsApplyDefaults(const neodevice_t* device);
*/
extern bool DLLExport icsneo_settingsApplyDefaultsTemporary(const neodevice_t* device);
/**
* \brief Apply the default settings structure for a specified device temporarily.
* \param[in] device A pointer to the neodevice_t structure specifying the device to operate on.
* \param[out] structure A pointer to a device settings structure for the current device.
* \param[in] structureSize The size of the current device settings structure in bytes.
* \returns Number of bytes written to structure
*
* See icsneo_settingsApply() for further information about applying settings. See icsneo_settingsApplyDefaults() for further information about applying default settings.
*
* This function sets the default settings such that they will revert to the values saved in non-volatile storage when the device loses power.
*
* If possible, use functions specific to the operation you want to acomplish (such as icsneo_setBaudrate()) instead of modifying the structure directly.
* This allows the client application to work with other hardware.
*/
extern size_t DLLExport icsneo_settingsReadStructure(const neodevice_t* device, void* structure, size_t structureSize);
/**
* \brief Apply a provided settings structure for a specified device.
* \param[in] device A pointer to the neodevice_t structure specifying the device to operate on.
* \param[in] structure A pointer to a device settings structure for the current device.
* \param[in] structureSize The size of the current device settings structure in bytes.
* \returns True if the settings were applied.
*
* This function immediately applies the provided settings. See icsneo_settingsApplyTemporary() for further information about applying settings.
*
* If possible, use functions specific to the operation you want to acomplish (such as icsneo_setBaudrate()) instead of modifying the structure directly.
* This allows the client application to work with other hardware.
*/
extern bool DLLExport icsneo_settingsApplyStructure(const neodevice_t* device, const void* structure, size_t structureSize);
/**
* \brief Apply a provided settings structure for a specified device without saving to non-volatile EEPROM.
* \param[in] device A pointer to the neodevice_t structure specifying the device to operate on.
* \param[in] structure A pointer to a device settings structure for the current device.
* \param[in] structureSize The size of the current device settings structure in bytes.
* \returns True if the settings were applied.
*
* This function immediately applies the provided settings. See icsneo_settingsApply() for further information about applying settings.
*
* This function sets the default settings such that they will revert to the values saved in non-volatile storage when the device loses power.
*
* If possible, use functions specific to the operation you want to acomplish (such as icsneo_setBaudrate()) instead of modifying the structure directly.
* This allows the client application to work with other hardware.
*/
extern bool DLLExport icsneo_settingsApplyStructureTemporary(const neodevice_t* device, const void* structure, size_t structureSize);
/**
* \brief Get the network baudrate for a specified device.
* \param[in] device A pointer to the neodevice_t structure specifying the device to operate on.
@@ -649,6 +695,15 @@ fn_icsneo_settingsApplyDefaults icsneo_settingsApplyDefaults;
typedef bool(*fn_icsneo_settingsApplyDefaultsTemporary)(const neodevice_t* device);
fn_icsneo_settingsApplyDefaultsTemporary icsneo_settingsApplyDefaultsTemporary;
typedef size_t(*fn_icsneo_settingsReadStructure)(const neodevice_t* device, void* structure, size_t structureSize);
fn_icsneo_settingsReadStructure icsneo_settingsReadStructure;
typedef bool(*fn_icsneo_settingsApplyStructure)(const neodevice_t* device, const void* structure, size_t structureSize);
fn_icsneo_settingsApplyStructure icsneo_settingsApplyStructure;
typedef bool(*fn_icsneo_settingsApplyStructureTemporary)(const neodevice_t* device, const void* structure, size_t structureSize);
fn_icsneo_settingsApplyStructureTemporary icsneo_settingsApplyStructureTemporary;
typedef int64_t(*fn_icsneo_getBaudrate)(const neodevice_t* device, uint16_t netid);
fn_icsneo_getBaudrate icsneo_getBaudrate;
@@ -729,6 +784,9 @@ int icsneo_init() {
ICSNEO_IMPORTASSERT(icsneo_settingsApplyTemporary);
ICSNEO_IMPORTASSERT(icsneo_settingsApplyDefaults);
ICSNEO_IMPORTASSERT(icsneo_settingsApplyDefaultsTemporary);
ICSNEO_IMPORTASSERT(icsneo_settingsReadStructure);
ICSNEO_IMPORTASSERT(icsneo_settingsApplyStructure);
ICSNEO_IMPORTASSERT(icsneo_settingsApplyStructureTemporary);
ICSNEO_IMPORTASSERT(icsneo_getBaudrate);
ICSNEO_IMPORTASSERT(icsneo_setBaudrate);
ICSNEO_IMPORTASSERT(icsneo_getFDBaudrate);