From 3488e36f2af1d8e8d6c86c56e5058a47bd8ed0eb Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Mon, 10 Dec 2018 14:57:43 -0500 Subject: [PATCH] Added the ability to get the baudrate for networks --- api/icsneoc/icsneoc.cpp | 22 ++- device/idevicesettings.cpp | 185 +++++++++++++----- include/icsneo/device/idevicesettings.h | 30 ++- .../device/neovifire/neovifiresettings.h | 2 +- .../device/neovifire2/neovifire2settings.h | 4 +- .../device/valuecan3/valuecan3settings.h | 2 +- .../valuecan4/settings/valuecan4-1settings.h | 2 +- .../settings/valuecan4-2elsettings.h | 4 +- .../valuecan4/settings/valuecan4-2settings.h | 4 +- .../valuecan4/settings/valuecan4-4settings.h | 4 +- include/icsneo/icsneoc.h | 39 +++- 11 files changed, 221 insertions(+), 77 deletions(-) diff --git a/api/icsneoc/icsneoc.cpp b/api/icsneoc/icsneoc.cpp index 3fc5cce..15ff771 100644 --- a/api/icsneoc/icsneoc.cpp +++ b/api/icsneoc/icsneoc.cpp @@ -338,7 +338,16 @@ bool icsneo_settingsApplyDefaultsTemporary(const neodevice_t* device) { return device->device->settings->applyDefaults(true); } -bool icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate) { +int64_t icsneo_getBaudrate(const neodevice_t* device, uint16_t netid) { + if(!icsneo_isValidNeoDevice(device)) { + ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); + return -1; + } + + return device->device->settings->getBaudrateFor(netid); +} + +bool icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, int64_t newBaudrate) { if(!icsneo_isValidNeoDevice(device)) { ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); return false; @@ -347,7 +356,16 @@ bool icsneo_setBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newB return device->device->settings->setBaudrateFor(netid, newBaudrate); } -bool icsneo_setFDBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate) { +int64_t icsneo_getFDBaudrate(const neodevice_t* device, uint16_t netid) { + if(!icsneo_isValidNeoDevice(device)) { + ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); + return -1; + } + + return device->device->settings->getFDBaudrateFor(netid); +} + +bool icsneo_setFDBaudrate(const neodevice_t* device, uint16_t netid, int64_t newBaudrate) { if(!icsneo_isValidNeoDevice(device)) { ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); return false; diff --git a/device/idevicesettings.cpp b/device/idevicesettings.cpp index fe2415d..f8fe7e9 100644 --- a/device/idevicesettings.cpp +++ b/device/idevicesettings.cpp @@ -42,6 +42,92 @@ uint16_t IDeviceSettings::CalculateGSChecksum(const std::vector& settin return gs_crc; } +CANBaudrate IDeviceSettings::GetEnumValueForBaudrate(int64_t baudrate) { + switch(baudrate) { + case 20000: + return BPS20; + case 33000: + return BPS33; + case 50000: + return BPS50; + case 62000: + return BPS62; + case 83000: + return BPS83; + case 100000: + return BPS100; + case 125000: + return BPS125; + case 250000: + return BPS250; + case 500000: + return BPS500; + case 800000: + return BPS800; + case 1000000: + return BPS1000; + case 666000: + return BPS666; + case 2000000: + return BPS2000; + case 4000000: + return BPS4000; + case 5000000: + return CAN_BPS5000; + case 6667000: + return CAN_BPS6667; + case 8000000: + return CAN_BPS8000; + case 10000000: + return CAN_BPS10000; + default: + return (CANBaudrate)-1; + } +} + +int64_t IDeviceSettings::GetBaudrateValueForEnum(CANBaudrate enumValue) { + switch(enumValue) { + case BPS20: + return 20000; + case BPS33: + return 33000; + case BPS50: + return 50000; + case BPS62: + return 62000; + case BPS83: + return 83000; + case BPS100: + return 100000; + case BPS125: + return 125000; + case BPS250: + return 250000; + case BPS500: + return 500000; + case BPS800: + return 800000; + case BPS1000: + return 1000000; + case BPS666: + return 666000; + case BPS2000: + return 2000000; + case BPS4000: + return 4000000; + case CAN_BPS5000: + return 5000000; + case CAN_BPS6667: + return 6667000; + case CAN_BPS8000: + return 8000000; + case CAN_BPS10000: + return 10000000; + default: + return -1; + } +} + bool IDeviceSettings::refresh(bool ignoreChecksum) { if(disabled) { err(APIError::SettingsNotAvailable); @@ -186,7 +272,27 @@ bool IDeviceSettings::applyDefaults(bool temporary) { return (msg && msg->data[0] == 1); // Device sends 0x01 for success } -bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { +int64_t IDeviceSettings::getBaudrateFor(Network net) const { + if(disabled) + return -1; + + switch(net.getType()) { + case Network::Type::CAN: { + const CAN_SETTINGS* cfg = getCANSettingsFor(net); + if(cfg == nullptr) + return -1; + + int64_t baudrate = GetBaudrateValueForEnum((CANBaudrate)cfg->Baudrate); + if(baudrate == -1) + return -1; + return baudrate; + } + default: + return -1; + } +} + +bool IDeviceSettings::setBaudrateFor(Network net, int64_t baudrate) { if(disabled || readonly) return false; @@ -199,10 +305,10 @@ bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { if(cfg == nullptr) return false; - uint8_t newBaud = getEnumValueForBaudrate(baudrate); - if(newBaud == 0xFF) + CANBaudrate newBaud = GetEnumValueForBaudrate(baudrate); + if(newBaud == (CANBaudrate)-1) return false; - cfg->Baudrate = newBaud; + cfg->Baudrate = (uint8_t)newBaud; cfg->auto_baud = false; cfg->SetBaudrate = AUTO; // Device will use the baudrate value to set the TQ values return true; @@ -212,7 +318,27 @@ bool IDeviceSettings::setBaudrateFor(Network net, uint32_t baudrate) { } } -bool IDeviceSettings::setFDBaudrateFor(Network net, uint32_t baudrate) { +int64_t IDeviceSettings::getFDBaudrateFor(Network net) const { + if(disabled) + return -1; + + switch(net.getType()) { + case Network::Type::CAN: { + const CANFD_SETTINGS* cfg = getCANFDSettingsFor(net); + if(cfg == nullptr) + return -1; + + int64_t baudrate = GetBaudrateValueForEnum((CANBaudrate)cfg->FDBaudrate); + if(baudrate == -1) + return -1; + return baudrate; + } + default: + return -1; + } +} + +bool IDeviceSettings::setFDBaudrateFor(Network net, int64_t baudrate) { if(disabled || readonly) return false; @@ -222,10 +348,10 @@ bool IDeviceSettings::setFDBaudrateFor(Network net, uint32_t baudrate) { if(cfg == nullptr) return false; - uint8_t newBaud = getEnumValueForBaudrate(baudrate); - if(newBaud == 0xFF) + CANBaudrate newBaud = GetEnumValueForBaudrate(baudrate); + if(newBaud == (CANBaudrate)-1) return false; - cfg->FDBaudrate = newBaud; + cfg->FDBaudrate = (uint8_t)newBaud; return true; } default: @@ -245,47 +371,4 @@ template bool IDeviceSettings::setStructure(const T& newStructure) { memcpy(settings.data(), &newStructure, structSize); return true; -} - -uint8_t IDeviceSettings::getEnumValueForBaudrate(uint32_t baudrate) { - switch(baudrate) { - case 20000: - return BPS20; - case 33000: - return BPS33; - case 50000: - return BPS50; - case 62000: - return BPS62; - case 83000: - return BPS83; - case 100000: - return BPS100; - case 125000: - return BPS125; - case 250000: - return BPS250; - case 500000: - return BPS500; - case 800000: - return BPS800; - case 1000000: - return BPS1000; - case 666000: - return BPS666; - case 2000000: - return BPS2000; - case 4000000: - return BPS4000; - case 5000000: - return CAN_BPS5000; - case 6667000: - return CAN_BPS6667; - case 8000000: - return CAN_BPS8000; - case 10000000: - return CAN_BPS10000; - default: - return 0xFF; - } } \ No newline at end of file diff --git a/include/icsneo/device/idevicesettings.h b/include/icsneo/device/idevicesettings.h index 81a00c6..792da57 100644 --- a/include/icsneo/device/idevicesettings.h +++ b/include/icsneo/device/idevicesettings.h @@ -13,7 +13,7 @@ enum }; /* Baudrate in CAN_SETTINGS/CANFD_SETTINGS */ -enum +enum CANBaudrate { BPS20, BPS33, @@ -282,6 +282,8 @@ class IDeviceSettings { public: static constexpr uint16_t GS_VERSION = 5; static uint16_t CalculateGSChecksum(const std::vector& settings); + static CANBaudrate GetEnumValueForBaudrate(int64_t baudrate); + static int64_t GetBaudrateValueForEnum(CANBaudrate enumValue); IDeviceSettings(std::shared_ptr com, size_t size) : com(com), err(com->err), structSize(size) {} virtual ~IDeviceSettings() {} @@ -293,19 +295,29 @@ public: bool apply(bool temporary = false); bool applyDefaults(bool temporary = false); - virtual bool setBaudrateFor(Network net, uint32_t baudrate); - virtual bool setFDBaudrateFor(Network net, uint32_t baudrate); + virtual int64_t getBaudrateFor(Network net) const; + virtual bool setBaudrateFor(Network net, int64_t baudrate); - virtual CAN_SETTINGS* getCANSettingsFor(Network net) { (void)net; return nullptr; } - virtual CANFD_SETTINGS* getCANFDSettingsFor(Network net) { (void)net; return nullptr; } + virtual int64_t getFDBaudrateFor(Network net) const; + 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(static_cast(this)->getCANSettingsFor(net)); + } + + virtual const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const { (void)net; return nullptr; } + CANFD_SETTINGS* getCANFDSettingsFor(Network net) { + return const_cast(static_cast(this)->getCANFDSettingsFor(net)); + } + + const void* getRawStructurePointer() const { return settings.data(); } void* getRawStructurePointer() { return settings.data(); } - template T* getStructurePointer() { return static_cast((void*)settings.data()); } - template T getStructureCopy() { return *getStructurePointer(); } + template const T* getStructurePointer() const { return static_cast(getRawStructurePointer()); } + template T* getStructurePointer() { return static_cast(getRawStructurePointer()); } + template T getStructureCopy() const { return *getStructurePointer(); } template bool setStructure(const T& newStructure); - uint8_t getEnumValueForBaudrate(uint32_t baudrate); - bool disabled = false; bool readonly = false; protected: diff --git a/include/icsneo/device/neovifire/neovifiresettings.h b/include/icsneo/device/neovifire/neovifiresettings.h index 647ac7a..e9ad5b5 100644 --- a/include/icsneo/device/neovifire/neovifiresettings.h +++ b/include/icsneo/device/neovifire/neovifiresettings.h @@ -100,7 +100,7 @@ typedef struct { class NeoVIFIRESettings : public IDeviceSettings { public: NeoVIFIRESettings(std::shared_ptr com) : IDeviceSettings(com, sizeof(neovifire_settings_t)) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/device/neovifire2/neovifire2settings.h b/include/icsneo/device/neovifire2/neovifire2settings.h index 90fffc6..b71ad6c 100644 --- a/include/icsneo/device/neovifire2/neovifire2settings.h +++ b/include/icsneo/device/neovifire2/neovifire2settings.h @@ -114,7 +114,7 @@ typedef struct { class NeoVIFIRE2Settings : public IDeviceSettings { public: NeoVIFIRE2Settings(std::shared_ptr com) : IDeviceSettings(com, sizeof(neovifire2_settings_t)) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: @@ -137,7 +137,7 @@ public: return nullptr; } } - CANFD_SETTINGS* getCANFDSettingsFor(Network net) override { + const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/device/valuecan3/valuecan3settings.h b/include/icsneo/device/valuecan3/valuecan3settings.h index fdb79cd..0fedf1e 100644 --- a/include/icsneo/device/valuecan3/valuecan3settings.h +++ b/include/icsneo/device/valuecan3/valuecan3settings.h @@ -36,7 +36,7 @@ typedef struct { class ValueCAN3Settings : public IDeviceSettings { public: ValueCAN3Settings(std::shared_ptr com) : IDeviceSettings(com, sizeof(valuecan3_settings_t)) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/device/valuecan4/settings/valuecan4-1settings.h b/include/icsneo/device/valuecan4/settings/valuecan4-1settings.h index bed3d79..e4c167d 100644 --- a/include/icsneo/device/valuecan4/settings/valuecan4-1settings.h +++ b/include/icsneo/device/valuecan4/settings/valuecan4-1settings.h @@ -10,7 +10,7 @@ namespace icsneo { class ValueCAN4_1Settings : public ValueCAN4_1_2Settings { public: ValueCAN4_1Settings(std::shared_ptr com) : ValueCAN4_1_2Settings(com) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/device/valuecan4/settings/valuecan4-2elsettings.h b/include/icsneo/device/valuecan4/settings/valuecan4-2elsettings.h index 71f476e..43aa282 100644 --- a/include/icsneo/device/valuecan4/settings/valuecan4-2elsettings.h +++ b/include/icsneo/device/valuecan4/settings/valuecan4-2elsettings.h @@ -11,7 +11,7 @@ namespace icsneo { class ValueCAN4_2ELSettings : public ValueCAN4_4_2ELSettings { public: ValueCAN4_2ELSettings(std::shared_ptr com) : ValueCAN4_4_2ELSettings(com) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: @@ -22,7 +22,7 @@ public: return nullptr; } } - CANFD_SETTINGS* getCANFDSettingsFor(Network net) override { + const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/device/valuecan4/settings/valuecan4-2settings.h b/include/icsneo/device/valuecan4/settings/valuecan4-2settings.h index 0830b9c..023c960 100644 --- a/include/icsneo/device/valuecan4/settings/valuecan4-2settings.h +++ b/include/icsneo/device/valuecan4/settings/valuecan4-2settings.h @@ -10,7 +10,7 @@ namespace icsneo { class ValueCAN4_2Settings : public ValueCAN4_1_2Settings { public: ValueCAN4_2Settings(std::shared_ptr com) : ValueCAN4_1_2Settings(com) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: @@ -21,7 +21,7 @@ public: return nullptr; } } - CANFD_SETTINGS* getCANFDSettingsFor(Network net) override { + const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/device/valuecan4/settings/valuecan4-4settings.h b/include/icsneo/device/valuecan4/settings/valuecan4-4settings.h index dc55f3b..ddf8e1d 100644 --- a/include/icsneo/device/valuecan4/settings/valuecan4-4settings.h +++ b/include/icsneo/device/valuecan4/settings/valuecan4-4settings.h @@ -11,7 +11,7 @@ namespace icsneo { class ValueCAN4_4Settings : public ValueCAN4_4_2ELSettings { public: ValueCAN4_4Settings(std::shared_ptr com) : ValueCAN4_4_2ELSettings(com) {} - CAN_SETTINGS* getCANSettingsFor(Network net) override { + const CAN_SETTINGS* getCANSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: @@ -26,7 +26,7 @@ public: return nullptr; } } - CANFD_SETTINGS* getCANFDSettingsFor(Network net) override { + const CANFD_SETTINGS* getCANFDSettingsFor(Network net) const override { auto cfg = getStructurePointer(); switch(net.getNetID()) { case Network::NetID::HSCAN: diff --git a/include/icsneo/icsneoc.h b/include/icsneo/icsneoc.h index 0e64434..81885d2 100644 --- a/include/icsneo/icsneoc.h +++ b/include/icsneo/icsneoc.h @@ -357,6 +357,17 @@ extern bool DLLExport icsneo_settingsApplyDefaults(const neodevice_t* device); */ extern bool DLLExport icsneo_settingsApplyDefaultsTemporary(const neodevice_t* device); +/** + * \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. + * \param[in] netid The network for which the baudrate should be retrieved. + * \returns The value in baud with no multipliers. (i.e. 500k becomes 500000) A negative value is returned if an error occurs. + * + * In the case of CAN, this function gets the standard CAN baudrate. + * See icsneo_getFDBaudrate() to get the baudrate for (the baudrate-switched portion of) CAN FD. + */ +extern int64_t DLLExport icsneo_getBaudrate(const neodevice_t* device, uint16_t netid); + /** * \brief Set the network baudrate for a specified device. * \param[in] device A pointer to the neodevice_t structure specifying the device to operate on. @@ -369,7 +380,17 @@ extern bool DLLExport icsneo_settingsApplyDefaultsTemporary(const neodevice_t* d * * Call icsneo_settingsApply() or similar to make the changes active on the 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, int64_t newBaudrate); + +/** + * \brief Get the CAN FD baudrate for a specified device. + * \param[in] device A pointer to the neodevice_t structure specifying the device to operate on. + * \param[in] netid The network for which the baudrate should be retrieved. + * \returns The value in baud with no multipliers. (i.e. 500k becomes 500000) A negative value is returned if an error occurs. + * + * See icsneo_getBaudrate() to get the baudrate for the non baudrate-switched portion of CAN FD, classical CAN 2.0, and other network types. + */ +extern int64_t DLLExport icsneo_getFDBaudrate(const neodevice_t* device, uint16_t netid); /** * \brief Set the CAN FD baudrate for a specified device. @@ -378,9 +399,11 @@ extern bool DLLExport icsneo_setBaudrate(const neodevice_t* device, uint16_t net * \param[in] newBaudrate The requested baudrate, with no multipliers. (i.e. 2Mbaud CAN FD should be represented as 2000000) * \returns True if the baudrate could be set. * + * See icsneo_setBaudrate() to set the baudrate for the non baudrate-switched portion of CAN FD, classical CAN 2.0, and other network types. + * * Call icsneo_settingsApply() or similar to make the changes active on the device. */ -extern bool DLLExport icsneo_setFDBaudrate(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate); +extern bool DLLExport icsneo_setFDBaudrate(const neodevice_t* device, uint16_t netid, int64_t newBaudrate); /** * \brief Transmit a single message. @@ -626,10 +649,16 @@ 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); +typedef int64_t(*fn_icsneo_getBaudrate)(const neodevice_t* device, uint16_t netid); +fn_icsneo_getBaudrate icsneo_getBaudrate; + +typedef bool(*fn_icsneo_setBaudrate)(const neodevice_t* device, uint16_t netid, int64_t newBaudrate); fn_icsneo_setBaudrate icsneo_setBaudrate; -typedef bool(*fn_icsneo_setFDBaudrate)(const neodevice_t* device, uint16_t netid, uint32_t newBaudrate); +typedef int64_t(*fn_icsneo_getFDBaudrate)(const neodevice_t* device, uint16_t netid); +fn_icsneo_getFDBaudrate icsneo_getFDBaudrate; + +typedef bool(*fn_icsneo_setFDBaudrate)(const neodevice_t* device, uint16_t netid, int64_t newBaudrate); fn_icsneo_setFDBaudrate icsneo_setFDBaudrate; typedef bool(*fn_icsneo_transmit)(const neodevice_t* device, const neomessage_t* message); @@ -700,7 +729,9 @@ int icsneo_init() { ICSNEO_IMPORTASSERT(icsneo_settingsApplyTemporary); ICSNEO_IMPORTASSERT(icsneo_settingsApplyDefaults); ICSNEO_IMPORTASSERT(icsneo_settingsApplyDefaultsTemporary); + ICSNEO_IMPORTASSERT(icsneo_getBaudrate); ICSNEO_IMPORTASSERT(icsneo_setBaudrate); + ICSNEO_IMPORTASSERT(icsneo_getFDBaudrate); ICSNEO_IMPORTASSERT(icsneo_setFDBaudrate); ICSNEO_IMPORTASSERT(icsneo_transmit); ICSNEO_IMPORTASSERT(icsneo_transmitMessages);