搭配原生用戶端使用 VHAL

VHAL 支援 Java 和原生用戶端。Car Service 是 VHAL 唯一的 Java 用戶端。針對車輛應用程式,請使用 Car API (例如 CarPropertyManager) 存取 VHAL 屬性,而非直接與 VHAL 通訊。事實上,SELinux 會封鎖直接存取權。詳情請參閱 Package Index 中的 Car API 說明文件。

針對原生用戶端,從 Android 13 開始,請使用 libvhalclient,而非直接連線至 VHAL。這是用戶端程式庫,可公開一個通用介面 IVhalClient.h,供 AIDL 和 HIDL VHAL 實作。以下範例說明如何建立 VHAL 原生用戶端,並使用該用戶端取得車輛識別號碼 (VIN) 號碼:

#include <IVhalClient.h>
#include <VehicleHalTypes.h>
#include <VehicleUtils.h>

using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
using ::android::frameworks::automotive::vhal::IVhalClient;
using ::android::hardware::automotive::vehicle::toInt;

int main(int argc, char** argv) {
  auto vhalClient = IVhalClient::tryCreate();
  if (vhalClient == nullptr) {
    // handle error.
    return -1;
  }
  auto result = vhalClient->getValueSync(
      *vhalClient->createHalPropValue(toInt(VehicleProperty::INFO_VIN)));
  // Use result

  return 0;
}

您必須設定 SELinux 政策,允許原生用戶端存取 VHAL。例如:

# Define my domain
type my_native_daemon, domain;

# Define the exec file type.
type my_native_daemon_exec, exec_type, file_type, system_file_type;

# Initialize domain.
init_daemon_domain(my_native_daemon)

# Allow using hwbinder for HIDL VHAL, not required if AIDL is used.
hwbinder_use(my_native_daemon)
# Allow using binder for AIDL VHAL
binder_use(my_native_daemon)
# Allow my_native_daemon to be a VHAL client.
hal_client_domain(my_native_daemon, hal_vehicle)