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.

292 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. 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. FILE *fd;
  121. fd = fopen("ONVIF_MSGS.txt","a+");
  122. fprintf(fd,me->buff);
  123. fprintf(fd,"--------------------\r\n");
  124. fclose(fd);
  125. #endif
  126. if( tParseResults.error ){
  127. char tBuffer[CTEXT];
  128. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  129. TLOG_ERROR( tBuffer );
  130. } else {
  131. if(response.isEmpty()){
  132. TLOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  133. continue;
  134. } else {
  135. XMLNode Body = response.getChildNode("Body", 0);
  136. if(!Body.isEmpty()){
  137. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
  138. if(!ProbeMatches.isEmpty()) {
  139. int matches = ProbeMatches.nChildNode();
  140. resultWoWo *tRecord;
  141. for(int index=0;index<matches;index++){
  142. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  143. tRecord = (resultWoWo *)malloc(sizeof(resultWoWo));
  144. if (tRecord != NULL){
  145. memset(tRecord, 0, sizeof(resultWoWo));
  146. tRecord->wowoID = me->AddressID;
  147. if(!ProbeMatch.isEmpty()) {
  148. {
  149. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  150. if(!node.isEmpty()) {
  151. XMLNode nod = node.getChildNode("Address", 0);
  152. char tBuf[CLOG];
  153. sprintf(tBuf,"%s", nod.getText(0));
  154. CArray *tArr;
  155. tArr = explode(':',tBuf);
  156. for(int j=0; j<tArr->size(); j++) {
  157. if( strlen(tArr->at(j)) > 10 ) {
  158. sprintf(tRecord->RefID,"%s",tArr->at(j));
  159. break;
  160. };
  161. };
  162. tArr->clear();
  163. delete(tArr);
  164. };
  165. }
  166. {
  167. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  168. char tBuf[CLOG];
  169. sprintf(tBuf,"%s", node.getText(0));
  170. CArray *tArr;
  171. tArr = explode(' ',tBuf);
  172. sprintf(tRecord->Type,"%s",tArr->at(0));
  173. tArr->clear();
  174. delete(tArr);
  175. }
  176. {
  177. XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
  178. char tBuf[CLOG];
  179. sprintf(tBuf,"%s", node.getText(0));
  180. CArray *tArr;
  181. tArr = explode(' ',tBuf);
  182. if( tArr->size() > 0 ) {
  183. for( int index = 0; index < tArr->size(); index++){
  184. char *tPtr;
  185. if( tPtr = strstr( tArr->at(index), "mac/" ) ){
  186. sprintf(tRecord->MAC,"%s",tPtr+4);
  187. };
  188. if( tPtr = strstr( tArr->at(index), "hardware/" ) ){
  189. sprintf(tRecord->Hardware,"%s",tPtr+9);
  190. };
  191. if( tPtr = strstr( tArr->at(index), "name/" ) ){
  192. sprintf(tRecord->Name,"%s",tPtr+5);
  193. };
  194. if (tPtr = strstr(tArr->at(index), "MAC/")) {
  195. sprintf(tRecord->MAC, "%s", tPtr + 4);
  196. };
  197. if (tPtr = strstr(tArr->at(index), "HARDWARE/")) {
  198. sprintf(tRecord->Hardware, "%s", tPtr + 9);
  199. };
  200. if (tPtr = strstr(tArr->at(index), "NAME/")) {
  201. sprintf(tRecord->Name, "%s", tPtr + 5);
  202. };
  203. };
  204. tArr->clear();
  205. };
  206. delete(tArr);
  207. }
  208. {
  209. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  210. char tBuf[CLOG];
  211. sprintf(tBuf,"%s", node.getText(0));
  212. CArray *tArr;
  213. tArr = explode(' ',tBuf);
  214. if( tArr->size() > 0 ) {
  215. for(int index=0; index < tArr->size(); index++) {
  216. if( strstr( tArr->at(index), "[" ) != NULL ){
  217. // IPv6
  218. } else{
  219. // IPv4
  220. sprintf(tRecord->XAddrs,"%s",tArr->at(0));
  221. };
  222. };
  223. tArr->clear();
  224. };
  225. delete(tArr);
  226. }
  227. };
  228. me->observer->m_vResultPool.push_back(tRecord);
  229. };
  230. };
  231. };
  232. };
  233. };
  234. };
  235. };
  236. me->observer->m_nStatus += me->step;
  237. //TLOG("Probe Finished");
  238. #ifdef __linux__
  239. close(me->socketFD);
  240. #else
  241. closesocket(me->socketFD);
  242. #endif
  243. free( me );
  244. return NULL;
  245. };
  246. bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp){
  247. probeWoWo *tVal;
  248. tVal = init_WoWo(IP, ID, stp);
  249. if (tVal == NULL) return false;
  250. onvifObserverThread *tThread;
  251. tThread = (onvifObserverThread *)malloc(sizeof(onvifObserverThread));
  252. if (tThread != NULL){
  253. #ifdef __linux__
  254. int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
  255. if( RetVal == -1 ) {
  256. LOG_ERROR("Thread Creation Faled.");
  257. return false;
  258. };
  259. #else
  260. tThread->m_hThreadHandle = CreateThread(
  261. NULL,
  262. 0,
  263. (LPTHREAD_START_ROUTINE)probeThreadProc,
  264. (LPVOID)tVal,
  265. 0,
  266. &tThread->m_dwThreadID);
  267. if( tThread->m_hThreadHandle==NULL ){
  268. free(tVal);
  269. free(tThread);
  270. return false;
  271. };
  272. #endif
  273. //LOG("Thread Started");
  274. tThread->context = tVal;
  275. m_vThreads.push_back(tThread);
  276. return true;
  277. };
  278. return false;
  279. };
  280. #undef LOG
  281. #undef LOG_ERROR
  282. #undef TLOG
  283. #undef TLOG_ERROR