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.
255 lines
6.4 KiB
255 lines
6.4 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");
|
|
m_pIpAddresses = getLocalIP();
|
|
m_bWSAOK = false;
|
|
m_nStatus = 0;
|
|
#ifndef __linux__
|
|
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
|
|
}
|
|
|
|
bool onvifMediaObserver::startDiscovery(void)
|
|
{
|
|
m_nStatus = 0;
|
|
double dStatus, dStep;
|
|
dStatus = 0;
|
|
if (! m_bWSAOK)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// m_pDevice = OnvifClientDevice();
|
|
// OnvifClientSearch *OS;
|
|
|
|
// OS = new OnvifClientSearch( m_pDevice );
|
|
|
|
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;
|
|
};
|
|
};
|
|
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;
|
|
}
|
|
|
|
static int koko = 0;
|
|
|
|
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;
|
|
};
|
|
TLOG("Have Response");
|
|
me->recv_buff = me->buff;
|
|
// Debug
|
|
FILE * fd;
|
|
char tName[64];
|
|
sprintf(tName, "onvif_responce_%d.txt", koko++);
|
|
fd = fopen(tName, "wb+");
|
|
if (fd != NULL)
|
|
{
|
|
fprintf(fd, me->buff);
|
|
fclose(fd);
|
|
};
|
|
// Debug
|
|
{
|
|
XMLNode node = XMLNode::parseString(me->buff, "root");
|
|
if (node.isEmpty())
|
|
{
|
|
TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
|
|
continue;
|
|
};
|
|
{
|
|
XMLNode node2 = node.getChildNode("Body", 0);
|
|
{
|
|
XMLNode node3 = node2.getChildNode("ProbeMatches", 0);
|
|
{
|
|
XMLNode node4 = node3.getChildNode("ProbeMatch", 0);
|
|
int index = 0;
|
|
while (! node4.isEmpty())
|
|
{
|
|
XMLNode node5 = node4.getChildNode("Types", 0);
|
|
XMLNode node6 = node4.getChildNode("XAddrs", 0);
|
|
resultWoWo * tRecord;
|
|
tRecord = (resultWoWo *) malloc(sizeof(resultWoWo));
|
|
if (tRecord != NULL)
|
|
{
|
|
tRecord->wowoID = me->AddressID;
|
|
sprintf(tRecord->Types, "%s", node5.getText(0));
|
|
sprintf(tRecord->XAddrs, "%s", node6.getText(0));
|
|
me->observer->m_vResultPool.push_back(tRecord);
|
|
};
|
|
index++;
|
|
node4 = node3.getChildNode("ProbeMatch", index);
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
me->observer->m_nStatus += me->step;
|
|
TLOG("Probe Finished");
|
|
#ifdef __linux__
|
|
shutdown(me->socketFD, SHUT_RDWR); // SD_RECEIVE
|
|
close(me->socketFD);
|
|
#else
|
|
closesocket(me->socketFD);
|
|
#endif
|
|
free(me);
|
|
return NULL;
|
|
}
|
|
|
|
bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp)
|
|
{
|
|
probeWoWo * tVal;
|
|
LOG(IP);
|
|
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 *) this);
|
|
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
|