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.
 
 
 
 
 
 

302 lines
11 KiB

#include "kernelModuleBosch.h"
// #include "mojoKernel.h"
#define LOG(X) kernelModule::m_pLog->Log("MOD_BVMS", 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); \
kernelModule::m_pLog->Log("MOD_BVMS", this, _LOG_ERROR, _tBfrErrMSG2w34); \
}
#else
#define LOG_ERROR(X) kernelModule::m_pLog->Log("MOD_BVMS", this, _LOG_ERROR, X);
#endif
const CSTR bmvs_zones[] = { "C2", "C3", "D1", "D2", "E1", "E2", "E3", "F1", "F2", "G1", "G2", "G3", "H1", "H2", "C1", "B2", "B1", "A3", "A2", "A1", "" };
const uint8_t bmvs_zones_count = 20;
kernelModuleBosch::kernelModuleBosch(uint32_t ModuleID) :
kernelModule(ModuleID, KERNEL_MODULE_BOSCH_REST){
m_pCURL = NULL;
m_pHttpAccessManager = NULL;
m_pEventHeap = basicEventHeap::getInstance();
}
kernelModuleBosch::~kernelModuleBosch(){
m_bEnabled = false;
if ( m_pCURL ){
curl_easy_cleanup(m_pCURL);
m_pCURL = NULL;
};
}
bool kernelModuleBosch::startService(){
m_pHttpAccessManager = curlRequestsManager::getInstance();
if( !m_bEnabled ) return false;
if ( m_pCURL ) return true;
if ( reloadSettings() ) return true;
return false;
}
bool kernelModuleBosch::stopService(bool force){
m_bEnabled = false;
return true;
}
bool kernelModuleBosch::reloadSettings(){
if (m_pDBC == NULL) return false;
if (! m_bEnabled) return false;
REST_SETTINGS * tData = m_pDBC->getBoschData( m_nDBAUXID );
if ( tData != NULL ){
memcpy( &m_sRestSettings, tData, sizeof(REST_SETTINGS));
free( tData );
return true;
};
return false;
}
bool kernelModuleBosch::_send_request(CSTR url, CSTR post_string ){
#ifdef CURL_MANAGER
bool RetVal = false;
if (m_pHttpAccessManager){
m_mResponseProtector.lock();
curlRequest * tRC;
tRC = new curlRequest( url, (CSTR) "Content-Type: application/json; charset=utf-8;");
if (tRC != NULL){
#ifdef DEBUG_BVMS_TRAFFIC
FILE * tf;
tf = fopen("Traffic_bvms.txt", "a+");
fprintf(tf, "--------------------\r\n");
if ( post_string != NULL ) {
fprintf( tf, "REQ: %s (%d)\n", url, strlen(post_string));
fprintf( tf, ">>>>> %s\n", post_string );
} else fprintf(tf, "REQ: %s\n", url );
fprintf(tf,"Creds: [%s]:[%s]\n", m_sRestSettings.access_user, m_sRestSettings.access_pass );
fprintf(tf, "--------------------\r\n");
fclose(tf);
//bool mustReturnData = true; // In Debug Allways Read Return
#endif
tRC->setCredentials( m_sRestSettings.access_user, m_sRestSettings.access_pass, m_sRestSettings.auth_mode );
if( post_string != NULL ){
tRC->setPOST(post_string, strlen(post_string));
};
RetVal = m_pHttpAccessManager->accomplish( tRC );
} else {
LOG_ERROR("Cannot Create curlRequest!");
};
m_mResponseProtector.unlock();
};
return RetVal;
#else
char tBuffer[CTEXT];
if (m_pCURL == NULL)
{
if (m_pLastResponse == NULL)
{
m_pLastResponse = (CSTR) malloc(DEFAULT_BUFFER_SIZE);
};
m_pCURL = curl_easy_init();
if (m_pProxyHost != NULL && m_nProxyPort != 0)
{
curl_easy_setopt(m_pCURL, CURLOPT_PROXY, m_pProxyHost);
curl_easy_setopt(m_pCURL, CURLOPT_PROXYPORT, m_nProxyPort);
if (m_pProxyUsername != NULL && m_pProxyPassword != NULL)
{
sprintf(tBuffer, "%s:%s", m_pProxyUsername, m_pProxyPassword);
curl_easy_setopt(m_pCURL, CURLOPT_PROXYUSERPWD, tBuffer);
};
};
curl_easy_setopt(m_pCURL, CURLOPT_CONNECTTIMEOUT, 1);
curl_easy_setopt(m_pCURL, CURLOPT_TIMEOUT, 2);
curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(m_pCURL, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt(m_pCURL, CURLOPT_VERBOSE, 0);
curl_easy_setopt(m_pCURL, CURLOPT_POST, 1);
// curl_easy_setopt( m_pCURL, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS, 0 );
// curl_easy_setopt( m_pCURL, CURLOPT_NOPROGRESS, 1 );
// curl_easy_setopt( m_pCURL, CURLOPT_NOSIGNAL, 1 );
curl_easy_setopt(m_pCURL, CURLOPT_TCP_FASTOPEN, 1);
curl_easy_setopt(m_pCURL, CURLOPT_TCP_NODELAY, 1);
// curl_easy_setopt( m_pCURL, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_easy_setopt(m_pCURL, CURLOPT_WRITEFUNCTION, pOnvif::_writer);
curl_easy_setopt(m_pCURL, CURLOPT_WRITEDATA, this);
};
if (m_pCURL != NULL)
{
curl_easy_setopt(m_pCURL, CURLOPT_URL, url);
struct curl_slist * sList = NULL;
// Content-Type: application/soap+xml; charset=utf-8; action="http://www.onvif.org/ver20/ptz/wsdl/ContinuousMove"
// Connection: Close
sList = curl_slist_append(sList, "Content-Type: text/xml; charset=utf-8");
sprintf(tBuffer, "Content-Length: %d", strlen(post_string));
sList = curl_slist_append(sList, tBuffer);
curl_easy_setopt(m_pCURL, CURLOPT_HTTPHEADER, sList);
curl_easy_setopt(m_pCURL, CURLOPT_POSTFIELDS, post_string);
sprintf(tBuffer, "%s:%s", m_pUsername, m_pPassword);
curl_easy_setopt(m_pCURL, CURLOPT_USERPWD, tBuffer);
switch (m_bAuthMode)
{
case 0: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); break;
case 1: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); break;
case 2: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST_IE); break;
case 3: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_BEARER); break;
case 4: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NEGOTIATE); break;
case 5: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); break;
case 6: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_NTLM_WB); break;
case 7: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY); break;
case 8: curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ANYSAFE); break;
case 9:
curl_easy_setopt(m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_ONLY);
break;
// case 10: curl_easy_setopt( m_pCURL, CURLOPT_HTTPAUTH, CURLAUTH_AWS_SIGV4 ); break;
};
#ifdef DEBUG
char tErrorBuffer[CURL_ERROR_SIZE];
curl_easy_setopt(m_pCURL, CURLOPT_ERRORBUFFER, tErrorBuffer);
#endif
m_pLastIndex = 0;
if (curl_easy_perform(m_pCURL))
{
LOG_ERROR("CURL Failure!");
#ifdef DEBUG
LOG_ERROR(tErrorBuffer);
#endif
}
else
{
m_pLastResponse[m_pLastIndex] = (char) 0;
return true;
};
m_pLastIndex = 0;
};
return false;
#endif
}
bool kernelModuleBosch::buildTargetEventMsg( CSTR Buffer, float az, float range, float lat, float lon, float alt, uint64_t extID, CSTR src_Name ){
if ( Buffer == NULL ) return false;
char tTS[64];
time_t LocalTime;
time(&LocalTime);
tm gmtt;
tm * gmt_time = &gmtt;
if( gmtime_r( &LocalTime, &gmtt ) == NULL) return false;
CSTR pp = strstr( src_Name, "_" );
uint8_t zid = bmvs_zones_count;
if( pp!= NULL ) zid = atoi( pp+1 )-1;
if ( zid > bmvs_zones_count ) zid = bmvs_zones_count;
sprintf(tTS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec);
sprintf( Buffer, "{\"externalAlarmId\":%d,\"data\":[%0.2f,%0.1f,%0.12f,%0.12f,%0.3f,10,\"%s\",\"%s\",\"%s\",\"Detection Event\"]}",
m_sRestSettings.detection_src,
az, range,
lat, lon, alt,
src_Name,
bmvs_zones[zid],
tTS
);
return true;
}
bool kernelModuleBosch::buildCameraEventMsg( CSTR Buffer, CSTR CamName, uint8_t Status ){
if ( Buffer == NULL ) return false;
char tTS[64];
time_t LocalTime;
time(&LocalTime);
tm gmtt;
tm * gmt_time = &gmtt;
if( gmtime_r( &LocalTime, &gmtt ) == NULL) return false;
sprintf( tTS, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + gmt_time->tm_year, 1 + gmt_time->tm_mon, gmt_time->tm_mday, gmt_time->tm_hour, gmt_time->tm_min, gmt_time->tm_sec );
sprintf( Buffer, "{\"externalAlarmId\":%d,\"data\":[\"%s\",%d,\"%s\",\"Camera Event\"]}",
m_sRestSettings.camera_src,
CamName,
Status,
tTS
);
return true;
}
bool kernelModuleBosch::dispatchEvent(EVENT_CONTAINER * Evt){
if( Evt == NULL ) return false;
if( !m_bEnabled ) return true;
char tBuffer[1024];
char tURL[256];
#ifdef DEBUG_EXTRA
sprintf(tBuffer, "Event Class %d Type %d ", Evt->m_eClass, Evt->m_eType);
LOG(tBuffer);
#endif
switch (Evt->m_eType){
case EVT_ROI_ACTIVE:
case EVT_TARGET_2D_BLACK:
case EVT_TARGET_2D_WHITE:{
if ( m_sRestSettings.detection_enabled ) {
TARGET_DATA * tTARGET = (TARGET_DATA *) Evt->m_pEvtData;
if (tTARGET != NULL){
CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID );
if ( tN ) {
if ( buildTargetEventMsg(
tBuffer,
tTARGET->local_target.fi,
tTARGET->local_target.r,
tTARGET->global_target.lat,
tTARGET->global_target.lon,
tTARGET->global_target.alt,
Evt->m_nSourceNodeID,
tN
) ) {
sprintf( tURL,"https://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api );
_send_request( tURL ,tBuffer );
};
free( tN );
};
};
};
break;
};
case EVT_CAMERA_MOVE_TO:{
// Evt->m_nSourceNodeID
//LOG("CAM MOVE!");
break;
};
case EVT_CAMERA_ON_POSITION:{
if ( m_sRestSettings.camera_enabled ){
DBC_SNAPSHOT_DATA * tData = (DBC_SNAPSHOT_DATA *) Evt->m_pEvtData;
if( tData ){
CSTR tN = m_pDBC->getNodeName( Evt->m_nSourceNodeID );
if ( tN ) {
if ( buildCameraEventMsg( tBuffer, tN, 1 ) ) {
sprintf( tURL,"https://%s:%d%s", m_sRestSettings.server_ip, m_sRestSettings.port, m_sRestSettings.rest_api );
_send_request( tURL ,tBuffer );
};
free( tN );
};
};
};
break;
};
default: return true;
};
return true; // Never return false -> to propogate event to next dispatcher.
}
bool kernelModuleBosch::validate(){
//check settings
REST_SETTINGS * tData = m_pDBC->getBoschData( m_nDBAUXID );
if( tData ){
if( memcmp( &m_sRestSettings, tData, sizeof(REST_SETTINGS) ) != 0 ){
memcpy( &m_sRestSettings, tData, sizeof(REST_SETTINGS) );
};
free( tData );
return true;
};
return false;
}
#undef LOG
#undef LOG_ERROR