onvif设备发现

1)由onvif官方的wsdl链接生成onvif.h头文件

wsdl2h -c -s -t typemap.dat -o onvif.h http://www.onvif.org/onvif/ver10/network/wsdl/remotediscovery.wsdl

2)soapcpp2.exe把onvif.h生成对应框架代码

soapcpp2.exe -2 -c onvif.h -I..\gsoap-2.8\gsoap\import -I..\gsoap-2.8\

-2: 使用soap1.2版本
-c: 生成c代码
注:
a.路径为实际的路径,根据自己环境进行具体设置

3)根据我前面一篇文件把一些无用的文件删除掉
4)保留真正要用到的文件

2014/11/08  14:26           509,842 stdsoap2.c
2016/07/04  11:02            23,882 soapStub.h
2016/07/04  11:02            21,693 soapClient.c
2014/11/08  14:26            98,504 stdsoap2.h
2016/07/04  16:55            11,513 soapServer.c
2016/07/04  11:02               725 wsdd.nsmap
2016/07/04  11:02           215,157 soapC.c
2016/07/04  11:02            95,813 soapH.h

5)编写main.c,主要填充__wsdd__Probe函数,socket层接收到discovery组播包后会自动调用此函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "soapH.h"
#include "wsdd.nsmap"
#include "soapStub.h"


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

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Hello(struct soap* soap, struct wsdd__HelloType *wsdd__Hello)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Bye(struct soap* soap, struct wsdd__ByeType *wsdd__Bye)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__ProbeMatches(struct soap* soap, struct wsdd__ProbeMatchesType *wsdd__ProbeMatches)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__Resolve(struct soap* soap, struct wsdd__ResolveType *wsdd__Resolve)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __wsdd__ResolveMatches(struct soap* soap, struct wsdd__ResolveMatchesType *wsdd__ResolveMatches)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __tdn__Hello(struct soap* soap, struct wsdd__HelloType tdn__Hello, struct wsdd__ResolveType *tdn__HelloResponse)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __tdn__Bye(struct soap* soap, struct wsdd__ByeType tdn__Bye, struct wsdd__ResolveType *tdn__ByeResponse)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6 __tdn__Probe(struct soap* soap, struct wsdd__ProbeType tdn__Probe, struct wsdd__ProbeMatchesType *tdn__ProbeResponse)
{

    return 0;
}

SOAP_FMAC5 int SOAP_FMAC6  __wsdd__Probe(struct soap* soap, struct wsdd__ProbeType *wsdd__Probe)
{

    #define MACH_ADDR_LENGTH 6
    #define INFO_LENGTH 512
    #define LARGE_INFO_LENGTH 1024
    #define SMALL_INFO_LENGTH 512

    printf("[%d] __wsdd__Probe start !\n", __LINE__);

    unsigned char macaddr[6] = {0};
    char _IPAddr[INFO_LENGTH] = {0};
    char _HwId[1024] = {0};

    wsdd__ProbeMatchesType ProbeMatches;
    ProbeMatches.ProbeMatch = (struct wsdd__ProbeMatchType *)soap_malloc(soap, sizeof(struct wsdd__ProbeMatchType));
    ProbeMatches.ProbeMatch->XAddrs = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
    ProbeMatches.ProbeMatch->Types = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
    ProbeMatches.ProbeMatch->Scopes = (struct wsdd__ScopesType*)soap_malloc(soap,sizeof(struct wsdd__ScopesType));
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties = (struct wsa__ReferencePropertiesType*)soap_malloc(soap,sizeof(struct wsa__ReferencePropertiesType));
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters = (struct wsa__ReferenceParametersType*)soap_malloc(soap,sizeof(struct wsa__ReferenceParametersType));
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName = (struct wsa__ServiceNameType*)soap_malloc(soap,sizeof(struct wsa__ServiceNameType));
    ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType = (char **)soap_malloc(soap, sizeof(char *) * SMALL_INFO_LENGTH);
    ProbeMatches.ProbeMatch->wsa__EndpointReference.__any = (char **)soap_malloc(soap, sizeof(char*) * SMALL_INFO_LENGTH);
    ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
    ProbeMatches.ProbeMatch->wsa__EndpointReference.Address = (char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);

    //netGetMac("eth4", macaddr); //eth0  根据实际情况填充
    macaddr[0]=0x01;macaddr[1]=0x01;macaddr[2]=0x01;macaddr[3]=0x01;macaddr[4]=0x01;macaddr[5]=0x01;
    sprintf(_HwId,"urn:uuid:2419d68a-2dd2-21b2-a205-%02X%02X%02X%02X%02X%02X",macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);

    sprintf(_IPAddr, "http://%s/onvif/device_service", "192.168.1.33");
    printf("[%d] _IPAddr ==== %s\n", __LINE__, _IPAddr);

    ProbeMatches.__sizeProbeMatch = 1;
    ProbeMatches.ProbeMatch->Scopes->__item =(char *)soap_malloc(soap, 1024);
    //memset(ProbeMatches.ProbeMatch->Scopes->__item,0,sizeof(ProbeMatches.ProbeMatch->Scopes->__item));
    memset(ProbeMatches.ProbeMatch->Scopes->__item,0,1024);

    //Scopes MUST BE
    strcat(ProbeMatches.ProbeMatch->Scopes->__item, "onvif://www.onvif.org/type/NetworkVideoTransmitter");

    ProbeMatches.ProbeMatch->Scopes->MatchBy = NULL;
    strcpy(ProbeMatches.ProbeMatch->XAddrs, _IPAddr);
    strcpy(ProbeMatches.ProbeMatch->Types, wsdd__Probe->Types);
    printf("wsdd__Probe->Types=%s\n",wsdd__Probe->Types);
    ProbeMatches.ProbeMatch->MetadataVersion = 1;

    //ws-discovery规定 为可选项
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__size = 0;
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceProperties->__any = NULL;
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__size = 0;
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ReferenceParameters->__any = NULL;

    ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
    //ws-discovery规定 为可选项
    strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.PortType[0], "ttl");
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__item = NULL;
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->PortName = NULL;
    ProbeMatches.ProbeMatch->wsa__EndpointReference.ServiceName->__anyAttribute = NULL;
    ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0] = (char *)soap_malloc(soap, sizeof(char) * SMALL_INFO_LENGTH);
    strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__any[0], "Any");
    strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.__anyAttribute, "Attribute");
    ProbeMatches.ProbeMatch->wsa__EndpointReference.__size = 0;
    strcpy(ProbeMatches.ProbeMatch->wsa__EndpointReference.Address, _HwId);

    soap->header->wsa__To = "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous";
    soap->header->wsa__Action = "http://schemas.xmlsoap.org/ws/2005/04/discovery/ProbeMatches";
    soap->header->wsa__RelatesTo = (struct wsa__Relationship*)soap_malloc(soap, sizeof(struct wsa__Relationship));
    soap->header->wsa__RelatesTo->__item = soap->header->wsa__MessageID;
    printf("__item: %p, wsa__MessageID: %p: %s\n", soap->header->wsa__RelatesTo->__item, soap->header->wsa__MessageID, soap->header->wsa__MessageID);
    soap->header->wsa__RelatesTo->RelationshipType = NULL;
    soap->header->wsa__RelatesTo->__anyAttribute = NULL;

    soap->header->wsa__MessageID =(char *)soap_malloc(soap, sizeof(char) * INFO_LENGTH);
    strcpy(soap->header->wsa__MessageID,_HwId+4);

    if (SOAP_OK == soap_send___wsdd__ProbeMatches(soap, "http://", NULL, &ProbeMatches))
    {
        printf("send ProbeMatches success !\n");
        return SOAP_OK;
    }

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

    return soap->error;;

}



int main(int argc,char ** argv)
{
#define ONVIF_LISTEN_PORT 3702
    printf("[%s][%d][%s][%s] start \n", __FILE__, __LINE__, __TIME__, __func__);

    int count = 0;
    struct soap ServerSoap;
    struct ip_mreq mcast;

    soap_init1(&ServerSoap, SOAP_IO_UDP | SOAP_XML_IGNORENS);
    soap_set_namespaces(&ServerSoap,  namespaces);

    printf("[%s][%d][%s][%s] ServerSoap.version = %d \n", __FILE__, __LINE__, __TIME__, __func__, ServerSoap.version);

    if(!soap_valid_socket(soap_bind(&ServerSoap, NULL, ONVIF_LISTEN_PORT, 10)))
    {
        soap_print_fault(&ServerSoap, stderr);
        exit(1);
    }

    mcast.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
    mcast.imr_interface.s_addr = htonl(INADDR_ANY);

    if(setsockopt(ServerSoap.master, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&mcast, sizeof(mcast)) < 0)
    {
            printf("setsockopt error! error code = %d,err string = %s\n",errno,strerror(errno));
        return 0;
    }

    for(;;)
    {
        if(soap_serve(&ServerSoap))
        {
            soap_print_fault(&ServerSoap, stderr);
        }

        soap_destroy(&ServerSoap);
        soap_end(&ServerSoap);

        //客户端的IP地址
        printf("RECEIVE count %d, connection from IP = %lu.%lu.%lu.%lu socket = %d \r\n", count, ((ServerSoap.ip)>>24)&0xFF, ((ServerSoap.ip)>>16)&0xFF, ((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF, (ServerSoap.socket));
        count++;
    }

    //分离运行时的环境
    soap_done(&ServerSoap);

    return 0;
}

6)编译

PWD := $(shell pwd)
CPP=gcc
CFLAGS=-Wall -I.
TARGET1 = $(PWD)/server

all: $(TARGET1)
$(TARGET1):$(OBJ)
    $(CPP) $(CFLAGS) -o $@ main.c soapC.c soapServer.c stdsoap2.c soapClient.c
clean:
    rm $(TARGET1)

7)使用onvif测试工具测试

root@ubuntu:/home/work/test/onvif/discovery# ./server 
[main.c][161][08:54:02][main] start 
[main.c][170][08:54:02][main] ServerSoap.version = 2 
[75] __wsdd__Probe start !
[99] _IPAddr ==== http://192.168.1.33/onvif/device_service
wsdd__Probe->Types="http://www.onvif.org/ver10/device/wsdl":Device
__item: 0x8fea608, wsa__MessageID: 0x8fea608: uuid:370c90b7-384c-4763-bd9c-873471e37963
send ProbeMatches success !
RECEIVE count 0, connection from IP = 192.168.1.32 socket = 3 
[75] __wsdd__Probe start !
[99] _IPAddr ==== http://192.168.1.33/onvif/device_service
wsdd__Probe->Types="http://www.onvif.org/ver10/device/wsdl":Device
__item: 0x8fea608, wsa__MessageID: 0x8fea608: uuid:370c90b7-384c-4763-bd9c-873471e37963
send ProbeMatches success !
RECEIVE count 1, connection from IP = 192.168.1.32 socket = 3 
[75] __wsdd__Probe start !
[99] _IPAddr ==== http://192.168.1.33/onvif/device_service
wsdd__Probe->Types=tdn:NetworkVideoTransmitter
__item: 0x8fea608, wsa__MessageID: 0x8fea608: uuid:0b4dfada-7a12-42bb-908f-ec4c0285270a
send ProbeMatches success !
RECEIVE count 2, connection from IP = 192.168.1.32 socket = 3 
[75] __wsdd__Probe start !
[99] _IPAddr ==== http://192.168.1.33/onvif/device_service
wsdd__Probe->Types=tdn:NetworkVideoTransmitter
__item: 0x8fea608, wsa__MessageID: 0x8fea608: uuid:0b4dfada-7a12-42bb-908f-ec4c0285270a
send ProbeMatches success !
RECEIVE count 3, connection from IP = 192.168.1.32 socket = 3 

这里写图片描述

8)相关资料下载地址

gsoap: http://download.csdn.net/detail/codeheng/9569006
onvif测试工具:http://download.csdn.net/detail/codeheng/9569000

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值