O VHAL oferece suporte a clientes Java e nativos. O Car Service é o único
cliente Java para VHAL. Para apps de carro, use as APIs Car (por exemplo,
CarPropertyManager
)
para acessar propriedades VHAL em vez de se comunicar diretamente com
o VHAL. Na verdade, o SELinux bloqueia o acesso direto. Para mais detalhes, consulte a documentação da API Car no
índice de pacotes.
Para clientes nativos, a partir do Android 13, use
libvhalclient
em vez de se conectar diretamente
com a VHAL. Essa é uma biblioteca de cliente que expõe uma interface comum,
IVhalClient.h
para implementações de AIDL e HIDL VHAL. O exemplo a seguir mostra
como criar um cliente nativo do VHAL e usá-lo para receber um número de identificação do veí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; }
É necessário configurar a política do SELinux para permitir que o cliente nativo acesse o VHAL. Exemplo:
# 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)