设备搜索:要访问一个IPC摄像头,或者说要调用IPC摄像头提供的WEB服务接口,就要先知道其IP地址,这就是设备发现的过程,或者叫设备搜索的过程。IPC摄像头用的是239.255.255.250(端口3702),所以设备搜索的原理是,只要在设备上服务器监听239.255.255.250的3702端口。ONVIF规范并没有自己定义服务设备发现框架,而是复用了已经很成熟的WS-Discovery标准,根据.wsdl的文件,用gsoap产生框架代码,调用其产生的函数接口去实现设备的搜索。
1、gsoap框架代码:https://blog.csdn.net/weixin_42432281/article/details/84818575
2、上一部如果完成,就直接略过,将安装的gsoap-2.8gsoap目录下的两个文件:stdsoap2.c、stdsoap2.h拷贝到你工作目录下
3、注释stdsoap2.c如下代码:不注释的话会在编译运行的时候产生log日志,最后会发现磁盘已满的现象。
/*
#ifdef SOAP_DEBUG
#ifdef TANDEM_NONSTOP
soap_set_test_logfile(soap, "TESTLOG");
soap_set_sent_logfile(soap, "SENTLOG");
soap_set_recv_logfile(soap, "RECVLOG");
#else
soap_set_test_logfile(soap, "TEST.log");
soap_set_sent_logfile(soap, "SENT.log");
soap_set_recv_logfile(soap, "RECV.log");
#endif
#endif
*/
和修改
if (/*s == r || *r || */n < -128 || n > 127)
4、将安装的gsoap2.8目录下的import目录,拷贝到生成.c、.h的工作的文件夹里,cp gsoap-2.8/gsoap/import ./ ,REAMOD.txt是我写的记录文档,不必在意,其他的文件都拷贝到这个目录下
5、设备搜索的代码:我是直接copy别人的代码,做了一下修改(https://blog.csdn.net/saloon_yuan/article/details/27524875)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "soapH.h" #include "stdsoap2.h" #include "soapStub.h" #include "wsdd.nsmap" //命名空间 static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout) { struct soap *soap = NULL; // soap环境变量 unsigned char macaddr[6]; char _HwId[1024]; unsigned int Flagrand; soap = soap_new(); if(soap == NULL) { printf("[%d]soap = NULL ", __LINE__); return NULL; } soap_set_namespaces(soap, namespaces); // 设置soap的namespaces,即设置命名空间 // 设置超时(超过指定时间没有数据就退出) if(timeout > 0) { soap->recv_timeout = timeout; soap->send_timeout = timeout; soap->connect_timeout = timeout; } else { //Maximum waittime : 20s soap->recv_timeout = 20; soap->send_timeout = 20; soap->connect_timeout = 20; } soap_default_SOAP_ENV__Header(soap, header); //Create SessionID randomly,生成uuid(windows下叫guid,linux下叫uuid),格式为urn:uuid:8-4-4-4-12,由系统随机产生 srand((int)time(0)); Flagrand = rand()%9000 + 8888; macaddr[0] = 0x1; macaddr[1] = 0x2; macaddr[2] = 0x3; macaddr[3] = 0x4; macaddr[4] = 0x5; macaddr[5] = 0x6; sprintf(_HwId, "urn:uuid:%ud68a-1dd2-11b2-a105-%02X%02X%02X%02X%02X%02X", Flagrand, macaddr[0], macaddr[1], macaddr[2],macaddr[3],macaddr[4],macaddr[5]); header->wsa__MessageID = (char *)malloc(100); memset(header->wsa__MessageID, 0, 100); strncpy(header->wsa__MessageID, _HwId, strlen(_HwId)); //wsa__MessageID存放的是uuid if(was_Action != NULL) { header->wsa__Action = (char*)malloc(1024); memset(header->wsa__Action, '