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.

624 lines
24 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "kernelModuleOnvifObserver.h"
  2. #include "hardwareManager.h"
  3. #include "hardwareResourceNetworkProvider.h"
  4. #define SEARCH_REQUEST_STRING "<?xml version=\"1.0\" encoding=\"UTF-8\"?><e:Envelope xmlns:e=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:w=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:d=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:dn=\"http://www.onvif.org/ver10/network/wsdl\"><e:Header><w:MessageID>uuid:84ede3de-7dec-11d0-c360-f01234567890</w:MessageID><w:To e:mustUnderstand=\"true\">urn:schemas-xmlsoap-org:ws:2005:04:discovery</w:To><w:Action a:mustUnderstand=\"true\">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</w:Action></e:Header><e:Body><d:Probe><d:Types>dn:NetworkVideoTransmitter</d:Types></d:Probe></e:Body></e:Envelope>"
  5. #define SEARCH_REQUEST_STRING2323 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" \
  6. "<SOAP:Envelope xmlns:SOAP=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\" xmlns:DISCOVERY=\"http://schemas.xmlsoap.org/ws/2005/04/discovery\" xmlns:ssdp=\"http://example.com/wsdp/sample/print\">\n" \
  7. "<SOAP:Header>\n" \
  8. "<wsa:MessageID>urn:uuid:ac9823ee-6813-498c-8c1b-6272a22353f7</wsa:MessageID>\n" \
  9. "<wsa:Action>\n" \
  10. "http ://schemas.xmlsoap.org/ws/2005/04/discovery/Probe\n" \
  11. "</wsa:Action>\n" \
  12. "<wsa:To>urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>\n" \
  13. "</SOAP:Header>\n" \
  14. "<SOAP:Body>\n" \
  15. "<DISCOVERY:Probe>\n" \
  16. "<DISCOVERY:Types>ssdp:any</DISCOVERY:Types>\n" \
  17. "</DISCOVERY:Probe>\n" \
  18. "</SOAP:Body>\n" \
  19. "</SOAP:Envelope>\r\n\r\n"
  20. #define HTTP_OK "200 OK"
  21. /******************************************************************
  22. ** Action Defines *
  23. *******************************************************************/
  24. #define HTTP_HEADER_ACTION "POST %s HTTP/1.1\r\n" \
  25. "HOST: %s:%u\r\n" \
  26. "SOAPACTION:\"%s#%s\"\r\n" \
  27. "CONTENT-TYPE: text/xml ; charset=\"utf-8\"\r\n"\
  28. "Content-Length: %d \r\n\r\n"
  29. #define SOAP_ACTION "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" \
  30. "<s:Envelope xmlns:s=" \
  31. "\"http://schemas.xmlsoap.org/soap/envelope/\" " \
  32. "s:encodingStyle=" \
  33. "\"http://schemas.xmlsoap.org/soap/encoding/\">\r\n" \
  34. "<s:Body>\r\n" \
  35. "<u:%s xmlns:u=\"%s\">\r\n%s" \
  36. "</u:%s>\r\n" \
  37. "</s:Body>\r\n" \
  38. "</s:Envelope>\r\n"
  39. #define PORT_MAPPING_LEASE_TIME "63072000" //two year
  40. #define ADD_PORT_MAPPING_PARAMS "<NewRemoteHost></NewRemoteHost>\r\n" \
  41. "<NewExternalPort>%u</NewExternalPort>\r\n"\
  42. "<NewProtocol>%s</NewProtocol>\r\n" \
  43. "<NewInternalPort>%u</NewInternalPort>\r\n"\
  44. "<NewInternalClient>%s</NewInternalClient>\r\n" \
  45. "<NewEnabled>1</NewEnabled>\r\n" \
  46. "<NewPortMappingDescription>%s</NewPortMappingDescription>\r\n" \
  47. "<NewLeaseDuration>" \
  48. PORT_MAPPING_LEASE_TIME \
  49. "</NewLeaseDuration>\r\n"
  50. #define LOG( X ) kernelModule::m_pLog->Log( "MOD_ONVIF", this, X )
  51. #ifdef DEBUG
  52. #define LOG_ERROR( X ) {\
  53. char _tBfrErrMSG2w34[256];\
  54. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  55. kernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  56. }
  57. #else
  58. #define LOG_ERROR( X ) kernelModule::m_pLog->Log( "MOD_ONVIF", this, _LOG_ERROR, X );
  59. #endif
  60. kernelModuleOnvifObserver::kernelModuleOnvifObserver( uint32_t ModuleID ): kernelModule( ModuleID, KERNEL_MODULE_ONVIF_OBSERVER ), basicThread(){
  61. m_bEnabled = false;
  62. m_pEventHeap = basicEventHeap::getInstance();
  63. m_bCurrentNIF = 0;
  64. m_bProbeIsActive = false;
  65. m_bDetached = false;
  66. #ifdef __linux__
  67. m_bWSAOK = true;
  68. #else
  69. m_bWSAOK = false;
  70. WORD wVersionRequested;
  71. WSADATA wsaData;
  72. int err;
  73. wVersionRequested = MAKEWORD(2, 2);
  74. err = WSAStartup(wVersionRequested, &wsaData);
  75. if (err != 0){
  76. LOG_ERROR("WSAStartup failed with error");
  77. } else m_bWSAOK = true;
  78. #endif
  79. };
  80. kernelModuleOnvifObserver::~kernelModuleOnvifObserver(){
  81. if( !m_bDetached ) stopService( true );
  82. };
  83. bool kernelModuleOnvifObserver::startService(){
  84. #ifdef DEBUG_EXTRA
  85. LOG("Starting Service!");
  86. #endif
  87. if( !m_bWSAOK ) return false;
  88. m_bEnabled = reloadSettings();
  89. return init();
  90. };
  91. bool kernelModuleOnvifObserver::stopService( bool force ){
  92. m_bEnabled = false;
  93. if( !m_bDetached ) m_bDetached = uninit();
  94. if( m_bDetached ) {
  95. //Clear Results
  96. m_mResultProtector.lock();
  97. if (m_vResultPool.size()){ // Clear structures
  98. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  99. free(*index);
  100. };
  101. m_vResultPool.clear();
  102. };
  103. m_mResultProtector.unlock();
  104. };
  105. return m_bDetached;
  106. };
  107. /*
  108. #include <stdio.h>
  109. #include <stdint.h>
  110. #include <string.h>
  111. #include <sys/types.h>
  112. #include <sys/socket.h>
  113. #include <linux/in.h>
  114. #include <linux/if_ether.h>
  115. #include <net/if.h>
  116. #include <errno.h>
  117. // Join multicast group, support both IPv4 and IPv6. * @param sockfd socket descripter * @param grp sockaddr * @param grplen sockaddr length * @return 0:on success; non-zero:on failed
  118. static int mcast_join(int sockfd, const struct sockaddr *grp, socklen_t grplen){
  119. #if MCAST_JOIN_GROUP
  120. struct group_req req;
  121. req.gr_interface = 0;
  122. if (grplen > sizeof(req.gr_group)) {
  123. errno = EINVAL;
  124. return -1;
  125. }
  126. memcpy(&req.gr_group, grp, grplen);
  127. return (setsockopt(sockfd, IPPROTO_IP, MCAST_JOIN_GROUP, &req, sizeof(req)));
  128. #else
  129. switch (grp->sa_family) {
  130. case AF_INET: {
  131. struct ip_mreq mreq;
  132. struct ifreq ifreq;
  133. memcpy(&mreq.imr_multiaddr, &((const struct sockaddr_in *) grp)->sin_addr, sizeof(struct in_addr));
  134. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  135. return (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  136. &mreq, sizeof(mreq)));
  137. }
  138. #ifdef IPV6
  139. #ifndef IPV6_JOIN_GROUP // APIv0 compatibility
  140. #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
  141. #endif
  142. case AF_INET6: {
  143. struct ipv6_mreq mreq6;
  144. memcpy(&mreq6.ipv6mr_multiaddr, &((const struct sockaddr_in6 *) grp)->sin6_addr, sizeof(struct in6_addr));
  145. mreq6.ipv6mr_interface = 0;
  146. return (setsockopt(sockfd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6, sizeof(mreq6)));
  147. }
  148. #endif
  149. default:
  150. errno = EAFNOSUPPORT;
  151. return -1;
  152. }
  153. #endif
  154. };
  155. int main(int argc, char **argv){
  156. int fd = socket(AF_INET, SOCK_DGRAM, 0);
  157. struct sockaddr_in grp;
  158. bzero(&grp, sizeof(grp));
  159. grp.sin_family = AF_INET;
  160. grp.sin_addr.s_addr = inet_addr("224.0.0.1");;
  161. if (mcast_join(fd, (struct sockaddr*)(&grp), sizeof(grp)))
  162. perror("Join multicast group failed");
  163. }
  164. */
  165. void kernelModuleOnvifObserver::m_fThreadProc(){
  166. int socketFD;
  167. int RetVal;
  168. struct sockaddr_in r_address;
  169. bool bOptVal;
  170. int bOptLen;
  171. int iOptLen;
  172. uint16_t bCounterTO = 600; // 3 second
  173. char rBuffer[MAX_BUFF_SIZE];
  174. #ifdef DEBUG_EXTRA
  175. char tB[128];
  176. LOG("Module Started!");
  177. #endif
  178. m_bDetached = false;
  179. m_bThreadStatus = TH_WORKING;
  180. while( m_bThreadStatus < TH_ENDED ) {
  181. if ( m_bEnabled ) {
  182. if ( m_bProbeIsActive ) {
  183. if( socketFD > -1 ){
  184. if( bCounterTO-- ){
  185. memset( rBuffer, 0, MAX_BUFF_SIZE );
  186. #ifdef DEBUG_EXTRA
  187. struct sockaddr src_addr;
  188. socklen_t addrlen = sizeof( src_addr );
  189. RetVal = recvfrom( socketFD, rBuffer, MAX_BUFF_SIZE, 0, &src_addr, &addrlen );
  190. if ( RetVal > -1 ) {
  191. sprintf(tB, "(%d){%d}MSG[%d] from %s", bCounterTO, socketFD, RetVal, inet_ntoa(((struct sockaddr_in *)&src_addr)->sin_addr));
  192. LOG( tB );
  193. LOG( rBuffer );
  194. };
  195. #else //http://192.168.200.127/axis-cgi/com/ptz.cgi?info=1&camera=1
  196. RetVal = recvfrom( socketFD, rBuffer, MAX_BUFF_SIZE, 0, NULL, NULL );
  197. #endif
  198. if ((RetVal == -1) && /*(errno != EAGAIN) &&*/ (errno != EINTR) && (errno != EWOULDBLOCK)) {
  199. LOG_ERROR("Error acures while reading UDP package");
  200. close( socketFD );
  201. m_bProbeIsActive = false;
  202. continue; // Error ocured but continue to next address
  203. };
  204. if( RetVal < 1 ){
  205. //LOG("SLEEP");
  206. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  207. continue; // No Data - wait to next time
  208. };
  209. OBSERVER_RESULT *tResult = parseXML( rBuffer );
  210. if ( tResult ) {
  211. m_mResultProtector.lock();
  212. m_vResultPool.push_back( tResult );
  213. m_mResultProtector.unlock();
  214. #ifdef DEBUG_EXTRA
  215. LOG("Probe Finished!");
  216. #endif
  217. } else {
  218. #ifdef DEBUG_EXTRA
  219. LOG("Probe Finished with unrecognized data!");
  220. #endif
  221. };
  222. continue; //To Fetch Another responces
  223. #ifdef DEBUG_EXTRA
  224. } else {
  225. LOG("Probe Finished By Timeout!");
  226. #endif
  227. };
  228. #ifdef __linux__
  229. close(socketFD);
  230. #else
  231. closesocket(socketFD);
  232. #endif
  233. };
  234. m_bProbeIsActive = false;
  235. //Sleep 5 minutes if done all scans
  236. if ( m_bCurrentNIF == 0 ){
  237. for( int i=12000; i, m_bThreadStatus < TH_ENDED; i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  238. };
  239. } else {
  240. // start new probe;
  241. m_mNIFMUTEX.lock();
  242. if ( m_vNIFIPS.size() ) {
  243. #ifdef DEBUG_EXTRA
  244. sprintf( tB, "Starting probe on %s address", m_vNIFIPS.at(m_bCurrentNIF) );
  245. LOG( tB );
  246. #endif
  247. socketFD = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  248. if( socketFD > -1 ){
  249. CSTR send_buff = (CSTR)SEARCH_REQUEST_STRING;
  250. r_address.sin_family = AF_INET;
  251. r_address.sin_port = htons(HTTPMU_HOST_PORT);
  252. r_address.sin_addr.s_addr = inet_addr(m_vNIFIPS.at(m_bCurrentNIF));
  253. bOptVal = true;
  254. #ifdef DEBUG_EXTRA
  255. LOG("Set Broadcast Flag");
  256. #endif
  257. RetVal = setsockopt( socketFD, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen );
  258. struct group_req req;
  259. req.gr_interface = 0;
  260. memcpy( &req.gr_group, &r_address, sizeof(r_address));
  261. #ifdef DEBUG_EXTRA
  262. LOG("Join Broadcast Group");
  263. #endif
  264. RetVal = setsockopt( socketFD, IPPROTO_IP, MCAST_JOIN_GROUP, (char*)&req, sizeof(req) );
  265. //The broadcast address to be used may be determined by issuing an ioctl() with the SIOCGIFBRDADDR request.
  266. if ( RetVal > -1 ) {
  267. LOG_ERROR("Setting Socked as Broadcasted Error!");
  268. };
  269. //
  270. struct sockaddr_in tServerAddr;
  271. memset( &tServerAddr, 0, sizeof(tServerAddr) );
  272. tServerAddr.sin_family = AF_INET; // IPv4
  273. tServerAddr.sin_addr.s_addr = inet_addr( "0.0.0.0" ); //INADDR_ANY;
  274. tServerAddr.sin_port = htons(rand( 20000, 40000));
  275. // Bind the socket with the server address
  276. RetVal = bind( socketFD, (const struct sockaddr *)&tServerAddr, sizeof(tServerAddr));
  277. if ( RetVal > -1 ) {
  278. LOG_ERROR("Bind Socked Error!");
  279. };
  280. #ifdef DEBUG_EXTRA
  281. LOG("Sending Probe Request");
  282. #endif
  283. RetVal = sendto( socketFD, send_buff, strlen(send_buff), 0, (struct sockaddr*)&r_address, sizeof(struct sockaddr_in));
  284. if ( RetVal > -1 ) {
  285. #ifdef DEBUG_EXTRA
  286. sprintf( tB, "Sent %d Bytes of %d", RetVal, strlen(send_buff) );
  287. LOG( tB );
  288. #endif
  289. #ifdef __linux__
  290. fcntl( socketFD, F_SETFL, fcntl( socketFD, F_GETFL, 0) | O_NONBLOCK );
  291. #else
  292. u_long val = 1;
  293. ioctlsocket( socketFD, FIONBIO, &val );//none block
  294. #endif
  295. bCounterTO = 600;
  296. m_bProbeIsActive = true;
  297. } else {
  298. #ifdef DEBUG_EXTRA
  299. // Failure to send probe package
  300. LOG_ERROR("Error in sending Probe Request");
  301. #endif
  302. #ifdef __linux__
  303. close(socketFD);
  304. #else
  305. closesocket(socketFD);
  306. #endif
  307. };
  308. };
  309. m_bCurrentNIF++;
  310. if ( m_bCurrentNIF == m_vNIFIPS.size() ) m_bCurrentNIF = 0;
  311. };
  312. m_mNIFMUTEX.unlock();
  313. };
  314. } else {
  315. //Sleep 3 Sec.
  316. for( int i=300; i, m_bThreadStatus < TH_ENDED; i-- ) SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  317. };
  318. };
  319. m_bDetached = true;
  320. };
  321. bool kernelModuleOnvifObserver::dispatchEvent(EVENT_CONTAINER *Evt){
  322. if(!m_bEnabled) {
  323. #ifdef DEBUG_EXTRA
  324. LOG("Module Disabled!");
  325. #endif
  326. return true;
  327. }
  328. if( Evt == NULL ) return false;
  329. return true;
  330. };
  331. bool kernelModuleOnvifObserver::reloadSettings(){
  332. m_mNIFMUTEX.lock();
  333. if( m_vNIFIPS.size() ) {
  334. for ( vectorCSTR::iterator idx=m_vNIFIPS.begin(); idx < m_vNIFIPS.end(); idx++ ) free(*idx);
  335. m_vNIFIPS.clear();
  336. };
  337. m_mNIFMUTEX.unlock();
  338. #ifdef DEBUG_EXTRA
  339. LOG("Check IPS");
  340. #endif
  341. hardwareManager *tHWM = hardwareManager::getInstance();
  342. if( tHWM != NULL ) {
  343. #ifdef DEBUG_EXTRA
  344. LOG("Check IPS 11");
  345. #endif
  346. DBC_INT_LIST *rIDS = tHWM->requestResourcesByType( HARDWARE_PROVIDER_NETWORK );
  347. if( rIDS != NULL ) {
  348. #ifdef DEBUG_EXTRA
  349. LOG("Check IPS 22");
  350. #endif
  351. for( DBC_INT_LIST::iterator idx = rIDS->begin(); idx < rIDS->end(); idx++ ){
  352. #ifdef DEBUG_EXTRA
  353. LOG("Check IPS 33");
  354. #endif
  355. hardwareResourceNetwork *tRES = ( hardwareResourceNetwork *) tHWM->requestResource( HARDWARE_PROVIDER_NETWORK, *idx );
  356. if( tRES != NULL ) {
  357. #ifdef DEBUG_EXTRA
  358. LOG("Have NIF Resource");
  359. #endif
  360. CSTR BC = tRES->getBroadcast();
  361. if( BC!= NULL ) {
  362. #ifdef DEBUG_EXTRA
  363. LOG("Have BC Address");
  364. LOG(BC);
  365. #endif
  366. m_mNIFMUTEX.lock();
  367. m_vNIFIPS.push_back( BC );
  368. m_mNIFMUTEX.unlock();
  369. #ifdef DEBUG_EXTRA
  370. LOG("NIF defined");
  371. #endif
  372. }
  373. #ifdef DEBUG_EXTRA
  374. else {
  375. LOG("NO BC Address");
  376. }
  377. #endif
  378. ;
  379. } else {
  380. LOG_ERROR("Cannot Get Resource!");
  381. };
  382. };
  383. delete( rIDS );
  384. } else {
  385. LOG_ERROR("No Networks Detected!");
  386. m_bEnabled = false;
  387. };
  388. } else {
  389. LOG_ERROR("No HW Manager");
  390. };
  391. #ifdef DEBUG_EXTRA
  392. LOG("Default Probe");
  393. LOG( HTTPMU_HOST_ADDRESS );
  394. #endif
  395. CSTR tBC = (CSTR) malloc(16);
  396. if ( tBC ) {
  397. sprintf(tBC, HTTPMU_HOST_ADDRESS );
  398. m_mNIFMUTEX.lock();
  399. m_vNIFIPS.push_back( tBC );
  400. m_mNIFMUTEX.unlock();
  401. } else {
  402. LOG_ERROR("Malloc Error!");
  403. };
  404. return true;
  405. };
  406. OBSERVER_RESULT *kernelModuleOnvifObserver::parseXML( CSTR data ){
  407. OBSERVER_RESULT *retVal = NULL;
  408. if( data!=NULL ){
  409. XMLResults tParseResults;
  410. XMLNode response = XMLNode::parseString( data, "root", &tParseResults );
  411. if( tParseResults.error ){
  412. char tBuffer[CTEXT];
  413. sprintf( tBuffer, "XML Parse Error %d at %d:%d", tParseResults.error, tParseResults.nLine, tParseResults.nColumn );
  414. LOG_ERROR( tBuffer );
  415. } else {
  416. if( response.isEmpty() ){
  417. LOG_ERROR("The device descripe XML file is not a valid XML file. Cann't find root element.");
  418. } else {
  419. #ifdef DEBUG_EXTRA
  420. LOG("Probe XML parsed");
  421. #endif
  422. XMLNode Body = response.getChildNode( "Body", 0 );
  423. if(!Body.isEmpty()){
  424. #ifdef DEBUG_EXTRA
  425. LOG("Have Body elm");
  426. #endif
  427. XMLNode ProbeMatches = Body.getChildNode("ProbeMatches", 0);//EndpointReference
  428. if(!ProbeMatches.isEmpty()) {
  429. #ifdef DEBUG_EXTRA
  430. LOG("Have ProbeMathes elm");
  431. #endif
  432. int matches = ProbeMatches.nChildNode();
  433. for(int index=0;index<matches;index++){
  434. XMLNode ProbeMatch = ProbeMatches.getChildNode("ProbeMatch", index);
  435. retVal = (OBSERVER_RESULT *)malloc(sizeof(OBSERVER_RESULT));
  436. if( retVal != NULL ){
  437. memset( retVal, 0, sizeof(OBSERVER_RESULT));
  438. if(!ProbeMatch.isEmpty()) {
  439. #ifdef DEBUG_EXTRA
  440. LOG("Have ProbeMatch elm");
  441. #endif
  442. {
  443. XMLNode node = ProbeMatch.getChildNode("EndpointReference", 0);
  444. if(!node.isEmpty()) {
  445. #ifdef DEBUG_EXTRA
  446. LOG("Have ProbeMatch EndpointReference");
  447. #endif
  448. XMLNode nod = node.getChildNode("Address", 0);
  449. if(!nod.isEmpty()){
  450. char tBuf[CLOG];
  451. sprintf(tBuf,"%s", nod.getText(0));
  452. CArray *tArr;
  453. #ifdef DEBUG_EXTRA
  454. LOG("Before Explode");
  455. LOG(tBuf);
  456. #endif
  457. tArr = explode(':',tBuf);
  458. #ifdef DEBUG_EXTRA
  459. LOG("After Explode");
  460. #endif
  461. for(int j=0; j<tArr->size(); j++) {
  462. if( strlen(tArr->at(j)) > 10 ) {
  463. sprintf(retVal->RefID,"%s",tArr->at(j));
  464. break;
  465. };
  466. };
  467. tArr->clear();
  468. delete(tArr);
  469. };
  470. };
  471. }
  472. {
  473. XMLNode node = ProbeMatch.getChildNode("Types", 0);
  474. if( !node.isEmpty() ){
  475. char tBuf[CLOG];
  476. sprintf(tBuf,"%s", node.getText(0));
  477. CArray *tArr;
  478. tArr = explode(' ',tBuf);
  479. sprintf(retVal->Type,"%s",tArr->at(0));
  480. tArr->clear();
  481. delete(tArr);
  482. }
  483. }
  484. {
  485. XMLNode node = ProbeMatch.getChildNode("Scopes", 0);
  486. char tBuf[CLOG];
  487. sprintf( tBuf,"%s", node.getText(0) );
  488. CArray *tArr = explode(' ',tBuf);
  489. if( tArr->size() > 0 ) {
  490. for( int index = 0; index < tArr->size(); index++){
  491. char *tPtr;
  492. if( tPtr = strstr( tArr->at(index), "mac/" ) ){
  493. sprintf(retVal->MAC,"%s",tPtr+4);
  494. };
  495. if( tPtr = strstr( tArr->at(index), "hardware/" ) ){
  496. sprintf(retVal->Hardware,"%s",tPtr+9);
  497. };
  498. if( tPtr = strstr( tArr->at(index), "name/" ) ){
  499. sprintf(retVal->Name,"%s",tPtr+5);
  500. };
  501. if (tPtr = strstr(tArr->at(index), "MAC/")) {
  502. sprintf(retVal->MAC, "%s", tPtr + 4);
  503. };
  504. if (tPtr = strstr(tArr->at(index), "HARDWARE/")) {
  505. sprintf(retVal->Hardware, "%s", tPtr + 9);
  506. };
  507. if (tPtr = strstr(tArr->at(index), "NAME/")) {
  508. sprintf(retVal->Name, "%s", tPtr + 5);
  509. };
  510. };
  511. tArr->clear();
  512. };
  513. delete(tArr);
  514. }
  515. {
  516. XMLNode node = ProbeMatch.getChildNode("XAddrs", 0);
  517. char tBuf[1024];
  518. sprintf(tBuf,"%s", node.getText(0));
  519. #ifdef DEBUG_EXTRA
  520. LOG("Before Explode");
  521. LOG(tBuf);
  522. #endif
  523. CArray *tArr;
  524. tArr = explode(' ',tBuf);
  525. if( tArr->size() > 0 ) {
  526. for( int index=0; index < tArr->size(); index++ ){
  527. if( strstr( tArr->at(index), "[" ) != NULL ){
  528. // IPv6
  529. } else {
  530. // IPv4
  531. sprintf( retVal->XAddrs,"%s",tArr->at(0));
  532. };
  533. };
  534. tArr->clear();
  535. };
  536. delete(tArr);
  537. }
  538. };
  539. };
  540. };
  541. };
  542. };
  543. };
  544. };
  545. };
  546. return retVal;
  547. };
  548. bool kernelModuleOnvifObserver::validate(){
  549. m_mResultProtector.lock();
  550. if( m_vResultPool.size() ){ // Clear structures
  551. for( vectorOBSERVER_RESULT::iterator index = m_vResultPool.begin(); index < m_vResultPool.end(); index++){
  552. #ifdef DEBUG_EXTRA
  553. char tBuffer[256];
  554. sprintf( tBuffer, "Found Camwera [%s] %s", (*index)->XAddrs, (*index)->RefID );
  555. LOG( tBuffer );
  556. #endif
  557. uint32_t cid = m_pDBC->getCamId( (*index)->RefID ); //isCameraListed( DiscoverResults->at(index)->RefID );
  558. if( cid ) {
  559. #ifdef DEBUG_EXTRA
  560. sprintf( tBuffer, "Camera is Listed as %d", cid );
  561. LOG( tBuffer );
  562. #endif
  563. m_pDBC->updateIPCam( cid, (*index)->XAddrs );// DiscoverResults->at(index));
  564. } else {
  565. // Uncknown camera - no checking
  566. #ifdef DEBUG_EXTRA
  567. LOG("Camera is Not Listed");
  568. #endif
  569. m_pDBC->addIPCam( (*index) );
  570. };
  571. free(*index);
  572. }
  573. m_vResultPool.clear();
  574. #ifdef DEBUG_EXTRA
  575. LOG("Clear Structs Done!");
  576. #endif
  577. } else {
  578. #ifdef DEBUG_EXTRA
  579. LOG("No Online IP Cameras in Network");
  580. #endif
  581. };
  582. m_mResultProtector.unlock();
  583. return true;
  584. };
  585. #undef LOG
  586. #undef LOG_ERROR