onvif規範的實現:server端Discovery實現,通過OnvifTestTool12.06測試
【http://blog.csdn.net/ghostyu/article/details/8182516】
說明:網上關於ONVIF開發的文章並不多,也更找不到具體的例項來入門學習。只能靠翻閱各種Specification摸索中前進,下面是最近幾天的成果。調通了服務端(或者說裝置端)的Discovery,使用OnvifTestTool12.06能夠搜到我的裝置。【來自http://blog.csdn.net/ghostyu】
1、在使用wsdl2h產生標頭檔案前需要修改typemap.dat,
修改的依據在這裡:http://www.cs.fsu.edu/~engelen/soap.html,在FAQ頁面下的How do I use gSOAP for the ONVIF specifications?
- #Use gSOAP 2.8.10 and up. In the typemap.dat file used by wsdl2h, add:
- # ONVIF recommended prefixes
- tds = "http://www.onvif.org/ver10/device/wsdl"
- tev = "http://www.onvif.org/ver10/events/wsdl"
- tls = "http://www.onvif.org/ver10/display/wsdl"
- tmd = "http://www.onvif.org/ver10/deviceIO/wsdl"
- timg = "http://www.onvif.org/ver20/imaging/wsdl"
- trt = "http://www.onvif.org/ver10/media/wsdl"
- tptz = "http://www.onvif.org/ver20/ptz/wsdl"
- trv = "http://www.onvif.org/ver10/receiver/wsdl"
- trc = "http://www.onvif.org/ver10/recording/wsdl"
- tse = "http://www.onvif.org/ver10/search/wsdl"
- trp = "http://www.onvif.org/ver10/replay/wsdl"
- tan = "http://www.onvif.org/ver20/analytics/wsdl"
- tad = "http://www.onvif.org/ver10/analyticsdevice/wsdl"
- tdn = "http://www.onvif.org/ver10/network/wsdl"
- tt = "http://www.onvif.org/ver10/schema"
- # OASIS recommended prefixes
- wsnt = "http://docs.oasis-open.org/wsn/b-2"
- wsntw = "http://docs.oasis-open.org/wsn/bw-2"
- wsrfbf = "http://docs.oasis-open.org/wsrf/bf-2"
- wsrfr = "http://docs.oasis-open.org/wsrf/r-2"
- wsrfrw = "http://docs.oasis-open.org/wsrf/rw-2"
- wstop = "http://docs.oasis-open.org/wsn/t-1"
- # WS-Discovery 1.0 remapping
- wsdd10__HelloType = | wsdd__HelloType
- wsdd10__ByeType = | wsdd__ByeType
- wsdd10__ProbeType = | wsdd__ProbeType
- wsdd10__ProbeMatchesType = | wsdd__ProbeMatchesType
- wsdd10__ProbeMatchType = | wsdd__ProbeMatchType
- wsdd10__ResolveType = | wsdd__ResolveType
- wsdd10__ResolveMatchesType = | wsdd__ResolveMatchesType
- wsdd10__ResolveMatchType = | wsdd__ResolveMatchType
- # SOAP-ENV mapping
- SOAP_ENV__Envelope = struct SOAP_ENV__Envelope { struct SOAP_ENV__Header *SOAP_ENV__Header; _XML SOAP_ENV__Body; }; | struct SOAP_ENV__Envelope
- SOAP_ENV__Header = | struct SOAP_ENV__Header
- SOAP_ENV__Fault = | struct SOAP_ENV__Fault
- SOAP_ENV__Detail = | struct SOAP_ENV__Detail
- SOAP_ENV__Code = | struct SOAP_ENV__Code
- SOAP_ENV__Subcode = | struct SOAP_ENV__Subcode
- SOAP_ENV__Reason = | struct SOAP_ENV__Reason
2、根據onvif官網提供的remotediscovery.wsdl產生onvif.h標頭檔案
關於onvif所有的wsdl都在這裡:http://www.onvif.org/Documents/Specifications.aspx中的ONVIF WSDL and XML Schemas Specifications一節,雖然可以全部下載為wsdl檔案,但是wsdl檔案中存在相互依賴的關係,並且是帶有儲存的依賴,所以最好直接使用url來產生標頭檔案,不要下載下來。
- wsdl2h -o onvif.h -c -s -t ./typemap.dat http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl
3、使用onvif.h來產生骨架程式碼
- soapcpp2 -c onvif.h -x -I /root/onvif/gsoap-2.8/gsoap/import -I /root/onvif/gsoap-2.8/gsoap/
4、ProbeMatches程式碼
這樣就建立了基本的服務端和客戶端的程式碼了,下面需要新增具體的程式碼了。
其中包括:
(1)建立組播用的udp socket,繫結組播地址為239.255.255.250,埠為3702,因為ws-discovery的組播地址和埠就是為239.255.255.250和3702
(2)在產生的Probe函式中新增ProbeMatches程式碼
首先是udp socket
- int bind_server_udp1(int server_s)
- {
- struct sockaddr_in local_addr;
- memset(&local_addr,0,sizeof(local_addr));
- local_addr.sin_family = AF_INET;
- local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- local_addr.sin_port = htons(3702);
- return bind(server_s,(struct sockaddr*)&local_addr,sizeof(local_addr));
- }
- static int create_server_socket_udp(void)
- {
- int server_udp;
- unsigned char one = 1;
- int sock_opt = 1;
- //server_udp = socket(PF_INET, SOCK_DGRAM, 0);
- server_udp = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (server_udp == -1) {
- printf("unable to create socket\n");
- }
- /* reuse socket addr */
- if ((setsockopt(server_udp, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
- sizeof (sock_opt))) == -1) {
- printf("setsockopt\n");
- }
- if ((setsockopt(server_udp, IPPROTO_IP, IP_MULTICAST_LOOP,
- &one, sizeof (unsigned char))) == -1) {
- printf("setsockopt\n");
- }
- struct ip_mreq mreq;
- mreq.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
- mreq.imr_interface.s_addr = htonl(INADDR_ANY);
- if(setsockopt(server_udp,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq))==-1){
- perror("memberchip error\n");
- }
- return server_udp;
- }
其次是新增ProbeMatches程式碼
(1)首先複製client的soap_send___wsdd__ProbeMatches函式到服務端來,因為soap_send___wsdd__ProbeMatches已經寫好了用於響應Probe訊息的框架了,不用白不用啊。
(2)編寫__wsdd__Probe函式,新增如下內容
- int __wsdd__Probe(struct soap* soap, struct wsdd__ProbeType *wsdd__Probe)
- {
- DBG("__wsdd__Probe\n");
- char macaddr[6];
- char _IPAddr[INFO_LENGTH];
- char _HwId[1024];
- wsdd__ProbeMatchesType ProbeMatches;
- ProbeMatches.ProbeMatch = (struct wsdd__ProbeMatchType *)soap_malloc(soap, sizeof(struct wsdd__ProbeMatchType));
- ProbeMatches.ProbeMatch->XAddrs = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
- ProbeMatches.ProbeMatch->Types = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
- ProbeMatches.ProbeMatch->Scopes = (struct wsdd__ScopesType*)soap_malloc(soap,sizeof(struct wsdd__ScopesType));
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties = (struct wsa__ReferencePropertiesType*)soap_malloc(soap,sizeof(struct wsa__ReferencePropertiesType));
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters = (struct wsa__ReferenceParametersType*)soap_malloc(soap,sizeof(struct wsa__ReferenceParametersType));
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName = (struct wsa__ServiceNameType*)soap_malloc(soap,sizeof(struct wsa__ServiceNameType));
- ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType = (char **)soap_malloc(soap, sizeof(char *) * SMALL_INFO_LENGTH);
- ProbeMatches.ProbeMatch->wsa__EndpointReference.__any = (char **)soap_malloc(soap, sizeof(char*) * SMALL_INFO_LENGTH);
- ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
- ProbeMatches.ProbeMatch->wsa__EndpointReference.Address = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
- macaddr[0]=0x01;macaddr[1]=0x01;macaddr[2]=0x01;macaddr[3]=0x01;macaddr[4]=0x01;macaddr[5]=0x01;
- sprintf(_HwId,"urn:uuid:2419d68a-2dd2-21b2-a205-%02X%02X%02X%02X%02X%02X",macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
- sprintf(_IPAddr, "http://%03d.%03d.%1d.%03d/onvif/device_service", 192, 168, 1, 233);
- ProbeMatches.__sizeProbeMatch = 1;
- ProbeMatches.ProbeMatch->Scopes->__item =(char *)soap_malloc(soap, 1024);
- memset(ProbeMatches.ProbeMatch->Scopes->__item,0,sizeof(ProbeMatches.ProbeMatch->Scopes->__item));
- //Scopes MUST BE
- strcat(ProbeMatches.ProbeMatch->Scopes->__item, "onvif://www.onvif.org/type/NetworkVideoTransmitter");
- ProbeMatches.ProbeMatch->Scopes->MatchBy = NULL;
- strcpy(ProbeMatches.ProbeMatch->XAddrs, _IPAddr);
- strcpy(ProbeMatches.ProbeMatch->Types, wsdd__Probe->Types);
- DBG("wsdd__Probe->Types=%s\n",wsdd__Probe->Types);
- ProbeMatches.ProbeMatch->MetadataVersion = 1;
- //ws-discovery規定 為可選項
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__size = 0;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__any = NULL;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__size = 0;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__any = NULL;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
- //ws-discovery規定 為可選項
- strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0], "ttl");
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__item = NULL;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->PortName = NULL;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__anyAttribute = NULL;
- ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
- strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0], "Any");
- strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute, "Attribute");
- ProbeMatches.ProbeMatch->wsa__EndpointReference.__size = 0;
- strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.Address, _HwId);
- /*註釋的部分為可選,註釋掉onvif test也能發現ws-d*/
- //soap->header->wsa__To = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
- //soap->header->wsa__Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches";
- soap->header->wsa__RelatesTo = (struct wsa__Relationship*)soap_malloc(soap, sizeof(struct wsa__Relationship));
- //it's here
- soap->header->wsa__RelatesTo->__item = soap->header->wsa__MessageID;
- soap->header->wsa__RelatesTo->RelationshipType = NULL;
- soap->header->wsa__RelatesTo->__anyAttribute = NULL;
- soap->header->wsa__MessageID =(char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
- strcpy(soap->header->wsa__MessageID,_HwId+4);
- /* send over current socket as HTTP OK response: */
- /*測試過,第二引數必須http,action隨意*/
- soap_send___wsdd__ProbeMatches(soap, "http://", NULL, &ProbeMatches);
- return SOAP_OK;
- }
1首先是瞭解訊息頭header和ProbeMatches中的內容,非常重要,可以參考這裡http://www.w3.org/Submission/ws-addressing/ 最好詳細的學習一下,裡面的內容非常重要。
2其次需要理解的是,其實當你看完ws-addressing後你會發現,骨架程式碼中的結構體和SOAP訊息中的內容是一一對應的,例如:
結構體osap->header對應SOAP訊息的<SOAP-ENV:Header></SOAP-ENV:Header>中的內容,包含在header裡的內容當然會包含在SOAP的header內。例如:
結構體soap->header->wsa__RelatesTo對應的是<wsa:RelatesTo></wsa:RelatesTo>。
3最後需要理解的是,在程式碼中的"__"雙下劃線一般對應xml中的名稱空間的":",下劃線前是名稱空間,後是具體內容。
4最後的最後是要詳細的閱讀ONVIF Core Specification
下圖為響應OnvifTestTool的Probe命令的SOAP訊息
結合上圖再分析程式碼就親切多了。在ONVIF Core Specification的7.3.2.2 Scopes 一節描述了onvif需要的Scopes,這個是需要在程式裡填充,具體填充什麼,文件裡說的很明確:
注意點是在太多,隨便漏掉一個都可能會導致搜不到裝置,下圖是非常重要的一個:
SOAP1.1和SOAP1.2所使用的SOAP-ENV是不同的,ONVIF使用的是SOAP1.1,如果soapcpp2產生的nsmap檔案中的SOAP-ENV是SOAP1.2版本的話,那麼OnvifTestTool是不會識別裝置發出的SOAP訊息的。
5、該main函式登場了
- int main()
- {
- int server_udp;
- int retval=0;
- struct soap *soap_udp;
- int fault_flag = 0;
- server_udp = create_server_socket_udp();
- bind_server_udp1(server_udp);
- while(1){
- soap_udp=soap_new();
- soap_init1(soap_udp, SOAP_IO_UDP);
- soap_udp->master = server_udp;
- soap_udp->socket = server_udp;
- soap_udp->errmode = 0;
- soap_udp->bind_flags = 1;
- if (!soap_valid_socket(soap_bind(soap_udp, NULL, 3702, 100)))
- {
- soap_print_fault(soap_udp, stderr);
- }
- fprintf(stderr,"soap_serve starting..\n");
- retval = soap_serve(soap_udp); //阻塞在這裡
- fprintf(stderr,"retval=%d\n",retval);
- if(retval && !(fault_flag))
- {
- fault_flag = 1;
- }
- else if(!retval)
- {
- fault_flag = 0;
- }
- soap_destroy(soap_udp);
- soap_end(soap_udp);
- soap_done(soap_udp);
- free(soap_udp);
- }
- }
最後編譯執行
make server
./discovery.tmp
單擊OnvifTestTool的Discover Devices,執行discovery.tmp的中會列印除錯資訊,如圖
然後,在OnvifTestTool中會搜尋到我的裝置
響應Discover Devices的SOAP訊息如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <SOAP-ENV:Envelope
- xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
- xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema"
- xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
- xmlns:wsdd="http://schemas.xmlsoap.org/ws/2005/04/discovery"
- xmlns:ns1="http://www.onvif.org/ver10/network/wsdl/RemoteDiscoveryBinding"
- xmlns:ns2="http://www.onvif.org/ver10/network/wsdl/DiscoveryLookupBinding"
- xmlns:ns3="http://www.onvif.org/ver10/network/wsdl/DiscoveryLookupBinding"
- xmlns:tdn="http://www.onvif.org/ver10/network/wsdl">
- <SOAP-ENV:Header>
- <wsa:MessageID>uuid:2419d68a-2dd2-21b2-a205-010101010101</wsa:MessageID>
- <wsa:RelatesTo>uuid:88a3958a-6155-4510-8279-69aeafd31681</wsa:RelatesTo>
- <wsa:To SOAP-ENV:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>
- <wsa:Action SOAP-ENV:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>
- </SOAP-ENV:Header>
- <SOAP-ENV:Body>
- <wsdd:ProbeMatches>
- <wsdd:ProbeMatch xmlns:_0="http://www.onvif.org/ver10/device/wsdl">
- <wsa:EndpointReference>
- <wsa:Address>urn:uuid:2419d68a-2dd2-21b2-a205-010101010101</wsa:Address>
- <wsa:ReferenceProperties></wsa:ReferenceProperties>
- <wsa:ReferenceParameters></wsa:ReferenceParameters>
- <wsa:PortType>ttl</wsa:PortType>
- </wsa:EndpointReference>
- <wsdd:Types>_0:Device</wsdd:Types>
- <wsdd:Scopes>onvif://www.onvif.org/type/NetworkVideoTransmitter</wsdd:Scopes>
- <wsdd:XAddrs>http://192.168.1.233/onvif/device_service</wsdd:XAddrs>
- <wsdd:MetadataVersion>1</wsdd:MetadataVersion>
- </wsdd:ProbeMatch>
- </wsdd:ProbeMatches>
- </SOAP-ENV:Body>
- </SOAP-ENV:Envelope>
資料下載
上述完整的程式碼包在這裡,有需要的就去下載吧:http://download.csdn.net/detail/ghostyu/4766025
另外我參考的部分文件可以再這裡下載
ONVIF-Core-Spec-v210.pdf:http://download.csdn.net/detail/ghostyu/4766067
gSOAP手冊:http://download.csdn.net/detail/ghostyu/4766075
OnvifTestTool12.06測試工具網上有的,我就不上傳了。
相關文章
- Promise/A+ 規範的實現Promise
- 通過sysbench工具實現MySQL資料庫的效能測試MySql資料庫
- Event Loop的規範和實現OOP
- 通過SQL SERVER遠端上傳檔案的實現SQLServer
- 通過佇列實現棧OR通過棧實現佇列佇列
- SQL Server 通過REVERSE實現C#中lastIndexOf的效果SQLServerC#ASTIndex
- 通過 ProxyChains-NG 實現終端代理AI
- 通過模板實現POI
- postgresql通過建立規則(RULE)實現表記錄SQL
- NCF 如何通過WebApi實現前後端分離WebAPI後端
- 通過rsync實現遠端容災備份薦
- WPF 通過程式實現異常隔離的客戶端客戶端
- 如何實現介面異常場景測試?測試方法探索與測試工具實現
- 實現 CommonJs 規範中的 Require 模組JSUI
- 實現一個符合 Promise/A+ 規範的 MyPromisePromise
- LRU 實現 通過 LinkedHashMapHashMap
- 通過redis實現session共享RedisSession
- QueueUserApc實現DLL注入的測試
- 通過C#實現OPC-UA服務端(二)C#服務端
- ios端app讀取iphone檔案(通過itunes實現)iOSAPPiPhone
- 通過 ProxySQL 在 TiDB 上實現 SQL 的規則化路由SQLTiDB路由
- WebSocket實現前後端通訊Web後端
- pytorch實現yolov3(5) 實現端到端的目標檢測PyTorchYOLO
- Android UI 自動化測試實現過程AndroidUI
- 基於JWT規範實現的認證微服務JWT微服務
- Promise的原始碼實現(完美符合Promise/A+規範)Promise原始碼
- 手寫實現滿足 Promise/A+ 規範的 PromisePromise
- 如何實現一個符合promiseA+規範的promisePromise
- 實現一個完美符合Promise/A+規範的PromisePromise
- 使用 Nightwatch.js 實現 Node.js 的端到端測試Node.js
- Jmeter實現 Dubbo介面測試JMeter
- 通過Gradle Plugin實現Git Hooks檢測機制GradlePluginGitHook
- C# 通過socket實現UDP 通訊C#UDP
- 通過nginx部署前端程式碼實現前後端分離Nginx前端後端
- Zabbix透過PSK共享金鑰實現Server和Agent的通訊加密Server加密
- 測試流程規範--提測規範(釘釘、郵件)
- Java的代理模式(通過公共介面實現)Java模式
- Promise 規範解讀及實現細節 (二)Promise