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.

312 lines
11 KiB

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