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.
 
 
 
 
 
 

797 lines
18 KiB

#include "protocolDefender.h"
#include "../../alg/generic/md5.h"
#include "../json/json_tool.h"
#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
#include <strings.h>
#define LOG(X) m_pLog->Log("Defender", this, X)
#define LOGS(X) basicLogCollection::getInstance()->Log("Defender", NULL, X)
#define LOG_ERROR(X) m_pLog->Log("Defender", this, _LOG_ERROR, X)
CSTR protocolDefender::buildURL(CSTR templ, int dim, CSTR devID)
{
CSTR RetVal;
RetVal = (CSTR) malloc(256);
if (RetVal != NULL)
{
if (m_bUseSSL)
{
sprintf(RetVal, "https://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
}
else
{
sprintf(RetVal, "http://%s:%d%s", m_pServerIP, m_nServerPort, m_pAccessURL);
};
};
return RetVal;
}
/* Default Constructor */
protocolDefender::protocolDefender(config_setting_t * Settings) :
protocolHTTP(basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_CONNECTION"))
{
m_pLog = basicLogCollection::getInstance();
m_bMitigationStatus = false;
m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "DEFENDER_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_CEP_ADDRESS_SIZE);
if (m_pServerIP == NULL)
{
return;
}
memset(m_pServerIP, 0, OS_CEP_ADDRESS_SIZE);
m_pAccessURL = (CSTR) malloc(CTEXT);
if (m_pAccessURL == NULL)
{
return;
}
memset(m_pAccessURL, 0, CTEXT);
m_bCurrentStatus = false;
if (m_pSettings != NULL)
{
int intVar;
double floatVar;
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_float(m_pSettings, "LOCATION_LON", &floatVar))
{
m_fLon = floatVar = 35.0938962;
pSetting = config_setting_add(m_pSettings, "LOCATION_LON", CONFIG_TYPE_FLOAT);
config_setting_set_float(pSetting, floatVar);
basicSettingsProvider::getInstance()->compromise();
}
else
{
m_fLon = floatVar;
};
if (! config_setting_lookup_float(m_pSettings, "LOCATION_LAT", &floatVar))
{
m_fLat = floatVar = 32.904208;
pSetting = config_setting_add(m_pSettings, "LOCATION_LAT", CONFIG_TYPE_FLOAT);
config_setting_set_float(pSetting, floatVar);
basicSettingsProvider::getInstance()->compromise();
}
else
{
m_fLat = floatVar;
};
if (! config_setting_lookup_bool(m_pSettings, "USE_SSL", &intVar))
{
intVar = m_bUseSSL = false;
LOG("PROTOCOL DISABLED[default]");
pSetting = config_setting_add(m_pSettings, "USE_SSL", CONFIG_TYPE_BOOL);
config_setting_set_bool(pSetting, intVar);
basicSettingsProvider::getInstance()->compromise();
}
else
{
m_bUseSSL = (bool) intVar;
};
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, "CEP_IP_ADDRESS", (const char **) &m_pServerIP))
{
strncpy(m_pServerIP, "192.168.200.204", 16);
LOG(m_pServerIP);
pSetting = config_setting_add(m_pSettings, "CEP_IP_ADDRESS", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pServerIP);
basicSettingsProvider::getInstance()->compromise();
};
LOG(m_pServerIP);
if (! config_setting_lookup_int(m_pSettings, "CEP_IP_PORT", &m_nServerPort))
{
m_nServerPort = 80;
pSetting = config_setting_add(m_pSettings, "CEP_IP_PORT", CONFIG_TYPE_INT);
config_setting_set_int(pSetting, m_nServerPort);
basicSettingsProvider::getInstance()->compromise();
};
if (! config_setting_lookup_string(m_pSettings, "ACCESS_URL", (const char **) &m_pAccessURL))
{
strncpy(m_pAccessURL, DFSL_DEFENDER_DEFAULT_URL, CTEXT);
pSetting = config_setting_add(m_pSettings, "ACCESS_URL", CONFIG_TYPE_STRING);
config_setting_set_string(pSetting, m_pAccessURL);
basicSettingsProvider::getInstance()->compromise();
};
LOG(m_pAccessURL);
};
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.
*/
protocolDefender::~protocolDefender()
{
if (m_pScannerName != NULL)
{
free(m_pScannerName);
}
if (m_pServerIP != NULL)
{
free(m_pServerIP);
}
if (m_pAccessURL != NULL)
{
free(m_pAccessURL);
}
}
void protocolDefender::setScannerName(CSTR Name)
{
sprintf(m_pScannerName, "%s", Name);
}
void protocolDefender::setProtocolType(bool DeviceIs3D)
{
if (DeviceIs3D)
{
m_nScannerDimentions = 3;
}
else
{
m_nScannerDimentions = 2;
};
}
CSTR protocolDefender::assembleStatusData(bool StatusOK)
{
CSTR RetVal = NULL;
CSTR TIME_STAMP;
CSTR tSRC;
CSTR tTARGET;
CSTR tID;
RetVal = (CSTR) malloc(CLOG);
if (RetVal != NULL)
{
TIME_STAMP = getTimeStamp();
if (TIME_STAMP != NULL)
{
tTARGET = (CSTR) malloc(128);
if (tTARGET != NULL)
{
tSRC = (CSTR) malloc(128);
if (tSRC)
{
tID = (CSTR) malloc(64);
if (tID != NULL)
{
sprintf(tID, "%s", MD5String(TIME_STAMP));
sprintf(tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName);
sprintf(tTARGET, DFSL_STATUS_DTO, TIME_STAMP, defenderStatusMode[((StatusOK) ? 1 : 0)]);
sprintf(RetVal, PACKAGE_SE_STATUS_DATA_ELEMENT, defenderTypeDescription[DC_STATUS], TIME_STAMP, tSRC, "DFSL_LIDAR_SENSOR_STATUS", DFSL_DATA_TYPE_STATUS, tID, tTARGET);
free(tID);
free(tSRC);
free(tTARGET);
free(TIME_STAMP);
}
else
{
free(tSRC);
free(tTARGET);
free(TIME_STAMP);
free(RetVal);
}
}
else
{
free(tTARGET);
free(TIME_STAMP);
free(RetVal);
};
}
else
{
free(TIME_STAMP);
free(RetVal);
};
}
else
{
free(RetVal);
RetVal = NULL;
};
};
return RetVal;
}
CSTR protocolDefender::assembleFaultData(uint8_t Code, CSTR Desc)
{
CSTR RetVal;
CSTR TIME_STAMP;
CSTR tSRC;
CSTR tTARGET;
CSTR tID;
RetVal = (CSTR) malloc(CLOG);
if (RetVal != NULL)
{
TIME_STAMP = getTimeStamp();
if (TIME_STAMP != NULL)
{
tTARGET = (CSTR) malloc(128);
if (tTARGET != NULL)
{
tSRC = (CSTR) malloc(128);
if (tSRC)
{
tID = (CSTR) malloc(64);
if (tID != NULL)
{
sprintf(tID, "%s", MD5String(TIME_STAMP));
sprintf(tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName);
sprintf(tTARGET, DFSL_ERROR_DTO, TIME_STAMP, Code, Desc);
sprintf(RetVal, PACKAGE_SE_STATUS_DATA_ELEMENT, Desc, TIME_STAMP, tSRC, "DFSL_LIDARE_SENSOR_FAULT_DATA", DFSL_DATA_TYPE_ERROR, tID, tTARGET);
free(tID);
free(tSRC);
free(tTARGET);
free(TIME_STAMP);
}
else
{
free(tSRC);
free(tTARGET);
free(TIME_STAMP);
free(RetVal);
}
}
else
{
free(tTARGET);
free(TIME_STAMP);
free(RetVal);
};
}
else
{
free(TIME_STAMP);
free(RetVal);
};
}
else
{
free(RetVal);
RetVal = NULL;
};
};
return RetVal;
}
CSTR protocolDefender::assembleTargetData(int targetID, int Range, int Width, int Azimuth, int Elevation)
{
CSTR RetVal;
CSTR TIME_STAMP;
CSTR tSRC;
CSTR tTARGET;
CSTR tID;
RetVal = (CSTR) malloc(CLOG);
if (RetVal != NULL)
{
TIME_STAMP = getTimeStamp();
if (TIME_STAMP != NULL)
{
tTARGET = (CSTR) malloc(128);
if (tTARGET != NULL)
{
tSRC = (CSTR) malloc(128);
if (tSRC)
{
tID = (CSTR) malloc(64);
if (tID != NULL)
{
sprintf(tID, "%s", MD5String(TIME_STAMP));
sprintf(tSRC, DFSL_SENSOR_SOURCE_DTO, DFSL_HARDWARE_TYPE, DFRL_PROVIDER_ID, m_pScannerName);
if (m_nScannerDimentions > 2)
{
sprintf(tTARGET, DFSL_3D_TARGET_DTO, Azimuth, Elevation, Range);
}
else
{
sprintf(tTARGET, DFSL_2D_TARGET_DTO, Azimuth, Range, Width);
};
sprintf(RetVal, PACKAGE_SE_TARGET_DATA_ELEMENT, defenderTypeDescription[DC_ALARM], TIME_STAMP, tSRC, "DFSL_TARGET_DATA", DFSL_DATA_TYPE_TARGET, tID, tTARGET);
free(tID);
free(tSRC);
free(tTARGET);
free(TIME_STAMP);
}
else
{
free(tSRC);
free(tTARGET);
free(TIME_STAMP);
free(RetVal);
}
}
else
{
free(tTARGET);
free(TIME_STAMP);
free(RetVal);
};
}
else
{
free(TIME_STAMP);
free(RetVal);
};
}
else
{
free(RetVal);
RetVal = NULL;
};
};
return RetVal;
}
static uint32_t DEFENDER_MESSAGE_ID = 0;
size_t protocolDefender::assembleDefenderJSON(CSTR Buffer, defenderCategoryTypes Type, uint8_t Severity, CSTR Location, CSTR Data)
{
char tBuffer[CLOG];
size_t RetVal;
CSTR tBuf;
RetVal = 0;
tBuf = getTimeStamp();
DEFENDER_MESSAGE_ID++;
// memset( tBuffer, 0, CLOG );
if (tBuf != NULL)
{
// char ID[64];
switch (Type)
{
case DC_STATUS: Severity = 0; break;
case DC_ERROR : Severity = 2; break;
case DC_ALARM : Severity = 4; break;
default : Severity = 2;
};
// sprintf( ID, "%s", MD5String( tBuf ) );
if ((m_nScannerDimentions > 2) && (Type == DC_ALARM))
{
Type = DC_ALARM_3D;
}
sprintf(tBuffer, DFSL_SE_ENVELOPE,
// DEFENDER_MESSAGE_ID,
defenderCategories[Type], defenderTypeSeverity[Severity], tBuf, tBuf, Location, Data);
RetVal = strlen(tBuffer);
memcpy(Buffer, tBuffer, RetVal);
Buffer[RetVal] = (char) 0;
free(tBuf);
};
return RetVal;
}
bool protocolDefender::buildStatusPackage(unsigned char * Buffer, int & BufferSize, bool Status)
{
if (Buffer != NULL)
{
CSTR tBuf;
tBuf = assembleStatusData(Status);
#ifdef DEBUG
// LOG(tBuf);
#endif
if (tBuf != NULL)
{
char DFSL_LOCATION[256];
sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat);
BufferSize = assembleDefenderJSON((CSTR) Buffer, DC_STATUS, (Status) ? 0 : 2, DFSL_LOCATION, tBuf);
#ifdef DEBUG
LOG((CSTR) Buffer);
#endif
free(tBuf);
return true;
}
};
return false;
}
bool protocolDefender::buildFaultPackage(unsigned char * Buffer, int & BufferSize, unsigned char Type, CSTR Description)
{
if (Buffer != NULL)
{
CSTR tBuf;
tBuf = assembleFaultData(Type, Description);
if (tBuf != NULL)
{
char DFSL_LOCATION[256];
sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat);
BufferSize = assembleDefenderJSON((CSTR) Buffer, DC_ERROR, 3, DFSL_LOCATION, tBuf);
free(tBuf);
return true;
};
};
return false;
}
bool protocolDefender::buildEvent2DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, float Angle, float Range, float Width)
{
if (Buffer != NULL)
{
CSTR tBuf;
tBuf = assembleTargetData(targetID, Range, Width, Angle, 0);
if (tBuf != NULL)
{
char DFSL_LOCATION[256];
sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat);
BufferSize = assembleDefenderJSON((CSTR) Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf);
free(tBuf);
return true;
};
};
return false;
}
bool protocolDefender::buildEvent3DPackage(unsigned char * Buffer, int & BufferSize, CSTR msgUID, int targetID, float Angle1, float Angle2, float Range)
{
if (Buffer != NULL)
{
CSTR tBuf;
tBuf = assembleTargetData(targetID, Range, 0, Angle1, Angle2);
#ifdef DEBUG
// LOG(tBuf);
#endif
if (tBuf != NULL)
{
char DFSL_LOCATION[256];
sprintf(DFSL_LOCATION, DFSL_LOCATION_DTO, m_fLon, m_fLat);
BufferSize = assembleDefenderJSON((CSTR) Buffer, DC_ALARM, 3, DFSL_LOCATION, tBuf);
#ifdef DEBUG
LOG((CSTR) Buffer);
#endif
free(tBuf);
return true;
};
};
return false;
}
bool protocolDefender::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 protocolDefender::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 protocolDefender::Alarm2D(float Angle, float Range, float Width, int ID)
{
bool RetVal = false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
CSTR UID;
if (! m_bEnabled)
{
return false;
}
UID = getUniqID_MD5();
if (UID != NULL)
{
URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
#ifdef DEBUG
LOG(URL);
#endif
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 protocolDefender::Alarm3D(float Angle1, float Angle2, float Range, int ID)
{
bool RetVal = false;
char Buffer[1024];
int BufferSize = 0;
CSTR URL;
CSTR UID;
if (! m_bEnabled)
{
return false;
}
UID = getUniqID_MD5();
if (UID != NULL)
{
URL = buildURL(SERVICE_URL_TEMPLATE_EVENT, m_nScannerDimentions, m_pScannerName);
#ifdef DEBUG
LOG(URL);
#endif
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 protocolDefender::VisibilityChanged(bool Worsened)
{
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);
#ifdef DEBUG
LOG(URL);
#endif
RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, ! Worsened);
if (URL != NULL)
{
RetVal = issuePost(URL, Buffer, BufferSize);
free(URL);
};
return RetVal;
}
bool protocolDefender::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);
#ifdef DEBUG
LOG(URL);
#endif
RetVal = buildStatusPackage((unsigned char *) Buffer, BufferSize, m_bCurrentStatus);
if (URL != NULL)
{
RetVal = issuePost(URL, Buffer, BufferSize);
free(URL);
};
return RetVal;
}
bool protocolDefender::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);
#ifdef DEBUG
LOG(URL);
#endif
RetVal = buildFaultPackage((unsigned char *) Buffer, BufferSize, lvl, Descr);
if (URL != NULL)
{
RetVal = issuePost(URL, Buffer, BufferSize);
free(URL);
};
return RetVal;
}
bool protocolDefender::StartTimeSynch(void)
{
bool RetVal = false;
// LOG("TimeSYnc");
/*char Buffer[1024];
int BufferSize = 0;
CSTR URL = NULL;
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 protocolDefender::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
{
Json::Value root;
#ifdef OLD_WAY_JSON
Json::Reader reader;
if (! reader.parse((char *) Package->m_pData, root))
{
LOG_ERROR("Error parsing the string");
return true;
};
#else
JSONCPP_STRING err;
Json::CharReaderBuilder builder;
const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
if (! reader->parse((char *) Package->m_pData, (char *) Package->m_pData + Package->m_nSize, &root, &err))
{
LOG_ERROR("Error parsing the string");
LOG_ERROR(err.c_str());
return true;
};
#endif
if (root.size())
{
Json::Value tVal;
tVal = root["mitigation"]["parameters"]["cmd"];
if (! tVal.isNull())
{
#ifdef DEBUG
LOG("MITIGATION MESSAGE!");
#endif
if (strcasecmp(tVal.asString().c_str(), "ARM") == 0)
{
#ifdef DEBUG
LOG("ARM!");
#endif
m_bMitigationStatus = true;
};
if (strcasecmp(tVal.asString().c_str(), "DISARM") == 0)
{
#ifdef DEBUG
LOG("DISARM!");
#endif
m_bMitigationStatus = false;
};
};
};
}
catch (exception e)
{
};
return true;
}
#undef LOG
#undef LOG_ERROR