You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

398 lines
11 KiB

/*******************************************************************
*
* DESCRIPTION: ARGUS Kernel source
*
* AUTHOR: gagua
*
* HISTORY:
*
* DATE:5/29/2019
*
*******************************************************************/
#include "Pathfinder_Kernel.h"
#include <sys/types.h>
#define LOG( X ) m_pLog->Log( "KRNL", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "KRNL", this, _LOG_ERROR, X )
PF_Kernel::PF_Kernel( config_setting_t *Settings ):basicThread(){
m_bExitCause = 0;
m_pDBC = NULL;
m_pBNO = NULL;
m_pCamera = NULL;
m_pScanner = NULL;
m_tTimeTSeconds = time( NULL );
m_bArmed = ( bool ) digitalRead ( 4 );
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "KERNEL" );
m_pServiceAnnouncer = new ssdpAnnouncer( m_pSettings );
};
PF_Kernel::~PF_Kernel(){
m_pDBC->unregisterServer();
#ifdef DEBUG
LOG("Destructor");
#endif
WaitFinish(true);
#ifdef DEBUG
LOG("Deleting Components");
#endif
if( m_pCamera!= NULL ) delete( m_pCamera );
if( m_pScanner!= NULL ) delete( m_pScanner );
#ifdef DEBUG
LOG("Destructor Finished");
#endif
};
bool PF_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;
};
m_pBNO = new sensorBNO( m_pSettings );
if( m_pBNO != NULL ) {
if( !m_pBNO->create() ){
LOG_ERROR("BNO Creation failure!");
};
} else {
LOG_ERROR("BNO Construction failure!");
};
if( m_pDBC == NULL ) {
LOG("Explicitly Create DBC Object");
m_pDBC = new PATHFINDER_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->clenupCommandQueue();
m_pDBC->registerServer();
#ifdef DEBUG
LOG( "Start SSDP Beacon" );
#endif
if( m_pServiceAnnouncer != NULL ) {
if( !m_pServiceAnnouncer->init() ){
delete(m_pServiceAnnouncer);
m_pServiceAnnouncer = NULL;
};
};
#ifdef DEBUG
LOG("Init Camera");
#endif
m_pCamera = new gPhotoCamera( m_pSettings );
if( m_pCamera != NULL ) {
m_pCamera->create();
};
#ifdef DEBUG
LOG("Init LRF Scanner");
#endif
m_pScanner = new LRF_techScanner( m_pSettings );
if( m_pScanner != NULL ) {
if( !m_pScanner->create() ){
LOG_ERROR( "Error Create LRF Scanner!" );
} else {
};
};
/*
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( "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 PF_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 PF_Kernel::m_fThreadProc( void ){
char Buffer[CTEXT];
time_t tTimeTSeconds;
int lastTSS = 0;
int lastTMS = 0;
DB_COMMAND * tCMD;
char pidBuf[16];
pid_t pid = gettid();
sprintf(pidBuf,"pid:%d", pid);
LOG(pidBuf);
m_bThreadStatus = TH_WORKING;
while( m_bThreadStatus < TH_ENDED ){
m_tTimeTSeconds = time( NULL );
// Network IP OnVif Discovery
// Command Processing Loop
tCMD = m_pDBC->fetchCommand();
if( tCMD != NULL ){
switch( tCMD->cmd ) {
case 10:{
if( m_pScanner!= NULL ) {
m_pScanner->startScan();
};
break;
};
case 13:{
if( m_pScanner!= NULL ) {
m_pScanner->resetDevice();
};
break;
};
case 15:{
if( m_pScanner!= NULL ) {
m_pScanner->stopScan();
};
break;
};
case 22:{
if( m_pCamera!= NULL ) {
m_pCamera->makeShot();
};
break;
};
case 23:{
if( m_pCamera!= NULL ) {
m_pCamera->startBurst();
};
break;
};
case 24:{
if( m_pCamera!= NULL ) {
m_pCamera->stopBurst();
};
break;
};
};
m_pDBC->finishCommand( tCMD->id );
free( tCMD );
tCMD = NULL;
};
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 );
};
};
bool PF_Kernel::setStatus( int A ){
m_bThreadStatus = (thredStatus)A;
return true;
};
bool PF_Kernel::signalEvent( void ){
//if( m_pDBC->getLastEvents( m_vEvtRecords, 0,0 ) ){
//};
return true;
};
void PF_Kernel::armStatusChanged( void ){
int tPinVal = digitalRead ( 4 );
if( tPinVal == 1 && !m_bArmed ) {
LOG("!!!!!!!ARMED!!!!!!!");
m_bArmed = true;
} else {
if( tPinVal == 0 && m_bArmed ) {
LOG("!!!!!DISARMED!!!!!!");
m_bArmed = false;
};
};
};
#undef LOG
#undef LOG_ERROR