diff --git a/lib/App/common/deviceNode.cpp b/lib/App/common/deviceNode.cpp index 8e4c4879..15b69109 100644 --- a/lib/App/common/deviceNode.cpp +++ b/lib/App/common/deviceNode.cpp @@ -88,7 +88,12 @@ bool deviceNode::validate() DBC_NODE_DATA * tDATA = m_pDBC->getNodeData(m_sNodeData.node_id); if (tDATA) { - if (memcmp(&m_sNodeData, tDATA, sizeof(DBC_NODE_DATA)) != 0) + // Configuration only: "online" is written by every node object of this id (setNodeOnline / + // setNodeOffline) - deleting a second object of the same node made this one "changed" + bool tChanged = (m_sNodeData.aux_id != tDATA->aux_id) || (m_sNodeData.aux_parameter != tDATA->aux_parameter) || (m_sNodeData.type != tDATA->type) || + (m_sNodeData.enabled != tDATA->enabled) || (m_sNodeData.tss != tDATA->tss) || (m_sNodeData.tsm != tDATA->tsm) || + (strncmp(m_sNodeData.name, tDATA->name, sizeof(m_sNodeData.name)) != 0); + if (tChanged) { free(tDATA); LOG("Node Settings Changed!"); diff --git a/lib/App/common/deviceNodeEthernet2Serial.cpp b/lib/App/common/deviceNodeEthernet2Serial.cpp index f9719a21..b7e612fc 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.cpp +++ b/lib/App/common/deviceNodeEthernet2Serial.cpp @@ -3,6 +3,11 @@ #include "../../data/protocol/protocol.h" #include "hardwareManager.h" +#include +#include +#include +#include + // #define DEBUG_EXTRA // #define DEBUG_SERIAL_COMMUNICATION @@ -40,22 +45,45 @@ deviceETH2UART::deviceETH2UART(uint32_t nodeID) : m_iMasterFD = -1; m_iInFID = -1; m_iOutFID = -1; + m_bRegistered = false; + m_bLastPingOk = true; + m_nNextPingMs = 0; + m_nNextConnectMs = 0; + m_nConnectBackoffMs = 0; hardwareResource::create(m_sNodeData.aux_parameter, HARDWARE_PROVIDER_SERIAL); } +// Status of the I/O thread (sender thread in the dual-thread variant) +#ifdef SERIAL_PING_PONG + #define E2U_IO_STATUS m_bThreadStatus +#else + #define E2U_IO_STATUS m_bThreadAStatus +#endif + +static uint64_t e2uNowMs(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t) ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} + deviceETH2UART::~deviceETH2UART() { - closePort(); + // Leave the hardware manager first: its validate() loop holds the resource list locked while it + // calls validate() / create() on us, so once this returns it no longer uses this object hardwareManager * tH = hardwareManager::getInstance(); if (tH) { tH->resignResource(this); } + std::lock_guard tLock(m_mConnLock); + closePort(); } bool deviceETH2UART::create(void) { - DBC_UART_DATA * tDATA = NULL; + std::lock_guard tLock(m_mConnLock); + DBC_UART_DATA * tDATA = NULL; if (m_sResourceSettings.id == 0) { return false; @@ -73,28 +101,95 @@ bool deviceETH2UART::create(void) LOG(m_sUartData.path); #endif free(tDATA); - m_bValid = create_SOL(); + // Also called by the hardware manager to re-create the resource: obeys the back-off + m_bValid = connectSOL(); }; - if (m_bValid) + if (m_bValid && ! m_bRegistered) { + // Once only: the hardware manager calls create() again with its resource list locked - + // assigning from there locked the (non recursive) list mutex twice and added us twice hardwareManager * tH = hardwareManager::getInstance(); if (tH) { tH->assignResource(this); + m_bRegistered = true; } }; return m_bValid; } +/* + Is the link to the gateway still up? + - TCP (SOL_TCP_SERVER: we are the client): the TCP connection itself (socket, I/O thread and + keepalive). ICMP is not used: gateways reached over the Internet (mobile networks, firewalls) + often do not answer ping although the TCP port works - the working connection was closed every + second ("No Ping to device!"). + - UDP: no connection state, ICMP echo (at most every E2UART_PING_INTERVAL_MS). +*/ +bool deviceETH2UART::linkAlive(void) +{ + if (m_sUartData.mode == SOL_TCP_SERVER) + { + if (m_iDeviceFD < 0 || m_eConnectionStatus != CS_CONNECTED || E2U_IO_STATUS == TH_PRE_INIT || E2U_IO_STATUS >= TH_ENDED) + { + LOG_ERROR("TCP connection to the gateway lost"); + return false; + }; + int tErr = 0; + socklen_t tLen = sizeof(tErr); + if (getsockopt(m_iDeviceFD, SOL_SOCKET, SO_ERROR, &tErr, &tLen) == 0 && tErr != 0) + { + char tB[160]; + snprintf(tB, sizeof(tB), "TCP connection to the gateway failed: %s", strerror(tErr)); + LOG_ERROR(tB); + return false; + }; + return true; + }; + uint64_t tNow = e2uNowMs(); + if (tNow < m_nNextPingMs) + { + return m_bLastPingOk; + }; + m_bLastPingOk = pingDevice(); + m_nNextPingMs = e2uNowMs() + E2UART_PING_INTERVAL_MS; + return m_bLastPingOk; +} + +bool deviceETH2UART::connectSOL(void) +{ + uint64_t tNow = e2uNowMs(); + if (tNow < m_nNextConnectMs) + { + return false; // waiting for the next attempt (a gateway that is down is not hammered) + }; + if (create_SOL()) + { + m_nConnectBackoffMs = 0; + m_nNextConnectMs = 0; + m_nNextPingMs = 0; + m_bLastPingOk = true; + return true; + }; + m_nConnectBackoffMs = (m_nConnectBackoffMs == 0) ? E2UART_RECONNECT_MIN_MS : m_nConnectBackoffMs * 2; + if (m_nConnectBackoffMs > E2UART_RECONNECT_MAX_MS) + { + m_nConnectBackoffMs = E2UART_RECONNECT_MAX_MS; + }; + m_nNextConnectMs = e2uNowMs() + m_nConnectBackoffMs; + return false; +} + bool deviceETH2UART::validate(void) { + std::lock_guard tLock(m_mConnLock); if (m_bValid) { - m_bValid = pingDevice(); + m_bValid = linkAlive(); if (! m_bValid) { - LOG_ERROR("No Ping to device!"); + LOG_ERROR("No connection to the device!"); closePort(); }; }; @@ -123,16 +218,14 @@ bool deviceETH2UART::validate(void) #ifdef DEBUG LOG("Settings Has been Changed!"); #endif - if (m_bValid) - { - closePort(); // if port not closed - } + closePort(); // create_SOL() closes too; also stops the I/O thread of the old settings // Apply new settings memcpy(&m_sUartData, tDATA, sizeof(DBC_UART_DATA)); #ifdef DEBUG LOG("Reconect with new settings"); #endif - m_bValid = create_SOL(); + m_nNextConnectMs = 0; // new address: no back-off + m_bValid = connectSOL(); }; free(tDATA); } @@ -140,13 +233,13 @@ bool deviceETH2UART::validate(void) { LOG_ERROR("Error Fetch DB Data!"); }; - // Finally in any case If Not Connected -> try to Connect! - if (! m_bValid) + // Finally in any case If Not Connected -> try to Connect (after the back-off) + if (! m_bValid && e2uNowMs() >= m_nNextConnectMs) { #ifdef DEBUG LOG("Try to Conncet"); #endif - m_bValid = create_SOL(); + m_bValid = connectSOL(); }; if (m_bValid) { @@ -177,103 +270,122 @@ struct ping_pkt char msg[PING_PKT_S - sizeof(struct icmphdr)]; }; +/* + ICMP echo to the gateway: up to E2UART_PING_ATTEMPTS requests, each waiting E2UART_PING_TIMEOUT_MS. + Before: one request and one recvfrom() (3 s - the 300 ms set first was overwritten). A single lost + packet failed the check, a signal (EINTR) ended the wait at once, and ANY ICMP packet received by + the raw socket (e.g. the reply to another gateway's ping) counted as the answer. + Now only an echo reply from the gateway with our id and sequence number counts. +*/ bool deviceETH2UART::pingDevice() { - bool RetVal = false; - if (m_bValid) + int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + if (sockfd < 0) { -#ifdef DEBUG_EXTRA - LOG("Pinging Device!"); -#endif - int sockfd; - int ttl_val = 64, msg_count = 0;//, flag = 1, msg_received_count = 0; - size_t i; - struct ping_pkt pckt; - struct sockaddr_in ping_addr; - struct sockaddr_in r_addr; - struct timeval tv_out; + char tB[160]; + snprintf(tB, sizeof(tB), "ICMP socket failed: %s (needs root / CAP_NET_RAW)", strerror(errno)); + LOG_ERROR(tB); + return false; + }; + int ttl_val = 64; + if (setsockopt(sockfd, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) != 0) + { + LOG_ERROR("Setting socket options to TTL failed!"); + close(sockfd); + return false; + }; + static std::atomic s_nSequence(0); + uint16_t tId = (uint16_t) (getpid() & 0xFFFF); + char tReason[96] = "no reply"; + bool RetVal = false; + for (int tAttempt = 0; tAttempt < E2UART_PING_ATTEMPTS && ! RetVal; tAttempt++) + { + RetVal = pingOnce(sockfd, tId, ++s_nSequence, tReason, sizeof(tReason)); + }; + close(sockfd); + if (! RetVal) + { + char tB[256]; + snprintf(tB, sizeof(tB), "No ICMP reply from %.64s: %s (%d x %d ms)", m_sUartData.path, tReason, E2UART_PING_ATTEMPTS, E2UART_PING_TIMEOUT_MS); + LOG_ERROR(tB); + }; + return RetVal; +} - tv_out.tv_sec = 0; - tv_out.tv_usec = 300000; // 1/3 of second +bool deviceETH2UART::pingOnce(int sockfd, uint16_t id, uint16_t seq, char * reason, size_t reasonSize) +{ + struct ping_pkt pckt; + struct sockaddr_in ping_addr; + memset(&ping_addr, 0, sizeof(ping_addr)); + ping_addr.sin_family = AF_INET; + ping_addr.sin_addr.s_addr = inet_addr(m_sUartData.path); + + memset(&pckt, 0, sizeof(pckt)); + pckt.hdr.type = ICMP_ECHO; + pckt.hdr.un.echo.id = htons(id); + pckt.hdr.un.echo.sequence = htons(seq); + size_t i; + for (i = 0; i < sizeof(pckt.msg) - 1; i++) + { + pckt.msg[i] = (char) i + '0'; + } + pckt.msg[i] = 0; + pckt.hdr.checksum = checksumPing(&pckt, sizeof(pckt)); - ping_addr.sin_family = AF_INET; - ping_addr.sin_addr.s_addr = inet_addr(m_sUartData.path); - // ping_addr.sin_port = htons( m_nPort ); -#ifdef DEBUG_EXTRA - LOG(m_sUartData.path); -#endif - sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); - if (sockfd < 0) + if (sendto(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr *) &ping_addr, sizeof(ping_addr)) <= 0) + { + snprintf(reason, reasonSize, "send failed: %s", strerror(errno)); + return false; + }; + uint64_t tDeadline = e2uNowMs() + E2UART_PING_TIMEOUT_MS; + for (;;) + { + uint64_t tNow = e2uNowMs(); + if (tNow >= tDeadline) { - LOG_ERROR("Socket file descriptor not received!"); + snprintf(reason, reasonSize, "timeout"); return false; }; - - // set socket options at ip to TTL and value to 64, - // change to what you want by setting ttl_val - if (setsockopt(sockfd, SOL_IP, IP_TTL, &ttl_val, sizeof(ttl_val)) != 0) + struct pollfd tPfd; + tPfd.fd = sockfd; + tPfd.events = POLLIN; + int tRc = poll(&tPfd, 1, (int) (tDeadline - tNow)); + if (tRc < 0) { - LOG_ERROR("Setting socket options to TTL failed!"); - shutdown(sockfd, SHUT_RDWR); - close(sockfd); + if (errno == EINTR) + { + continue; // a signal is not a missing reply + }; + snprintf(reason, reasonSize, "poll failed: %s", strerror(errno)); return false; }; - - // setting timeout of recv setting - setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv_out, sizeof tv_out); - - // flag is whether packet was sent or not - //flag = 1; - - // filling packet - bzero(&pckt, sizeof(pckt)); - - pckt.hdr.type = ICMP_ECHO; - pckt.hdr.un.echo.id = getpid(); - - for (i = 0; i < sizeof(pckt.msg) - 1; i++) + if (tRc == 0) { - pckt.msg[i] = (char)i + '0'; - } - - pckt.msg[i] = 0; - pckt.hdr.un.echo.sequence = msg_count++; - pckt.hdr.checksum = checksumPing(&pckt, sizeof(pckt)); // checksum(&pckt, sizeof(pckt)); - - // send packet - if (sendto(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr *) &ping_addr, sizeof(ping_addr)) <= 0) + continue; // deadline check above + }; + uint8_t tBuf[1024]; + struct sockaddr_in r_addr; + socklen_t addr_len = sizeof(r_addr); + ssize_t tLen = recvfrom(sockfd, tBuf, sizeof(tBuf), MSG_DONTWAIT, (struct sockaddr *) &r_addr, &addr_len); + if (tLen <= 0) { - LOG_ERROR("Packet Sending Failed!"); - shutdown(sockfd, SHUT_RDWR); - close(sockfd); - return false; + continue; }; - - // receive packet - socklen_t addr_len = sizeof(r_addr); - struct timeval tv; - tv.tv_sec = 3; // timeout_in_seconds; - tv.tv_usec = 0; - setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv, sizeof tv); - - if (recvfrom(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr *) &r_addr, &addr_len) <= 0) + // A raw socket returns the IP header too and gets every ICMP packet of the host + size_t tIpLen = (size_t) (tBuf[0] & 0x0F) * 4; + if ((size_t) tLen < tIpLen + sizeof(struct icmphdr)) { - LOG_ERROR("Packet receive failed!"); - shutdown(sockfd, SHUT_RDWR); - close(sockfd); - return false; - } - else + continue; + }; + struct icmphdr * tIcmp = (struct icmphdr *) (tBuf + tIpLen); + if (r_addr.sin_addr.s_addr == ping_addr.sin_addr.s_addr && tIcmp->type == ICMP_ECHOREPLY && ntohs(tIcmp->un.echo.id) == id && ntohs(tIcmp->un.echo.sequence) == seq) { #ifdef DEBUG_EXTRA LOG("Device Is OK!"); #endif - RetVal = true; + return true; }; - shutdown(sockfd, SHUT_RDWR); - close(sockfd); }; - return RetVal; } bool deviceETH2UART::create_SOL() @@ -300,6 +412,13 @@ bool deviceETH2UART::create_SOL() LOG_ERROR("No Adress Defined!"); return false; }; + // A previous connection / I/O thread is closed first. Before, the socket number was overwritten + // (the connection leaked) and init() failed while the old thread ran: every reconnect left an open + // TCP connection at the gateway and the node stayed invalid although "Connected". + if (m_iDeviceFD != -1 || E2U_IO_STATUS != TH_PRE_INIT) + { + closePort(); + }; switch (m_sUartData.mode) { @@ -450,6 +569,15 @@ bool deviceETH2UART::create_SOL() return false; }; + // Keepalive: a gateway that disappears (no FIN, e.g. mobile link lost) is detected + { + int tOn = 1, tIdle = E2UART_KEEPALIVE_IDLE_S, tIntvl = E2UART_KEEPALIVE_INTVL_S, tCnt = E2UART_KEEPALIVE_CNT; + setsockopt(m_iDeviceFD, SOL_SOCKET, SO_KEEPALIVE, &tOn, sizeof(tOn)); + setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_KEEPIDLE, &tIdle, sizeof(tIdle)); + setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_KEEPINTVL, &tIntvl, sizeof(tIntvl)); + setsockopt(m_iDeviceFD, IPPROTO_TCP, TCP_KEEPCNT, &tCnt, sizeof(tCnt)); + } + // assign IP, PORT struct sockaddr_in tServerAddr; memset(&tServerAddr, 0, sizeof(tServerAddr)); @@ -466,15 +594,23 @@ bool deviceETH2UART::create_SOL() #ifdef DEBUG LOG("EINPROGRESS in connect() - selecting"); #endif + // Over the Internet a connect takes longer than on a LAN (the timeout was 1 s) + uint64_t tDeadline = e2uNowMs() + E2UART_CONNECT_TIMEOUT_MS; do { + uint64_t tNow = e2uNowMs(); + uint64_t tLeft = (tNow < tDeadline) ? tDeadline - tNow : 0; fd_set myset; struct timeval tv; - tv.tv_sec = 1; // one second - tv.tv_usec = 0; + tv.tv_sec = tLeft / 1000; + tv.tv_usec = (tLeft % 1000) * 1000; FD_ZERO(&myset); FD_SET(m_iDeviceFD, &myset); retVal = select(m_iDeviceFD + 1, NULL, &myset, NULL, &tv); + if (retVal < 0 && errno == EINTR && e2uNowMs() < tDeadline) + { + continue; // interrupted by a signal: wait the rest of the time + } if (retVal < 0 && errno != EINTR) { char cBuffer[512]; @@ -516,7 +652,9 @@ bool deviceETH2UART::create_SOL() } else { - LOG_ERROR("Timeout in select() - Cancelling!"); + char cBuffer[160]; + snprintf(cBuffer, sizeof(cBuffer), "Timeout (%d ms) connecting to [%.64s:%d] - Cancelling!", E2UART_CONNECT_TIMEOUT_MS, m_sUartData.path, m_sUartData.port); + LOG_ERROR(cBuffer); close(m_iDeviceFD); m_iDeviceFD = -1; @@ -549,7 +687,13 @@ bool deviceETH2UART::create_SOL() break; }; }; - return init(); + if (! init()) + { + LOG_ERROR("Can not start the I/O thread - closing the connection"); + closePort(); + return false; + }; + return true; } /* @@ -961,8 +1105,8 @@ bool deviceETH2UART::sendData(uint8_t * Data, int DataSize) bool deviceETH2UART::reconnect(void) { - closePort(); - m_bValid = create_SOL(); + std::lock_guard tLock(m_mConnLock); + m_bValid = create_SOL(); // closes the old connection first return m_bValid; } @@ -984,9 +1128,11 @@ int deviceETH2UART::recptBlock(unsigned char * Buffer, int * BufferLength) socklen_t addr_len = sizeof(ClientAddress); // len is value/resuslt // RetVal = recvfrom( m_nSocketFD, (char *)Buffer, MAXIMAL_SOCKET_PACKET_LENGTH, MSG_WAITALL, ( struct sockaddr *) &m_sClientAddr, &addr_len ); RetVal = recvfrom(m_iDeviceFD, (char *) Buffer, MAXIMAL_SOCKET_IO_BUFFER_LENGTH, MSG_DONTWAIT, (struct sockaddr *) &ClientAddress, &addr_len); - if (RetVal < 1) + // TCP: 0 = the gateway closed the connection (errno is not set then: the old check read a + // stale errno and could keep a closed connection forever). < 0: an error unless EAGAIN/EINTR + if ((RetVal == 0 && m_sUartData.mode == SOL_TCP_SERVER) || RetVal < 0) { - if (errno != EAGAIN && errno != EINTR) + if (RetVal == 0 || (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { LOG_ERROR("Peer Closes Connection!"); shutdown(m_iDeviceFD, 2); @@ -1172,5 +1318,6 @@ int deviceETH2UART::sendBlock(unsigned char * Data, int DataSize) return RetVal; } +#undef E2U_IO_STATUS #undef LOG_ERROR #undef LOG diff --git a/lib/App/common/deviceNodeEthernet2Serial.h b/lib/App/common/deviceNodeEthernet2Serial.h index 5d86e7f3..eb341037 100644 --- a/lib/App/common/deviceNodeEthernet2Serial.h +++ b/lib/App/common/deviceNodeEthernet2Serial.h @@ -5,6 +5,29 @@ //#include "../components/base/basicTransport.h" #include "deviceNode.h" #include "hardwareResource.h" + +#include + +// Timeouts for a gateway reached over the Internet (the 1 s connect timeout was a LAN value) +#ifndef E2UART_CONNECT_TIMEOUT_MS + #define E2UART_CONNECT_TIMEOUT_MS 5000 // TCP connect +#endif +#ifndef E2UART_PING_TIMEOUT_MS + #define E2UART_PING_TIMEOUT_MS 2000 // wait for one ICMP echo reply +#endif +#ifndef E2UART_PING_ATTEMPTS + #define E2UART_PING_ATTEMPTS 3 // echo requests before the device counts as unreachable +#endif +#ifndef E2UART_PING_INTERVAL_MS + #define E2UART_PING_INTERVAL_MS 10000 // UDP modes: ping at most this often (validate() runs every second) +#endif +// Reconnect back-off after a failed connect: 2 s, 4 s, ... up to 30 s +#define E2UART_RECONNECT_MIN_MS 2000 +#define E2UART_RECONNECT_MAX_MS 30000 +// TCP keepalive: a dead link is detected in about IDLE + INTVL * CNT seconds +#define E2UART_KEEPALIVE_IDLE_S 20 +#define E2UART_KEEPALIVE_INTVL_S 5 +#define E2UART_KEEPALIVE_CNT 4 #ifdef SERIAL_PING_PONG class deviceETH2UART : public deviceNode, public hardwareResource, public basicTransport, public basicThread #else @@ -25,7 +48,19 @@ DCB PortDCB; HANDLE m_ioFID; #endif + // create() / validate() / reconnect() / closePort() run on the node factory thread AND on the + // hardware manager thread (the class is a deviceNode and a hardwareResource): one at a time + std::recursive_mutex m_mConnLock; + bool m_bRegistered; // assigned to the hardware manager (once) + bool m_bLastPingOk; + uint64_t m_nNextPingMs; + uint64_t m_nNextConnectMs; + uint32_t m_nConnectBackoffMs; + bool pingDevice(); + bool pingOnce(int sockfd, uint16_t id, uint16_t seq, char * reason, size_t reasonSize); + bool linkAlive(void); + bool connectSOL(void); // create_SOL() with the reconnect back-off bool create_SOL(); bool closePort(); int sendBlock(unsigned char * Data, int DataSize); diff --git a/lib/App/common/deviceNodeFactory.cpp b/lib/App/common/deviceNodeFactory.cpp index 9540483d..3caf1427 100644 --- a/lib/App/common/deviceNodeFactory.cpp +++ b/lib/App/common/deviceNodeFactory.cpp @@ -502,6 +502,9 @@ void deviceNodeFactory::deleteNode(deviceNode * ptr) #ifdef DEBUG LOG("Destroying Ethernet to UART Gateway"); #endif + // Was not deleted: the object kept running (still validated by the hardware manager, still + // connected to the gateway) next to the new node created for the same id + delete((deviceETH2UART *) ptr); break; case ARGUS_IP_VIDEO_SERVER: #ifdef DEBUG @@ -682,6 +685,9 @@ void deviceNodeFactory::m_fThreadProc(void) tList = m_pDBC->listNodes(); if (tList != NULL) { + // deleteNode() erases from m_vDeviceNodes: collect first, delete after the loop + // (erasing inside the loop invalidated the iterator and skipped the next node) + vectorDeviceNodes tToDelete; for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++) { bool tRemoved = true; @@ -707,9 +713,13 @@ void deviceNodeFactory::m_fThreadProc(void) #ifdef DEBUG_EXTRA LOG("Node not passed Validation!"); #endif - deleteNode(*idx); + tToDelete.push_back(*idx); }; }; + for (vectorDeviceNodes::iterator idx = tToDelete.begin(); idx != tToDelete.end(); idx++) + { + deleteNode(*idx); + }; tList->clear(); delete(tList); } @@ -718,7 +728,8 @@ void deviceNodeFactory::m_fThreadProc(void) #ifdef DEUBG_EXTRA LOG("All Modules Must be Deleted!"); #endif - for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++) + vectorDeviceNodes tToDelete(m_vDeviceNodes); // deleteNode() erases from m_vDeviceNodes + for (vectorDeviceNodes::iterator idx = tToDelete.begin(); idx != tToDelete.end(); idx++) { deleteNode(*idx); } diff --git a/lib/App/common/hardwareManager.cpp b/lib/App/common/hardwareManager.cpp index 7bf71cb0..d5d8bf01 100644 --- a/lib/App/common/hardwareManager.cpp +++ b/lib/App/common/hardwareManager.cpp @@ -1,5 +1,7 @@ #include "hardwareManager.h" +#include + #include #include #include @@ -1718,8 +1720,15 @@ bool hardwareManager::assignResource(hardwareResource * ptr, bool protected_mode m_mVectorProtector.lock(); } LOG("LOCKED"); - m_vResources.push_back(ptr); - LOG("ADDED"); + if (std::find(m_vResources.begin(), m_vResources.end(), ptr) == m_vResources.end()) + { + m_vResources.push_back(ptr); + LOG("ADDED"); + } + else + { + LOG("ALREADY ASSIGNED"); // validated twice per round otherwise + }; if (protected_mode) { m_mVectorProtector.unlock();