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.

248 lines
7.8 KiB

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->clear();
  181. delete(tArr);
  182. }
  183. };
  184. me->observer->m_vResultPool.push_back(tRecord);
  185. };
  186. };
  187. };
  188. };
  189. };
  190. };
  191. };
  192. me->observer->m_nStatus += me->step;
  193. //TLOG("Probe Finished");
  194. #ifdef __linux__
  195. close(me->socketFD);
  196. #else
  197. closesocket(me->socketFD);
  198. #endif
  199. free( me );
  200. return NULL;
  201. };
  202. bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp){
  203. probeWoWo *tVal;
  204. tVal = init_WoWo(IP, ID, stp);
  205. if (tVal == NULL) return false;
  206. onvifObserverThread *tThread;
  207. tThread = (onvifObserverThread *)malloc(sizeof(onvifObserverThread));
  208. if (tThread != NULL){
  209. #ifdef __linux__
  210. int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
  211. if( RetVal == -1 ) {
  212. LOG_ERROR("Thread Creation Faled.");
  213. return false;
  214. };
  215. #else
  216. tThread->m_hThreadHandle = CreateThread(
  217. NULL,
  218. 0,
  219. (LPTHREAD_START_ROUTINE)probeThreadProc,
  220. (LPVOID)tVal,
  221. 0,
  222. &tThread->m_dwThreadID);
  223. if( tThread->m_hThreadHandle==NULL ){
  224. free(tVal);
  225. free(tThread);
  226. return false;
  227. };
  228. #endif
  229. //LOG("Thread Started");
  230. tThread->context = tVal;
  231. m_vThreads.push_back(tThread);
  232. return true;
  233. };
  234. return false;
  235. };
  236. #undef LOG
  237. #undef LOG_ERROR
  238. #undef TLOG
  239. #undef TLOG_ERROR