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

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

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