 la_mojo: fix memory leaks and heap corruption
Continuous leaks:
- protocolParser: on package-pool overflow the oldest package was removed
but never freed while the newest (still pooled) one was freed. Every
packet a handler does not consume (e.g. other sensors on a shared bus)
leaked once the pool was full. Free the removed package, free pooled
packages in the destructor and getPackage(), use memmove for the
overlapping buffer shifts.
- SQLite_Connector::Query: loop only while sqlite3_step() returns
SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on
SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed
open, fix escapeBinary() writing into uninitialised buffers.
- basicThread/basicDualThread: threads that ended by themselves were never
joined, leaking their stacks on every stream drop/restart. Always join
once (detach when called from the thread itself); check pthread_create
result correctly.
- curlRequest: free the header list; initialise m_pPostData and the
content type; curlRequestsManager deletes requests it cannot queue.
- kernelModuleModemManager: free the phonebook ID list on every reload.
- deviceNodeFactory: do not append the same pending node every cycle.
- streamHandler: free frames left in the encoding queue on destruction.
Heap corruption:
- kernelModuleFactory: module that failed to start was deleted twice.
- mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a
bool; compare with -1. Fix packetExtractor and destructor leaks.
- utilizeBasicEvent: payloads are malloc'ed, release with free().
- videoAVLIBSource: avoid sws context double free, NULL check order.
- trim(): memmove for overlapping copy.
Smaller leaks: dropSession duplicate query, LRF upload data, report
backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago  la_mojo: fix memory leaks and heap corruption
Continuous leaks:
- protocolParser: on package-pool overflow the oldest package was removed
but never freed while the newest (still pooled) one was freed. Every
packet a handler does not consume (e.g. other sensors on a shared bus)
leaked once the pool was full. Free the removed package, free pooled
packages in the destructor and getPackage(), use memmove for the
overlapping buffer shifts.
- SQLite_Connector::Query: loop only while sqlite3_step() returns
SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on
SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed
open, fix escapeBinary() writing into uninitialised buffers.
- basicThread/basicDualThread: threads that ended by themselves were never
joined, leaking their stacks on every stream drop/restart. Always join
once (detach when called from the thread itself); check pthread_create
result correctly.
- curlRequest: free the header list; initialise m_pPostData and the
content type; curlRequestsManager deletes requests it cannot queue.
- kernelModuleModemManager: free the phonebook ID list on every reload.
- deviceNodeFactory: do not append the same pending node every cycle.
- streamHandler: free frames left in the encoding queue on destruction.
Heap corruption:
- kernelModuleFactory: module that failed to start was deleted twice.
- mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a
bool; compare with -1. Fix packetExtractor and destructor leaks.
- utilizeBasicEvent: payloads are malloc'ed, release with free().
- videoAVLIBSource: avoid sws context double free, NULL check order.
- trim(): memmove for overlapping copy.
Smaller leaks: dropSession duplicate query, LRF upload data, report
backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago  la_mojo: fix memory leaks and heap corruption
Continuous leaks:
- protocolParser: on package-pool overflow the oldest package was removed
but never freed while the newest (still pooled) one was freed. Every
packet a handler does not consume (e.g. other sensors on a shared bus)
leaked once the pool was full. Free the removed package, free pooled
packages in the destructor and getPackage(), use memmove for the
overlapping buffer shifts.
- SQLite_Connector::Query: loop only while sqlite3_step() returns
SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on
SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed
open, fix escapeBinary() writing into uninitialised buffers.
- basicThread/basicDualThread: threads that ended by themselves were never
joined, leaking their stacks on every stream drop/restart. Always join
once (detach when called from the thread itself); check pthread_create
result correctly.
- curlRequest: free the header list; initialise m_pPostData and the
content type; curlRequestsManager deletes requests it cannot queue.
- kernelModuleModemManager: free the phonebook ID list on every reload.
- deviceNodeFactory: do not append the same pending node every cycle.
- streamHandler: free frames left in the encoding queue on destruction.
Heap corruption:
- kernelModuleFactory: module that failed to start was deleted twice.
- mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a
bool; compare with -1. Fix packetExtractor and destructor leaks.
- utilizeBasicEvent: payloads are malloc'ed, release with free().
- videoAVLIBSource: avoid sws context double free, NULL check order.
- trim(): memmove for overlapping copy.
Smaller leaks: dropSession duplicate query, LRF upload data, report
backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
6 days ago  Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check
From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234):
deviceETH2UART
- In TCP mode, link health is the TCP connection itself (socket, I/O
thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on
mobile networks often don't answer ping, and the working connection was
closed every second ("No Ping to device!").
- Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an
echo reply from the gateway with our id and sequence counts. A signal
(EINTR) no longer ends the wait. The old single recvfrom() with
SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART.
- TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in
select() continues waiting.
- create_SOL() closes the previous socket and I/O thread first. It
overwrote the socket (the connection leaked) and init() failed while
the old thread ran, so each reconnect left an open TCP connection at the
gateway and the node stayed invalid although "Connected". If init()
fails, the new socket is closed.
- create()/validate()/reconnect() are serialised: they run on the node
factory thread and on the hardware manager thread.
- Registered with the hardware manager once. Re-creation from the
hardware manager locked its non-recursive list mutex again (deadlock).
- Reconnect back-off 2 s -> 30 s.
- recv() == 0 on TCP means the gateway closed the connection (a stale
errno was checked before).
- Destructor resigns from the hardware manager before closing.
deviceNodeFactory
- deleteNode() deletes the gateway object. It was only removed from the
list and kept running next to the new node created for the same id.
- Nodes are deleted after the validation loop (erasing inside it skipped
the next node).
deviceNode::validate
- Compares the configuration only. "online" changes whenever another
object of the same node id is created or deleted.
hardwareManager::assignResource
- Ignores a resource that is already assigned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
12 hours ago  Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check
From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234):
deviceETH2UART
- In TCP mode, link health is the TCP connection itself (socket, I/O
thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on
mobile networks often don't answer ping, and the working connection was
closed every second ("No Ping to device!").
- Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an
echo reply from the gateway with our id and sequence counts. A signal
(EINTR) no longer ends the wait. The old single recvfrom() with
SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART.
- TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in
select() continues waiting.
- create_SOL() closes the previous socket and I/O thread first. It
overwrote the socket (the connection leaked) and init() failed while
the old thread ran, so each reconnect left an open TCP connection at the
gateway and the node stayed invalid although "Connected". If init()
fails, the new socket is closed.
- create()/validate()/reconnect() are serialised: they run on the node
factory thread and on the hardware manager thread.
- Registered with the hardware manager once. Re-creation from the
hardware manager locked its non-recursive list mutex again (deadlock).
- Reconnect back-off 2 s -> 30 s.
- recv() == 0 on TCP means the gateway closed the connection (a stale
errno was checked before).
- Destructor resigns from the hardware manager before closing.
deviceNodeFactory
- deleteNode() deletes the gateway object. It was only removed from the
list and kept running next to the new node created for the same id.
- Nodes are deleted after the validation loop (erasing inside it skipped
the next node).
deviceNode::validate
- Compares the configuration only. "online" changes whenever another
object of the same node id is created or deleted.
hardwareManager::assignResource
- Ignores a resource that is already assigned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
12 hours ago  Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check
From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234):
deviceETH2UART
- In TCP mode, link health is the TCP connection itself (socket, I/O
thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on
mobile networks often don't answer ping, and the working connection was
closed every second ("No Ping to device!").
- Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an
echo reply from the gateway with our id and sequence counts. A signal
(EINTR) no longer ends the wait. The old single recvfrom() with
SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART.
- TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in
select() continues waiting.
- create_SOL() closes the previous socket and I/O thread first. It
overwrote the socket (the connection leaked) and init() failed while
the old thread ran, so each reconnect left an open TCP connection at the
gateway and the node stayed invalid although "Connected". If init()
fails, the new socket is closed.
- create()/validate()/reconnect() are serialised: they run on the node
factory thread and on the hardware manager thread.
- Registered with the hardware manager once. Re-creation from the
hardware manager locked its non-recursive list mutex again (deadlock).
- Reconnect back-off 2 s -> 30 s.
- recv() == 0 on TCP means the gateway closed the connection (a stale
errno was checked before).
- Destructor resigns from the hardware manager before closing.
deviceNodeFactory
- deleteNode() deletes the gateway object. It was only removed from the
list and kept running next to the new node created for the same id.
- Nodes are deleted after the validation loop (erasing inside it skipped
the next node).
deviceNode::validate
- Compares the configuration only. "online" changes whenever another
object of the same node id is created or deleted.
hardwareManager::assignResource
- Ignores a resource that is already assigned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
12 hours ago  Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check
From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234):
deviceETH2UART
- In TCP mode, link health is the TCP connection itself (socket, I/O
thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on
mobile networks often don't answer ping, and the working connection was
closed every second ("No Ping to device!").
- Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an
echo reply from the gateway with our id and sequence counts. A signal
(EINTR) no longer ends the wait. The old single recvfrom() with
SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART.
- TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in
select() continues waiting.
- create_SOL() closes the previous socket and I/O thread first. It
overwrote the socket (the connection leaked) and init() failed while
the old thread ran, so each reconnect left an open TCP connection at the
gateway and the node stayed invalid although "Connected". If init()
fails, the new socket is closed.
- create()/validate()/reconnect() are serialised: they run on the node
factory thread and on the hardware manager thread.
- Registered with the hardware manager once. Re-creation from the
hardware manager locked its non-recursive list mutex again (deadlock).
- Reconnect back-off 2 s -> 30 s.
- recv() == 0 on TCP means the gateway closed the connection (a stale
errno was checked before).
- Destructor resigns from the hardware manager before closing.
deviceNodeFactory
- deleteNode() deletes the gateway object. It was only removed from the
list and kept running next to the new node created for the same id.
- Nodes are deleted after the validation loop (erasing inside it skipped
the next node).
deviceNode::validate
- Compares the configuration only. "online" changes whenever another
object of the same node id is created or deleted.
hardwareManager::assignResource
- Ignores a resource that is already assigned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
12 hours ago  Ethernet2UART gateway: fix the reconnect loop, leaked connections and ping check
From the la_mojo log of a gateway reached over the Internet (2.54.237.28:8234):
deviceETH2UART
- In TCP mode, link health is the TCP connection itself (socket, I/O
thread, SO_ERROR, TCP keepalive). ICMP isn't used there: gateways on
mobile networks often don't answer ping, and the working connection was
closed every second ("No Ping to device!").
- Ping (UDP modes) up to 3 echoes x 2000 ms, at most every 10 s. Only an
echo reply from the gateway with our id and sequence counts. A signal
(EINTR) no longer ends the wait. The old single recvfrom() with
SO_RCVTIMEO returned EINTR at the first signal, even with SA_RESTART.
- TCP connect timeout 1 s -> 5 s (E2UART_CONNECT_TIMEOUT_MS), and EINTR in
select() continues waiting.
- create_SOL() closes the previous socket and I/O thread first. It
overwrote the socket (the connection leaked) and init() failed while
the old thread ran, so each reconnect left an open TCP connection at the
gateway and the node stayed invalid although "Connected". If init()
fails, the new socket is closed.
- create()/validate()/reconnect() are serialised: they run on the node
factory thread and on the hardware manager thread.
- Registered with the hardware manager once. Re-creation from the
hardware manager locked its non-recursive list mutex again (deadlock).
- Reconnect back-off 2 s -> 30 s.
- recv() == 0 on TCP means the gateway closed the connection (a stale
errno was checked before).
- Destructor resigns from the hardware manager before closing.
deviceNodeFactory
- deleteNode() deletes the gateway object. It was only removed from the
list and kept running next to the new node created for the same id.
- Nodes are deleted after the validation loop (erasing inside it skipped
the next node).
deviceNode::validate
- Compares the configuration only. "online" changes whenever another
object of the same node id is created or deleted.
hardwareManager::assignResource
- Ignores a resource that is already assigned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
12 hours ago |
|
#include "deviceNodeFactory.h"
#include <algorithm>
#ifdef USE_MYSQL
#include "MySQL_dataBinder.h"
#else
#include "SQLite_dataBinder.h"
#endif
#include "deviceNode2D.h"
#include "deviceNode3D.h"
#include "deviceNodeEthernet2Serial.h"
#include "deviceNodePresetCamControl.h"
#include "deviceNodePTZCamControl.h"
#include "deviceNodeStaticCamControl.h"
#ifdef MEM_DEBUG
#include "../alg/generic/heapDebug.h"
#endif
#define LOG(X) m_pLog->Log("NFAC", this, X)
#define LOG_ERROR(X) m_pLog->Log("NFAC", this, _LOG_ERROR, X)
deviceNodeFactory * deviceNodeFactory::_instance = NULL;
/**
* Returns the single instance of the object. */deviceNodeFactory * deviceNodeFactory::getInstance(){ if (NULL == _instance) { _instance = new deviceNodeFactory(); }; return _instance;}
deviceNodeFactory::deviceNodeFactory() : basicThread(){ m_pLog = basicLogCollection::getInstance();#ifdef USE_MYSQL
m_pDBC = mysqlDBC::getInstance();#else
m_pDBC = sqliteDBC::getInstance();#endif
m_nPendingInitCounter = 0; init();}
deviceNodeFactory::~deviceNodeFactory(){ waitThreadEnd(true); unloadNodes(); _instance = NULL;}
bool deviceNodeFactory::unloadNodes(void){ if (m_pDBC != NULL) { if (m_vDeviceNodes.size() > 0) { for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++) { LOG(" Unload Node... BEGIN"); switch ((*index)->getNodeType()) { case ARGUS_ANALOG_STATIC_CAMERA: LOG(" Static Analog Cam"); delete((deviceNodeStaticCamControl *) (*index)); break; case ARGUS_ANALOG_PTZ_CAMERA: LOG(" PTZ Analog Cam"); delete((deviceNodePTZCamControl *) (*index)); break; case ARGUS_IP_PTZ_CAMERA: { LOG(" PTZ IP Cam"); delete((deviceNodePTZCamControl *) (*index)); break; }; case ARGUS_IP_STATIC_CAMERA: { LOG(" Static IP Cam"); delete((deviceNodeStaticCamControl *) (*index)); break; }; case ARGUS_IP_PRESET_CAMERA: { LOG(" Preset IP Cam"); delete((deviceNodePresetCamControl *) (*index)); break; }; case ARGUS_ANALOG_PRESET_CAMERA: { LOG(" Preset Analog Cam"); // delete( (deviceNodeStaticCamControl*) (*index) );
break; }; case ARGUS_IP_UART_GATEWAY: { LOG(" UART Gateway"); delete((deviceETH2UART *) (*index)); break; }; case ARGUS_ALS_SENSOR: case ARGUS_PLS_200_SENSOR: case ARGUS_PLS_360_SENSOR: case ARGUS_LFS_SENSOR: { LOG(" 2D LRF"); delete((device2DSensor *) (*index)); LOG(" 2D LRF done"); break; }; case ARGUS_3D_SENSOR: { LOG(" 3D LRF"); delete((device3DSensor *) (*index)); break; }; default: break; }; LOG(" Unload Node... DONE!"); }; m_vDeviceNodes.clear(); m_mDeviceNodesIDS.clear(); }; }; return true;}
/*
bool deviceNodeFactory::loadNodes( uint32_t SiteID ){ LOG("Node Loader"); if(m_pDBC == NULL) return false; m_nSiteID = SiteID; DBC_INT_LIST *tList = m_pDBC->listNodes( SiteID ); if( tList != NULL ) { for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ){ uint32_t id = *idx; char msg[64]; sprintf( msg, "Creating Node[%d]", id ); createNode( id ); }; tList->clear(); delete( tList ); return true; }; return false;};*/bool deviceNodeFactory::loadNodes(){ if (m_pDBC == NULL) { return false; } DBC_INT_LIST * tList = m_pDBC->listNodes(false); m_nPendingInitCounter = 0; if (tList != NULL) { for (DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++) { createNode(*idx); } tList->clear(); delete(tList); }#ifdef DEBUG
else { LOG("No Nodes Defined"); }#endif
return true;}
bool deviceNodeFactory::reloadNodes(){ if (! unloadNodes()) { return false; } return loadNodes();}
bool deviceNodeFactory::reloadNode(uint32_t NodeID){ deleteNode(NodeID); deviceNode * tNode = createNode(NodeID); if (tNode) { return true; }; return false;}
deviceNode * deviceNodeFactory::createNode(uint32_t id){ deviceNode * tVal = NULL; if (m_pDBC != NULL) { DBC_NODE_DATA * tData = NULL; tData = m_pDBC->getNodeData(id); if (tData != NULL) {#ifdef DEBUG
char tB[256]; sprintf(tB, "Node id: %d AUX id: %d", id, tData->aux_id); LOG(tB); sprintf(tB, "Node type: %d", tData->type); LOG(tB); sprintf(tB, "Node Name: %s", tData->name); LOG(tB);#endif
switch (tData->type) { case ARGUS_ANALOG_STATIC_CAMERA: {#ifdef DEBUG
LOG("Creating Analog Static Camera");#endif
deviceNodeStaticCamControl * tCamera; tCamera = new deviceNodeStaticCamControl(id); if (tCamera) { LOG("Starting Service"); tCamera->startService(); tVal = (deviceNode *) tCamera; LOG("Success"); }; break; }; case ARGUS_ANALOG_PTZ_CAMERA: {#ifdef DEBUG
LOG("Creating Analog PTZ Camera");#endif
deviceNodePTZCamControl * tCamera; tCamera = new deviceNodePTZCamControl(id); if (tCamera) { LOG("Starting Service"); tCamera->startService(); tVal = (deviceNode *) tCamera; LOG("Success"); }; break; }; case ARGUS_IP_STATIC_CAMERA: {#ifdef DEBUG
LOG("Creating IP Static Camera");#endif
deviceNodeStaticCamControl * tCamera; tCamera = new deviceNodeStaticCamControl(id); if (tCamera) {#ifdef DEBUG
LOG("Starting Service");#endif
if (tCamera->startService()) { tVal = (deviceNode *) tCamera;#ifdef DEBUG
LOG("Success");#endif
} else {#ifdef DEBUG
LOG_ERROR("Error Sterting Service");#endif
delete(tCamera); }; }; break; }; case ARGUS_IP_PTZ_CAMERA: {
#ifdef DEBUG
LOG("Creating IP PTZ Camera");#endif
deviceNodePTZCamControl * tCamera; tCamera = new deviceNodePTZCamControl(id); if (tCamera) {#ifdef DEBUG
LOG("Starting Service");#endif
if (tCamera->startService()) { tVal = (deviceNode *) tCamera;#ifdef DEBUG
LOG("Success");#endif
} else {#ifdef DEBUG
LOG_ERROR("Error Starting Service!");#endif
delete(tCamera); }; }; break; }; case ARGUS_IP_PRESET_CAMERA: {
#ifdef DEBUG
LOG("Creating IP Preset Camera");#endif
deviceNodePresetCamControl * tCamera; tCamera = new deviceNodePresetCamControl(id); if (tCamera) {#ifdef DEBUG
LOG("Starting Service");#endif
if (tCamera->startService()) { tVal = (deviceNode *) tCamera;#ifdef DEBUG
LOG("Success");#endif
} else {#ifdef DEBUG
LOG_ERROR("Error Starting Service!");#endif
delete(tCamera); }; }; break; }; case ARGUS_IP_UART_GATEWAY: {#ifdef DEBUG
LOG("Creating Ethernet to UART Gateway");#endif
deviceETH2UART * tGW; tGW = new deviceETH2UART(id); if (tGW) { if (tGW->create()) { tVal = (deviceNode *) tGW; } else { delete(tGW); }; }; break; }; case ARGUS_IP_VIDEO_SERVER:#ifdef DEBUG
LOG("Creating Network Video Encoder");#endif
break; case ARGUS_LFS_SENSOR: case ARGUS_ALS_SENSOR: case ARGUS_PLS_200_SENSOR: case ARGUS_PLS_360_SENSOR: {#ifdef DEBUG
LOG("Creating 2D Sensor");#endif
device2DSensor * tSensor; tSensor = new device2DSensor(id); if (tSensor) {#ifdef DEBUG
LOG("Starting Service");#endif
if (tSensor->startService()) {#ifdef DEBUG
LOG("Success");#endif
tVal = (deviceNode *) tSensor; } else { delete(tSensor); }; }; break; }; case ARGUS_3D_SENSOR: {#ifdef DEBUG
LOG("Creating 3D Sensor");#endif
device3DSensor * t3DRadar; t3DRadar = new device3DSensor(id); if (t3DRadar) { LOG("Success"); t3DRadar->startService(); tVal = (deviceNode *) t3DRadar; }; break; }; case ARGUS_JAMER:#ifdef DEBUG
LOG("Creating RF Jamer");#endif
break; case ARGUS_NET_PLS_200:#ifdef DEBUG
LOG("Creating Network-PLS200 Sensor");#endif
break; case ARGUS_DRONE_CONTROL:#ifdef DEBUG
LOG("Creating Drone Controll");#endif
break; }; free(tData); }; }; if (tVal != NULL) {#ifdef DEBUG
char tB[64]; sprintf(tB, "Node PTR: %p", (void *)tVal); LOG(tB);#endif
m_vDeviceNodes.push_back(tVal); m_mDeviceNodesIDS[id] = tVal; // Node is alive now - it is no longer pending
m_nPendingNodes.erase(std::remove(m_nPendingNodes.begin(), m_nPendingNodes.end(), (int) id), m_nPendingNodes.end()); } else {#ifdef DEBUG_EXTRA
LOG("Node Creation Failure -> Push to Pending Mode!");#endif
// Add only once: this runs on every retry and the list was never trimmed
if (std::find(m_nPendingNodes.begin(), m_nPendingNodes.end(), (int) id) == m_nPendingNodes.end()) { m_nPendingNodes.push_back(id); } m_nPendingInitCounter = FACTORY_NODE_INIT_PENDING_COUNT; }; return tVal;}
void deviceNodeFactory::deleteNode(deviceNode * ptr){ if (ptr == NULL) { return; } // if some troubles move this to an end of the function!
for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index != m_vDeviceNodes.end(); index++) { if ((*index) == ptr) { m_mDeviceNodesIDS.erase(ptr->getNodeID()); m_vDeviceNodes.erase(index); break; }; }; switch (ptr->getNodeType()) { case ARGUS_ANALOG_STATIC_CAMERA:#ifdef DEBUG
LOG("Destroying Analog Static Camera");#endif
break; case ARGUS_ANALOG_PTZ_CAMERA:#ifdef DEBUG
LOG("Destroying Analog PTZ Camera");#endif
break; case ARGUS_IP_STATIC_CAMERA:#ifdef DEBUG
LOG("Destroying Network Static Camera");#endif
// SLEEP(2000);
LOG("Actual Destroying Network Static Camera"); delete((deviceNodePTZCamControl *) ptr); break; case ARGUS_IP_PTZ_CAMERA:#ifdef DEBUG
LOG("Destroying Network PTZ Camera");#endif
// SLEEP(2000);
delete((deviceNodePTZCamControl *) ptr);#ifdef DEBUG
LOG("Done Successfuly");#endif
break; case ARGUS_IP_UART_GATEWAY:#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
LOG("Destroying Network Video Encoder");#endif
break; case ARGUS_LFS_SENSOR:#ifdef DEBUG
LOG("Destroying LFS Sensor");#endif
delete((device2DSensor *) ptr); break; case ARGUS_ALS_SENSOR:#ifdef DEBUG
LOG("Destroying ALS Sensor");#endif
delete((device2DSensor *) ptr); break; case ARGUS_PLS_200_SENSOR:#ifdef DEBUG
LOG("Destroying PLS200 Sensor");#endif
delete((device2DSensor *) ptr); break; case ARGUS_PLS_360_SENSOR:#ifdef DEBUG
LOG("Destroying PLS360 Sensor");#endif
delete((device2DSensor *) ptr); break; case ARGUS_3D_SENSOR:#ifdef DEBUG
LOG("Destroying 3D Sensor");#endif
delete((device3DSensor *) ptr); break; case ARGUS_JAMER:#ifdef DEBUG
LOG("Destroying RF Jamer");#endif
break; case ARGUS_NET_PLS_200:#ifdef DEBUG
LOG("Destroying Network-PLS200 Sensor");#endif
break; case ARGUS_DRONE_CONTROL:#ifdef DEBUG
LOG("Destroying Drone Controll");#endif
break; default: break; };}
/*
@id Database ID of Node to be deleted*/deviceNode * deviceNodeFactory::getNode(uint32_t id){ mapDeviceNode2Id::iterator it; it = m_mDeviceNodesIDS.find(id); if (it != m_mDeviceNodesIDS.end()) { return m_mDeviceNodesIDS[id]; }; return NULL;}
/*
@id Database ID of Node to be deleted*/void deviceNodeFactory::deleteNode(uint32_t id){ deviceNode * tNode = getNode(id); deleteNode(tNode);}
bool deviceNodeFactory::validate(void){ return (m_bThreadStatus < TH_ENDED);}
void deviceNodeFactory::m_fThreadProc(void){ if (m_pDBC == NULL) { m_bThreadStatus = TH_ENDED; } else { pthread_setname_np( pthread_self(), "NodeFactory" ); m_bThreadStatus = TH_WORKING; while (m_bThreadStatus < TH_ENDED) {
DBC_INT_LIST * tList = NULL;#ifdef DEBUG
#ifdef MEM_DEBUG
size_t memStart, memEnd, memDiff; char MEM_MSG[256]; memStart = getCurrentRSS(); #endif
#endif
if (m_nPendingInitCounter) { m_nPendingInitCounter--; } if (m_vDeviceNodes.size()) { DBC_INT_LIST tL; for (vectorDeviceNodes::iterator idx = m_vDeviceNodes.begin(); idx < m_vDeviceNodes.end(); idx++) { tL.push_back((*idx)->getNodeID()); } tList = m_pDBC->listNewNodes(&tL);//ToDo: Must be Written ad method to Delete on the fly Disabled Nodes
// To provide this this performance without restarting App
// tL may be used to query Database if a Node is disabled
tL.clear(); } else { tList = m_pDBC->listNewNodes(NULL); }; if (tList != NULL) {#ifdef DEBUG_EXTRA
LOG("Nodes are Added!");#endif
for (DBC_INT_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++) { if (m_nPendingNodes.size()) { bool mustAdd = true; for (DBC_INT_LIST::iterator dx = m_nPendingNodes.begin(); dx != m_nPendingNodes.end(); dx++) { if ((*dx == *idx) && (m_nPendingInitCounter)) { mustAdd = false; } }; if (mustAdd) { createNode(*idx);#ifdef DEBUG_EXTRA
} else { LOG("Node is in PENDING mode!");#endif
}; } else { createNode(*idx); }; }; tList->clear(); delete(tList);#ifdef DEBUG_EXTRA
} else { LOG("No New Nodes");#endif
};#ifdef MEM_DEBUG
memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG, "MEM START: %d MEM DIF1: %d", memStart, memDiff); LOG(MEM_MSG);#endif
if (m_vDeviceNodes.size()) { // Get Full List
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; // Check if Removed By changes in DB
for (DBC_INT_LIST::iterator idz = tList->begin(); idz < tList->end(); idz++) { if ((*idz) == (int)(*idx)->getNodeID()) { tRemoved = false; } } // If Not - check validation
if (! tRemoved) {#ifdef DEBUG_EXTRA
LOG("Node Among Active Nodes.... make Validation");#endif
tRemoved = ! (*idx)->validate(); }; // If Node is Invalid - Removing
if (tRemoved) {#ifdef DEBUG_EXTRA
LOG("Node not passed Validation!");#endif
tToDelete.push_back(*idx); }; }; for (vectorDeviceNodes::iterator idx = tToDelete.begin(); idx != tToDelete.end(); idx++) { deleteNode(*idx); }; tList->clear(); delete(tList); } else {#ifdef DEUBG_EXTRA
LOG("All Modules Must be Deleted!");#endif
vectorDeviceNodes tToDelete(m_vDeviceNodes); // deleteNode() erases from m_vDeviceNodes
for (vectorDeviceNodes::iterator idx = tToDelete.begin(); idx != tToDelete.end(); idx++) { deleteNode(*idx); } m_vDeviceNodes.clear(); m_mDeviceNodesIDS.clear(); };#ifdef DEBUG_EXTRA
} else { LOG("No Nodes to Validate");#endif
};#ifdef DEBUG
#ifdef MEM_DEBUG
memEnd = getCurrentRSS(); memDiff = memEnd - memStart; sprintf(MEM_MSG, "MEM START: %d MEM DIF2: %d", memStart, memDiff); LOG(MEM_MSG); #endif
#endif
// Sleep for 1 second
for (int i = (1000 / DEFAULT_THREAD_SLEEP_TIME); i && (m_bThreadStatus < TH_ENDED); i--) { SLEEP(DEFAULT_THREAD_SLEEP_TIME); } }; }; LOG("EXIT FROM CTRL THREAD");}
uint32_t deviceNodeFactory::getNodesCount(ARGUS_NODE_TYPE type){ uint32_t RetVal = 0; if (m_vDeviceNodes.size()) { for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++) { if ((*index)->getNodeType() == type) { RetVal++; } } } return RetVal;}
uint32_t deviceNodeFactory::fetchNodes(ARGUS_NODE_TYPE type, deviceNode **& RA){ uint32_t RetVal = 0; if (RA == NULL) { uint32_t cnt = getNodesCount(type); if (cnt > 0) { RA = (deviceNode **) calloc(cnt, sizeof(deviceNode *)); if (RA == NULL) { return 0; } // memset( RA, 0, cnt * sizeof(deviceNode *) );
for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++) { if ((*index)->getNodeType() == type) { RA[RetVal] = (*index); RetVal++; }; }; }; } else { if (m_vDeviceNodes.size()) { for (vectorDeviceNodes::iterator index = m_vDeviceNodes.begin(); index < m_vDeviceNodes.end(); index++) { if ((*index)->getNodeType() == type) { RA[RetVal] = (*index); RetVal++; }; }; }; }; return RetVal;}
#undef LOG
#undef LOG_ERROR
|