ONVIF Event消息解析(How to work with gSoap)

Prepare Requirements

ONVIF Event

gSoap reference

ONVIF Specification

问题描述

Event是ONVIF核心规范中一块, 文档解释了如何基于WS-Notification框架体系来工作.但是依据Event.wsdl 生成的消息结构部分, wsdl没有给出参考标准.而是给出了一个可扩展定义的dom结点点位描述. 见下引用

        <xs:element name="PullMessagesResponse">
            <xs:complexType>
               <xs:sequence>
                  <xs:element name="CurrentTime" type="xs:dateTime">
                     <xs:annotation>
                        <xs:documentation>The date and time when the messages have been delivered by the web server to the client.</xs:documentation>
                     </xs:annotation>
                  </xs:element>
                  <xs:element name="TerminationTime" type="xs:dateTime">
                     <xs:annotation>
                        <xs:documentation>Date time when the PullPoint will be shut down without further pull requests.</xs:documentation>
                     </xs:annotation>
                  </xs:element>
                  <xs:element ref="wsnt:NotificationMessage" minOccurs="0" maxOccurs="unbounded">
                     <xs:annotation>
                        <xs:documentation>List of messages. This list shall be empty in case of a timeout.</xs:documentation>
                     </xs:annotation>
                  </xs:element>
               </xs:sequence>
            </xs:complexType>
         </xs:element>
 


Okay, let's see the reference ="wsnt:NotificationMessage"

   <!--================== Message Helper Types  =====================-->
   <xsd:complexType name="NotificationMessageHolderType">
      <xsd:sequence>
         <xsd:element ref="wsnt:SubscriptionReference" minOccurs="0" maxOccurs="1"/>
         <xsd:element ref="wsnt:Topic" minOccurs="0" maxOccurs="1"/>
         <xsd:element ref="wsnt:ProducerReference" minOccurs="0" maxOccurs="1"/>
         <xsd:element name="Message">
            <xsd:complexType>
               <xsd:sequence>
                  <xsd:any namespace="##any" processContents="lax" minOccurs="1" maxOccurs="1"/>
               </xsd:sequence>
            </xsd:complexType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="NotificationMessage" type="wsnt:NotificationMessageHolderType"/>
 


you see. the element Message. no defined fixture struct.

Let's do somthing to watch the right truth. this way we will see how to work with gSoap to make the soap protocol comfortable.得意

执行wsdl2h生成处理头文件.

/// "http://docs.oasis-open.org/wsn/b-2":NotificationMessageHolderType is a complexType.
struct wsnt__NotificationMessageHolderType
{
/// Element reference "http://docs.oasis-open.org/wsn/b-2":SubscriptionReference.
    wsa5__EndpointReferenceType*         SubscriptionReference          0;	///< Optional element.
/// Element reference "http://docs.oasis-open.org/wsn/b-2":Topic.
    struct wsnt__TopicExpressionType*    Topic                          0;	///< Optional element.
/// Element reference "http://docs.oasis-open.org/wsn/b-2":ProducerReference.
    wsa5__EndpointReferenceType*         ProducerReference              0;	///< Optional element.
    struct _wsnt__NotificationMessageHolderType_Message
    {
/// TODO: <any namespace="##any" minOccurs="1" maxOccurs="1">
/// TODO: Schema extensibility is user-definable.
///       Consult the protocol documentation to change or insert declarations.
///       Use wsdl2h option -x to remove this element.
///       Use wsdl2h option -d for xsd__anyType DOM (soap_dom_element).
    xsd__anyType                         __any                         0;	///< Catch any element content in DOM.
    }                                    Message                        1;	///< Required element.
};


now, see it. this generation with no option specitied. 没有指定任何选项默认生成为C++的.h处理文件.

you should have to see the line

_XML                                 __any                         0; ///< Catch any element content in XML string.

每个ONVIF新手上来都会被这个字段搞迷糊. Message字段, 我们必需关注的内容.居然未被解析成可用类型的数据, 那我们依赖gSoap是为什么. 我们用它来生成.h, 进一步生成我们能调用的C/C++代码. 生成的代码量还是庞大可观的. 编译老费时间, 还需要stdsoap2.h/.c的支持, 还可能会用到gSoap/plugin中相关支持.

yes, it must be useful. 是的我们会发现gSoap带来给我很大解脱. 只要我们保持一颗乐观向上的心情, 保持上进的心态.

下面由请你把这个结构中的__any字段改成指定类型,然后我们再进入下一环节的讲述.

  

/// Top-level root element "http://www.onvif.org/ver10/schema":Message


/// "http://www.onvif.org/ver10/schema":Message is a complexType.
struct _tt__Message
{
/// @brief Token value pairs that triggered this message. Typically only one item is present.
/// Element Source of type "http://www.onvif.org/ver10/schema":ItemList.
    struct tt__ItemList*                 Source                         0;	///< Optional element.
/// Element Key of type "http://www.onvif.org/ver10/schema":ItemList.
    struct tt__ItemList*                 Key                            0;	///< Optional element.
/// Element Data of type "http://www.onvif.org/ver10/schema":ItemList.
    struct tt__ItemList*                 Data                           0;	///< Optional element.
/// Element Extension of type "http://www.onvif.org/ver10/schema":MessageExtension.
    struct tt__MessageExtension*         Extension                      0;	///< Optional element.
/// Attribute UtcTime of type xs:dateTime.
   @time_t                               UtcTime                        1;	///< Required attribute.
/// Attribute PropertyOperation of type "http://www.onvif.org/ver10/schema":PropertyOperation.
   @enum tt__PropertyOperation*          PropertyOperation              0;	///< Optional attribute.
/// <anyAttribute namespace="##any">
/// TODO: Schema extensibility is user-definable.
///       Consult the protocol documentation to change or insert declarations.
///       Use wsdl2h option -x to remove this attribute.
///       Use wsdl2h option -d for xsd__anyAttribute DOM (soap_dom_attribute).
   @xsd__anyAttribute                    __anyAttribute                ;	///< Store anyAttribute content in DOM soap_dom_attribute linked node structure.
};

由于我们参考阅读ONVIF事件一块的文档资料以及通过odm及wireshark工具抓包分析可了解到, 国内使用的许多IPC都基本上使用已有的事件消息结构来填充上面提到的_wsnt__NotificationMessageHolderType_Message内容, 即被解释为_tt__Message的参考结构.

好吧, 那我们就来把 那行代码改掉
_XML                                 __any;

改成
_tt__Message                  tt__Message;

为什么要这样修改,后面我们会了解,这个字段命名也是有规则的. 在生成soapC.cpp的时候,会依赖字段来生成相应的解析标签, tt__Message被soapcpp2理解为一个tt:Message的标签元素,所以他解析与序列化组装消息都会依赖这个规则.

执行soapcpp2生成C/C++使用代码.

#ifndef SOAP_TYPE__wsnt__NotificationMessageHolderType_Message
#define SOAP_TYPE__wsnt__NotificationMessageHolderType_Message (1303)
/* wsnt:NotificationMessageHolderType-Message */
struct _wsnt__NotificationMessageHolderType_Message
{
	struct _tt__Message *tt__Message;	/* optional element of type tt:Message */
};
#endif

#ifndef SOAP_TYPE_wsnt__NotificationMessageHolderType
#define SOAP_TYPE_wsnt__NotificationMessageHolderType (1177)
/* wsnt:NotificationMessageHolderType */
struct wsnt__NotificationMessageHolderType
{
	struct wsa5__EndpointReferenceType *SubscriptionReference;	/* optional element of type wsa5:EndpointReferenceType */
	struct wsnt__TopicExpressionType *Topic;	/* optional element of type wsnt:TopicExpressionType */
	struct wsa5__EndpointReferenceType *ProducerReference;	/* optional element of type wsa5:EndpointReferenceType */
	struct _wsnt__NotificationMessageHolderType_Message Message;	/* required element of type wsnt:NotificationMessageHolderType-Message */
};
#endif

gSoap对消息解析一块生成的代码,let's look.

SOAP_FMAC3 int SOAP_FMAC4 soap_out__wsnt__NotificationMessageHolderType_Message(struct soap *soap, const char *tag, int id, const struct _wsnt__NotificationMessageHolderType_Message *a, const char *type)
{
	(void)soap; (void)tag; (void)id; (void)type;
	if (soap_element_begin_out(soap, tag, soap_embedded_id(soap, id, a, SOAP_TYPE__wsnt__NotificationMessageHolderType_Message), type))
		return soap->error;
	if (soap_out_PointerTo_tt__Message(soap, "tt:Message", -1, &a->tt__Message, ""))
		return soap->error;
	return soap_element_end_out(soap, tag);
}

SOAP_FMAC3 struct _wsnt__NotificationMessageHolderType_Message * SOAP_FMAC4 soap_in__wsnt__NotificationMessageHolderType_Message(struct soap *soap, const char *tag, struct _wsnt__NotificationMessageHolderType_Message *a, const char *type)
{
	size_t soap_flag_tt__Message = 1;
	if (soap_element_begin_in(soap, tag, 0, type))
		return NULL;
	a = (struct _wsnt__NotificationMessageHolderType_Message *)soap_id_enter(soap, soap->id, a, SOAP_TYPE__wsnt__NotificationMessageHolderType_Message, sizeof(struct _wsnt__NotificationMessageHolderType_Message), 0, NULL, NULL, NULL);
	if (!a)
		return NULL;
	soap_default__wsnt__NotificationMessageHolderType_Message(soap, a);
	if (soap->body && !*soap->href)
	{
		for (;;)
		{	soap->error = SOAP_TAG_MISMATCH;
			if (soap_flag_tt__Message && soap->error == SOAP_TAG_MISMATCH)
				if (soap_in_PointerTo_tt__Message(soap, "tt:Message", &a->tt__Message, ""))
				{	soap_flag_tt__Message--;
					continue;
				}
			if (soap->error == SOAP_TAG_MISMATCH)
				soap->error = soap_ignore_element(soap);
			if (soap->error == SOAP_NO_TAG)
				break;
			if (soap->error)
				return NULL;
		}
		if (soap_element_end_in(soap, tag))
			return NULL;
	}
	else
	{	a = (struct _wsnt__NotificationMessageHolderType_Message *)soap_id_forward(soap, soap->href, (void*)a, 0, SOAP_TYPE__wsnt__NotificationMessageHolderType_Message, 0, sizeof(struct _wsnt__NotificationMessageHolderType_Message), 0, NULL);
		if (soap->body && soap_element_end_in(soap, tag))
			return NULL;
	}
	return a;
}



结述语

最后由我来总结性的提些问题.

  1. 请问你使用gSoap的目的是为什么?
  2. 请问你觉得gSoap是用于处理什么样事情的?
  3. 请问ONVIF是什么?

对于问题1,我想大多数人都回答是出于工作需要才用到它的. 毫不例外我也是, 而且技术决策完全不是由我们来决定的. 因为我们完全无法推断上级的意志. 但BOSS的意志是必须执行的.所以我们路途就算会艰辛一点吧.

问题2 你可能会觉得和问题1没有什么两样, 但是你只要认真思考了问题1, 也就不难醒悟到, gSoap基本是soap协议的一个库支持. 依赖于gSoap方能使们从消息的封装与解析的繁琐工作中解放出来. gSoap为我们做的事情就是帮我组装序列化好的消息以便于发送, 并且能够将解析好的消息结构提供给我们以开发者真正关系的数据型态.

问题3 Sorry. ONVIF是一个市场标准, 由几个大企业协商制定的一个访问接口标准. 不是什么高级的东西.如果我们能加入该讨论社区我们也将参考标准的制定.


any way. let's talking on the Group 251296672 by QQ.

原文地址:https://www.cnblogs.com/qianwen36/p/3657421.html