完成框架搭建后,編寫自己的主函數起onvif服務
編寫makefile
objs = onvif.o onvif_func.o duration.o soapC.o soapServer.o stdsoap2.o
onvif:$(objs)
gcc -o onvif $(objs)
.PHONY:clean
clean:
#-rm onvif
rm *[!C.o].o
發現提示好多函數沒有定義,在頭文件soapStub.h中定義的,直接把沒有定義的函數聲明拷貝到一個onvif_func.c中
暫時用ue等工具實現一個空函數,
將;替換為^p{^p^treturn SOAP_OK;^p}
就可以實現素有函數的空實現,然后編譯通過就可以了。。。
下面就是啟動onvif服務端代碼的具體實現了。。
組播setsockopt:no such device問題的解決方法
route add -net 224.0.0.0 netmask 224.0.0.0 eth0
然后
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;
- }
需要注意幾點:1/設置socket屬性SO_REUSEADDR,2、設置socket屬性IP_ADD_MEMBERSHIP,目的是讓3702的端口能夠重復綁定,一家加入組播組。
其次是添加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;
- }
想要寫出上述代碼,是一定要了解SOAP格式的,在WS-Discovery中描述了discovery所用的soap格式
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);
- }
- }
soap_server函數會一直阻塞,直到接收到SOAP消息,並且該處理是一次性的,所以要將將soap_server放到while里或者獨立的線程中。
最后編譯運行
問題:哥編譯出來的代碼,各項都正確啊,就是搜索不到,test工具提示沒有返回消息,麻蛋啊。。
自習查看上文才發現,哥的SOAP1.2的,onvif只支持1.1,換了果斷可以搜索到。。妹的。。
