转载 CSDN上的 onvif协议的设备发现实现代码 RemoteDiscovery

转载 2013年12月04日 17:27:47

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdint.h>


#include "onvif/soapH.h"
#include "onvif/stdsoap2.h"
#include "onvif/onvif.nsmap"


#include "onvif/soapCreatePullPointBindingProxy.h"
#include "onvif/soapDeviceBindingProxy.h"
#include "onvif/soapDiscoveryLookupBindingProxy.h"
#include "onvif/soapEventBindingProxy.h"
#include "onvif/soapMediaBindingProxy.h"
#include "onvif/soapNotificationConsumerBindingProxy.h"
#include "onvif/soapNotificationProducerBindingProxy.h"
#include "onvif/soapPausableSubscriptionManagerBindingProxy.h"
#include "onvif/soapPullPointBindingProxy.h"
#include "onvif/soapPullPointSubscriptionBindingProxy.h"
#include "onvif/soapReceiverBindingProxy.h"
#include "onvif/soapSubscriptionManagerBindingProxy.h"
#include "onvif/soapwsddProxy.h"
#include "onvif/soapRemoteDiscoveryBindingProxy.h"


#pragma comment(lib,"Rpcrt4.lib")


class COnvifNetworkVideoTransmitterDeviceInfo :
public CObject
{
private:
CString m_strIP;
CString m_strUUID;
CString m_strDeviceEPAddress;
CString m_strDeviceTypes;
CString m_strDeviceServiceAddresss; //WebServiceURL
CString m_strUserName;
CString m_strPassword;
CString m_strScopes;
CString m_strMetadataVersion;
CString m_strBrand;
CString m_strModel;
CString m_strSerialNumber;
CString m_strFirmwareVersion;


public:
CString m_strGuid;
CString m_strGuidString;


public:
COnvifNetworkVideoTransmitterDeviceInfo(void);
~COnvifNetworkVideoTransmitterDeviceInfo(void);


public:
void SetDeviceIP(CString &arg_strIP);
void SetDeviceUUID(CString &arg_strUUID);
void SetDeviceTypes(CString &arg_strTypes);
void SetDeviceEPAddress(CString &arg_strEPAddress);
void SetWebServiceURL(CString &arg_strWebSrvUrl);
void SetUserName(CString &arg_strUserName);
void SetPassword(CString &arg_strPassword);
void SetScopes(CString &arg_strScopes);
void SetMetadataVersion(CString &arg_strMetaVersion);
void SetBrand(CString &arg_strBrand);
void SetModel(CString &arg_strModel);
void SetSN(CString &arg_strSerialNumber);
void SetFirmwareVersion(CString &arg_strFirmwareVersion);


CString GetDeviceIP(void);
CString GetDeviceUUID(void);
CString GetDeviceTypes(void);
CString GetDeviceEPAddress(void);
CString GetWebServiceURL(void);
CString GetUserName(void);
CString GetPassword(void);
void GetScopes(CStringArray &arg_sarrScopes);
CString GetMetadataVersion(void);
CString GetBrand(void);
CString GetModel(void);
CString GetSN(void);
CString GetFirmwareVersion(void);


};


COnvifNetworkVideoTransmitterDeviceInfo::COnvifNetworkVideoTransmitterDeviceInfo(void)
{


m_strIP = "";
m_strUUID = "";
m_strDeviceEPAddress = "";
m_strDeviceTypes = "";
m_strDeviceServiceAddresss = ""; 
m_strUserName = "";
m_strPassword = "";
m_strScopes = "";
m_strMetadataVersion = "";
m_strBrand = "";
m_strModel = "";
m_strSerialNumber = "";
m_strFirmwareVersion = "";


}




COnvifNetworkVideoTransmitterDeviceInfo::~COnvifNetworkVideoTransmitterDeviceInfo(void)
{


m_strIP = "";
m_strUUID = "";
m_strDeviceEPAddress = "";
m_strDeviceTypes = "";
m_strDeviceServiceAddresss = ""; 
m_strUserName = "";
m_strPassword = "";
m_strScopes = "";
m_strMetadataVersion = "";
m_strBrand = "";
m_strModel = "";
m_strSerialNumber = "";
m_strFirmwareVersion = "";


}


void COnvifNetworkVideoTransmitterDeviceInfo::SetDeviceIP(CString &arg_strIP)
{
m_strIP = arg_strIP;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetDeviceUUID(CString &arg_strUUID)
{
m_strUUID = arg_strUUID;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetDeviceTypes(CString &arg_strTypes)
{
m_strDeviceTypes = arg_strTypes;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetDeviceEPAddress(CString &arg_strEPAddress)
{
m_strDeviceEPAddress = arg_strEPAddress;
SetDeviceUUID(m_strDeviceEPAddress.Right(36));
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetWebServiceURL(CString &arg_strWebSrvUrl)
{
m_strDeviceServiceAddresss = arg_strWebSrvUrl;


int nCount, nPos;
CString t_strNetAddr;
t_strNetAddr = m_strDeviceServiceAddresss.Right(m_strDeviceServiceAddresss.GetLength()-7);
nPos = t_strNetAddr.Find('//');
t_strNetAddr = t_strNetAddr.Left(nPos);
nCount = t_strNetAddr.GetLength();


nPos = t_strNetAddr.Find(':');
if (nPos>0)
{
nCount = nPos;
}


SetDeviceIP(m_strDeviceServiceAddresss.Mid(7,nCount));
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetUserName(CString &arg_strUserName)
{
m_strUserName = arg_strUserName;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetPassword(CString &arg_strPassword)
{
m_strPassword = arg_strPassword;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetScopes(CString &arg_strScopes)
{
m_strScopes = arg_strScopes;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetMetadataVersion(CString &arg_strMetaVersion)
{
m_strMetadataVersion = m_strMetadataVersion;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetBrand(CString &arg_strBrand)
{
m_strBrand = arg_strBrand;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetModel(CString &arg_strModel)
{
m_strModel = arg_strModel;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetSN(CString &arg_strSerialNumber)
{
m_strSerialNumber = arg_strSerialNumber;
}


void COnvifNetworkVideoTransmitterDeviceInfo::SetFirmwareVersion(CString &arg_strFirmwareVersion)
{
m_strFirmwareVersion = arg_strFirmwareVersion;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetDeviceIP(void)
{
return m_strIP;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetDeviceUUID(void)
{
return m_strUUID;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetDeviceTypes(void)
{
return m_strDeviceTypes;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetDeviceEPAddress(void)
{
return m_strDeviceEPAddress;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetWebServiceURL(void)
{
return m_strDeviceServiceAddresss;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetUserName(void)
{
return m_strUserName;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetPassword(void)
{
return m_strPassword;
}


void COnvifNetworkVideoTransmitterDeviceInfo::GetScopes(CStringArray &arg_sarrScopes)
{
CString t_strSwap = m_strScopes;
char cFindChar = 32;
char cBuffer[1000] = {0};
strcpy(cBuffer, m_strScopes.LockBuffer());

for (int nPos=0; t_strSwap.GetLength()>0; nPos++)
{
nPos = t_strSwap.Find(cFindChar, nPos);


if ( (nPos>0) && (nPos<t_strSwap.GetLength()) )
{
arg_sarrScopes.Add(t_strSwap.Left(nPos));
}
else
{
break;
}
 
}//end for(?; nPos<m_strScopes.GetLength();?)


}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetMetadataVersion(void)
{
return m_strMetadataVersion;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetBrand(void)
{
return m_strBrand;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetModel(void)
{
return m_strModel;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetSN(void)
{
return m_strSerialNumber;
}


CString COnvifNetworkVideoTransmitterDeviceInfo::GetFirmwareVersion(void)
{
return m_strFirmwareVersion;
}



//namespace

namespace HYTeam
{


GUID CreateGuid()
{
GUID guid;
CoCreateGuid(&guid);
return guid;
}


std::string GuidToString(const GUID &guid)
{
char buf[64] = {0};


_snprintf_s(
buf,
sizeof(buf),
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
guid.Data1, guid.Data2, guid.Data3, 
guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5],
guid.Data4[6], guid.Data4[7]);


return std::string(buf);
}


}//end namespace






SOAP_FMAC5 int SOAP_FMAC6 soap_discovery_start(struct soap *soap, const char *soap_endpoint, 
const char *soap_action, struct wsdd__ProbeType *d__Probe)
{
struct __wsdd__Probe soap_tmp___wsdd__Probe;


if (!soap_action)
soap_action = "http://www.onvif.org/ver10/network/wsdl/Probe";
soap->encodingStyle = NULL;
soap_tmp___wsdd__Probe.wsdd__Probe = d__Probe;
soap_begin(soap);
soap_serializeheader(soap);
soap_serialize___wsdd__Probe(soap, &soap_tmp___wsdd__Probe);
if (soap_begin_count(soap))
return soap->error;


if (soap->mode & SOAP_IO_LENGTH)
{
if (soap_envelope_begin_out(soap)
|| soap_putheader(soap)
|| soap_body_begin_out(soap)
|| soap_put___wsdd__Probe(soap, &soap_tmp___wsdd__Probe, "-wsdd:Probe", NULL)
|| soap_body_end_out(soap)
|| soap_envelope_end_out(soap))
return soap->error;
}
if (soap_end_count(soap))
return soap->error;
if (soap_connect(soap, soap_endpoint, soap_action)
|| soap_envelope_begin_out(soap)
|| soap_putheader(soap)
|| soap_body_begin_out(soap)
|| soap_put___wsdd__Probe(soap, &soap_tmp___wsdd__Probe, "-wsdd:Probe", NULL)
|| soap_body_end_out(soap)
|| soap_envelope_end_out(soap)
|| soap_end_send(soap))
return soap_closesock(soap);
return SOAP_OK;
}


SOAP_FMAC5 int SOAP_FMAC6 soap_discovery_recv(struct soap *soap, struct wsdd__ProbeMatchesType *d__ProbeMatches)
{
if (!d__ProbeMatches)
{
soap_closesock(soap);
return -1;
}


soap_default_wsdd__ProbeMatchesType(soap, d__ProbeMatches);


if (soap_begin_recv(soap)
|| soap_envelope_begin_in(soap)
|| soap_recv_header(soap)
|| soap_body_begin_in(soap))
return soap_closesock(soap);
soap_get_wsdd__ProbeMatchesType(soap, d__ProbeMatches, "wsdd:ProbeMatches", "wsdd:ProbeMatchesType");
if (soap->error)
{
soap_recv_fault(soap, 0);
return -1;
}
if (soap_body_end_in(soap)
|| soap_envelope_end_in(soap)
|| soap_end_recv(soap))
{
soap_closesock(soap);
return -1;
}
return SOAP_OK;
}


SOAP_FMAC5 int SOAP_FMAC6 soap_discovery_stop(struct soap *soap)
{
return soap_closesock(soap);
}




int Discoverymain(int argc, std::vector<COnvifNetworkVideoTransmitterDeviceInfo*> *arg_vecDiscoveryList)
{
std::vector<COnvifNetworkVideoTransmitterDeviceInfo*> *t_vecDiscoveryList = arg_vecDiscoveryList;
ConvifDeviceCliLibDlg *objDlg = ((ConvifDeviceCliLibDlg*)AfxGetMainWnd());



struct soap *soap; 
int result = 0; 
struct wsdd__ProbeType req;
struct wsdd__ProbeMatchesType resp;
struct wsdd__ScopesType sScope;
struct SOAP_ENV__Header header;
int count = 0;
char guid_string[100];
GUID uuid;


soap = soap_new(); 
if(soap==NULL)
return -1;


////////////////////////////////////////////////////////////////////////////
// discovery test
strcpy(guid_string, "urn:uuid:");
uuid = HYTeam::CreateGuid();
std::string  myguid_string = HYTeam::GuidToString(uuid);
strcpy(guid_string+9, myguid_string.c_str());


//获取本机的guid
CString m_strGuid, m_strGuidString;
m_strGuid.Insert(0, myguid_string.c_str());
m_strGuidString.Insert(0, guid_string);
objDlg->m_strGuidString = m_strGuidString;


soap_set_namespaces(soap, namespaces); 
//超过5秒钟没有数据就退出
soap->recv_timeout = 10;


soap_default_SOAP_ENV__Header(soap, &header);


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 = "onvif://www.onvif.org";


soap_default_wsdd__ProbeType(soap, &req);
req.Scopes = &sScope;
req.Types = "dn:NetworkVideoTransmitter";
CString  m_csShowMsg = "";
result = soap_discovery_start(soap, "soap.udp://239.255.255.250:3702", NULL, &req);


while(result==SOAP_OK)
{
result = soap_discovery_recv(soap, &resp); 
if(result==SOAP_OK)
{
if (soap->error) { 
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); 
result = soap->error; 

CString  m_csError;
m_csError.Format("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
m_csShowMsg += m_csError;

else
{
if (! (soap->header) )
{
continue;
}


CString  m_csTemp, m_csTemp2;
m_csShowMsg += "====================================================================\r\n";

COnvifNetworkVideoTransmitterDeviceInfo *objItemDiscovery = new COnvifNetworkVideoTransmitterDeviceInfo();
objItemDiscovery->m_strGuid = m_strGuid;


/*if(soap->header->wsa__MessageID)
{
m_csTemp.Format("MessageID   : %s\r\n", soap->header->wsa__MessageID);
m_csTemp2 += m_csTemp;
} */
if(soap->header->wsa__RelatesTo && soap->header->wsa__RelatesTo->__item)
{
m_csTemp.Format("RelatesTo   : %s\r\n", soap->header->wsa__RelatesTo->__item);
objItemDiscovery->m_strGuidString.Insert(0, soap->header->wsa__RelatesTo->__item);
m_csTemp2 += m_csTemp;
}
/*if(soap->header->wsa__To)
{
m_csTemp.Format("To          : %s\r\n", soap->header->wsa__To);
m_csTemp2 += m_csTemp;
}
if(soap->header->wsa__Action)
{
m_csTemp.Format("Action      : %s\r\n", soap->header->wsa__Action);
m_csTemp2 += m_csTemp;
}*/


if (soap->userid)
{
CString m_strUserName = "";
m_strUserName.Insert(0, soap->userid);
objItemDiscovery->SetUserName(m_strUserName);
}


if (soap->passwd)
{
CString m_strPassword;
m_strPassword.Insert(0, soap->passwd);
objItemDiscovery->SetPassword(m_strPassword);
}


//m_csTemp.Format("__sizeProbeMatch=%d\r\n",resp.__sizeProbeMatch);
//m_csTemp2 += m_csTemp;


CString m_strEPAddress = "";
m_strEPAddress.Insert(0, resp.ProbeMatch[0].wsa__EndpointReference.Address);
objItemDiscovery->SetDeviceEPAddress(m_strEPAddress);
//m_csTemp.Format("Target EP Address       : %s\r\n", resp.ProbeMatch[0].wsa__EndpointReference.Address);
//m_csTemp2 += m_csTemp;


CString m_strTypes = "";
m_strTypes.Insert(0, resp.ProbeMatch[0].Types);
objItemDiscovery->SetDeviceTypes(m_strTypes);
//m_csTemp.Format("Target Type             : %s\r\n", resp.ProbeMatch[0].Types);
//m_csTemp2 += m_csTemp;


CString m_strWebServiceURL = "";
m_strWebServiceURL.Insert(0, resp.ProbeMatch[0].XAddrs);
objItemDiscovery->SetWebServiceURL(m_strWebServiceURL);
//m_csTemp.Format("Target Service Address  : %s\r\n", resp.ProbeMatch[0].XAddrs);
//m_csTemp2 += m_csTemp;


CString m_strMetadataVersion = "";
m_strMetadataVersion.Insert(0, resp.ProbeMatch[0].MetadataVersion);
objItemDiscovery->SetMetadataVersion(m_strMetadataVersion);
//m_csTemp.Format("Target Metadata Version : %d\r\n", resp.ProbeMatch[0].MetadataVersion);
//m_csTemp2 += m_csTemp;


if(resp.ProbeMatch[0].Scopes)
{
CString m_strScopes;
m_strScopes.Insert(0, resp.ProbeMatch[0].Scopes->__item);
objItemDiscovery->SetScopes(m_strScopes);
//m_csTemp.Format("Target Scopes Address   : %s\r\n", resp.ProbeMatch[0].Scopes->__item);
//m_csTemp2 += m_csTemp;
}


t_vecDiscoveryList->push_back(objItemDiscovery);
//m_csShowMsg +=m_csTemp2;
//objDlg->m_ctlEdtShowMsg.SetWindowText(m_csShowMsg);
}//end if (soap->error)


}
else if (soap->error) { 
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); 
result = soap->error; 
}//end if(result==SOAP_OK) 


}//end while(result==SOAP_OK)


soap_discovery_stop(soap);


// remove deserialized class instances (C++ only)
soap_destroy(soap);


// clean up and remove deserialized data
soap_end(soap); 


////////////////////////////////////////////////////////////////////////////
failed:
soap_free(soap);//detach and free runtime context
soap_done(soap); // detach context (last use and no longer in scope)


return result; 
}


std::vector<COnvifNetworkVideoTransmitterDeviceInfo*> m_vecDiscoveryList;

CString m_strGuidString;


int main(int argc, char* argv[])

{

        int argc =1;
std::vector<COnvifNetworkVideoTransmitterDeviceInfo*> *arg_vecDiscoveryList= NULL;
arg_vecDiscoveryList = &m_vecDiscoveryList;
Discoverymain(argc, arg_vecDiscoveryList);

}




onvif开发之设备发现功能的实现

忙了一个多月,onvif总算告一段落了。这几个星期忙着其他的项目,也没有好好整理一下onvif的东西。接下来得好好整理一下自己的项目思路和项目经验,同时将自己的一些心得写出来,希望对人有所帮助。   ...
  • u011597695
  • u011597695
  • 2013年09月18日 21:45
  • 8094

使用Onvif协议进行设备发现以及指定设备信息探测

关于EasyNVR,可以移步基于EasyNVR实现RTSP/Onvif监控摄像头Web无插件化直播监控EasyNVR支持使用onvif协议对设备进行设备发现,云台控制,onvif地址获取等功能这里第一...
  • cai6811376
  • cai6811376
  • 2017年08月12日 01:26
  • 902

Onvif开源项目

ONVIF Device Manager http://synesis.ru/en/surveillance/onvifdm/ http://sourceforge.net/proje...
  • q339659207
  • q339659207
  • 2014年12月13日 23:59
  • 2884

Onvif开发框架

  • 2016年04月10日 22:36
  • 30.36MB
  • 下载

ONVIF 客户端简单实现Discovery,实现设备搜索.

两种方法,一种是使用GSOAP的库WS-Discovery,另一种是直接自己写一个SOCKET多播消息。 第一种解XML和定制XML方便,发送的消息也是标准的Discovery协议, 这种方法能收...
  • jhting
  • jhting
  • 2013年03月18日 12:09
  • 11739

onvif开发之设备发现功能的实现

忙了一个多月,onvif总算告一段落了。这几个星期忙着其他的项目,也没有好好整理一下onvif的东西。接下来得好好整理一下自己的项目思路和项目经验,同时将自己的一些心得写出来,希望对人有所帮助。   ...
  • tongjing524
  • tongjing524
  • 2015年01月04日 10:56
  • 603

onvif wsdl2java

在使用onvif2的wsdl转java代码时,一直在等待。。。。 后来发现onvif.xsd中 http://schemas.xmlsoap.org/soap/envelope/" t...
  • baowg1212
  • baowg1212
  • 2014年11月26日 16:40
  • 982

onvif规范的实现:成功实现ONVIF协议RTSP-Video-Stream与OnvifDeviceManager的视频对接

有了前几篇的基础,现在可以正式开始onvif的实现工作,其中一项非常重要的部分就是视频流的对接,即能够在符合onvif标准的监控客户端软件里接收到设备端NVT发来的RTSP视频流。这里,我所用的客户端...
  • ghostyu
  • ghostyu
  • 2012年11月22日 09:59
  • 116936

ONVIF协议开发例子程序—设备发现C/S

  • 2012年11月10日 22:00
  • 415KB
  • 下载

onvif设备发现大量设备(跨网段)

1.配置onvif设备IP和子网掩码,让所有onvif设备在同一网段里。(配置工作量太大) 2.设置发probe的主机IP,让它在每个网段都有一个IP。(容易IP冲突) 3.配置路由器,让它转发组播包...
  • qwerty26256
  • qwerty26256
  • 2015年09月11日 09:01
  • 1919
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:转载 CSDN上的 onvif协议的设备发现实现代码 RemoteDiscovery
举报原因:
原因补充:

(最多只允许输入30个字)