本文是Linux c++ onvif客户端开发系列文章之一:
- Linux c++ onvif客户端开发(1): 根据wsdl生成cpp源文件
- Linux c++ onvif客户端开发(2): 获取摄像头H264/H265 RTSP地址
- Linux c++ onvif客户端开发(3): 扫描设备
- Linux c++ onvif客户端开发(4): 扫描某个设备是否支持onvif
- Linux c++ onvif客户端开发(5):gsoap内存管理
-
Linux c++ onvif客户端开发(6):获取设备信息
可以先先看一下gsoap内存管理这篇文章。
先定义一个fault结构
struct OnvifFault {std::string code;std::string subcode;std::string str; // stringstd::string detail;
};
再对struct soap进行包装
class OnvifSoap {
public:OnvifSoap(int timeout);~OnvifSoap();void InitHeader();void InitProbeType(struct wsdd__ProbeType *probe);int Error() { return soap_->error; }// 返回的xml中soap:Faultstd::string FaultString();std::string FaultCode();std::string FaultSubcode();std::string FaultDetail();struct soap *Soap() const;void Destroy();// 填写Fault信息void FillFault(OnvifFault *fault = nullptr);private:struct soap *soap_;
};
实现
#define SOAP_PROBE_TO "urn:schemas-xmlsoap-org:ws:2005:04:discovery"
#define SOAP_PROBE_ACTION \"http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"#define SOAP_MCAST_ADDR "soap.udp://239.255.255.250:3702" // onvif规定的组播地址#define SOAP_ITEM "" // 寻找的设备范围
#define SOAP_TYPES "dn:NetworkVideoTransmitter" // 寻找的设备类型#define SOAP_SOCK_TIMEOUT (2) // socket超时时间(单秒秒)OnvifSoap::OnvifSoap(int timeout) {// There is no need to call soap_init to initialize the context// allocated with soap_new, since soap_new initializes the allocated// context.// https://www.genivia.com/doc/guide/html/group__group__context.html#ga87c20488b2dc680aaa7689b1d024989csoap_ = soap_new();if (!soap_)throw std::runtime_error("soap_new() fail");soap_set_namespaces(soap_, namespaces); // 设置soap的namespaces// 不正常数据设置成5sif (timeout <= 0)timeout = SOAP_SOCK_TIMEOUT;soap_->recv_timeout = timeout; // 设置超时(超过指定时间没有数据就退出)soap_->send_timeout = timeout;soap_->connect_timeout = timeout;#if defined(__linux__) || \defined(__linux) // 参考https://www.genivia.com/dev.html#client-c的修改:soap_->socket_flags = MSG_NOSIGNAL; // To prevent connection reset errors
#endifsoap_set_mode(soap_,SOAP_C_UTFSTRING); // 设置为UTF-8编码,否则叠加中文OSD会乱码
}OnvifSoap::~OnvifSoap() {soap_destroy(soap_); // delete managed C++ objectssoap_end(soap_); // delete managed memory。soap_mallocsoap_done(soap_); // stackedsoap_free(soap_); /* we're done with the context */
}/*** @brief 填充Header, 用于Probe操作*
<SOAP-ENV:Header><wsa:MessageID>urn:uuid:dc8f9f8a-05b2-45c2-a63e-f5b47636af83</wsa:MessageID><wsa:To
SOAP-ENV:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To><wsa:Action
SOAP-ENV:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>
</SOAP-ENV:Header>*/
void OnvifSoap::InitHeader() {// T * soap_new_T(struct soap*) allocates and initializes data of type T// in context-managed heap memory, managed data is deleted with// soap_destroy (deletes C++ objects) and soap_end (deletes all other// data), and you can also use soap_malloc to allocate uninitialized// context-managed memory.struct SOAP_ENV__Header *header = soap_new_SOAP_ENV__Header(soap_);header->wsa__MessageID = (char *)soap_wsa_rand_uuid(soap_);header->wsa__To = soap_strdup(soap_, SOAP_PROBE_TO);header->wsa__Action = soap_strdup(soap_, SOAP_PROBE_ACTION);soap_->header = header;
}/*** @brief 填充body Probe数据, 用于Probe操作* <SOAP-ENV:Body><wsdd:Probe><wsdd:Types>dn:NetworkVideoTransmitter</wsdd:Types><wsdd:Scopes/></wsdd:Probe></SOAP-ENV:Body>* @param probe*/
void OnvifSoap::InitProbeType(struct wsdd__ProbeType *probe) {// 用于描述查找哪类的Web服务struct wsdd__ScopesType *scope = soap_new_wsdd__ScopesType(soap_);// soap_default_wsdd__ScopesType(soap_, scope); // 设置寻找设备的范围scope->__item = soap_strdup(soap_, "");probe->Scopes = scope;probe->Types = soap_strdup(soap_, SOAP_TYPES); // 设置寻找设备的类型
}std::string OnvifSoap::FaultString() {const char *fault_string = soap_fault_string(soap_);if (!fault_string)return std::string();elsereturn std::string(fault_string);
}std::string OnvifSoap::FaultCode() {const char **code = soap_faultcode(soap_);if (code && *code)return std::string(*code);elsereturn std::string();
}std::string OnvifSoap::FaultSubcode() {const char *subcode = soap_fault_subcode(soap_);if (!subcode)return std::string();elsereturn std::string(subcode);
}std::string OnvifSoap::FaultDetail() {const char *detail = soap_fault_detail(soap_);if (!detail)return std::string();elsereturn std::string(detail);
}struct soap *OnvifSoap::Soap() const {return soap_;
}void OnvifSoap::Destroy() { soap_->destroy(); }void OnvifSoap::FillFault(OnvifFault *fault) {if (!fault)return;// strconst char *fault_string = soap_fault_string(soap_);if (fault_string)fault->str.assign(fault_string);// codeconst char **code = soap_faultcode(soap_);if (code && *code)fault->code.assign(*code);// subcodeconst char *subcode = soap_fault_subcode(soap_);if (subcode)fault->subcode.assign(subcode);// detailconst char *detail = soap_fault_detail(soap_);if (detail)fault->detail.assign(detail);
}