onvif 客户端 linux,Linux下onvif客户端关于ipc摄像头的搜索
設備搜索:要訪問一個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 #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環境變量
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);//設置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;returnsoap;
}//釋放函數
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;//用于發送Probe消息
struct __wsdd__ProbeMatches resp; //用于接收Probe應答
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/"; //設備上服務器監聽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);returnretval;
}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,為下一步獲取能力做準備。
總結
以上是生活随笔為你收集整理的onvif 客户端 linux,Linux下onvif客户端关于ipc摄像头的搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下写sql语言,如何写这个sq
- 下一篇: linux空指针异常能捕获到吗,一次ke