Native Client와 함께 VHAL 사용

VHAL은 Java 및 네이티브 클라이언트를 지원합니다. 자동차 서비스는 VHAL의 유일한 Java 클라이언트입니다. 자동차 앱의 경우 Car API (예: CarPropertyManager)를 사용하여 VHAL 속성에 액세스하며 VHAL과 직접 통신하지 않습니다. 실제로 SELinux는 직접 액세스를 차단합니다. 자세한 내용은 패키지 색인의 Car API 문서를 참고하세요.

네이티브 클라이언트의 경우 Android 13부터 VHAL과 직접 연결하는 대신 libvhalclient를 사용하세요. 이는 AIDL 및 HIDL VHAL 구현을 위한 하나의 공통 인터페이스인 IVhalClient.h를 노출하는 클라이언트 라이브러리입니다. 다음 예는 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;
}

네이티브 클라이언트가 VHAL에 액세스할 수 있도록 SELinux 정책을 구성해야 합니다. 예를 들면 다음과 같습니다.

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