Use VHAL with the native client

VHAL supports Java and native clients. Car Service is the only Java client for VHAL. For car apps, use the Car APIs (for example, CarPropertyManager) to access VHAL properties instead of directly communicating with the VHAL. In fact, SELinux blocks direct access. For details, see the Car API documentation at Package Index.

For native clients, starting with Android 13, use libvhalclient instead of directly connecting with VHAL. This is a client library that exposes one common interface, IVhalClient.h for AIDL and HIDL VHAL implementations. The following example shows how to create a VHAL native client and use it to get a Vehicle Identification Number (VIN) number:

#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;
}

You must configure SELinux policy to allow your native client to access VHAL. For example:

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