前言
SOME/IP协议越来越多的用于汽车电子行业中,关于协议详细完全的中文资料却没有,所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块:
1. SOME/IP协议讲解
2. SOME/IP-SD协议讲解
3. python/C++举例调试讲解
这一章节是按照规范文档顺序讲解的。按内容 这章节应该排在 string/arry数据类型讲解完 再讲 才好理解。读者可直接先阅读 后面string/arry内容后再回过来看这一节。
4.1.4.3 Structured Datatypes and Arguments with Identifier and optional members (’TLV’)
To achieve enhanced forward and backward compatibility, an additional Data ID can
be added in front of struct members or method arguments. The receiver then can
skip unknown members/arguments, i.e. where the Data ID is unknown. New members/arguments can be added at arbitrary positions when Data IDs are transferred in the
serialized byte stream.
Moreover, the usage of Data IDs allows describing structs and methods with optional
members/arguments. Whether a member/argument is optional or not, is defined in the
data definition.
Whether an optional member/argument is actually present in the struct/method or not,
must be determined during runtime. How this is realized depends on the used programming
language or software platform (e.g. using a special available flag, using a
special method, using pointers which might be null, ...).
给结构体每个成员前加个数据ID 用于标识当前methed/notify/struct每个参数成员的唯一性。这样的一个好处就是就是可以向前、后兼容。另一个好处就是同一个method/notify的参数列表(也可以理解成结构体成员,只是对连续多个数据组织方式的一种描述)可以做配置项--打开或关闭,这个要在运行时体现,需要在代码中配置好。比如:同一个method, 根据不同的入参请求 返回不同的参数表。后面说的dataID 也可以称为Tag.
发送端给需要发送的成员的前添加ID,接收端就可以根据ID判断 是哪个成员参数,无法识别的就跳过。这样成员的顺序就没强制要求。
注意:上面说的参数列表是notify/method的所有参数。TLV-struct如果是被嵌套在另一个非TLV的struct中,那TLV-struct就是notify/method就是参数的一部分。SOME/IP TLV的用法和Protobuf协议中TLV一样,可以做参考。
### **TLV的缺点:**
1. **数据冗余:**
- 每个字段都有Tag和Length,会增加传输的数据量。
2. **实现复杂度提升:**
- 需要为TLV的序列化和反序列化设计专门的逻辑。
- 特别是Tag和Length的编码需要一致性,并需要在传输前进行规划。
---
### **总结:TLV的必要性**
尽管直接传输结构体在简单系统中更高效,但在复杂通信场景(如AUTOSAR车载系统)中,使用TLV提供了以下关键优势:
- **前向兼容性**:支持较新版本的字段。
- **后向兼容性**:跳过旧版本不支持的字段。
- **动态扩展性**:支持可选字段和未来扩展。
[PRS_SOMEIP_00201]
Upstream requirements: RS_SOMEIP_00050
A Data ID shall be unique within the direct members of a struct or arguments of a
method. 同一个method/notify/struct的 数据成员ID 要唯一,否则不需要唯一。
Note:
Please note that a Data ID does not need to be unique across different structs or
methods.
Note:
Please note that neither the AUTOSAR Methodology nor AUTOSAR CP RTE, nor
AUTOSAR AP ara::com support the definition or usage of optional method arguments
at the time being. --- 不过现在的AUTOSAR版本不支持可变参数列表的功能
[PRS_SOMEIP_00230]
Upstream requirements: RS_SOMEIP_00050
A Data ID shall be defined either for all members of the same hierarchical level of a
struct or for none of them. -- 同一层次级别的所有参数成员 要么都有ID标识 要么都没有。
[PRS_SOMEIP_00231]
Upstream requirements: RS_SOMEIP_00050
A Data ID shall be defined either for all arguments of a method or for none of them.
In addition to the Data ID, a wire type encodes the datatype of the following member.
Data ID and wire type are encoded in a so-called tag. dataID中有专门几个位是wire-type,用来表示后面携带数据的数据类型,用以反序列化时知道后面数据