onvif 客户端 linux,Linux下onvif客户端关于ipc摄像头的搜索
設(shè)備搜索:要訪問(wèn)一個(gè)IPC攝像頭,或者說(shuō)要調(diào)用IPC攝像頭提供的WEB服務(wù)接口,就要先知道其IP地址,這就是設(shè)備發(fā)現(xiàn)的過(guò)程,或者叫設(shè)備搜索的過(guò)程。IPC攝像頭用的是239.255.255.250(端口3702),所以設(shè)備搜索的原理是,只要在設(shè)備上服務(wù)器監(jiān)聽(tīng)239.255.255.250的3702端口。ONVIF規(guī)范并沒(méi)有自己定義服務(wù)設(shè)備發(fā)現(xiàn)框架,而是復(fù)用了已經(jīng)很成熟的WS-Discovery標(biāo)準(zhǔn),根據(jù).wsdl的文件,用gsoap產(chǎn)生框架代碼,調(diào)用其產(chǎn)生的函數(shù)接口去實(shí)現(xiàn)設(shè)備的搜索。
1、gsoap框架代碼:https://blog.csdn.net/weixin_42432281/article/details/84818575
2、上一部如果完成,就直接略過(guò),將安裝的gsoap-2.8\gsoap目錄下的兩個(gè)文件:stdsoap2.c、stdsoap2.h拷貝到你工作目錄下
3、注釋stdsoap2.c如下代碼:不注釋的話會(huì)在編譯運(yùn)行的時(shí)候產(chǎn)生log日志,最后會(huì)發(fā)現(xiàn)磁盤(pán)已滿的現(xiàn)象。
/*
#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是我寫(xiě)的記錄文檔,不必在意,其他的文件都拷貝到這個(gè)目錄下
5、設(shè)備搜索的代碼:我是直接copy別人的代碼,做了一下修改(https://blog.csdn.net/saloon_yuan/article/details/27524875)
#include #include#include#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, inttimeout)
{struct soap *soap = NULL; //soap環(huán)境變量
unsigned char macaddr[6];char _HwId[1024];
unsignedintFlagrand;
soap=soap_new();if(soap ==NULL)
{
printf("[%d]soap = NULL\n", __LINE__);returnNULL;
}
soap_set_namespaces(soap, namespaces);//設(shè)置soap的namespaces,即設(shè)置命名空間//設(shè)置超時(shí)(超過(guò)指定時(shí)間沒(méi)有數(shù)據(jù)就退出)
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下叫g(shù)uid,linux下叫uuid),格式為urn:uuid:8-4-4-4-12,由系統(tǒng)隨機(jī)產(chǎn)生
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;returnsoap;
}//釋放函數(shù)
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
}intONVIF_ClientDiscovery()
{int FoundDevNo = 0;int retval =SOAP_OK;
wsdd__ProbeType req;//用于發(fā)送Probe消息
struct __wsdd__ProbeMatches resp; //用于接收Probe應(yīng)答
wsdd__ScopesType sScope;structSOAP_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/"; //設(shè)備上服務(wù)器監(jiān)聽(tīng)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); //設(shè)置尋找設(shè)備的范圍
sScope.__item =NULL;
soap_default_wsdd__ProbeType(soap,&req); //設(shè)置尋找設(shè)備的類(lèi)型
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;
}
}//釋放函數(shù)
ONVIF_soap_delete(soap);returnretval;
}int main(int argc, char *argv[])
{if(ONVIF_ClientDiscovery() != 0)
{
printf("discover failed! \n");return -1;
}return 0;
}
6、在編譯時(shí)如果出現(xiàn):對(duì)‘namespaces’未定義的引用,那是你在程序中沒(méi)有加 #include "wsdd.nsmap" ,這個(gè)頭文件,加上即可。
7、編譯,生成可執(zhí)行文件test:gcc -o test search_test.c ?stdsoap2.c ?soapC.c ?soapClient.c -I import/
8、運(yùn)行test:?./test
設(shè)備搜索已完成!
設(shè)備搜索的主要目的是獲取他服務(wù)器的地址:http://172.168.0.216/onvif/device_service,為下一步獲取能力做準(zhǔn)備。
總結(jié)
以上是生活随笔為你收集整理的onvif 客户端 linux,Linux下onvif客户端关于ipc摄像头的搜索的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux下写sql语言,如何写这个sq
- 下一篇: linux 其他常用命令