车载技术开发—{Android CarFrameWork}

Android Automotive平台

Android Automotive是通过Android的通用框架,语言和API来实现的一个全栈,开源,高度可定制的平台。

Android Automotive与整个Android生态系统的关系

  • Android Automotive是Android的一部分。 Android Automotive不是Android的分支或并行开发,它与手机,平板电脑等安卓设备上的Android具有相同的代码库,并且位于相同的存储库中。它基于经过10多年开发的强大平台和功能集,可利用现有的安全模型,兼容性程序,开发人员工具和基础架构,同时继续具有高度可定制性和可移植性,完全免费和开源的特点。
  • Android Automotive扩展了Android 。在将Android打造为功能齐全的信息娱乐平台的过程中,我们添加了对汽车特定要求,功能和技术的支持。就像今天用于移动设备的Android一样,Android Automotive将是一个完整的汽车信息娱乐平台。

Android Automotive架构

Automotive

Android Automative是在原先Android的系统架构上增加了一些与车相关的(图中虚线框中绿色背景的)模块。

  • Car App :包括OEM和第三方开发的App
  • Car API :提供给汽车App特有的接口
  • Car Service :系统中与车相关的服务,主要是基于CarProperty实现Vechile相关的一些策略
  • Vehicle Network Service :汽车的网络服务
  • Vehicle HAL :汽车的硬件抽象层描述,定义 OEM 可以实现的车辆属性的接口

Android CarAPI

CarAPI

· annotation:包含了两个注解。 · app · menu:车辆应用菜单相关API。 · cluster:仪表盘相关API。 · render:渲染相关API。 · content · pm:应用包相关API。 · diagnostic:包含与汽车诊断相关的API。 · hardware:车辆硬件相关API。 · cabin:座舱相关API。 · hvac:通风空调相关API。 · property:属性相关API(实现定制的property)。 · radio:收音机相关API。 · input:输入相关API。 · media:多媒体相关API。 · navigation:导航相关API。 · settings:设置相关API。 · vms:汽车监测相关API

这些api集合中,我们可以通过CarpropertyManager去实现定制的property功能,简要类图:

CarpropertyManager内部方法

  • 通过registerListener注册自定义的propertyId以及property变更通知的callback

  • 解注册CarpropertyEventListener

  • 获取所有property, 返回一个元素类型是CarpropertyConfig的list

  • 获取property状态

  • 获取property value

  • 设置property value

CarpropertyManager与service层交互的AIDL接口

需要注意的是ICarProperty是同步接口,ICarPropertyEventListener是异步接口。

onEvent传上来的是参数是CarPropertyEvent的list,CarPropertyEvent中包含event type与CarPropertyValue; eventType包含PROPERTY_EVENT_PROPERTY_CHANGE与PROPERTY_EVENT_ERROR,分别对应listener中的onPropertyChanged和PROPERTY_EVENT_ERROR, CarPropertyValue则包含具体的propId、propValue等具体属性信息。

Android CarService

代码目录: packages/services/Car/service
  • CarService并非一个服务,而是一系列的服务。这些服务都在ICarImpl.java构造函数中列了出来

CarService启动流程

启动CarServiceHelperService

SystemServer启动过程中,在ActivityManagerService systemReady时,启动CarServiceHelperService。 代码:

android/frameworks/base/services/java/com/android/server/SystemServer.java
​
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
    t.traceBegin("StartCarServiceHelperService");
    final SystemService cshs = mSystemServiceManager
            .startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
    if (cshs instanceof Dumpable) {
        mDumper.addDumpable((Dumpable) cshs);
    }
    if (cshs instanceof DevicePolicySafetyChecker) {
        dpms.setDevicePolicySafetyChecker((DevicePolicySafetyChecker) cshs);
    }
    t.traceEnd();
}
​
private static final String CAR_SERVICE_HELPER_SERVICE_CLASS =
        "com.android.internal.car.CarServiceHelperService";

启动CarService

CarServiceHelperService在onStart时拉起CarServcie,加载Native库car-framework-service-jni。

代码:

android/frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java
​
public void onStart() {
    EventLog.writeEvent(EventLogTags.CAR_HELPER_START);
​
    IntentFilter filter = new IntentFilter(Intent.ACTION_REBOOT);
    filter.addAction(Intent.ACTION_SHUTDOWN);
    mContext.registerReceiverForAllUsers(mShutdownEventReceiver, filter, null, null);
    mCarWatchdogDaemonHelper.addOnConnectionChangeListener(mConnectionListener);
    mCarWatchdogDaemonHelper.connect();
    Intent intent = new Intent();
    intent.setPackage("com.android.car");
    intent.setAction(CAR_SERVICE_INTERFACE);
    if (!mContext.bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
            mHandler, UserHandle.SYSTEM)) {
        Slogf.wtf(TAG, "cannot start car service");
    }
    loadNativeLibrary();
}
​
void loadNativeLibrary() {
    System.loadLibrary("car-framework-service-jni");
}
​

代码:

android/packages/services/Car/car-lib/src/com/android/car/internal/common/CommonConstants.java
​
public static final String CAR_SERVICE_INTERFACE = "android.car.ICar";

代码:

android/packages/services/Car/service/AndroidManifest.xml
​
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
     package="com.android.car"
     coreApp="true"
     android:sharedUserId="android.uid.system">
​
    <application android:label="@string/app_title"
         android:directBootAware="true"
         android:allowBackup="false"
         android:persistent="true">
    
        <service android:name=".CarService"
             android:singleUser="true"
             android:exported="true">
            <intent-filter>
                <action android:name="android.car.ICar"/>
            </intent-filter>
        </service>

初始化ICarImpl

CarService在onCreate时先判断Vehicle HAL起来了,然后再初始化ICarImpl。 代码:

android/packages/services/Car/service/src/com/android/car/CarService.java
​
public void onCreate() {
    LimitedTimingsTraceLog initTiming = new LimitedTimingsTraceLog(CAR_SERVICE_INIT_TIMING_TAG,
            Trace.TRACE_TAG_SYSTEM_SERVER, CAR_SERVICE_INIT_TIMING_MIN_DURATION_MS);
    initTiming.traceBegin("CarService.onCreate");
​
    initTiming.traceBegin("getVehicle");
    mVehicle = getVehicle();
    initTiming.traceEnd();
    
    EventLog.writeEvent(EventLogTags.CAR_SERVICE_CREATE, mVehicle == null ? 0 : 1);
    
    if (mVehicle == null) {
        throw new IllegalStateException("Vehicle HAL service is not available.");
    }
    try {
        mVehicleInterfaceName = mVehicle.interfaceDescriptor();
    } catch (RemoteException e) {
        throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e);
    }
    
    Slog.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);
    EventLog.writeEvent(EventLogTags.CAR_SERVICE_CONNECTED, mVehicleInterfaceName);
    
    mICarImpl = new ICarImpl(this,
            mVehicle,
            SystemInterface.Builder.defaultSystemInterface(this).build(),
            mVehicleInterfaceName);
    mICarImpl.init();
    
    linkToDeath(mVehicle, mVehicleDeathRecipient);
    
    ServiceManager.addService("car_service", mICarImpl);
    SystemProperties.set("boot.car_service_created", "1");
    
    super.onCreate();
    
    initTiming.traceEnd(); // "CarService.onCreate"
}
​
private static IVehicle getVehicle() {
    final String instanceName = SystemProperties.get("ro.vehicle.hal", "default");
​
    try {
        return android.hardware.automotive.vehicle.V2_0.IVehicle.getService(instanceName);
    } catch (RemoteException e) {
        Slog.e(CarLog.TAG_SERVICE, "Failed to get IVehicle/" + instanceName + " service", e);
    } catch (NoSuchElementException e) {
        Slog.e(CarLog.TAG_SERVICE, "IVehicle/" + instanceName + " service not registered yet");
    }
    return null;
}
​

代码:

android/packages/services/Car/car-lib/src/android/car/ICar.aidl
android/packages/services/Car/service/src/com/android/car/ICarImpl.java

初始化各个Car Services组件

ICarImpl构造函数中,构造了下面几个实现了CarServiceBase接口的Car Services组件,以及VehicleHal和CarStatsService;然后通过init函数进行初始化。

CarFeatureController
CarPropertyService
CarDrivingStateService
CarUxRestrictionsManagerService
CarUserService
CarOccupantZoneService
SystemActivityMonitoringService
CarPowerManagementService
CarUserNoticeService
CarPackageManagerService
PerUserCarServiceHelper
CarBluetoothService
CarInputService
CarProjectionService
GarageModeService
AppFocusService
CarAudioService
CarNightService
FixedActivityService
ClusterNavigationService
InstrumentClusterService
VmsBrokerService
CarDiagnosticService
CarStorageMonitoringService
CarLocationService
CarMediaService
CarBugreportManagerService
CarExperimentalFeatureServiceController
CarWatchdogService
CarDevicePolicyService
ClusterHomeService
CarEvsService
CarTelemetryService
CarActivityService

初始化各个Hal Services组件

VehicleHal构造函数中,构造了下面几个继承自HalServiceBase基类的Hal Services组件以及HalClient;然后通过init函数进行初始化。

PowerHalService
PropertyHalService
InputHalService
VmsHalService
UserHalService
DiagnosticHalService
ClusterHalService
EvsHalService
TimeHalService

代码:

android/packages/services/Car/service/src/com/android/car/hal/VehicleHal.java

VehicleHAL目录结构

下面是vehicle 2.0参考实现的目录结构。

android/hardware/interfaces/automotive/vehicle/2.0/
|-- default // 默认实现
    |-- common
        |-- include
            |-- vhal_v2_0 // .h
        |-- src // .cpp
    |-- impl
        |-- vhal_v2_0 // impl代码
            |-- proto // proto协议
            |-- tests
    |-- tests
    |-- Android.bp // 编译文件
    |-- VehicleService.cpp // main函数
    |-- [email protected] // init rc
    |-- [email protected] // matrix xml
|-- utils
|-- vts
|-- Android.bp // 编译文件
|-- IVehicle.hal // interface
|-- IVehicleCallback.hal // callback
|-- types.hal // types

Hidl代码有个明显的namespace规则,如下。

namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {
namespace V2_0 {
// ...
}  // namespace V2_0
}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android

VehicleHAL启动流程

rc

VehicleHAL由init rc启动。 代码:

android/hardware/interfaces/automotive/vehicle/2.0/default/[email protected]
​
service vendor.vehicle-hal-2.0 /vendor/bin/hw/[email protected]
    class early_hal
    user vehicle_network
    group system inet

main

下面是VehicleHAL的main函数。 代码:

android/hardware/interfaces/automotive/vehicle/2.0/default/VehicleService.cpp
​
int main(int /* argc */, char* /* argv */ []) {
    auto store = std::make_unique<VehiclePropertyStore>();
    auto connector = std::make_unique<impl::EmulatedVehicleConnector>();
    auto userHal = connector->getEmulatedUserHal();
    auto hal = std::make_unique<impl::EmulatedVehicleHal>(store.get(), connector.get(), userHal);
    auto emulator = std::make_unique<impl::VehicleEmulator>(hal.get());
    auto service = std::make_unique<VehicleHalManager>(hal.get());
    connector->setValuePool(hal->getValuePool());
​
    configureRpcThreadpool(4, true /* callerWillJoin */);
    
    ALOGI("Registering as service...");
    status_t status = service->registerAsService();
    
    if (status != OK) {
        ALOGE("Unable to register vehicle service (%d)", status);
        return 1;
    }
    
    ALOGI("Ready");
    joinRpcThreadpool();
    
    return 1;
}
  • VehiclePropertyStore:存储、访问和修改vehicle相关的配置及属性,VehiclePropertyValues存储在有序map中。使用了同步锁,线程安全。
  • EmulatedVehicleConnector:VehicleHalClient与VehicleHalServer通信的连接器。
  • EmulatedUserHal:User Hal。
  • EmulatedVehicleHal:Vehicle Hal。
  • VehicleEmulator:Vehicle Emulator。
  • VehicleHalManager:This class is a thick proxy between IVehicle HIDL interface and vendor’s implementation.

以上为车机开发技术中的CarFrameWork学习;启动流程及原理介绍,更多的车载技术可以前往《车机开发技术手册》查看更多车载技术。

交互

CarService向上与App通过CarLib(aidl)交互,向下与VehicleHAL通过hidl交互,其中包括了一个很重要的概念:Property。 Property可以是静态的,也可以是变化后向上通报的,还可以是以一定的频率实时上报的。 下面以HVAC为例,说明HVAC的控制过程。

  • 【App】调用CarLib中【CarHvacManager】的setBooleanProperty设置Property。
  • 【CarHvacManager】调用CarLib中【CarPropertyManager】的setBooleanProperty。
  • 【CarPropertyManager】将Property封装成CarPropertyValue后调用CarServcie中【ICarProperty-CarPropertyService】的setProperty。(aidl) 【CarPropertyService】调用CarServcie中【PropertyHalService】的setProperty。
  • 【PropertyHalService】将CarPropertyValue封装成VehiclePropValue后调用CarServcie中【VehicleHal】的set。
  • 【VehicleHal】调用CarServcie中【HalClient】的setValue。
  • 【HalClient】调用VehicleHAL中【IVehicle-VehicleHalManager】的set。(hidl)
  • 【VehicleHalManager】,首先,通过订阅这个Property的【Native HalClient】的callback即【IVehicleCallback-HalClient$VehicleCallback】的onPropertySet通知Property变更。(hidl callback)
  • 【VehicleHalManager】,然后,调用【EmulatedVehicleHal】的set。
  • 【EmulatedVehicleHal】检查Property后调用【VehicleHalClient】的setProperty。
  • 【VehicleHalClient】与EmulatedVehicleConnector有关,调用【EmulatedVehicleConnector】的onSetProperty。
  • 【EmulatedVehicleConnector】与VehicleHalServer有关,调用【VehicleHalServer】的onSetProperty。

最终,将Property或转换后的信号发到【Car ECU】。vehicle 2.0模拟实现是将Property存储到VehiclePropertyStore

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>