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.
379 lines
10 KiB
379 lines
10 KiB
#include "onvifMediaObserver.h"
|
|
|
|
#define LOG(X) basicLogCollection::getInstance()->Log("onvifMediaObserver", this, X)
|
|
#define LOG_ERROR(X) basicLogCollection::getInstance()->Log("onvifMediaObserver", this, _LOG_ERROR, X)
|
|
#define TLOG(X) basicLogCollection::getInstance()->Log("TonvifMediaObserver", me, X)
|
|
#define TLOG_ERROR(X) basicLogCollection::getInstance()->Log("TonvifMediaObserver", me, _LOG_ERROR, X)
|
|
|
|
onvifMediaObserver * onvifMediaObserver::_instance = 0;
|
|
|
|
/**
|
|
* Returns the single instance of the object.
|
|
*/
|
|
onvifMediaObserver * onvifMediaObserver::getInstance()
|
|
{
|
|
// check if the instance has been created yet
|
|
if (0 == _instance)
|
|
{
|
|
// if not, then create it
|
|
_instance = new onvifMediaObserver;
|
|
}
|
|
// return the single instance
|
|
return _instance;
|
|
}
|
|
|
|
// Constructor implementation
|
|
inline onvifMediaObserver::onvifMediaObserver()
|
|
{
|
|
m_pLog = basicLogCollection::getInstance();
|
|
// m_pSettings = basicSettingsProvider::getInstance();
|
|
// m_pSettingsNode = m_pSettings->getSubNode(NULL, "ONVIF_MEDIA_OBSERVER");
|
|
// Must Be checked to not to take any VPN TUN devices!!
|
|
m_pIpAddresses = getLocalIP();
|
|
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
|
|
}
|
|
|
|
inline onvifMediaObserver::~onvifMediaObserver()
|
|
{
|
|
if (m_vThreads.size())
|
|
{ // Kill-Wait Started Probes
|
|
LOG("Have to Wait Probes!");
|
|
for (onvifObserverThreads::iterator index = m_vThreads.begin(); index < m_vThreads.end(); index++)
|
|
{
|
|
pthread_join((*index)->m_hThreadHandle, NULL);
|
|
};
|
|
m_vThreads.clear();
|
|
};
|
|
LOG("No More Probes!");
|
|
if (m_vResultPool.size())
|
|
{ // Clear structures
|
|
for (vectorWoWoResults::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++)
|
|
{
|
|
free(*index);
|
|
};
|
|
m_vResultPool.clear();
|
|
};
|
|
LOG("Data is Cleared!");
|
|
_instance = NULL;
|
|
}
|
|
|
|
bool onvifMediaObserver::startDiscovery(void)
|
|
{
|
|
m_nStatus = 0;
|
|
double dStatus, dStep;
|
|
dStatus = 0;
|
|
if (! m_bWSAOK)
|
|
{
|
|
return false;
|
|
}
|
|
if (m_vResultPool.size())
|
|
{ // Clear structures
|
|
for (vectorWoWoResults::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++)
|
|
{
|
|
free(*index);
|
|
};
|
|
m_vResultPool.clear();
|
|
};
|
|
if (m_pIpAddresses != NULL)
|
|
{
|
|
if (m_pIpAddresses->size() > 0)
|
|
{
|
|
// Start Reaper thread
|
|
dStep = 30.0 / (m_pIpAddresses->size() + 1);
|
|
// Start Discoverers on each broadcast IP
|
|
int id = 0;
|
|
for (localIpAddresses::iterator index = m_pIpAddresses->begin(); index < m_pIpAddresses->end(); index++)
|
|
{
|
|
if (strlen((*index)->broadcast))
|
|
{
|
|
startProbe((*index)->broadcast, id, (int) dStep);
|
|
}
|
|
dStatus += dStep;
|
|
m_nStatus = (int) dStatus;
|
|
id++;
|
|
};
|
|
startProbe(HTTPMU_HOST_ADDRESS, id, (int) dStep);
|
|
dStatus += dStep;
|
|
m_nStatus = (int) dStatus;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("No Networks Detected!");
|
|
};
|
|
}
|
|
else
|
|
{
|
|
LOG_ERROR("ERROR FETCHING LOCAL IP ADDRESSES!");
|
|
}
|
|
return false;
|
|
}
|
|
|
|
probeWoWo * onvifMediaObserver::init_WoWo(CSTR ADR, int ID, int stp)
|
|
{
|
|
probeWoWo * RetVal;
|
|
RetVal = (probeWoWo *) malloc(sizeof(probeWoWo));
|
|
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 * onvifMediaObserver::probeThreadProc(void * ptr)
|
|
{
|
|
probeWoWo * me;
|
|
if (ptr == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
me = (probeWoWo *) ptr;
|
|
// TLOG("Probe Request Sent");
|
|
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));
|
|
// sprintf(me->buff, "Sent %d Bytes", me->RetVal );
|
|
// TLOG(me->buff);
|
|
#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();
|
|
resultWoWo * tRecord;
|
|
for (int index = 0; index < matches; index++)
|
|
{
|
|
XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
|
|
tRecord = (resultWoWo *) malloc(sizeof(resultWoWo));
|
|
if (tRecord != NULL)
|
|
{
|
|
memset(tRecord, 0, sizeof(resultWoWo));
|
|
tRecord->wowoID = 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; j < tArr->size(); 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;
|
|
// TLOG("Probe Finished");
|
|
#ifdef __linux__
|
|
close(me->socketFD);
|
|
#else
|
|
closesocket(me->socketFD);
|
|
#endif
|
|
free(me);
|
|
return NULL;
|
|
}
|
|
|
|
bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp)
|
|
{
|
|
probeWoWo * tVal;
|
|
tVal = init_WoWo(IP, ID, stp);
|
|
if (tVal == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
onvifObserverThread * tThread;
|
|
tThread = (onvifObserverThread *) malloc(sizeof(onvifObserverThread));
|
|
if (tThread != NULL)
|
|
{
|
|
#ifdef __linux__
|
|
int RetVal = pthread_create(&tThread->m_hThreadHandle, NULL, probeThreadProc, (void *) tVal);
|
|
if (RetVal == -1)
|
|
{
|
|
LOG_ERROR("Thread Creation Faled.");
|
|
return false;
|
|
};
|
|
#else
|
|
tThread->m_hThreadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) probeThreadProc, (LPVOID) tVal, 0, &tThread->m_dwThreadID);
|
|
if (tThread->m_hThreadHandle == NULL)
|
|
{
|
|
free(tVal);
|
|
free(tThread);
|
|
return false;
|
|
};
|
|
#endif
|
|
// LOG("Thread Started");
|
|
tThread->context = tVal;
|
|
m_vThreads.push_back(tThread);
|
|
return true;
|
|
};
|
|
return false;
|
|
}
|
|
|
|
#undef LOG
|
|
#undef LOG_ERROR
|
|
#undef TLOG
|
|
#undef TLOG_ERROR
|