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

Commit 2909ca28 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I0e0be46f,I2d34138a,I48729915

* changes:
  Create ConnectedClient to manage VHAL clients.
  Add MockVehicleCallback for testing.
  Add ParcelableUtils.
parents 265aa9bc 7a1c24fd
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@
#include <aidl/android/hardware/automotive/vehicle/FuelType.h>
#include <aidl/android/hardware/automotive/vehicle/GetValueRequest.h>
#include <aidl/android/hardware/automotive/vehicle/GetValueResult.h>
#include <aidl/android/hardware/automotive/vehicle/GetValueResults.h>
#include <aidl/android/hardware/automotive/vehicle/Obd2CommonIgnitionMonitors.h>
#include <aidl/android/hardware/automotive/vehicle/Obd2FuelSystemStatus.h>
#include <aidl/android/hardware/automotive/vehicle/Obd2FuelType.h>
@@ -34,6 +35,7 @@
#include <aidl/android/hardware/automotive/vehicle/PortLocationType.h>
#include <aidl/android/hardware/automotive/vehicle/SetValueRequest.h>
#include <aidl/android/hardware/automotive/vehicle/SetValueResult.h>
#include <aidl/android/hardware/automotive/vehicle/SetValueResults.h>
#include <aidl/android/hardware/automotive/vehicle/StatusCode.h>
#include <aidl/android/hardware/automotive/vehicle/VehicleApPowerStateReport.h>
#include <aidl/android/hardware/automotive/vehicle/VehicleApPowerStateReq.h>
+32 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@

#include <VehicleHalTypes.h>

#include <android-base/format.h>
#include <android-base/result.h>
#include <utils/Log.h>

@@ -208,6 +209,37 @@ std::string getErrorMsg(const ::android::base::Result<T>& result) {
    return result.error().message();
}

template <class T>
::ndk::ScopedAStatus toScopedAStatus(
        const ::android::base::Result<T>& result,
        ::aidl::android::hardware::automotive::vehicle::StatusCode status,
        std::string additionalErrorMsg) {
    if (result.ok()) {
        return ::ndk::ScopedAStatus::ok();
    }
    return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
            toInt(status),
            fmt::format("{}, error: {}", additionalErrorMsg, getErrorMsg(result)).c_str());
}

template <class T>
::ndk::ScopedAStatus toScopedAStatus(
        const ::android::base::Result<T>& result,
        ::aidl::android::hardware::automotive::vehicle::StatusCode status) {
    return toScopedAStatus(result, status, "");
}

template <class T>
::ndk::ScopedAStatus toScopedAStatus(const ::android::base::Result<T>& result) {
    return toScopedAStatus(result, getErrorCode(result));
}

template <class T>
::ndk::ScopedAStatus toScopedAStatus(const ::android::base::Result<T>& result,
                                     std::string additionalErrorMsg) {
    return toScopedAStatus(result, getErrorCode(result), additionalErrorMsg);
}

}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
+4 −1
Original line number Diff line number Diff line
@@ -53,7 +53,10 @@ cc_library {
    ],
    local_include_dirs: ["include"],
    export_include_dirs: ["include"],
    srcs: ["src/DefaultVehicleHal.cpp"],
    srcs: [
        "src/ConnectedClient.cpp",
        "src/DefaultVehicleHal.cpp",
    ],
    static_libs: [
        "VehicleHalUtils",
        "android-automotive-large-parcelable-vendor-lib",
+82 −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_vhal_include_ConnectedClient_H_
#define android_hardware_automotive_vehicle_aidl_impl_vhal_include_ConnectedClient_H_

#include <VehicleHalTypes.h>

#include <aidl/android/hardware/automotive/vehicle/IVehicleCallback.h>
#include <android-base/result.h>

#include <memory>
#include <unordered_set>
#include <vector>

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

// A class to represent a binder client with a callback interface. Each callback function, e.g.
// GetValues or SetValues for a specific binder client is a separate {@code ConnectedClient}.
// For one {@code ConnectedClient}, we use one pending request pool to manage all pending requests,
// so the request IDs must be unique for one client. We also manage a set of callback functions
// for one client, e.g. timeoutCallback which could be passed to hardware.
// This class is thread-safe.
class ConnectedClient {
  public:
    ConnectedClient(
            std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>
                    callback);

    virtual ~ConnectedClient() = default;

  protected:
    const std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>
            mCallback;
};

// A class to represent a client that calls {@code IVehicle.setValues} or {@code
// IVehicle.getValues}.
template <class ResultType, class ResultsType>
class GetSetValuesClient final : public ConnectedClient {
  public:
    GetSetValuesClient(
            std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>
                    callback);

    // Sends the results to this client.
    void sendResults(const std::vector<ResultType>& results);

    // Sends each result separately to this client. Each result would be sent through one callback
    // invocation.
    void sendResultsSeparately(const std::vector<ResultType>& results);

    // Gets the callback to be called when the request for this client has finished.
    std::shared_ptr<const std::function<void(std::vector<ResultType>)>> getResultCallback();

  private:
    // The following members are only initialized during construction.
    std::shared_ptr<const std::function<void(std::vector<ResultType>)>> mResultCallback;
};

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

#endif  // android_hardware_automotive_vehicle_aidl_impl_vhal_include_ConnectedClient_H_
+89 −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_vhal_include_ParcelableUtils_H_
#define android_hardware_automotive_vehicle_aidl_impl_vhal_include_ParcelableUtils_H_

#include <LargeParcelableBase.h>
#include <VehicleHalTypes.h>
#include <VehicleUtils.h>

#include <memory>
#include <vector>

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

template <class T1, class T2>
::ndk::ScopedAStatus vectorToStableLargeParcelable(std::vector<T1>&& values, T2* output) {
    auto result = ::android::automotive::car_binder_lib::LargeParcelableBase::
            parcelableVectorToStableLargeParcelable(values);
    if (!result.ok()) {
        return toScopedAStatus(
                result, ::aidl::android::hardware::automotive::vehicle::StatusCode::INTERNAL_ERROR);
    }
    auto& fd = result.value();
    if (fd == nullptr) {
        // If we no longer needs values, move it inside the payloads to avoid copying.
        output->payloads = std::move(values);
    } else {
        // Move the returned ScopedFileDescriptor pointer to ScopedFileDescriptor value in
        // 'sharedMemoryFd' field.
        output->sharedMemoryFd = std::move(*fd);
    }
    return ::ndk::ScopedAStatus::ok();
}

template <class T1, class T2>
::ndk::ScopedAStatus vectorToStableLargeParcelable(const std::vector<T1>& values, T2* output) {
    // Because 'values' is passed in as const reference, we have to do a copy here.
    std::vector<T1> valuesCopy = values;

    return vectorToStableLargeParcelable(std::move(valuesCopy), output);
}

template <class T1, class T2>
::android::base::expected<std::vector<T1>, ::ndk::ScopedAStatus> stableLargeParcelableToVector(
        const T2& largeParcelable) {
    ::android::base::Result<std::optional<std::vector<T1>>> result =
            ::android::automotive::car_binder_lib::LargeParcelableBase::
                    stableLargeParcelableToParcelableVector<T1>(largeParcelable.sharedMemoryFd);

    if (!result.ok()) {
        return ::android::base::unexpected(toScopedAStatus(
                result, ::aidl::android::hardware::automotive::vehicle::StatusCode::INVALID_ARG,
                "failed to parse large parcelable"));
    }

    if (!result.value().has_value()) {
        return ::android::base::unexpected(
                ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
                        toInt(::aidl::android::hardware::automotive::vehicle::StatusCode::
                                      INVALID_ARG),
                        "empty request"));
    }

    return std::move(result.value().value());
}

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

#endif  // android_hardware_automotive_vehicle_aidl_impl_vhal_include_ParcelableUtils_H_
Loading