欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux下onvif客户端关于ipc摄像头的搜索

程序员文章站 2022-04-17 21:09:54
设备搜索:要访问一个IPC摄像头,或者说要调用IPC摄像头提供的WEB服务接口,就要先知道其IP地址,这就是设备发现的过程,或者叫设备搜索的过程。IPC摄像头用的是239.255.255.250(端口3702),所以设备搜索的原理是,只要在设备上服务器监听239.255.255.250的3702端口 ......

设备搜索:要访问一个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" ,这个头文件,加上即可。

Linux下onvif客户端关于ipc摄像头的搜索

 

7、编译,生成可执行文件test:gcc -o test search_test.c  stdsoap2.c  soapc.c  soapclient.c -i import/

Linux下onvif客户端关于ipc摄像头的搜索

 

8、运行test: ./test

Linux下onvif客户端关于ipc摄像头的搜索

 

设备搜索已完成!

设备搜索的主要目的是获取他服务器的地址:http://172.168.0.216/onvif/device_service,为下一步获取能力做准备。