Vhal 启动流程

2025-09-03

HIDL

Vhal启动流程

hidl init

首先查看VehicleServicemain方法:

int main(int /* argc */, char* /* argv */ []) {
    //加载VehiclePropertyStore
    auto store = std::make_unique<VehiclePropertyStore>();
    //加载VehicleConnector
    auto connector = std::make_unique<XXXVehicleConnector>();
    //加载VehicleHal
    auto hal = std::make_unique<XXXVehicleHal>(store.get(), connector.get());
    //加载VehicalHalManager
    auto service = std::make_unique<VehicleHalManager>(hal.get());
    connector->setValuePool(hal->getValuePool());
    android::hardware::configureRpcThreadpool(4, true /* callerWillJoin */);
    ...
    android::status_t status = service->registerAsService();
    ...
}

,主要创建了四个类型的对象,分别是VehiclePropertyStoreVehicleConnectorVehicleHalvehicleHalManager,首先 VehicleService会创建未初始化的VehiclePropertyStoreVehicleConnector,然后通过这两个对象创建VehicleHal

创建VehicleHal

查看VehicleHal的构造函数:

XXXVehicleHal::XXXVehicleHal(VehiclePropertyStore *propStore, XXXVehicleConnector *connector)
    : mPropStore(propStore),
    mConnector(connector)
{
    initStaticConfig();
    for (size_t i = 0; i < arraysize(kVehicleProperties); i++)
    {
        mPropStore->registerProperty(kVehicleProperties[i].config);
    }
    mVehicleClient->registerPropertyValueCallback(std::bind(&EmulatedVehicleHal::onPropertyValue,
                                                            this, std::placeholders::_1,
                                                            std::placeholders::_2));
}

,主要执行了initStaicConfig方法,并向VehiclePropertyStore中写入config

默认VehiclePropConfig的加载

查看initStaticConfig的代码:

void EmulatedVehicleHal::initStaticConfig() {
    for (auto&& it = std::begin(kVehicleProperties); it != std::end(kVehicleProperties); ++it) {
        const auto& cfg = it->config;
        VehiclePropertyStore::TokenFunction tokenFunction = nullptr;

        switch (cfg.prop) {
            case OBD_FREEZE_FRAME: {
                tokenFunction = [](const VehiclePropValue& propValue) {
                    return propValue.timestamp;
                };
                break;
            }
            default:
                break;
        }

        mPropStore->registerProperty(cfg, tokenFunction);
    }
}

,该方法从kVehicleProperties中读取property的配置信息,向VehiclePropertyStore中注册配置信息。同样,在VehicleHal的构造函数中,也执行了操作:

for (size_t i = 0; i < arraysize(kVehicleProperties); i++)
{
    mPropStore->registerProperty(kVehicleProperties[i].config);
}

DefaultConfig.h中查看kVehicleProperties的定义:

const ConfigDeclaration kVehicleProperties[]{
    {.config =
            {
                    .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY),
                    .access = VehiclePropertyAccess::READ,
                    .changeMode = VehiclePropertyChangeMode::STATIC,
            },
    .initialValue = {.floatValues = {15000.0f}}},
    ...
};

,是ConfigDeclaration类型的数组,ConfigDeclaration的定义在DefaultConfig.h中:

struct ConfigDeclaration {
    VehiclePropConfig config;
    VehiclePropValue::RawValue initialValue;
    std::map<int32_t, VehiclePropValue::RawValue> initialAreaValues;
};

,包含了VehiclePropConfig、property的初始物理值和在每个area的对应初始值。

VehiclePropConfig的定义详见Vhal 结构 与 get/set 流程

查看VehiclePropertyStoreregisterProperty方法:

void VehiclePropertyStore::registerProperty(const VehiclePropConfig& config,
                                            VehiclePropertyStore::TokenFunction tokenFunc) {
    MuxGuard g(mLock);
    mConfigs.insert({ config.prop, RecordConfig { config, tokenFunc } });
}
  

,向mConfigs中插入了一个propRecordConfig的键值对,其中mConfigs是一个property id到RecordConfig的map:

struct RecordConfig {
    VehiclePropConfig propConfig;
    TokenFunction tokenFunction;
};

std::unordered_map<int32_t /* VehicleProperty */, RecordConfig> mConfigs;

启动VehicleHalManager

在完成VehicleHal的创建后,会进行VehicleHalManager的启动,这一步会创建VehicleHalManager,并对之前创建的一些对象进行初始化的操作。

首先查看VehicleHalManager的构造函数:

VehicleHalManager(VehicleHal* vehicleHal)
    : mHal(vehicleHal),
    //首先创建`SubscriptionManager`对象
    mSubscriptionManager(std::bind(&VehicleHalManager::onAllClientsUnsubscribed,
                                    this, std::placeholders::_1)) {
    //然后执行初始化方法
    init();
}

,这一步会先创建SubscriptionManager的对象用于管理subscribe/unsubscribe,然后执行初始化的操作。

创建SubscriptionManager

这一步主要是将SubscriptionManagermOnPropertyUnsubscribed方法设置为VehicleHalManageronAllClientsUnsubscribed方法,并设置mCallbackDeathRecipient onCallbackDead方法:

SubscriptionManager(const OnPropertyUnsubscribed& onPropertyUnsubscribed)
        : mOnPropertyUnsubscribed(onPropertyUnsubscribed),
            mCallbackDeathRecipient(new DeathRecipient(
                std::bind(&SubscriptionManager::onCallbackDead, this, std::placeholders::_1)))
{}

onAllClientUnsubscribed方法主要执行了VehicleHalunsubscribe方法:

void VehicleHalManager::onAllClientsUnsubscribed(int32_t propertyId) {
    mHal->unsubscribe(propertyId);
}

方法onCallBackDead在订阅属性的远程对象的binder死亡时,取消该远程对象对属性的订阅:

void SubscriptionManager::onCallbackDead(uint64_t cookie) {
    ...
    ClientId clientId = cookie;

    std::vector<int32_t> props;
    {
        MuxGuard g(mLock);
        //获取id对应的client对象
        const auto& it = mClients.find(clientId);
        if (it == mClients.end()) {
            return; 
        }
        const auto& halClient = it->second;
        //获取订阅的属性
        props = halClient->getSubscribedProperties();
    }
    //取消订阅
    for (int32_t propId : props) {
        unsubscribe(clientId, propId);
    }
}

初始化VehicleHalManager

在前面的步骤中,完成了所有对象的创建,在这一步,需要完成对创建的对象的初始化操作。

查看VehicleHalManager::init方法:

void VehicleHalManager::init() {
    ...

    //初始化VehicleHal
    mHal->init(&mValueObjectPool,
                std::bind(&VehicleHalManager::onHalEvent, this, _1),
                std::bind(&VehicleHalManager::onHalPropertySetError, this,
                        _1, _2, _3));

    ...
}

执行VehicleHalinit方法。

初始化VehicleHal

VehicleHalinit方法在VehicleHal.h中:

void init(
    VehiclePropValuePool* valueObjectPool,
    const HalEventFunction& onHalEvent,
    const HalErrorFunction& onHalError) {
    mValuePool = valueObjectPool;
    mOnHalEvent = onHalEvent;
    mOnHalPropertySetError = onHalError;

    onCreate();
}

,在这一步里,设置VehicleHalmValuePoolmOnHalEventmOnHalPropertySetErrorVehicleHalManagermValueObjectPoolonHalEventonHalPorpertySetError,第一个对象用来进行property对象的复用,第二个和第三个则是设置了两个回调函数。 进行了上述设置后,执行onCreate方法:

void EmulatedVehicleHal::onCreate() {
    static constexpr bool shouldUpdateStatus = true;

    for (auto& it : kVehicleProperties) {
        VehiclePropConfig cfg = it.config;
        int32_t numAreas = cfg.areaConfigs.size();
        ...
        for (int i = 0; i < numAreas; i++) {
            ...
            VehiclePropValue prop = {
                    .areaId = curArea,
                    .prop = cfg.prop, 
            };
            ...
            mPropStore->writeValue(prop, shouldUpdateStatus);
        }
    }
    ...
}

,在onCreate方法中,会从kVehicleProperties中读取所有的config,并根据其中的area执行VehiclePropertyStore::writeValue方法,这一步会将在kVehicleProperties中设定的属性值写入到VehiclePropertyStore中。

AIDL

aidl init

Vhal启动流程

首先查看VehicleService中定义的main方法:

int main(int /* argc */, char* /* argv */[]) {
    ...
    if (!ABinderProcess_setThreadPoolMaxThreadCount(4)) {
        ...
    }
    ABinderProcess_startThreadPool();

    //创建VehicleHardWare对象
    std::unique_ptr<XXXVehicleHardware> hardware = std::make_unique<XXXVehicleHardware>();
    //创建VehicleHal对象
    std::shared_ptr<XXXVehicleHal> vhal =
            ::ndk::SharedRefBase::make<XXXVehicleHal>(std::move(hardware));

    ...
    binder_exception_t err = AServiceManager_addService(
            vhal->asBinder().get(), "android.hardware.automotive.vehicle.IVehicle/default");
    ...

    ABinderProcess_joinThreadPool();
    ...

    return 0;
}

创建并初始化VehicleHardware

XXXVehicleHardware::XXXVehicleHardware()
    : XXXVehicleHardware(DEFAULT_CONFIG_DIR, OVERRIDE_CONFIG_DIR, false) {}

XXXVehicleHardware::XXXVehicleHardware(std::string defaultConfigDir,
                                        std::string overrideConfigDir, bool forceOverride)
    : mValuePool(std::make_unique<VehiclePropValuePool>()),
    mServerSidePropStore(new VehiclePropertyStore(mValuePool)),
    ...
    mRecurrentTimer(new RecurrentTimer()),
    ...
    mDefaultConfigDir(defaultConfigDir),
    mOverrideConfigDir(overrideConfigDir),
    mForceOverride(forceOverride) {
    init();
}

,这一步首先创建了一个VehiclePropValuePool,然后创建一个VehiclePropertyStore,然后,设置了mDefaultConfigDirmOverrideConfigDir,后续执行了init方法:

void XXXVehicleHardware::init() {
    //首先加载配置信息
    for (auto& [_, configDeclaration] : loadConfigDeclarations()) {
        VehiclePropConfig cfg = configDeclaration.config;
        VehiclePropertyStore::TokenFunction tokenFunction = nullptr;

        ...
        //将配置信息注册到`VehiclePropertyStore`中
        mServerSidePropStore->registerProperty(cfg, tokenFunction);

        ...

        //将config中的初始值写入
        storePropInitialValue(configDeclaration);
    }

    ...

    //设置VehiclePropertyStore在property值改变时的回调函数为XXXVehicleHardware::onValueChangeCallback
    mServerSidePropStore->setOnValueChangeCallback(
            [this](const VehiclePropValue& value) { return onValueChangeCallback(value); });
}

加载默认配置并设置VehiclePropertyStore

查看loadConfigDeclarations方法:

std::unordered_map<int32_t, ConfigDeclaration> XXXVehicleHardware::loadConfigDeclarations() {
    std::unordered_map<int32_t, ConfigDeclaration> configsByPropId;
    loadPropConfigsFromDir(mDefaultConfigDir, &configsByPropId);
    if (mForceOverride ||
        android::base::GetBoolProperty(OVERRIDE_PROPERTY, /*default_value=*/false)) {
        loadPropConfigsFromDir(mOverrideConfigDir, &configsByPropId);
    }
    return configsByPropId;
}

,主要调用了loadPropConfigsFromDir方法:

void XXXVehicleHardware::loadPropConfigsFromDir(
        const std::string& dirPath,
        std::unordered_map<int32_t, ConfigDeclaration>* configsByPropId) {
    ...
    if (auto dir = opendir(dirPath.c_str()); dir != NULL) {
        std::regex regJson(".*[.]json", std::regex::icase);
        while (auto f = readdir(dir)) {
            ...
            auto result = mLoader.loadPropConfig(filePath);
            ...
            for (auto& [propId, configDeclaration] : result.value()) {
                (*configsByPropId)[propId] = std::move(configDeclaration);
            }
        }
        closedir(dir);
    }
}

,这一步主要通过mLoader从json文件中加载配置信息,mLoaderJsonConfigLoader类的实例,查看其loadPropConfig方法:

android::base::Result<std::unordered_map<int32_t, ConfigDeclaration>>
JsonConfigLoader::loadPropConfig(std::istream& is) {
    return mParser->parseJsonConfig(is);
}

android::base::Result<std::unordered_map<int32_t, ConfigDeclaration>>
JsonConfigLoader::loadPropConfig(const std::string& configPath) {
    std::ifstream ifs(configPath.c_str());
    ...
    return loadPropConfig(ifs);
}

,调用了parseJsonConfig方法从json文件中解析配置信息,json文件的格式如下:

{
    "apiVersion": 1,
    "properties": [
        {
            // property id
            "property": "VehicleProperty::INFO_FUEL_CAPACITY",
            // (optional, number/string) property 访问模式
            "access": "VehiclePropertyAccess::READ",
            // (optional, number/string) propert change mode
            "changeMode": "VehiclePropertyChangeMode::STATIC",
            // (optional, string) config string.
            "configString": "blahblah",
            // (optional, array of number/string) config array.
            "configArray": [1, 2, "Constants::HVAC_ALL"],
            // (optional, object) property 的默认值
            "defaultValue": {
                // (optional, array of int number/string) Int values.
                "int32Values": [1, 2, "Constants::HVAC_ALL"],
                // (optional, array of int number/string) Long values.
                "int64Values": [1, 2],
                // (optional, array of float number/string) Float values.
                "floatValues": [1.1, 2.2],
                // (optional, string) String value.
                "stringValue": "test"
            },
            "minSampleRate": 1,
            "maxSampleRate": 10,
            // (optional, array of objects) 不同area的配置
            "areas:" [
                {
                    "areaId": "Constants::DOOR_1_LEFT",
                    // (optional number/string)
                    "minInt32Value": 1,
                    // (optional number/string)
                    "maxInt32Value": 10,
                    // (optional number/string)
                    "minInt64Value": 1,
                    // (optional number/string)
                    "maxInt64Value": 10,
                    // (optional number/string)
                    "minFloatValue": 1,
                    // (optional number/string)
                    "maxFloatValue": 10,
                    // (optional object)
                    "defaultValue": {
                        "int32Values": [1, 2, "Constants::HVAC_ALL"],
                        "int64Values": [1, 2],
                        "floatValues": [1.1, 2.2],
                        "stringValue": "test"
                    }
                }
            ]
        }
     ]
}

在执行完loadConfigDeclarations后,调用VehiclePropertyStore::registerProperty方法,该步与HIDL相同。然后,执行storePropInitialValue方法:

void FakeVehicleHardware::storePropInitialValue(const ConfigDeclaration& config) {
    const VehiclePropConfig& vehiclePropConfig = config.config;
    int propId = vehiclePropConfig.prop;
    bool globalProp = isGlobalProp(propId);
    size_t numAreas = globalProp ? 1 : vehiclePropConfig.areaConfigs.size();

    for (size_t i = 0; i < numAreas; i++) {
        int32_t curArea = globalProp ? 0 : vehiclePropConfig.areaConfigs[i].areaId;
        VehiclePropValue prop = {
                .areaId = curArea,
                .prop = propId,
                .timestamp = elapsedRealtimeNano(),
        };

        ...

        auto result =
                mServerSidePropStore->writeValue(mValuePool->obtain(prop), /*updateStatus=*/true);
        ...
    }
}

,这一步也与HIDL实现类似,向VehicleProeprtyStore中写入了config中配置的初始值。

创建VehicleHal

XXXVehicleHal::XXXVehicleHal(std::unique_ptr<IVehicleHardware> vehicleHardware)
    : mVehicleHardware(std::move(vehicleHardware)),
    mPendingRequestPool(std::make_shared<PendingRequestPool>(TIMEOUT_IN_NANO)) {
    ...
    IVehicleHardware* vehicleHardwarePtr = mVehicleHardware.get();

    //创建SubscriptionManager
    mSubscriptionManager = std::make_shared<SubscriptionManager>(vehicleHardwarePtr);

    //绑定回调函数
    std::weak_ptr<SubscriptionManager> subscriptionManagerCopy = mSubscriptionManager;
    mVehicleHardware->registerOnPropertyChangeEvent(
            std::make_unique<IVehicleHardware::PropertyChangeCallback>(
                    [subscriptionManagerCopy](std::vector<VehiclePropValue> updatedValues) {
                        onPropertyChangeEvent(subscriptionManagerCopy, updatedValues);
                    }));
    mVehicleHardware->registerOnPropertySetErrorEvent(
            std::make_unique<IVehicleHardware::PropertySetErrorCallback>(
                    [subscriptionManagerCopy](std::vector<SetValueErrorEvent> errorEvents) {
                        onPropertySetErrorEvent(subscriptionManagerCopy, errorEvents);
                    }));

    ...
}

,在该步中,首先创建了SubscriptionManager,然后将onPropertyChangeEventonPropertySetErrorEvent方法注册到VehicleHardware上。

属性配置的加载

在AIDL实现中,将属性的初始配置加载从读取DefaultConfig.h头文件转移到了读取XXXpackages.json文件。

JsonConfigLoader加载json文件

查看JsonConfigParserparseJsonConfig方法:

Result<std::unordered_map<int32_t, ConfigDeclaration>> JsonConfigParser::parseJsonConfig(
        std::istream& is) {
    ...
    std::unordered_map<int32_t, ConfigDeclaration> configsByPropId;
    ...
    Json::Value properties = root["properties"];
    ...
    for (unsigned int i = 0; i < properties.size(); i++) {
        if (auto maybeConfig = parseEachProperty(properties[i], &errors); maybeConfig.has_value()) {
            configsByPropId[maybeConfig.value().config.prop] = std::move(maybeConfig.value());
        }
    }
    ...
    return configsByPropId;
}

,查看parseEachProperty方法:

std::optional<ConfigDeclaration> JsonConfigParser::parseEachProperty(
        const Json::Value& propJsonValue, std::vector<std::string>* errors) {
    ...
    int32_t propId;

    if (!tryParseJsonValueToVariable(propJsonValue, "property", /*optional=*/false, &propId,
                                    errors)) {
        return std::nullopt;
    }

    configDecl.config.prop = propId;
    std::string propStr = propJsonValue["property"].toStyledString();

    //解析access
    parseAccessChangeMode(propJsonValue, "access", propId, propStr, AccessForVehicleProperty,
                        &configDecl.config.access, errors);

    //解析changeMode
    parseAccessChangeMode(propJsonValue, "changeMode", propId, propStr,
                        ChangeModeForVehicleProperty, &configDecl.config.changeMode, errors);
    ...


    tryParseJsonValueToVariable(propJsonValue, "configString", /*optional=*/true,
                                &configDecl.config.configString, errors);

    tryParseJsonArrayToVariable(propJsonValue, "configArray", /*optional=*/true,
                                &configDecl.config.configArray, errors);

    //解析默认值
    parsePropValues(propJsonValue, "defaultValue", &configDecl.initialValue, errors);

    tryParseJsonValueToVariable(propJsonValue, "minSampleRate", /*optional=*/true,
                                &configDecl.config.minSampleRate, errors);

    tryParseJsonValueToVariable(propJsonValue, "maxSampleRate", /*optional=*/true,
                                &configDecl.config.maxSampleRate, errors);

    parseAreas(propJsonValue, "areas", &configDecl, errors);
    ...
    return configDecl;
}

,这一步会按照json文件中的每个property的配置按照顺序解析json键值对。重点关注parseAccessChangeMode方法和parsePropValues函数。

解析 access / change mode

首先查看parseAccessChangeMode方法:

void JsonConfigParser::parseAccessChangeMode(
        const Json::Value& parentJsonNode, const std::string& fieldName, int propId,
        const std::string& propStr, const std::unordered_map<VehicleProperty, T>& defaultMap,
        T* outPtr, std::vector<std::string>* errors) {
    ...
    if (parentJsonNode.isMember(fieldName)) {
        //从Json文件中加载access/change mode
        auto result = mValueParser.parseValue<int32_t>(fieldName, parentJsonNode[fieldName]);
        ...
        *outPtr = static_cast<T>(result.value());
        return;
    }
    //从defaultMap中加载access/change mode
    auto it = defaultMap.find(static_cast<VehicleProperty>(propId));
    ...
    *outPtr = it->second;
    return;
}

,这一步会从Json文件中加载access/change mode,如果Json文件中没有定义access/change mode的话,则会从传入的defaultMap中加载access/ change mode。 观察AccessForVehiclePropertyChangeModeForVehicleProperty所在的文件,在文件中可以看到下面一段注释:

// AccessForVehicleProperty.h

/**
* DO NOT EDIT MANUALLY!!!
*
* Generated by tools/generate_annotation_enums.py.
*/

,查看/hardware/interfaces/automotive/vehicle/tools/generate_annotation_enums.py

...
RE_COMMENT_BEGIN = re.compile("\s*\/\*\*?")
RE_COMMENT_END = re.compile("\s*\*\/")
RE_CHANGE_MODE = re.compile("\s*\* @change_mode (\S+)\s*")
RE_ACCESS = re.compile("\s*\* @access (\S+)\s*")
RE_VALUE = re.compile("\s*(\w+)\s*=(.*)")
...
class Converter:

    def __init__(self, name, annotation_re):
        self.name = name
        self.annotation_re = annotation_re

    def convert(self, input, output, header, footer, cpp):
        processing = False
        in_comment = False
        content = LICENSE + header
        annotation = None
        id = 0
        with open(input, 'r') as f:
            for line in f.readlines():
                ...
                # 注释开始 /**
                if RE_COMMENT_BEGIN.match(line):
                    in_comment = True
                # 注释结束 */
                if RE_COMMENT_END.match(line):
                    in_comment = False
                if in_comment:
                    # 在注释中匹配 @change_mode XXX 或 @access XXX
                    match = self.annotation_re.match(line)
                    # 解析其第二个字段 XXX
                    if match:
                        annotation = match.group(1)
                else:
                    # 注释结束,匹配属性名
                    match = RE_VALUE.match(line)
                    if match:
                        prop_name = match.group(1)
                        ...
                        # 生成代码
                        if cpp:
                            annotation = annotation.replace(".", "::")
                            content += (TAB + TAB + "{VehicleProperty::" + prop_name + ", " +
                                        annotation + "},")
                        else:
                            content += (TAB + TAB + "Map.entry(VehicleProperty." + prop_name + ", " +
                                        annotation + "),")
                        id += 1

        ...

,这段脚本的作用是从VehicleProperty.aidl文件中解析注释并生成C++或Java代码。给定一个属性如:

/**
 * VIN of vehicle
 *
 * @change_mode VehiclePropertyChangeMode.STATIC
 * @access VehiclePropertyAccess.READ
 */
INFO_VIN = 0x0100 + 0x10000000 + 0x01000000
        + 0x00100000, // VehiclePropertyGroup:SYSTEM,VehicleArea:GLOBAL,VehiclePropertyType:STRING

,则会在ChangeModeForVehiclePropertyAccessForVehicleProperty中生成条目:

//change mode
std::unordered_map<VehicleProperty, VehiclePropertyChangeMode> ChangeModeForVehicleProperty = {
    {VehicleProperty::INFO_VIN, VehiclePropertyChangeMode::STATIC},
    ...
}

//access
std::unordered_map<VehicleProperty, VehiclePropertyAccess> AccessForVehicleProperty = {
    {VehicleProperty::INFO_VIN, VehiclePropertyAccess::READ},
    ...
}
//change mode
public final class AccessForVehicleProperty { 
    public static final Map<Integer, Integer> values = Map.ofEntries(
        Map.entry(VehicleProperty.INFO_VIN, VehiclePropertyAccess.READ),
        ...
    )
}

//access
public final class ChangeModeForVehicleProperty {
    public static final Map<Integer, Integer> values = Map.ofEntries(
        Map.entry(VehicleProperty.INFO_VIN, VehiclePropertyChangeMode.STATIC),
        ...
    )
}

注意到,在生成代码时:

content += (TAB + TAB + "{VehicleProperty::" + prop_name + ", " +
            annotation + "},")

,此部分VehicleProperty是硬编码在脚本中的,同样的,对于方法parseAccessChangeMode

void JsonConfigParser::parseAccessChangeMode(
        const Json::Value& parentJsonNode, const std::string& fieldName, int propId,
        const std::string& propStr, const std::unordered_map<VehicleProperty, T>& defaultMap,
        T* outPtr, std::vector<std::string>* errors) 

VehicleProperty也是硬编码在代码中的,如果需要实现自定义的XXXVehicleProperty,则需要重写相应的内容。 ***

解析default values

查看parsePropValues方法:

bool JsonConfigParser::parsePropValues(const Json::Value& parentJsonNode,
                                        const std::string& fieldName, RawPropValues* outPtr,
                                        std::vector<std::string>* errors) {
    ...
    success &= tryParseJsonArrayToVariable(jsonValue, "int32Values",
                                            /*optional=*/true, &(outPtr->int32Values), errors);
    success &= tryParseJsonArrayToVariable(jsonValue, "floatValues",
                                            /*optional=*/true, &(outPtr->floatValues), errors);
    success &= tryParseJsonArrayToVariable(jsonValue, "int64Values",
                                            /*optional=*/true, &(outPtr->int64Values), errors);
    success &= tryParseJsonValueToVariable(jsonValue, "stringValue",
                                            /*optional=*/true, &(outPtr->stringValue), errors);
    ...
}

,调用了tryParseJsonArrayToVariable方法:

bool JsonConfigParser::tryParseJsonArrayToVariable(const Json::Value& parentJsonNode,
                                                    const std::string& fieldName,
                                                    bool fieldIsOptional, std::vector<T>* outPtr,
                                                    std::vector<std::string>* errors) {
    ...
    auto result = mValueParser.parseArray<T>(fieldName, parentJsonNode[fieldName]);
    ...
    *outPtr = std::move(result.value());
    return true;
}

,调用了JsonValueParser::parseArray方法:

template <class T>
Result<std::vector<T>> JsonValueParser::parseArray(const std::string& fieldName,
                                                    const Json::Value& value) const {
    ...
    std::vector<T> parsedValues;
    for (unsigned int i = 0; i < value.size(); i++) {
        auto result = parseValue<T>(fieldName, value[i]);
        ...
        parsedValues.push_back(result.value());
    }
    return std::move(parsedValues);
}

,调用了parseValue方法:

template <class T>
Result<T> JsonValueParser::parseValue(const std::string& fieldName,
                                    const Json::Value& value) const {
    //如果value不是字符串格式的
    if (!value.isString()) {
        return convertValueToType<T>(fieldName, value);
    }
    //如果value是字符串格式的
    auto maybeTypeAndValue = maybeGetTypeAndValueName(value.asString());
    ...
    auto constantParseResult = parseConstantValue(maybeTypeAndValue.value());
    ...
    int constantValue = constantParseResult.value();
    return static_cast<T>(constantValue);
}

,上述方法在value不是字符串格式时直接解析为值,如果value不是字符串格式时,首先会调用maybeGetTypeAndValueName方法:

//JsonConfigLoader.h
inline static const std::string DELIMITER = "::";

//JsonConfigLoader.cpp
std::optional<std::pair<std::string, std::string>> JsonValueParser::maybeGetTypeAndValueName(
        const std::string& jsonFieldValue) const {
    size_t pos = jsonFieldValue.find(DELIMITER);
    ...
    std::string type = jsonFieldValue.substr(0, pos);
    std::string valueName = jsonFieldValue.substr(pos + DELIMITER.length(), std::string::npos);
    ...
    return std::make_pair(type, valueName);
}

,这一步回去解析Json文件中类似"FuelType::FUEL_TYPE_UNLEADED"的声明,将其拆分为两部分:type"FuelType")和valueName"FUEL_TYPE_UNLEADED")。在执行完maybeGetTypeAndValueName后,会调用parseConstantValue

Result<int> JsonValueParser::parseConstantValue(
        const std::pair<std::string, std::string>& typeValueName) const {
    const std::string& type = typeValueName.first;
    const std::string& valueName = typeValueName.second;
    auto it = mConstantParsersByType.find(type);
    ...
    auto result = it->second->parseValue(valueName);
    ...
    return result;
}

,这一步会在mConstantParsersByType中找到与type对应的ConstantParser对象,mConstantParsersByTypeJsonValueParser的构造函数中被填充:

JsonValueParser::JsonValueParser() {
    ...
    mConstantParsersByType["VehicleProperty"] = std::make_unique<ConstantParser<VehicleProperty>>();
    ...
}

ConstantParser的定义如下:

template <class T>
class ConstantParser final : public ConstantParserInterface {
public:
    ConstantParser() {
        for (const T& v : ndk::enum_range<T>()) {
            std::string name = aidl::android::hardware::automotive::vehicle::toString(v);
            ...
            mValueByName[name] = toInt(v);
        }
    }

    ~ConstantParser() = default;

    Result<int> parseValue(const std::string& name) const override {
        auto it = mValueByName.find(name);
        ...
        return it->second;
    }

private:
    std::unordered_map<std::string, int> mValueByName;
};

,其parseValue方法会从mValueByName中找到name对应的值,而构造ConstantParser时添加。


注意到,在ConstantParser中,使用了aidl::android::hardware::automotive::vehicle::toString,对于自定义的XXXVehicleProperty,是无法解析的,需要重写该类。 ***