2012年7月25日 星期三

ONVIF -- 透過 gSOAP 產生程式碼



gSOAP 可以幫忙將 wsdl 所定義的 schema 轉換成對應的程式碼,以下紀錄如何轉換的過程。


操作步驟

一、下載 gSOAP
連結 http://gsoap2.sourceforge.net/,下載 gSOAP,此篇文章撰寫時的最新版本為 2.8.9 。
 
二、利用 wsdl2h.exe ,產生 header files,此處以 media.wsdl 為例
wsdl2h.exe -c -o media.h http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl http://www.w3.org/2006/03/addressing/ws-addr.xsd 

三、使用 header file 產生程式碼,若 header file 較大,則需較長的產生時間。
soapcpp2 -C –I../../import  media.h
註:"-C"表示產生Client端的程式碼,改成"-S"便會產生Server端程式碼。

四、成功後,會產生 149 個檔案,如下
media.h
MediaBinding.AddAudioDecoderConfiguration.req.xml
MediaBinding.AddAudioDecoderConfiguration.res.xml
MediaBinding.AddAudioEncoderConfiguration.req.xml
MediaBinding.AddAudioEncoderConfiguration.res.xml
MediaBinding.AddAudioOutputConfiguration.req.xml
MediaBinding.AddAudioOutputConfiguration.res.xml
MediaBinding.AddAudioSourceConfiguration.req.xml
MediaBinding.AddAudioSourceConfiguration.res.xml
MediaBinding.AddMetadataConfiguration.req.xml
MediaBinding.AddMetadataConfiguration.res.xml
MediaBinding.AddPTZConfiguration.req.xml
MediaBinding.AddPTZConfiguration.res.xml
MediaBinding.AddVideoAnalyticsConfiguration.req.xml
MediaBinding.AddVideoAnalyticsConfiguration.res.xml
MediaBinding.AddVideoEncoderConfiguration.req.xml
MediaBinding.AddVideoEncoderConfiguration.res.xml
MediaBinding.AddVideoSourceConfiguration.req.xml
MediaBinding.AddVideoSourceConfiguration.res.xml
MediaBinding.CreateProfile.req.xml
MediaBinding.CreateProfile.res.xml
MediaBinding.DeleteProfile.req.xml
MediaBinding.DeleteProfile.res.xml
MediaBinding.GetAudioDecoderConfiguration.req.xml
MediaBinding.GetAudioDecoderConfiguration.res.xml
MediaBinding.GetAudioDecoderConfigurationOptions.req.xml
MediaBinding.GetAudioDecoderConfigurationOptions.res.xml
MediaBinding.GetAudioDecoderConfigurations.req.xml
MediaBinding.GetAudioDecoderConfigurations.res.xml
MediaBinding.GetAudioEncoderConfiguration.req.xml
MediaBinding.GetAudioEncoderConfiguration.res.xml
MediaBinding.GetAudioEncoderConfigurationOptions.req.xml
MediaBinding.GetAudioEncoderConfigurationOptions.res.xml
MediaBinding.GetAudioEncoderConfigurations.req.xml
MediaBinding.GetAudioEncoderConfigurations.res.xml
MediaBinding.GetAudioOutputConfiguration.req.xml
MediaBinding.GetAudioOutputConfiguration.res.xml
MediaBinding.GetAudioOutputConfigurationOptions.req.xml
MediaBinding.GetAudioOutputConfigurationOptions.res.xml
MediaBinding.GetAudioOutputConfigurations.req.xml
MediaBinding.GetAudioOutputConfigurations.res.xml
MediaBinding.GetAudioOutputs.req.xml
MediaBinding.GetAudioOutputs.res.xml
MediaBinding.GetAudioSourceConfiguration.req.xml
MediaBinding.GetAudioSourceConfiguration.res.xml
MediaBinding.GetAudioSourceConfigurationOptions.req.xml
MediaBinding.GetAudioSourceConfigurationOptions.res.xml
MediaBinding.GetAudioSourceConfigurations.req.xml
MediaBinding.GetAudioSourceConfigurations.res.xml
MediaBinding.GetAudioSources.req.xml
MediaBinding.GetAudioSources.res.xml
MediaBinding.GetCompatibleAudioDecoderConfigurations.req.xml
MediaBinding.GetCompatibleAudioDecoderConfigurations.res.xml
MediaBinding.GetCompatibleAudioEncoderConfigurations.req.xml
MediaBinding.GetCompatibleAudioEncoderConfigurations.res.xml
MediaBinding.GetCompatibleAudioOutputConfigurations.req.xml
MediaBinding.GetCompatibleAudioOutputConfigurations.res.xml
MediaBinding.GetCompatibleAudioSourceConfigurations.req.xml
MediaBinding.GetCompatibleAudioSourceConfigurations.res.xml
MediaBinding.GetCompatibleMetadataConfigurations.req.xml
MediaBinding.GetCompatibleMetadataConfigurations.res.xml
MediaBinding.GetCompatibleVideoAnalyticsConfigurations.req.xml
MediaBinding.GetCompatibleVideoAnalyticsConfigurations.res.xml
MediaBinding.GetCompatibleVideoEncoderConfigurations.req.xml
MediaBinding.GetCompatibleVideoEncoderConfigurations.res.xml
MediaBinding.GetCompatibleVideoSourceConfigurations.req.xml
MediaBinding.GetCompatibleVideoSourceConfigurations.res.xml
MediaBinding.GetGuaranteedNumberOfVideoEncoderInstances.req.xml
MediaBinding.GetGuaranteedNumberOfVideoEncoderInstances.res.xml
MediaBinding.GetMetadataConfiguration.req.xml
MediaBinding.GetMetadataConfiguration.res.xml
MediaBinding.GetMetadataConfigurationOptions.req.xml
MediaBinding.GetMetadataConfigurationOptions.res.xml
MediaBinding.GetMetadataConfigurations.req.xml
MediaBinding.GetMetadataConfigurations.res.xml
MediaBinding.GetProfile.req.xml
MediaBinding.GetProfile.res.xml
MediaBinding.GetProfiles.req.xml
MediaBinding.GetProfiles.res.xml
MediaBinding.GetServiceCapabilities.req.xml
MediaBinding.GetServiceCapabilities.res.xml
MediaBinding.GetSnapshotUri.req.xml
MediaBinding.GetSnapshotUri.res.xml
MediaBinding.GetStreamUri.req.xml
MediaBinding.GetStreamUri.res.xml
MediaBinding.GetVideoAnalyticsConfiguration.req.xml
MediaBinding.GetVideoAnalyticsConfiguration.res.xml
MediaBinding.GetVideoAnalyticsConfigurations.req.xml
MediaBinding.GetVideoAnalyticsConfigurations.res.xml
MediaBinding.GetVideoEncoderConfiguration.req.xml
MediaBinding.GetVideoEncoderConfiguration.res.xml
MediaBinding.GetVideoEncoderConfigurationOptions.req.xml
MediaBinding.GetVideoEncoderConfigurationOptions.res.xml
MediaBinding.GetVideoEncoderConfigurations.req.xml
MediaBinding.GetVideoEncoderConfigurations.res.xml
MediaBinding.GetVideoSourceConfiguration.req.xml
MediaBinding.GetVideoSourceConfiguration.res.xml
MediaBinding.GetVideoSourceConfigurationOptions.req.xml
MediaBinding.GetVideoSourceConfigurationOptions.res.xml
MediaBinding.GetVideoSourceConfigurations.req.xml
MediaBinding.GetVideoSourceConfigurations.res.xml
MediaBinding.GetVideoSources.req.xml
MediaBinding.GetVideoSources.res.xml
MediaBinding.nsmap
MediaBinding.RemoveAudioDecoderConfiguration.req.xml
MediaBinding.RemoveAudioDecoderConfiguration.res.xml
MediaBinding.RemoveAudioEncoderConfiguration.req.xml
MediaBinding.RemoveAudioEncoderConfiguration.res.xml
MediaBinding.RemoveAudioOutputConfiguration.req.xml
MediaBinding.RemoveAudioOutputConfiguration.res.xml
MediaBinding.RemoveAudioSourceConfiguration.req.xml
MediaBinding.RemoveAudioSourceConfiguration.res.xml
MediaBinding.RemoveMetadataConfiguration.req.xml
MediaBinding.RemoveMetadataConfiguration.res.xml
MediaBinding.RemovePTZConfiguration.req.xml
MediaBinding.RemovePTZConfiguration.res.xml
MediaBinding.RemoveVideoAnalyticsConfiguration.req.xml
MediaBinding.RemoveVideoAnalyticsConfiguration.res.xml
MediaBinding.RemoveVideoEncoderConfiguration.req.xml
MediaBinding.RemoveVideoEncoderConfiguration.res.xml
MediaBinding.RemoveVideoSourceConfiguration.req.xml
MediaBinding.RemoveVideoSourceConfiguration.res.xml
MediaBinding.SetAudioDecoderConfiguration.req.xml
MediaBinding.SetAudioDecoderConfiguration.res.xml
MediaBinding.SetAudioEncoderConfiguration.req.xml
MediaBinding.SetAudioEncoderConfiguration.res.xml
MediaBinding.SetAudioOutputConfiguration.req.xml
MediaBinding.SetAudioOutputConfiguration.res.xml
MediaBinding.SetAudioSourceConfiguration.req.xml
MediaBinding.SetAudioSourceConfiguration.res.xml
MediaBinding.SetMetadataConfiguration.req.xml
MediaBinding.SetMetadataConfiguration.res.xml
MediaBinding.SetSynchronizationPoint.req.xml
MediaBinding.SetSynchronizationPoint.res.xml
MediaBinding.SetVideoAnalyticsConfiguration.req.xml
MediaBinding.SetVideoAnalyticsConfiguration.res.xml
MediaBinding.SetVideoEncoderConfiguration.req.xml
MediaBinding.SetVideoEncoderConfiguration.res.xml
MediaBinding.SetVideoSourceConfiguration.req.xml
MediaBinding.SetVideoSourceConfiguration.res.xml
MediaBinding.StartMulticastStreaming.req.xml
MediaBinding.StartMulticastStreaming.res.xml
MediaBinding.StopMulticastStreaming.req.xml
MediaBinding.StopMulticastStreaming.res.xml
soapC.c
soapClient.c
soapClientLib.c
soapH.h
soapStub.h

五、參考 soapStub.h,實作對應的函數,例如:soap_serve___ns2__GetStreamUri(struct soap*);,此處,我將所有相關的函數統一放在soapStub.c內。


六、若要產生完整的 onvif API,則可以加入所有 onvif 所定義的 wsdl, 參考下列指令

wsdl2h.exe -c -o onvif.h
http://www.onvif.org/onvif/ver10/schema/onvif.xsd
http://www.w3.org/2006/03/addressing/ws-addr.xsd
http://www.onvif.org/onvif/ver10/device/wsdl/devicemgmt.wsdl
http://www.onvif.org/onvif/ver10/media/wsdl/media.wsdl
http://www.onvif.org/onvif/ver10/deviceio.wsdl
http://docs.oasis-open.org/wsrf/rw-2.wsdl
http://docs.oasis-open.org/wsn/bw-2.wsdl
http://www.onvif.org/onvif/ver10/event/wsdl/event.wsdl
http://www.onvif.org/onvif/ver20/ptz/wsdl/ptz.wsdl
http://www.onvif.org/onvif/ver20/imaging/wsdl/imaging.wsdl


問題釋疑

1. Critical error: #import: Cannot open file "ns1.h" for reading.
    wsdl2h.exe 使用時加入 http://www.w3.org/2006/03/addressing/ws-addr.xsd 即可。


2. 編譯時發現少了 stdsoap2.h
    此檔案可在 \gsoap_2.8.9\gsoap-2.8\gsoap\ 取得。
    複製時應該同時複製 stdsoap2.h 與 stdsoap2.c


3. Makefile 舉例,因為我在 Windows 使用 mingw 來編譯,其Makefile 如下
OBJS = stdsoap2.o soapC.o soapClient.o soapClientLib.o soapStub.o
all: onvif_media
%.o: %.c soapH.h soapStub.h import/*.h
gcc -c $< -o $@
onvif_media: $(OBJS)
gcc $(OBJS) -o $@
clean:
rm -f *.o onvif_media.exe

參考資料:

  1. wsdl2h 與 soapcpp2 用法
  2. gSOAP 中文使用介紹
  3. http://www.cppblog.com/qiujian5628/archive/2008/10/11/54019.html