Linux下通过GSOAP C++实现ONVIF(1):使用GSOAP生成C++版本的ONVIF框架

1.工具下载

开源版下载地址:https://sourceforge.net/projects/gsoap2/files/
此处我是用的为2.8.87版本。

2.依赖库安装

  • sudo apt-get install bison
  • sudo apt-get install flex
  • sudo apt-get install openssl

3.GSOAP安装

  • 拷贝文件:将下载的程序“gsoap_2.8.87.zip”拷贝至linux环境下;
  • 解压文件:进入文件所在目录,使用命令“unzip gsoap_2.8.87.zip”解压文件;
  • 编译gsoap:

$ cd gsoap-2.8/
$ sudo ./configure --with-openssl=/usr/local/ssl
$ sudo make
$ sudo make install

  • 校验安装是否完成

$ which wsdl2h soapcpp2
/usr/local/bin/wsdl2h
/usr/local/bin/soapcpp2

4.生成ONVIF框架

  • 修改gsoap\typemap.dat文件

    由于后续编译源代码需要用到 duration.c 文件,会遇到类型LONG64报错的问题,需要typemap.dat 文件中取消以下行的注释:

xsd__duration = #import “custom/duration.h” | xsd__duration

  • 生成onvif.h文件

#进入gsopa目录
cd gsoap-2.8/gsoap/
#创建生成onvif文件目录
mkdir -p samples/onvif
#生成onvif.h
wsdl2h -P -x -t ./typemap.dat -o samples/onvif/onvif.h https://www.onvif.org/ver10/network/wsdl/remotediscovery.wsdl https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl https://www.onvif.org/ver10/media/wsdl/media.wsdl

如上生成的onvif.h包括了remotediscovery、devicemgmt、media模块,可以根据需求增删模块;
wsdl2h的选项含义,可以使用wsdl2h -h查阅。
命令成功执行后会得到onvif.h头文件,终端打印如下:

$ wsdl2h -P -x -t ./typemap.dat -o samples/onvif/onvif.h https://www.onvif.org/ver10/network/wsdl/remotediscovery.wsdl https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl https://www.onvif.org/ver10/media/wsdl/media.wsdl
Saving samples/onvif/onvif.h

** The gSOAP WSDL/WADL/XSD processor for C and C++, wsdl2h release 2.8.87
** Copyright © 2000-2019 Robert van Engelen, Genivia Inc.
** All Rights Reserved. This product is provided “as is”, without any warranty.
** The wsdl2h tool and its generated software are released under the GPL.
** ----------------------------------------------------------------------------
** A commercial use license is available from Genivia Inc., [email protected]
** ----------------------------------------------------------------------------

Reading type definitions from type map “./typemap.dat”
Connecting to ‘https://www.onvif.org/ver10/network/wsdl/remotediscovery.wsdl’ to retrieve WSDL/WADL or XSD… connected, receiving…
Done reading ‘https://www.onvif.org/ver10/network/wsdl/remotediscovery.wsdl’
Connecting to ‘https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl’ to retrieve WSDL/WADL or XSD… connected, receiving…
Connecting to ‘https://www.onvif.org/ver10/schema/onvif.xsd’ to retrieve schema… connected, receiving…
Connecting to ‘http://docs.oasis-open.org/wsn/b-2.xsd’ to retrieve schema… connected, receiving…
Connecting to ‘http://docs.oasis-open.org/wsrf/bf-2.xsd’ to retrieve schema… connected, receiving…
Done reading ‘http://docs.oasis-open.org/wsrf/bf-2.xsd’
Connecting to ‘http://docs.oasis-open.org/wsn/t-1.xsd’ to retrieve schema… connected, receiving…
Done reading ‘http://docs.oasis-open.org/wsn/t-1.xsd’
Done reading ‘http://docs.oasis-open.org/wsn/b-2.xsd’
Connecting to ‘https://www.onvif.org/ver10/schema/common.xsd’ to retrieve schema… connected, receiving…
Done reading ‘https://www.onvif.org/ver10/schema/common.xsd’
Done reading ‘https://www.onvif.org/ver10/schema/onvif.xsd’
Done reading ‘https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl’
Connecting to ‘https://www.onvif.org/ver10/media/wsdl/media.wsdl’ to retrieve WSDL/WADL or XSD… connected, receiving…
Done reading ‘https://www.onvif.org/ver10/media/wsdl/media.wsdl’

Warning: 2 service bindings found, but collected as one service (use option -Nname to produce a separate service for each binding)

To finalize code generation, execute:
soapcpp2 samples/onvif/onvif.h
Or to generate C++ proxy and service classes:
soapcpp2 -j samples/onvif/onvif.h

  • 修改onvif.h
    因为鉴权的需要,在onvif.h头文件中加入#import “wsse.h”

vi samples/onvif/onvif.h

  • 修改wsa5.h,搜索SOAP_ENV__Fault,将结构体名字修改为SOAP_ENV__Fault_alex

vi import/wsa5.h

  • 生成框架代码

soapcpp2 -S -L -x -I ./import ./custom -d samples/onvif/ samples/onvif/onvif.h

各个选项的含义,通过soapcpp2.exe -h查看帮助。

  • 拷贝需要使用的源码

cp stdsoap2.c stdsoap2.h plugin/wsaapi.c plugin/wsaapi.h custom/duration.c custom/duration.h samples/onvif/

  • 修改文件后缀

cd samples/onvif
mv duration.c duration.cpp
mv stdsoap2.c stdsoap2.cpp
mv wsaapi.c wsaapi.cpp

  • 关联自己的命名空间
    修改stdsoap2.cpp文件,在开头加入:

#include “wsdd.nsmap”

  • 拷贝完全的框架文件
    删除onvif.h文件,将除wsdd.nsmap之外的其他.nsmap文件删除,剩余文件将是编写onvif需要的文件,列表如下:

$ ls -l
total 21604
-rw-r–r-- 1 lcc lcc 6841 Aug 15 11:42 duration.cpp
-rw-r–r-- 1 lcc lcc 3961 Aug 15 11:42 duration.h
-rw-rw-r-- 1 lcc lcc 10952493 Aug 15 11:42 soapC.cpp
-rw-rw-r-- 1 lcc lcc 6706773 Aug 15 11:42 soapH.h
-rw-rw-r-- 1 lcc lcc 328871 Aug 15 11:42 soapServer.cpp
-rw-rw-r-- 1 lcc lcc 3238528 Aug 15 11:42 soapStub.h
-rw-r–r-- 1 lcc lcc 635117 Aug 15 11:42 stdsoap2.cpp
-rw-r–r-- 1 lcc lcc 155707 Aug 15 11:42 stdsoap2.h
-rw-r–r-- 1 lcc lcc 62372 Aug 15 11:42 wsaapi.cpp
-rw-r–r-- 1 lcc lcc 7402 Aug 15 11:42 wsaapi.h
-rw-rw-r-- 1 lcc lcc 2423 Aug 15 11:42 wsdd.nsmap

5.文章参考
参考文章:https://blog.csdn.net/benkaoya/article/details/72466827

你可能感兴趣的:(ONVIF)