C语言 gSOAP ONVIF 服务端开发教程 - 详细步骤指南
gSOAP 是一款用于开发 SOAP(Simple Object Access Protocol)和 RESTful(Representational State Transfer)服务的 C 语言工具包。它提供了一组 API 和工具,让开发者可以轻松创建和部署 Web 服务。
本文将介绍如何使用 gSOAP 开发一个 ONVIF 服务端。ONVIF(开放式网络视频接口论坛)旨在为网络视频设备(例如 IP 摄像头)提供统一的接口标准。我们将使用 gSOAP 实现一个符合 ONVIF 标准的服务端,提供基本的设备管理和媒体服务。
以下是开发步骤:
步骤 1:安装 gSOAP
首先,你需要下载并安装 gSOAP。你可以从官方网站 (https://www.genivia.com/products/gsoap/) 下载最新版本的 gSOAP。安装过程很简单,只需按照安装向导中的说明进行即可。
步骤 2:定义 ONVIF 服务
在本步骤中,我们将使用 gSOAP 定义 ONVIF 服务。在 gSOAP 中,服务定义使用 WSDL(Web 服务描述语言)文件进行描述。WSDL 文件定义了服务的接口、输入参数、输出参数等信息。
以下是一个简单的 WSDL 文件,用于定义 ONVIF 服务:
<?xml version='1.0' encoding='UTF-8'?>
<definitions name='ONVIFService'
targetNamespace='http://www.onvif.org/ver10/device/wsdl'
xmlns:tns='http://www.onvif.org/ver10/device/wsdl'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>
<types>
<xsd:schema targetNamespace='http://www.onvif.org/ver10/device/wsdl'>
<xsd:import namespace='http://www.w3.org/2001/XMLSchema' schemaLocation='http://www.w3.org/2001/XMLSchema.xsd'/>
<xsd:complexType name='Device'>
<xsd:sequence>
<xsd:element name='Name' type='xsd:string'/>
<xsd:element name='Model' type='xsd:string'/>
<xsd:element name='SerialNumber' type='xsd:string'/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
<message name='GetDeviceRequest'>
<part name='Dummy' type='xsd:int'/>
</message>
<message name='GetDeviceResponse'>
<part name='Device' type='tns:Device'/>
</message>
<portType name='DeviceService'>
<operation name='GetDevice'>
<input message='tns:GetDeviceRequest'/>
<output message='tns:GetDeviceResponse'/>
</operation>
</portType>
<binding name='DeviceServiceBinding' type='tns:DeviceService'>
<soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='GetDevice'>
<soap:operation soapAction='http://www.onvif.org/ver10/device/wsdl/GetDevice'/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
</operation>
</binding>
<service name='DeviceService'>
<port name='DeviceServicePort' binding='tns:DeviceServiceBinding'>
<soap:address location='http://localhost:8080/'/>
</port>
</service>
</definitions>
在这个 WSDL 文件中,我们定义了一个名为 'DeviceService' 的服务,它包含一个名为 'GetDevice' 的操作。这个操作接收一个名为 'Dummy' 的输入参数,并返回一个名为 'Device' 的输出参数,该参数类型为 'Device' 结构体。
步骤 3:生成代码
一旦你定义了 WSDL 文件,就可以使用 gSOAP 的 wsdl2h 工具将其转换为 C 头文件。在命令行中,使用以下命令生成头文件:
wsdl2h -o onvif.h onvif.wsdl
该命令将生成一个名为 'onvif.h' 的头文件,其中包含 WSDL 文件中定义的服务接口和数据类型。
接下来,我们使用 gSOAP 的 soapcpp2 工具生成 C 源代码。在命令行中,使用以下命令生成源代码:
soapcpp2 -c -C -d . onvif.h
该命令将生成一系列源代码文件,包括 'soapClient.c'、'soapH.h'、'soapStub.h' 等。
步骤 4:实现服务
现在,我们可以使用生成的源代码实现 ONVIF 服务端。在本例中,我们将使用 gSOAP 提供的 'soap_serve' 函数和 'soap_accept' 函数来处理 SOAP 请求。
以下是一个简单的 ONVIF 服务端实现:
#include "soapH.h"
#include "DeviceBinding.nsmap"
int main()
{
struct soap soap;
soap_init(&soap);
soap_set_namespaces(&soap, namespaces);
while (true)
{
soap_accept(&soap);
if (soap_serve(&soap) != SOAP_OK)
soap_print_fault(&soap, stderr);
soap_destroy(&soap);
soap_end(&soap);
}
soap_done(&soap);
return 0;
}
int DeviceBinding__GetDevice(struct soap* soap, struct _tds__GetDevice* tds__GetDevice, struct _tds__GetDeviceResponse* tds__GetDeviceResponse)
{
tds__GetDeviceResponse->Device = (struct tt__Device*)soap_malloc(soap, sizeof(struct tt__Device));
tds__GetDeviceResponse->Device->Name = "My Camera";
tds__GetDeviceResponse->Device->Model = "ABC123";
tds__GetDeviceResponse->Device->SerialNumber = "123456789";
return SOAP_OK;
}
在上述代码中,我们使用了 gSOAP 的 'soap_init' 函数来初始化 SOAP 环境。然后,我们使用 'soap_set_namespaces' 函数指定命名空间。接下来,我们在一个无限循环中调用 'soap_accept' 和 'soap_serve' 函数来处理 SOAP 请求。在 'DeviceBinding__GetDevice' 函数中,我们实现了 'GetDevice' 操作的逻辑,该操作返回一个名为 'My Camera' 的设备,并且模型号为 'ABC123',序列号为 '123456789'。
步骤 5:编译和运行
最后,我们可以使用 gcc 编译生成的源代码,并运行 ONVIF 服务端。在命令行中,使用以下命令编译源代码:
gcc -o onvif onvif.c soapC.c soapClient.c soapServer.c stdsoap2.c -lgsoap
该命令将生成一个名为 'onvif' 的可执行文件。现在,我们可以运行服务端,使用一个 SOAP 客户端来测试它。例如,我们可以使用 SOAPUI (https://www.soapui.org/) 来测试 ONVIF 服务端。
结论
本文介绍了如何使用 gSOAP 开发一个符合 ONVIF 标准的服务端。gSOAP 提供了一组 API 和工具,让开发者可以轻松地创建和部署 SOAP 和 RESTful 服务。虽然 gSOAP 已经很久没有更新了,但它仍然是一个非常有用的工具,可以帮助开发者快速开发 Web 服务。
原文地址: https://www.cveoy.top/t/topic/n4AV 著作权归作者所有。请勿转载和采集!