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.
 
 
 
 
 
 

503 lines
14 KiB

#include "protocolSafeShore.h"
#include "../../alg/generic/md5.h"
#define LOG( X ) m_pLog->Log( "SafeShore", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "SafeShore", this, _LOG_ERROR, X )
CSTR systemStatusMode[] = {
"Operational",
"Degraded"
};
CSTR systemFaultCode[] = {
"Critical",
"Warning",
"Info"
};
/* Global Time Diff Value to correct Timestamps */
static int g_iTimeDiff = 0;
/**
* number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
*/
//#define EPOCH_DIFF 11644473600LL
#define UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT 116444736000000000
#ifdef __linux__
unsigned long long getSHTimeValue( void ){
unsigned long long tMilliseconds;
struct timeval tData;
struct timezone tTZ;
tMilliseconds = 0;
tTZ.tz_minuteswest = 0;
tTZ.tz_dsttime = 0;
if( gettimeofday( &tData, &tTZ ) == -1 ){
} else {
tMilliseconds = tData.tv_sec;
tMilliseconds = tMilliseconds *1000;
tMilliseconds += tData.tv_usec / 1000;
};
#else
/*
void TimetToFileTime( time_t t, LPFILETIME pft ){
LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
pft->dwLowDateTime = (DWORD) ll;
pft->dwHighDateTime = ll >>32;
}*/
unsigned __int64 getSHTimeValue( void ){
unsigned __int64 tMilliseconds;
SYSTEMTIME Ztime;
GetSystemTime(&Ztime);
SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
tMilliseconds -= UNIX_TimeStamp_2_WINDOWS_FileTime_SHIFT;
tMilliseconds = tMilliseconds / 10000;
#endif
tMilliseconds += g_iTimeDiff;
return tMilliseconds;
};
CSTR getSHTimeStamp( void ){
CSTR RetVal;
#ifdef __linux__
RetVal = (CSTR) malloc( 64 );
if( RetVal!=NULL ){
sprintf( RetVal, "%llu", getSHTimeValue() );
};
#else
RetVal = (CSTR) malloc( 64 );
if( RetVal!=NULL ){
sprintf( RetVal, "%I64d", getSHTimeValue() );
};
#endif
return RetVal;
};
bool correctSHTime( int diff ){
#ifdef __linux__
struct timeval tData;
struct timezone tTZ;
tTZ.tz_minuteswest = 0;
tTZ.tz_dsttime = 0;
if( gettimeofday( &tData, &tTZ ) == -1 ){
return false;
} else {
tData.tv_sec += diff / 1000;
tData.tv_usec += ( diff * 1000 ) % 1000000;
};
if( settimeofday( &tData, &tTZ ) == -1 ){
return false;
};
#else
unsigned __int64 tMilliseconds;
SYSTEMTIME Ztime;
GetSystemTime(&Ztime);
SystemTimeToFileTime( &Ztime, (FILETIME*) &tMilliseconds );
tMilliseconds += diff * 10000;
FileTimeToSystemTime( (FILETIME*) &tMilliseconds, &Ztime );
if( 0 == SetSystemTime(&Ztime) ) return false;
#endif
return true;
};
CSTR protocolSafeShore::buildURL( CSTR templ, int dim, CSTR devID ){
CSTR RetVal;
char tpl[256];
RetVal = (CSTR) malloc( 256 );
sprintf( tpl, "http://%s:%d%s", m_pServerIP, m_nServerPort, templ );
sprintf( RetVal, tpl, dim, devID );
return RetVal;
};
CSTR protocolSafeShore::buildUniqID( void ){
CSTR RetVal;
RetVal = (CSTR) malloc(64);
sprintf( RetVal, "%s", MD5String( getSHTimeStamp() ) );
return RetVal;
};
/* Default Constructor */
protocolSafeShore::protocolSafeShore( config_setting_t *Settings ): protocolHTTP( basicSettingsProvider::getInstance()->getSubNode(Settings, "SAFESHORE_CONNECTION") ){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "SAFESHORE_CONNECTION");
m_pScannerName = (CSTR)malloc(OS_SCANNER_NAME_SIZE);
if( m_pScannerName == NULL ) return;
memset( m_pScannerName, 0 , OS_SCANNER_NAME_SIZE);
m_pServerIP = (CSTR)malloc(OS_LCU_ADDRESS_SIZE);
if( m_pServerIP == NULL ) return;
memset( m_pServerIP, 0 , OS_LCU_ADDRESS_SIZE);
m_bCurrentStatus = false;
if( m_pSettings != NULL ){
int intVar;
config_setting_t* pSetting;
if (Settings != NULL) {
m_bEnabled = true;
} else {
if (!config_setting_lookup_bool(m_pSettings, "ENABLE", &intVar)) {
intVar = m_bEnabled = false;
LOG("PROTOCOL DISABLED[default]");
pSetting = config_setting_add(m_pSettings, "ENABLE", CONFIG_TYPE_BOOL);
config_setting_set_bool(pSetting, intVar);
basicSettingsProvider::getInstance()->compromise();
} else {
m_bEnabled = (bool)intVar;
};
};
if (m_bEnabled) {
if (!config_setting_lookup_string(m_pSettings, "SCANNER_NAME", (const char**)&m_pScannerName)) {
strncpy(m_pScannerName, "LRF_2D_001", 64);
LOG(m_pScannerName);
pSetting = config_setting_add(m_pSettings, "SCANNER_NAME", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pScannerName);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_int(m_pSettings, "SCANNER_DIMENTIONS", &m_nScannerDimentions)) {
m_nScannerDimentions = 2;
pSetting = config_setting_add(m_pSettings, "SCANNER_DIMENTIONS", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nScannerDimentions);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_string(m_pSettings, "LCU_IP_ADDRESS", (const char**)&m_pServerIP)) {
strncpy(m_pServerIP, "192.168.200.204", 16);
LOG(m_pServerIP);
pSetting = config_setting_add(m_pSettings, "LCU_IP_ADDRESS", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pServerIP);
basicSettingsProvider::getInstance()->compromise();
};
if (!config_setting_lookup_int(m_pSettings, "LCU_IP_PORT", &m_nServerPort)) {
m_nServerPort = 80;
pSetting = config_setting_add(m_pSettings, "LCU_IP_PORT", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nServerPort);
basicSettingsProvider::getInstance()->compromise();
};
};
basicSettingsProvider::getInstance()->validate();
};
};
/*
1.2 GHz
Number of Cores
Quad-Core
Type
Tegra 3
Processor Number
T30SL
Manufacturer
NVIDIA
Verify that you have the following installed on your system
Ubuntu 16.04 LTS (with GCC 5.4)
NVIDIA Display Driver ver. 375
NVIDIA VideoSDK 7.1
cuDNN 6 & TensorRT 2.1
NVIDIA CUDA 8.0
Download the DeepStream 1.0 tar file (below)
Extract the package at the location of your choice
Follow the usage instructions from the Deepstream_User_guide.pdf in the downloaded package.
*/
protocolSafeShore::~protocolSafeShore(){
if(m_pScannerName!=NULL)free(m_pScannerName);
if(m_pServerIP!=NULL)free(m_pServerIP);
};
void protocolSafeShore::setScannerName( CSTR Name ){
sprintf( m_pScannerName, "%s", Name );
};
void protocolSafeShore::setProtocolType( bool DeviceIs3D ){
if( DeviceIs3D ) {
m_nScannerDimentions = 3;
} else {
m_nScannerDimentions = 2;
};
};
bool protocolSafeShore::buildStatusPackage( unsigned char *Buffer, int &BufferSize, bool Status ){
if( Buffer!= NULL ){
CSTR tBuf;
tBuf = getSHTimeStamp();
if( tBuf != NULL ){
if( Status ){
sprintf( ((char *)(Buffer+BufferSize)), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[0], tBuf );
} else {
sprintf( ((char *)(Buffer+BufferSize)), PACKAGE_ENVELOPE_TEMPLATE_STATUS, systemStatusMode[1], tBuf );
};
BufferSize = strlen( (char *)Buffer );
free( tBuf );
return true;
};
};
return false;
};
bool protocolSafeShore::buildFaultPackage( unsigned char *Buffer, int &BufferSize, unsigned char Type, CSTR Description ){
if( Buffer!= NULL ){
CSTR tBuf;
tBuf = getSHTimeStamp();
if( tBuf != NULL ){
sprintf(
((char *)(Buffer+BufferSize)),
PACKAGE_ENVELOPE_TEMPLATE_FAULT,
systemFaultCode[Type],
tBuf,
Description
);
BufferSize = strlen( (char *)Buffer );
free( tBuf );
return true;
};
};
return false;
};
bool protocolSafeShore::buildEvent2DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, int Angle, int Range, int Width){
if( Buffer!= NULL ){
CSTR tBuf;
tBuf = getSHTimeStamp();
if( tBuf != NULL ){
sprintf(
((char *)(Buffer+BufferSize)),
PACKAGE_ENVELOPE_TEMPLATE_EVT2D,
msgUID,
tBuf,
targetID,
Angle,
Range,
Width
);
BufferSize = strlen( (char *)Buffer );
free( tBuf );
return true;
};
};
return false;
};
bool protocolSafeShore::buildEvent3DPackage( unsigned char *Buffer, int &BufferSize, CSTR msgUID, int targetID, int Angle1, int Angle2, int Range ){
if( Buffer!= NULL ){
CSTR tBuf;
tBuf = getSHTimeStamp();
if( tBuf != NULL ){
sprintf(
((char *)(Buffer+BufferSize)),
PACKAGE_ENVELOPE_TEMPLATE_EVT3D,
msgUID,
tBuf,
targetID,
Angle1,
Angle2,
Range
);
BufferSize = strlen( (char *)Buffer );
free( tBuf );
return true;
};
};
return false;
};
bool protocolSafeShore::buildClockSyncPackage( unsigned char *Buffer, int &BufferSize ){
if( Buffer!= NULL ){
CSTR tBuf;
tBuf = getSHTimeStamp();
if( tBuf != NULL ){
sprintf(
((char *)(Buffer+BufferSize)),
PACKAGE_ENVELOPE_TEMPLATE_CLOCKSYNC,
tBuf
);
BufferSize = strlen( (char *)Buffer );
free( tBuf );
return true;
};
};
return false;
};
bool protocolSafeShore::buildCallBackPackage( unsigned char *Buffer, int &BufferSize, CSTR CallBackPath ){
if( Buffer!= NULL ){
sprintf(
((char *)(Buffer+BufferSize)),
PACKAGE_ENVELOPE_TEMPLATE_REG_CALL,
CallBackPath
);
BufferSize = strlen( (char *)Buffer );
return true;
};
return false;
};
bool protocolSafeShore::Alarm2D( int Angle, int Range, int Width, int ID ){
bool RetVal= false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
CSTR UID;
if(!m_bEnabled) return false;
UID = buildUniqID();
if( UID!= NULL ){
URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
RetVal = buildEvent2DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle, Range, Width );
if( URL != NULL ){
RetVal = issuePost( URL, Buffer, BufferSize );
free( URL );
};
free( UID );
};
return RetVal;
};
bool protocolSafeShore::Alarm3D( int Angle1, int Angle2, int Range, int ID ){
bool RetVal= false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
CSTR UID;
if(!m_bEnabled) return false;
UID = buildUniqID();
if( UID!= NULL ){
URL = buildURL( SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName );
LOG( URL );
RetVal = buildEvent3DPackage( ( unsigned char *) Buffer, BufferSize, UID, ID, Angle1, Angle2, Range );
if( URL != NULL ){
RetVal = issuePost( URL, Buffer, BufferSize );
free( URL );
};
free( UID );
};
return RetVal;
};
bool protocolSafeShore::VisibilityChanged( bool Worsened ){
bool RetVal= false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, !Worsened );
if( URL != NULL ){
RetVal = issuePost( URL, Buffer, BufferSize );
free( URL );
};
return RetVal;
};
bool protocolSafeShore::StatusUpdate( void ){
bool RetVal= false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
if(!m_bEnabled) return false;
URL = buildURL( SERVICE_URL_TEMPLATE_STATUS, m_nScannerDimentions, m_pScannerName );
RetVal = buildStatusPackage( ( unsigned char *) Buffer, BufferSize, m_bCurrentStatus );
if( URL != NULL ){
RetVal = issuePost( URL, Buffer, BufferSize );
free( URL );
};
return RetVal;
};
bool protocolSafeShore::ErrorReporting( int lvl, CSTR Descr ){
bool RetVal= false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
if(!m_bEnabled) return false;
URL = buildURL( SERVICE_URL_TEMPLATE_FAULT, m_nScannerDimentions, m_pScannerName );
RetVal = buildFaultPackage( ( unsigned char *) Buffer, BufferSize, lvl, Descr );
if( URL != NULL ){
RetVal = issuePost( URL, Buffer, BufferSize );
free( URL );
};
return RetVal;
};
bool protocolSafeShore::StartTimeSynch( void ){
bool RetVal= false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL = NULL;
if(!m_bEnabled) return false;
URL = buildURL( SERVICE_URL_TEMPLATE_CLOCKSYNC, m_nScannerDimentions, m_pScannerName );
RetVal = buildClockSyncPackage(( unsigned char *) Buffer, BufferSize );
if( URL != NULL ){
RetVal = issuePost( URL, Buffer, BufferSize );
free( URL );
};
return RetVal;
};
bool protocolSafeShore::packetHandler( protocolPackage *Package ){
//LOG("PACKET HANDLER");
if(!m_bEnabled) return false;
if( Package==NULL )return false;
if( Package->m_nSize == 0 ) return false;
if( Package->m_pData == NULL ) return false;
if( Package->m_nCode != 200 ) return false;
try{
XMLNode node1 = XMLNode::parseString( (const char *)Package->m_pData, "root");
if( node1.isEmpty() ){
//char BUBU[128];
//sprintf( BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen(pPackage), Package->m_nSize );
//LOG_ERROR(BUBU);
} else {
LOG( node1.getName() );
if( strstr(node1.getName(),"ClockSync")!=NULL ){
#ifdef __linux__
unsigned long long t0, t1, t2, t3;
#else
unsigned __int64 t0, t1, t2, t3;
#endif
XMLNode node3 = node1.getChildNode("t0", 0);
XMLNode node4 = node1.getChildNode("t1", 0);
XMLNode node5 = node1.getChildNode("t2", 0);
t3 = getSHTimeValue();
#ifdef __linux__
t0 = atoll(node3.getText(0));
t1 = atoll(node4.getText(0));
t2 = atoll(node5.getText(0));
#else
t0 = _atoi64(node3.getText(0));
t1 = _atoi64(node4.getText(0));
t2 = _atoi64(node5.getText(0));
#endif
int offset = ((t1-t0)+(t2-t3))/2;
int latency = (t3-t0)-(t2-t1);
int jagajaga = offset+latency;
//if( 0 & correctSHTime( jagajaga ) ){
// LOG("Time is Synced");
// g_iTimeDiff = 0;
//} else {
// LOG_ERROR("Time Sync Unucessfull - Will use DIFF!");
g_iTimeDiff = jagajaga;
//};
};
};
}catch( exception e ){
return false;
};
return true;
};
#undef LOG
#undef LOG_ERROR