From 92368f70a59be14804a3cc72923824453fd44d6e Mon Sep 17 00:00:00 2001 From: EricLiu2000 Date: Tue, 18 Jun 2019 11:38:12 -0400 Subject: [PATCH] Added error checking for polling --- api/icsneoc/icsneoc.cpp | 3 +-- api/icsneocpp/error.cpp | 20 ++++++++++++-------- device/device.cpp | 19 +++++++++++-------- include/icsneo/api/error.h | 7 ++++--- include/icsneo/device/device.h | 2 +- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/api/icsneoc/icsneoc.cpp b/api/icsneoc/icsneoc.cpp index 9c2447f..4bf7d92 100644 --- a/api/icsneoc/icsneoc.cpp +++ b/api/icsneoc/icsneoc.cpp @@ -178,8 +178,7 @@ bool icsneo_enableMessagePolling(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) return false; - device->device->enableMessagePolling(); - return true; + return device->device->enableMessagePolling(); } bool icsneo_disableMessagePolling(const neodevice_t* device) { diff --git a/api/icsneocpp/error.cpp b/api/icsneocpp/error.cpp index 2139e3e..acfdb46 100644 --- a/api/icsneocpp/error.cpp +++ b/api/icsneocpp/error.cpp @@ -47,11 +47,12 @@ static constexpr const char* ERROR_REQUIRED_PARAMETER_NULL = "A required paramet static constexpr const char* ERROR_BUFFER_INSUFFICIENT = "The provided buffer was insufficient. No data was written."; static constexpr const char* ERROR_OUTPUT_TRUNCATED = "The output was too large for the provided buffer and has been truncated."; static constexpr const char* ERROR_PARAMETER_OUT_OF_RANGE = "A parameter was out of range."; -static constexpr const char* ERROR_DEVICE_CURRENTLY_OPEN = "The device is currently open. Perhaps a different device state was required."; -static constexpr const char* ERROR_DEVICE_CURRENTLY_CLOSED = "The device is currently closed. Perhaps a different device state was required."; -static constexpr const char* ERROR_DEVICE_CURRENTLY_ONLINE = "The device is currently online. Perhaps a different device state was required."; -static constexpr const char* ERROR_DEVICE_CURRENTLY_OFFLINE = "The device is currently offline. Perhaps a different device state was required."; -static constexpr const char* ERROR_DEVICE_NOT_POLLING = "The device is not currently polling for messages."; +static constexpr const char* ERROR_DEVICE_CURRENTLY_OPEN = "The device is currently open. Perhaps a different device state is required."; +static constexpr const char* ERROR_DEVICE_CURRENTLY_CLOSED = "The device is currently closed. Perhaps a different device state is required."; +static constexpr const char* ERROR_DEVICE_CURRENTLY_ONLINE = "The device is currently online. Perhaps a different device state is required."; +static constexpr const char* ERROR_DEVICE_CURRENTLY_OFFLINE = "The device is currently offline. Perhaps a different device state is required."; +static constexpr const char* ERROR_DEVICE_CURRENTLY_POLLING = "The device is currently polling for messages. Perhaps a different device state is required."; +static constexpr const char* ERROR_DEVICE_NOT_CURRENTLY_POLLING = "The device is not currently polling for messages. Perhaps a different device state is required."; static constexpr const char* ERROR_UNSUPPORTED_TX_NETWORK = "Message network is not a supported TX network."; static constexpr const char* ERROR_MESSAGE_MAX_LENGTH_EXCEEDED = "The message was too long."; @@ -114,8 +115,10 @@ const char* APIError::DescriptionForType(ErrorType type) { return ERROR_DEVICE_CURRENTLY_ONLINE; case DeviceCurrentlyOffline: return ERROR_DEVICE_CURRENTLY_OFFLINE; - case DeviceNotPolling: - return ERROR_DEVICE_NOT_POLLING; + case DeviceCurrentlyPolling: + return ERROR_DEVICE_CURRENTLY_POLLING; + case DeviceNotCurrentlyPolling: + return ERROR_DEVICE_NOT_CURRENTLY_POLLING; case UnsupportedTXNetwork: return ERROR_UNSUPPORTED_TX_NETWORK; case MessageMaxLengthExceeded: @@ -205,7 +208,8 @@ APIError::Severity APIError::SeverityForType(ErrorType type) { case DeviceCurrentlyClosed: case DeviceCurrentlyOnline: case DeviceCurrentlyOffline: - case DeviceNotPolling: + case DeviceCurrentlyPolling: + case DeviceNotCurrentlyPolling: // Device Warnings case PollingMessageOverflow: case DeviceFirmwareOutOfDate: diff --git a/device/device.cpp b/device/device.cpp index 6759c4a..2e2472d 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -77,20 +77,23 @@ std::string Device::describe() const { return ss.str(); } -void Device::enableMessagePolling() { - if(isMessagePollingEnabled()) // We are already polling - return; - +bool Device::enableMessagePolling() { + if(isMessagePollingEnabled()) {// We are already polling + err(APIError::DeviceCurrentlyPolling); + return false; + } messagePollingCallbackID = com->addMessageCallback(MessageCallback([this](std::shared_ptr message) { pollingContainer.enqueue(message); enforcePollingMessageLimit(); })); + return true; } bool Device::disableMessagePolling() { - if(!isMessagePollingEnabled()) - return true; // Not currently polling - + if(!isMessagePollingEnabled()) { + err(APIError::DeviceNotCurrentlyPolling); + return false; // Not currently polling + } auto ret = com->removeMessageCallback(messagePollingCallbackID); getMessages(); // Flush any messages still in the container messagePollingCallbackID = 0; @@ -126,7 +129,7 @@ bool Device::getMessages(std::vector>& container, size_ // not currently polling, throw error if(!isMessagePollingEnabled()) { - err(APIError::DeviceNotPolling); + err(APIError::DeviceNotCurrentlyPolling); return false; } diff --git a/include/icsneo/api/error.h b/include/icsneo/api/error.h index 077b985..f04288e 100644 --- a/include/icsneo/api/error.h +++ b/include/icsneo/api/error.h @@ -42,9 +42,10 @@ public: DeviceCurrentlyClosed = 0x1006, DeviceCurrentlyOnline = 0x1007, DeviceCurrentlyOffline = 0x1008, - DeviceNotPolling = 0x1009, - UnsupportedTXNetwork = 0x1010, - MessageMaxLengthExceeded = 0x1011, + DeviceCurrentlyPolling = 0x1009, + DeviceNotCurrentlyPolling = 0x1010, + UnsupportedTXNetwork = 0x1011, + MessageMaxLengthExceeded = 0x1012, // Device Errors PollingMessageOverflow = 0x2000, diff --git a/include/icsneo/device/device.h b/include/icsneo/device/device.h index dc0b4f3..f874d7a 100644 --- a/include/icsneo/device/device.h +++ b/include/icsneo/device/device.h @@ -50,7 +50,7 @@ public: virtual bool goOffline(); // Message polling related functions - void enableMessagePolling(); + bool enableMessagePolling(); bool disableMessagePolling(); bool isMessagePollingEnabled() { return messagePollingCallbackID != 0; }; std::vector> getMessages();