Onvif之设备发现-基于gsoap2.8.27

Onvif之设备发现-基于gsoap2.8.27

工具及平台:
Gsoap版本:gsoap2.8.27
Onvif :WSDL文档,(2015.12.15最新文档)
框架代码生成:windows下wsdl2、soapcpp2(win下和linux生成一样的)
编译链为:Linux gcc4.4.3

上篇文章介绍了onvif框架的生成,下面来介绍下基于前面框架设备发现的实现,搭建好框架后设备的发现就特别简单啦!

gsoap中有很多实用的插件,此次设备发现就是利用wsdd(在目录gsoap_2.8.27\gsoap-2.8\gsoap\plugin\ wsddapi.c中)插件实现。

1.在main.c中实现

int main(int argc, char **argv) 
{
    struct ip_mreq mcast;
    struct soap soap;

    soap_init2(&soap, SOAP_IO_UDP | SOAP_IO_FLUSH, \
             SOAP_IO_UDP|SOAP_IO_FLUSH);  
    soap_set_namespaces(&soap, namespaces);

    soap_set_mode(&soap, SOAP_C_UTFSTRING);

    soap.bind_flags        = SO_REUSEADDR;
    soap.connect_timeout   = 10;
    soap.recv_timeout      = 5;
    soap.send_timeout      = 5;

    soap_register_plugin(&soap, soap_wsa);

#ifdef DEBUG 
    //定义调试输出文件
    soap_set_recv_logfile(&soap, "./log/recv.xml");
    soap_set_sent_logfile(&soap, "./log/send.xml");
    soap_set_test_logfile(&soap, "./log/test.log");
#endif 

    if(!soap_valid_socket(soap_bind(&soap, NULL, 3702, 10)))
    {
        soap_print_fault(&soap, stderr);
        exit(1);
    }
    mcast.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
    mcast.imr_interface.s_addr = inet_addr("0.0.0.0");
    if(setsockopt(soap.master, IPPROTO_IP, \
            IP_ADD_MEMBERSHIP, \
            (char*)&mcast, sizeof(mcast)) < 0)
    {
        exit(1);
    }  
    //循环监听
    soap_wsdd_listen(&soap, 0);
    soap_done(&soap);
}

2.再在waddapi.c中实现函数

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)
{
    soap_wsdd_init_ProbeMatches(soap,matches);
    soap_wsdd_add_ProbeMatch(soap,matches,
        "urn:uuid:E24A4344-1236-4567-2349-234156781888",
        "tdn:NetworkVideoTransmitter",
        "onvif://www.onvif.org/type/video onvif://www.onvif.org/type/audio onvif://www.onvif.org/hardware/Hisi onvif://www.onvif.org/name/IPNC",
        NULL,
        "http://192.168.2.200:80/onvif/device_service", 1);

    return SOAP_WSDD_MANAGED;
}

注意tdn:NetworkVideoTransmitter 这个要与wsdd.nsmap命名空间中的一致,此处为”tdn”
这里写图片描述

最后编译生成可执行文件即可。

你可能感兴趣的:(框架,插件,gsoap,onvif)