diff --git a/Projects/argus_server/argus_server.vpj b/Projects/argus_server/argus_server.vpj index 3de08d04..944d29bd 100644 --- a/Projects/argus_server/argus_server.vpj +++ b/Projects/argus_server/argus_server.vpj @@ -261,7 +261,15 @@ Name="Source Files" Filters="*.c;*.C;*.cc;*.cpp;*.cp;*.cxx;*.c++;*.prg;*.pas;*.dpr;*.asm;*.s;*.bas;*.java;*.cs;*.sc;*.scala;*.e;*.cob;*.html;*.rc;*.tcl;*.py;*.pl;*.d;*.m;*.mm;*.go;*.groovy;*.gsh" GUID="{03BEF1E1-D7E3-4088-A72A-934FCD9D6E12}"> + + + + + + + + Log( "main", 0, X ) +#define LOG_ERROR( X ) m_pLog->Log( "main", 0, _LOG_ERROR, X ) + +argusKernel * Kernel = NULL; +basicLogCollection * m_pLog = NULL; +networkServer * NetworkManager = 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 + + + + +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; + CSTR FLE_NAME; + CSTR DCF_NAME; + CSTR SSP_NAME; + objectFileLoggingService * FLE = NULL; + objectConsoleLoggingService * CLE = NULL; + // default init + FLE_NAME = (CSTR) DEFAULT_SERVER_LOG_FILE; + DCF_NAME = (CSTR) DEFAULT_SERVER_CONFIG_PATH; + SSP_NAME = (CSTR) DEFAULT_SERVER_SOCKET_PATH; + 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 + ; + + + // Reading Config + if( argc > 1 ){ + for( int i = 0; 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 ARGUS Server\n"); + printf( "Software version <%s>\n", ARGUS_BUILD_STRING ); + 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 LRF Server ver. <%s>\n", ARGUS_BUILD_STRING ); + 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 = argusKernel::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( SIGTERM /* 15 */, signal_SIGKILL_handler ); + old_kill_handler_9 = signal( SIGKILL /* 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 ++++" ); + }; + } while( m_bKernelExitCode > 0 ); + + delete( NetworkManager ); + //LOG( "++++ CDC DEL ++++" ); + unlink( gsCFG_PID_FILE ); + LOG( "PID File Deleted" ); + if( FLE!= NULL ) delete( FLE ); + if( CLE!= NULL ) delete( CLE ); + delete( m_pLog ); + return(0); +} + diff --git a/lib/App/argus_server/ArgusBackend.h b/lib/App/argus_server/ArgusBackend.h new file mode 100644 index 00000000..9ec5fdc9 --- /dev/null +++ b/lib/App/argus_server/ArgusBackend.h @@ -0,0 +1,46 @@ +/******************************************************************* +* +* DESCRIPTION: Argus project main header file +* +* AUTHOR: Gagua +* +* HISTORY: +* +* DATE:2/25/2024 +* +*******************************************************************/ +#ifndef __ARGUS_BACKEND_MAIN_HEADER__ +#define __ARGUS_BACKEND_MAIN_HEADER__ + +/** include files **/ + +#include +#include +#include + +#include "../../components/base/basicSettings.h" +#include "../../components/base/basicLogCollection.h" +#include "../../components/base/basicMemoryOperation.h" +#include "../../components/object/objectFileLoggingService.h" +#include "../../components/object/objectConsoleLoggingService.h" +#include "ArgusKernel.h" + +/** local definitions **/ +//http://xmsecu.com:8080/ocx/NewActive.exe +/* default settings */ + +/** external functions **/ + +/** external data **/ + +/** internal functions **/ + +/** public data **/ + +/** private data **/ + +/** public functions **/ + +/** private functions **/ + +#endif diff --git a/lib/App/argus_server/ArgusKernel.cpp b/lib/App/argus_server/ArgusKernel.cpp new file mode 100644 index 00000000..45c2717f --- /dev/null +++ b/lib/App/argus_server/ArgusKernel.cpp @@ -0,0 +1,637 @@ +/******************************************************************* +* +* DESCRIPTION: ARGUS Kernel source +* +* AUTHOR: gagua +* +* HISTORY: +* +* DATE:2/25/2024 +* +*******************************************************************/ +#include "ArgusKernel.h" +#include + +#define LOG( X ) m_pLog->Log( "KRNL", this, X ) +#define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X ) + + +ARGUS_Kernel::ARGUS_Kernel( 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_pEventHeap = basicEventHeap::getInstance(); + m_pOnvifMediaObserver = onvifMediaObserver::getInstance(); + m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" ); + m_pServiceAnnouncer = new ssdpAnnouncer( m_pSettings ); + if( basicSettingsProvider::getInstance()->isCompromised() ) { + basicSettingsProvider::getInstance()->validate(); + }; + m_pNodeFactory = argusNodeFactory::getInstance(); + m_pModuleFactory = argusModuleFactory::getInstance(); +}; + +ARGUS_Kernel::~ARGUS_Kernel(){ + + m_pDBC->unregisterServer( m_nSiteID, m_nServerNodeID ); +#ifdef DEBUG + LOG("Destructor"); +#endif + WaitFinish(true); +#ifdef DEBUG + LOG("Deleting Components"); +#endif + + delete( m_pStreamManager ); + +#ifdef DEBUG + LOG("Destructor Finished"); +#endif +}; + +bool ARGUS_Kernel::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 ); + }; +/* +#ifdef __EU_PROJECT__ + if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_SAFE_SHORE, &tVal ) ){ + tVal = m_bSafeShoreSupport = true; + pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_SAFE_SHORE, CONFIG_TYPE_BOOL ); + config_setting_set_bool( pSetting, tVal ); + m_pSettingsProvider->compromise(); + LOG("Safe-Shore support not defined"); + } else { + m_bSafeShoreSupport = (bool)tVal; + sprintf( tBuffer, "Safe-Shore support Read From Settings: %d", tVal ); + LOG( tBuffer ); + }; +#endif + if( !config_setting_lookup_bool( m_pSettings, KNL_SETTINGS_DEFENDER, &tVal ) ){ + tVal = m_bDefenderSupport = true; + pSetting = config_setting_add( m_pSettings, KNL_SETTINGS_DEFENDER, CONFIG_TYPE_BOOL ); + config_setting_set_bool( pSetting, tVal ); + m_pSettingsProvider->compromise(); + LOG("Defender support not defined"); + } else { + m_bDefenderSupport = (bool)tVal; + sprintf( tBuffer, "Defender support Read From Settings: %d", tVal ); + LOG( tBuffer ); + }; + + if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, &tVal )){ + tVal = m_nWidth = 1000; + pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_WIDTH, CONFIG_TYPE_INT); + config_setting_set_int(pSetting, tVal ); + m_pSettingsProvider->compromise(); + LOG("No Detector Width"); + } else { + m_nWidth = tVal; + sprintf( tBuffer, "Detector Width Read From Settings: %d", tVal ); + LOG( tBuffer ); + }; + if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, &tVal )){ + tVal = m_nKernelFilter = 0x000000ff; + pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_FILTER_KERNELS, CONFIG_TYPE_INT); + config_setting_set_int(pSetting, tVal ); + m_pSettingsProvider->compromise(); + LOG("No Fltered Kernels Defined - All LRF Will Work"); + } else { + m_nKernelFilter = tVal; + sprintf( tBuffer, "Filter Kernels Read From Settings: %d", tVal ); + LOG( tBuffer ); + }; + if (!config_setting_lookup_int(m_pSettings, KNL_SETTINGS_DETECTOR_SST, &tVal )){ + tVal = m_nSST = 0; + pSetting = config_setting_add(m_pSettings, KNL_SETTINGS_DETECTOR_SST, CONFIG_TYPE_INT); + config_setting_set_int(pSetting, tVal ); + m_pSettingsProvider->compromise(); + LOG("No Detector SST"); + } else { + m_nSST = tVal; + sprintf( tBuffer, "Detector SST Read From Settings: %d", tVal ); + LOG( tBuffer ); + }; + if( m_pSettingsProvider->isCompromised() ) { + LOG("Save Settings"); + m_pSettingsProvider->validate(); + }; +*/ + } else { + LOG_ERROR("Failed Read Config Settings!"); + return false; + }; + + if( m_pDBC == NULL ) { + LOG("Explicitly Create DBC Object"); + m_pDBC = new ARGUS_DBC(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 ); + 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 = mediaNginxManager::getInstance(); + if( m_pStreamManager != NULL ) { + m_pStreamManager->attachStream("rtsp://gagua:4GetMyID@192.168.0.109: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; +}; +int xioctl(int fh, int request, void *arg){ + int r; + do { + r = ioctl( fh, request, arg ); + } while (-1 == r && EINTR == errno); + return r; +}; + +#define CLEAR(x) memset(&(x), 0, sizeof(x)) + +#include +#include + +void ARGUS_Kernel::enumerateHardware( void ){ + DIR * dFD; + struct dirent * pDR = NULL; + char tBuffer[CTEXT]; + struct ifaddrs * ifaddr = NULL; + struct ifaddrs * ifa; + + /* Add Network Interfaces */ + if( -1 == getifaddrs(&ifaddr)) { + } else { + for( ifa = ifaddr; ifa; ifa = ifa->ifa_next ) { + if (ifa->ifa_addr->sa_family != AF_INET) continue; + char ip[16]; + struct sockaddr_in *inn; + unsigned char A,B,C,D; + inn = (struct sockaddr_in *)ifa->ifa_addr; + D = ( inn->sin_addr.s_addr >> 24) & 0xFF; + C = ( inn->sin_addr.s_addr >> 16) & 0xFF; + B = ( inn->sin_addr.s_addr >> 8) & 0xFF; + A = ( inn->sin_addr.s_addr >> 0) & 0xFF; + sprintf( ip, "%d.%d.%d.%d", A, B, C, D ); + sprintf( tBuffer, "Network Interface: %s [%s]", ifa->ifa_name, ip ); + LOG(tBuffer); + m_pDBC->addNodeSocket( m_nServerNodeID, ARGUS_NODE_SOCK_TYPE_NETWORK, ifa->ifa_name, ip ); + }; + freeifaddrs( ifaddr ); + }; + /* Add devices from DEV */ + dFD = opendir("/dev"); + if( dFD != NULL ) { + while( ( pDR = readdir( dFD ))!= NULL ){ + if( strcasestr( pDR->d_name, "video" ) == pDR->d_name ) { + /* Enumerate Video Capture */ + sprintf( tBuffer, "Video Source: %s", pDR->d_name ); + LOG( tBuffer ); + struct v4l2_capability cap; + //struct v4l2_cropcap cropcap; + //struct v4l2_crop crop; + struct v4l2_format fmt; + char dev_name[CTEXT]; + int fd = -1; + struct stat st; + + sprintf( dev_name, "/dev/%s", pDR->d_name ); + + if (-1 == stat( dev_name, &st )) { + sprintf( tBuffer, "Cannot identify '%s': %d, %s\\n", dev_name, errno, strerror(errno)); + LOG_ERROR( tBuffer ); + continue; + }; + if (!S_ISCHR(st.st_mode)) { + sprintf( tBuffer, "%s is no devicen", dev_name); + LOG_ERROR( tBuffer ); + continue; + }; + fd = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0); + if (-1 == fd) { + sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno)); + LOG_ERROR( tBuffer ); + continue; + } else { + if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) { + if (EINVAL == errno) { + sprintf( tBuffer, "%s is no V4L2 device", dev_name ); + LOG_ERROR( tBuffer ); + } else { + LOG_ERROR( "VIDIOC_QUERYCAP" ); + }; + close(fd); + continue; + }; + if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { + sprintf(tBuffer, "%s is no video capture device\\n", dev_name); + LOG_ERROR( tBuffer ); + close(fd); + continue; + }; + CLEAR(fmt); + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + /* Preserve original settings as set by v4l2-ctl for example */ + if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt)){ + LOG_ERROR("VIDIOC_G_FMT"); + } else { + sprintf( tBuffer, "Grabber[%dx%d]",fmt.fmt.pix.width,fmt.fmt.pix.height); + LOG( tBuffer ); + m_pDBC->addNodeSocket( m_nServerNodeID, ARGUS_NODE_SOCK_TYPE_VIDEO_GRABBER, dev_name, tBuffer ); + }; + close(fd); + continue; + }; + }; + if( strcasestr( pDR->d_name, "ttyS" ) == pDR->d_name ){ + /* Enumerate BuiltIn Serial */ + sprintf( tBuffer, "Native Serial: %s", pDR->d_name ); + LOG( tBuffer ); + char dev_name[CTEXT]; + int fd = -1; + struct serial_struct ss; + sprintf( dev_name, "/dev/%s", pDR->d_name ); + fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK ); + if (-1 == fd) { + sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno)); + LOG_ERROR( tBuffer ); + continue; + } else { + if (-1 == xioctl(fd, TIOCGSERIAL, &ss)){ + LOG_ERROR("TIOCGSERIAL"); + } else { + sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]", + ss.port, + ss.irq, + ss.baud_base, + ss.line, + ss.type, + ss.flags + ); + LOG( tBuffer ); + char comName[16]; + int pn = atoi( pDR->d_name+4) + 1; + sprintf( comName,"COM%d", pn ); + m_pDBC->addNodeSocket( m_nServerNodeID, ARGUS_NODE_SOCK_TYPE_SERIAL_PORT, dev_name, comName ); + }; + close(fd); + continue; + }; + continue; + }; + if( strcasestr( pDR->d_name, "ttyUSB" ) == pDR->d_name ){ + /* Enumerate USB Serial devices */ + sprintf( tBuffer, "USB Serial: %s", pDR->d_name ); + LOG( tBuffer ); + char dev_name[CTEXT]; + int fd = -1; + struct serial_struct ss; + sprintf( dev_name, "/dev/%s", pDR->d_name ); + fd = open( dev_name, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK ); + if (-1 == fd) { + sprintf( tBuffer, "Cannot open '%s': %d, %s\\n", dev_name, errno, strerror(errno)); + LOG_ERROR( tBuffer ); + continue; + } else { + if (-1 == xioctl(fd, TIOCGSERIAL, &ss)){ + LOG_ERROR("TIOCGSERIAL"); + } else { + sprintf( tBuffer, "Serial Port[Port:0x%X IRQ:%d BB:%d Line:%d Type:%d Flags:%X]", + ss.port, + ss.irq, + ss.baud_base, + ss.line, + ss.type, + ss.flags + ); + LOG( tBuffer ); + char comName[16]; + int pn = atoi( pDR->d_name+4) + 1; + sprintf( comName,"USB-COM%d", pn ); + m_pDBC->addNodeSocket( m_nServerNodeID, ARGUS_NODE_SOCK_TYPE_SERIAL_PORT, dev_name, comName ); + }; + close(fd); + continue; + }; + continue; + }; + }; + closedir( dFD ); + }; +}; + + + + +void ARGUS_Kernel::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 ARGUS_Kernel::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 ARGUS_Kernel::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; + }; + return -1; +}; + +void ARGUS_Kernel::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 ); + int cid = isCameraListed(DiscoverResults->at(index)->RefID); + if( cid != -1 ) { + sprintf( tBuffer, "Camera is Listed as %d", cid ); + LOG( tBuffer ); + if( strstr(DiscoverResults->at(index)->XAddrs,m_vCamRecords.at(cid)->ip)==NULL ) { + m_pDBC->updateIPCam( m_nSiteID, DiscoverResults->at(index)); + + }; + } else { + // Uncknown camera - no checking + LOG("Camera is Not Listed"); + m_pDBC->updateIPCam( 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 ARGUS_Kernel::signalEvent( void ){ + if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){ + + + + }; + return true; +}; + +int ARGUS_Kernel::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 *ARGUS_Kernel::getNetNode( int id ){ + return NULL; +}; + +int ARGUS_Kernel::getModules( ARGUS_MODULE_TYPE type, argusKernelModule **&RA ){ + return 0; +}; + +argusKernelModule *ARGUS_Kernel::getModule( int id ){ + return NULL; +}; + +#undef LOG +#undef LOG_ERROR + diff --git a/lib/App/argus_server/ArgusKernel.h b/lib/App/argus_server/ArgusKernel.h new file mode 100644 index 00000000..aaeba6fb --- /dev/null +++ b/lib/App/argus_server/ArgusKernel.h @@ -0,0 +1,125 @@ +/******************************************************************* +* +* DESCRIPTION: ARGUS Kernel Header File +* +* AUTHOR: gagua +* +* HISTORY: +* +* DATE:2/25/2024 +* +*******************************************************************/ +#ifndef __ARGUS_KERNEL_HEADER__ +#define __ARGUS_KERNEL_HEADER__ + +#include +#include + +#include +#include +#include +#include + +#include /* getopt_long() */ + +#include /* low-level i/o */ +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +struct serial_struct { + int type; + int line; + unsigned int port; + int irq; + int flags; + int xmit_fifo_size; + int custom_divisor; + int baud_base; + unsigned short close_delay; + char io_type; + char reserved_char[1]; + int hub6; + unsigned short closing_wait; /* time to wait before closing */ + unsigned short closing_wait2; /* no longer used... */ + unsigned char *iomem_base; + unsigned short iomem_reg_shift; + unsigned int port_high; + unsigned long iomap_base; /* cookie passed into ioremap */ +}; + +#include "../../alg/generic/md5.h" +#include "../../components/base/basicThread.h" +#include "../../components/base/basicEngine.h" +#include "../../components/base/basicSettingsProvider.h" +#include "../../components/base/basicEventHeap.h" +#include "../../data/net/netServer.h" + + +#define SLEEP_KERNEL_INTERVAL 10 +#define ONVIF_DISCOVERY_INTERVAL 300 // 5 min +#define ONVIF_DISCOVERY_RESULT_TIME 5 + +#define ARGUS_KERNEL_TAG_SITE_ID "SITE_ID" +#define ARGUS_KERNEL_TAG_SERVER_IDENTITY "IDENTITY" + +#define ARGUS_NODE_SOCK_TYPE_SERIAL_PORT 1 +#define ARGUS_NODE_SOCK_TYPE_NETWORK 2 +#define ARGUS_NODE_SOCK_TYPE_DC 3 +#define ARGUS_NODE_SOCK_TYPE_VIDEO_GRABBER 4 +#define ARGUS_NODE_SOCK_TYPE_AUDIO_GRABBER 5 +#define ARGUS_NODE_SOCK_TYPE_VIDEO_DIGITAL 6 + +class ARGUS_Kernel: public basicThread { + private: + uint32_t m_nSiteID; + uint32_t m_nServerID; + uint32_t m_nServerNodeID; + char m_sIdentity[33]; + time_t m_tTimeTSeconds; + basicLogCollection * m_pLog; + basicEventHeap * m_pEventHeap; + config_setting_t * m_pSettings; + ssdpAnnouncer * m_pServiceAnnouncer; + uint8_t m_bExitCause; + ARGUS_DBC * m_pDBC; + networkServer * m_pNetClientsServer; + onvifMediaObserver * m_pOnvifMediaObserver; + bool m_bDiscoveryStarted; + vectorARGUS_Cameras m_vCameras; + vectorCamRecords m_vCamRecords; + vectorEventRecords m_vEvtRecords; + argusNodeFactory * m_pNodeFactory; + argusModuleFactory * m_pModuleFactory; + mediaNginxManager * m_pStreamManager; + //servletWS * m_pServletWS; + //int isCameraListed( CSTR uid ); + //void validateCameras( void ); + //void validateEvents( int &lastTSS, int &lastTMS ); + void enumerateHardware( void ); + protected: + void m_fThreadProc( void ); + public: + // Constructor + explicit ARGUS_Kernel( config_setting_t *Settings = NULL ); + // Destructor + virtual ~ARGUS_Kernel(); + bool create( void ); + bool signalEvent( void ); + void WaitFinish( bool Force = false ); + uint8_t getExitCause( void ){ return m_bExitCause;}; + int getNetNodes( ARGUS_NODE_TYPE type, argusNetNode **&RA ); + argusNetNode * getNetNode( int id ); + int getModules( ARGUS_MODULE_TYPE type, argusKernelModule **&RA ); + argusKernelModule * getModule( int id ); +}; + + +#endif diff --git a/lib/components/base/basicSSL.cpp b/lib/components/base/basicSSL.cpp new file mode 100644 index 00000000..a6469136 --- /dev/null +++ b/lib/components/base/basicSSL.cpp @@ -0,0 +1,24 @@ +#include "basicSSL.h" + +#ifdef USE_OPEN_SSL + static bool OPEN_SSL_LIB_INIT_DONE = false; +#endif + + + + +static void sslLibInit( void ){ +#ifdef USE_OPEN_SSL + if (!OPEN_SSL_LIB_INIT_DONE) { + OPEN_SSL_LIB_INIT_DONE = true; + /* ---------------------------------------------------------- * + * These function calls initialize openssl for correct work. * + * ---------------------------------------------------------- */ + OpenSSL_add_all_algorithms(); + ERR_load_BIO_strings(); + ERR_load_crypto_strings();a + SSL_load_error_strings(); + }; +#endif +}; + diff --git a/lib/components/base/basicSSL.h b/lib/components/base/basicSSL.h new file mode 100644 index 00000000..326b3f55 --- /dev/null +++ b/lib/components/base/basicSSL.h @@ -0,0 +1,20 @@ +#ifndef __BASIC_SSL_INIT_HEADER__ + +#define __BASIC_SSL_INIT_HEADER__ + +#define USE_OPEN_SSL + +#ifdef USE_OPEN_SSL + + #include + #include + #include + #include + #include + #include + +#endif + +static void sslLibInit( void ); + +#endif diff --git a/lib/components/object/objectCertStorage.cpp b/lib/components/object/objectCertStorage.cpp new file mode 100644 index 00000000..6a17ce14 --- /dev/null +++ b/lib/components/object/objectCertStorage.cpp @@ -0,0 +1,13 @@ +#include "objectCertStorage.h" + + + + +certStorage::certStorage(){ + +}; + +certStorage::~certStorage(){ + +}; + diff --git a/lib/components/object/objectCertStorage.h b/lib/components/object/objectCertStorage.h new file mode 100644 index 00000000..e7d844e9 --- /dev/null +++ b/lib/components/object/objectCertStorage.h @@ -0,0 +1,12 @@ +#ifndef __ARGUS_CERTIFICATES_STORAGE_OBJECT_HEADER__ +#define __ARGUS_CERTIFICATES_STORAGE_OBJECT_HEADER__ + + + +class certStorage{ + public: + certStorage(); + virtual ~certStorage(); +}; + +#endif diff --git a/lib/data/net/netServer.h b/lib/data/net/netServer.h index b2845670..e3f07532 100644 --- a/lib/data/net/netServer.h +++ b/lib/data/net/netServer.h @@ -51,6 +51,16 @@ #define AUTOMATIC_IP_DETECTION +#define USE_SSL_ENCRIPTION + + + + +#ifdef USE_SSL_ENCRIPTION + #include "../../components/obect/objectCertStorage.h" + + +#endif #define NWS_SETTINGS_HOST "NW_HOSTNAME_IP" #define NWS_SETTINGS_PORT "NW_LISTEN_PORT" @@ -83,8 +93,8 @@ class networkServer: public basicThread, public basicEventHandler{ explicit networkServer( config_setting_t *Settings = NULL, networkServiceEngine *ptr=NULL ); virtual ~networkServer(); bool BootUp( void ); // Config By Settings provided in constructor, on fail uses next BootUp - bool BootUp( U16 Port ); // Uses Default Hostname and Port as parameter, on fail (getlocalhost) uses next Bootup - bool BootUp( CSTR HOST_NAME, U16 Port ); // Uses parameters + bool BootUp( U16 Port, bool useSSL = false ); // Uses Default Hostname and Port as parameter, on fail (getlocalhost) uses next Bootup + bool BootUp( CSTR HOST_NAME, U16 Port, bool useSSL = false ); // Uses parameters #ifdef __linux__ bool BootUp( CSTR UNIX_SOCKET_NAME ); #endif