Linux下gSoap编译Onvif框架详细图文教程

一、  下载gSoap包

地址:https://sourceforge.net/projects/gsoap2/

Linux下gSoap编译Onvif框架详细图文教程_第1张图片

   

二、编译gSoap

#:./configure--prefix=/home/jack/hj/onvif/gsoap

#:make

报错:

处理方法:#:apt-get install openssl #:apt-get install libssl-dev

         继续编译:

       #:make

#:make install

  编译安装完成:

Linux下gSoap编译Onvif框架详细图文教程_第2张图片


二、使用gSoap工具生成onvif.h(以设备发现wsdl为例)

#:/home/jack/hj/onvif/gsoap/bin/wsdl2h  -c–s  -t ./typemap.dat  -o  onvif.h http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl 

Linux下gSoap编译Onvif框架详细图文教程_第3张图片

Linux下gSoap编译Onvif框架详细图文教程_第4张图片

三、 新建目录discovery,将onvif.h、soapcpp2拷贝到该目录下

Linux下gSoap编译Onvif框架详细图文教程_第5张图片

soapcpp2、onvif.h生成源代码:

#:./soapcpp2 -s -2 onvif.h -x -I../gsoap-2.8/gsoap -I ../gsoap-2.8/gsop/import/ -I ../gsoap-2.8/gsoap/plugin -I../gsoap-2.8/gsoap/custom

-C -C:仅生成客户端代码

-S:仅生成服务器端代码

-L:不要产生soapClientLib.csoapServerLib.c文件

- -c:产生纯C代码,否则是C++代码
-I:指定import路径
-x:不要产生XML示例文件
-i:生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。

Linux下gSoap编译Onvif框架详细图文教程_第6张图片

   Linux下gSoap编译Onvif框架详细图文教程_第7张图片

   生成的soapClientLib.csoapServerLib.c无实际作用,可直接删除。

   copy相关文件到discovery目录下:

Linux下gSoap编译Onvif框架详细图文教程_第8张图片

Linux下gSoap编译Onvif框架详细图文教程_第9张图片

四、新建onvif_server_interface.c文件

填充__wsdd__Probe等函数,接收到discovery组播包后会自动调用此函数。

#include 
#include 
#include 

#include "soapH.h"
#include "soapStub.h"


SOAP_FMAC5 int SOAP_FMAC6 SOAP_ENV__Fault(struct soap* soap, 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)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Hello(struct soap* soap, struct wsdd__HelloType *wsdd__Hello)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Bye(struct soap* soap, struct wsdd__ByeType *wsdd__Bye)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__ProbeMatches(struct soap* soap, struct wsdd__ProbeMatchesType *wsdd__ProbeMatches)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Resolve(struct soap* soap, struct wsdd__ResolveType *wsdd__Resolve)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__ResolveMatches(struct soap* soap, struct wsdd__ResolveMatchesType *wsdd__ResolveMatches)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __tdn__Hello(struct soap* soap, struct wsdd__HelloType tdn__Hello, struct wsdd__ResolveType *tdn__HelloResponse)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __tdn__Bye(struct soap* soap, struct wsdd__ByeType tdn__Bye, struct wsdd__ResolveType *tdn__ByeResponse)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __tdn__Probe(struct soap* soap, struct wsdd__ProbeType tdn__Probe, struct wsdd__ProbeMatchesType *tdn__ProbeResponse)
{

	return 0;
}

SOAP_FMAC5 int SOAP_FMAC6  __wsdd__Probe(struct soap* soap, struct wsdd__ProbeType *wsdd__Probe)
{

#define MACH_ADDR_LENGTH 6
#define INFO_LENGTH 512
#define LARGE_INFO_LENGTH 1024
#define SMALL_INFO_LENGTH 512

	printf("[%d] __wsdd__Probe start !\n", __LINE__);

	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/onvif/device_service", "192.168.1.33");
	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;;

}

五、新建onvif_server.c文件,添加main函数

#include 
#include 
#include 

#include "soapH.h"
#include "nsmap.h"
#include "soapStub.h"


int main(int argc, char ** argv)
{
#define ONVIF_LISTEN_PORT 3702
	printf("[%s][%d][%s][%s] start \n", __FILE__, __LINE__, __TIME__, __func__);

	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, ONVIF_LISTEN_PORT, 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);

	return 0;
}

六、编写Makefile

GSOAP_ROOT = ../gsoap-2.8/gsoap 
CC = gcc -g -DWITH_NONAMESPACES  -DSOAP_DEBUG -DDEBUG
INCLUDE = -I$(GSOAP_ROOT)
SERVER_OBJS = soapC.o stdsoap2.o soapClient.o  onvif_server_interface.o soapServer.o duration.o onvif_server.o


all: server 
server: $(SERVER_OBJS)
	$(CC) $(INCLUDE) -o deviceserver -lpthread $(SERVER_OBJS)
 
clean: 
	rm -f *.o deviceprobe  deviceserver onvif

 七、编译

#:make

报 错:

Linux下gSoap编译Onvif框架详细图文教程_第10张图片

问题处理:

Linux下gSoap编译Onvif框架详细图文教程_第11张图片

八、运行,并用onvif测试工具测试

Linux下gSoap编译Onvif框架详细图文教程_第12张图片

 

工程连接:discover工程、gsoap源码包


你可能感兴趣的:(流媒体)