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

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

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