#include "rgooseModuleOnvifObserver.h"
#include "rgooseKernel.h"
#define SEARCH_REQUEST_STRING "uuid:84ede3de-7dec-11d0-c360-f01234567890urn:schemas-xmlsoap-org:ws:2005:04:discoveryhttp://schemas.xmlsoap.org/ws/2005/04/discovery/Probedn:NetworkVideoTransmitter"
#define SEARCH_REQUEST_STRING2323 "\n" \
"\n" \
"\n" \
"urn:uuid:ac9823ee-6813-498c-8c1b-6272a22353f7\n" \
"\n" \
"http ://schemas.xmlsoap.org/ws/2005/04/discovery/Probe\n" \
"\n" \
"urn:schemas-xmlsoap-org:ws:2005:04:discovery\n" \
"\n" \
"\n" \
"\n" \
"ssdp:any\n" \
"\n" \
"\n" \
"\r\n\r\n"
#define HTTP_OK "200 OK"
/******************************************************************
** Action Defines *
*******************************************************************/
#define HTTP_HEADER_ACTION "POST %s HTTP/1.1\r\n" \
"HOST: %s:%u\r\n" \
"SOAPACTION:\"%s#%s\"\r\n" \
"CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n"\
"Content-Length: %d \r\n\r\n"
#define SOAP_ACTION "\r\n" \
"\r\n" \
"\r\n" \
"\r\n%s" \
"\r\n" \
"\r\n" \
"\r\n"
#define PORT_MAPPING_LEASE_TIME "63072000" //two year
#define ADD_PORT_MAPPING_PARAMS "\r\n" \
"%u\r\n"\
"%s\r\n" \
"%u\r\n"\
"%s\r\n" \
"1\r\n" \
"%s\r\n" \
"" \
PORT_MAPPING_LEASE_TIME \
"\r\n"
#define LOG( X ) argusKernelModule::m_pLog->Log( "MOD_ONVIF", 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 );\
argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
}
#else
#define LOG_ERROR( X ) argusKernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, X );
#endif
#define TLOG( X ) basicLogCollection::getInstance()->Log( "MOD_ONVIF_T", me, X )
#define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "MOD_ONVIF_T", me, _LOG_ERROR, X )
argusModuleOnvifObserver::argusModuleOnvifObserver( uint32_t ModuleID ): argusKernelModule( ModuleID, ARGUS_MODULE_FLYHUNTER ), basicThread(){
m_bEnabled = false;
m_pEventHeap = basicEventHeap::getInstance();
m_pIpAddresses = NULL;
m_nStatus = 0;
#ifdef __linux__
m_bWSAOK = true;
#else
m_bWSAOK = false;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0){
LOG_ERROR("WSAStartup failed with error");
} else m_bWSAOK = true;
#endif
};
argusModuleOnvifObserver::~argusModuleOnvifObserver(){
m_bEnabled = false;
stopService();
};
bool argusModuleOnvifObserver::startService(){
LOG("Starting Service!");
m_bEnabled = true;
//if( m_bEnabled ) return false;
if( !m_bWSAOK ) return false;
return init();
};
bool argusModuleOnvifObserver::stopService(){
waitThreadEnd( true );
if( m_vThreads.size() ) { // Kill-Wait Started Probes
#ifdef DEBUG
LOG("Have to Wait Probes!");
#endif
for( vectorOBSERVER_CTX::iterator index = m_vThreads.begin(); index < m_vThreads.end(); index++ ) {
pthread_join( (*index)->m_hThreadHandle, NULL );
};
m_vThreads.clear();
};
#ifdef DEBUG
LOG("No More Probes!");
#endif
if (m_vResultPool.size()){ // Clear structures
for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
free(*index);
};
m_vResultPool.clear();
};
#ifdef DEBUG
LOG("Data is Cleared!");
#endif
return true;
};
void argusModuleOnvifObserver::m_fThreadProc(){
double dStatus = 0, dStep = 30.0;
int id = 0;
dStatus = 0;
m_bThreadStatus = TH_WORKING;
if( !m_bEnabled ) {
m_bThreadStatus = TH_ENDED;
#ifdef DEBUG
LOG("Module is Disabled!");
#endif
};
#ifdef DEBUG
LOG("Module Started!");
#endif
while( m_bThreadStatus < TH_ENDED ) {
if( m_pIpAddresses != NULL ){
LOG("Check IPS");
if( m_pIpAddresses->size() > 0 ){
LOG("Have IPS");
//Start Reaper thread
dStep = 30.0 / (m_pIpAddresses->size() + 1);
// Start Discoverers on each broadcast IP
id = 0;
for( localIpAddresses::iterator index = m_pIpAddresses->begin(); index < m_pIpAddresses->end(); index++ ){
if (strlen((*index)->broadcast)){
LOG("Have BC Address");
startProbe((*index)->broadcast, id, (int) dStep );
LOG("Probe Started");
};
dStatus += dStep;
m_nStatus = (int)dStatus;
id++;
};
// default probe was here
} else {
LOG_ERROR("No Networks Detected!");
#ifdef DEBUG
LOG("Start Default Probe");
#endif
startProbe(HTTPMU_HOST_ADDRESS, id, (int)dStep);
dStatus += dStep;
m_nStatus = (int)dStatus;
};
} else {
LOG_ERROR("ERROR FETCHING IP ADDRESSES!" );
#ifdef DEBUG
LOG("Start Default Probe");
#endif
startProbe(HTTPMU_HOST_ADDRESS, id, (int)dStep);
dStatus += dStep;
m_nStatus = (int)dStatus;
};
SLEEP( 30000 );
};
};
void argusModuleOnvifObserver::dispatchEvent(EVENT_CONTAINER *Evt){
if(!m_bEnabled) {
LOG("Module Disabled!");
return;
}
if( Evt == NULL ) return;
};
bool argusModuleOnvifObserver::reloadSettings(){
return true;
};
PROBE_CTX *argusModuleOnvifObserver::init_PROBE_CTX(CSTR ADR, int ID, int stp){
PROBE_CTX *RetVal;
RetVal = (PROBE_CTX*)malloc(sizeof(PROBE_CTX));
if (RetVal == NULL) return NULL;
sprintf(RetVal->IpAddress, "%s", ADR);
RetVal->Port = HTTPMU_HOST_PORT;
RetVal->socketFD = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
RetVal->send_buff = (CSTR) SEARCH_REQUEST_STRING;
RetVal->r_address.sin_family = AF_INET;
RetVal->r_address.sin_port = htons(RetVal->Port);
RetVal->r_address.sin_addr.s_addr = inet_addr(RetVal->IpAddress);
RetVal->bOptVal = true;
RetVal->bOptLen = sizeof(bool);
RetVal->iOptLen = sizeof(int);
RetVal->AddressID = ID;
RetVal->step = stp;
RetVal->observer = this;
return RetVal;
};
void *argusModuleOnvifObserver::probeThreadProc( void *ptr ){
PROBE_CTX *me;
if (ptr == NULL) return NULL;
me = (PROBE_CTX *)ptr;
#ifdef DEBUG
//TLOG("Probe Request Sent");
#endif
me->RetVal = setsockopt(me->socketFD, SOL_SOCKET, SO_BROADCAST, (char*)&me->bOptVal, me->bOptLen);
me->RetVal = sendto(me->socketFD, me->send_buff, strlen(me->send_buff), 0, (struct sockaddr*)&me->r_address, sizeof(struct sockaddr_in));
#ifdef DEBUG
//sprintf(me->buff, "Sent %d Bytes", me->RetVal );
//TLOG(me->buff);
#endif
#ifdef __linux__
fcntl( me->socketFD, F_SETFL, fcntl(me->socketFD, F_GETFL, 0) | O_NONBLOCK );
#else
u_long val = 1;
ioctlsocket(me->socketFD, FIONBIO, &val);//none block
#endif
for (me->index = 1; me->index <= 10; me->index++){
memset(me->buff, 0, sizeof(me->buff));
me->RetVal = recvfrom(me->socketFD, me->buff, MAX_BUFF_SIZE, 0, NULL, NULL);
if( me->RetVal < 0 ){//SOCKET_ERROR){
SLEEP(1000);
continue;
};
me->recv_buff = me->buff;
XMLResults tParseResults;
XMLNode response = XMLNode::parseString( me->buff, "root", &tParseResults );
#ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
FILE *fd;
fd = fopen("ONVIF_MSGS.txt","a+");
fprintf(fd,me->buff);
fprintf(fd,"--------------------\r\n");
fclose(fd);
#endif
if( tParseResults.error ){
char tBuffer[CTEXT];
sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
TLOG_ERROR( tBuffer );
} else {
if(response.isEmpty()){
TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
continue;
} else {
XMLNode Body = response.getChildNode("Body", 0);
if(!Body.isEmpty()){
XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
if(!ProbeMatches.isEmpty()) {
int matches = ProbeMatches.nChildNode();
OBSERVER_RESULT *tRecord;
for(int index=0;indexresultID = me->AddressID;
if(!ProbeMatch.isEmpty()) {
{
XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
if(!node.isEmpty()) {
XMLNode nod = node.getChildNode("Address", 0);
char tBuf[CLOG];
sprintf(tBuf,"%s", nod.getText(0));
CArray *tArr;
tArr = explode(':',tBuf);
for(int j=0; jsize(); j++) {
if( strlen(tArr->at(j)) > 10 ) {
sprintf(tRecord->RefID,"%s",tArr->at(j));
break;
};
};
tArr->clear();
delete(tArr);
};
}
{
XMLNode node = ProbeMatch.getChildNode("Types", 0);
char tBuf[CLOG];
sprintf(tBuf,"%s", node.getText(0));
CArray *tArr;
tArr = explode(' ',tBuf);
sprintf(tRecord->Type,"%s",tArr->at(0));
tArr->clear();
delete(tArr);
}
{
XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
char tBuf[CLOG];
sprintf(tBuf,"%s", node.getText(0));
CArray *tArr;
tArr = explode(' ',tBuf);
if( tArr->size() > 0 ) {
for( int index = 0; index < tArr->size(); index++){
char *tPtr;
if( tPtr = strstr( tArr->at(index), "mac/" ) ){
sprintf(tRecord->MAC,"%s",tPtr+4);
};
if( tPtr = strstr( tArr->at(index), "hardware/" ) ){
sprintf(tRecord->Hardware,"%s",tPtr+9);
};
if( tPtr = strstr( tArr->at(index), "name/" ) ){
sprintf(tRecord->Name,"%s",tPtr+5);
};
if (tPtr = strstr(tArr->at(index), "MAC/")) {
sprintf(tRecord->MAC, "%s", tPtr + 4);
};
if (tPtr = strstr(tArr->at(index), "HARDWARE/")) {
sprintf(tRecord->Hardware, "%s", tPtr + 9);
};
if (tPtr = strstr(tArr->at(index), "NAME/")) {
sprintf(tRecord->Name, "%s", tPtr + 5);
};
};
tArr->clear();
};
delete(tArr);
}
{
XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
char tBuf[CLOG];
sprintf(tBuf,"%s", node.getText(0));
CArray *tArr;
tArr = explode(' ',tBuf);
if( tArr->size() > 0 ) {
for(int index=0; index < tArr->size(); index++) {
if( strstr( tArr->at(index), "[" ) != NULL ){
// IPv6
} else{
// IPv4
sprintf(tRecord->XAddrs,"%s",tArr->at(0));
};
};
tArr->clear();
};
delete(tArr);
}
};
me->observer->m_vResultPool.push_back(tRecord);
};
};
};
};
};
};
};
me->observer->m_nStatus += me->step;
#ifdef DEBUG
TLOG("Probe Finished");
#endif
#ifdef __linux__
close(me->socketFD);
#else
closesocket(me->socketFD);
#endif
free( me );
return NULL;
};
bool argusModuleOnvifObserver::startProbe(CSTR IP, int ID, int stp){
#ifdef DEBUG
LOG("Starting Probe!");
#endif
PROBE_CTX *tVal = init_PROBE_CTX(IP, ID, stp);
if( tVal == NULL ) return false;
OBSERVER_CTX *tThread;
tThread = (OBSERVER_CTX *) malloc(sizeof(OBSERVER_CTX));
if( tThread != NULL ){
memset( tThread, 0, sizeof(OBSERVER_CTX));
#ifdef __linux__
int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
if( RetVal == -1 ) {
LOG_ERROR("Thread Creation Faled.");
free(tVal);
free(tThread);
return false;
};
#else
tThread->m_hThreadHandle = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)probeThreadProc,
(LPVOID)tVal,
0,
&tThread->m_dwThreadID);
if( tThread->m_hThreadHandle==NULL ){
LOG_ERROR("Thread Creation Faled.");
free(tVal);
free(tThread);
return false;
};
#endif
tThread->context = tVal;
m_vThreads.push_back(tThread);
#ifdef DEBUG
LOG("Probe Created");
#endif
return true;
} else {
LOG_ERROR("Can not allocate Memory for Probe CTX");
free(tVal);
};
return false;
};
bool argusModuleOnvifObserver::validate(){
if( m_vResultPool.size() ){ // Clear structures
for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
#ifdef DEBUG
char tBuffer[256];
sprintf( tBuffer, "Found Camwera [%s] %s", (*index)->XAddrs, (*index)->RefID );
LOG( tBuffer );
#endif
uint32_t cid = m_pDBC->getCamId( (*index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID );
if( cid ) {
#ifdef DEBUG
sprintf( tBuffer, "Camera is Listed as %d", cid );
LOG( tBuffer );
#endif
m_pDBC->updateIPCam( m_pKernel->getSiteId(), cid, (*index)->XAddrs );// DiscoverResults->at(index));
} else {
// Uncknown camera - no checking
#ifdef DEBUG
LOG("Camera is Not Listed");
#endif
m_pDBC->addIPCam( m_pKernel->getSiteId(), (*index) );
};
free(*index);
}
m_vResultPool.clear();
#ifdef DEBUG_EXTRA
LOG("Clear Structs Done!");
#endif
} else {
#ifdef DEBUG_EXTRA
LOG("No Online IP Cameras in Network");
#endif
};
return true;
};
#undef LOG
#undef LOG_ERROR
#undef TLOG
#undef TLOG_ERROR