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

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