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

#include  
#include  
#include  
#include


#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{
arg_sarrScopes.Add(t_strSwap.Left(nPos));
}
else
{
break;
}
 
}//end for(?; nPos

}


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 *arg_vecDiscoveryList)
{
std::vector *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 m_vecDiscoveryList;

CString m_strGuidString;


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

{

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

}




你可能感兴趣的:(转载 CSDN上的 onvif协议的设备发现实现代码 RemoteDiscovery)