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.

293 lines
10 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
​
​
​
​
​
​
​
​
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. //Must Be checked to not to take any VPN TUN devices!!
  25. m_pIpAddresses = getLocalIP();
  26. m_nStatus = 0;
  27. #ifdef __linux__
  28. m_bWSAOK = true;
  29. #else
  30. m_bWSAOK = false;
  31. WORD wVersionRequested;
  32. WSADATA wsaData;
  33. int err;
  34. wVersionRequested = MAKEWORD(2, 2);
  35. err = WSAStartup(wVersionRequested, &wsaData);
  36. if (err != 0){
  37. LOG_ERROR("WSAStartup failed with error");
  38. } else m_bWSAOK = true;
  39. #endif
  40. };
  41. bool onvifMediaObserver::startDiscovery(void){
  42. m_nStatus = 0;
  43. double dStatus, dStep;
  44. dStatus = 0;
  45. if ( !m_bWSAOK ) return false;
  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. } else {
  69. LOG_ERROR("No Networks Detected!");
  70. };
  71. } else {
  72. LOG_ERROR("ERROR FETCHING LOCAL IP ADDRESSES!" );
  73. }
  74. return false;
  75. };
  76. probeWoWo *onvifMediaObserver::init_WoWo(CSTR ADR, int ID, int stp){
  77. probeWoWo *RetVal;
  78. RetVal = (probeWoWo*)malloc(sizeof(probeWoWo));
  79. if (RetVal == NULL) return NULL;
  80. sprintf(RetVal->IpAddress, "%s", ADR);
  81. RetVal->Port = HTTPMU_HOST_PORT;
  82. RetVal->socketFD = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  83. RetVal->send_buff = (CSTR) SEARCH_REQUEST_STRING;
  84. RetVal->r_address.sin_family = AF_INET;
  85. RetVal->r_address.sin_port = htons(RetVal->Port);
  86. RetVal->r_address.sin_addr.s_addr = inet_addr(RetVal->IpAddress);
  87. RetVal->bOptVal = true;
  88. RetVal->bOptLen = sizeof(bool);
  89. RetVal->iOptLen = sizeof(int);
  90. RetVal->AddressID = ID;
  91. RetVal->step = stp;
  92. RetVal->observer = this;
  93. return RetVal;
  94. };
  95. void *onvifMediaObserver::probeThreadProc(void *ptr){
  96. probeWoWo *me;
  97. if (ptr == NULL) return NULL;
  98. me = (probeWoWo *)ptr;
  99. //TLOG("Probe Request Sent");
  100. me->RetVal = setsockopt(me->socketFD, SOL_SOCKET, SO_BROADCAST, (char*)&me->bOptVal, me->bOptLen);
  101. me->RetVal = sendto(me->socketFD, me->send_buff, strlen(me->send_buff), 0, (struct sockaddr*)&me->r_address, sizeof(struct sockaddr_in));
  102. //sprintf(me->buff, "Sent %d Bytes", me->RetVal );
  103. //TLOG(me->buff);
  104. #ifdef __linux__
  105. fcntl( me->socketFD, F_SETFL, fcntl(me->socketFD, F_GETFL, 0) | O_NONBLOCK );
  106. #else
  107. u_long val = 1;
  108. ioctlsocket(me->socketFD, FIONBIO, &val);//none block
  109. #endif
  110. for (me->index = 1; me->index <= 10; me->index++){
  111. memset(me->buff, 0, sizeof(me->buff));
  112. me->RetVal = recvfrom(me->socketFD, me->buff, MAX_BUFF_SIZE, 0, NULL, NULL);
  113. if( me->RetVal < 0 ){//SOCKET_ERROR){
  114. SLEEP(1000);
  115. continue;
  116. };
  117. me->recv_buff = me->buff;
  118. XMLResults tParseResults;
  119. XMLNode response = XMLNode::parseString( me->buff, "root", &tParseResults );
  120. #ifdef DEBUG_ONVIF_PRINTOUT_MESSAGES
  121. FILE *fd;
  122. fd = fopen("ONVIF_MSGS.txt","a+");
  123. fprintf(fd,me->buff);
  124. fprintf(fd,"--------------------\r\n");
  125. fclose(fd);
  126. #endif
  127. if( tParseResults.error ){
  128. char tBuffer[CTEXT];
  129. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  130. TLOG_ERROR( tBuffer );
  131. } else {
  132. if(response.isEmpty()){
  133. TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  134. continue;
  135. } else {
  136. XMLNode Body = response.getChildNode("Body", 0);
  137. if(!Body.isEmpty()){
  138. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
  139. if(!ProbeMatches.isEmpty()) {
  140. int matches = ProbeMatches.nChildNode();
  141. resultWoWo *tRecord;
  142. for(int index=0;index<matches;index++){
  143. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  144. tRecord = (resultWoWo *)malloc(sizeof(resultWoWo));
  145. if (tRecord != NULL){
  146. memset(tRecord, 0, sizeof(resultWoWo));
  147. tRecord->wowoID = me->AddressID;
  148. if(!ProbeMatch.isEmpty()) {
  149. {
  150. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  151. if(!node.isEmpty()) {
  152. XMLNode nod = node.getChildNode("Address", 0);
  153. char tBuf[CLOG];
  154. sprintf(tBuf,"%s", nod.getText(0));
  155. CArray *tArr;
  156. tArr = explode(':',tBuf);
  157. for(int j=0; j<tArr->size(); j++) {
  158. if( strlen(tArr->at(j)) > 10 ) {
  159. sprintf(tRecord->RefID,"%s",tArr->at(j));
  160. break;
  161. };
  162. };
  163. tArr->clear();
  164. delete(tArr);
  165. };
  166. }
  167. {
  168. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  169. char tBuf[CLOG];
  170. sprintf(tBuf,"%s", node.getText(0));
  171. CArray *tArr;
  172. tArr = explode(' ',tBuf);
  173. sprintf(tRecord->Type,"%s",tArr->at(0));
  174. tArr->clear();
  175. delete(tArr);
  176. }
  177. {
  178. XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
  179. char tBuf[CLOG];
  180. sprintf(tBuf,"%s", node.getText(0));
  181. CArray *tArr;
  182. tArr = explode(' ',tBuf);
  183. if( tArr->size() > 0 ) {
  184. for( int index = 0; index < tArr->size(); index++){
  185. char *tPtr;
  186. if( tPtr = strstr( tArr->at(index), "mac/" ) ){
  187. sprintf(tRecord->MAC,"%s",tPtr+4);
  188. };
  189. if( tPtr = strstr( tArr->at(index), "hardware/" ) ){
  190. sprintf(tRecord->Hardware,"%s",tPtr+9);
  191. };
  192. if( tPtr = strstr( tArr->at(index), "name/" ) ){
  193. sprintf(tRecord->Name,"%s",tPtr+5);
  194. };
  195. if (tPtr = strstr(tArr->at(index), "MAC/")) {
  196. sprintf(tRecord->MAC, "%s", tPtr + 4);
  197. };
  198. if (tPtr = strstr(tArr->at(index), "HARDWARE/")) {
  199. sprintf(tRecord->Hardware, "%s", tPtr + 9);
  200. };
  201. if (tPtr = strstr(tArr->at(index), "NAME/")) {
  202. sprintf(tRecord->Name, "%s", tPtr + 5);
  203. };
  204. };
  205. tArr->clear();
  206. };
  207. delete(tArr);
  208. }
  209. {
  210. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  211. char tBuf[CLOG];
  212. sprintf(tBuf,"%s", node.getText(0));
  213. CArray *tArr;
  214. tArr = explode(' ',tBuf);
  215. if( tArr->size() > 0 ) {
  216. for(int index=0; index < tArr->size(); index++) {
  217. if( strstr( tArr->at(index), "[" ) != NULL ){
  218. // IPv6
  219. } else{
  220. // IPv4
  221. sprintf(tRecord->XAddrs,"%s",tArr->at(0));
  222. };
  223. };
  224. tArr->clear();
  225. };
  226. delete(tArr);
  227. }
  228. };
  229. me->observer->m_vResultPool.push_back(tRecord);
  230. };
  231. };
  232. };
  233. };
  234. };
  235. };
  236. };
  237. me->observer->m_nStatus += me->step;
  238. //TLOG("Probe Finished");
  239. #ifdef __linux__
  240. close(me->socketFD);
  241. #else
  242. closesocket(me->socketFD);
  243. #endif
  244. free( me );
  245. return NULL;
  246. };
  247. bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp){
  248. probeWoWo *tVal;
  249. tVal = init_WoWo(IP, ID, stp);
  250. if (tVal == NULL) return false;
  251. onvifObserverThread *tThread;
  252. tThread = (onvifObserverThread *)malloc(sizeof(onvifObserverThread));
  253. if (tThread != NULL){
  254. #ifdef __linux__
  255. int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
  256. if( RetVal == -1 ) {
  257. LOG_ERROR("Thread Creation Faled.");
  258. return false;
  259. };
  260. #else
  261. tThread->m_hThreadHandle = CreateThread(
  262. NULL,
  263. 0,
  264. (LPTHREAD_START_ROUTINE)probeThreadProc,
  265. (LPVOID)tVal,
  266. 0,
  267. &tThread->m_dwThreadID);
  268. if( tThread->m_hThreadHandle==NULL ){
  269. free(tVal);
  270. free(tThread);
  271. return false;
  272. };
  273. #endif
  274. //LOG("Thread Started");
  275. tThread->context = tVal;
  276. m_vThreads.push_back(tThread);
  277. return true;
  278. };
  279. return false;
  280. };
  281. #undef LOG
  282. #undef LOG_ERROR
  283. #undef TLOG
  284. #undef TLOG_ERROR