Onvif: Ubuntu18下使用gsoap开发Onvif设备发现服务端

目标

本文讨论基于Gsoap在ubuntu18上开发支持Onvif协议的模拟摄像头端。

Onvif简介

ONVIF(开放式网络视频接口论坛)是一个全球性的开放式行业论坛,其目标是促进开发和使用基于物理IP的安全产品接口的全球开放标准。ONVIF规范的目标是实现一个网络视频框架协议,使不同厂商所生产的网络视频产品(网络摄像头、NVR)完全互通。总结Onvif标准协议的内容主要包括如下几大部分:

  1. 基于WS-Discovery的设备发现协议(服务端监听在组播地址 239.255.255.250:3702,支持TCP与UDP)。
  2. 基于WSDL的设备控制协议(WSDL是XML协议的扩展)。
  3. 基于RTP/RTSP等协议的媒体扩展支持。

GSOAP简介

gSOAP是一套c/c++的编译工具SDK,提供了一个SOAP/XML 关于C/C++ 语言的实现,使用gsoap可以很容易开发支持Onvif协议的客户端与服务端应用。

工具下载

  1. Onvif Test Tool: http://8dx.pc6.com/wwb6/ONVIFDeviceTestTool1906.zip
  2. Gsoap: https://master.dl.sourceforge.net/project/gsoap2/gsoap-2.8/gsoap_2.8.105.zip

设备发现

编译Gsoap

首先使用如下命令编译gsoap源码,安装wsdl2h与soapcpp2。

unzip gsoap_2.8.105.zip
cd gsoap_2.8.105

./configure
make
sudo make install

生成源代码

其次使用Onvif的接口描述文件生成服务端的SDK代码文件如下:

wsdl2h -o onvif.h -c -s -t /home/cxm/work/gsoap/pc/gsoap-2.8/gsoap/wsdl/typemap.dat http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl
soapcpp2 -c ./onvif.h

自动生成的文件说明如下:

  1. typemap.data可以在gsoap的源码目录下找到。
  2. soapC.c,soapServer.c,soapClientLib.c文件为gsoap sdk,直接放入源码编译即可。

设备发现

代码

服务端的设备发现main.c代码如下,udp_thread()函数启动UDP设备发现端口监听;设备发现主要代码在__wsdd__Probe()中。

#include 
#include 

#include "soapH.h"
#include "wsdd.nsmap"

void *udp_thread(void *args) {
	int count = 0;
	struct soap ServerSoap;
	struct ip_mreq mcast;
	
	soap_init1(&ServerSoap, SOAP_IO_UDP | SOAP_XML_IGNORENS);
	soap_set_namespaces(&ServerSoap,  namespaces);
	
	printf("[%s][%d][%s][%s] ServerSoap.version = %d \n", __FILE__, __LINE__, __TIME__, __func__, ServerSoap.version);
	
	if(!soap_valid_socket(soap_bind(&ServerSoap, NULL, 3702, 10)))
	{
		soap_print_fault(&ServerSoap, stderr);
		exit(1);
	}
	
	mcast.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
	mcast.imr_interface.s_addr = htonl(INADDR_ANY);
 
	if(setsockopt(ServerSoap.master, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(mcast)) < 0)
	{
	    	printf("setsockopt error! error code = %d,err string = %s\n",errno,strerror(errno));
		return 0;
	}
 
	for(;;)
	{
		if(soap_serve(&ServerSoap))
		{
			soap_print_fault(&ServerSoap, stderr);
		}
 
		soap_destroy(&ServerSoap);
		soap_end(&ServerSoap);
 
		//客户端的IP地址
		// printf("RECEIVE count %d, connection from IP = %lu.%lu.%lu.%lu socket = %d \r\n", count, ((ServerSoap.ip)>>24)&0xFF, ((ServerSoap.ip)>>16)&0xFF, ((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF, (ServerSoap.socket));
		count++;
	}
 
	//分离运行时的环境
	soap_done(&ServerSoap);
}

int main(int argc, char **argv) {
    pthread_t udpThread;
    pthread_create(&udpThread, NULL, udp_thread, NULL);

    while (1)
	usleep(1000 * 10);
    return 0;
}

/** Web service one-way operation 'SOAP_ENV__Fault' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 SOAP_ENV__Fault(struct soap*s, char *faultcode, char *faultstring, char *faultactor, struct SOAP_ENV__Detail *detail, struct SOAP_ENV__Code *SOAP_ENV__Code, struct SOAP_ENV__Reason *SOAP_ENV__Reason, char *SOAP_ENV__Node, char *SOAP_ENV__Role, struct SOAP_ENV__Detail *SOAP_ENV__Detail) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}

/** Web service one-way operation '__wsdd__Hello' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Hello(struct soap*s, struct wsdd__HelloType *wsdd__Hello) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}
/** Web service one-way operation '__wsdd__Bye' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Bye(struct soap*s, struct wsdd__ByeType *wsdd__Bye) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}

#define INFO_LENGTH 1024
#define SMALL_INFO_LENGTH 1024

/** Web service one-way operation '__wsdd__Probe' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Probe(struct soap*soap, struct wsdd__ProbeType *wsdd__Probe) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);

    unsigned char macaddr[6] = { 0 };
	char _IPAddr[INFO_LENGTH] = { 0 };
	char _HwId[1024] = { 0 };
 
	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);

    //netGetMac("eth4", macaddr); //eth0  根据实际情况填充
	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://%s:8080/onvif/device_service", "192.168.1.3");
	printf("[%d] _IPAddr ==== %s\n", __LINE__, _IPAddr);
 
	ProbeMatches.__sizeProbeMatch = 1;
	ProbeMatches.ProbeMatch->Scopes->__item = (char *)soap_malloc(soap, 1024);
	//memset(ProbeMatches.ProbeMatch->Scopes->__item,0,sizeof(ProbeMatches.ProbeMatch->Scopes->__item));
	memset(ProbeMatches.ProbeMatch->Scopes->__item, 0, 1024);
 
	//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);
	printf("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);
 
	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));
	soap->header->wsa__RelatesTo->__item = soap->header->wsa__MessageID;
	printf("__item: %p, wsa__MessageID: %p: %s\n", soap->header->wsa__RelatesTo->__item, soap->header->wsa__MessageID, 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);
 
	if (SOAP_OK == soap_send___wsdd__ProbeMatches(soap, "http://", NULL, &ProbeMatches))
	{
		printf("send ProbeMatches success !\n");
		return SOAP_OK;
	}
 
	printf("[%d] soap error: %d, %s, %s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
 
	return soap->error;;
}

/** Web service one-way operation '__wsdd__ProbeMatches' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 __wsdd__ProbeMatches(struct soap*s, struct wsdd__ProbeMatchesType *wsdd__ProbeMatches) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}
/** Web service one-way operation '__wsdd__Resolve' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Resolve(struct soap*s, struct wsdd__ResolveType *wsdd__Resolve) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}
/** Web service one-way operation '__wsdd__ResolveMatches' implementation, should return value of soap_send_empty_response() to send HTTP Accept acknowledgment, or return an error code, or return SOAP_OK to immediately return without sending an HTTP response message */
SOAP_FMAC5 int SOAP_FMAC6 __wsdd__ResolveMatches(struct soap*s, struct wsdd__ResolveMatchesType *wsdd__ResolveMatches) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}
/** Web service operation '__tdn__Hello' implementation, should return SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 __tdn__Hello(struct soap*s, struct wsdd__HelloType tdn__Hello, struct wsdd__ResolveType *tdn__HelloResponse) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}
/** Web service operation '__tdn__Bye' implementation, should return SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 __tdn__Bye(struct soap*s, struct wsdd__ByeType tdn__Bye, struct wsdd__ResolveType *tdn__ByeResponse) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}
/** Web service operation '__tdn__Probe' implementation, should return SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 __tdn__Probe(struct soap*s, struct wsdd__ProbeType tdn__Probe, struct wsdd__ProbeMatchesType *tdn__ProbeResponse) {
	printf("In function: %s\n", __FUNCTION__);
	fflush(stdout);
	return 0;
}

编译

使用如下命令编译,运行可执行文件:

gcc main.c soapC.c soapServer.c soapClientLib.c -I /home/cxm/work/gsoap/pc/gsoap-2.8/gsoap -lgsoap -L /home/cxm/work/gsoap/pc/gsoap-2.8/gsoap -lpthread
./a.out

测试

打开Onvif Test Tool, 选择适合的网卡后点击 Discovery Device, 即可发现设备。
Onvif: Ubuntu18下使用gsoap开发Onvif设备发现服务端_第1张图片

你可能感兴趣的:(c++,onvif)