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.

283 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 = strcasestr( tArr->at(index), "mac/" ) ){
  186. sprintf(tRecord->MAC,"%s",tPtr+4);
  187. };
  188. if( tPtr = strcasestr( tArr->at(index), "hardware/" ) ){
  189. sprintf(tRecord->Hardware,"%s",tPtr+9);
  190. };
  191. if( tPtr = strcasestr( tArr->at(index), "name/" ) ){
  192. sprintf(tRecord->Name,"%s",tPtr+5);
  193. };
  194. };
  195. tArr->clear();
  196. };
  197. delete(tArr);
  198. }
  199. {
  200. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  201. char tBuf[CLOG];
  202. sprintf(tBuf,"%s", node.getText(0));
  203. CArray *tArr;
  204. tArr = explode(' ',tBuf);
  205. if( tArr->size() > 0 ) {
  206. for(int index=0; index < tArr->size(); index++) {
  207. if( strcasestr( tArr->at(index), "[" ) != NULL ){
  208. // IPv6
  209. } else{
  210. // IPv4
  211. sprintf(tRecord->XAddrs,"%s",tArr->at(0));
  212. };
  213. };
  214. tArr->clear();
  215. };
  216. delete(tArr);
  217. }
  218. };
  219. me->observer->m_vResultPool.push_back(tRecord);
  220. };
  221. };
  222. };
  223. };
  224. };
  225. };
  226. };
  227. me->observer->m_nStatus += me->step;
  228. //TLOG("Probe Finished");
  229. #ifdef __linux__
  230. close(me->socketFD);
  231. #else
  232. closesocket(me->socketFD);
  233. #endif
  234. free( me );
  235. return NULL;
  236. };
  237. bool onvifMediaObserver::startProbe(CSTR IP, int ID, int stp){
  238. probeWoWo *tVal;
  239. tVal = init_WoWo(IP, ID, stp);
  240. if (tVal == NULL) return false;
  241. onvifObserverThread *tThread;
  242. tThread = (onvifObserverThread *)malloc(sizeof(onvifObserverThread));
  243. if (tThread != NULL){
  244. #ifdef __linux__
  245. int RetVal = pthread_create( &tThread->m_hThreadHandle, NULL, probeThreadProc, ( void * ) tVal );
  246. if( RetVal == -1 ) {
  247. LOG_ERROR("Thread Creation Faled.");
  248. return false;
  249. };
  250. #else
  251. tThread->m_hThreadHandle = CreateThread(
  252. NULL,
  253. 0,
  254. (LPTHREAD_START_ROUTINE)probeThreadProc,
  255. (LPVOID)tVal,
  256. 0,
  257. &tThread->m_dwThreadID);
  258. if( tThread->m_hThreadHandle==NULL ){
  259. free(tVal);
  260. free(tThread);
  261. return false;
  262. };
  263. #endif
  264. //LOG("Thread Started");
  265. tThread->context = tVal;
  266. m_vThreads.push_back(tThread);
  267. return true;
  268. };
  269. return false;
  270. };
  271. #undef LOG
  272. #undef LOG_ERROR
  273. #undef TLOG
  274. #undef TLOG_ERROR