/******************************************************************* * * DESCRIPTION: ARGUS Kernel source * * AUTHOR: gagua * * HISTORY: * * DATE:5/29/2019 * *******************************************************************/ #include "rgooseKernel.h" #include #define LOG( X ) m_pLog->Log( "KRNL", this, X ) #ifdef DEBUG #define LOG_ERROR( X ) {\ char _tBfrErrMSG2w34[256];\ sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG2w34 );\ } #else #define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X ); #endif argusKernel *argusKernel::_instance = NULL; /** * Returns the single instance of the object. */ argusKernel* argusKernel::getInstance( config_setting_t *Settings ) { // check if the instance has been created yet if( 0 == _instance ){ // if not, then create it _instance = new argusKernel( Settings ); }; // return the single instance return _instance; }; argusKernel::argusKernel( config_setting_t *Settings ):basicThread(){ m_bExitCause = 0; m_nServerID = 0; m_nSiteID = 0; m_pDBC = NULL; m_tTimeTSeconds = time( NULL ); m_pLog = basicLogCollection::getInstance(); m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" ); m_pServiceAnnouncer = NULL; m_pEventHeap = NULL; m_pOnvifMediaObserver = NULL; m_pNodeFactory = NULL; m_pModuleFactory = NULL; }; argusKernel::~argusKernel(){ #ifdef DEBUG LOG("Destructor"); #endif WaitFinish(true); if( m_pDBC != NULL ) m_pDBC->unregisterServer( m_nSiteID, m_nServerNodeID ); #ifdef DEBUG LOG("Deleting Components"); #endif if( m_pStreamManager != NULL ) delete( m_pStreamManager ); //Last DBC if( m_pDBC != NULL ) delete( m_pDBC ); #ifdef DEBUG LOG("Destructor Finished"); #endif }; bool argusKernel::create( void ){ char tBuffer[256]; if( m_pSettings != NULL ) { config_setting_t * pSetting; int tVal; const char * tBuf; //Connect( CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port = 3300 ); LOG("Getting Settings ..."); if( !config_setting_lookup_string( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){ char * tVar; char * tTS; tTS = getTStr(NULL); tVar = MD5String(tTS); free(tTS); sprintf( m_sIdentity, tVar ); pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING ); config_setting_set_string( pSetting, tVar ); free( tVar ); basicSettingsProvider::getInstance()->compromise(); LOG("Server Identity not defined - generating new!"); } else { sprintf( m_sIdentity, tBuf ); sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf ); LOG( tBuffer ); }; if( !config_setting_lookup_int( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, &tVal ) ){ tVal = m_nSiteID = 1; pSetting = config_setting_add( m_pSettings, ARGUS_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT ); config_setting_set_int( pSetting, tVal ); basicSettingsProvider::getInstance()->compromise(); LOG("Site ID not defined - using [1] as number!"); } else { m_nSiteID = tVal; sprintf( tBuffer, "Site ID Read From Settings: %d", m_nSiteID ); LOG( tBuffer ); }; } else { LOG_ERROR("Failed Read Config Settings!"); return false; }; if( m_pDBC == NULL ) { LOG("Explicitly Create DBC Object"); m_pDBC = argusDBC::getInstance( m_pSettings ); }; if( m_pDBC == NULL ){ LOG_ERROR("DBC Object Creation Failure!"); return false; }; if(!m_pDBC->connect()){ LOG_ERROR("DBC Connection Failure!"); SLEEP(5000); //ToDO: Set Restart Kernel! return false; }; #ifdef DEBUG LOG( "Settings Read Done" ); #endif m_pDBC->registerServer( m_nSiteID, m_sIdentity ); m_nServerID = m_pDBC->getServerID( m_sIdentity ); m_nServerNodeID = m_pDBC->getServerNode( m_nServerID ); //Init Subcomponents m_pHardwareManager = argusHardwareManager::getInstance(); if( m_pHardwareManager!= NULL ) { m_pHardwareManager->validate(); } else { LOG_ERROR("Failure to get HardwareManager Instance!"); return false; }; m_pServiceAnnouncer = new ssdpAnnouncer( m_pSettings ); if( basicSettingsProvider::getInstance()->isCompromised() ) { basicSettingsProvider::getInstance()->validate(); }; m_pEventHeap = basicEventHeap::getInstance(); m_pOnvifMediaObserver = onvifMediaObserver::getInstance(); m_pNodeFactory = argusNodeFactory::getInstance(); m_pModuleFactory = argusModuleFactory::getInstance(); //enumerateHardware(); //m_pNodeFactory->setDBC( m_pDBC ); m_pNodeFactory->loadNodes( m_nSiteID ); //m_pModuleFactory->setDBC( m_pDBC ); //m_pDBC->getIPCamRecords( m_vCamRecords ); m_pModuleFactory->loadModules( m_nSiteID ); #ifdef DEBUG LOG( "Start SSDP Beacon" ); #endif if( m_pServiceAnnouncer != NULL ) { if( !m_pServiceAnnouncer->init() ){ delete(m_pServiceAnnouncer); m_pServiceAnnouncer = NULL; }; }; m_pStreamManager = NULL;//mediaNginxManager::getInstance(); //if( m_pStreamManager != NULL ) { // m_pStreamManager->attachStream("rtsp://gagua:4GetMyID@192.168.0.104:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1", true, 12, "12918" ); //}; /* #ifdef DEBUG LOG("Init IP Cameras"); #endif if( m_vCamRecords.size() ) { ARGUS_Camera *tPtr = NULL; for( int i=0; i < m_vCamRecords.size(); i++ ) { tPtr = new ARGUS_Camera(); if( tPtr != NULL ) { if( tPtr->create( m_vCamRecords.at(i)->ip, m_vCamRecords.at(i)->username, m_vCamRecords.at(i)->password ) ){ #ifdef DEBUG LOG("Camera Created!"); #endif m_vCameras.push_back( tPtr ); tPtr = NULL; } else { LOG_ERROR("Camera Creation Failure!"); delete( tPtr ); tPtr = NULL; }; }; }; }; */ #ifdef DEBUG LOG("Start OnVIF Discovery"); #endif if( m_pOnvifMediaObserver!= NULL ) { m_pOnvifMediaObserver->startDiscovery(); m_bDiscoveryStarted = true; }; #ifdef DEBUG LOG( "Kernel Trhead is about to be Created" ); #endif if( init() == false ){ LOG_ERROR("Error in starting Kernel Thread!"); return false; }; #ifdef DEBUG LOG( "Kernel Thread Created" ); #endif /* m_pServletWS = new servletWS(); if( m_pServletWS != NULL ) { LOG("ServletWS Created"); if(m_pServletWS->create(8080)){ LOG("ServletWS Started"); //if( ! m_pServletWS->attachChannel("rtsp://gagua:4GetMyID@192.168.0.109:554/Streaming/Channels/101?transportmode=unicast&profile=Profile_1", false ) ){ // LOG("Error Adding Channel!"); //} else { // LOG("Channel Attached"); //}; }; }; */ #ifdef DEBUG sprintf( tBuffer, "Server is registered as id[%d]", m_nServerID ); LOG( tBuffer ); #endif return true; }; void argusKernel::WaitFinish( bool Force ){ if( Force ){ #ifdef DEBUG LOG( "Thread End Forced" ); #endif } else { // 3 seconds sleep to settle down processes SLEEP(3000); }; waitThreadEnd( Force ); #ifdef DEBUG LOG( "Thread is Detached" ); #endif }; /* Main Kernel Thread Procedure */ void argusKernel::m_fThreadProc( void ){ char Buffer[CTEXT]; time_t tTimeTSeconds; int lastTSS = 0; int lastTMS = 0; EventRecordDBC * tEvtPtr; pid_t pid = gettid(); char pidBuf[16]; sprintf(pidBuf,"pid:%d", pid); LOG(pidBuf); m_bThreadStatus = TH_WORKING; while( m_bThreadStatus < TH_ENDED ){ m_tTimeTSeconds = time( NULL ); // Network IP OnVif Discovery //* if( m_pOnvifMediaObserver!= NULL ){ if( !m_bDiscoveryStarted ) { if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==0){ #ifdef DEBUG LOG("Start OnVIF Discovery"); #endif m_pOnvifMediaObserver->startDiscovery(); m_bDiscoveryStarted = true; }; } else { //* if((m_tTimeTSeconds%ONVIF_DISCOVERY_INTERVAL)==ONVIF_DISCOVERY_RESULT_TIME){ #ifdef DEBUG LOG("Validate IP Cameras"); #endif validateCameras(); m_bDiscoveryStarted = false; }; //*/ }; }; //*/ //* // Event Processing Loop if( !m_pDBC->getLastEvents( m_vEvtRecords, lastTSS, lastTMS ) ){ // Error In DB Access m_bThreadStatus = TH_ENDED; m_bExitCause = 1; continue; }; /* if( m_vEvtRecords.size() > 0 ){ validateEvents( lastTSS, lastTMS ); }; */ //*/ if( m_tTimeTSeconds != tTimeTSeconds ){ // Each Second Process tTimeTSeconds = m_tTimeTSeconds; if( m_pModuleFactory != NULL ) { m_pModuleFactory->validate(); }; //if( m_pNodeFactory != NULL ) { //m_pNodeFactory->validate(); //}; }; SLEEP( SLEEP_KERNEL_INTERVAL ); }; }; int argusKernel::isCameraListed( CSTR uid ){ if( m_vCamRecords.size() ){ for( int index = 0; index < m_vCamRecords.size(); index++ ){ if(strstr(m_vCamRecords.at(index)->uuid,uid)!=NULL){ m_vCamRecords.at(index)->online = true; return index; }; }; } else { LOG("No Registered Cameras!"); }; return -1; }; void argusKernel::validateCameras( void ){ vectorWoWoResults *DiscoverResults = m_pOnvifMediaObserver->getResults(); if( DiscoverResults->size() > 0 ) { for (int index = 0; index < DiscoverResults->size(); index++) { char tBuffer[256]; sprintf( tBuffer,"Found Camwera [%s] %s", DiscoverResults->at(index)->XAddrs, DiscoverResults->at(index)->RefID ); LOG( tBuffer ); uint32_t cid = m_pDBC->getCamId( DiscoverResults->at(index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID ); if( cid ) { sprintf( tBuffer, "Camera is Listed as %d", cid ); LOG( tBuffer ); //if( strstr(DiscoverResults->at(index)->XAddrs,m_vCamRecords.at(cid)->ip)==NULL ) { // LOG( DiscoverResults->at(index)->XAddrs ); m_pDBC->updateIPCam( m_nSiteID, cid, DiscoverResults->at(index)->XAddrs );// DiscoverResults->at(index)); //}; } else { // Uncknown camera - no checking LOG("Camera is Not Listed"); m_pDBC->addIPCam( m_nSiteID, DiscoverResults->at(index) ); }; }; } else { LOG("No Online IP Cameras in Network"); }; }; /* void ARGUS_Kernel::validateEvents( int &lastTSS, int &lastTMS ){ EventRecordDBC *tPtr = NULL; basicEvent *tEvt = NULL; if(m_vEvtRecords.size()) { lastTSS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_s; lastTMS = m_vEvtRecords.at(m_vEvtRecords.size()-1)->ts_ms; #ifndef ONE_EVENT_PER_LOOP while(m_vEvtRecords.size()) { #endif tPtr = *m_vEvtRecords.begin(); if( tPtr != NULL ) { if( tEvt != NULL ) { m_pEventHeap->broadcast( tEvt ); //free( tEvt ); }; free( tPtr ); }; m_vEvtRecords.erase(m_vEvtRecords.begin()); #ifndef ONE_EVENT_PER_LOOP }; #endif }; }; */ bool argusKernel::signalEvent( void ){ if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){ }; return true; }; int argusKernel::getNetNodes( ARGUS_NODE_TYPE type, argusNetNode **&RA ){ RA = NULL; int cnt = 0; if( m_pNodeFactory != NULL ){ cnt = m_pNodeFactory->getNodeCount( type ); if( cnt ) { RA = (argusNetNode **) calloc( cnt, sizeof( argusNetNode * ) ); m_pNodeFactory->fetchNodes(type, RA ); }; }; return cnt; }; argusNetNode *argusKernel::getNetNode( int id ){ return NULL; }; int argusKernel::getModules( ARGUS_MODULE_TYPE type, argusKernelModule **&RA ){ return 0; }; argusKernelModule *argusKernel::getModule( int id ){ return NULL; }; #undef LOG #undef LOG_ERROR