Sử dụng VHAL với ứng dụng gốc

VHAL hỗ trợ Java và ứng dụng gốc. Dịch vụ ô tô là ứng dụng Java duy nhất cho VHAL. Đối với ứng dụng ô tô, hãy sử dụng API ô tô (ví dụ: CarPropertyManager) để truy cập vào các thuộc tính VHAL thay vì giao tiếp trực tiếp với VHAL. Trên thực tế, SELinux chặn quyền truy cập trực tiếp. Để biết thông tin chi tiết, hãy xem tài liệu về API ô tô tại Chỉ mục gói.

Đối với ứng dụng gốc, bắt đầu từ Android 13, hãy sử dụng libvhalclient thay vì kết nối trực tiếp với VHAL. Đây là một thư viện ứng dụng hiển thị một giao diện chung, IVhalClient.h để triển khai AIDL và HIDL VHAL. Ví dụ sau đây cho thấy cách tạo ứng dụng gốc VHAL và sử dụng ứng dụng đó để lấy Mã nhận dạng xe (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;
}

Bạn phải định cấu hình chính sách SELinux để cho phép ứng dụng gốc truy cập vào VHAL. Ví dụ:

# 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)