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.

250 lines
8.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
  1. #include "onvifMediaObserver.h"
  2. #define LOG( X ) basicLogCollection::getInstance()->Log( "onvifMediaObserver", this, X )
  3. #define LOG_ERROR( X ) basicLogCollection::getInstance()->Log( "onvifMediaObserver", this, _LOG_ERROR, X )
  4. #define TLOG( X ) basicLogCollection::getInstance()->Log( "TonvifMediaObserver", me, X )
  5. #define TLOG_ERROR( X ) basicLogCollection::getInstance()->Log( "TonvifMediaObserver", me, _LOG_ERROR, X )
  6. onvifMediaObserver* onvifMediaObserver::_instance = 0;
  7. /**
  8. * Returns the single instance of the object.
  9. */
  10. onvifMediaObserver* onvifMediaObserver::getInstance() {
  11. // check if the instance has been created yet
  12. if (0 == _instance) {
  13. // if not, then create it
  14. _instance = new onvifMediaObserver;
  15. }
  16. // return the single instance
  17. return _instance;
  18. };
  19. // Constructor implementation
  20. inline onvifMediaObserver::onvifMediaObserver(){
  21. m_pLog = basicLogCollection::getInstance();
  22. //m_pSettings = basicSettingsProvider::getInstance();
  23. //m_pSettingsNode = m_pSettings->getSubNode(NULL, "ONVIF_MEDIA_OBSERVER");
  24. m_pIpAddresses = getLocalIP();
  25. m_nStatus = 0;
  26. #ifdef __linux__
  27. m_bWSAOK = true;
  28. #else
  29. m_bWSAOK = false;
  30. WORD wVersionRequested;
  31. WSADATA wsaData;
  32. int err;
  33. wVersionRequested = MAKEWORD(2, 2);
  34. err = WSAStartup(wVersionRequested, &wsaData);
  35. if (err != 0){
  36. LOG_ERROR("WSAStartup failed with error");
  37. } else m_bWSAOK = true;
  38. #endif
  39. };
  40. bool onvifMediaObserver::startDiscovery(void){
  41. m_nStatus = 0;
  42. double dStatus, dStep;
  43. dStatus = 0;
  44. if ( !m_bWSAOK ) return false;
  45. if (m_vResultPool.size()){ // Clear structures
  46. for (vectorWoWoResults::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  47. free(*index);
  48. };
  49. m_vResultPool.clear();
  50. };
  51. if ( m_pIpAddresses != NULL){
  52. if (m_pIpAddresses->size() > 0){
  53. //Start Reaper thread
  54. dStep = 30.0 / (m_pIpAddresses->size() + 1);
  55. // Start Discoverers on each broadcast IP
  56. int id = 0;
  57. for (localIpAddresses::iterator index = m_pIpAddresses->begin(); index < m_pIpAddresses->end(); index++){
  58. if (strlen((*index)->broadcast)) startProbe((*index)->broadcast, id, (int) dStep );
  59. dStatus += dStep;
  60. m_nStatus = (int)dStatus;
  61. id++;
  62. };
  63. startProbe(HTTPMU_HOST_ADDRESS, id, (int)dStep);
  64. dStatus += dStep;
  65. m_nStatus = (int)dStatus;
  66. return true;
  67. } else {
  68. LOG_ERROR("No Networks Detected!");
  69. };
  70. } else {
  71. LOG_ERROR("ERROR FETCHING LOCAL IP ADDRESSES!" );
  72. }
  73. return false;
  74. };
  75. probeWoWo *onvifMediaObserver::init_WoWo(CSTR ADR, int ID, int stp){
  76. probeWoWo *RetVal;
  77. RetVal = (probeWoWo*)malloc(sizeof(probeWoWo));
  78. if (RetVal == NULL) return NULL;
  79. sprintf(RetVal->IpAddress, "%s", ADR);
  80. RetVal->Port = HTTPMU_HOST_PORT;
  81. RetVal->socketFD = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  82. RetVal->send_buff = (CSTR) SEARCH_REQUEST_STRING;
  83. RetVal->r_address.sin_family = AF_INET;
  84. RetVal->r_address.sin_port = htons(RetVal->Port);
  85. RetVal->r_address.sin_addr.s_addr = inet_addr(RetVal->IpAddress);
  86. RetVal->bOptVal = true;
  87. RetVal->bOptLen = sizeof(bool);
  88. RetVal->iOptLen = sizeof(int);
  89. RetVal->AddressID = ID;
  90. RetVal->step = stp;
  91. RetVal->observer = this;
  92. return RetVal;
  93. };
  94. void *onvifMediaObserver::probeThreadProc(void *ptr){
  95. probeWoWo *me;
  96. if (ptr == NULL) return NULL;
  97. me = (probeWoWo *)ptr;
  98. //TLOG("Probe Request Sent");
  99. me->RetVal = setsockopt(me->socketFD, SOL_SOCKET, SO_BROADCAST, (char*)&me->bOptVal, me->bOptLen);
  100. me->RetVal = sendto(me->socketFD, me->send_buff, strlen(me->send_buff), 0, (struct sockaddr*)&me->r_address, sizeof(struct sockaddr_in));
  101. //sprintf(me->buff, "Sent %d Bytes", me->RetVal );
  102. //TLOG(me->buff);
  103. #ifdef __linux__
  104. fcntl( me->socketFD, F_SETFL, fcntl(me->socketFD, F_GETFL, 0) | O_NONBLOCK );
  105. #else
  106. u_long val = 1;
  107. ioctlsocket(me->socketFD, FIONBIO, &val);//none block
  108. #endif
  109. for (me->index = 1; me->index <= 10; me->index++){
  110. memset(me->buff, 0, sizeof(me->buff));
  111. me->RetVal = recvfrom(me->socketFD, me->buff, MAX_BUFF_SIZE, 0, NULL, NULL);
  112. if( me->RetVal < 0 ){//SOCKET_ERROR){
  113. SLEEP(1000);
  114. continue;
  115. };
  116. me->recv_buff = me->buff;
  117. XMLResults tParseResults;
  118. XMLNode response = XMLNode::parseString( me->buff, "root", &tParseResults );
  119. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  120. TLOG("RCPT:");
  121. printf(me->buff);
  122. #endif
  123. if( tParseResults.error ){
  124. char tBuffer[CTEXT];
  125. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  126. TLOG_ERROR( tBuffer );
  127. } else {
  128. if(response.isEmpty()){
  129. TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  130. continue;
  131. } else {
  132. XMLNode Body = response.getChildNode("Body", 0);
  133. if(!Body.isEmpty()){
  134. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
  135. if(!ProbeMatches.isEmpty()) {
  136. int matches = ProbeMatches.nChildNode();
  137. resultWoWo *tRecord;
  138. for(int index=0;index<matches;index++){
  139. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  140. tRecord = (resultWoWo *)malloc(sizeof(resultWoWo));
  141. if (tRecord != NULL){
  142. memset(tRecord, 0, sizeof(resultWoWo));
  143. tRecord->wowoID = me->AddressID;
  144. if(!ProbeMatch.isEmpty()) {
  145. {
  146. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  147. if(!node.isEmpty()) {
  148. XMLNode nod = node.getChildNode("Address", 0);
  149. char tBuf[CLOG];
  150. sprintf(tBuf,"%s", nod.getText(0));
  151. CArray *tArr;
  152. tArr = explode(':',tBuf);
  153. for(int j=0; j<tArr->size(); j++) {
  154. if( strlen(tArr->at(j)) > 10 ) {
  155. sprintf(tRecord->RefID,"%s",tArr->at(j));
  156. break;
  157. };
  158. };
  159. tArr->clear();
  160. delete(tArr);
  161. };
  162. }
  163. {
  164. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  165. char tBuf[CLOG];
  166. sprintf(tBuf,"%s", node.getText(0));
  167. CArray *tArr;
  168. tArr = explode(' ',tBuf);
  169. sprintf(tRecord->Type,"%s",tArr->at(0));
  170. tArr->clear();
  171. delete(tArr);
  172. }
  173. {
  174. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  175. char tBuf[CLOG];
  176. sprintf(tBuf,"%s", node.getText(0));
  177. CArray *tArr;
  178. //tArr = explode('/',tBuf);
  179. //sprintf(tRecord->XAddrs,"%s",tArr->at(2));
  180. tArr = explode(' ',tBuf);
  181. sprintf(tRecord->XAddrs,"%s",tArr->at(0));
  182. tArr->clear();
  183. delete(tArr);
  184. }
  185. };
  186. me->observer->m_vResultPool.push_back(tRecord);
  187. };
  188. };
  189. };
  190. };
  191. };
  192. };
  193. };
  194. me->observer->m_nStatus += me->step;
  195. //TLOG("Probe Finished");
  196. #ifdef __linux__
  197. close(me->socketFD);
  198. #else
  199. closesocket(me->socketFD);
  200. #endif
  201. free( me );
  202. return NULL;
  203. };
  204. bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp){
  205. probeWoWo *tVal;
  206. tVal = init_WoWo(IP, ID, stp);
  207. if (tVal == NULL) return false;
  208. onvifObserverThread *tThread;
  209. tThread = (onvifObserverThread *)malloc(sizeof(onvifObserverThread));
  210. if (tThread != NULL){
  211. #ifdef __linux__
  212. int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
  213. if( RetVal == -1 ) {
  214. LOG_ERROR("Thread Creation Faled.");
  215. return false;
  216. };
  217. #else
  218. tThread->m_hThreadHandle = CreateThread(
  219. NULL,
  220. 0,
  221. (LPTHREAD_START_ROUTINE)probeThreadProc,
  222. (LPVOID)tVal,
  223. 0,
  224. &tThread->m_dwThreadID);
  225. if( tThread->m_hThreadHandle==NULL ){
  226. free(tVal);
  227. free(tThread);
  228. return false;
  229. };
  230. #endif
  231. //LOG("Thread Started");
  232. tThread->context = tVal;
  233. m_vThreads.push_back(tThread);
  234. return true;
  235. };
  236. return false;
  237. };
  238. #undef LOG
  239. #undef LOG_ERROR
  240. #undef TLOG
  241. #undef TLOG_ERROR