VHAL obsługuje klientów natywne i w Javie. Car Service to jedyny klient Java dla VHAL. W przypadku aplikacji samochodowych używaj interfejsów Car API (np. CarPropertyManager
), aby uzyskiwać dostęp do właściwości VHAL, zamiast komunikować się bezpośrednio z VHAL. SELinux blokuje bezpośredni dostęp. Szczegółowe informacje znajdziesz w dokumentacji interfejsu Car API na stronie Indeks pakietów.
W przypadku klientów natywnych, począwszy od Androida 13, użyj libvhalclient
zamiast łączyć się bezpośrednio z VHAL. Jest to biblioteka klienta, która udostępnia jeden wspólny interfejs IVhalClient.h
do implementacji AIDL i HIDL VHAL. Ten przykład pokazuje, jak utworzyć natywny klient VHAL i użyć go do uzyskania numeru identyfikacji pojazdu (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; }
Musisz skonfigurować zasady SELinux, aby umożliwić klientowi natywnemu dostęp do VHAL. Na przykład:
# 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)