学习Onvif的最关键步骤就是设备发现,一般来说开发的设备都是客户端,只要能被服务端正确发现就大功告成啦!

本文分别实现了客户端和服务端的识别流程,可以配合起来运行测试。

第一部分:实现Probe检测实例

代码目录:

\\192.168.0.234\work\gaoht\gsoap\test

\\192.168.0.234\work\gaoht\gsoap\probe-sample

Gsoap 2.8.15

onviftest 12.06

Wsdl 下载地址:Home - ONVIF Mandarin

选择链接另存为wsdl文件即可。

1 创建工作目录\\192.168.0.234\work\gaoht\gsoap\probe-sample,把remotediscovery.wsdl、soapcpp2、wsdl2h、WS-typemap.dat拷贝到此处。

cp /work/gaoht/gsoap/gsoap-2.8/gsoap/bin/linux386/soapcpp2   ./

cp /work/gaoht/gsoap/gsoap-2.8/gsoap/bin/linux386/ wsdl2h   ./

cp /work/gaoht/gsoap/gsoap-2.8/gsoap/WS/WS-typemap.dat  ./

Gsoap 2.8.15版本下的WS-typemap.dat是完整的,支持WS-Discovery。

Gsoap 2.8.3下的gsoap/typemap.dat需要手工添加命令空间。这个文件不同导致生成的RemoteDiscoveryBinding.nsmap前缀是不同的。

2 运行wsdl2h:

./wsdl2h -s -c  -t WS-typemap.dat -o remotediscovery.h  remotediscovery.wsdl

生成结果是remotediscovery.h

3 运行soapcpp2

用到了/gsoap-2.8/gsoap/import 中的头文件,必须指定import路径。

./soapcpp2  -c -L -x -I../gsoap-2.8/gsoap/import  remotediscovery.h

生成的文件是:

RemoteDiscoveryBinding.nsmap、wsdd.nsmap   这两者是相同的

soapC.c

soapH.h

soapClient.c

soapServer.c

soapStub.h

4 拷贝工作所需的文件到工作目录

    gsoap/stdsoap2.c

    gsoap/stdsoap2.h

    gsoap/plugin/wsaapi.c

    gsoap/plugin/wsaapi.h

   gsoap/plugin/wsddapi.c

   gsoap/plugin/ wsddapi.h

   gsoap/plugin/threads.h      这个文件被wsddapi.h包含,

   gsoap/plugin/cdatatype.h    这个文件被wsddapi.h包含

5 编写server端程序,创建deviceserver.c,内容如下:

/******************************************************************************

* Copyright 2010-2013

* FileName:     deviceserver.c

* Desc:  监视网络摄像头设备发现服务器文件

*

*

* Author:          Tom-hongtao.gao

* Date:    2013/05/30

* Notes:  数据结构使用链表

*

* -----------------------------------------------------------------

* Histroy: v1.0   2013/05/30, Tom-hongtao.gao create this file

*

******************************************************************************/

  

/*-------------------------------- Includes ----------------------------------*/

#include "uuid32.h" 

#include "soapStub.h"

#include "RemoteDiscoveryBinding.nsmap"

#include "wsddapi.h"

  

/*-------------------- Global Definitions and Declarations -------------------*/

/*----------------------- Constant / Macro Definitions -----------------------*/

/*------------------------ Type Declarations ---------------------------------*/

/*------------------------ Variable Declarations -----------------------------*/

/*------------------------ Function Prototype --------------------------------*/

/*------------------------ Function Implement --------------------------------*/

  

 

int main(int argc, char **argv) 

    int m, s;

    struct ip_mreq mcast;

   

    //初始化SOAP执行环境

    struct soap probe_soap;

    soap_init2(&probe_soap, SOAP_IO_UDP|SOAP_IO_FLUSH, SOAP_IO_UDP|SOAP_IO_FLUSH);   

    soap_set_namespaces(&probe_soap, namespaces); 

    /*下面的函数是将创建的SOAP与指定的监听端口进行绑定,即通过SOAP创建监听 

        套接字,并将该套接字绑定到指定端口,从而在该端口上监听Web服务请求,

        返回创建的套接字标识 */ 

    if(!soap_valid_socket(soap_bind(&probe_soap, NULL, 3702, 10)))

         {

                   soap_print_fault(&probe_soap, stderr);

                   exit(1);

         }

 

    mcast.imr_multiaddr.s_addr = inet_addr("239.255.255.250");

    mcast.imr_interface.s_addr = htonl(INADDR_ANY);

    if(setsockopt(probe_soap.master, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(mcast)) < 0)

    {

        printf("setsockopt error!\n");

        return 0;

    }  

    //成功绑定之后,便开始监听

    for (;;)

    { 

        //监听直到有连接请求

        s = soap_accept(&probe_soap); 

        if (s < 0) { 

            soap_print_fault(&probe_soap, stderr); 

            exit(-1); 

        } 

        fprintf(stderr, "Socket connection successful: slave socket = %d\n", s); 

        /*  连接成功后,便处理并响应请求,下面的函数就是用来处理并响应请求的,

            它封装了所有的处理与响应过程,在该函数中调用了本地所实现的相关Web

            服务方法,它所需要的所有请求信息都在SOAP结构体中。*/

        soap_serve(&probe_soap); 

        soap_destroy(&probe_soap);   //服务完成后,便撤销SOAP环境

        soap_end(&probe_soap);       //清除所有资源,关闭套接字

    }

   

    soap_done(&probe_soap);

    return 0; 

SOAP_FMAC5 int SOAP_FMAC6 SOAP_ENV__Fault(struct soap* soap, char *faultcode, char *faultstring, char *faultactor,

                            struct SOAP_ENV__Detail *detail, struct SOAP_ENV__Code *SOAP_ENV__Code,

                            struct SOAP_ENV__Reason *SOAP_ENV__Reason,

                            char *SOAP_ENV__Node, char *SOAP_ENV__Role,

                            struct SOAP_ENV__Detail *SOAP_ENV__Detail )

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    return 0;

}

void wsdd_event_Hello(struct soap *soap, unsigned int InstanceId, const char *SequenceId, unsigned int MessageNumber, const char *MessageID, const char *RelatesTo, const char *EndpointReference, const char *Types, const char *Scopes, const char *MatchBy, const char *XAddrs, unsigned int MetadataVersion)

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    return 0;

}

void wsdd_event_Bye(struct soap *soap, unsigned int InstanceId, const char *SequenceId, unsigned int MessageNumber, const char *MessageID, const char *RelatesTo, const char *EndpointReference, const char *Types, const char *Scopes, const char *MatchBy, const char *XAddrs, unsigned int *MetadataVersion)

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    return 0;

}

soap_wsdd_mode wsdd_event_Probe(struct soap *soap, const char *MessageID, const char *ReplyTo, const char *Types, const char *Scopes, const char *MatchBy, struct wsdd__ProbeMatchesType *matches)

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    printf("MessageID:%s\n", MessageID);

    printf("ReplyTo:%s\n",ReplyTo);

    printf("Types:%s\n",Types);

    printf("Scopes:%s\n",Scopes);

    printf("MatchBy:%s\n",MatchBy); 

   

    //测试代码

    soap_wsdd_init_ProbeMatches(soap,matches);

    soap_wsdd_add_ProbeMatch(soap,matches,

        "urn:uuid:464A4854-4656-5242-4530-313035394100",

        "ns1:NetworkVideoTransmitter",

        "onvif://www.onvif.org/type/video_encoder onvif://www.onvif.org/type/audio_encoder onvif://www.onvif.org/hardware/IPC-model onvif://www.onvif.org/name/IPC-model",

        NULL,

        "http://192.168.0.243/onvif/device_service",10);

    return 0;

}

void wsdd_event_ProbeMatches(struct soap *soap, unsigned int InstanceId, const char *SequenceId, unsigned int MessageNumber, const char *MessageID, const char *RelatesTo, struct wsdd__ProbeMatchesType *matches)

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    return 0;

}

soap_wsdd_mode wsdd_event_Resolve(struct soap *soap, const char *MessageID, const char *ReplyTo, const char *EndpointReference, struct wsdd__ResolveMatchType *match)

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    return 0;

}

void wsdd_event_ResolveMatches(struct soap *soap, unsigned int InstanceId, const char *SequenceId, unsigned int MessageNumber, const char *MessageID, const char *RelatesTo, struct wsdd__ResolveMatchType *match)

{

    printf("%s,%d\n",__FUNCTION__, __LINE__);

    return 0;

}

6 创建客户端程序 deviceprobe.c,代码如下:

/******************************************************************************

* Copyright 2010-2013 A

* FileName:     deviceprobe.c

* Desc:  监视网络摄像头设备发现文件

*

*

* Author:          Tom-hongtao.gao

* Date:    2013/05/30

* Notes:  数据结构使用链表

*

* -----------------------------------------------------------------

* Histroy: v1.0   2013/05/30, Tom-hongtao.gao create this file

*

******************************************************************************/

/*-------------------------------- Includes ----------------------------------*/

#include "uuid32.h"

//#include "stdsoap2.h"

//#include "soapH.h"

//#include "remotediscovery.h"

#include "soapStub.h"

#include "RemoteDiscoveryBinding.nsmap"

/*-------------------- Global Definitions and Declarations -------------------*/

/*----------------------- Constant / Macro Definitions -----------------------*/

/*------------------------ Type Declarations ---------------------------------*/

/*------------------------ Variable Declarations -----------------------------*/

/*------------------------ Function Prototype --------------------------------*/

/*------------------------ Function Implement --------------------------------*/

int main(int argc, char *argv[])

{

    /* 变量声明 */

    struct soap *soap;  //soap环境变量      

    struct wsdd__ProbeType req;   //用于发送消息描述   

    struct wsdd__ProbeType wsdd__Probe;

    struct __wsdd__ProbeMatches resp;

    //struct wsdd__ProbeMatchesType resp; //请求消息的回应

    struct wsdd__ScopesType sScope; //描述查找哪类的Web服务

    struct SOAP_ENV__Header header; //soap消息头描述

    int count = 0;  //获得的设信息备个数

    int result = 0;    //返回值        

   

   

   

    char uuid_string[64]; //存放uuid的(8-4-4-4-12)的格式

    printf("%s : %d  000 : \n ",__FUNCTION__, __LINE__);

   

    /************初始化*************/

    memset(uuid_string, 0, 64);

    strncpy(uuid_string,uuid_create_string(),36); //必须考虑到4个中间连字符

    printf("uuid=%s \n ",uuid_string);

   

    soap = soap_new(); //为soap申请变量空间,并初始化

    if(soap==NULL)

        return -1;

    soap_set_namespaces(soap, namespaces); //设置soap的namespaces

    printf("%s : %d   \n ",__FUNCTION__, __LINE__);

   

    soap->recv_timeout = 5; //超过5秒钟没有数据就退出

    soap_default_SOAP_ENV__Header(soap, &header);//将header设置为soap消息    头属性

    header.wsa__MessageID = uuid_string;

    header.wsa__To     = "urn:schemas-xmlsoap-org:ws:2005:04:discovery";

    header.wsa__Action = "http://schemas.xmllocal_soap.org/ws/2005/04/discovery/Probe";

    soap->header = &header; //设置soap头消息的ID

    printf("%s : %d   \n ",__FUNCTION__, __LINE__);

    /*设置所需寻找设备的类型和范围,二者至少设定一个,

      否则可能收到非ONVIF设备,出现异常*/

   

    //设置soap消息的请求服务属性

    soap_default_wsdd__ScopesType(soap, &sScope);

    //sScope.__item = "";     

    sScope.__item = "onvif://www.onvif.org";

    soap_default_wsdd__ProbeType(soap, &req);

    req.Scopes = &sScope;

   

    /*设置所需设备的类型,ns1为命名空间前缀,为wsdd.nsmap文件中

    {"tdn","http://www.onvif.org/ver10/network/wsdl"}的tdn,如果不是tdn,而是其它,

    例如ns1这里也要随之改为ns1                   */   

    req.Types = "ns1:NetworkVideoTransmitter";

   

   

    printf("%s : %d   \n ",__FUNCTION__, __LINE__);

   

    //调用gSoap接口  

    //soap_wsdd_Probe

    result = soap_send___wsdd__Probe(soap, "soap.udp://239.255.255.250:3702/", NULL, &req);

   

    printf("%s: %d, send probe request success!\n",__FUNCTION__, __LINE__);

    if(result==-1)       

    {

        printf("soap error: %d, %s, %s\n", soap->error, *soap_faultcode(soap), *soap_faultstring(soap));

        result = soap->error;

    }

    else

    {

        do

        {

            printf("%s: %d, begin receive probematch... \n",__FUNCTION__, __LINE__);

            printf("count=%d \n",count);

            //接收ProbeMatches,成功返回0,否则-1

            result = soap_recv___wsdd__ProbeMatches(soap, &resp);

            printf(" --soap_recv___wsdd__ProbeMatches() result=%d \n",result);

            if (result==-1) 

            {

                printf("Find %d devices!\n", count);

                break;

            }

            else

            {                     

                //读取服务端回应的Probematch消息

                printf("soap_recv___wsdd__Probe:  __sizeProbeMatch=%d\r\n",resp.wsdd__ProbeMatches->__sizeProbeMatch);

                printf("Target EP Address : %s\r\n",      resp.wsdd__ProbeMatches->ProbeMatch->wsa__EndpointReference.Address);

                printf("Target Type : %s\r\n",            resp.wsdd__ProbeMatches->ProbeMatch->Types);

                printf("Target Service Address : %s\r\n", resp.wsdd__ProbeMatches->ProbeMatch->XAddrs);

                printf("Target Metadata Version : %d\r\n",resp.wsdd__ProbeMatches->ProbeMatch->MetadataVersion);

                printf("Target Scopes Address : %s\r\n",  resp.wsdd__ProbeMatches->ProbeMatch->Scopes->__item);

                count++;

            }

        }while(1);

    }

   

    printf("%s : %d   \n ",__FUNCTION__, __LINE__);

    //清除soap

    soap_end(soap); // clean up and remove deserialized data

    soap_free(soap);//detach and free runtime context

    soap_done(soap); // detach context (last use and no longer in scope)

    return result;

}

7 创建uuid文件:md5c.c, md5c.h, uuid32.h, uuid32.c

 

8 创建makefile,内容如下:

GSOAP_ROOT = /work/gaoht/gsoap/gsoap-2.8/gsoap 

CC = gcc -g -DWITH_NONAMESPACES 

INCLUDE = -I$(GSOAP_ROOT)

SERVER_OBJS = soapC.o stdsoap2.o  soapClient.o soapServer.o md5c.o uuid32.o deviceserver.o  wsddapi.o  wsaapi.o

CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o md5c.o uuid32.o deviceprobe.o 

 

all: server client

server: $(SERVER_OBJS)

         $(CC) $(INCLUDE) -o deviceserver $(SERVER_OBJS)

client: $(CLIENT_OBJS)

         $(CC) $(INCLUDE) -o deviceprobe $(CLIENT_OBJS)

 

clean: 

         rm -f *.o deviceprobe  deviceserver

9 直接运行make all,生成应用程序deviceprobe、deviceserver。

tom@shelly-desktop:~/gsoap/probe-sample$ make all 

gcc -g -DWITH_NONAMESPACES      -c -o soapC.o soapC.c

gcc -g -DWITH_NONAMESPACES      -c -o stdsoap2.o stdsoap2.c

gcc -g -DWITH_NONAMESPACES      -c -o soapClient.o soapClient.c

gcc -g -DWITH_NONAMESPACES      -c -o soapServer.o soapServer.c

gcc -g -DWITH_NONAMESPACES      -c -o deviceserver.o deviceserver.c

deviceserver.c: In function ‘wsdd_event_Hello’:

deviceserver.c:115: warning: ‘return’ with a value, in function returning void

deviceserver.c: In function ‘wsdd_event_Bye’:

deviceserver.c:121: warning: ‘return’ with a value, in function returning void

deviceserver.c: In function ‘wsdd_event_ProbeMatches’:

deviceserver.c:146: warning: ‘return’ with a value, in function returning void

deviceserver.c: In function ‘wsdd_event_ResolveMatches’:

deviceserver.c:157: warning: ‘return’ with a value, in function returning void

gcc -g -DWITH_NONAMESPACES      -c -o wsddapi.o wsddapi.c

gcc -g -DWITH_NONAMESPACES      -c -o wsaapi.o wsaapi.c

gcc -g -DWITH_NONAMESPACES   -I/work/gaoht/gsoap/gsoap-2.8/gsoap   -o deviceserver soapC.o stdsoap2.o  soapClient.o soapServer.o deviceserver.o  wsddapi.o  wsaapi.o

gcc -g -DWITH_NONAMESPACES      -c -o md5c.o md5c.c

gcc -g -DWITH_NONAMESPACES      -c -o uuid32.o uuid32.c

uuid32.c: In function ‘uuid_create’:

uuid32.c:152: warning: integer constant is too large for ‘long’ type

uuid32.c: In function ‘uuid_to_string’:

uuid32.c:224: warning: format ‘%08lx’ expects type ‘long unsigned int’, but argument 8 has type ‘uint32’

uuid32.c: In function ‘uuid_to_timestamp’:

uuid32.c:277: warning: integer constant is too large for ‘long’ type

uuid32.c:286: warning: passing argument 1 of ‘localtime’ from incompatible pointer type

/usr/include/time.h:237: note: expected ‘const time_t *’ but argument is of type ‘int64 *’

gcc -g -DWITH_NONAMESPACES      -c -o deviceprobe.o deviceprobe.c

gcc -g -DWITH_NONAMESPACES   -I/work/gaoht/gsoap/gsoap-2.8/gsoap   -o deviceprobe soapC.o stdsoap2.o soapClient.o md5c.o uuid32.o deviceprobe.o  

tom@shelly-desktop:~/gsoap/probe-sample$

10 运行deviceserver,

使用onviftest 12.06工具测试:发现了服务器192.168.15.234这个设备。

  

11 运行deviceprobe,收到deviceserver、摄像头的应答。

发出probe命令, 收到服务器deviceserver应答:

紧接着又收到了摄像头的应答

   居然上传不了图片:::

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐