linux系统下,qtcreator实现onvif协议设备发现

一、开发环境

Fedora23
qtcreator4.0.3 based on Qt5.6.2
海康威视DS-2CD2512F-IS摄像机

二、创建工程

很喜欢用qtcreator,其强大的补全和检错功能,用起来十分舒服。因此使用qtcreator来创建这个工程。创建步骤如下
1. 打开qtcreator,选择New Project,由于作者要创建一个非qt工程,所以选择Non-Qt Project下的Plain C++ Application工程,这样就可以创建一个纯C++的工程了。
2. 创建完成后,将上一篇文章中得到的onvif框架代码文件全部拷贝到工程目录下,右键单击工程名,选择Add Existing Files…将拷贝过来的文件添加进工程中。

三、编写发现设备函数

static struct soap* ONVIF_Initsoap(struct SOAP_ENV__Header *header, const char *was_To, const char *was_Action, int timeout)
{
    struct soap *soap = NULL;
    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);

    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
    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));

    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;
}
int ONVIF_ClientDiscovery()
{

    int FoundDevNo = 0;
    int retval = SOAP_OK;
    wsdd__ProbeType req;
    struct __wsdd__ProbeMatches resp;
    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/";

    //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);

    //printf("[%d]:probe resault is %d\n", __LINE__, retval);
    //printf("[%d]:recv soap error :%d, %s, %s\n", __LINE__, soap->error, *soap_faultcode(soap), *soap_faultstring(soap));
    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;
        }
    }


    soap_destroy(soap);
    soap_end(soap);
    soap_free(soap);
    return retval;
}

这段代码来自http://blog.csdn.net/saloon_yuan/article/details/27524875

接下来在main函数中调用设备发现函数即可

int main(int argc, char *argv[])
{
    if(ONVIF_ClientDiscovery() != 0)
    {
        printf("discover failed! \n");
        return -1;
    }
    return 0;
}

记得包含wsdd.h,在文件开始添加#include "wsdd.h"
接下来编译、运行、即可正常发现设备了。

注意

发现设备功能不需要客户端和服务端在同一网段,发现设备的数据包是通过广播发出的。但是如果客户端和服务端处于不同网段,可能需要路由支持,要确保广播的数据包能够被服务端正常接收到。而且后续的获取设备能力、获取rtsp地址等功能需要客户端和服务端在同一网段,因此最好一开始就使客户端和服务端在同一网段。


刚刚接触onvif协议开发, 如有疏漏、偏差,欢迎各位指正,不胜感激。

你可能感兴趣的:(onvif)