diff --git a/src/vscp/common/register.cpp b/src/vscp/common/register.cpp index 3bdacd0c4..1c2a12a2b 100644 --- a/src/vscp/common/register.cpp +++ b/src/vscp/common/register.cpp @@ -241,7 +241,8 @@ vscp_readLevel1RegisterBlock(CVscpClient &client, uint8_t offset, uint8_t count, std::map &values, - uint32_t timeout) + uint32_t timeout, + std::function statusCallback) { int rv = VSCP_ERROR_ERROR; uint8_t rcvcnt = 0; // Number of registers read @@ -284,7 +285,8 @@ vscp_readLevel1RegisterBlock(CVscpClient &client, return rv; } - uint32_t startTime = vscp_getMsTimeStamp(); + uint32_t startTime = vscp_getMsTimeStamp(); + uint32_t callbackTime = vscp_getMsTimeStamp(); // Wait for reponse @@ -336,12 +338,23 @@ vscp_readLevel1RegisterBlock(CVscpClient &client, break; } + /*! + if a callback is defined call it every half second + and report presentage of operation complete + */ + if (nullptr != statusCallback) { + if ((vscp_getMsTimeStamp() - callbackTime) > 500) { + statusCallback((100 * (int) count) / (int) rcvcnt); + callbackTime = vscp_getMsTimeStamp(); + } + } + #ifdef WIN32 win_usleep(2000); #else usleep(2000); #endif - //printf("l2\n"); + // printf("l2\n"); } while (true); return rv; @@ -357,7 +370,8 @@ vscp_writeLevel1RegisterBlock(CVscpClient &client, cguid &guidInterface, uint16_t page, std::map ®values, - uint32_t timeout) + uint32_t timeout, + std::function statusCallback) { int rv = VSCP_ERROR_SUCCESS; CVscpClient::connType conntype = client.getType(); @@ -424,7 +438,8 @@ vscp_writeLevel1RegisterBlock(CVscpClient &client, return rv; } - uint32_t startTime = vscp_getMsTimeStamp(); + uint32_t startTime = vscp_getMsTimeStamp(); + uint32_t callbackTime = vscp_getMsTimeStamp(); // Wait for reponse @@ -473,6 +488,14 @@ vscp_writeLevel1RegisterBlock(CVscpClient &client, } while (timeout); + /*! + if a callback is defined call it every half second + and report presentage of operation complete + */ + if (nullptr != statusCallback) { + statusCallback((100 * i) / nwrites); + } + } // writes } // startmap @@ -485,7 +508,11 @@ vscp_writeLevel1RegisterBlock(CVscpClient &client, // int -vscp_scanForDevices(CVscpClient &client, cguid &guidIf, std::set &found, uint32_t timeout) +vscp_scanForDevices(CVscpClient &client, + cguid &guidIf, + std::set &found, + uint32_t timeout, + std::function statusCallback) { int rv = VSCP_ERROR_SUCCESS; uint8_t offset = guidIf.isNULL() ? 0 : 16; @@ -513,6 +540,7 @@ vscp_scanForDevices(CVscpClient &client, cguid &guidIf, std::set &foun } uint32_t startTime = vscp_getMsTimeStamp(); + uint32_t callbackTime = vscp_getMsTimeStamp(); while (true) { @@ -534,6 +562,17 @@ vscp_scanForDevices(CVscpClient &client, cguid &guidIf, std::set &foun break; } + /*! + if a callback is defined call it every half second + and report presentage of operation complete + */ + if (nullptr != statusCallback) { + if ((vscp_getMsTimeStamp() - callbackTime) > 500) { + statusCallback(found.size()); + callbackTime = vscp_getMsTimeStamp(); + } + } + #ifdef WIN32 win_usleep(100); #else @@ -553,7 +592,8 @@ vscp_scanSlowForDevices(CVscpClient &client, std::set &search_nodes, std::set &found_nodes, uint32_t delay, - uint32_t timeout) + uint32_t timeout, + std::function statusCallback) { uint8_t offset = guidIf.isNULL() ? 0 : 16; int rv = VSCP_ERROR_SUCCESS; @@ -595,6 +635,7 @@ vscp_scanSlowForDevices(CVscpClient &client, } uint32_t startTime = vscp_getMsTimeStamp(); + uint32_t callbackTime = vscp_getMsTimeStamp(); while (true) { @@ -627,6 +668,17 @@ vscp_scanSlowForDevices(CVscpClient &client, break; } + /*! + if a callback is defined call it every half second + and report presentage of operation complete + */ + if (nullptr != statusCallback) { + if ((vscp_getMsTimeStamp() - callbackTime) > 500) { + statusCallback((100*found_nodes.size())/search_nodes.size()); + callbackTime = vscp_getMsTimeStamp(); + } + } + #ifdef WIN32 win_usleep(100); #else @@ -648,7 +700,8 @@ vscp_scanSlowForDevices(CVscpClient &client, uint8_t end_node, std::set &found_nodes, uint32_t delay, - uint32_t timeout) + uint32_t timeout, + std::function statusCallback) { std::set search_nodes; @@ -656,7 +709,7 @@ vscp_scanSlowForDevices(CVscpClient &client, search_nodes.insert(i); } - return vscp_scanSlowForDevices(client, guid, search_nodes, found_nodes, delay, timeout); + return vscp_scanSlowForDevices(client, guid, search_nodes, found_nodes, delay, timeout, statusCallback); } // ---------------------------------------------------------------------------- @@ -1238,12 +1291,24 @@ CStandardRegisters::~CStandardRegisters() // int -CStandardRegisters::init(CVscpClient &client, cguid &guidNode, cguid &guidInterface, uint32_t timeout) +CStandardRegisters::init(CVscpClient &client, + cguid &guidNode, + cguid &guidInterface, + uint32_t timeout, + std::function statusCallback) { int rv; m_regs.clear(); m_change.clear(); - rv = vscp_readLevel1RegisterBlock(client, guidNode, guidInterface, 0, 0x80, 128, m_regs, timeout); + rv = vscp_readLevel1RegisterBlock(client, + guidNode, + guidInterface, + 0, + 0x80, + 128, + m_regs, + timeout, + statusCallback); return rv; } @@ -1272,7 +1337,10 @@ CStandardRegisters::init(CRegisterPage ®Page) // int -restoreStandardConfig(CVscpClient &client, cguid &guidNode, cguid &guidInterface, uint32_t timeout) +restoreStandardConfig(CVscpClient &client, + cguid &guidNode, + cguid &guidInterface, + uint32_t timeout) { int rv; rv = vscp_writeLevel1Register(client, guidNode, guidInterface, 0, 0xA2, 0x55, timeout); @@ -1371,7 +1439,8 @@ CUserRegisters::init(CVscpClient &client, cguid &guidNode, cguid &guidInterface, std::set &pages, - uint32_t timeout) + uint32_t timeout, + std::function statusCallback) { int rv = VSCP_ERROR_SUCCESS; m_pages.clear(); @@ -1380,7 +1449,15 @@ CUserRegisters::init(CVscpClient &client, std::map registers; m_registerPageMap[page] = new CRegisterPage(m_level, page); - rv = vscp_readLevel1RegisterBlock(client, guidNode, guidInterface, page, 0, 127, registers, timeout); + rv = vscp_readLevel1RegisterBlock(client, + guidNode, + guidInterface, + page, + 0, + 127, + registers, + timeout, + statusCallback); if (VSCP_ERROR_SUCCESS != rv) { return rv; } diff --git a/src/vscp/common/register.h b/src/vscp/common/register.h index b33d562f6..30df9fe9b 100644 --- a/src/vscp/common/register.h +++ b/src/vscp/common/register.h @@ -104,6 +104,8 @@ vscp_writeLevel1Register(CVscpClient &client, @param count Number of registers to read. Zero means read 256 registers (0-255). @param values Pointer to map with registers to read. @param timeout Timeout in milliseconds. Zero means no timeout i.e. wait forever. + @param statusCallback Optional callback that return status information + in percent void f(int) @return VSCP_ERROR_SUCCESS on success. */ int @@ -114,7 +116,8 @@ vscp_readLevel1RegisterBlock(CVscpClient &client, uint8_t offset, uint8_t count, std::map &values, - uint32_t timeout = 2000); + uint32_t timeout = 2000, + std::function statusCallback = nullptr); /*! Write VSCP register block. @@ -128,6 +131,8 @@ vscp_readLevel1RegisterBlock(CVscpClient &client, @param page Register page to read from. @param values Pointer to map with register values to write. @param timeout Timeout in milliseconds. Zero means no timeout i.e. wait forever. + @param statusCallback Optional callback that return status information + in percent void f(int) @return VSCP_ERROR_SUCCESS on success. */ int @@ -136,17 +141,29 @@ vscp_writeLevel1RegisterBlock(CVscpClient &client, cguid &guidInterface, uint16_t page, std::map &values, - uint32_t timeout = 2000); + uint32_t timeout = 2000, + std::function statusCallback = nullptr); /*! Read all standard registers + @param client VSCP client derived from the client vase class over + which the communication is carried out. + @param guid GUID of the device to read from. Only the lsb (nickname) + is used for level I communication. + @param guidInterface GUID of the interface to read from. Set to all zero + if no interface. + @param timeout Timeout in milliseconds. Zero means no timeout i.e. wait forever. + @param statusCallback Optional callback that return status information + in percent void f(int) + @return VSCP_ERROR_SUCCESS on success. */ int vscp_readStandardRegisters(CVscpClient &client, cguid &guid, cguid &guidInterface, CStandardRegisters &stdregs, - uint32_t timeout = 2000); + uint32_t timeout = 2000, + std::function statusCallback = nullptr); /*! Do a fast register scan using who is there protocol functionality @@ -156,9 +173,16 @@ vscp_readStandardRegisters(CVscpClient &client, is used. @param found A set with nodeid's for found nodes. @param timeout Timeout in milliseconds. Zero means no timeout + @param statusCallback Optional callback that return status information + with found devices (not percentage) void f(int) + @return VSCP_ERROR_SUCCESS on success. */ int -vscp_scanForDevices(CVscpClient &client, cguid &guid, std::set &found, uint32_t timeout = 2000); +vscp_scanForDevices(CVscpClient &client, + cguid &guid, + std::set &found, + uint32_t timeout = 2000, + std::function statusCallback = nullptr); /*! Do a slow register scan using read register (firts byte of GUID) @@ -170,14 +194,18 @@ vscp_scanForDevices(CVscpClient &client, cguid &guid, std::set &found, @param found_nodes A set with nodeid's for found nodes. @param delay Delay in micro seconds between nodeid's to search. @param timeout Timeout in milliseconds. Zero means no timeout + @param statusCallback Optional callback that return status information + in percent void f(int) + @return VSCP_ERROR_SUCCESS on success. */ int vscp_scanSlowForDevices(CVscpClient &client, cguid &guid, std::set &search_nodes, std::set &found_nodes, - uint32_t delay = 10000, - uint32_t timeout = 2000); + uint32_t delay = 10000, + uint32_t timeout = 2000, + std::function statusCallback = nullptr); /*! Do a slow register scan using register read functionality @@ -190,6 +218,9 @@ vscp_scanSlowForDevices(CVscpClient &client, @param found_nodes A set with nodeid's for found nodes. @param delay Delay in micro seconds between nodeid's to search. @param timeout Timeout in milliseconds. Zero means no timeout + @param statusCallback Optional callback that return status information + in percent void f(int) + @return VSCP_ERROR_SUCCESS on success. */ int vscp_scanSlowForDevices(CVscpClient &client, @@ -197,8 +228,9 @@ vscp_scanSlowForDevices(CVscpClient &client, uint8_t start_node, uint8_t end_node, std::set &found_nodes, - uint32_t delay = 10000, - uint32_t timeout = 2000); + uint32_t delay = 10000, + uint32_t timeout = 2000, + std::function statusCallback = nullptr); /*! Get device information on HTML format @@ -418,13 +450,16 @@ class CUserRegisters { @param guidInterface GUID for interface. If zero no interface is used. @param pages A set holding the valied pages @param timeout Timeout in milliseconds. Zero means no timeout + @param statusCallback Optional callback that return status information + in percent void f(int) @return VSCP_ERROR_SUCCESS on success. */ int init(CVscpClient &client, cguid &guidNode, cguid &guidInterface, std::set &pages, - uint32_t timeout = 1000); + uint32_t timeout = 1000, + std::function statusCallback = nullptr); /*! Set value for register @@ -809,9 +844,15 @@ class CStandardRegisters { @param guidNode GUID for node. Only LSB is used for level I node. @param guidInterface GUID for interface. If zero no interface is used. @param timeout Timeout in milliseconds. Zero means no timeout + @param statusCallback Optional callback that return status information + in percent void f(int) @return VSCP_ERROR_SUCCESS on success. */ - int init(CVscpClient &client, cguid &guidNode, cguid &guidInterface, uint32_t timeout = 1000); + int init(CVscpClient &client, + cguid &guidNode, + cguid &guidInterface, + uint32_t timeout = 1000, + std::function statusCallback = nullptr); /*! Initialization with already read standard registers @@ -953,7 +994,10 @@ class CStandardRegisters { /*! Restore standard configuration for node */ - int restoreStandardConfig(CVscpClient &client, cguid &guidNode, cguid &guidInterface, uint32_t timeout = 1000); + int restoreStandardConfig(CVscpClient &client, + cguid &guidNode, + cguid &guidInterface, + uint32_t timeout = 1000); /*! Get firmare device code (added in 1.13)