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

Commit a2bbcd26 authored by Yu Shan's avatar Yu Shan
Browse files

Migrate proto_message_converter to AIDL vhal.

Migrate proto_message_converter to AIDL vhal. This would be used
in grpc vehicleClient/vehiclServer. This CL also creates the
required proto files.

Test: unit tests.
Bug: 199337732
Change-Id: Iac69a8c1578d4aba374ee0d9716da8b6b18ccace
parent 5e000b8a
Loading
Loading
Loading
Loading
+503 −593

File changed.

Preview size limit exceeded, changes collapsed.

+3 −1
Original line number Diff line number Diff line
@@ -22,17 +22,19 @@ namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {
namespace defaultconfig {

namespace test {

TEST(DefaultConfigTest, loadDefaultConfigs) {
    for (ConfigDeclaration config : kVehicleProperties) {
    for (ConfigDeclaration config : getDefaultConfigs()) {
        ASSERT_NE(0, config.config.prop);
    }
}

}  // namespace test

}  // namespace defaultconfig
}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.
 */

cc_library {
    name: "VehicleHalProtoMessageConverter",
    srcs: [
        "src/*.cpp",
    ],
    vendor: true,
    local_include_dirs: ["include"],
    export_include_dirs: ["include"],
    shared_libs: ["libprotobuf-cpp-full"],
    static_libs: [
        "VehicleHalProtos",
        "VehicleHalUtils",
    ],
    defaults: ["VehicleHalDefaults"],
    export_static_lib_headers: ["VehicleHalUtils"],
}

cc_test {
    name: "VehicleHalProtoMessageConverterTest",
    srcs: [
        "test/*.cpp",
    ],
    vendor: true,
    defaults: ["VehicleHalDefaults"],
    shared_libs: ["libprotobuf-cpp-full"],
    static_libs: [
        "VehicleHalProtoMessageConverter",
        "VehicleHalProtos",
        "VehicleHalUtils",
        "libgtest",
    ],
    header_libs: ["VehicleHalDefaultConfig"],
    test_suites: ["device-tests"],
}
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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_grpc_utils_proto_message_converter_include_ProtoMessageConverter_H_
#define android_hardware_automotive_vehicle_aidl_impl_grpc_utils_proto_message_converter_include_ProtoMessageConverter_H_

#include <VehicleHalTypes.h>
#include <android/hardware/automotive/vehicle/VehicleAreaConfig.pb.h>
#include <android/hardware/automotive/vehicle/VehiclePropConfig.pb.h>
#include <android/hardware/automotive/vehicle/VehiclePropValue.pb.h>
#include <android/hardware/automotive/vehicle/VehiclePropertyAccess.pb.h>
#include <android/hardware/automotive/vehicle/VehiclePropertyChangeMode.pb.h>
#include <android/hardware/automotive/vehicle/VehiclePropertyStatus.pb.h>

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

// Convert AIDL VehiclePropConfig to Protobuf VehiclePropConfig.
void aidlToProto(
        const ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig& inAidlConfig,
        ::android::hardware::automotive::vehicle::proto::VehiclePropConfig* outProtoConfig);
// Convert Protobuf VehiclePropConfig to AIDL VehiclePropConfig.
void protoToAidl(
        const ::android::hardware::automotive::vehicle::proto::VehiclePropConfig& inProtoConfig,
        ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig* outAidlConfig);
// Convert AIDL VehiclePropValue to Protobuf VehiclePropValue.
void aidlToProto(const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& inAidlVal,
                 ::android::hardware::automotive::vehicle::proto::VehiclePropValue* outProtoVal);
// Convert Protobuf VehiclePropValue to AIDL VehiclePropValue.
void protoToAidl(
        const ::android::hardware::automotive::vehicle::proto::VehiclePropValue& inProtoVal,
        ::aidl::android::hardware::automotive::vehicle::VehiclePropValue* outAidlVal);

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

#endif  // android_hardware_automotive_vehicle_aidl_impl_grpc_utils_proto_message_converter_include_ProtoMessageConverter_H_
+146 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.
 */

#define LOG_TAG "ProtoMsgConverter"

#include "ProtoMessageConverter.h"

#include <VehicleUtils.h>

#include <memory>
#include <vector>

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

namespace aidl_vehicle = ::aidl::android::hardware::automotive::vehicle;
namespace proto = ::android::hardware::automotive::vehicle::proto;

// Copy the vector PROTO_VECNAME of protobuf class PROTO_VALUE to
// VHAL_TYPE_VALUE->VHAL_TYPE_VECNAME, every element of PROTO_VECNAME is casted by CAST.
#define CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE(PROTO_VALUE, PROTO_VECNAME, VHAL_TYPE_VALUE, \
                                            VHAL_TYPE_VECNAME, CAST)                     \
    do {                                                                                 \
        (VHAL_TYPE_VALUE)->VHAL_TYPE_VECNAME.resize(PROTO_VALUE.PROTO_VECNAME##_size()); \
        size_t idx = 0;                                                                  \
        for (auto& value : PROTO_VALUE.PROTO_VECNAME()) {                                \
            VHAL_TYPE_VALUE->VHAL_TYPE_VECNAME[idx++] = CAST(value);                     \
        }                                                                                \
    } while (0)

// Copying the vector PROTO_VECNAME of protobuf class PROTO_VALUE to
// VHAL_TYPE_VALUE->VHAL_TYPE_VECNAME.
#define COPY_PROTOBUF_VEC_TO_VHAL_TYPE(PROTO_VALUE, PROTO_VECNAME, VHAL_TYPE_VALUE, \
                                       VHAL_TYPE_VECNAME)                           \
    CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE(                                            \
            PROTO_VALUE, PROTO_VECNAME, VHAL_TYPE_VALUE, VHAL_TYPE_VECNAME, /*NO CAST*/)

void aidlToProto(const aidl_vehicle::VehiclePropConfig& in, proto::VehiclePropConfig* out) {
    out->set_prop(in.prop);
    out->set_access(static_cast<proto::VehiclePropertyAccess>(toInt(in.access)));
    out->set_change_mode(static_cast<proto::VehiclePropertyChangeMode>(toInt(in.changeMode)));
    out->set_config_string(in.configString.c_str(), in.configString.size());
    out->set_min_sample_rate(in.minSampleRate);
    out->set_max_sample_rate(in.maxSampleRate);

    for (auto& configElement : in.configArray) {
        out->add_config_array(configElement);
    }

    out->clear_area_configs();
    for (auto& areaConfig : in.areaConfigs) {
        auto* protoACfg = out->add_area_configs();
        protoACfg->set_area_id(areaConfig.areaId);
        protoACfg->set_min_int64_value(areaConfig.minInt64Value);
        protoACfg->set_max_int64_value(areaConfig.maxInt64Value);
        protoACfg->set_min_float_value(areaConfig.minFloatValue);
        protoACfg->set_max_float_value(areaConfig.maxFloatValue);
        protoACfg->set_min_int32_value(areaConfig.minInt32Value);
        protoACfg->set_max_int32_value(areaConfig.maxInt32Value);
    }
}

void protoToAidl(const proto::VehiclePropConfig& in, aidl_vehicle::VehiclePropConfig* out) {
    out->prop = in.prop();
    out->access = static_cast<aidl_vehicle::VehiclePropertyAccess>(in.access());
    out->changeMode = static_cast<aidl_vehicle::VehiclePropertyChangeMode>(in.change_mode());
    out->configString = in.config_string();
    out->minSampleRate = in.min_sample_rate();
    out->maxSampleRate = in.max_sample_rate();

    COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, config_array, out, configArray);

    auto cast_to_acfg = [](const proto::VehicleAreaConfig& protoAcfg) {
        return aidl_vehicle::VehicleAreaConfig{
                .areaId = protoAcfg.area_id(),
                .minInt32Value = protoAcfg.min_int32_value(),
                .maxInt32Value = protoAcfg.max_int32_value(),
                .minInt64Value = protoAcfg.min_int64_value(),
                .maxInt64Value = protoAcfg.max_int64_value(),
                .minFloatValue = protoAcfg.min_float_value(),
                .maxFloatValue = protoAcfg.max_float_value(),
        };
    };
    CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, area_configs, out, areaConfigs, cast_to_acfg);
}

void aidlToProto(const aidl_vehicle::VehiclePropValue& in, proto::VehiclePropValue* out) {
    out->set_prop(in.prop);
    out->set_timestamp(in.timestamp);
    out->set_status(static_cast<proto::VehiclePropertyStatus>(in.status));
    out->set_area_id(in.areaId);
    out->set_string_value(in.value.stringValue);
    out->set_byte_values(in.value.byteValues.data(), in.value.byteValues.size());

    for (auto& int32Value : in.value.int32Values) {
        out->add_int32_values(int32Value);
    }

    for (auto& int64Value : in.value.int64Values) {
        out->add_int64_values(int64Value);
    }

    for (auto& floatValue : in.value.floatValues) {
        out->add_float_values(floatValue);
    }
}

void protoToAidl(const proto::VehiclePropValue& in, aidl_vehicle::VehiclePropValue* out) {
    out->prop = in.prop();
    out->timestamp = in.timestamp();
    out->status = static_cast<aidl_vehicle::VehiclePropertyStatus>(in.status());
    out->areaId = in.area_id();
    out->value.stringValue = in.string_value();
    for (const char& byte : in.byte_values()) {
        out->value.byteValues.push_back(byte);
    }

    COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, int32_values, out, value.int32Values);
    COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, int64_values, out, value.int64Values);
    COPY_PROTOBUF_VEC_TO_VHAL_TYPE(in, float_values, out, value.floatValues);
}

#undef COPY_PROTOBUF_VEC_TO_VHAL_TYPE
#undef CAST_COPY_PROTOBUF_VEC_TO_VHAL_TYPE

}  // namespace proto_msg_converter
}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace android
Loading