Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03cd34e2 authored by Yu Shan's avatar Yu Shan
Browse files

Create JsonConfigLoader.

This CL adds a JsonConfigLoader to parse the vehicle proeprty
config file in JSON format.

This CL also adds DefaultConfigTest to verify the JSON file
is compatible with the existing header file.

Test: atest JsonConfigLoaderUnitTest
atest VehicleHalDefaultConfigTest
Bug: 238685398

Change-Id: If679afd2245cb7e2a49fd964f343b660c9bcea25
parent 54744342
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -6,6 +6,15 @@
    {
      "name": "VehicleHalDefaultConfigTest"
    },
    {
      "name": "VehicleHalDefaultConfigTestEnableTestProperties"
    },
    {
      "name": "JsonConfigLoaderUnitTest"
    },
    {
      "name": "JsonConfigLoaderUnitTestEnableTestProperties"
    },
    {
      "name": "VehicleHalVehicleUtilsTest"
    },
+8 −2
Original line number Diff line number Diff line
@@ -25,7 +25,13 @@ cc_library_headers {
    export_include_dirs: ["include"],
    defaults: ["VehicleHalDefaults"],
    static_libs: ["VehicleHalUtils"],
    header_libs: ["VehicleHalTestUtilHeaders"],
    header_libs: [
        "VehicleHalJsonConfigLoaderHeaders",
        "VehicleHalTestUtilHeaders",
    ],
    export_static_lib_headers: ["VehicleHalUtils"],
    export_header_lib_headers: ["VehicleHalTestUtilHeaders"],
    export_header_lib_headers: [
        "VehicleHalTestUtilHeaders",
        "VehicleHalJsonConfigLoaderHeaders",
    ],
}
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

cc_library {
    name: "VehicleHalJsonConfigLoader",
    vendor: true,
    srcs: ["src/*.cpp"],
    local_include_dirs: ["include"],
    export_include_dirs: ["include"],
    defaults: ["VehicleHalDefaults"],
    static_libs: ["VehicleHalUtils"],
    header_libs: [
        "IVehicleGeneratedHeaders",
    ],
    shared_libs: ["libjsoncpp"],
}

cc_library {
    name: "VehicleHalJsonConfigLoaderEnableTestProperties",
    vendor: true,
    srcs: ["src/*.cpp"],
    local_include_dirs: ["include"],
    export_include_dirs: ["include"],
    defaults: ["VehicleHalDefaults"],
    static_libs: ["VehicleHalUtils"],
    header_libs: [
        "VehicleHalTestUtilHeaders",
        "IVehicleGeneratedHeaders",
    ],
    cflags: ["-DENABLE_VEHICLE_HAL_TEST_PROPERTIES"],
    shared_libs: ["libjsoncpp"],
}

cc_library_headers {
    name: "VehicleHalJsonConfigLoaderHeaders",
    vendor: true,
    local_include_dirs: ["include"],
    export_include_dirs: ["include"],
    defaults: ["VehicleHalDefaults"],
    static_libs: ["VehicleHalUtils"],
    header_libs: [
        "IVehicleGeneratedHeaders",
    ],
    shared_libs: ["libjsoncpp"],
}
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef android_hardware_automotive_vehicle_aidl_impl_default_config_JsonConfigLoader_include_ConfigDeclaration_H_
#define android_hardware_automotive_vehicle_aidl_impl_default_config_JsonConfigLoader_include_ConfigDeclaration_H_

#include <VehicleHalTypes.h>

#include <unordered_map>

namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {

// ConfigDeclaration represents one property config, its optional initial value and its optional
// area configs and initial values for each area.
struct ConfigDeclaration {
    aidl::android::hardware::automotive::vehicle::VehiclePropConfig config;

    // This value will be used as an initial value for the property. If this field is specified for
    // property that supports multiple areas then it will be used for all areas unless particular
    // area is overridden in initialAreaValue field.
    aidl::android::hardware::automotive::vehicle::RawPropValues initialValue;
    // Use initialAreaValues if it is necessary to specify different values per each area.
    std::unordered_map<int32_t, aidl::android::hardware::automotive::vehicle::RawPropValues>
            initialAreaValues;

    inline bool operator==(const ConfigDeclaration& other) const {
        return (config == other.config && initialValue == other.initialValue &&
                initialAreaValues == other.initialAreaValues);
    }

    friend std::ostream& operator<<(std::ostream& os, const ConfigDeclaration& c) {
        return os << "Config Declaration for property: "
                  << aidl::android::hardware::automotive::vehicle::toString(
                             static_cast<
                                     aidl::android::hardware::automotive::vehicle::VehicleProperty>(
                                     c.config.prop));
    }
};

}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_automotive_vehicle_aidl_impl_default_config_JsonConfigLoader_include_ConfigDeclaration_H_
+132 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef android_hardware_automotive_vehicle_aidl_impl_default_config_JsonConfigLoader_include_JsonConfigLoader_H_
#define android_hardware_automotive_vehicle_aidl_impl_default_config_JsonConfigLoader_include_JsonConfigLoader_H_

#include <ConfigDeclaration.h>
#include <VehicleHalTypes.h>

#include <android-base/result.h>
#include <json/json.h>
#include <unordered_map>
#include <utility>
#include <vector>

namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {

// private namespace
namespace jsonconfigloader_impl {

// A class to parse a value field in JSON config file.
// If the field is a string and the field is in the format of "XX::XX", the value will be parsed
// as a constant value in the format of "TYPE::NAME". Otherwise, the field will be return as is
// converted to the expected type.
class JsonValueParser final {
  public:
    JsonValueParser();

    android::base::Result<std::string> parseStringValue(const std::string& fieldName,
                                                        const Json::Value& value) const;

    template <class T>
    android::base::Result<std::vector<T>> parseArray(const std::string& fieldName,
                                                     const Json::Value& value) const;

    template <class T>
    android::base::Result<T> parseValue(const std::string& fieldName,
                                        const Json::Value& value) const;

  private:
    template <class T>
    static android::base::Result<T> convertValueToType(const std::string& fieldName,
                                                       const Json::Value& value);

    std::optional<std::pair<std::string, std::string>> maybeGetTypeAndValueName(
            const std::string& jsonFieldValue) const;
};

// The main class to parse a VHAL config file in JSON format.
class JsonConfigParser {
  public:
    android::base::Result<std::vector<ConfigDeclaration>> parseJsonConfig(std::istream& is);

  private:
    JsonValueParser mValueParser;

    // Parses configuration for each property.
    std::optional<ConfigDeclaration> parseEachProperty(const Json::Value& propJsonValue,
                                                       std::vector<std::string>* errors);
    // Tries to parse a JSON value to a specific type.
    //
    // If fieldIsOptional is True, then if the field specified by "fieldName" does not exist,
    // this method will return true without doing anything, otherwise, it will return false.
    //
    // @param parentJsonNode The parent node of the field you are going to parse.
    // @param fieldName The name for the field.
    // @param fieldIsOptional Whether the field is optional.
    // @param outPtr The pointer to output to if the field exists and parsing succeeded.
    // @param errors The error array to append error to if errors are found.
    // @return true if the field is optional and does not exist or parsed successfully.
    template <class T>
    bool tryParseJsonValueToVariable(const Json::Value& parentJsonNode,
                                     const std::string& fieldName, bool fieldIsOptional, T* outPtr,
                                     std::vector<std::string>* errors);
    // Tries to parse a JSON value to an array of specific type.
    //
    // If fieldIsOptional is True, then if the field specified by "fieldName" does not exist,
    // this method will return true without doing anything, otherwise, it will return false.
    //
    // @param parentJsonNode The parent node of the field you are going to parse.
    // @param fieldName The name for the field.
    // @param fieldIsOptional Whether the field is optional.
    // @param outPtr The pointer to output to if the field exists and parsing succeeded.
    // @param errors The error array to append error to if errors are found.
    // @return true if the field is optional and does not exist or parsed successfully.
    template <class T>
    bool tryParseJsonArrayToVariable(const Json::Value& parentJsonNode,
                                     const std::string& fieldName, bool fieldIsOptional,
                                     std::vector<T>* outPtr, std::vector<std::string>* errors);

    // Parses a JSON field to RawPropValues.
    void parsePropValues(const Json::Value& parentJsonNode, const std::string& fieldName,
                         aidl::android::hardware::automotive::vehicle::RawPropValues* outPtr,
                         std::vector<std::string>* errors);
};

}  // namespace jsonconfigloader_impl

// A class to load vehicle property configs and initial values in JSON format.
class JsonConfigLoader final {
  public:
    JsonConfigLoader();

    // Loads a JSON file stream and parses it to a list of ConfigDeclarations.
    android::base::Result<std::vector<ConfigDeclaration>> loadPropConfig(std::istream& is);

  private:
    std::unique_ptr<jsonconfigloader_impl::JsonConfigParser> mParser;
};

}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android

#endif  // android_hardware_automotive_vehicle_aidl_impl_default_config_JsonConfigLoader_include_JsonConfigLoader_H_
Loading