ใช้ VHAL กับไคลเอ็นต์เนทีฟ

VHAL รองรับไคลเอ็นต์ Java และเนทีฟ Car Service เป็นไคลเอ็นต์ Java เพียงรายการเดียวสําหรับ VHAL สำหรับแอปรถยนต์ ให้ใช้ Car API (เช่น CarPropertyManager) เพื่อเข้าถึงพร็อพเพอร์ตี้ VHAL แทนการติดต่อ VHAL โดยตรง ความจริงแล้ว SELinux จะบล็อกการเข้าถึงโดยตรง โปรดดูรายละเอียดในเอกสารประกอบของ Car API ที่ดัชนีแพ็กเกจ

สำหรับไคลเอ็นต์เนทีฟ ตั้งแต่ Android 13 เป็นต้นไป ให้ใช้ libvhalclient แทนการเชื่อมต่อกับ VHAL โดยตรง ไลบรารีไคลเอ็นต์นี้จะแสดงอินเทอร์เฟซทั่วไป 1 รายการ IVhalClient.h สำหรับการใช้งาน VHAL ของ AIDL และ HIDL ตัวอย่างต่อไปนี้แสดงวิธีสร้างไคลเอ็นต์ 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)