diff --git a/api/icsneoc/icsneoc.cpp b/api/icsneoc/icsneoc.cpp index 29eb4e6..3aebd46 100644 --- a/api/icsneoc/icsneoc.cpp +++ b/api/icsneoc/icsneoc.cpp @@ -143,6 +143,15 @@ bool icsneo_closeDevice(const neodevice_t* device) { return true; } +bool icsneo_isOpen(const neodevice_t* device) { + if(!icsneo_isValidNeoDevice(device)) { + ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); + return false; + } + + return device->device->isOpen(); +} + bool icsneo_goOnline(const neodevice_t* device) { if(!icsneo_isValidNeoDevice(device)) { ErrorManager::GetInstance().add(APIError::InvalidNeoDevice); diff --git a/device/device.cpp b/device/device.cpp index 0d121a8..1459706 100644 --- a/device/device.cpp +++ b/device/device.cpp @@ -135,6 +135,10 @@ void Device::enforcePollingMessageLimit() { } bool Device::open() { + if(opened) { + return false; + } + if(!com) { err(APIError::Unknown); return false; @@ -170,10 +174,15 @@ bool Device::open() { handleInternalMessage(message); })); + opened = true; return true; } bool Device::close() { + if(!opened) { + return false; + } + if(!com) { err(APIError::Unknown); return false; @@ -183,7 +192,8 @@ bool Device::close() { com->removeMessageCallback(internalHandlerCallbackID); goOffline(); - return com->close(); + opened = !com->close(); + return !opened; } bool Device::goOnline() { diff --git a/include/icsneo/device/device.h b/include/icsneo/device/device.h index 410d391..2871968 100644 --- a/include/icsneo/device/device.h +++ b/include/icsneo/device/device.h @@ -44,6 +44,7 @@ public: virtual bool open(); virtual bool close(); virtual bool isOnline() const { return online; } + virtual bool isOpen() const { return opened; } virtual bool goOnline(); virtual bool goOffline(); @@ -83,6 +84,7 @@ public: protected: uint16_t productId = 0; bool online = false; + bool opened = false; int messagePollingCallbackID = 0; int internalHandlerCallbackID = 0; device_errorhandler_t err; diff --git a/include/icsneo/icsneoc.h b/include/icsneo/icsneoc.h index e27d0aa..362c515 100644 --- a/include/icsneo/icsneoc.h +++ b/include/icsneo/icsneoc.h @@ -115,6 +115,17 @@ extern bool DLLExport icsneo_openDevice(const neodevice_t* device); */ extern bool DLLExport icsneo_closeDevice(const neodevice_t* device); +/** + * \brief Verify network connection for the specified hardware + * \param[in] device A pointer to the neodevice_t structure specifying the device to operate on. + * \returns True if the device is connected. + * + * This function does not modify the working state of the device at all. + * + * See icsneo_openDevice() for an explanation about the concept of being "open". + */ +extern bool DLLExport icsneo_isOpen(const neodevice_t* device); + /** * \brief Enable network communication for the specified hardware * \param[in] device A pointer to the neodevice_t structure specifying the device to operate on. @@ -135,7 +146,7 @@ extern bool DLLExport icsneo_goOnline(const neodevice_t* device); * \param[in] device A pointer to the neodevice_t structure specifying the device to operate on. * \returns True if communication could be disabled. * - * See icsneo_goOnline() for an explaination about the concept of being "online". + * See icsneo_goOnline() for an explanation about the concept of being "online". */ extern bool DLLExport icsneo_goOffline(const neodevice_t* device); @@ -146,7 +157,7 @@ extern bool DLLExport icsneo_goOffline(const neodevice_t* device); * * This function does not modify the working state of the device at all. * - * See icsneo_goOnline() for an explaination about the concept of being "online". + * See icsneo_goOnline() for an explanation about the concept of being "online". */ extern bool DLLExport icsneo_isOnline(const neodevice_t* device); @@ -667,6 +678,9 @@ fn_icsneo_openDevice icsneo_openDevice; typedef bool(*fn_icsneo_closeDevice)(const neodevice_t* device); fn_icsneo_closeDevice icsneo_closeDevice; +typedef bool(*fn_icsneo_isOpen)(const neodevice_t* device); +fn_icsneo_isOpen icsneo_isOpen; + typedef bool(*fn_icsneo_goOnline)(const neodevice_t* device); fn_icsneo_goOnline icsneo_goOnline; @@ -784,6 +798,7 @@ int icsneo_init() { ICSNEO_IMPORTASSERT(icsneo_isValidNeoDevice); ICSNEO_IMPORTASSERT(icsneo_openDevice); ICSNEO_IMPORTASSERT(icsneo_closeDevice); + ICSNEO_IMPORTASSERT(icsneo_isOpen); ICSNEO_IMPORTASSERT(icsneo_goOnline); ICSNEO_IMPORTASSERT(icsneo_goOffline); ICSNEO_IMPORTASSERT(icsneo_isOnline);