linux设备上的Onvif 实现5:实现Probe命令检测设备

2024-06-23 08:38

本文主要是介绍linux设备上的Onvif 实现5:实现Probe命令检测设备,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

学习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应答:

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

   居然上传不了图片:::

这篇关于linux设备上的Onvif 实现5:实现Probe命令检测设备的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1086664

相关文章

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

Linux磁盘分区、格式化和挂载方式

《Linux磁盘分区、格式化和挂载方式》本文详细介绍了Linux系统中磁盘分区、格式化和挂载的基本操作步骤和命令,包括MBR和GPT分区表的区别、fdisk和gdisk命令的使用、常见的文件系统格式以... 目录一、磁盘分区表分类二、fdisk命令创建分区1、交互式的命令2、分区主分区3、创建扩展分区,然后

Linux中chmod权限设置方式

《Linux中chmod权限设置方式》本文介绍了Linux系统中文件和目录权限的设置方法,包括chmod、chown和chgrp命令的使用,以及权限模式和符号模式的详细说明,通过这些命令,用户可以灵活... 目录设置基本权限命令:chmod1、权限介绍2、chmod命令常见用法和示例3、文件权限详解4、ch

Linux内核之内核裁剪详解

《Linux内核之内核裁剪详解》Linux内核裁剪是通过移除不必要的功能和模块,调整配置参数来优化内核,以满足特定需求,裁剪的方法包括使用配置选项、模块化设计和优化配置参数,图形裁剪工具如makeme... 目录简介一、 裁剪的原因二、裁剪的方法三、图形裁剪工具四、操作说明五、make menuconfig

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python实现大文件切片上传及断点续传的方法

《使用Python实现大文件切片上传及断点续传的方法》本文介绍了使用Python实现大文件切片上传及断点续传的方法,包括功能模块划分(获取上传文件接口状态、临时文件夹状态信息、切片上传、切片合并)、整... 目录概要整体架构流程技术细节获取上传文件状态接口获取临时文件夹状态信息接口切片上传功能文件合并功能小

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

C#实现文件读写到SQLite数据库

《C#实现文件读写到SQLite数据库》这篇文章主要为大家详细介绍了使用C#将文件读写到SQLite数据库的几种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录1. 使用 BLOB 存储文件2. 存储文件路径3. 分块存储文件《文件读写到SQLite数据库China编程的方法》博客中,介绍了文

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实