
正文
Linux下onvif客户端获取ipc摄像头 获取能力:GetCapabilities
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
GetCapabilities:获取能力,主要目的获取设备能力信息(获取媒体服务地址)
鉴权: 但是在调用获取设备能力之前是需要鉴权的。ONVIF协议规定,部分接口需要鉴权,部分接口不需要鉴权,在调用需要鉴权的接口时不使用鉴权,会导致接口调用失败。实现鉴权的方式之一可以调用gSOAP源码中的 soap_wsse_add_UsernameTokenDigest()函数。要安装依赖库OpenSSL
实现代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h> #include "soapH.h"
#include "stdsoap2.h"
#include "soapStub.h"
#include "wsseapi.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[];
char _HwId[];
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 > )
{
soap->recv_timeout = timeout;
soap->send_timeout = timeout;
soap->connect_timeout = timeout;
}
else
{
//Maximum waittime : 20s
soap->recv_timeout = ;
soap->send_timeout = ;
soap->connect_timeout = ;
} 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());
Flagrand = rand()% + ;
macaddr[] = 0x1;
macaddr[] = 0x2;
macaddr[] = 0x3;
macaddr[] = 0x4;
macaddr[] = 0x5;
macaddr[] = 0x6;
sprintf(_HwId, "urn:uuid:%ud68a-1dd2-11b2-a105-%02X%02X%02X%02X%02X%02X", Flagrand, macaddr[], macaddr[], macaddr[],macaddr[],macaddr[],macaddr[]);
header->wsa__MessageID = (char *)malloc();
memset(header->wsa__MessageID, , );
strncpy(header->wsa__MessageID, _HwId, strlen(_HwId)); //wsa__MessageID存放的是uuid if(was_Action != NULL)
{
header->wsa__Action = (char*)malloc();
memset(header->wsa__Action, '\0', );
strncpy(header->wsa__Action, was_Action, ); //
}
if(was_To != NULL)
{
header->wsa__To = (char *)malloc();
memset(header->wsa__To, '\0', );
strncpy(header->wsa__To, was_To, );//"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
} //鉴权
static int ONVIF_SetAuthInfo(struct soap *soap, const char *username, const char *password)
{
int result = ;
if((NULL != username) || (NULL != password)){
soap_wsse_add_UsernameTokenDigest(soap, NULL, username, password);
}else{
printf("un etAuth\n");
result = -;
} return result;
} int main(int argc,char *argv[])
{
int ret = ;
char sercer_addr[] = "http://172.168.0.211/onvif/device_service"; //设备搜索得到的地址 struct SOAP_ENV__Header header;
struct soap* soap = ONVIF_Initsoap(&header, NULL, NULL, ); struct _tds__GetCapabilities *req;
struct _tds__GetCapabilitiesResponse *Response; if(NULL == (req = (struct _tds__GetCapabilities *)calloc(,sizeof(struct _tds__GetCapabilities))))
{
printf("calloc is error \n");
ret = -;
return ret;
}else{
req->__sizeCategory = ;
req->Category = (enum tt__CapabilityCategory *)soap_malloc(soap, sizeof(int));
*(req->Category) = (enum tt__CapabilityCategory); //5表示:tt__CapabilityCategory__Media ONVIF_SetAuthInfo(soap,"admin","hk123456"); //鉴权,输入摄像头的用户名、密码
ret = soap_call___tds__GetCapabilities(soap, sercer_addr, NULL,req, Response);
if(soap->error){
ret = -;
printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
return ret;
}else{;
if(NULL != Response->Capabilities)
{
if (Response->Capabilities->Media != NULL){
if (Response->Capabilities->Media->XAddr != NULL){
printf(" media_addr: %s \n", Response->Capabilities->Media->XAddr);
}
}
}
}
} if(NULL != req)
{
free(req);
req = NULL;
} ONVIF_soap_delete(soap);
return ret;
}
在编译之前要把:stdsoap2.c soapC.c md5.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapClient.c 把这些.c 还有有些配套的.h 拷贝到当前工作目录下。
如果出现以下错误要在编译的时候加:-DWITH_OPENSSL
/tmp/cccao7U5.o:在函数‘soap_mec_init’中:
mecevp.c:(.text+0xe):对‘soap_ssl_init’未定义的引用
/tmp/ccV2yl3Q.o:在函数‘soap_smd_init’中:
smdevp.c:(.text+0x24c):对‘soap_ssl_init’未定义的引用
编译:gcc -o test get_GetCapabilities_test.c stdsoap2.c soapC.c md5.c dom.c mecevp.c smdevp.c threads.c wsaapi.c wsseapi.c soapClient.c -I import/ -DWITH_OPENSSL -lssl -lcrypto -ldl
结果:成功得到我们想要的媒体地址







