参考文章
https://blog.csdn.net/zhizhengguan/article/details/109311628
一、简介
客户端要做的第一件事情就是获取IPC摄像头的基本信息,即调用GetDeviceInformation接口。
有关GetDeviceInformation接口的描述,可以参阅devicemgmt.wsdl文档(https://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl),如下图所示:
二、相关数据结构和函数
1.数据结构
# 数据结构定义在client\application\soapStub.h
_tds__GetDeviceInformation devinfo_req;
_tds__GetDeviceInformationResponse devinfo_resp;
/* ../onvif_head/onvif.h:438 */
#ifndef SOAP_TYPE__tds__GetDeviceInformation
#define SOAP_TYPE__tds__GetDeviceInformation (455)
/* complex XML schema type 'tds:GetDeviceInformation': */
class SOAP_CMAC _tds__GetDeviceInformation {
public:
/// Context that manages this object
struct soap *soap;
public:
/// Return unique type id SOAP_TYPE__tds__GetDeviceInformation
virtual long soap_type(void) const { return SOAP_TYPE__tds__GetDeviceInformation; }
/// (Re)set members to default values
virtual void soap_default(struct soap*);
/// Serialize object to prepare for SOAP 1.1/1.2 encoded output (or with SOAP_XML_GRAPH) by analyzing its (cyclic) structures
virtual void soap_serialize(struct soap*) const;
/// Output object in XML, compliant with SOAP 1.1 encoding style, return error code or SOAP_OK
virtual int soap_put(struct soap*, const char *tag, const char *type) const;
/// Output object in XML, with tag and optional id attribute and xsi:type, return error code or SOAP_OK
virtual int soap_out(struct soap*, const char *tag, int id, const char *type) const;
/// Get object from XML, compliant with SOAP 1.1 encoding style, return pointer to object or NULL on error
virtual void *soap_get(struct soap*, const char *tag, const char *type);
/// Get object from XML, with matching tag and type (NULL matches any tag and type), return pointer to object or NULL on error
virtual void *soap_in(struct soap*, const char *tag, const char *type);
/// Return a new object of type _tds__GetDeviceInformation, default initialized and not managed by a soap context
virtual _tds__GetDeviceInformation *soap_alloc(void) const { return SOAP_NEW_UNMANAGED(_tds__GetDeviceInformation); }
public:
/// Constructor with default initializations
_tds__GetDeviceInformation() : soap() { }
/// Destructor
virtual ~_tds__GetDeviceInformation() { }
/// Friend allocator used by soap_new__tds__GetDeviceInformation(struct soap*, int)
friend SOAP_FMAC1 _tds__GetDeviceInformation * SOAP_FMAC2 soap_instantiate__tds__GetDeviceInformation(struct soap*, int, const char*, const char*, size_t*);
};
#endif
/* ../onvif_head/onvif.h:440 */
#ifndef SOAP_TYPE__tds__GetDeviceInformationResponse
#define SOAP_TYPE__tds__GetDeviceInformationResponse (456)
/* complex XML schema type 'tds:GetDeviceInformationResponse': */
class SOAP_CMAC _tds__GetDeviceInformationResponse {
public:
/// Required element 'tds:Manufacturer' of XML schema type 'xsd:string'
char *Manufacturer;
/// Required element 'tds:Model' of XML schema type 'xsd:string'
char *Model;
/// Required element 'tds:FirmwareVersion' of XML schema type 'xsd:string'
char *FirmwareVersion;
/// Required element 'tds:SerialNumber' of XML schema type 'xsd:string'
char *SerialNumber;
/// Required element 'tds:HardwareId' of XML schema type 'xsd:string'
char *HardwareId;
/// Context that manages this object
struct soap *soap;
public:
/// Return unique type id SOAP_TYPE__tds__GetDeviceInformationResponse
virtual long soap_type(void) const { return SOAP_TYPE__tds__GetDeviceInformationResponse; }
/// (Re)set members to default values
virtual void soap_default(struct soap*);
/// Serialize object to prepare for SOAP 1.1/1.2 encoded output (or with SOAP_XML_GRAPH) by analyzing its (cyclic) structures
virtual void soap_serialize(struct soap*) const;
/// Output object in XML, compliant with SOAP 1.1 encoding style, return error code or SOAP_OK
virtual int soap_put(struct soap*, const char *tag, const char *type) const;
/// Output object in XML, with tag and optional id attribute and xsi:type, return error code or SOAP_OK
virtual int soap_out(struct soap*, const char *tag, int id, const char *type) const;
/// Get object from XML, compliant with SOAP 1.1 encoding style, return pointer to object or NULL on error
virtual void *soap_get(struct soap*, const char *tag, const char *type);
/// Get object from XML, with matching tag and type (NULL matches any tag and type), return pointer to object or NULL on error
virtual void *soap_in(struct soap*, const char *tag, const char *type);
/// Return a new object of type _tds__GetDeviceInformationResponse, default initialized and not managed by a soap context
virtual _tds__GetDeviceInformationResponse *soap_alloc(void) const { return SOAP_NEW_UNMANAGED(_tds__GetDeviceInformationResponse); }
public:
/// Constructor with default initializations
_tds__GetDeviceInformationResponse() : Manufacturer(), Model(), FirmwareVersion(), SerialNumber(), HardwareId(), soap() { }
/// Destructor
virtual ~_tds__GetDeviceInformationResponse() { }
/// Friend allocator used by soap_new__tds__GetDeviceInformationResponse(struct soap*, int)
friend SOAP_FMAC1 _tds__GetDeviceInformationResponse * SOAP_FMAC2 soap_instantiate__tds__GetDeviceInformationResponse(struct soap*, int, const char*, const char*, size_t*);
};
#endif
2.函数
函数声明在client\application\soapClient.h
/** Web service synchronous operation 'soap_call___tds__GetDeviceInformation' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_call___tds__GetDeviceInformation(struct soap *soap, const char *soap_endpoint, const char *soap_action, _tds__GetDeviceInformation *tds__GetDeviceInformation, _tds__GetDeviceInformationResponse &tds__GetDeviceInformationResponse);
函数定义在client\application\soapClient.cpp
SOAP_FMAC5 int SOAP_FMAC6 soap_call___tds__GetDeviceInformation(struct soap *soap, const char *soap_endpoint, const char *soap_action, _tds__GetDeviceInformation *tds__GetDeviceInformation, _tds__GetDeviceInformationResponse &tds__GetDeviceInformationResponse)
{ if (soap_send___tds__GetDeviceInformation(soap, soap_endpoint, soap_action, tds__GetDeviceInformation) || soap_recv___tds__GetDeviceInformation(soap, tds__GetDeviceInformationResponse))
return soap->error;
return SOAP_OK;
}
三、获取设备基本信息实现
struct OnvifDeviceInfo
{
std::string manufacturer;
std::string model;
std::string firmwareVersion;
std::string serialNumber;
std::string hardwareId;
};
/************************************************************************
**函数:ONVIF_GetDeviceInformation
**功能:获取设备基本信息
**参数:
[in] DeviceXAddr - 设备服务地址
**返回:
0表明成功,非0表明失败
**备注:
************************************************************************/
int ONVIF_GetDeviceInformation(const std::string& deviceXAddr, struct OnvifDeviceInfo* DeviceInfo)
{
int result = 0;
struct soap *soap = nullptr;
_tds__GetDeviceInformation devinfo_req;
_tds__GetDeviceInformationResponse devinfo_resp;
SOAP_ASSERT(!deviceXAddr.empty());
SOAP_ASSERT(nullptr != (soap = ONVIF_soap_new(SOAP_SOCK_TIMEOUT)));
// 获取设备信息需要鉴权
ONVIF_SetAuthInfo(soap, USERNAME, PASSWORD);
result = soap_call___tds__GetDeviceInformation(soap, deviceXAddr.c_str(), nullptr, &devinfo_req, devinfo_resp);
SOAP_CHECK_ERROR(result, soap, "GetDeviceInformation");
DeviceInfo->manufacturer = devinfo_resp.Manufacturer;
DeviceInfo->model = devinfo_resp.Model;
DeviceInfo->firmwareVersion = devinfo_resp.FirmwareVersion;
DeviceInfo->serialNumber = devinfo_resp.SerialNumber;
DeviceInfo->hardwareId = devinfo_resp.HardwareId;
#if DEBUG
std::cout << "-------------------ONVIF_GetDeviceInformation----------------" << "\n";
std::cout << "Manufacturer:" << DeviceInfo->manufacturer << "\n";
std::cout << "Model:" << DeviceInfo->model << "\n";
std::cout << "FirmwareVersion:" << DeviceInfo->firmwareVersion;
std::cout << "SerialNumber:" << DeviceInfo->serialNumber << "\n";
std::cout << "HardwareId:" << DeviceInfo->hardwareId << "\n";
std::cout << "-------------------------------------------------------------" << "\n";
#endif
EXIT:
if (nullptr != soap) {
ONVIF_soap_delete(soap);
}
return result;
}
四、实现效果
-------------------ONVIF_GetDeviceInformation----------------
Manufacturer:N
Model:HD_Camera
FirmwareVersion:X8.02.91
SerialNumber:vf392b11e84
HardwareId:002
-------------------------------------------------------------