通信易懂唠唠SOME/IP——SOME/IP 协议规范
主要介绍SOME/IP协议中远程过程调用RPC,事件通知Event。
一 Transport Protocol Bindings传输协议绑定
SOME/IP目前支持TCP连接,UDP单播连接,UDP多播连接
1.1 使用UDP还是TCP的指导原则:
• 只有在传输大数据块(>1400bytes),且在出现错误时对延时没有严格要求时使用TCP。
• 如果对延时有严格要求(延时<100ms)时使用UDP。
• 如果要传输的数据块大,同时对延时也有严格要求,可以使用SOME/IP-TP的UDP传输。
同一个insatnce的所有event,method,field应该使用同一个udp单播或者udp多播或者tcp连接,即绑定方式是针对instance的不是针对具体的event或者method的。
而实际上,车辆内部许多应用程序,尤其是智驾相关的应用程序,为了做出快速的响应,对延时有严格的要求,所以UDP传输用的更普遍。虽然UDP不能像TCP那样处理位错误、丢包、分段、网络拥塞等错误,但应用程序本身可以处理 这些不太可能发生的错误。
1.2 支持一个包里传输多个SOME/IP消息
比如下面的例子,一个PDU中包含了2个Subscribe消息和6条Subscribe ACK消息。
1.3 多service instance
一个service可以有多个service instance,不同的service instance用instance id区分。
不同服务的多个instance可以使用相同的端口号,同一个service的多个instance不能使用相同的端口号。
服务发现阶段的报文不包含instance id,可以用socket三元素(ip,port,传输协议)区分不同的instance。
同一个service instance 建议tcp和udp使用相同的端口号。
二 Request/Response Communication 请求应答通信
请求应答通信方式是常见的通信方式之一。通信的Clinet端发出请求消息,通信的Server端作出应答。
2.1 SOME/IP的Request/Response消息Client和Server端分别要做的工作
Client:
• Construct the payload
构造payload。Method的所有参数按照函数签名中的顺序序列化。
• Set the Message ID based on the method the client wants to call。
设置Message ID(SerivceId和MethodID)
• Set the Length field to 8 bytes (for the part of the SOME/IP header after the length field) + length of the serialized payload
设置Length=SOME/IP header length之后的部分的大小+payload大小
• Optionally set the Request ID to a unique number (shall be unique for client only)。
设置RequsetID(Client ID和Session ID)。
• Set the Protocol Version according [PRS_SOMEIP_00052]
设置协议版本号0x01
• Set the Interface Version according to the interface definition
设置Interface版本号
• Set the Message Type to REQUEST (i.e. 0x00)
设置Message Type=0x00
• Set the Return Code to 0x00
设置Return Code=0x00
Server:
• Construct the payload
构造payload。Method的Out和InOut参数按照函数签名中的顺序序列化。
• take over the Message ID from the corresponding request。
从相应的请求中接管Message ID。
• Set the length to the 8 Bytes + new payload size
设置length
• take over the Request ID from the corresponding request
从相应的请求中接管Request ID
• Set the Message Type to RESPONSE (i.e. 0x80) or ERROR (i.e. 0x81)
设置MessageType=0x80(正常时)或者0x81(出错时)
• set Return Code to the return code of called method or in case of an Error message to a valid error code.
设置RerurCode。
Return Code的有效值如下
2.2 例子
Client Request消息
Server端Response
三 Fire/Forget Communication请求无影灯的通信
客户端需要做的工作与Request/Response类似,只是MessageType是0x01。服务端不需要应答。
我们通常说的Method就是指Request/Response通信或者Fire/Forget通信。如果Method中有Out或者InOut参数,那么就要使用Request/Response,Server在Response的Payload中携带Out参数。如果Method参数列表中没有Out和InOut参数,那么Method可以设计成Request/Response,这时的Response的 Payload是空的,ReturnCode可以表明执行是否成功。也可以设计成Fire/Forget通信。
四 Notification Events通知事件
通知事件描述了一个通用的发布/订阅概念。 通常,服务器发布客户端订阅的服务。SOME/IP只是发布更新的事件,不做发布订阅的机制,这个机制的实现在SOME/IP-SD中。
4.1 在SOME/IP的通知消息中,服务端需要做的工作
• Construct the payload
构造payload。通知消息的有效载荷应包括事件的序列化数据 。
• Set the Message ID based on the event the server wants to send
根据服务端想发送的event设置Message ID,包括ServiceID和Method ID。
• Set the Length field to 8 bytes (for the part of the SOME/IP header after the length field) + length of the serialized payload
设置Length=SOME/IP header length之后的部分的大小+payload大小
• Set the Client ID to 0x00. Set the Session ID according to [PRS_SOMEIP_00932], [PRS_SOMEIP_00933], and [PRS_SOMEIP_00521]. In case of active Session Handling the Session ID shall be incremented upon each transmission
设置ClientID=0x00,设置Session ID从0x1-0xFFFF循环递增。
. • Set the Protocol Version according [PRS_SOMEIP_00052]
设置协议版本号0x01
• Set the Interface Version according to the interface definition
设置Interface版本号
• Set the Message Type to NOTIFICATION (i.e. 0x02)
设置Message Type=0x02
• Set the Return Code to 0x00
设置ReturnCode=0x00
4.2 例子
4.3 发送通知的三种机制
• Cyclic update — send an updated value in a fixed interval (e.g. every 100 ms for safety relevant messages with Alive)
周期发送
• Update on change — send an update as soon as a "value" changes (e.g. door open)
只在发送变化时发送
• Epsilon change — only send an update when the difference to the last value is greater than a certain epsilon. This concept may be adaptive, i.e. the prediction is based on a history; thus, only when the difference between prediction and current value is greater than epsilon an update is transmitted.
与上一次的值不一样的发送。
五 Fields
字段表示状态并具有有效值。 消费者订阅 获取字段值作为初始事件。
一个Field可以有getter和setter方法,和一个notification event。
即Field是上述Request/Response和Notification Event的结合。getter是一个只有Out参数的Method,其Out的值就是Filed要更新的值,即Request的Payload是空,Response的Payload是返回的Field的值。setter有一个In参数一个Out参数,In参数是期望设置的值,Out参数是返回的FIeld的值。即Request的Payload中是要设置的值,Response的Payload中是返回的filed的值。