Usa VHAL con el cliente nativo

VHAL admite clientes Java y nativos. Car Service es el único cliente de Java para VHAL. En el caso de las apps para vehículos, usa las APIs de Car (por ejemplo, CarPropertyManager) para acceder a las propiedades de VHAL en lugar de comunicarte directamente con VHAL. De hecho, SELinux bloquea el acceso directo. Para obtener más información, consulta la documentación de la API de Car en el índice de paquetes.

En el caso de los clientes nativos, a partir de Android 13, usa libvhalclient en lugar de conectarte directamente con VHAL. Esta es una biblioteca cliente que expone una interfaz común, IVhalClient.h, para implementaciones de VHAL de AIDL y HIDL. En el siguiente ejemplo, se muestra cómo crear un cliente nativo de VHAL y usarlo para obtener un número de identificación del vehículo (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;
}

Debes configurar la política de SELinux para permitir que tu cliente nativo acceda a VHAL. Por ejemplo:

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