当前位置: 首页 > article >正文

SOME/IP--协议英文原文讲解6

前言
SOME/IP协议越来越多的用于汽车电子行业中,关于协议详细完全的中文资料却没有,所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块:

1. SOME/IP协议讲解

2. SOME/IP-SD协议讲解

3. python/C++举例调试讲解


4.1.4.4 Strings
Following requirements are common for both fixed length and dynamic length strings.
以下要求 适用于 动态和 固定长度string


[PRS_SOMEIP_00372]
Upstream requirements: RS_SOMEIP_00038
Different Unicode encoding shall be supported including UTF-8, UTF-16BE and UTF16LE.
UTF-16LE和UTF-16BE都是UTF-16编码的不同字节序表示方法,它们之间的主要区别在于字节的存储顺序。

UTF-16LE(Little Endian)
定义:UTF-16LE,即Unicode Transformation Format 16-bit Little Endian,是一种将Unicode字符转换为二进制数据的编码方式,其中每个Unicode字符被表示为一个16位的二进制数。
字节顺序:在UTF-16LE中,最低有效字节(即低位字节)位于每个字符的前面,而最高有效字节(即高位字节)位于后面。这种存储方式被称为“小端序”或“Little Endian”。
使用场景:UTF-16LE是现代计算机系统广泛使用的一种编码方式,尤其在Windows操作系统上被广泛支持。它使得计算机能够处理世界上几乎所有的文字字符,并在国际化和本地化应用中起到了重要的作用。
UTF-16BE(Big Endian)
定义:UTF-16BE,即Unicode Transformation Format 16-bit Big Endian,同样是一种将Unicode字符转换为二进制数据的编码方式,但字节顺序与UTF-16LE相反。
字节顺序:在UTF-16BE中,最高有效字节位于每个字符的前面,而最低有效字节位于后面。这种存储方式被称为“大端序”或“Big Endian”。
字节顺序标记:为了区分UTF-16LE和UTF-16BE,UTF-16编码格式通常会在文件开头插入一个字节顺序标记(BOM)。对于UTF-16BE,BOM为FE FF;而对于UTF-16LE,BOM为FF FE。这个BOM可以帮助系统或应用程序正确识别并解析文件的编码格式。
主要区别
字节存储顺序:UTF-16LE采用小端序存储字节,即低位字节在前;而UTF-16BE采用大端序存储字节,即高位字节在前。
BOM:虽然UTF-16编码本身可以不带BOM,但带BOM的UTF-16文件可以通过BOM来区分是UTF-16LE还是UTF-16BE。
在实际应用中,选择使用UTF-16LE还是UTF-16BE主要取决于目标系统或应用程序的兼容性要求。不同的系统或应用程序可能支持不同的字节序,因此在进行数据交换或存储时,需要确保编码格式与目标系统或应用程序的要求相匹配。


[PRS_SOMEIP_00948]
Upstream requirements: RS_SOMEIP_00038
UTF-8 strings shall be zero terminated with a "\0" character. This means they shall
end with a 0x00 Byte.
UTF-8格式后面必须要有结束符 \0
 

[PRS_SOMEIP_00084]
Upstream requirements: RS_SOMEIP_00038
UTF-16LE and UTF-16BE strings shall be zero terminated with a "\0" character. This
means they shall end with (at least) two 0x00 Bytes.
UTF-16x格式 后面必须跟连个结束符 \0 -- 0x00

[PRS_SOMEIP_00085]
Upstream requirements: RS_SOMEIP_00038
UTF-16LE and UTF-16BE strings shall have an even length.
两种编码方式的长度应该一样

[PRS_SOMEIP_00086]
Upstream requirements: RS_SOMEIP_00038
UTF-16LE and UTF-16BE strings having an odd length the last byte shall be ignored.
UTF16是偶数个字节 多出来应忽略

[PRS_SOMEIP_00087]
Upstream requirements: RS_SOMEIP_00038
All strings shall always start with a Byte Order Mark (BOM) in the first three (UTF-8)
or two (UTF-16) bytes of the to be serialized array containing the string. The BOM shall
be included in fixed-length-strings as well as dynamic-length strings. BOM allows the
possibility to detect the used encoding.
在 UTF-8 和 UTF-16 中:
UTF-8 BOM: 0xEF, 0xBB, 0xBF(3个字节)。
UTF-16 BOM:
Big-Endian (BE): 0xFE, 0xFF(2个字节)。
Little-Endian (LE): 0xFF, 0xFE(2个字节)。
BOM 的主要作用是:
标识编码格式:告诉接收方数据是使用何种编码格式。
消除歧义:避免因字节序(大端或小端)的不同而产生的问题。
BOM 既适用于固定长度字符串,也适用于动态长度字符串。
位于字符串的最前面

举例:UTF-16BE
FE FF 4F 60 59 7D 00 00 --> 其中前面三个字节是BOM,4F60是汉字'你' 597D是汉字'好'

举例:UTF-8
EF BB BF E4 BD A0 E5 A5 BD 00 --> 其中前面三个字节是BOM,E4 BD A0是汉字'你' E5 A5 BD是汉字'好'

4.1.4.4.1 Strings (fixed length)

[PRS_SOMEIP_00760]
Upstream requirements: RS_SOMEIP_00038
Strings with fixed length may start with an optional length field.
可以在固定长度前添加长度字段--需要由客户明确指定是否这样做

[PRS_SOMEIP_00373]
Upstream requirements: RS_SOMEIP_00038
Strings shall be terminated with a "\0"-character despite having a fixed length.
[PRS_SOMEIP_00374]
Upstream requirements: RS_SOMEIP_00038
The length of the string (this includes the "\0") in Bytes has to be specified in the data
type definition.
就算是固定长度也要以0结尾 且这个0包含在固定长度中。

4.1.4.4.2 Strings (dynamic length)
[PRS_SOMEIP_00089]
Upstream requirements: RS_SOMEIP_00039
Strings with dynamic length shall start with a length field. The length is measured in
Bytes.
动态长度string前面有个长度字段,表示后面的字节数

[PRS_SOMEIP_00090]
Upstream requirements: RS_SOMEIP_00039
The length field is placed before the BOM, and the BOM is included in the length.
BOM长度包含在string总长度内,固定长度string也一样,所以utf8最少有4个字节的长度(固定)

[PRS_SOMEIP_00091]
Upstream requirements: RS_SOMEIP_00039
String are terminated with a "\0".
Note:
The maximum number of bytes of the string (including termination with "\0") shall also
be derived from the data type definition.
注:客户释放矩阵表规范的的时候 通常会有最大长度,这个需要根据使用哪种编码格式来确定。
因为UTF-8 和 UTF-16编码出来的长度是不一样的

[PRS_SOMEIP_00092]
Upstream requirements: RS_SOMEIP_00039
[PRS_SOMEIP_00084], [PRS_SOMEIP_00085] and [PRS_SOMEIP_00086] shall
also be valid for strings with dynamic length.
[PRS_SOMEIP_00093]
Upstream requirements: RS_SOMEIP_00039
Dynamic length strings shall have a length field of 8, 16 or 32 Bits. This shall be
determined by configuration.
[PRS_SOMEIP_00094]
Upstream requirements: RS_SOMEIP_00039, RS_SOMEIP_00040
If not configured the length of the length field that is added in front of the string is 32
Bits (default length of length field).
需要客户指定长度字段的位数,如果未指定则默认32位(4字节)

[PRS_SOMEIP_00095]
Upstream requirements: RS_SOMEIP_00039
The length of the Strings length field is not considered in the value of the length field;
i.e. the length field does not count itself.
string长度 不包括长度字段本身的字节数

举例:UTF-16BE 你好
00 00 00 08 FE FF 4F 60 59 7D 00 00

4.1.4.5 Arrays
4.1.4.5.1 Arrays (fixed length) 定长数组
Fixed length arrays are easier for use in very small devices. Dynamic length arrays
might need more resources on the ECU using them.
固定长度数组占用资源比较动态长度较少 更适合小设备(资源不足的设备)使用
[PRS_SOMEIP_00944]
Upstream requirements: RS_SOMEIP_00036
Arrays with fixed length may start with an optional length field.
Note: Overruns of fixed-size arrays can only be detected with a length field.
可选的:可在前面加指定字节数的长度字段,需要客户指定
注:要做 溢出 判断 依赖于前面的长度字段

One-dimensional 一维数组
[PRS_SOMEIP_00099]
Upstream requirements: RS_SOMEIP_00035, RS_SOMEIP_00036
The one-dimensional arrays with fixed length "n" shall carry exactly "n" elements
of the same type. An optional length field may preceed the first element (see
[PRS_SOMEIP_00944].
可选 长度字段 需要放在第一个元素之前 表示的元素总字节数
Note: If a length field is defined for a specific fixed-length array, then this array is represented
 on the bus as a composite of the length field and the collection of n elements
of the same data type.
The layout of [PRS_SOMEIP_00099] is shown in Figure 4.7.
如果配置了长度字段,则在内存排布上由 长度字段 和 相同元素数组构成
可配置项 -- 需要客户指定

Multidimensional

[PRS_SOMEIP_00101]
Upstream requirements: RS_SOMEIP_00028, RS_SOMEIP_00035, RS_SOMEIP_00036
The serialization of multidimensional arrays follows the in-memory layout of multidimensional
arrays in the C/C++ programming language (row-major order).
多维数组的内存排布和C/C++语言一样 (行主序 -- 按行遍历)
Note: If a length field is defined for a specific multidimensional fixed-length array, then
this array is represented on the bus as a composite of a length field and n collections
consisting each of a length field and m elements of the same data type.
The layout of [PRS_SOMEIP_00101] is shown in Figure 4.8.

 

4.1.4.5.2 Dynamic Length Arrays
[PRS_SOMEIP_00375]
Upstream requirements: RS_SOMEIP_00037
The layout of arrays with dynamic length shall be based on the layout of fixed length
arrays.
动态数组 必须要依赖 长度字段描述动态数组长度
[PRS_SOMEIP_00376]
Upstream requirements: RS_SOMEIP_00037
A length field at the beginning of a dynamic length array shall be used to specify the
length of the array in Bytes.
需要前面的长度字段 描述后面的数组字节数
[PRS_SOMEIP_00107]
Upstream requirements: RS_SOMEIP_00037
Dynamic length arrays shall have a length field with a length of 8, 16 or 32 Bits. This
shall be determined by configuration.
客户可以指定前面长度字段 的字节数,如果不指定 默认4个字节,且长度字段描述的长度不包含自己本身的长度
[PRS_SOMEIP_00377]
Upstream requirements: RS_SOMEIP_00037
The length does not include the size of the length field.
长度字段不应统计包含自己本身的字节数

Note:
If the length of the length field is set to 0 Bits, the number of elements in the array has
to be fixed; thus, being an array with fixed length.
如果没有长度字段 那一定是固定长度数组
这句不是废话:它杜绝了自以聪明的客户 通过SOME/IP header的length字段 推断 数组长度 而不设置长度字段的可能性。
因为 UDP报文可能会出错,另外需要一维数组的布局 和 多维数组的布局保持一致 所以必须要这个长度字段(见下面)
The layout of dynamic arrays is shown in Figure 4.9 and Figure 4.10

 

In the one-dimensional array one length field is used, which carries the number of bytes
used for the array. 长度域是后面数组总字节数
The number of static length elements can be easily calculated by dividing by the size
of an element.
In the case of dynamical length elements the number of elements cannot be calculated,
but the elements must be parsed sequentially.
如果元素的长度是固定的,那么数组元素的格式计算就简单:总长度 除以 单个元素的长度
否则,就没法计算,只能每个元素挨个解析(可变长度元素的长度肯定是可以解析到的 比如元素是变长字符串)
等解析完之后 再统计元素个数
Figure 4.10 shows the structure of a Multidimensional Array of dynamic length. 

 

[PRS_SOMEIP_00114]
Upstream requirements: RS_SOMEIP_00037
In multidimensional arrays every sub array of different dimensions shall have its own
length field.
多维数组的每个子数组前必须要有自己的长度字段
If static buffer size allocation is required, the data type definition shall define the maximum
length of each dimension.
编程指导:数组的缓存区要按最长数组维度的大小
Rationale: When measuring the length in Bytes, complex multi-dimensional arrays can
be skipped over in deserialization.
数组前加长度字段的原因:在反序列化时,可以跳过不需要的复杂数组,直接访问后面需要的数据
SOME/IP also supports that different length for columns and different length for rows
in the same dimension. See k_1 and k_2 in Figure 4.10. A length indicator needs to
be present in front of every dynamic length array. This applies for both outer and all
inner/nested arrays.
此结构支持 不同长度的数组嵌套:
类似:
int *a[2];
a[0] 指向 int b[4];
a[1]指向  int c[5];
且 b和c的内存排布连续
[PRS_SOMEIP_00945]
Upstream requirements: RS_SOMEIP_00037, RS_SOMEIP_00040
If not configured the length of the length field that is added in front of the dynamic
length array is 32 Bits (default length of length field).
客户没有指定 长度域的字节数 默认4个字节

4.1.4.6 Enumeration
[PRS_SOMEIP_00705]
Upstream requirements: RS_SOMEIP_00030, RS_SOMEIP_00033
Enumerations are not considered in SOME/IP. Enumerations shall be transmitted as
unsigned integer datatypes.
枚举在SOME/IP协议中是无符号整数类型
具体是几个字节的 需要 客户指定

4.1.4.7 Bitfield
[PRS_SOMEIP_00300]
Upstream requirements: RS_SOMEIP_00033, RS_SOMEIP_00030
Bitfields shall be transported as unsigned datatypes uint8/uint16/uint32/uint64.
The data type definition will be able to define the name and values of each bit.
位域 按照无符号数据传输,
具体是几个字节 需要客户指定
每一位表示一个变量名 和 值

4.1.4.8 Union / Variant
联合体 变体
联合体的传输 额外需要一个字段指定 联合体中哪个成员形式的传输。这样接收方也好解析出来
There are use cases for defining data as unions on the network where the payload can
be of different data types.
A union (also called variant) is such a parameter that can contain different types of
data. For example, if one defines a union of type uint8 and type uint16, the union shall
carry data which are a uint8 or a uint16.
Which data type will be transmitted in the payload can only be decided during execution.
In this case, however, it is necessary to not only send the data itself but add an
information about the applicable data type as a form of "meta-data" to the transmission.
By the means of the attached meta-data the sender can identify the applicable data
type of the union and the receiver can accordingly access the data properly.
[PRS_SOMEIP_00118]
Upstream requirements: RS_SOMEIP_00034
A union shall be used to transport data with alternative data types over the network.
[PRS_SOMEIP_00119] Union (length field, type selector field and payload)
Upstream requirements: RS_SOMEIP_00034

长度字段的 字节数 需要客户指定。注:可以为0个字节,表示联合体的的每个成员长度相等
union {
  float a;
  int b;
}
配置字段的 长度 需要客户指定

还需要添加填充字符,填充的长度 是前面的长度字段减去实际的数据长度

 

[PRS_SOMEIP_00126]
Upstream requirements: RS_SOMEIP_00034
The length field shall define the size of the payload and padding in bytes and does
not include the size of the length field and type selector field.
长度字段 描述的长度 不包含自己本身 和 配置字段 
Note:
The padding can be used to align following data in the serialized data stream if configured
accordingly.
如果客户有需求,填充的字符还可用来和后面的数据做内存对齐,见下面例子
[PRS_SOMEIP_00121]
Upstream requirements: RS_SOMEIP_00034
dThe length of the length field shall be defined by configuration and shall be 32, 16, 8,
or 0 bitsc
[PRS_SOMEIP_00122]
Upstream requirements: RS_SOMEIP_00034
A length of the length field of 0 Bit means that no length field will be written to the
PDU.
[PRS_SOMEIP_00123]
Upstream requirements: RS_SOMEIP_00034
If the length of the length field is 0 Bit, all types in the union shall be of the same
length.
[PRS_SOMEIP_00129]
Upstream requirements: RS_SOMEIP_00034
The type selector field shall specify the payload type of the payload.
[PRS_SOMEIP_00127]
Upstream requirements: RS_SOMEIP_00034
The length of the type selector field shall be defined by configuration and shall be 32,
16, or 8 bits.
[PRS_SOMEIP_00906]
Upstream requirements: RS_SOMEIP_00034
Possible values of the type selector field shall be defined by the configuration for each
union separately.
[PRS_SOMEIP_00907]
Upstream requirements: RS_SOMEIP_00024, RS_SOMEIP_00034
The value 0 of the type selector field shall be reserved for the NULL type. In this case
the length of the payload shall be 0.
Note:
This denotes an empty union.
类型 值为0 表示没有类型 即不传输联合体数据。此时长度域的值也为0。

[PRS_SOMEIP_00130]
Upstream requirements: RS_SOMEIP_00028, RS_SOMEIP_00034
The payload is serialized depending on the type in the type selector field.
In the following example a length of the length field is specified as 32 Bits. The union
shall support a uint8 and a uint16 as data. Both are padded to the 32 bit boundary
(length=4 Bytes).
填充举例 -- 4字节内存对齐
根据反序列化规范[PRS_SOMEIP_00916]
如果不需要填充那么例子中的多余填充可以去掉,并修改uinon-length段的值为实际长度。


http://www.kler.cn/a/551533.html

相关文章:

  • UE5控件组件显示UMG文本不正常
  • 【Python项目】文件销毁工具文档
  • Pearson相关分析法
  • Qt的QTabWidget的使用
  • Mistral Saba:为中东和南亚量身打造的AI模型
  • DeepSeek横空出世,真的拯救了算力焦虑吗?
  • JavaScript数组-创建数组
  • V8 引擎的内存优化原理
  • Unity3D实现自动打包AB包
  • 【鸿蒙Next】鸿蒙应用发布前的准备
  • DeepSeek 助力 Vue 开发:打造丝滑的右键菜单(RightClickMenu)
  • C 单片机存储器修饰符xdata
  • Day48(补)【AI思考】-设计模式三大类型统一区分与记忆指南
  • 如何使用 Docker 实现容器化运维:从零开始的实战指南
  • 上传解析 Excel 表(XLSX 文件)转为JSON数据
  • VideoPipe-使用VLC构建RTSP串流显示
  • 千峰React:脚手架准备+JSX基础
  • 数字信道化过程中多相滤波器组matlab代码及测试
  • 完美支持Mermaid图表渲染:ChatBox多功能AI助手使用简介
  • 【自学笔记】版本控制与持续集成基础知识点总览-持续更新