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.

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