基于 https://blog.csdn.net/gubenpeiyuan/article/details/25618177#
1、下载 gSOAP
2、解压 gsoap_2.8.114.zip , 找到目录gsoap\bin\win32,设置为Path,或者复制文件夹下的exe到Path。
3、创建生成源码目录,并在gsoap解压目录的一下文件拷贝到generate目录 gsoap\typemap.dat ,gsoap\stdsoap2.h,stdsoap2.c,gsoap\dom.c
4 、生成onvif头文件
5、生成onvif代码
6、generated文件夹中所有源码添加到vs工工程
7、解决openssl库的引用
操作CMD脚本:
set GSOAP_PATH=D:\gsoap_2.8.114\gsoap-2.8\gsoap
mkdir generated
cd generated
copy %GSOAP_PATH%\typemap.dat .\
copy %GSOAP_PATH%\stdsoap2.h .\
copy %GSOAP_PATH%\stdsoap2.c .\
copy %GSOAP_PATH%\dom.c .\
%GSOAP_PATH%/bin/win32/wsdl2h -o onvif.h -c -s -t ./typemap.dat http://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl http://www.onvif.org/onvif/ver10/event/wsdl/event.wsdl http://www.onvif.org/onvif/ver10/display.wsdl http://www.onvif.org/onvif/ver10/deviceio.wsdl http://www.onvif.org/onvif/ver20/imaging/wsdl/imaging.wsdl http://www.onvif.org/onvif/ver20/ptz/wsdl/ptz.wsdl http://www.onvif.org/onvif/ver10/receiver.wsdl http://www.onvif.org/onvif/ver10/recording.wsdl http://www.onvif.org/onvif/ver10/search.wsdl http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl http://www.onvif.org/onvif/ver10/replay.wsdl http://www.onvif.org/onvif/ver20/analytics/wsdl/analytics.wsdl http://www.onvif.org/onvif/ver10/analyticsdevice.wsdl http://www.onvif.org/ver10/actionengine.wsdl http://www.onvif.org/ver10/pacs/accesscontrol.wsdl http://www.onvif.org/ver10/pacs/doorcontrol.wsdl
%GSOAP_PATH%/bin/win32/soapcpp2 -c onvif.h -x -I%GSOAP_PATH%/import -I%GSOAP_PATH%/ -I%GSOAP_PATH%/gsoap/custom -I%GSOAP_PATH%/extras -I%GSOAP_PATH%/plugin
test.c
#include <soapH.h>
// gsoap gen
#include "RemoteDiscoveryBinding.nsmap"
#define ONVIF_USER "admin"
#define ONVIF_PASSWORD "admin"
void UserGetCapabilities(struct soap* soap, struct __wsdd__ProbeMatches* resp,
struct _tds__GetCapabilities* capa_req, struct _tds__GetCapabilitiesResponse* capa_resp)
{
capa_req->Category = (enum tt__CapabilityCategory*)soap_malloc(soap, sizeof(int));
capa_req->__sizeCategory = 1;
*(capa_req->Category) = (enum tt__CapabilityCategory)(tt__CapabilityCategory__Media);
capa_resp->Capabilities = (struct tt__Capabilities*)soap_malloc(soap, sizeof(struct tt__Capabilities));
//soap_wsse_add_UsernameTokenDigest(soap, "user", ONVIF_USER, ONVIF_PASSWORD);
printf("\n--------------------Now Gettting Capabilities NOW --------------------\n\n");
int result = soap_call___tds__GetCapabilities(soap, resp->wsdd__ProbeMatches->ProbeMatch->XAddrs, NULL, capa_req, capa_resp);
if (soap->error)
{
printf("[%s][%d]--->>> soap error: %d, %s, %s\n", __func__, __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
int retval = soap->error;
exit(-1);
}
else
{
printf(" \n--------------------GetCapabilities OK! result=%d--------------\n \n", result);
if (capa_resp->Capabilities == NULL)
{
printf(" GetCapabilities failed! result=%d \n", result);
}
else
{
printf(" Media->XAddr=%s \n", capa_resp->Capabilities->Media->XAddr);
}
}
}
void UserGetProfiles(struct soap* soap, struct _trt__GetProfiles* trt__GetProfiles,
struct _trt__GetProfilesResponse* trt__GetProfilesResponse, struct _tds__GetCapabilitiesResponse* capa_resp)
{
int result = 0;
printf("\n-------------------Getting Onvif Devices Profiles--------------\n\n");
//soap_wsse_add_UsernameTokenDigest(soap, "user", ONVIF_USER, ONVIF_PASSWORD);
result = soap_call___trt__GetProfiles(soap, capa_resp->Capabilities->Media->XAddr, NULL, trt__GetProfiles, trt__GetProfilesResponse);
if (result == -1)
//NOTE: it may be regular if result isn't SOAP_OK.Because some attributes aren't supported by server.
//any question email leoluopy@gmail.com
{
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
exit(-1);
}
else {
printf("\n-------------------Profiles Get OK--------------\n\n");
if (trt__GetProfilesResponse->Profiles != NULL)
{
if (trt__GetProfilesResponse->Profiles->Name != NULL) {
printf("Profiles Name:%s \n", trt__GetProfilesResponse->Profiles->Name);
}
if (trt__GetProfilesResponse->Profiles->token != NULL) {
printf("Profiles Taken:%s\n", trt__GetProfilesResponse->Profiles->token);
}
}
else {
printf("Profiles Get inner Error\n");
}
}
printf("Profiles Get Procedure over\n");
}
//
void UserGetUri(struct soap* soap, struct _trt__GetStreamUri* trt__GetStreamUri, struct _trt__GetStreamUriResponse* trt__GetStreamUriResponse,
struct _trt__GetProfilesResponse* trt__GetProfilesResponse, struct _tds__GetCapabilitiesResponse* capa_resp)
{
int result = 0;
trt__GetStreamUri->StreamSetup = (struct tt__StreamSetup*)soap_malloc(soap, sizeof(struct tt__StreamSetup));//初始化,分配空间
trt__GetStreamUri->StreamSetup->Stream = 0;//stream type
trt__GetStreamUri->StreamSetup->Transport = (struct tt__Transport*)soap_malloc(soap, sizeof(struct tt__Transport));//初始化,分配空间
trt__GetStreamUri->StreamSetup->Transport->Protocol = 0;
trt__GetStreamUri->StreamSetup->Transport->Tunnel = 0;
trt__GetStreamUri->StreamSetup->__size = 1;
trt__GetStreamUri->StreamSetup->__any = NULL;
trt__GetStreamUri->StreamSetup->__anyAttribute = NULL;
for (int j = 0; j < trt__GetProfilesResponse->__sizeProfiles; j++) {
trt__GetStreamUri->ProfileToken = trt__GetProfilesResponse->Profiles[j].token;
printf("\n\n---------------Getting Uri----------------\n\n");
//soap_wsse_add_UsernameTokenDigest(soap, "user", ONVIF_USER, ONVIF_PASSWORD);
soap_call___trt__GetStreamUri(soap, capa_resp->Capabilities->Media->XAddr, NULL, trt__GetStreamUri, trt__GetStreamUriResponse);
if (soap->error) {
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
}
else {
printf("!!!!NOTE: RTSP Addr Get Done is :%s \n", trt__GetStreamUriResponse->MediaUri->Uri);
}
}
}
#define MULTICAST_ADDRESS "soap.udp://239.255.255.250:3702"
int main()
{
printf("[%s][%d][%s][%s] start \n", __FILE__, __LINE__, __TIME__, __func__);
int result = 0;
wsdd__ProbeType req;
struct __wsdd__ProbeMatches resp;
wsdd__ScopesType sScope;
struct SOAP_ENV__Header header;
struct soap* soap;
soap = soap_new();
if (NULL == soap)
{
printf("sopa new error\r\n");
return -1;
}
soap->recv_timeout = 10;
soap_set_namespaces(soap, namespaces);
soap_default_SOAP_ENV__Header(soap, &header);
uuid_t uuid;
char guid_string[100] = "7D170CCD-B90E-417A-A4FF-A9A0050AD756";
//uuid_generate(uuid);
//uuid_unparse(uuid, guid_string);
header.wsa__MessageID = guid_string;
header.wsa__To = "urn:schemas-xmlsoap-org:ws:2005:04:discovery";
header.wsa__Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe";
soap->header = &header;
soap_default_wsdd__ScopesType(soap, &sScope);
sScope.__item = "";
soap_default_wsdd__ProbeType(soap, &req);
req.Scopes = &sScope;
req.Types = ""; //"dn:NetworkVideoTransmitter";
int i = 0;
result = soap_send___wsdd__Probe(soap, MULTICAST_ADDRESS, NULL, &req);
while (result == SOAP_OK)
{
result = soap_recv___wsdd__ProbeMatches(soap, &resp);
if (result == SOAP_OK)
{
if (soap->error)
{
printf("soap error 1: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
}
else
{
break;
}
}
else if (soap->error)
{
printf("[%d] soap error 2: %d, %s, %s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
result = soap->error;
}
}
if (result == SOAP_OK) {
printf("guog *********************************************\r\n");
if (soap->header->wsa__MessageID)
{
printf("MessageID : %s\r\n", soap->header->wsa__MessageID);
}
if (soap->header->wsa__RelatesTo && soap->header->wsa__RelatesTo->__item)
{
printf("RelatesTo : %s\r\n", soap->header->wsa__RelatesTo->__item);
}
if (soap->header->wsa__To)
{
printf("To : %s\r\n", soap->header->wsa__To);
}
if (soap->header->wsa__Action)
{
printf("Action : %s\r\n", soap->header->wsa__Action);
}
for (i = 0; i < resp.wsdd__ProbeMatches->__sizeProbeMatch; i++)
{
printf("__sizeProbeMatch : %d\r\n", resp.wsdd__ProbeMatches->__sizeProbeMatch);
printf("wsa__EndpointReference : %p\r\n", &resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference);
printf("Target EP Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference.Address);
printf("Target Type : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Types);
printf("Target Service Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->XAddrs);
printf("Target Metadata Version : %d\r\n", resp.wsdd__ProbeMatches->ProbeMatch->MetadataVersion);
if (resp.wsdd__ProbeMatches->ProbeMatch->Scopes)
{
printf("Target Scopes Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Scopes->__item);
}
struct _tds__GetCapabilities capa_req;
struct _tds__GetCapabilitiesResponse capa_resp;
UserGetCapabilities(soap, &resp, &capa_req, &capa_resp);
struct _trt__GetProfiles trt__GetProfiles;
struct _trt__GetProfilesResponse trt__GetProfilesResponse;
UserGetProfiles(soap, &trt__GetProfiles, &trt__GetProfilesResponse, &capa_resp);
struct _trt__GetStreamUri trt__GetStreamUri;
struct _trt__GetStreamUriResponse trt__GetStreamUriResponse;
UserGetUri(soap, &trt__GetStreamUri, &trt__GetStreamUriResponse, &trt__GetProfilesResponse, &capa_resp);
}
}
soap_destroy(soap);
soap_end(soap);
soap_free(soap);
printf("[%d] guog discover over !\n", __LINE__);
return result;
}