语言规范

本页面使用 ISO/IEC 14977 扩展巴科斯-诺尔范式 (EBNF) 和 protobuf 指定车辆服务接口定义语言 (VSIDL)。本页面重点介绍该语言的上下文无关语法以及语言元素的含义。

快速参考

下表总结了 VSIDL 语言元素及其主要编译器验证代码:

语言元素 在 VSIDL 目录中的角色 关键编译器错误代码
VSIDL 条目 (VsidlEntry) 声明目录 package、服务包和协议映射的根 textproto 容器。 E10B、E211
服务套装 (ServiceBundle) 逻辑部署和授权单元,用于对发布者、订阅者、RPC 服务器和 RPC 客户端进行分组。 E100、E205–E20C、E301–E303、E308、E309
Publisher (Publisher) 在一个或多个小写短划线大小写主题上发布 protobuf message,且队列 capacity ≥ 2。 E20D、E20F、E302、E304、E306、E308、E314、E406、E407、E504、E601
Subscriber (Subscriber) 在一个或多个匹配的发布主题上订阅 protobuf message。 E311、E408、E504、E608
RPC 服务器 (Server) 在小写短划线命名法 channel 上公开 protobuf RPC service(一元、客户端流式传输或服务器流式传输)。 E100、E20E、E409、E40B、E603
RPC 客户端 (Client) 连接到匹配的 protobuf RPC service 和 channel。 E409、E60A、E60B
build 配置 (BuildConfiguration) 可选:snake_case Soong target_name 替换或 skip_codegen 切换服务软件包。 E205 - E208,E301

下表按验证类别对十六进制 vsidlc 诊断代码进行了分组:

错误系列(十六进制) 验证类别 vsidlc 检查的内容
E1xx 目录、构建文件和服务器警告 目录 Android.bp 文件组覆盖率 (E10B) 和每个 bundle 的 RPC 服务器限制 (E100)。
E2xx 命名和长度规范 软件包长度 (E211)、PascalCase 个软件包名称 (E209–E20C)、snake_case 个目标名称 (E205–E208) 以及小写短划线分隔的主题和渠道 (E20D–E20F)。
E3xx 唯一性和冲突 全局 bundle 名称唯一性 (E309)、build 目标唯一性 (E301)、服务单元名称唯一性 (E302–E308) 以及主题唯一性或重新声明 (E311、E314)。
E4xx 必填字段和结构不变量 偶数发布者 capacity ≥ 2(E406、E407)、强制性订阅者 topic (E408) 和强制性唯一 RPC channel(E409、E40B)。
E5xx 跨目录配对和中间件限制 每个订阅者都有匹配的发布者 (E504),每个客户端都有匹配的 RPC 服务器 (E503),以及不支持双向 RPC 流式传输 (E514)。
E6xx Protobuf 类型和服务存在 发布者和订阅者上引用的 protobuf message 存在性(E601、E608)以及 RPC 服务器和客户端上唯一的 protobuf service 存在性(E603、E60A、E60B)。

语言层次结构

根据元对象设施 (MOF),VSIDL 编译器 (VSIDLC) 使用图 1 所示的语言:

VSIDLC 语言

图 1. VSIDLC 语言。

VSIDLC 主要基于协议缓冲区 (protobuf) 语言。Protobuf 指定使用发布-订阅和远程过程调用 (RPC) 交换的数据类型。从技术上讲,VSIDL 模型是 textproto 文件,VSIDL 语法在 protobuf 文件 (syntax.proto) 中定义。

用于指定数据类型的 protobuf 文件和 VSIDL 模型都用于生成 Rust 代码。生成的代码包含用于交换消息数据结构的结构体,以及用于创建服务单元的 Rust 函数,但不会自动调用这些服务单元。然后,自定义 Rust 代码会使用生成的代码来实例化服务单元并实现应用逻辑。

抽象语法

图 2 显示了 VSIDL 中的核心消息类型:

VSIDL 中的核心消息类型

图 2. VSIDL 中的核心消息类型。

VSIDL 条目

本部分介绍了 VsidlEntry 消息类型。

EBNF 语法

start VsidlEntry =
  "package" , ":" , String , ";" ,
  { "service_bundle" , ServiceBundle } ,
  { "extension" , ":" , Any } ,
  { "some_ip_mapping" , ":" , SomeIpMapping } ,
  { "vhal_mapping" , ":" , VhalMapping }
;

Proto 定义

// The root message for VSIDL files
message VsidlEntry {
  // Required. Package name for entities mentioned in the file.
  string package = 1;
  // Enables custom extensions beyond the standard VSIDL model.
  repeated google.protobuf.Any extension = 3;

  // SOMEIP mapping rules
  repeated sdv.someip.v1.SomeIpMapping some_ip_mapping = 4;
  // VHAL mapping rules
  repeated VhalMapping vhal_mapping = 5;
  // List of SDV service bundles defined in the file.
  repeated ServiceBundle service_bundle = 6;
}

用法示例

package: "com.android.sdv.sample.vsidl"

service_bundle {
  name: "Manager"

  publisher {
    message: "TirePressure"
    topic: "front-left"
    topic: "front-right"
    capacity: 10
  }
}

说明

VsidlEntry 消息充当扩展名为 .vsidl 的 VSIDL 文件的根容器。此消息将所有定义和配置封装在单个 VSIDL 文件中。VsidlEntry 消息是绑定所有其他元素的顶级元素。

VsidlEntry 消息具有以下用途:

  • 定义 VSIDL 文件的整体结构。
  • 指定文件中所有实体的软件包命名空间。
  • 包含一组服务包定义。
  • 允许对 VSIDL 模型进行自定义扩展。
  • 包含 SOME/IP 和 VHAL 的映射规则。

限制条件

  • 软件包名称 (E211):软件包名称不得超过 127 个字符。
  • 悬空文件 (E10B):目录中的所有文件都必须在 Android.bp 文件组中引用。

服务包

本部分介绍了服务包消息类型。

EBNF 语法

ServiceBundle = "{" , { ServiceBundleElement } , "}" ;

ServiceBundleElement =
  "name" , ":" , String |
  "publisher" , Publisher |
  "subscriber" , Subscriber |
  "server" , Server |
  "client" , Client |
  "extension" , ":" , Any |
  "diagnostics_declaration" , DiagnosticsDeclaration |
  "build_cfg" , BuildConfiguration |
  "register_reflection_metadata" , Boolean
;

Proto 定义

// Defines an SDV service
message ServiceBundle {
  // Required. Name of the service bundle (without the package name).
  string name = 1;
  // List of publications the service bundle provides.
  repeated Publisher publisher = 2;
  // List of publications a service bundle subscribes to.
  repeated Subscriber subscriber = 3;
  // RPC services offered by a service bundle.
  repeated Server server = 4;
  // RPC services consumed by a service bundle.
  repeated Client client = 5;
  // Enables custom extensions beyond the standard VSIDL model.
  repeated google.protobuf.Any extension = 7;

  // Diagnostics declarations
  sdv.diagnostics.v1.DiagnosticsDeclaration diagnostics_declaration = 8;

  // Build Configuration
  optional BuildConfiguration build_cfg = 9;

  // Register metadata for service units provided by this service bundle.
  // Setting this to true will increase the memory footprint
  // and network load significantly.
  bool register_reflection_metadata = 10;
}

用法示例

service_bundle {
    name: "SeatController"

    publisher {
      message: "SeatHeating"
      topic: "driver-seat"
      capacity: 10
    }
}

说明

服务包定义了相关服务、发布者、订阅者、RPC 服务器和 RPC 客户端的逻辑分组。服务包充当一组特定功能及其交互的容器。

限制条件

  • 软件包名称要求(E209、E20A、E20B、E20C):
    • 服务包必须具有已填充的名称。
    • 名称必须以有效的 Unicode 标识符起始字符(通常为字母)开头。
    • 名称中的后续字符必须是有效的 Unicode 标识符延续字符(通常是字母或数字)。
    • 该名称不得是 Rust、Java 或 C++ 中的预留关键字。
  • 全局 bundle 唯一性 (E309):每个服务 bundle 都必须具有唯一的完全限定名称(基于其软件包和名称)。
  • 内部唯一性(E100、E302、E303、E308):
    • 在单个服务包中,每个 RPC 服务最多可由一个服务器定义提供服务。
    • 在单个服务包中,所有用户定义的服务单元名称(对于发布者或服务器)都必须是唯一的。
    • 在单个服务包中,所有服务单元名称(无论是用户定义的还是自动生成的)都必须是唯一的。
  • 构建目标命名规范(E205、E206、E207、E208):如果提供了自定义构建目标名称 (build_cfg.target_name),则该名称必须遵循蛇形命名法格式(小写字母、数字和单个下划线,不以下划线开头或结尾)。
  • 构建目标名称唯一性 (E301):用户定义的构建目标名称不得与其他服务软件包的任何自动生成的目标名称冲突。

发布商

本部分介绍了“发布商”消息类型。

EBNF 语法

Publisher = "{" , { PublisherElement } , "}" ;

PublisherElement =
  "message" , ":" , String |
  "topic" , ":" , String |
  "capacity" , ":" , Integer |
  "service_unit_name" , ":" , String
;

Proto 定义

// Represents a publisher within a service bundle.
message Publisher {
  // Name of the service unit. Name may only use characters from [a-z0-9\-]+,
  // must start with [a-z], may not end with a hyphen,
  // and may not contain consecutive hyphens.
  string service_unit_name = 3;
  // Required. The type of data being published.
  string message = 4;
  // Required. The number of messages a publication queue can hold.
  // Must be an even number >= 2.
  int64 capacity = 6;
  // Required. Unique identifier for the publication topic.
  // Must be in lowercase dash-case.
  repeated string topic = 7;
}

用法示例

publisher {
  message: "SeatHeating"
  topic: "driver-heating"
  capacity: 10
}

说明

Publisher 消息类型定义了 ServiceBundle 提供的数据源。此消息类型指定要发布的数据类型以及该数据的特定主题和容量。

主题

每个 Publisher 实例都有一个 message 字段,该字段引用正在发布的 proto 消息。它必须指定主题(由 topic 表示)和容量(由 capacity 表示)。

  • 主题:发布主题的唯一标识符。它必须遵循小写短划线命名法(例如 my-topic)。
  • 容量:指定队列的大小,即队列在舍弃未读消息之前可以容纳的消息数量。它必须是大于或等于 2 的偶数。

用户定义的名称

发布商可以设置用户定义的服务单元名称,以覆盖自动选择的服务单元名称。此功能可让您选择较短的名称。如果发布者使用用户定义的服务单元名称,则发布者只能声明一个主题,以便服务单元名称映射到一个主题。

# VALID: A publisher assigns a user-defined name to a single topic
publisher {
  message: "SeatHeating"
  topic: "seat-heating-status"
  capacity: 10
  service_unit_name: "heating-is-off"
}

# ERROR: User-defined names are allowed only when declaring a single topic
publisher {
  message: "SeatHeating"
  topic: "driver-seat-heating-status"
  topic: "passenger-seat-heating-status"
  capacity: 10
  service_unit_name: "heating-status"
}

限制条件

  • 本地名称唯一性 (E302):在任何单个服务包中,所有发布者都必须具有唯一的用户定义服务单元名称。
  • 全局名称唯一性 (E304):同一发布类型的发布者在所有服务包中必须具有全局唯一的用户定义服务单元名称。
  • 为单个通道命名 (E306):用户定义的服务单元名称只能分配给声明单个主题的发布者。
  • 服务单元名称唯一性 (E308):所有服务单元名称(无论是生成的还是用户定义的)在其服务包中都必须是唯一的;用户定义的名称应用于解决与生成的名称之间的冲突。
  • 订阅者的发布者存在性 (E504):每个定义的订阅者都需要至少一个针对指定消息类型和主题的相应发布者。
  • 有效的发布者类型 (E601):发布者必须引用与现有 protobuf 消息对应的消息类型。
  • 主题命名(E20D、E20F):主题必须采用小写短划线命名法,且不得超过 127 个字符。
  • 主题唯一性 (E314):主题在所有发布商中必须是全局唯一的。
  • 容量要求 (E406、E407):容量为必填项,且必须是大于或等于 2 的偶数。

订阅者

本部分介绍了 Subscriber 消息类型。

EBNF 语法

Subscriber = "{" , { SubscriberElement } , "}" ;

SubscriberElement =
  "message" , ":" , String |
  "topic" , ":" , String
;

Proto 定义

// Represents a subscriber within a service bundle.
message Subscriber {
  // Required. The type of data being subscribed to.
  string message = 4;
  // Required. Specific topic(s) of the message to subscribe to.
  // Must match the publisher's topic.
  repeated string topic = 6;
}

用法示例

subscriber {
  message: "SeatHeating"
  topic: "driver-seat"
}

说明

Subscriber 消息定义了 ServiceBundle 提供的发布接收器。此消息指定了要订阅的数据类型以及该发布内容的特定主题。如果一个主题有多个发布者,则订阅者会收到所有发布者发布的消息。

限制条件

  • 发布者存在 (E504):对于每个定义的订阅者,必须至少存在一个发布指定消息类型和主题的相应发布者。
  • 有效的订阅类型 (E608):订阅者必须引用与现有 protobuf 消息对应的消息类型。
  • 主题为必填项 (E408):主题是订阅者的必填项。
  • 主题重新声明 (E311):不应在同一服务包中重新声明订阅者主题。

RPC 服务器

本部分介绍了 RPC 服务器消息类型。

EBNF 语法

Server = "{" , { ServerElement } , "}" ;

ServerElement =
  "service" , ":" , String |
  "channel" , ":" , String |
  "service_unit_name" , ":" , String
;

Proto 定义

// Represents an RPC server within a service bundle.
message Server {
  // Deprecated. Name of the service unit.
  string service_unit_name = 3 [ deprecated = true ];
  // Required. Name of the RPC service.
  string service = 4;
  // Required. Name of the RPC channel.
  // Must be in lowercase dash-case.
  string channel = 5;
}

用法示例

server {
  service: "SetTemperature"
  channel: "temp-setter"
}

说明

Server 消息定义了 ServiceBundle 提供的 RPC 服务器。此消息指定服务器实现的服务和 RPC 通道。

RPC 服务器会公开一组方法,供客户端远程调用。service 字段用于指定服务器实现的 RPC 服务的名称。此服务在 proto 文件中定义,并在自定义 Rust 代码中实现。RPC 服务可以包含一元、客户端流式传输和服务器流式传输方法,如 protobuf 服务定义中所定义。channel 字段定义了通信端点,并且是必填字段 (E409)。

限制条件

  • 服务定义 (E603):RPC 服务器必须指定一个与现有 protobuf RPC service 值对应的 service 值。
  • 每个服务的服务器限制 (E100):在单个服务包中,特定的 RPC service 最多只能由一个服务器定义提供服务。
  • 频道名称 (E20E):RPC 频道必须采用小写短划线命名法。
  • 渠道为必填项 (E409):RPC 渠道为必填项。
  • 通道唯一性 (E40B):RPC 通道只能由一项服务使用。

RPC 客户端

本部分介绍了 RPC 客户端消息类型。

EBNF 语法

Client = "{" , { ClientElement } , "}" ;

ClientElement =
  "service" , ":" , String |
  "channel" , ":" , String
;

Proto 定义

// Represents an RPC client within a service bundle.
message Client {
  // Required. Name of the RPC service.
  string service = 2;
  // Required. Name of the RPC channel.
  // Must match the server's channel and be in lowercase dash-case.
  string channel = 3;
}

用法示例

client {
  service: "SetTemperature"
  channel: "temp-setter"
}

说明

client 定义了 ServiceBundle 使用的 RPC 客户端。client 指定客户端与之交互的服务以及要连接到的通道。客户端可以与一元、客户端流式传输和服务器流式传输方法进行交互,具体取决于服务定义。

限制条件

  • 服务定义 (E60A):RPC 客户端必须指定与现有 protobuf service 定义相对应的 service。
  • 唯一服务来源 (E60B):RPC 客户端的 service 引用的 protobuf service 定义必须在所有 protobuf 文件中唯一定义(不能多次定义)。
  • 渠道为必填项 (E409):RPC 渠道为必填项。

构建配置

本部分介绍了 build 配置消息类型。

EBNF 语法

BuildConfiguration = "{" , BuildConfigurationElement, "}" ;

BuildConfigurationElement =
  "target_name" , ":" , String |
  "skip_codegen" , ":" , Boolean
;

Proto 定义

// Defines additional information used to configure build settings
message BuildConfiguration {
  /// Build target name
  optional string target_name = 1;
  // Do not generate code for this service bundle
  optional bool skip_codegen = 2;
}

用法示例

build_cfg {
  target_name: "my_custom_target_name"
  skip_codegen: false
}

说明

BuildConfiguration 为代码生成配置 ServiceBundle 的非标准参数。所有 build 配置都是可选的。

  • target_name(可选 string):指定 Android.bp 文件中 build 目标的名称。使用此选项可设置比自动选择的名称更短的目标名称。
  • skip_codegen(可选 bool):指示是否应为此服务包跳过代码生成。如果设置为 true,则不会为此特定服务包生成任何代码。这对于手动实现的服务包非常有用。默认情况下,该设置为 false。

限制条件

  • 目标名称格式(E205、E206、E207、E208):如果提供了自定义 build 目标名称 (build_cfg.target_name),则该名称必须严格遵循蛇形命名法格式:
    • 它只能包含小写字母(a 到 z)、数字(0 到 9)和下划线 (_)。
    • 不得包含连续的下划线 (__)。
    • 不得以下划线开头。
    • 不得以下划线结尾。
  • 目标名称唯一性 (E301):用户定义的 build_cfg.target_name 在整个构建系统中必须是唯一的,并且不得与从其他服务包定义派生的任何自动生成的目标名称冲突。