Utiliser VHAL avec le client natif

VHAL est compatible avec les clients Java et natifs. Car Service est le seul client Java pour VHAL. Pour les applications pour voitures, utilisez les API pour voitures (par exemple, CarPropertyManager) pour accéder aux propriétés VHAL au lieu de communiquer directement avec le VHAL. En fait, SELinux bloque l'accès direct. Pour en savoir plus, consultez la documentation de l'API Car sur le site de l'index des packages.

Pour les clients natifs, à partir d'Android 13, utilisez libvhalclient au lieu de vous connecter directement à VHAL. Il s'agit d'une bibliothèque cliente qui expose une interface commune, IVhalClient.h, pour les implémentations VHAL AIDL et HIDL. L'exemple suivant montre comment créer un client natif VHAL et l'utiliser pour obtenir un numéro VIN (Vehicle Identification 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;
}

Vous devez configurer la règle SELinux pour autoriser votre client natif à accéder à VHAL. Exemple :

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