onvif规范的实现:server端Discovery实现,通过OnvifTestTool12.06测试

说明:网上关于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?

  1. #Use gSOAP 2.8.10 and up. In the typemap.dat file used by wsdl2h, add:
  2. #   ONVIF recommended prefixes
  3. tds = "http://www.onvif.org/ver10/device/wsdl"
  4. tev = "http://www.onvif.org/ver10/events/wsdl"
  5. tls = "http://www.onvif.org/ver10/display/wsdl"
  6. tmd = "http://www.onvif.org/ver10/deviceIO/wsdl"
  7. timg    = "http://www.onvif.org/ver20/imaging/wsdl"
  8. trt = "http://www.onvif.org/ver10/media/wsdl"
  9. tptz    = "http://www.onvif.org/ver20/ptz/wsdl"
  10. trv = "http://www.onvif.org/ver10/receiver/wsdl"
  11. trc = "http://www.onvif.org/ver10/recording/wsdl"
  12. tse = "http://www.onvif.org/ver10/search/wsdl"
  13. trp = "http://www.onvif.org/ver10/replay/wsdl"
  14. tan = "http://www.onvif.org/ver20/analytics/wsdl"
  15. tad = "http://www.onvif.org/ver10/analyticsdevice/wsdl"
  16. tdn = "http://www.onvif.org/ver10/network/wsdl"
  17. tt  = "http://www.onvif.org/ver10/schema"
  18. #   OASIS recommended prefixes
  19. wsnt    = "http://docs.oasis-open.org/wsn/b-2"
  20. wsntw   = "http://docs.oasis-open.org/wsn/bw-2"
  21. wsrfbf  = "http://docs.oasis-open.org/wsrf/bf-2"
  22. wsrfr   = "http://docs.oasis-open.org/wsrf/r-2"
  23. wsrfrw  = "http://docs.oasis-open.org/wsrf/rw-2"
  24. wstop   = "http://docs.oasis-open.org/wsn/t-1"
  25. #   WS-Discovery 1.0 remapping
  26. wsdd10__HelloType       = | wsdd__HelloType
  27. wsdd10__ByeType         = | wsdd__ByeType
  28. wsdd10__ProbeType       = | wsdd__ProbeType
  29. wsdd10__ProbeMatchesType    = | wsdd__ProbeMatchesType
  30. wsdd10__ProbeMatchType      = | wsdd__ProbeMatchType
  31. wsdd10__ResolveType     = | wsdd__ResolveType
  32. wsdd10__ResolveMatchesType  = | wsdd__ResolveMatchesType
  33. wsdd10__ResolveMatchType    = | wsdd__ResolveMatchType
  34. #   SOAP-ENV mapping
  35. SOAP_ENV__Envelope  = struct SOAP_ENV__Envelope { struct SOAP_ENV__Header *SOAP_ENV__Header; _XML SOAP_ENV__Body; }; | struct SOAP_ENV__Envelope
  36. SOAP_ENV__Header    = | struct SOAP_ENV__Header
  37. SOAP_ENV__Fault     = | struct SOAP_ENV__Fault
  38. SOAP_ENV__Detail    = | struct SOAP_ENV__Detail
  39. SOAP_ENV__Code      = | struct SOAP_ENV__Code
  40. SOAP_ENV__Subcode   = | struct SOAP_ENV__Subcode
  41. SOAP_ENV__Reason    = | struct SOAP_ENV__Reason
#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来产生头文件,不要下载下来。

  1. wsdl2h -o onvif.h -c -s -t ./typemap.dat http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl
wsdl2h -o onvif.h -c -s -t ./typemap.dat http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl

3、使用onvif.h来产生骨架代码

  1. soapcpp2 -c onvif.h -x -I /root/onvif/gsoap-2.8/gsoap/import -I /root/onvif/gsoap-2.8/gsoap/
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

  1. int bind_server_udp1(int server_s)
  2. {
  3. struct sockaddr_in local_addr;
  4. memset(&local_addr,0,sizeof(local_addr));
  5. local_addr.sin_family = AF_INET;
  6. local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  7. local_addr.sin_port = htons(3702);
  8. return bind(server_s,(struct sockaddr*)&local_addr,sizeof(local_addr));
  9. }
  10. static int create_server_socket_udp(void)
  11. {
  12. int server_udp;
  13. unsigned char one = 1;
  14. int sock_opt = 1;
  15. //server_udp = socket(PF_INET, SOCK_DGRAM, 0);
  16. server_udp = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  17. if (server_udp == -1) {
  18. printf("unable to create socket\n");
  19. }
  20. /* reuse socket addr */
  21. if ((setsockopt(server_udp, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt,
  22. sizeof (sock_opt))) == -1) {
  23. printf("setsockopt\n");
  24. }
  25. if ((setsockopt(server_udp, IPPROTO_IP, IP_MULTICAST_LOOP,
  26. &one, sizeof (unsigned char))) == -1) {
  27. printf("setsockopt\n");
  28. }
  29. struct ip_mreq mreq;
  30. mreq.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
  31. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  32. if(setsockopt(server_udp,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq))==-1){
  33. perror("memberchip error\n");
  34. }
  35. return server_udp;
  36. }
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函数,添加如下内容

  1. int  __wsdd__Probe(struct soap* soap, struct wsdd__ProbeType *wsdd__Probe)
  2. {
  3. DBG("__wsdd__Probe\n");
  4. char macaddr[6];
  5. char _IPAddr[INFO_LENGTH];
  6. char _HwId[1024];
  7. wsdd__ProbeMatchesType ProbeMatches;
  8. ProbeMatches.ProbeMatch = (struct wsdd__ProbeMatchType *)soap_malloc(soap, sizeof(struct wsdd__ProbeMatchType));
  9. ProbeMatches.ProbeMatch->XAddrs = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  10. ProbeMatches.ProbeMatch->Types = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  11. ProbeMatches.ProbeMatch->Scopes = (struct wsdd__ScopesType*)soap_malloc(soap,sizeof(struct wsdd__ScopesType));
  12. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties = (struct wsa__ReferencePropertiesType*)soap_malloc(soap,sizeof(struct wsa__ReferencePropertiesType));
  13. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters = (struct wsa__ReferenceParametersType*)soap_malloc(soap,sizeof(struct wsa__ReferenceParametersType));
  14. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName = (struct wsa__ServiceNameType*)soap_malloc(soap,sizeof(struct wsa__ServiceNameType));
  15. ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType = (char **)soap_malloc(soap, sizeof(char *) * SMALL_INFO_LENGTH);
  16. ProbeMatches.ProbeMatch->wsa__EndpointReference.__any = (char **)soap_malloc(soap, sizeof(char*) * SMALL_INFO_LENGTH);
  17. ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
  18. ProbeMatches.ProbeMatch->wsa__EndpointReference.Address = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  19. macaddr[0]=0x01;macaddr[1]=0x01;macaddr[2]=0x01;macaddr[3]=0x01;macaddr[4]=0x01;macaddr[5]=0x01;
  20. 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]);
  21. sprintf(_IPAddr, "http://%03d.%03d.%1d.%03d/onvif/device_service", 192, 168, 1, 233);
  22. ProbeMatches.__sizeProbeMatch = 1;
  23. ProbeMatches.ProbeMatch->Scopes->__item =(char *)soap_malloc(soap, 1024);
  24. memset(ProbeMatches.ProbeMatch->Scopes->__item,0,sizeof(ProbeMatches.ProbeMatch->Scopes->__item));
  25. //Scopes MUST BE
  26. strcat(ProbeMatches.ProbeMatch->Scopes->__item, "onvif://www.onvif.org/type/NetworkVideoTransmitter");
  27. ProbeMatches.ProbeMatch->Scopes->MatchBy = NULL;
  28. strcpy(ProbeMatches.ProbeMatch->XAddrs, _IPAddr);
  29. strcpy(ProbeMatches.ProbeMatch->Types, wsdd__Probe->Types);
  30. DBG("wsdd__Probe->Types=%s\n",wsdd__Probe->Types);
  31. ProbeMatches.ProbeMatch->MetadataVersion = 1;
  32. //ws-discovery规定 为可选项
  33. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__size = 0;
  34. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__any = NULL;
  35. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__size = 0;
  36. ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__any = NULL;
  37. ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
  38. //ws-discovery规定 为可选项
  39. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0], "ttl");
  40. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__item = NULL;
  41. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->PortName = NULL;
  42. ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__anyAttribute = NULL;
  43. ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
  44. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0], "Any");
  45. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute, "Attribute");
  46. ProbeMatches.ProbeMatch->wsa__EndpointReference.__size = 0;
  47. strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.Address, _HwId);
  48. /*注释的部分为可选,注释掉onvif test也能发现ws-d*/
  49. //soap->header->wsa__To = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
  50. //soap->header->wsa__Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches";
  51. soap->header->wsa__RelatesTo = (struct wsa__Relationship*)soap_malloc(soap, sizeof(struct wsa__Relationship));
  52. //it's here
  53. soap->header->wsa__RelatesTo->__item = soap->header->wsa__MessageID;
  54. soap->header->wsa__RelatesTo->RelationshipType = NULL;
  55. soap->header->wsa__RelatesTo->__anyAttribute = NULL;
  56. soap->header->wsa__MessageID =(char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
  57. strcpy(soap->header->wsa__MessageID,_HwId+4);
  58. /* send over current socket as HTTP OK response: */
  59. /*测试过,第二参数必须http,action随意*/
  60. soap_send___wsdd__ProbeMatches(soap, "http://", NULL, &ProbeMatches);
  61. return SOAP_OK;
  62. }
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规范的实现:server端Discovery实现,通过OnvifTestTool12.06测试

结合上图再分析代码就亲切多了。在ONVIF Core Specification的7.3.2.2  Scopes 一节描述了onvif需要的Scopes,这个是需要在程序里填充,具体填充什么,文档里说的很明确:

onvif规范的实现:server端Discovery实现,通过OnvifTestTool12.06测试

注意点是在太多,随便漏掉一个都可能会导致搜不到设备,下图是非常重要的一个:

onvif规范的实现:server端Discovery实现,通过OnvifTestTool12.06测试

SOAP1.1和SOAP1.2所使用的SOAP-ENV是不同的,ONVIF使用的是SOAP1.1,如果soapcpp2产生的nsmap文件中的SOAP-ENV是SOAP1.2版本的话,那么OnvifTestTool是不会识别设备发出的SOAP消息的。

5、该main函数登场了

  1. int main()
  2. {
  3. int server_udp;
  4. int retval=0;
  5. struct soap *soap_udp;
  6. int fault_flag = 0;
  7. server_udp = create_server_socket_udp();
  8. bind_server_udp1(server_udp);
  9. while(1){
  10. soap_udp=soap_new();
  11. soap_init1(soap_udp, SOAP_IO_UDP);
  12. soap_udp->master = server_udp;
  13. soap_udp->socket = server_udp;
  14. soap_udp->errmode = 0;
  15. soap_udp->bind_flags = 1;
  16. if (!soap_valid_socket(soap_bind(soap_udp, NULL, 3702, 100)))
  17. {
  18. soap_print_fault(soap_udp, stderr);
  19. }
  20. fprintf(stderr,"soap_serve starting..\n");
  21. retval = soap_serve(soap_udp); //阻塞在这里
  22. fprintf(stderr,"retval=%d\n",retval);
  23. if(retval && !(fault_flag))
  24. {
  25. fault_flag = 1;
  26. }
  27. else if(!retval)
  28. {
  29. fault_flag = 0;
  30. }
  31. soap_destroy(soap_udp);
  32. soap_end(soap_udp);
  33. soap_done(soap_udp);
  34. free(soap_udp);
  35. }
  36. }
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里或者独立的线程中。
最后编译运行

make server

./discovery.tmp

单击OnvifTestTool的Discover Devices,运行discovery.tmp的中会打印调试信息,如图

onvif规范的实现:server端Discovery实现,通过OnvifTestTool12.06测试
然后,在OnvifTestTool中会搜索到我的设备

onvif规范的实现:server端Discovery实现,通过OnvifTestTool12.06测试

响应Discover Devices的SOAP消息如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <SOAP-ENV:Envelope
  3. xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
  4. xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  7. xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
  8. xmlns:wsdd="http://schemas.xmlsoap.org/ws/2005/04/discovery"
  9. xmlns:ns1="http://www.onvif.org/ver10/network/wsdl/RemoteDiscoveryBinding"
  10. xmlns:ns2="http://www.onvif.org/ver10/network/wsdl/DiscoveryLookupBinding"
  11. xmlns:ns3="http://www.onvif.org/ver10/network/wsdl/DiscoveryLookupBinding"
  12. xmlns:tdn="http://www.onvif.org/ver10/network/wsdl">
  13. <SOAP-ENV:Header>
  14. <wsa:MessageID>uuid:2419d68a-2dd2-21b2-a205-010101010101</wsa:MessageID>
  15. <wsa:RelatesTo>uuid:88a3958a-6155-4510-8279-69aeafd31681</wsa:RelatesTo>
  16. <wsa:To SOAP-ENV:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>
  17. <wsa:Action SOAP-ENV:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>
  18. </SOAP-ENV:Header>
  19. <SOAP-ENV:Body>
  20. <wsdd:ProbeMatches>
  21. <wsdd:ProbeMatch xmlns:_0="http://www.onvif.org/ver10/device/wsdl">
  22. <wsa:EndpointReference>
  23. <wsa:Address>urn:uuid:2419d68a-2dd2-21b2-a205-010101010101</wsa:Address>
  24. <wsa:ReferenceProperties></wsa:ReferenceProperties>
  25. <wsa:ReferenceParameters></wsa:ReferenceParameters>
  26. <wsa:PortType>ttl</wsa:PortType>
  27. </wsa:EndpointReference>
  28. <wsdd:Types>_0:Device</wsdd:Types>
  29. <wsdd:Scopes>onvif://www.onvif.org/type/NetworkVideoTransmitter</wsdd:Scopes>
  30. <wsdd:XAddrs>http://192.168.1.233/onvif/device_service</wsdd:XAddrs>
  31. <wsdd:MetadataVersion>1</wsdd:MetadataVersion>
  32. </wsdd:ProbeMatch>
  33. </wsdd:ProbeMatches>
  34. </SOAP-ENV:Body>
  35. </SOAP-ENV:Envelope>
<?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

上一篇:减少HTTP请求之将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片(大型网站优化技术)


下一篇:使用Excel对象模型在Excel单元格中设置不同的字体