Allow returning to the default settings
parent
7d821b9745
commit
b3471890eb
|
|
@ -212,24 +212,44 @@ bool icsneo_getProductName(const neodevice_t* device, char* str, size_t* maxLeng
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool icsneo_settingsSend(const neodevice_t* device) {
|
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
|
if(!device->device->settings) // Settings are not available for this device
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return device->device->settings->send();
|
return device->device->settings->apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool icsneo_settingsCommit(const neodevice_t* device) {
|
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
|
if(!device->device->settings) // Settings are not available for this device
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return device->device->settings->commit();
|
return device->device->settings->apply(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate) {
|
bool icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate) {
|
||||||
|
|
|
||||||
|
|
@ -44,9 +44,13 @@ extern bool DLLExport icsneo_setPollingMessageLimit(const neodevice_t* device, s
|
||||||
|
|
||||||
extern bool DLLExport icsneo_getProductName(const neodevice_t* device, char* str, size_t* maxLength);
|
extern bool DLLExport icsneo_getProductName(const neodevice_t* device, char* str, size_t* maxLength);
|
||||||
|
|
||||||
extern bool DLLExport icsneo_settingsSend(const neodevice_t* device);
|
extern bool DLLExport icsneo_settingsApply(const neodevice_t* device);
|
||||||
|
|
||||||
extern bool DLLExport icsneo_settingsCommit(const neodevice_t* device);
|
extern bool DLLExport icsneo_settingsApplyTemporary(const neodevice_t* device);
|
||||||
|
|
||||||
|
extern bool DLLExport icsneo_settingsApplyDefaults(const neodevice_t* device);
|
||||||
|
|
||||||
|
extern bool DLLExport icsneo_settingsApplyDefaultsTemporary(const neodevice_t* device);
|
||||||
|
|
||||||
extern bool DLLExport icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate);
|
extern bool DLLExport icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate);
|
||||||
|
|
||||||
|
|
@ -101,16 +105,22 @@ fn_icsneo_getPollingMessageLimit icsneo_getPollingMessageLimit;
|
||||||
typedef bool(*fn_icsneo_setPollingMessageLimit)(const neodevice_t* device, size_t newLimit);
|
typedef bool(*fn_icsneo_setPollingMessageLimit)(const neodevice_t* device, size_t newLimit);
|
||||||
fn_icsneo_setPollingMessageLimit icsneo_setPollingMessageLimit;
|
fn_icsneo_setPollingMessageLimit icsneo_setPollingMessageLimit;
|
||||||
|
|
||||||
typedef size_t(*fn_icsneo_getProductName)(const neodevice_t* device, char* str, size_t* maxLength);
|
typedef bool(*fn_icsneo_getProductName)(const neodevice_t* device, char* str, size_t* maxLength);
|
||||||
fn_icsneo_getProductName icsneo_getProductName;
|
fn_icsneo_getProductName icsneo_getProductName;
|
||||||
|
|
||||||
typedef size_t(*fn_icsneo_settingsSend)(const neodevice_t* device);
|
typedef bool(*fn_icsneo_settingsApply)(const neodevice_t* device);
|
||||||
fn_icsneo_settingsSend icsneo_settingsSend;
|
fn_icsneo_settingsApply icsneo_settingsApply;
|
||||||
|
|
||||||
typedef size_t(*fn_icsneo_settingsCommit)(const neodevice_t* device);
|
typedef bool(*fn_icsneo_settingsApplyTemporary)(const neodevice_t* device);
|
||||||
fn_icsneo_settingsCommit icsneo_settingsCommit;
|
fn_icsneo_settingsApplyTemporary icsneo_settingsApplyTemporary;
|
||||||
|
|
||||||
typedef size_t(*fn_icsneo_setBaudrate)(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate);
|
typedef bool(*fn_icsneo_settingsApplyDefaults)(const neodevice_t* device);
|
||||||
|
fn_icsneo_settingsApplyDefaults icsneo_settingsApplyDefaults;
|
||||||
|
|
||||||
|
typedef bool(*fn_icsneo_settingsApplyDefaultsTemporary)(const neodevice_t* device);
|
||||||
|
fn_icsneo_settingsApplyDefaultsTemporary icsneo_settingsApplyDefaultsTemporary;
|
||||||
|
|
||||||
|
typedef bool(*fn_icsneo_setBaudrate)(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate);
|
||||||
fn_icsneo_setBaudrate icsneo_setBaudrate;
|
fn_icsneo_setBaudrate icsneo_setBaudrate;
|
||||||
|
|
||||||
#define ICSNEO_IMPORT(func) func = (fn_##func)icsneo_dynamicLibraryGetFunction(icsneo_libraryHandle, #func)
|
#define ICSNEO_IMPORT(func) func = (fn_##func)icsneo_dynamicLibraryGetFunction(icsneo_libraryHandle, #func)
|
||||||
|
|
@ -143,8 +153,10 @@ int icsneo_init() {
|
||||||
ICSNEO_IMPORTASSERT(icsneo_getPollingMessageLimit);
|
ICSNEO_IMPORTASSERT(icsneo_getPollingMessageLimit);
|
||||||
ICSNEO_IMPORTASSERT(icsneo_setPollingMessageLimit);
|
ICSNEO_IMPORTASSERT(icsneo_setPollingMessageLimit);
|
||||||
ICSNEO_IMPORTASSERT(icsneo_getProductName);
|
ICSNEO_IMPORTASSERT(icsneo_getProductName);
|
||||||
ICSNEO_IMPORTASSERT(icsneo_settingsSend);
|
ICSNEO_IMPORTASSERT(icsneo_settingsApply);
|
||||||
ICSNEO_IMPORTASSERT(icsneo_settingsCommit);
|
ICSNEO_IMPORTASSERT(icsneo_settingsApplyTemporary);
|
||||||
|
ICSNEO_IMPORTASSERT(icsneo_settingsApplyDefaults);
|
||||||
|
ICSNEO_IMPORTASSERT(icsneo_settingsApplyDefaultsTemporary);
|
||||||
ICSNEO_IMPORTASSERT(icsneo_setBaudrate);
|
ICSNEO_IMPORTASSERT(icsneo_setBaudrate);
|
||||||
|
|
||||||
icsneo_initialized = true;
|
icsneo_initialized = true;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,8 @@ enum class Command : uint8_t {
|
||||||
RequestSerialNumber = 0xA1,
|
RequestSerialNumber = 0xA1,
|
||||||
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
|
SetSettings = 0xA4, // Previously known as RED_CMD_SET_BAUD_REQ, follow up with SaveSettings to write to EEPROM
|
||||||
GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ
|
GetSettings = 0xA5, // Previously known as RED_CMD_READ_BAUD_REQ
|
||||||
SaveSettings = 0xA6
|
SaveSettings = 0xA6,
|
||||||
|
SetDefaultSettings = 0xA8 // Follow up with SaveSettings to write to EEPROM
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,8 +82,7 @@ void IDeviceSettings::refresh(bool ignoreChecksum) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IDeviceSettings::send() {
|
bool IDeviceSettings::apply(bool temporary) {
|
||||||
constexpr uint16_t GS_VERSION = 5;
|
|
||||||
std::vector<uint8_t> bytestream;
|
std::vector<uint8_t> bytestream;
|
||||||
bytestream.resize(7 + structSize);
|
bytestream.resize(7 + structSize);
|
||||||
bytestream[0] = 0x00;
|
bytestream[0] = 0x00;
|
||||||
|
|
@ -114,18 +113,55 @@ bool IDeviceSettings::send() {
|
||||||
|
|
||||||
com->sendCommand(Command::SetSettings, bytestream);
|
com->sendCommand(Command::SetSettings, bytestream);
|
||||||
msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SetSettings), std::chrono::milliseconds(1000));
|
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
|
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
|
return (msg && msg->data[0] == 1); // Device sends 0x01 for success
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IDeviceSettings::commit() {
|
bool IDeviceSettings::applyDefaults(bool temporary) {
|
||||||
if(!send())
|
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;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
com->sendCommand(Command::SaveSettings);
|
refresh(true); // Refresh ignoring checksum
|
||||||
std::shared_ptr<Message> msg = com->waitForMessageSync(std::make_shared<Main51MessageFilter>(Command::SaveSettings), std::chrono::milliseconds(5000));
|
// 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);
|
||||||
|
|
||||||
|
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
|
refresh(); // Refresh our buffer with what the device has, whether we were successful or not
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -278,6 +278,7 @@ namespace icsneo {
|
||||||
|
|
||||||
class IDeviceSettings {
|
class IDeviceSettings {
|
||||||
public:
|
public:
|
||||||
|
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);
|
||||||
|
|
||||||
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) {}
|
||||||
|
|
@ -285,8 +286,10 @@ public:
|
||||||
bool ok() { return settingsLoaded; }
|
bool ok() { return settingsLoaded; }
|
||||||
|
|
||||||
void refresh(bool ignoreChecksum = false); // 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
|
// 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);
|
virtual bool setBaudrateFor(Network net, uint32_t baudrate);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue