Onvif開發之客戶端搜索篇


關於ONVIF的廣播,有客戶端搜索和服務端發現的區別:客戶端向固定的網段和固定的端口發送廣播消息,服務端在對應的端口回復廣播請求消息
本文首先介紹客戶端如何進行廣播的已經對廣播回復的信息的基本處理。

客戶端這里處理相對服務端比較復雜一點,需要注意幾個地方:

1 廣播的ip和端口號(注意這個端口號和通信的端口是不一樣的,通信端口號可以自己隨意定一個,但是這個廣播的是ONVIF協議定好公用的)

  ip:239:255:255:250   port:3702

2 MessageID ,此ID每次都需要不同,不然每次只有第一次能搜索到設備,第二次就不行了!一般情況下都是區uuid的隨機值的,但是我在開發的過程是去的一個隨機值加上自己設備的mac地址組合的

3 soap_send___wsdd__Probe函數,發送廣播信息,通過它來判斷是否發送成功,確認是否繼續下一步操作進行接收消息

4 soap_recv___wsdd__ProbeMatches函數,此函數是通過廣播后,設備通過onvif協議回復了設備的一些基本信息,客戶端通過循 環調用此函數來接多個設備回復的各個設備的一些基本信息包括設備ip以及通信的prot

知道了這幾個基本的注意點和前篇介紹的代碼生成框架,接下來就是敲代碼了。該main函數登場了

[cpp]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <span style="font-size:14px;">/* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  main.c 
  5.  *    Description:  簡單例程測試:客戶端通過ONVIF協議搜索前端設備, 
  6.  *        Created:  2013年12月26日 12時17分48秒 
  7.  *       Compiler:  gcc 
  8.  *         Author:  max_min_,  
  9.  * 
  10.  * ===================================================================================== 
  11.  */  
  12. #include "wsdd.h"  
  13. #include <stdio.h>  
  14.   
  15. static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout)  
  16. {  
  17.     struct soap *soap = NULL;   
  18.     unsigned char macaddr[6];  
  19.     char _HwId[1024];  
  20.     unsigned int Flagrand;  
  21.     soap = soap_new();  
  22.     if(soap == NULL)  
  23.     {     
  24.         printf("[%d]soap = NULL\n", __LINE__);  
  25.         return NULL;  
  26.     }     
  27.      soap_set_namespaces( soap, namespaces);  
  28.     //超過5秒鍾沒有數據就退出  
  29.     if (timeout > 0)  
  30.     {     
  31.         soap->recv_timeout = timeout;  
  32.         soap->send_timeout = timeout;  
  33.         soap->connect_timeout = timeout;  
  34.     }     
  35.     else  
  36.     {     
  37.         //如果外部接口沒有設備默認超時時間的話,我這里給了一個默認值10s  
  38.         soap->recv_timeout    = 10;   
  39.         soap->send_timeout    = 10;   
  40.         soap->connect_timeout = 10;   
  41.     }     
  42.    soap_default_SOAP_ENV__Header(soap, header);  
  43.   
  44.     // 為了保證每次搜索的時候MessageID都是不相同的!因為簡單,直接取了隨機值  
  45.     srand((int)time(0));  
  46.     Flagrand = rand()%9000 + 1000; //保證四位整數  
  47.     macaddr[0] = 0x1; macaddr[1] = 0x2; macaddr[2] = 0x3; macaddr[3] = 0x4; macaddr[4] = 0x5; macaddr[5] = 0x6;  
  48.     sprintf(_HwId,"urn:uuid:%ud68a-1dd2-11b2-a105-%02X%02X%02X%02X%02X%02X",  
  49.             Flagrand, macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);  
  50.     header->wsa__MessageID =(char *)malloc( 100);  
  51.     memset(header->wsa__MessageID, 0, 100);  
  52.     strncpy(header->wsa__MessageID, _HwId, strlen(_HwId));  
  53.   
  54.     if (was_Action != NULL)  
  55.     {  
  56.         header->wsa__Action =(char *)malloc(1024);  
  57.         memset(header->wsa__Action, '\0', 1024);  
  58.         strncpy(header->wsa__Action, was_Action, 1024);//"http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";  
  59.     }  
  60.     if (was_To != NULL)  
  61.     {  
  62.         header->wsa__To =(char *)malloc(1024);  
  63.         memset(header->wsa__To, '\0', 1024);  
  64.         strncpy(header->wsa__To,  was_To, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery";     
  65.     }  
  66.     soap->header = header;  
  67.     return soap;  
  68. }  
  69. int ONVIF_ClientDiscovery( )  
  70. {  
  71.     int HasDev = 0;  
  72.     int retval = SOAP_OK;  
  73.     wsdd__ProbeType req;  
  74.     struct __wsdd__ProbeMatches resp;  
  75.     wsdd__ScopesType sScope;  
  76.     struct SOAP_ENV__Header header;  
  77.     struct soap* soap;  
  78.   
  79.   
  80.     const char *was_To = "urn:schemas-xmlsoap-org:ws:2005:04:discovery";  
  81.     const char *was_Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";  
  82.     //這個就是傳遞過去的組播的ip地址和對應的端口發送廣播信息      
  83.     const char *soap_endpoint = "soap.udp://239.255.255.250:3702/";  
  84.   
  85.     //這個接口填充一些信息並new返回一個soap對象,本來可以不用額外接口,  
  86.     // 但是后期會作其他操作,此部分剔除出來后面的操作就相對簡單了,只是調用接口就好  
  87.     soap = ONVIF_Initsoap(&header, was_To, was_Action, 5);  
  88.   
  89.     soap_default_SOAP_ENV__Header(soap, &header);  
  90.     soap->header = &header;  
  91.   
  92.     soap_default_wsdd__ScopesType(soap, &sScope);  
  93.     sScope.__item = "";  
  94.     soap_default_wsdd__ProbeType(soap, &req);  
  95.     req.Scopes = &sScope;  
  96.     req.Types = ""; //"dn:NetworkVideoTransmitter";  
  97.   
  98.     retval = soap_send___wsdd__Probe(soap, soap_endpoint, NULL, &req);  
  99.     //發送組播消息成功后,開始循環接收各位設備發送過來的消息  
  100.     while (retval == SOAP_OK)  
  101.     {  
  102.         retval = soap_recv___wsdd__ProbeMatches(soap, &resp);  
  103.         if (retval == SOAP_OK)  
  104.         {  
  105.             if (soap->error)  
  106.             {  
  107.                 printf("[%d]: recv error:%d,%s,%s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));  
  108.                 retval = soap->error;  
  109.             }  
  110.             else //成功接收某一個設備的消息  
  111.             {  
  112.                 HasDev ++;  
  113.                 if (resp.wsdd__ProbeMatches->ProbeMatch != NULL && resp.wsdd__ProbeMatches->ProbeMatch->XAddrs != NULL)  
  114.                 {  
  115.                     printf(" ################  recv  %d devices info #### \n", HasDev );  
  116.                     printf("Target Service Address  : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->XAddrs);  
  117.                     printf("Target EP Address       : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference.Address);  
  118.                     printf("Target Type             : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Types);  
  119.                     printf("Target Metadata Version : %d\r\n", resp.wsdd__ProbeMatches->ProbeMatch->MetadataVersion);  
  120.                     sleep(1);  
  121.                 }  
  122.             }  
  123.         }  
  124.         else if (soap->error)  
  125.         {  
  126.             if (HasDev == 0)  
  127.             {  
  128.                 printf("[%s][%d] Thers Device discovery or soap error: %d, %s, %s \n", __func__, __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstrin  
  129.                 retval = soap->error;  
  130.             }  
  131.             else  
  132.             {  
  133.                 printf(" [%s]-[%d] Search end! It has Searched %d devices! \n", __func__, __LINE__, HasDev);  
  134.                 retval = 0;  
  135.             }  
  136.             break;  
  137.         }  
  138.     }  
  139.   
  140.     soap_destroy(soap);  
  141.     soap_end(soap);  
  142.     soap_free(soap);  
  143.   
  144.     return retval;  
  145. }  
  146. int main(void )  
  147. {  
  148.     //組播接口   
  149.     if (ONVIF_ClientDiscovery() != 0 )  
  150.     {  
  151.         printf("discovery failed!\n");  
  152.         return -1;  
  153.     }  
  154.   
  155.     return 0;  
  156. }  
  157. </span>  

運行測試結果如下圖

              

 

可以看到注意信息就是http://172.18.13.127:9000/onvif/device_service這句了!設備的IP地址以及通信端口都有了,第三個沒有端口,也就是使用默認的端口80了,海康和大華的貌似都是用80端口
到這里差不多客戶端的搜索就完成了,剩下只是把搜索到的設備信息解析出來,然后保存起來!提供給客戶端使用了,我在實際開發的過程中是通過調用外層接口傳遞一個回調函數指針進去,然后匹配信息出來,給客戶端!

搜索使用的源碼下載地址:onvif客戶端搜索


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM