Onvif备忘录(2)----客户端端设备发现

续上篇,框架代码生成之后,就可以进行基于Onvif的开发了,先实现一个简单的设备发现的例子。 VisualStudio中新建一个空白工程,将上篇中生成的框架代码添加进来,但soapServer.c文件中定义了许多要实现的函数,直接添加编译会报一大堆错误,且本例中只需要用到Client的功能,so 。。。 删掉soapServer.c, 留下soapClient.c即可。

工程结构如下:

Onvif备忘录(2)----客户端端设备发现_第1张图片         


main.c代码如下:   

[cpp] view plain copy
  1. #include "wsdd.h"  
  2. #include <stdio.h>  
  3.   
  4. static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout)  
  5. {  
  6.     struct soap *soap = NULL;   
  7.     unsigned char macaddr[6];  
  8.     char _HwId[1024];  
  9.     unsigned int Flagrand;  
  10.     soap = soap_new();  
  11.     if(soap == NULL)  
  12.     {  
  13.         printf("[%d]soap = NULL\n", __LINE__);  
  14.         return NULL;  
  15.     }  
  16.       
  17.     soap_set_namespaces( soap, namespaces);  
  18.   
  19.     if (timeout > 0)  
  20.     {  
  21.         soap->recv_timeout = timeout;  
  22.         soap->send_timeout = timeout;  
  23.         soap->connect_timeout = timeout;  
  24.     }  
  25.     else  
  26.     {  
  27.         //Maximum wait time: 20S   
  28.         soap->recv_timeout    = 20;  
  29.         soap->send_timeout    = 20;  
  30.         soap->connect_timeout = 20;  
  31.     }  
  32.     soap_default_SOAP_ENV__Header(soap, header);  
  33.   
  34.     // Create SessionID randomly  
  35.     srand((int)time(0));  
  36.     Flagrand = rand()%9000 + 8888;   
  37.     macaddr[0] = 0x1; macaddr[1] = 0x2; macaddr[2] = 0x3; macaddr[3] = 0x4; macaddr[4] = 0x5; macaddr[5] = 0x6;  
  38.     sprintf(_HwId,"urn:uuid:%ud68a-1dd2-11b2-a105-%02X%02X%02X%02X%02X%02X",   
  39.             Flagrand, macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);  
  40.     header->wsa__MessageID =(char *)malloc( 100);  
  41.     memset(header->wsa__MessageID, 0, 100);  
  42.     strncpy(header->wsa__MessageID, _HwId, strlen(_HwId));  
  43.   
  44.     if (was_Action != NULL)  
  45.     {  
  46.         header->wsa__Action =(char *)malloc(1024);  
  47.         memset(header->wsa__Action, '\0', 1024);  
  48.         strncpy(header->wsa__Action, was_Action, 1024);//"http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";  
  49.     }  
  50.     if (was_To != NULL)  
  51.     {  
  52.         header->wsa__To =(char *)malloc(1024);  
  53.         memset(header->wsa__To, '\0', 1024);  
  54.         strncpy(header->wsa__To,  was_To, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery";      
  55.     }  
  56.     soap->header = header;  
  57.     return soap;  
  58. }   
  59.   
  60. int ONVIF_ClientDiscovery( )  
  61. {  
  62.     int FoundDevNo = 0;  
  63.     int retval = SOAP_OK;  
  64.     wsdd__ProbeType req;         
  65.     struct __wsdd__ProbeMatches resp;  
  66.     wsdd__ScopesType sScope;  
  67.     struct SOAP_ENV__Header header;  
  68.     struct soap* soap;  
  69.       
  70.   
  71.     const char *was_To = "urn:schemas-xmlsoap-org:ws:2005:04:discovery";  
  72.     const char *was_Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";  
  73.     //IP Adress and PortNo, broadCast     
  74.     const char *soap_endpoint = "soap.udp://239.255.255.250:3702/";  
  75.   
  76.     //Create new soap object with info  
  77.     soap = ONVIF_Initsoap(&header, was_To, was_Action, 10);  
  78.       
  79.     soap_default_SOAP_ENV__Header(soap, &header);  
  80.     soap->header = &header;  
  81.   
  82.     soap_default_wsdd__ScopesType(soap, &sScope);  
  83.     sScope.__item = "";  
  84.     soap_default_wsdd__ProbeType(soap, &req);  
  85.     req.Scopes = &sScope;  
  86.     req.Types = ""//"dn:NetworkVideoTransmitter";  
  87.       
  88.     //sent the message broadcast and wait  
  89.     retval = soap_send___wsdd__Probe(soap, soap_endpoint, NULL, &req);            
  90.     while (retval == SOAP_OK)  
  91.     {  
  92.         retval = soap_recv___wsdd__ProbeMatches(soap, &resp);  
  93.         if (retval == SOAP_OK)   
  94.         {  
  95.             if (soap->error)  
  96.             {  
  97.                 printf("[%d]: recv soap error :%d, %s, %s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));   
  98.                 retval = soap->error;  
  99.             }  
  100.             else //we find a device  
  101.             {  
  102.                 FoundDevNo ++;  
  103.                 if (resp.wsdd__ProbeMatches->ProbeMatch != NULL && resp.wsdd__ProbeMatches->ProbeMatch->XAddrs != NULL)  
  104.                 {  
  105.                     printf("****** No %d Devices Information ******\n", FoundDevNo );  
  106.                     printf("Device Service Address  : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->XAddrs);    
  107.                     printf("Device EP Address       : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference.Address);    
  108.                     printf("Device Type             : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Types);    
  109.                     printf("Device Metadata Version : %d\r\n", resp.wsdd__ProbeMatches->ProbeMatch->MetadataVersion);    
  110.                     //sleep(1);  
  111.                 }  
  112.             }  
  113.         }  
  114.         else if (soap->error)    
  115.         {    
  116.             if (FoundDevNo == 0)  
  117.             {  
  118.                 printf("No Device found!\n");   
  119.                 retval = soap->error;    
  120.             }  
  121.             else   
  122.             {  
  123.                 printf("Search end! Find %d Device! \n", FoundDevNo);  
  124.                 retval = 0;  
  125.             }  
  126.             break;  
  127.         }    
  128.     }  
  129.   
  130.     soap_destroy(soap);   
  131.     soap_end(soap);   
  132.     soap_free(soap);  
  133.       
  134.     return retval;  
  135. }  
  136.   
  137. int main(void )  
  138. {  
  139.   
  140.     if (ONVIF_ClientDiscovery() != 0 )  
  141.     {  
  142.         printf("discovery failed!\n");  
  143.         return -1;  
  144.     }  
  145.   
  146.     return 0;  
  147. }  

Onvif相机接上路由器,运行程序之后,成功检测到该Camera,详细信息如下图:(整个工程已经上传,需要的请狂点 我的资源下载)

Onvif备忘录(2)----客户端端设备发现_第2张图片



       转自:http://blog.csdn.net/saloon_yuan/article/details/27524875

你可能感兴趣的:(Onvif备忘录(2)----客户端端设备发现)