设备搜索:要访问一个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.8\gsoap目录下的两个文件: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\n", __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, '\0', 1024); strncpy(header->wsa__Action, was_Action, 1024); // } if(was_To != NULL) { header->wsa__To = (char *)malloc(1024); memset(header->wsa__To, '\0', 1024); strncpy(header->wsa__To, was_To, 1024);//"urn:schemas-xmlsoap-org:ws:2005:04:discovery"; } soap->header = header; return soap; } //释放函数 void ONVIF_soap_delete(struct soap *soap) { soap_destroy(soap); // remove deserialized class instances (C++ only) soap_end(soap); // Clean up deserialized data (except class instances) and temporary data soap_free(soap); // Reset and deallocate the context created with soap_new or soap_copy } int ONVIF_ClientDiscovery() { int FoundDevNo = 0; int retval = SOAP_OK; wsdd__ProbeType req; // 用于发送Probe消息 struct __wsdd__ProbeMatches resp; // 用于接收Probe应答 wsdd__ScopesType sScope; struct SOAP_ENV__Header header; struct soap* soap; const char *was_To = "urn:schemas-xmlsoap-org:ws:2005:04:discovery"; const char *was_Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"; //IP Adress and PortNo, broadCast const char *soap_endpoint = "soap.udp://239.255.255.250:3702/"; //设备上服务器监听239.255.255.250的3702端口 //Create new soap object with info soap = ONVIF_Initsoap(&header, was_To, was_Action, 10); soap_default_SOAP_ENV__Header(soap, &header); soap->header = &header; soap_default_wsdd__ScopesType(soap, &sScope); // 设置寻找设备的范围 sScope.__item = NULL; soap_default_wsdd__ProbeType(soap, &req); // 设置寻找设备的类型 req.Scopes = &sScope; req.Types = NULL; //"dn:NetworkVideoTransmitter"; //sent the message broadcast and wait retval = soap_send___wsdd__Probe(soap, soap_endpoint, NULL, &req); // 向组播地址广播Probe消息 while(retval == SOAP_OK) { printf("**************1**************\n"); retval = soap_recv___wsdd__ProbeMatches(soap, &resp); if(retval == SOAP_OK) { if(soap->error) { printf("[%d]:recv soap error :%d, %s, %s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap)); retval = soap->error; } else //we find a device { FoundDevNo++; if(resp.wsdd__ProbeMatches->ProbeMatch != NULL && resp.wsdd__ProbeMatches->ProbeMatch->XAddrs != NULL) { printf("***** No %d Devices Information *****\n", FoundDevNo); printf("Device Service Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->XAddrs); printf("Device EP Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference.Address); printf("Device Type : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->Types); printf("Device Metadata Version: %d\r\n", resp.wsdd__ProbeMatches->ProbeMatch->MetadataVersion); printf("[%d]*********************************\n", __LINE__); } } } else if(soap->error) { if(FoundDevNo == 0) { printf("No Device found!\n"); retval = soap->error; } else { printf("Search end! Find %d Device! \n", FoundDevNo); retval = 0; } break; } } //释放函数 ONVIF_soap_delete(soap); return retval; } int main(int argc, char *argv[]) { if(ONVIF_ClientDiscovery() != 0) { printf("discover failed! \n"); return -1; } return 0; }
6、在编译时如果出现:对‘namespaces’未定义的引用,那是你在程序中没有加 #include "wsdd.nsmap" ,这个头文件,加上即可。
7、编译,生成可执行文件test:gcc -o test search_test.c stdsoap2.c soapC.c soapClient.c -I import/
8、运行test: ./test
设备搜索已完成!
设备搜索的主要目的是获取他服务器的地址:http://172.168.0.216/onvif/device_service,为下一步获取能力做准备。