diff --git a/Linux_Workspace.vpw b/Linux_Workspace.vpw index 78842a0d..0f2fa7c0 100644 --- a/Linux_Workspace.vpw +++ b/Linux_Workspace.vpw @@ -20,5 +20,6 @@ + diff --git a/Projects/la_mojo/la_mojo.vpj b/Projects/la_mojo/la_mojo.vpj index 909d172f..fcfc4e8b 100644 --- a/Projects/la_mojo/la_mojo.vpj +++ b/Projects/la_mojo/la_mojo.vpj @@ -526,6 +526,7 @@ + diff --git a/Projects/la_mojo/makefile b/Projects/la_mojo/makefile index e0fa8136..ff7e349b 100644 --- a/Projects/la_mojo/makefile +++ b/Projects/la_mojo/makefile @@ -38,9 +38,10 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -87,9 +88,10 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -227,9 +229,10 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -276,9 +279,10 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -416,9 +420,10 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -465,9 +470,10 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -610,9 +616,10 @@ COMMON_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -659,9 +666,10 @@ ALL_OBJ=$(OUTDIR)/base64.o $(OUTDIR)/basicCameraInfo.o \ $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ $(OUTDIR)/basicTransport.o $(OUTDIR)/clientPort.o \ - $(OUTDIR)/curlRequest.o $(OUTDIR)/curlRequestsManager.o \ - $(OUTDIR)/dataBinder.o $(OUTDIR)/dataBinder_ReportAux.o \ - $(OUTDIR)/detector.o $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ + $(OUTDIR)/cmdNodeConveyor.o $(OUTDIR)/curlRequest.o \ + $(OUTDIR)/curlRequestsManager.o $(OUTDIR)/dataBinder.o \ + $(OUTDIR)/dataBinder_ReportAux.o $(OUTDIR)/detector.o \ + $(OUTDIR)/deviceNode.o $(OUTDIR)/deviceNode2D.o \ $(OUTDIR)/deviceNode3D.o $(OUTDIR)/deviceNodeCamera.o \ $(OUTDIR)/deviceNodeEthernet2Serial.o $(OUTDIR)/deviceNodeFactory.o \ $(OUTDIR)/deviceNodePTZCamControl.o $(OUTDIR)/deviceNodeSensor.o \ @@ -803,6 +811,7 @@ $(OUTDIR)/basicSettingsProvider.o: ../../lib/components/base/basicSettingsProvid $(OUTDIR)/basicThread.o: ../../lib/components/base/basicThread.cpp ../../lib/components/base/basicThread.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicLoggingService.h ../../lib/components/base/basicRing.h ../../lib/components/base/basicMutex.h $(OUTDIR)/basicTransport.o: ../../lib/components/base/basicTransport.cpp ../../lib/components/base/basicTransport.h ../../lib/data/protocol/protocol.h $(OUTDIR)/clientPort.o: ../../lib/data/net/clientPort.cpp ../../lib/data/net/clientPort.h ../../lib/data/transport/transportSocket.h ../../lib/components/base/basicDualThread.h ../../lib/components/base/basicThread.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicLoggingService.h ../../lib/components/base/basicRing.h ../../lib/components/base/basicMutex.h ../../lib/components/base/basicTransport.h +$(OUTDIR)/cmdNodeConveyor.o: ../../lib/App/common/cmdNodeConveyor.cpp ../../lib/App/common/cmdNodeConveyor.h $(OUTDIR)/curlRequest.o: ../../lib/data/curl/curlRequest.cpp ../../lib/data/curl/curlRequest.h $(OUTDIR)/curlRequestsManager.o: ../../lib/data/curl/curlRequestsManager.cpp ../../lib/data/curl/curlRequestsManager.h $(OUTDIR)/dataBinder.o: ../../lib/App/common/dataBinder.cpp ../../lib/App/common/dataBinder.h ../../lib/App/common/dataBinder_structures.h ../../lib/App/common/dataBinder_ReportAux.h diff --git a/Projects/watchDogDaemon/Debug/default.conf b/Projects/watchDogDaemon/Debug/default.conf new file mode 100644 index 00000000..32e1d724 --- /dev/null +++ b/Projects/watchDogDaemon/Debug/default.conf @@ -0,0 +1,4 @@ +KERNEL : +{ + SOCKET_PATH = "/srv/etc/watchdog"; +}; diff --git a/Projects/watchDogDaemon/Debug/watchdogDaemon b/Projects/watchDogDaemon/Debug/watchdogDaemon new file mode 100755 index 00000000..be65e933 Binary files /dev/null and b/Projects/watchDogDaemon/Debug/watchdogDaemon differ diff --git a/Projects/watchDogDaemon/Release/watchdogDaemon b/Projects/watchDogDaemon/Release/watchdogDaemon new file mode 100755 index 00000000..61cd95df Binary files /dev/null and b/Projects/watchDogDaemon/Release/watchdogDaemon differ diff --git a/Projects/watchDogDaemon/build b/Projects/watchDogDaemon/build new file mode 100755 index 00000000..a01bb9e3 --- /dev/null +++ b/Projects/watchDogDaemon/build @@ -0,0 +1,9 @@ +#! /bin/sh + + +make clean -e "CFG=Debug" +make -j4 -e "CFG=Debug" + + +make clean -e "CFG=Release" +make -j4 -e "CFG=Release" diff --git a/Projects/watchDogDaemon/kernel.cpp b/Projects/watchDogDaemon/kernel.cpp new file mode 100644 index 00000000..51b584de --- /dev/null +++ b/Projects/watchDogDaemon/kernel.cpp @@ -0,0 +1,359 @@ +#include "kernel.h" + +#define LOG( X ) m_pLog->Log( "KERNEL", this, X ) +#define LOG_ERROR( X ) m_pLog->Log( "KERNEL", this, _LOG_ERROR, X ) + +SP_KERNEL *SP_KERNEL::_instance = NULL; + +SP_KERNEL *SP_KERNEL::getInstance( bool ColdStart ){ + if( _instance == NULL ) { + _instance = new SP_KERNEL(); + }; + return _instance; +}; + +SP_KERNEL::SP_KERNEL(): basicThread(){ + m_pLog = basicLogCollection::getInstance(); + m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, (CSTR)"KERNEL" ); + m_iServerSocket = INVALID_SOCKET; + m_iExitCause = 0; +}; + +SP_KERNEL::~SP_KERNEL(){ + if( m_iServerSocket != INVALID_SOCKET ) { + shutdown( m_iServerSocket, 2 ); + close( m_iServerSocket ); + if( fileExist ( m_pUnixSocketName ) ) unlink( m_pUnixSocketName ); + }; +}; + +void SP_KERNEL::wd_shutdown( void ){ + m_bThreadStatus = TH_ENDED; + m_iExitCause = 33; +}; +//Use Killall prior to kill all instances of mojo + +// https://stackoverflow.com/questions/8166415/how-to-get-the-pid-of-a-process-in-linux-in-c + +#include +#include +//#include +#include +#include +#include +#include + + + +//https://www.educative.io/answers/unix-socket-programming-in-c + + +/************************************************************************ + * + * Returns the pid of the process name which is given as the function + * argument. + * In case no process found with the given name -1 will be returned. + * + ************************************************************************* / +pid_t getProcessId(const char * csProcessName){ + struct kinfo_proc * sProcesses = NULL; + struct kinfo_proc * sNewProcesses = NULL; + pid_t iCurrentPid; + int aiNames[4]; + size_t iNamesLength; + int i, iRetCode, iNumProcs; + size_t iSize = 0; + + aiNames[0] = CTL_KERN; + aiNames[1] = KERN_PROC; + aiNames[2] = KERN_PROC_ALL; + aiNames[3] = 0; + + iNamesLength = 3; + iRetCode = sysctl( aiNames, iNamesLength, NULL, &iSize, NULL, 0 ); + + /* + * Allocate memory and populate info in the processes structure + * / + + do { + iSize += iSize / 10; + sNewProcesses = (struct kinfo_proc *) realloc( sProcesses, iSize ); + if( sNewProcesses == 0 ){ + if( sProcesses ) free(sProcesses); + errx(1, "could not reallocate memory"); + }; + sProcesses = sNewProcesses; + iRetCode = sysctl(aiNames, iNamesLength, sProcesses, &iSize, NULL, 0); + } while ( iRetCode == -1 && errno == ENOMEM ); + + iNumProcs = iSize / sizeof(struct kinfo_proc); + /* + * Search for the given process name and return its pid. + * / + + for (i = 0; i < iNumProcs; i++) { + iCurrentPid = sProcesses[i].kp_proc.p_pid; + if( strncmp(csProcessName, sProcesses[i].kp_proc.p_comm, MAXCOMLEN) == 0 ) { + free(sProcesses); + return iCurrentPid; + } + } + + /* + * Clean up and return -1 because the given proc name was not found + * / + + free(sProcesses); + return (-1); +} /* end of getProcessId() * / + + +int main(int argc, char **argv) + +{ + int pid = 0; + pid = getProcessId( argv[1] ); + if( pid < 0 ){ + printf("process [%s] not found in the system\n", argv[1]); + } else { + printf("pid of [%s] is %d\n", argv[1], pid); + } +} +*/ + + +bool SP_KERNEL::create(void){ + char tBuffer[PATH_MAX]; + if( m_pSettings != NULL ) { + config_setting_t * pSetting; + int tVal; + const char * tBuf; + LOG("Getting Settings ..."); + if( !config_setting_lookup_string( m_pSettings, "SOCKET_PATH", &tBuf ) ){ + LOG("Socket Path not defined - generating dummy!"); + sprintf( m_pUnixSocketName, "/srv/etc/watchdog" ); + pSetting = config_setting_add( m_pSettings, "SOCKET_PATH", CONFIG_TYPE_STRING ); + config_setting_set_string( pSetting, m_pUnixSocketName ); + basicSettingsProvider::getInstance()->compromise(); + } else { + sprintf( m_pUnixSocketName, tBuf ); + sprintf( tBuffer, "Socket Path From Settings: %s", tBuf ); + LOG( tBuffer ); + }; + /* + if( !config_setting_lookup_string( m_pSettings, "TARGET_APP_FILE", &tBuf ) ){ + LOG("Target Application not defined - generating dummy!"); + sprintf( m_pTargetAppFile, "TARGET_APP_NAME" ); + pSetting = config_setting_add( m_pSettings, "TARGET_APP_FILE", CONFIG_TYPE_STRING ); + config_setting_set_string( pSetting, m_pTargetAppFile ); + basicSettingsProvider::getInstance()->compromise(); + basicSettingsProvider::getInstance()->validate(); + LOG_ERROR("Exiting!"); + return false; + } else { + sprintf( m_pTargetAppFile, "%s/%s", m_pTargetEnvPath, tBuf ); + sprintf( tBuffer, "Target Application Read From Settings: %s", tBuf ); + LOG( tBuffer ); + if( !fileExist ( m_pTargetAppFile ) ) { + LOG_ERROR("Target Application File Not Exists!"); + LOG_ERROR("Exiting!"); + return false; + }; + }; + if( !config_setting_lookup_string( m_pSettings, "TARGET_PID_FILE", &tBuf ) ){ + LOG("Target PID File not defined - generating dummy!"); + sprintf( m_pTargetPIDFile, "TARGET_FILE_PATH" ); + pSetting = config_setting_add( m_pSettings, "TARGET_PID_FILE", CONFIG_TYPE_STRING ); + config_setting_set_string( pSetting, m_pTargetPIDFile ); + basicSettingsProvider::getInstance()->compromise(); + basicSettingsProvider::getInstance()->validate(); + LOG_ERROR("Exiting!"); + return false; + } else { + sprintf( m_pTargetPIDFile, tBuf ); + sprintf( tBuffer, "Target PID File Read From Settings: %s", tBuf ); + LOG( tBuffer ); + if( !fileExist ( m_pTargetPIDFile ) ) { + LOG_ERROR("Target PID File Not Exists!"); + LOG_ERROR("Starting Target Application!"); + if( !startTargetApplication() ){ + LOG_ERROR("Can not start application!"); + return false; + }; + }; + }; + */ + basicSettingsProvider::getInstance()->validate(); + } else { + LOG_ERROR("Failed Read Config Settings!"); + return false; + }; + + m_iServerSocket = socket( AF_UNIX, SOCK_STREAM, 0 ); + struct sockaddr_un server_addr; + + server_addr.sun_family = AF_UNIX; + strcpy( server_addr.sun_path, m_pUnixSocketName ); + int slen = sizeof( server_addr ); + + int x = fcntl( m_iServerSocket ,F_GETFL, 0); + fcntl( m_iServerSocket, F_SETFL, x | O_NONBLOCK); + + bind( m_iServerSocket, (struct sockaddr *) &server_addr, slen ); + if ( m_iServerSocket == INVALID_SOCKET ){ + LOG_ERROR("Could not bind to the UX socket!"); + return false; + }; + listen( m_iServerSocket, 5 ); + return init(); + }; + +void SP_KERNEL::WaitFinish( bool force ){ + waitThreadEnd( force ); +}; + +void *SP_KERNEL::sw_proc( void *ptr ){ + char tB[64]; + uint64_t cMS; + uint64_t tMS; + WATCHDOG_PROC_REC * mR = (WATCHDOG_PROC_REC *) ptr; + pid_t CPID = 0; + if ( mR ) { + cMS = getMilliseconds(); + tMS = cMS + SW_DEFAULT_TIMEOUT; + while( 1 ){ + cMS = getMilliseconds(); + int retVal = readLine( mR->client_socket, tB, 64 ); + if ( retVal < 0 ) { + if( errno!=EAGAIN && errno!=EINTR ) { + mR->master->m_pLog->Log( "K_T", mR->master, "Peer Closes Connection!"); + shutdown( mR->client_socket, 2 ); + close( mR->client_socket ); + mR->client_socket = -1; + break; //-----> exit thread + }; + } else { + if ( retVal ) { + //char jB[128]; + //sprintf( jB, "Server: I recieved [%d] %s from client!", retVal, tB ); + //mR->master->m_pLog->Log( "K_T", mR->master, jB ); + if( CPID ){ + uint64_t nMS = atoll(tB); + //sprintf( jB, "Will Wait next [%d] milliseconds", nMS ); + //mR->master->m_pLog->Log( "K_T", mR->master, jB ); + tMS = cMS + nMS; + } else { + CPID = ( pid_t ) atoll(tB); + }; + }; + }; + if ( cMS > tMS ) { + // WD TO EXIT! + mR->master->m_pLog->Log( "K_T", mR->master, "WD update STALLED!" ); + if ( CPID ) { + // Try Kill Client Process; + char jB[128]; + sprintf( jB, "Try Killing Process[%lld] By Signal 9!", CPID ); + mR->master->m_pLog->Log( "K_T", mR->master, jB ); + kill( CPID, 9 ); + } else { + mR->master->wd_shutdown(); + }; + break; //-----> exit thread + }; + SLEEP( SW_TIMEOUT_PERCISION ); + }; + }; + mR->finished = true; + pthread_exit( NULL ); + return NULL; + /* + read(client_socket, &ch, 1); + printf("\nServer: I recieved %c from client!\n", ch); + ch++; + write(client_socket, &ch, 1); + close(client_socket); + */ +}; + +void SP_KERNEL::m_fThreadProc( void ){ + struct sockaddr_un client_addr; + unsigned int clen = sizeof( client_addr ); + int ClientSocket; + m_bThreadStatus = TH_WORKING; + while( m_bThreadStatus < TH_ENDED ) { + + ClientSocket = accept( m_iServerSocket, (struct sockaddr *) &client_addr, &clen ); + + if ( ClientSocket < 0 ) { + switch( errno ){ + case EAGAIN:{ + // Checkup client disconnection + if( m_vWPV.size()) { + for( WATCHDOG_PROCS_VECTOR::iterator index = m_vWPV.begin(); index < m_vWPV.end(); index++ ) { + if( (*index)->finished ){ + LOG("Detach WD Procedure"); + pthread_join((*index)->handler, NULL); + LOG("WD Procedure Detached"); + // Remove Client Handler + LOG("Remove WD Procedure"); + m_vWPV.erase( index ); + }; + }; + }; + SLEEP(10); + continue; + }; + //case EWOULDBLOCK: me->Log(" The socket is marked non-blocking and no connections are present to be accepted."); break; + case EBADF: LOG_ERROR( "The descriptor is invalid." ); break; + case ECONNABORTED: LOG_ERROR( "A connection has been aborted."); break; + case EINTR: LOG_ERROR( "The system call was interrupted by a signal that was caught before a valid connection arrived."); break; + case EINVAL: LOG_ERROR( "Socket is not listening for connections, or addrlen is invalid (e.g., is negative)."); break; + case EMFILE: LOG_ERROR( "The per-process limit of open file descriptors has been reached."); break; + case ENFILE: LOG_ERROR( "The system limit on the total number of open files has been reached."); break; + case ENOTSOCK: LOG_ERROR( "The descriptor references a file, not a socket."); break; + case EOPNOTSUPP: LOG_ERROR( "The referenced socket is not of type SOCK_STREAM."); break; + case EFAULT: LOG_ERROR( "The addr argument is not in a writable part of the user address space."); break; + case ENOBUFS: + case ENOMEM: LOG_ERROR( "Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory."); break; + case EPROTO: LOG_ERROR( "Protocol error."); break; + case EPERM: LOG_ERROR( "Firewall rules forbid connection."); break; + case ENOSR: +#ifdef __linux__ + case ESOCKTNOSUPPORT: +#endif + case EPROTONOSUPPORT: + case ETIMEDOUT: LOG_ERROR( " Unusual error in Kernel (Timeout, No Socket Support in Kernel, Unsupported Protocol)"); break; + //case ERESTARTSYS: me->Log(" Unusual error by Kernel"); break; + default: LOG_ERROR( " Unhandled error in Kernel"); break; + }; + m_bThreadStatus = TH_ENDED; + } else { + WATCHDOG_PROC_REC *tPRC = (WATCHDOG_PROC_REC *) malloc(sizeof(WATCHDOG_PROC_REC)); + if ( tPRC ) { + + int x = fcntl( ClientSocket, F_GETFL, 0 ); + fcntl( ClientSocket, F_SETFL, x | O_NONBLOCK ); + + tPRC->client_socket = ClientSocket; + tPRC->master = this; + int RetVal = pthread_create( &tPRC->handler, NULL, sw_proc, ( void * ) tPRC ); + if( RetVal == -1 ) { + LOG_ERROR("No Client Thread Created!"); + shutdown( ClientSocket, 2 ); + close( ClientSocket ); + } else { + m_vWPV.push_back( tPRC ); + }; + } else { + LOG_ERROR("No Client Handler Created!"); + shutdown( ClientSocket, 2 ); + close( ClientSocket ); + }; + }; + }; +}; + +#undef LOG_ERROR +#undef LOG diff --git a/Projects/watchDogDaemon/kernel.h b/Projects/watchDogDaemon/kernel.h new file mode 100644 index 00000000..0f22a4d3 --- /dev/null +++ b/Projects/watchDogDaemon/kernel.h @@ -0,0 +1,49 @@ +#ifndef __START_PROC_KERNEL_HEADER__ +#define __START_PROC_KERNEL_HEADER__ + +#include "../../lib/components/base/basicThread.h" +#include "../../lib/components/base/basicSettingsProvider.h" + + +class SP_KERNEL; + +#define SW_DEFAULT_TIMEOUT 60000 // 60 seconds + +#define SW_TIMEOUT_PERCISION 100 // 0.1 second +#define INVALID_SOCKET -1 + +typedef struct S_WATCHDOG_PROC_REC{ + SP_KERNEL * master; + int client_socket; + uint64_t timeout; + bool finished; + pthread_t handler; +}WATCHDOG_PROC_REC; + + +typedef std::vector WATCHDOG_PROCS_VECTOR; + +class SP_KERNEL: public basicThread { + protected: + virtual void m_fThreadProc( void ); + public: + static SP_KERNEL * getInstance( bool ColdStart = true ); + bool create(void); + int getExitCause(){ return m_iExitCause; }; + void WaitFinish( bool force ); + ~SP_KERNEL(); + private: + int m_iServerSocket; + int m_iExitCause; + WATCHDOG_PROCS_VECTOR m_vWPV; + + static SP_KERNEL * _instance; + static void * sw_proc( void * ); + void wd_shutdown( void ); + SP_KERNEL(); + basicLogCollection * m_pLog; + config_setting_t * m_pSettings; + char m_pUnixSocketName[PATH_MAX]; +}; + +#endif diff --git a/Projects/watchDogDaemon/makefile b/Projects/watchDogDaemon/makefile new file mode 100644 index 00000000..4fc61d36 --- /dev/null +++ b/Projects/watchDogDaemon/makefile @@ -0,0 +1,160 @@ +# SlickEdit generated file. Do not edit this file except in designated areas. + +# Make command to use for dependencies +MAKE=make +RM=rm +MKDIR=mkdir + +# -----Begin user-editable area----- + +# -----End user-editable area----- + +# If no configuration is specified, "Debug" will be used +ifndef CFG +CFG=Debug +endif + +# +# Configuration: Debug +# +ifeq "$(CFG)" "Debug" +OUTDIR=Debug +OUTFILE=$(OUTDIR)/watchdogDaemon +CFG_INC= +CFG_LIB=-lpthread -lm -lconfig +CFG_OBJ= +COMMON_OBJ=$(OUTDIR)/basicFunctionality.o \ + $(OUTDIR)/basicLogCollection.o $(OUTDIR)/basicLoggingService.o \ + $(OUTDIR)/basicMemoryOperation.o $(OUTDIR)/basicRing.o \ + $(OUTDIR)/basicSettings.o $(OUTDIR)/basicSettingsProvider.o \ + $(OUTDIR)/basicThread.o $(OUTDIR)/kernel.o $(OUTDIR)/md5.o \ + $(OUTDIR)/objectConsoleLoggingService.o \ + $(OUTDIR)/objectFileLoggingService.o $(OUTDIR)/watchDogDaemon.o +OBJ=$(COMMON_OBJ) $(CFG_OBJ) +ALL_OBJ=$(OUTDIR)/basicFunctionality.o $(OUTDIR)/basicLogCollection.o \ + $(OUTDIR)/basicLoggingService.o $(OUTDIR)/basicMemoryOperation.o \ + $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ + $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ + $(OUTDIR)/kernel.o $(OUTDIR)/md5.o \ + $(OUTDIR)/objectConsoleLoggingService.o \ + $(OUTDIR)/objectFileLoggingService.o $(OUTDIR)/watchDogDaemon.o \ + -lpthread -lm -lconfig + +COMPILE=g++ -c -O0 -g3 -ggdb -o "$(OUTDIR)/$(*F).o" $(CFG_INC) $< +LINK=g++ -O0 -g3 -ggdb -o "$(OUTFILE)" $(ALL_OBJ) + +# Pattern rules +$(OUTDIR)/%.o : ../../lib/components/object/%.cpp + $(COMPILE) + +$(OUTDIR)/%.o : ../../lib/components/base/%.cpp + $(COMPILE) + +$(OUTDIR)/%.o : ../../lib/alg/generic/%.cpp + $(COMPILE) + +$(OUTDIR)/%.o : %.cpp + $(COMPILE) + +# Build rules +all: $(OUTFILE) + +$(OUTFILE): $(OUTDIR) $(OBJ) + $(LINK) + +$(OUTDIR): + $(MKDIR) -p "$(OUTDIR)" + +# Rebuild this project +rebuild: cleanall + @$(MAKE) -f "$(strip $(MAKEFILE_LIST))" $(MAKEFLAGS) all + +# Clean this project +clean: + $(RM) -f $(OUTFILE) + $(RM) -f $(OBJ) + +# Clean this project and all dependencies +cleanall: clean +endif + +# +# Configuration: Release +# +ifeq "$(CFG)" "Release" +OUTDIR=Release +OUTFILE=$(OUTDIR)/watchdogDaemon +CFG_INC= +CFG_LIB=-lpthread -lm -lconfig +CFG_OBJ= +COMMON_OBJ=$(OUTDIR)/basicFunctionality.o \ + $(OUTDIR)/basicLogCollection.o $(OUTDIR)/basicLoggingService.o \ + $(OUTDIR)/basicMemoryOperation.o $(OUTDIR)/basicRing.o \ + $(OUTDIR)/basicSettings.o $(OUTDIR)/basicSettingsProvider.o \ + $(OUTDIR)/basicThread.o $(OUTDIR)/kernel.o $(OUTDIR)/md5.o \ + $(OUTDIR)/objectConsoleLoggingService.o \ + $(OUTDIR)/objectFileLoggingService.o $(OUTDIR)/watchDogDaemon.o +OBJ=$(COMMON_OBJ) $(CFG_OBJ) +ALL_OBJ=$(OUTDIR)/basicFunctionality.o $(OUTDIR)/basicLogCollection.o \ + $(OUTDIR)/basicLoggingService.o $(OUTDIR)/basicMemoryOperation.o \ + $(OUTDIR)/basicRing.o $(OUTDIR)/basicSettings.o \ + $(OUTDIR)/basicSettingsProvider.o $(OUTDIR)/basicThread.o \ + $(OUTDIR)/kernel.o $(OUTDIR)/md5.o \ + $(OUTDIR)/objectConsoleLoggingService.o \ + $(OUTDIR)/objectFileLoggingService.o $(OUTDIR)/watchDogDaemon.o \ + -lpthread -lm -lconfig + +COMPILE=g++ -c -O3 -o "$(OUTDIR)/$(*F).o" $(CFG_INC) $< +LINK=g++ -O3 -o "$(OUTFILE)" $(ALL_OBJ) + +# Pattern rules +$(OUTDIR)/%.o : ../../lib/components/object/%.cpp + $(COMPILE) + +$(OUTDIR)/%.o : ../../lib/components/base/%.cpp + $(COMPILE) + +$(OUTDIR)/%.o : ../../lib/alg/generic/%.cpp + $(COMPILE) + +$(OUTDIR)/%.o : %.cpp + $(COMPILE) + +# Build rules +all: $(OUTFILE) + +$(OUTFILE): $(OUTDIR) $(OBJ) + $(LINK) + +$(OUTDIR): + $(MKDIR) -p "$(OUTDIR)" + +# Rebuild this project +rebuild: cleanall + @$(MAKE) -f "$(strip $(MAKEFILE_LIST))" $(MAKEFLAGS) all + +# Clean this project +clean: + $(RM) -f $(OUTFILE) + $(RM) -f $(OBJ) + +# Clean this project and all dependencies +cleanall: clean +endif + +# +# include dependencies: +# +$(OUTDIR)/basicFunctionality.o: ../../lib/components/base/basicFunctionality.cpp ../../lib/components/base/basicFunctionality.h ../../lib/alg/generic/md5.h ../../lib/components/base/basicLogCollection.h +$(OUTDIR)/basicLogCollection.o: ../../lib/components/base/basicLogCollection.cpp ../../lib/components/base/basicLogCollection.h ../../lib/alg/generic/heapDebug.h +$(OUTDIR)/basicLoggingService.o: ../../lib/components/base/basicLoggingService.cpp ../../lib/components/base/basicLoggingService.h +$(OUTDIR)/basicMemoryOperation.o: ../../lib/components/base/basicMemoryOperation.cpp ../../lib/components/base/basicMemoryOperation.h +$(OUTDIR)/basicRing.o: ../../lib/components/base/basicRing.cpp ../../lib/components/base/basicRing.h +$(OUTDIR)/basicSettings.o: ../../lib/components/base/basicSettings.cpp ../../lib/components/base/basicSettings.h +$(OUTDIR)/basicSettingsProvider.o: ../../lib/components/base/basicSettingsProvider.cpp ../../lib/components/base/basicSettingsProvider.h +$(OUTDIR)/basicThread.o: ../../lib/components/base/basicThread.cpp ../../lib/components/base/basicThread.h +$(OUTDIR)/kernel.o: kernel.cpp kernel.h ../../lib/components/base/basicThread.h ../../lib/components/base/basicSettingsProvider.h +$(OUTDIR)/md5.o: ../../lib/alg/generic/md5.cpp ../../lib/alg/generic/md5.h +$(OUTDIR)/objectConsoleLoggingService.o: ../../lib/components/object/objectConsoleLoggingService.cpp ../../lib/components/object/objectConsoleLoggingService.h +$(OUTDIR)/objectFileLoggingService.o: ../../lib/components/object/objectFileLoggingService.cpp ../../lib/components/object/objectFileLoggingService.h +$(OUTDIR)/watchDogDaemon.o: watchDogDaemon.cpp watchDogDaemon.h ../../lib/components/base/basicSettings.h ../../lib/components/base/basicLogCollection.h ../../lib/components/base/basicSettingsProvider.h ../../lib/components/base/basicMemoryOperation.h ../../lib/components/object/objectFileLoggingService.h ../../lib/components/object/objectConsoleLoggingService.h kernel.h ../../lib/components/base/basicThread.h ../../lib/components/base/basicSettingsProvider.h diff --git a/Projects/watchDogDaemon/watchDogDaemon.cpp b/Projects/watchDogDaemon/watchDogDaemon.cpp new file mode 100644 index 00000000..e2cd9bb0 --- /dev/null +++ b/Projects/watchDogDaemon/watchDogDaemon.cpp @@ -0,0 +1,327 @@ +#include "watchDogDaemon.h" + +// Global Variables + +CSTR gsCFG_ADDRESS; +uint16_t guCFG_PORT = DEFAULT_SERVER_PORT; +CSTR gsCFG_LICENSE; +CSTR gsCFG_DB_SERVER; +CSTR gsCFG_DB_NAME; +CSTR gsCFG_DB_USER; +CSTR gsCFG_DB_PASSWORD; +char gsCFG_PID_FILE[PATH_MAX]; //= (CSTR) DEFAULT_SERVER_PID_FILE; +SOFTWARE_INFO gsSOFT_INFO; +bool gbCFG_MASTER_ROLE = true; + +bool gModule_GeoFence_Enabled = true; +bool gModule_SMS_Enabled = false; +bool gModule_Email_Enabled = false; +bool gSignal_Handler_Enabled = true; + +#define LOG( X ) m_pLog->Log( "main", 0, X ) +#define LOG_ERROR( X ) m_pLog->Log( "main", 0, _LOG_ERROR, X ) + +SP_KERNEL * Kernel = NULL; +basicLogCollection * m_pLog = NULL; + +void (*old_update_handler_2)(int); //SIGINT +void (*old_kill_handler_3)(int); //SIGQUIT +void (*old_kill_handler_9)(int); //SIGKILL +void (*old_alarm_handler_14)(int); //SIGALRM +void (*old_kill_handler_15)(int); //SIGTERM + + +/* + +FILE *f; +pid_t pid_child; +char proc_path[256]; + +f = fopen("/tmp/mydaemon.pid", "r"); +fscanf(f, "%d", &pid_child); +fclose(f); +sprintf(proc_path, "/proc/%d/exe", pid_child); + +while(1) { + if (access(proc_path, F_OK) == 0) { + printf("Program alive !\n"); + sleep(1); + } else { + printf("Program dead!\n"); + break; + } +} + + +*/ + +void signal_SIGKILL_handler( int SIG ){ + if( Kernel!= NULL ){ + if( gSignal_Handler_Enabled ){ + m_pLog->Log("SIG", 0, _LOG_DEBUG, "Shutdown Service by Signal" ); + Kernel->setStatus( TH_ENDED ); // Closing all Connections; + } else { + m_pLog->Log("SIG", 0, _LOG_DEBUG, "Shutdown Signal Handler Disabled" ); + }; + }; +}; + +//#include + +int main (int argc, char *argv[]){ + /* + const rlim_t kStackSize = 16 * 1024 * 1024; // min stack size = 16 MB + struct rlimit rl; + int result; + + result = getrlimit(RLIMIT_STACK, &rl); + if (result == 0){ + printf( "Stack Size: %d\n", rl.rlim_cur ); + if ( rl.rlim_cur < kStackSize ){ + printf( "Resize Stack to: %d\n", kStackSize ); + rl.rlim_cur = kStackSize; + result = setrlimit(RLIMIT_STACK, &rl); + if (result != 0){ + fprintf(stderr, "setrlimit returned result = %d\n", result); + return -1; + }; + }; + }; + */ + + //MySQL_Connector * DBC = NULL; + basicSettingsProvider * SETTINGS = NULL; + bool WBG = true; // Working Background + bool SSE = true; // Server Statistics Engine + bool NDC = true; // No Default Configuration + bool COLD_START = true; + char FLE_NAME[PATH_MAX] = {0}; + char DVD_NAME[PATH_MAX] = {0}; + CSTR DCF_NAME; + CSTR SSP_NAME; + objectFileLoggingService * FLE = NULL; + objectConsoleLoggingService * CLE = NULL; + // default init + DCF_NAME = (CSTR) DEFAULT_SERVER_CONFIG_PATH; + SSP_NAME = (CSTR) DEFAULT_SERVER_SOCKET_PATH; + + CSTR tB = get_current_dir_name(); + if( tB ) { + sprintf( DVD_NAME , "%s", tB ); + free( tB ); + } else { + printf( "Can Not Get CWD!\n"); + }; + sprintf( gsCFG_PID_FILE, "%s/%s", DVD_NAME, DEFAULT_SERVER_PID_FILE ); + sprintf( FLE_NAME , "%s/%s", DVD_NAME, DEFAULT_SERVER_LOG_FILE ); + gsSOFT_INFO.SW_BUILD_DATE = UNIX_TIMESTAMP; + gsSOFT_INFO.DATA_VALID = false; + m_pLog = basicLogCollection::getInstance( _LOG_LVL_TIME_PTR ); + gsSOFT_INFO.SW_MODE_FLAGS = 0 +#ifdef SKIP_TARGET_LOG + | SW_INFO_NO_TARGET_LOG +#endif +#ifdef HIBERNATE_ENABLE + | SW_INFO_HIBERNATE_ENA +#endif +#ifdef TGD + | SW_INFO_TGD_MOTORS + #ifdef TGD_VERSION_2 + | SW_INFO_TGD_VER_2 + #endif +#endif +#ifdef ENCODER_8192 + | SW_INFO_EU_PROJECT +#endif +#ifdef NO_BLACK_TARGETS + | SW_INFO_NO_BLACK_TARGETS +#endif +#ifndef NO_MOTORS + | SW_INFO_MOTOR_CTRL_ENA +#endif +#ifdef THREADED_FLIP + | SW_INFO_THREAD_FLIP +#endif +#ifndef PRU_SLAVE + | SW_INFO_FLAG_MASTER +#endif +#ifdef DEBUG + | SW_INFO_DEBUG_MODE +#endif + ; + if( !isProcessUniq( argv[0] ) ){ + printf( "The Process [%s] Already is Runing\n", argv[0] ); + return 33; + }; + // Reading Config + if( argc > 1 ){ + for( int i = 1; i < argc; i++ ){ + switch( argv[i][0] ){ + case '-': + case '\\': + case '/':{ + switch( argv[i][1] ){ + case 'h': case 'H': case 'v': case 'V': case '?':{ + printf( "Dr Frucht Systems R-Goose Server\n"); + printf( "Software version <%s>\n", VERSION_NUMBER ); + printf( " -h -H This Message\n" ); + printf( " -i -I Interactive Mode\n" ); + return 0; + break; + }; + case 'i': case 'I':{ + WBG = false; + CLE = new objectConsoleLoggingService(); + if( CLE != NULL ) { + m_pLog->addHandler( CLE ); + }; + break; + }; + case 'c': case 'C':{ + DCF_NAME = (CSTR) malloc( MAX_PATH_LEN ); + if( DCF_NAME != NULL ) { + sprintf( DCF_NAME, "%s", argv[i+1] ); + } else { + printf("ERROR!\n\n"); + return -1; + }; + break; + }; + }; + break; + }; + }; + }; + }; + FLE = new objectFileLoggingService( FLE_NAME ); + if( FLE != NULL ) { + m_pLog->addHandler( FLE ); + }; + LOG("Starting Log Engne!"); + if ( fileExist( DCF_NAME ) ){ + SETTINGS = basicSettingsProvider::getInstance(DCF_NAME); + } else { + SETTINGS = basicSettingsProvider::getInstance(); + }; + if ( fileExist( gsCFG_PID_FILE ) ){ + COLD_START = false; + //check if process already running + FILE *tFD = fopen( gsCFG_PID_FILE, "r" ); + if( tFD ) { + pid_t pid; + char tBuff[256]; + //fscanf( tFD, "%d\n", pid ); + fread( tBuff, 1, 255, tFD ); + pid = atoi( tBuff ); + printf("Old Process PID: %d\n", pid ); + if( kill( pid, 0 )==0 ){ + printf("Process Already Running --> exiting!\n"); + delete( FLE ); + fclose( tFD ); + exit(12); + } else { + printf("No Running Process --> Hot Restart Detected!\n"); + }; + fclose( tFD ); + }; + }; + // Demonizing or not + if( WBG ){ + printf( "Dr Frucht Systems Software Watchdog Daemon ver. %s\n", VERSION_NUMBER ); + printf( "Configuration Applied:\n"); + printf( " Service Established %s at %d\n", gsCFG_ADDRESS, guCFG_PORT ); + printf( " PID File: %s\n", gsCFG_PID_FILE ); + printf( " Log File: %s\n", FLE_NAME ); + printf( " License: %s\n", gsCFG_LICENSE ); + printf( " Database Server: %s Database: %s\n", gsCFG_DB_SERVER, gsCFG_DB_NAME ); + if( NDC ){ + printf( " No configuration file (%s) supplied.\n", DCF_NAME); + } else { + printf( " Configuration read from: %s\n", DCF_NAME ); + }; + + //Demonizing + pid_t pid, sid; + sync(); + close( STDIN_FILENO ); + close( STDOUT_FILENO ); + close( STDERR_FILENO ); + + pid = fork(); + if( pid < 0 ){ + return pid; + }; + if( pid > 0 ){ + return 0; + }; + //umask(0); + sid = setsid(); + if (sid < 0) { + return sid; + }; + + pid = fork(); + if( pid < 0 ){ + return pid; + }; + if( pid > 0 ){ + FILE *pPIDF = fopen( gsCFG_PID_FILE, "w+" ); + if( pPIDF ){ + fprintf( pPIDF, "%d\n", pid ); + fclose( pPIDF ); + } + return 0; + }; + }; + + uint8_t m_bKernelExitCode = 0; + + do{ + Kernel = SP_KERNEL::getInstance( COLD_START ); + if( Kernel == NULL ) { + // Aborting Work + LOG_ERROR( "++++ Kernel failed to born ++++" ); + unlink( gsCFG_PID_FILE ); + LOG( "PID File Deleted" ); + delete( m_pLog ); + return -1; + }; + //Loop till some Restart action + if( Kernel != NULL ){ + LOG( "++++ Server Started ++++" ); + if ( Kernel->create() == false ){ + LOG_ERROR("Kernel create failure!"); + } else { + //Install Signal Listeners + old_kill_handler_15 = signal( 15, signal_SIGKILL_handler ); + old_kill_handler_9 = signal( 9, signal_SIGKILL_handler ); + // Waiting for Worker to Shut Down + Kernel->WaitFinish( false ); + m_bKernelExitCode = Kernel->getExitCause(); + signal( 9, old_kill_handler_9 ); + signal( 15, old_kill_handler_15 ); + }; + delete( Kernel ); + LOG( "++++ Server Ended ++++" ); + if ( m_bKernelExitCode == 33 ) { + m_bKernelExitCode = 0; + LOG( "Reboot The System by WD!" ); + if( FLE!= NULL ) delete( FLE ); + FLE = NULL; + if( CLE!= NULL ) delete( CLE ); + CLE = NULL; + if( m_pLog ) delete( m_pLog ); + m_pLog = NULL; + system("/sbin/reboot"); + }; + }; + } while( m_bKernelExitCode > 0 ); + unlink( gsCFG_PID_FILE ); + if( m_pLog ) LOG( "PID File Deleted" ); + if( FLE!= NULL ) delete( FLE ); + if( CLE!= NULL ) delete( CLE ); + if( m_pLog ) delete( m_pLog ); + return(0); +} + + diff --git a/Projects/watchDogDaemon/watchDogDaemon.h b/Projects/watchDogDaemon/watchDogDaemon.h new file mode 100644 index 00000000..7f5c85d1 --- /dev/null +++ b/Projects/watchDogDaemon/watchDogDaemon.h @@ -0,0 +1,31 @@ + +/******************************************************************* +* +* DESCRIPTION: starProcDaemon project main header file +* +* AUTHOR: Gagua +* +* HISTORY: +* +* DATE:2/13/2022 +* +*******************************************************************/ +#ifndef __START_PROC_MAIN_HEADER__ +#define __START_PROC_MAIN_HEADER__ + +/** include files **/ + +#include +#include +#include + +#include "../../lib/components/base/basicSettings.h" +#include "../../lib/components/base/basicLogCollection.h" +#include "../../lib/components/base/basicSettingsProvider.h" +#include "../../lib/components/base/basicMemoryOperation.h" +#include "../../lib/components/object/objectFileLoggingService.h" +#include "../../lib/components/object/objectConsoleLoggingService.h" +#include "kernel.h" + + +#endif diff --git a/Projects/watchDogDaemon/watchDogDaemon.vpj b/Projects/watchDogDaemon/watchDogDaemon.vpj new file mode 100644 index 00000000..3edae74d --- /dev/null +++ b/Projects/watchDogDaemon/watchDogDaemon.vpj @@ -0,0 +1,254 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/App/common/cmdNodeConveyor.cpp b/lib/App/common/cmdNodeConveyor.cpp new file mode 100644 index 00000000..20ff64bb --- /dev/null +++ b/lib/App/common/cmdNodeConveyor.cpp @@ -0,0 +1,41 @@ +#include "cmdNodeConveyor.h" + +cmdNodeConveyor::cmdNodeConveyor(){ + m_pCurrentCMD = NULL; + m_mCommandProtector.unlock(); +}; + +cmdNodeConveyor::~cmdNodeConveyor(){ + m_mCommandProtector.lock(); + if( m_qCommands.size() ) { + NI_COMMAND *tVal = NULL; + while( tVal = m_qCommands.front() ){ + m_qCommands.pop(); + if( tVal->parameter ) { + free( tVal->parameter ); + }; + free( tVal ); + }; + }; + m_mCommandProtector.unlock(); +}; + + +NI_COMMAND *cmdNodeConveyor::fetchCommand(){ + NI_COMMAND *RetVal = NULL; + m_mCommandProtector.lock(); + if( m_qCommands.size() ) { + RetVal = m_qCommands.front(); + m_qCommands.pop(); + }; + m_mCommandProtector.unlock(); + return RetVal; +}; + +void cmdNodeConveyor::enqueueCommand( NI_COMMAND *ptr ){ + if( ptr ) { + m_mCommandProtector.lock(); + m_qCommands.push( ptr ); + m_mCommandProtector.unlock(); + }; +}; diff --git a/lib/App/common/cmdNodeConveyor.h b/lib/App/common/cmdNodeConveyor.h new file mode 100644 index 00000000..d6a0ffe5 --- /dev/null +++ b/lib/App/common/cmdNodeConveyor.h @@ -0,0 +1,42 @@ +#ifndef __CMD_NODE_CONVEYOUR_HEADER__ +#define __CMD_NODE_CONVEYOUR_HEADER__ + +#include "dataBinder.h" +#include +#include +#include + +typedef struct S_NI_COMMAND{ + uint8_t cmd; + uint32_t db_cmd; + void * parameter; +}NI_COMMAND; + +typedef std::queue queueNI_COMMANDS; + +typedef enum E_NI_COMMAND_STATUSES{ + NI_CMD_NEW = 0, + NI_CMD_STARTED = 1, + NI_CMD_FINISHED = 100, + NI_CMD_FAILED = 128 +}NI_COMMAND_STATUSES; + +class cmdNodeConveyor{ + private: + queueNI_COMMANDS m_qCommands; + basicMutex m_mCommandProtector; + NI_COMMAND * m_pCurrentCMD; + basicMutex m_mCurrentCMDProtector; + protected: + NI_COMMAND * fetchCommand(); + public: + cmdNodeConveyor(); + ~cmdNodeConveyor(); + void enqueueCommand( NI_COMMAND *ptr ); + virtual bool updateCommandStatus( NI_COMMAND_STATUSES stt ){ return true; }; + virtual bool updateCommandAccomplishment( uint8_t perc ){ return true; }; +}; + + + +#endif \ No newline at end of file diff --git a/lib/App/common/dataBinder_structures.h b/lib/App/common/dataBinder_structures.h index f4ad090a..d3cc719e 100644 --- a/lib/App/common/dataBinder_structures.h +++ b/lib/App/common/dataBinder_structures.h @@ -597,6 +597,12 @@ typedef struct S_DBC_MODEM_MANAGER_DATA{ uint32_t id; uint32_t time_window; uint32_t evt_trigers; + uint16_t modem_id; + //char call_target[64]; + //char sms_target[64]; + bool sms_enabled; + bool call_enabled; + uint8_t call_hangup_wait; }DBC_MODEM_MANAGER_DATA; typedef struct S_DBC_MODEM_DATA{ @@ -623,6 +629,15 @@ typedef struct S_DBC_MODEM_DATA{ char registration[65]; }DBC_MODEM_DATA; +typedef struct S_DBC_MODEM_PHONEBOOK_RECORD{ + uint32_t id; + char number[64]; + bool call_only; + uint32_t trigger_special; + char label[64]; + char custom_msg[128]; +}DBC_MODEM_PHONEBOOK_RECORD; + typedef struct S_DBC_3DLRF_WSTAT{ }DBC_3DLRF_WSTAT; diff --git a/lib/App/common/deviceNode.cpp b/lib/App/common/deviceNode.cpp index 7d1eca58..ee62fa20 100644 --- a/lib/App/common/deviceNode.cpp +++ b/lib/App/common/deviceNode.cpp @@ -18,14 +18,13 @@ #define LOG_ERROR( X ) m_pLog->Log( "NODE", this, _LOG_ERROR, X ); #endif -deviceNode::deviceNode( uint32_t id ):basicEngine(){ +deviceNode::deviceNode( uint32_t id ):cmdNodeConveyor(),basicEngine(){ m_pLog = basicLogCollection::getInstance(); #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); #else m_pDBC = sqliteDBC::getInstance(); #endif - m_mCommandProtector.unlock(); if( m_pDBC != NULL ) { DBC_NODE_DATA *tData = m_pDBC->getNodeData( id ); if( tData != NULL ) { @@ -44,14 +43,13 @@ deviceNode::deviceNode( uint32_t id ):basicEngine(){ }; }; -deviceNode::deviceNode( DBC_NODE_DATA *Data ):basicEngine(){ +deviceNode::deviceNode( DBC_NODE_DATA *Data ):cmdNodeConveyor(),basicEngine(){ m_pLog = basicLogCollection::getInstance(); #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); #else m_pDBC = sqliteDBC::getInstance(); #endif - m_mCommandProtector.unlock(); if( Data ) { memcpy( &m_sNodeData , Data, sizeof(DBC_NODE_DATA)); if( m_pDBC ) { @@ -64,41 +62,9 @@ deviceNode::deviceNode( DBC_NODE_DATA *Data ):basicEngine(){ }; deviceNode::~deviceNode(){ - m_mCommandProtector.lock(); - if( m_qCommands.size() ) { - NI_COMMAND *tVal = NULL; - while( tVal = m_qCommands.front() ){ - m_qCommands.pop(); - if( tVal->parameter ) { - free( tVal->parameter ); - }; - free( tVal ); - }; - }; - m_mCommandProtector.unlock(); m_pDBC->setNodeOffline(m_sNodeData.node_id); }; - -NI_COMMAND *deviceNode::fetchCommand(){ - NI_COMMAND *RetVal = NULL; - m_mCommandProtector.lock(); - if( m_qCommands.size() ) { - RetVal = m_qCommands.front(); - m_qCommands.pop(); - }; - m_mCommandProtector.unlock(); - return RetVal; -}; - -void deviceNode::enqueueCommand( NI_COMMAND *ptr ){ - if( ptr ) { - m_mCommandProtector.lock(); - m_qCommands.push( ptr ); - m_mCommandProtector.unlock(); - }; -}; - bool deviceNode::validate(){ DBC_NODE_DATA *tDATA = m_pDBC->getNodeData( m_sNodeData.node_id ); if( tDATA ) { diff --git a/lib/App/common/deviceNode.h b/lib/App/common/deviceNode.h index b94b140c..9d49e00f 100644 --- a/lib/App/common/deviceNode.h +++ b/lib/App/common/deviceNode.h @@ -2,28 +2,16 @@ #define __DEVICE_NODE_HEADER__ #include "../../components/base/basicEngine.h" -#include "dataBinder.h" -#include -#include -#include +#include "cmdNodeConveyor.h" -typedef struct S_NI_COMMAND{ - uint8_t cmd; - uint32_t db_cmd; - void * parameter; -}NI_COMMAND; - -typedef std::queue queueNI_COMMANDS; - -class deviceNode: public basicEngine{ +class deviceNode: public cmdNodeConveyor, public basicEngine{ private: - queueNI_COMMANDS m_qCommands; - basicMutex m_mCommandProtector; + protected: basicLogCollection * m_pLog; dataBinder * m_pDBC; DBC_NODE_DATA m_sNodeData; - NI_COMMAND * fetchCommand(); + public: explicit deviceNode( uint32_t id ); explicit deviceNode( DBC_NODE_DATA *Data ); @@ -31,8 +19,7 @@ class deviceNode: public basicEngine{ uint32_t getNodeID() const { return m_sNodeData.node_id; }; uint32_t getDeviceID() const { return m_sNodeData.aux_id; }; ARGUS_NODE_TYPE getNodeType() const { return (ARGUS_NODE_TYPE) m_sNodeData.type; }; - bool isNodeOnline( void ) const { return m_sNodeData.online; }; - void enqueueCommand( NI_COMMAND *ptr ); + bool isNodeOnline( void ) const { return m_sNodeData.online; }; virtual bool validate(); }; diff --git a/lib/App/common/deviceNodeFactory.cpp b/lib/App/common/deviceNodeFactory.cpp index c61ba06d..97d16e33 100644 --- a/lib/App/common/deviceNodeFactory.cpp +++ b/lib/App/common/deviceNodeFactory.cpp @@ -483,7 +483,7 @@ void deviceNodeFactory::m_fThreadProc( void ){ tList = m_pDBC->listNewNodes( NULL ); }; if( tList != NULL ) { -#ifdef DEBUG +#ifdef DEBUG_EXTRA LOG("Nodes are Added!"); #endif for( DBC_INT_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) { diff --git a/lib/App/common/hardwareManager.cpp b/lib/App/common/hardwareManager.cpp index fccb8bf6..b1c0672b 100644 --- a/lib/App/common/hardwareManager.cpp +++ b/lib/App/common/hardwareManager.cpp @@ -64,10 +64,32 @@ hardwareManager::hardwareManager( bool cold_start ){ m_nServerNodeID = 0; m_nServerID = 0; m_nSiteID = 0; - m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT; + memset( &m_sSYS_INFO, 0, sizeof(DBC_SYS_INFO) ); strncpy( m_sSYS_INFO.SW_INFO, _BUILD_STRING_, 128 ); + config_setting_t * m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, "HARDWARE" ); + if ( m_pSettings ) { + config_setting_t * pSetting; + long long int tVal; + const char * tBuf; + char tBuffer[128]; + if( !config_setting_lookup_int64( m_pSettings, "MEMORY_LIMIT", &tVal ) ){ + tVal = m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT; + pSetting = config_setting_add( m_pSettings, "MEMORY_LIMIT", CONFIG_TYPE_INT64 ); + config_setting_set_int64( pSetting, tVal ); + basicSettingsProvider::getInstance()->compromise(); + LOG("MEMORY LIMIT NOT DEFINED - Using 2Gb Default!"); + } else { + m_nMemoryLimitHeapSize = tVal; + sprintf( tBuffer, "MEMORY LIMIT Read From Settings: %lld bytes", m_nMemoryLimitHeapSize ); + LOG( tBuffer ); + }; + basicSettingsProvider::getInstance()->validate(); + + } else { + m_nMemoryLimitHeapSize = MEMORY_CONSUMPTION_LIMIT; + }; #ifndef NO_EXEC_EXTERNALS FILE *fd = popen("/bin/uname -s -r -v -m","r"); if( fd!= NULL ) { @@ -341,7 +363,7 @@ bool hardwareManager::validate( void ){ size_t memStat = getCurrentRSS(); #ifdef DEBUG_EXTRA char MEM_MSG[128]; - sprintf( MEM_MSG, "MEM: %d TARG: %d", memStat, m_nMemoryLimitHeapSize ); + sprintf( MEM_MSG, "MEM: %lld TARG: %lld", memStat, m_nMemoryLimitHeapSize ); LOG( MEM_MSG ); #endif //https://www.ebay.com/itm/274974088159? diff --git a/lib/App/common/hardwareManager.h b/lib/App/common/hardwareManager.h index 46e9bc2a..e10bc4e0 100644 --- a/lib/App/common/hardwareManager.h +++ b/lib/App/common/hardwareManager.h @@ -5,7 +5,8 @@ #include "../components/base/basicMutex.h" #include "hardwareResource.h" -#define MEMORY_CONSUMPTION_LIMIT (524288000UL) //500 MB ~ 1 Hour +//#define MEMORY_CONSUMPTION_LIMIT (524288000UL) //500 MB ~ 1 Hour +#define MEMORY_CONSUMPTION_LIMIT (2147483648UL) // 2GB class hardwareManager{ private: diff --git a/lib/App/common/hardwareResourceNetworkProvider.h b/lib/App/common/hardwareResourceNetworkProvider.h index 699e3efb..98ce3ae4 100644 --- a/lib/App/common/hardwareResourceNetworkProvider.h +++ b/lib/App/common/hardwareResourceNetworkProvider.h @@ -13,7 +13,7 @@ class hardwareResourceNetwork: public hardwareResource { CSTR getBroadcast( NETWORK_TYPE nt=NETWORK_IPV4 ); CSTR getMask( NETWORK_TYPE nt=NETWORK_IPV4 ); CSTR getGateway( NETWORK_TYPE nt=NETWORK_IPV4 ); - virtual bool create( void ){ return true; }; + virtual bool create( void ){ return m_bValid; }; }; #endif diff --git a/lib/App/common/kernelEventFilterTrain.cpp b/lib/App/common/kernelEventFilterTrain.cpp index 11ce1598..a1439e8c 100644 --- a/lib/App/common/kernelEventFilterTrain.cpp +++ b/lib/App/common/kernelEventFilterTrain.cpp @@ -15,11 +15,12 @@ kernelEventFilterTrain::kernelEventFilterTrain( uint32_t id ): kernelEventFilter( id, TF_LA_METRO_TRAIN_FILTER ){ m_pLog = basicLogCollection::getInstance(); + m_oOSP_Helper = NULL; memset( &m_sTrainFilterData, 0, sizeof(TRAIN_FILTER_DATA)); }; kernelEventFilterTrain::~kernelEventFilterTrain(){ - + if( m_oOSP_Helper ) delete( m_oOSP_Helper ); }; bool kernelEventFilterTrain::validate(){ @@ -294,7 +295,9 @@ TrainFilterOSP_Helper::TrainFilterOSP_Helper( CSTR remote_ip ){ }; TrainFilterOSP_Helper::~TrainFilterOSP_Helper(){ + LOG("Destructor"); waitThreadEnd( true ); + LOG("Destructor Finished"); }; //https://www.geeksforgeeks.org/udp-server-client-implementation-c/ @@ -385,6 +388,7 @@ void TrainFilterOSP_Helper::m_fThreadProc( void ){ }; }; }; + //Close m_bStage = OSP_END; }; @@ -432,7 +436,7 @@ bool TrainFilterOSP_Helper::setLocalStatus( bool train_on_line ){ socklen_t len; sprintf( buffer, "%d", train_on_line ); - sendto( sockfd, (const char *)buffer, strlen(buffer), MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr) ); + sendto( sockfd, (const char *)buffer, strlen(buffer), 0 /*MSG_CONFIRM*/, (const struct sockaddr *) &servaddr, sizeof(servaddr) ); #ifdef DEBUG_EXTRA LOG( "Status message sent." ); #endif diff --git a/lib/App/common/kernelModule.cpp b/lib/App/common/kernelModule.cpp index 176b85b1..7b249e36 100644 --- a/lib/App/common/kernelModule.cpp +++ b/lib/App/common/kernelModule.cpp @@ -6,7 +6,7 @@ #include "SQLite_dataBinder.h" #endif -kernelModule::kernelModule( uint32_t id, KERNEL_MODULE_TYPE type ): basicEventHandler(), basicObject(){ +kernelModule::kernelModule( uint32_t id, KERNEL_MODULE_TYPE type ): basicEventHandler(), basicEngine(){ m_pLog = basicLogCollection::getInstance(); #ifdef USE_MYSQL m_pDBC = mysqlDBC::getInstance(); diff --git a/lib/App/common/kernelModule.h b/lib/App/common/kernelModule.h index b9006151..eb7ff085 100644 --- a/lib/App/common/kernelModule.h +++ b/lib/App/common/kernelModule.h @@ -25,7 +25,7 @@ typedef enum E_KERNEL_MODULE_TYPE{ KERNEL_MODULE_REPORT_BACKUPPER = 11 }KERNEL_MODULE_TYPE; -class kernelModule: public basicEventHandler, basicObject{ +class kernelModule: public basicEventHandler, public basicEngine{ protected: basicLogCollection * m_pLog; dataBinder * m_pDBC; diff --git a/lib/App/common/kernelModuleModemManager.cpp b/lib/App/common/kernelModuleModemManager.cpp index 877969e0..c9edc3f4 100644 --- a/lib/App/common/kernelModuleModemManager.cpp +++ b/lib/App/common/kernelModuleModemManager.cpp @@ -9,367 +9,221 @@ kernelModule::m_pLog->Log( "MOD_MODEM", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ } #else - #define LOG_ERROR( X ) kernelModule::m_pLog->Log( "MOD_MODEM", this, _LOG_ERROR, X ); + #define LOG_ERROR( X ) kernelModule::m_pLog->Log( "MOD_MODEM", this, _LOG_ERROR, X ) #endif -kernelModuleModemManager::kernelModuleModemManager( uint32_t moduleID ): kernelModule( moduleID, KERNEL_MODULE_MODEM_MANAGER ), basicThread(){ - LOG("Creating!"); +kernelModuleModemManager::kernelModuleModemManager( uint32_t moduleID ): kernelModule( moduleID, KERNEL_MODULE_MODEM_MANAGER ) /*, basicThread() */{ if( checkCmdExists("mmcli") > 0 ){ - //Modem Manager Exists - LOG("Manager OK!"); - //m_pDBC->clenupModemInfos(); - DBC_INT_LIST * tList = enumerateModems(); - if ( tList != NULL ) { - if( tList->size() ) { - for( DBC_INT_LIST::iterator idx = tList->begin(); idx < tList->end(); idx++ ) { - DBC_MODEM_DATA * tRecord = getModemInfo( *idx ); - if ( tRecord ) { - //m_pDBC->updateModemInfo( tRecord ); - free( tRecord ); - }; - }; - } else { - LOG("No Modems Presented in the System!"); - }; - delete(tList); - }; + LOG("ModemManager Present!"); + } else { + LOG_ERROR("No ModemManager installed in the System!"); }; }; kernelModuleModemManager::~kernelModuleModemManager(){ - //if ( m_pvModems!= NULL ) delete( m_pvModems ); + //if ( m_vUserData.size() ) { + //for ( VECTOR_MDM_USER_DATA::iterator idx = m_vUserData.begin(); idx < m_vUserData.end(); idx++ ) { + // if ( !(*idx)->finished ) pthread_join( (*idx)->handler, NULL); + //}; + //}; }; bool kernelModuleModemManager::reloadSettings(){ + LOG("Reload"); + //Reload Phonebook + return true; }; bool kernelModuleModemManager::startService(){ - return true; + LOG("Start Service"); + //Load PhoneBook + setWM( MDM_IDLE_WM ); + return moduleActive = init(); }; bool kernelModuleModemManager::stopService( bool force ){ - return true; + LOG("Stop Service"); + setWM( MDM_IDLE_WM ); + return moduleActive = !uninit(); }; void kernelModuleModemManager::m_fThreadProc( void ){ - + uint32_t nCurrentPhoneBookId = 0; + uint32_t nCurrentCallId = 0; + uint64_t lNextWindowAction = 0; + m_bThreadStatus == TH_WORKING; + while ( m_bThreadStatus < TH_ENDED ) { + uint8_t bCWM = getWM(); + switch ( bCWM ) { + case MDM_IDLE_WM:{ + break; + }; + case MDM_INIT_CALL_WM:{ + if ( nCurrentPhoneBookId < m_vPhoneBook.size() ) { + DBC_MODEM_PHONEBOOK_RECORD *tRECORD = m_vPhoneBook.at( nCurrentPhoneBookId ); + if ( tRECORD ) { + if( tRECORD->call_only ){ + LOG("Start Call"); + nCurrentCallId = startCall( tRECORD->number ); + lNextWindowAction = getMilliseconds() + m_sMMData.call_hangup_wait * 1000; + // Need to wait till calling + setWM( MDM_MODEM_IS_BUSY_WM ); + } else { + LOG("Start SMS"); + nCurrentCallId = 0; + sendSMS( tRECORD->number, tRECORD->custom_msg ); + //No need to wait SMS + setWM( MDM_FETCH_NEXT_WM ); + }; + } else { + LOG_ERROR("No Phone Book Record!"); + sessionStarted = false; + setWM( MDM_IDLE_WM ); + }; + } else { + LOG_ERROR("Phone Book is Empty!"); + sessionStarted = false; + setWM( MDM_IDLE_WM ); + }; + break; + }; + case MDM_MODEM_IS_BUSY_WM:{ + if ( lNextWindowAction < getMilliseconds() ){ + LOG("Hangup Call"); + hangupCall( nCurrentCallId ); + setWM( MDM_FETCH_NEXT_WM ); + }; + break; + }; + case MDM_FETCH_NEXT_WM:{ + if ( nCurrentPhoneBookId < m_vPhoneBook.size() ) { + nCurrentPhoneBookId++; + setWM( MDM_INIT_CALL_WM ); + } else { + nCurrentPhoneBookId = 0; + lNextWindowAction = getMilliseconds() + m_sMMData.time_window; + setWM( MDM_WAIT_WINDOW_WM ); + }; + break; + }; + case MDM_WAIT_WINDOW_WM:{ + if ( lNextWindowAction < getMilliseconds() ){ + sessionStarted = false; + setWM( MDM_IDLE_WM ); + }; + break; + }; + }; + SLEEP(DEFAULT_THREAD_SLEEP_TIME); + }; }; void kernelModuleModemManager::dispatchEvent(EVENT_CONTAINER *Evt){ + if ( moduleActive ) { + if ( sessionStarted ) { + // Seek just Close Session EVT + if( Evt->m_eType == EVT_LRF_END_SESSION_TO || Evt->m_eType == EVT_LRF_END_SESSION_MA ){ + sessionStarted = false; + }; + } else { + // React on All Events. + switch ( Evt->m_eType ) { + case EVT_TARGET_1D: + case EVT_TARGET_3D_WHITE: + case EVT_TARGET_3D_BLACK: + case EVT_TARGET_2D_WHITE: + case EVT_TARGET_2D_BLACK:{ + sessionStarted = true; + setWM( MDM_INIT_CALL_WM ); + break; + }; + }; + }; + }; }; bool kernelModuleModemManager::validate(){ - //DBC_MODEM_DATA *tDATA = getModemInfo( 0 ); - //if ( tDATA ) { - // free( tDATA ); - //}; + if ( moduleActive ) { + // Get current WAN IP Address + // + //LOG("Validate"); + + }; return true; }; -typedef enum E_CFG_TAG_ID{ - CFG_GENERAL = 0, - CFG_HARDWARE = 1, - CFG_SYSTEM, - CFG_STATUS, - CFG_MODES, - CFG_BANDS, - CFG_IP, - CFG_EPS, - CFG_3GPP, - CFG_SIM, - CFG_BEARER, - CFG_COUNT -}CFG_TAG_ID; - -const CSTR cfg_tags[]={ - "General", - "Hardware", - "System", - "Status", - "Modes", - "Bands", - "IP", - "3GPP EPS", - "3GPP", - "SIM", - "Bearer" +#define TLOG_ERROR( X ) DATA->parent->m_pLog->Log( "MOD_MODEM_T", DATA->parent, _LOG_ERROR, X ) + +void *kernelModuleModemManager::callProc( void *ptr ){ + MDM_CALL_DATA *DATA = (MDM_CALL_DATA *)ptr; + if ( DATA ) { + char CMD[128]; + uint32_t callID = 0; + sprintf( CMD, "mmcli -m %d --voice-create-call='number=%s'", DATA->modem_id, DATA->target ); +/* + +Successfully created new call: /org/freedesktop/ModemManager1/Call/2 + +*/ + FILE *fd = popen( CMD, "r"); + if ( fd ) { + + pclose( fd ); + }; + if ( callID ) { + // Wait X seconds then hang call + SLEEP( DATA->parent->m_sMMData.call_hangup_wait * 1000 ); + sprintf( CMD, "mmcli -m %d --close-call %d", DATA->modem_id, callID ); + fd = popen( CMD, "r"); + if ( fd ) { + + pclose( fd ); + } else { + TLOG_ERROR("Cannot Execute Call Hang Command!"); + }; + } else TLOG_ERROR("Call is not registered at Modem!"); + }; + pthread_exit( NULL ); + return NULL; }; -DBC_MODEM_DATA *kernelModuleModemManager::getModemInfo( uint32_t modem_id ){ - char tB[1024]; - char expBuf[1024]; - FILE * fd = NULL; - CFG_TAG_ID cMode = CFG_GENERAL; - DBC_MODEM_DATA * retVal = (DBC_MODEM_DATA *)malloc(sizeof(DBC_MODEM_DATA)); +void *kernelModuleModemManager::smsProc( void *ptr ){ + MDM_SMS_DATA *DATA = (MDM_SMS_DATA *)ptr; + if ( DATA ) { - if ( retVal != NULL ) { - memset( retVal, 0 , sizeof(DBC_MODEM_DATA)); // --output-json - sprintf( tB, "mmcli --modem=%d", modem_id ); - fd = popen( tB, "r"); - bool tPortsParsing = false; - if (fd!= NULL ) { - retVal->modem_id = modem_id; - while ( !feof( fd )) { - fgets( (CSTR)tB, 1024, fd ); - trim( tB ); - if( strstr(tB, "error:" ) != NULL){ - free( retVal ); - retVal = NULL; - break; - }; - LOG(tB); - strncpy( expBuf, tB, 1024 ); - CArray *CA = explode( ':', expBuf ); - if( CA ){ - if( CA->size() ){ - for( int ii = CFG_GENERAL; ii < CFG_COUNT; ii++ ){ - if( strstr( CA->at(0), cfg_tags[ii] ) != NULL ){ - cMode = (CFG_TAG_ID) ii; - break; - }; - }; - switch( cMode ){ - case CFG_GENERAL:{ - //LOG("GEN"); - if ( strstr( CA->at(0), "device id") != NULL ) { - trim(CA->at(1)); - strncpy( retVal->dbus_dev_id, CA->at(1), 64 ); - retVal->dbus_dev_id[64] = (char)0; - break; - }; - break; - }; - case CFG_HARDWARE:{ - //LOG("HW"); - if ( strstr( tB, "manufacturer") != NULL ) { - trim(CA->at(1)); - strncpy( retVal->hw_manufac, CA->at(1), 64); - retVal->hw_manufac[64] = (char)0; - break; - }; - if ( strstr( tB, "model") != NULL ) { - trim(CA->at(1)); - strncpy(retVal->hw_model, CA->at(1), 64); - retVal->hw_model[64] = (char)0; - break; - }; - if ( strstr( tB, "h/w revision") != NULL ) { - trim(CA->at(1)); - strncpy(retVal->hw_revision, CA->at(1), 64); - retVal->hw_revision[64] = (char)0; - break; - }; - if ( strstr( tB, "revision") != NULL ) { - trim(CA->at(1)); - strncpy(retVal->hw_version, CA->at(1), 32); - retVal->hw_revision[32] = (char)0; - break; - }; - if ( strstr( tB, "equipment id") != NULL ) { - trim(CA->at(1)); - strncpy(retVal->hw_id, CA->at(1), 32); - retVal->hw_id[32] = (char)0; - break; - }; - break; - }; - case CFG_SYSTEM:{ - //LOG("SYS"); - if ( strstr( tB, "ports") != NULL ) { - tPortsParsing = true; - }; - if( tPortsParsing ){ - trim( CA->at(1) ); - LOG("Ports:"); - LOG( CA->at(1) ); - CArray *CZ = explode( ':', CA->at(1) ); - if ( CZ ) { - int CP = 0; - for (int z=0; z< CZ->size(); z++) { - if ( strstr(CZ->at(z),"(at)")!= NULL ) { - if( CP == 0 ){ - retVal->at_uart_id = 0;//m_pDBC->getUartIdByDevice(); - } else { - retVal->at_uart_id_aux = 0; - }; - CP++; - }; - if (CP == 2) break; - }; - delete( CZ ); - }; - break; - }; - break; - }; - case CFG_STATUS:{ - //LOG("STAT"); - if ( strstr( tB, "power state") != NULL ) { - if ( strstr(CA->at(1),"on") ) { - retVal->power[0] = 'E'; - retVal->power[1]=(char)0; - } else { - retVal->power[0] = 'D'; - retVal->power[1]=(char)0; - }; - break; - }; - if ( strstr( tB, "state") != NULL ) { - if ( strstr(CA->at(1),"disabled") ) { - retVal->status[0]='D'; - retVal->status[1]=(char)0; - break; - }; - if ( strstr(CA->at(1),"registered") ) { - retVal->status[0]='R'; - retVal->status[1]=(char)0; - break; - }; - break; - }; - if ( strstr( tB, "access tech") != NULL ) { - trim(CA->at(1)); - strncpy(retVal->conmection_tech, CA->at(1), 5); - retVal->conmection_tech[5] = (char)0; - break; - }; - if ( strstr( tB, "signal quality") != NULL ) { - trim(CA->at(1)); - retVal->signal = atoi(CA->at(1)); - break; - }; - break; - }; - case CFG_MODES:{ - //LOG("MOD"); - //if ( strstr( CA->at(0), "current") != NULL ) { - // trim(CA->at(1)); - // strncpy( retVal->hw_manufac, CA->at(1), 64); - // retVal->hw_manufac[64] = (char)0; - // break; - //}; - break; - }; - case CFG_IP:{ - //LOG("IP"); - break; - }; - case CFG_EPS:{ - //LOG("EPS"); - break; - }; - case CFG_3GPP:{ - // LOG("3GPP"); - if ( strstr( tB, "imei") != NULL ) { - trim(CA->at(1)); - strncpy( retVal->imei, CA->at(1), 16); - retVal->imei[16] = (char)0; - break; - }; - if ( strstr( tB, "operator id") != NULL ) { - trim(CA->at(1)); - retVal->operator_id = atoi(CA->at(1)); - break; - }; - if ( strstr( tB, "registration") != NULL ) { - trim(CA->at(1)); - strncpy(retVal->registration, CA->at(1), 64); - retVal->registration[64] = (char)0; - break; - }; - break; - }; - case CFG_SIM:{ - // LOG("SIM"); - break; - }; - case CFG_BEARER:{ - // LOG("BEAR"); - break; - }; - //default: LOG("UNK"); - }; - } else { - LOG("No CA Parts!"); - }; - delete( CA ); - } else { - LOG("No CA!"); - }; - }; - fclose(fd); - }; - }; -#ifdef DEBUG - if ( retVal ) { - char ttB[256]; - sprintf(ttB, "Modem ID: [%d]", retVal->modem_id); - LOG(ttB); - sprintf(ttB, "Modem HW Manufac: [%s]", retVal->hw_manufac); - LOG(ttB); - sprintf(ttB, "Modem HW Model: [%s]", retVal->hw_model); - LOG(ttB); - sprintf(ttB, "Modem HW id: [%s]", retVal->hw_id); - LOG(ttB); - sprintf(ttB, "Modem Revision id: [%s]", retVal->hw_revision); - LOG(ttB); - sprintf(ttB, "Modem Revision Version: [%s]", retVal->hw_version); - LOG(ttB); - sprintf(ttB, "Modem DBus id: [%s]", retVal->dbus_dev_id); - LOG(ttB); - sprintf(ttB, "Modem IMEI: [%s]", retVal->imei); - LOG(ttB); - sprintf(ttB, "Modem Access Tech: [%s]", retVal->conmection_tech); - LOG(ttB); - sprintf(ttB, "Modem Operator: [%d]", retVal->operator_id); - LOG(ttB); - sprintf(ttB, "Modem Power: [%s]", retVal->power); - LOG(ttB); - sprintf(ttB, "Modem Registration: [%s]", retVal->registration); - LOG(ttB); - sprintf(ttB, "Modem Signal Power: [%d%%]", retVal->signal); - LOG(ttB); - sprintf(ttB, "Modem Status: [%s]", retVal->status); - LOG(ttB); }; -#endif - return retVal; + pthread_exit( NULL ); + return NULL; }; -DBC_INT_LIST *kernelModuleModemManager::enumerateModems(){ - DBC_INT_LIST *retVal = new DBC_INT_LIST(); - if( retVal != NULL ){ - char tB[1024]; - char expBuf[1024]; - FILE * fd = NULL; - sprintf( tB, "mmcli -L" ); - fd = popen( tB, "r"); - LOG("BEGIN"); - if( fd!= NULL ) { - while ( !feof( fd )) { - if( fgets( (CSTR)tB, 1024, fd ) ){ - trim( tB ); - strncpy( expBuf, tB, 1024 ); - LOG( expBuf ); - CArray *CA = explode( '/', expBuf ); - if ( CA ) { - int cMID = -1; - cMID = atoi( CA->at( CA->size()-1) ); - char tB[128]; - sprintf( tB ,"Detected Modem [%d]", cMID ); - retVal->push_back( cMID ); - LOG( tB ); - delete( CA ); - }; - }; - }; - fclose(fd); - }; - LOG("DONE"); +uint32_t kernelModuleModemManager::startCall( CSTR number ){ + MDM_CALL_DATA *DATA = ( MDM_CALL_DATA *) malloc( sizeof( MDM_CALL_DATA )); + if( DATA ) { + memset( DATA, 0 , sizeof( MDM_CALL_DATA )); + DATA->parent = this; + //strncpy( DATA->target, m_sMMData.call_target, 64 ); + int RetVal = pthread_create( &DATA->handler, NULL, callProc, ( void * ) DATA ); + if( RetVal == -1 ) { + LOG_ERROR("Cannot start Call Procedure."); + free( DATA ); + return 0; + }; + //Push Data into vector + m_vUserData.push_back( (MDM_USER_DATA*) DATA ); + } else { + LOG_ERROR("Cannot allocate Call data."); }; - return retVal; + return 0; +}; + +void kernelModuleModemManager::sendSMS( CSTR number, CSTR text ){ + +}; + +void kernelModuleModemManager::hangupCall( uint32_t call_id ){ + }; #undef LOG diff --git a/lib/App/common/kernelModuleModemManager.h b/lib/App/common/kernelModuleModemManager.h index f852a107..1d05bd6a 100644 --- a/lib/App/common/kernelModuleModemManager.h +++ b/lib/App/common/kernelModuleModemManager.h @@ -4,15 +4,56 @@ #include "kernelModule.h" #include "../../components/base/basicThread.h" -typedef std::vector VECTOR_MODEM_DATA; +//* + +typedef std::vector VECTOR_DBC_MODEM_DATA; + +class kernelModuleModemManager; + +typedef struct S_MDM_USER_DATA{ + uint8_t modem_id; + char target[64]; + kernelModuleModemManager * parent; + bool finished; + pthread_t handler; +}MDM_USER_DATA; + +typedef std::vector VECTOR_MDM_USER_DATA; + +typedef struct S_MDM_CALL_DATA: public MDM_USER_DATA{ + uint8_t waitTillHangup; +} MDM_CALL_DATA; + + +typedef struct S_MDM_SMS_DATA: public MDM_USER_DATA{ + CSTR text; +} MDM_SMS_DATA; +//*/ + +#define MDM_IDLE_WM 0x01 +#define MDM_INIT_CALL_WM 0x02 +#define MDM_MODEM_IS_BUSY_WM 0x03 +#define MDM_FETCH_NEXT_WM 0x04 +#define MDM_WAIT_WINDOW_WM 0x05 + +typedef std::vector VECTOR_MDM_PHONE_BOOK; class kernelModuleModemManager: public kernelModule, public basicThread { private: - //DBC_INT_LIST * m_pvModems; - VECTOR_MODEM_DATA m_vModemData; - DBC_MODEM_DATA * getModemInfo( uint32_t modem_id ); - DBC_INT_LIST * enumerateModems(); + DBC_MODEM_MANAGER_DATA m_sMMData; + VECTOR_MDM_USER_DATA m_vUserData; + VECTOR_MDM_PHONE_BOOK m_vPhoneBook; + bool moduleActive; + bool sessionStarted; + bool notificationSent; + //uint32_t m_nWindowCounterTO; + // MThread procs + static void * callProc( void * ); + static void * smsProc( void * ); protected: + uint32_t startCall( CSTR number ); + void sendSMS( CSTR number, CSTR text ); + void hangupCall( uint32_t call_id ); /* From Basic Thread */ virtual void m_fThreadProc( void ); public: diff --git a/lib/App/common/kernelPrototype.cpp b/lib/App/common/kernelPrototype.cpp index 9e7d8cf1..75a2b0e2 100644 --- a/lib/App/common/kernelPrototype.cpp +++ b/lib/App/common/kernelPrototype.cpp @@ -23,7 +23,46 @@ kernelPrototype *kernelPrototype::getInstance(){ }; kernelPrototype::kernelPrototype( config_setting_t *ptr ): basicEventHandler(), m_mWMProtector(){ - m_pLog = basicLogCollection::getInstance(); + m_pLog = basicLogCollection::getInstance(); +#ifdef LINUX_KERNEL_WATCHDOG + m_iWatchdogFd = -1; + struct sockaddr_un server_addr; + int connection_result; + + m_iWatchdogFd = socket( AF_UNIX, SOCK_STREAM, 0 ); + + server_addr.sun_family = AF_UNIX; + strcpy( server_addr.sun_path, "/srv/etc/watchdog" ); + + connection_result = connect( m_iWatchdogFd, (struct sockaddr *)&server_addr, sizeof(server_addr) ); + + if( connection_result == -1 ){ + LOG_ERROR("WD Connection Error"); + m_iWatchdogFd = -1; + }; + if ( m_iWatchdogFd < 0 ) { + LOG_ERROR("Watchdog device can not be opened!"); + } else { + //int timeout = 45; + //xioctl( m_iWatchdogFd, WDIOC_GETTIMEOUT, &timeout); + //LOG("Watchdog turned On!"); + //char tB[64]; + //sprintf(tB, "The timeout was set to %d seconds", timeout); + //LOG( tB) ; + //xioctl( m_iWatchdogFd, WDIOC_GETPRETIMEOUT, &timeout); + //sprintf(tB, "The pretimeout was is %d seconds", timeout); + //LOG( tB) ; + //struct watchdog_info ident; + //xioctl( m_iWatchdogFd, WDIOC_GETSUPPORT, &ident); + //LOG( (char *) ident.identity ); + //LOG( ident.firmware_version ); + char tB[64]; + sprintf( tB, "%lld\n", gettid() ); + LOG("PID SENT TO WD!"); + //ToDo: Make Error Handling here! + write( m_iWatchdogFd, &tB, strlen( tB ) ); + }; +#endif m_pEventHeap = basicEventHeap::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( ptr, "KERNEL" ); #ifdef USE_MYSQL @@ -40,6 +79,11 @@ kernelPrototype::kernelPrototype( config_setting_t *ptr ): basicEventHandler(), }; kernelPrototype::~kernelPrototype(){ +#ifdef LINUX_KERNEL_WATCHDOG + LOG("Closing WD!"); + if( !( m_iWatchdogFd < 0 ) ) close( m_iWatchdogFd ); + LOG("WatchDog turned Off!"); +#endif }; void kernelPrototype::setWM( uint8_t mode ){ diff --git a/lib/App/common/kernelPrototype.h b/lib/App/common/kernelPrototype.h index 667504e6..8a507455 100644 --- a/lib/App/common/kernelPrototype.h +++ b/lib/App/common/kernelPrototype.h @@ -15,6 +15,13 @@ #define WM_KERNEL_PROTOTYPE_INIT 0 +#ifdef __linux__ + #define LINUX_KERNEL_WATCHDOG + #include + #include + #include +#endif + #define EXIT_CODE_KERNEL_CLEAR 0 #define EXIT_CODE_RELOAD_SERVICES 1 @@ -32,6 +39,9 @@ class kernelPrototype: public basicThread, public basicEventHandler { uint8_t m_bWM; static kernelPrototype * _instance; protected: +#ifdef LINUX_KERNEL_WATCHDOG + int m_iWatchdogFd; +#endif /* basic */ basicLogCollection * m_pLog; basicEventHeap * m_pEventHeap; diff --git a/lib/App/mojo/mojoBackend.cpp b/lib/App/mojo/mojoBackend.cpp index f577146f..a07a526f 100644 --- a/lib/App/mojo/mojoBackend.cpp +++ b/lib/App/mojo/mojoBackend.cpp @@ -158,6 +158,10 @@ int main( int argc, char *argv[] ){ #endif ; + if( !isProcessUniq( argv[0] ) ){ + printf( "The Process [%s] Already is Runing\n", argv[0] ); + return 33; + }; // Reading Config if( argc > 1 ){ @@ -496,9 +500,9 @@ int main( int argc, char *argv[] ){ LOG( "DBC Deleted" ); }; - unlink(gsCFG_PID_FILE); + unlink( gsCFG_PID_FILE ); LOG( "PID File Deleted" ); - delete( m_pLog ); + delete( m_pLog ); return 0; }; diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index 90adfb3b..edaac002 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -109,6 +109,12 @@ mojoKernel::~mojoKernel(){ //m_pDBC = NULL; }; delete( m_pHardwareManager ); + //while(1){ + // char tB[200]; + // sprintf( tB, "[%d] Seconds Till System Restart", timeout ); + // LOG( "BopBop!" ); + // SLEEP(1000); + //}; _instance = NULL; }; @@ -318,7 +324,27 @@ void mojoKernel::m_fThreadProc( void ){ //Each Second if( m_tTimeTSeconds != tTimeTSeconds ){ tTimeTSeconds = m_tTimeTSeconds; - +#ifdef LINUX_KERNEL_WATCHDOG + if( m_iWatchdogFd > -1 ) { + char tB[64]; + char ttB[128]; + sprintf( tB, "2000\n" ); + //int ret = xioctl( m_iWatchdogFd, WDIOC_KEEPALIVE, NULL ); + //if( ret < 0 ){ + // char tB[200]; + // sprintf( tB, "Could not pat watchdog: %s\n", strerror(errno)); + // LOG_ERROR( tB ); + //}; + //LOG("Write WD!"); + //ToDo: Make Error Handling here! + write( m_iWatchdogFd, &tB, strlen( tB ) ); + //LOG("Read WD!"); + //read( m_iWatchdogFd, &tB, 64 ); + //LOG("Done WD!"); + //sprintf( ttB, "Client: I recieved %s from server!", tB ); + //LOG( ttB ); + }; +#endif m_bDebugMode = m_pDBC->getAppDebugMode(); validateFilters(); @@ -326,7 +352,13 @@ void mojoKernel::m_fThreadProc( void ){ validateSession(); validateReports(); - + //?? + if( !m_pHardwareManager->validate() ){ + LOG("Reload Mojo Node Due to Memory Consumption!"); + m_bThreadStatus = TH_ENDED; + setExitCause( EXIT_CODE_KERNEL_CLEAR ); + continue; + }; m_pHardwareManager->updateSysInfo( &m_sSysInfo ); DBC_PUSHED_CMD * tCMD = m_pDBC->getNodePushedCMD( m_nServerNodeID ); //Kernel if( tCMD != NULL ) { diff --git a/lib/components/base/basicFunctionality.cpp b/lib/components/base/basicFunctionality.cpp index 0977c85a..2ab47225 100644 --- a/lib/components/base/basicFunctionality.cpp +++ b/lib/components/base/basicFunctionality.cpp @@ -881,9 +881,68 @@ size_t escape_string( char *to, size_t to_length, const char *from, size_t lengt }; +/* Read characters from 'fd' until a newline is encountered. If a newline + character is not encountered in the first (n - 1) bytes, then the excess + characters are discarded. The returned string placed in 'buf' is + null-terminated and includes the newline character if it was read in the + first (n - 1) bytes. The function return value is the number of bytes + placed in buffer (which includes the newline character if encountered, + but excludes the terminating null byte). */ + +ssize_t readLine(int fd, char *buffer, size_t n) { + ssize_t numRead; /* # of bytes fetched by last read() */ + size_t totRead; /* Total bytes read so far */ + char *buf; + char ch; + + if (n <= 0 || buffer == NULL) { + errno = EINVAL; + return -1; + } + + buf = buffer; /* No pointer arithmetic on "void *" */ + + totRead = 0; + for (;;) { + numRead = read(fd, &ch, 1); + + if (numRead == -1) { + if (errno == EINTR) /* Interrupted --> restart read() */ + continue; + else + return -1; /* Some other error */ + + } else if (numRead == 0) { /* EOF */ + if (totRead == 0) /* No bytes read; return 0 */ + return 0; + else /* Some bytes read; add '\0' */ + break; + + } else { /* 'numRead' must be 1 if we get here */ + if (totRead < n - 1) { /* Discard > (n - 1) bytes */ + totRead++; + *buf++ = ch; + } + + if (ch == '\n') + break; + } + } + + *buf = '\0'; + return totRead; +}; + + localIpAddresses g_vLocalIpAdressess; bool g_bAdressesAreDiscovered = false; #ifdef __linux__ + +bool isProcessUniq( CSTR pName ){ + return true; +}; + + #include localIpAddresses *getLocalIP(){ // Check for Filter LO and TUN Devices! @@ -929,7 +988,7 @@ localIpAddresses *getLocalIP(){ return &g_vLocalIpAdressess; }; -#else // linux +#else // !linux //https://stackoverflow.com/questions/5213629/how-to-enumerate-all-available-network-interfaces #include #pragma comment(lib, "IPHLPAPI.lib") diff --git a/lib/components/base/basicFunctionality.h b/lib/components/base/basicFunctionality.h index 8a41a1aa..571a3158 100644 --- a/lib/components/base/basicFunctionality.h +++ b/lib/components/base/basicFunctionality.h @@ -69,6 +69,8 @@ CSTR strReplace( CSTR orig, CSTR rep, CSTR with ); int checkCmdExists( CSTR cmd ); +ssize_t readLine(int fd, char *buffer, size_t n ); + //#define EPOCH_DIFF 11644473600LL #define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000 @@ -87,6 +89,7 @@ bool correctSoftTime( int diff ); CSTR getTimeStamp(void); #ifdef __linux__ + bool isProcessUniq( CSTR pName ); inline PF_TIMESTAMP * getPFTS( PF_TIMESTAMP * ptr = NULL ){ struct timeval tData; struct timezone tTZ; diff --git a/lib/components/base/basicThread.cpp b/lib/components/base/basicThread.cpp index 0bf5a800..295199b6 100644 --- a/lib/components/base/basicThread.cpp +++ b/lib/components/base/basicThread.cpp @@ -1,21 +1,28 @@ #include "basicThread.h" +#ifndef BTH_NO_LOGGING + #define LOG( X ) m_pLog->Log( "BasicThread", this, X ) -#define LOG_ERROR( X ) m_pLog->Log( "BasicThread", this, _LOG_ERROR, X ) -//Thread Logging -#define SLOG( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, X ) -#define SLOG_ERROR( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, _LOG_ERROR, X ) + #define LOG_ERROR( X ) m_pLog->Log( "BasicThread", this, _LOG_ERROR, X ) + //Thread Logging + #define SLOG( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, X ) + #define SLOG_ERROR( X ) mThis->m_pLog->Log( "BasicThreadS", mThis, _LOG_ERROR, X ) +#endif bool basicThread::init( void ){ -#ifdef DEBUG_EXTRA - LOG("Init Single Thread"); +#ifndef BTH_NO_LOGGING + #ifdef DEBUG_EXTRA + LOG("Init Single Thread"); + #endif #endif if( m_bThreadStatus == TH_PRE_INIT ){ m_bThreadStatus = TH_ARMED; #if defined( __linux__ ) || defined(WIN_PTHREAD) int RetVal = pthread_create( &m_sThreadHandler, NULL, m_fThreadEntryPoint, ( void * ) this ); if( RetVal == -1 ) { +#ifndef BTH_NO_LOGGING LOG_ERROR("Thread Creation Failed."); +#endif return false; }; #else @@ -29,7 +36,9 @@ bool basicThread::init( void ){ //TODO: Add thread creation check #endif } else { +#ifndef BTH_NO_LOGGING LOG_ERROR("Thread object not ready to be created."); +#endif return false; }; return true; @@ -52,7 +61,9 @@ void basicThread::m_fThreadProc( void ){ if( mThis->m_bThreadStatus == TH_ARMED ) { mThis->m_fThreadProc(); } else { +#ifndef BTH_NO_LOGGING SLOG_ERROR("Thread is not ARMED!"); +#endif }; mThis->m_bThreadStatus = TH_EXIT; #ifdef __linux__ @@ -98,7 +109,9 @@ void basicThread::abort(){ pthread_join( m_sThreadHandler, NULL); }; -#undef LOG -#undef LOG_ERROR -#undef SLOG -#undef SLOG_ERROR +#ifndef BTH_NO_LOGGING + #undef LOG + #undef LOG_ERROR + #undef SLOG + #undef SLOG_ERROR +#endif diff --git a/lib/components/base/basicThread.h b/lib/components/base/basicThread.h index d8ea0dfe..af07014e 100644 --- a/lib/components/base/basicThread.h +++ b/lib/components/base/basicThread.h @@ -1,6 +1,9 @@ #ifndef __BASICTHREAD_H_INCL__ #define __BASICTHREAD_H_INCL__ + +#define BTH_NO_LOGGING + #ifndef __linux__ #ifndef WIN_PTHREAD //#define WIN_PTHREAD @@ -43,7 +46,9 @@ typedef enum thredStatus{ class basicThread { private: +#ifndef BTH_NO_LOGGING basicLogCollection *m_pLog; +#endif #if defined( __linux__ ) || defined(WIN_PTHREAD) pthread_t m_sThreadHandler; static void *m_fThreadEntryPoint(void *); @@ -78,7 +83,9 @@ class basicThread { }; // Constructor implementation inline basicThread::basicThread(){ +#ifndef BTH_NO_LOGGING m_pLog = basicLogCollection::getInstance(); +#endif m_bThreadStatus = TH_PRE_INIT; };