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

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

Merge "Implment getValues in Default VHAL."

parents b6928036 c2cf86b1
Loading
Loading
Loading
Loading
+46 −35
Original line number Diff line number Diff line
@@ -17,10 +17,15 @@
#ifndef android_hardware_automotive_vehicle_aidl_impl_vhal_include_DefaultVehicleHal_H_
#define android_hardware_automotive_vehicle_aidl_impl_vhal_include_DefaultVehicleHal_H_

#include "ConnectedClient.h"
#include "ParcelableUtils.h"

#include <IVehicleHardware.h>
#include <LargeParcelableBase.h>
#include <VehicleUtils.h>
#include <aidl/android/hardware/automotive/vehicle/BnVehicle.h>
#include <android-base/expected.h>
#include <android-base/thread_annotations.h>
#include <android/binder_auto_utils.h>

#include <memory>
@@ -40,12 +45,20 @@ constexpr int INVALID_MEMORY_FD = -1;
template <class T>
::ndk::ScopedAStatus toScopedAStatus(
        const ::android::base::Result<T>& result,
        ::aidl::android::hardware::automotive::vehicle::StatusCode status) {
        ::aidl::android::hardware::automotive::vehicle::StatusCode status,
        const std::string& additionalErrorMsg) {
    if (result.ok()) {
        return ::ndk::ScopedAStatus::ok();
    }
    return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(toInt(status),
                                                                     getErrorMsg(result).c_str());
    return ::ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
            toInt(status), (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>
@@ -53,43 +66,30 @@ template <class T>
    return toScopedAStatus(result, getErrorCode(result));
}

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) {
        output->payloads = values;
    } else {
        // Move the returned ScopedFileDescriptor pointer to ScopedFileDescriptor value in
        // 'sharedMemoryFd' field.
        output->sharedMemoryFd.set(fd->get());
        *(fd->getR()) = INVALID_MEMORY_FD;
    }
    return ::ndk::ScopedAStatus::ok();
template <class T>
::ndk::ScopedAStatus toScopedAStatus(const ::android::base::Result<T>& result,
                                     const std::string& additionalErrorMsg) {
    return toScopedAStatus(result, getErrorCode(result), additionalErrorMsg);
}

}  // namespace defaultvehiclehal_impl

class DefaultVehicleHal final : public ::aidl::android::hardware::automotive::vehicle::BnVehicle {
  public:
    using CallbackType =
            std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>;

    explicit DefaultVehicleHal(std::unique_ptr<IVehicleHardware> hardware);

    ::ndk::ScopedAStatus getAllPropConfigs(
            ::aidl::android::hardware::automotive::vehicle::VehiclePropConfigs* returnConfigs)
            override;
    ::ndk::ScopedAStatus getValues(
            const std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>&
                    callback,
            const CallbackType& callback,
            const ::aidl::android::hardware::automotive::vehicle::GetValueRequests& requests)
            override;
    ::ndk::ScopedAStatus setValues(
            const std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>&
                    callback,
            const CallbackType& callback,
            const ::aidl::android::hardware::automotive::vehicle::SetValueRequests& requests)
            override;
    ::ndk::ScopedAStatus getPropConfigs(
@@ -97,27 +97,38 @@ class DefaultVehicleHal final : public ::aidl::android::hardware::automotive::ve
            ::aidl::android::hardware::automotive::vehicle::VehiclePropConfigs* returnConfigs)
            override;
    ::ndk::ScopedAStatus subscribe(
            const std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>&
                    callback,
            const CallbackType& callback,
            const std::vector<::aidl::android::hardware::automotive::vehicle::SubscribeOptions>&
                    options,
            int32_t maxSharedMemoryFileCount) override;
    ::ndk::ScopedAStatus unsubscribe(
            const std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>&
                    callback,
    ::ndk::ScopedAStatus unsubscribe(const CallbackType& callback,
                                     const std::vector<int32_t>& propIds) override;
    ::ndk::ScopedAStatus returnSharedMemory(
            const std::shared_ptr<::aidl::android::hardware::automotive::vehicle::IVehicleCallback>&
                    callback,
    ::ndk::ScopedAStatus returnSharedMemory(const CallbackType& callback,
                                            int64_t sharedMemoryId) override;

    IVehicleHardware* getHardware();

  private:
    // friend class for unit testing.
    friend class DefaultVehicleHalTest;

    using GetValuesClient =
            GetSetValuesClient<::aidl::android::hardware::automotive::vehicle::GetValueResult,
                               ::aidl::android::hardware::automotive::vehicle::GetValueResults>;

    const std::unique_ptr<IVehicleHardware> mVehicleHardware;
    std::unordered_map<int32_t, ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig>
            mConfigsByPropId;
    std::unique_ptr<::ndk::ScopedFileDescriptor> mConfigFile;

    std::mutex mLock;
    std::unordered_map<CallbackType, std::shared_ptr<GetValuesClient>> mGetValuesClients
            GUARDED_BY(mLock);

    template <class T>
    std::shared_ptr<T> getOrCreateClient(
            std::unordered_map<CallbackType, std::shared_ptr<T>>* clients,
            const CallbackType& callback) REQUIRES(mLock);
};

}  // namespace vehicle
+59 −11
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <LargeParcelableBase.h>
#include <VehicleHalTypes.h>
#include <VehicleUtils.h>

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

@@ -29,14 +30,19 @@ namespace hardware {
namespace automotive {
namespace vehicle {

using ::aidl::android::hardware::automotive::vehicle::GetValueRequest;
using ::aidl::android::hardware::automotive::vehicle::GetValueRequests;
using ::aidl::android::hardware::automotive::vehicle::GetValueResult;
using ::aidl::android::hardware::automotive::vehicle::GetValueResults;
using ::aidl::android::hardware::automotive::vehicle::IVehicleCallback;
using ::aidl::android::hardware::automotive::vehicle::SetValueRequests;
using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::SubscribeOptions;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfigs;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
using ::android::automotive::car_binder_lib::LargeParcelableBase;
using ::android::base::expected;
using ::android::base::Result;
using ::ndk::ScopedAStatus;

@@ -70,14 +76,58 @@ ScopedAStatus DefaultVehicleHal::getAllPropConfigs(VehiclePropConfigs* output) {
    return ScopedAStatus::ok();
}

ScopedAStatus DefaultVehicleHal::getValues(const std::shared_ptr<IVehicleCallback>&,
                                           const GetValueRequests&) {
    // TODO(b/200737967): implement this.
template <class T>
std::shared_ptr<T> DefaultVehicleHal::getOrCreateClient(
        std::unordered_map<CallbackType, std::shared_ptr<T>>* clients,
        const CallbackType& callback) {
    if (clients->find(callback) == clients->end()) {
        // TODO(b/204943359): Remove client from clients when linkToDeath is implemented.
        (*clients)[callback] = std::make_shared<T>(callback);
    }
    return (*clients)[callback];
}

template std::shared_ptr<DefaultVehicleHal::GetValuesClient>
DefaultVehicleHal::getOrCreateClient<DefaultVehicleHal::GetValuesClient>(
        std::unordered_map<CallbackType, std::shared_ptr<GetValuesClient>>* clients,
        const CallbackType& callback);

ScopedAStatus DefaultVehicleHal::getValues(const CallbackType& callback,
                                           const GetValueRequests& requests) {
    // TODO(b/203713317): check for duplicate properties and duplicate request IDs.

    const std::vector<GetValueRequest>* getValueRequests;
    // Define deserializedResults here because we need it to have the same lifetime as
    // getValueRequests.
    expected<std::vector<GetValueRequest>, ScopedAStatus> deserializedResults;
    if (!requests.payloads.empty()) {
        getValueRequests = &requests.payloads;
    } else {
        deserializedResults = stableLargeParcelableToVector<GetValueRequest>(requests);
        if (!deserializedResults.ok()) {
            ALOGE("failed to parse getValues requests");
            return std::move(deserializedResults.error());
        }
        getValueRequests = &deserializedResults.value();
    }

    std::shared_ptr<GetValuesClient> client;
    {
        std::scoped_lock<std::mutex> lockGuard(mLock);
        client = getOrCreateClient(&mGetValuesClients, callback);
    }

    if (StatusCode status =
                mVehicleHardware->getValues(client->getResultCallback(), *getValueRequests);
        status != StatusCode::OK) {
        return ScopedAStatus::fromServiceSpecificErrorWithMessage(
                toInt(status), "failed to get value from VehicleHardware");
    }

    return ScopedAStatus::ok();
}

ScopedAStatus DefaultVehicleHal::setValues(const std::shared_ptr<IVehicleCallback>&,
                                           const SetValueRequests&) {
ScopedAStatus DefaultVehicleHal::setValues(const CallbackType&, const SetValueRequests&) {
    // TODO(b/200737967): implement this.
    return ScopedAStatus::ok();
}
@@ -90,23 +140,21 @@ ScopedAStatus DefaultVehicleHal::getPropConfigs(const std::vector<int32_t>& prop
            configs.push_back(mConfigsByPropId[prop]);
        }
    }
    return defaultvehiclehal_impl::vectorToStableLargeParcelable(configs, output);
    return vectorToStableLargeParcelable(std::move(configs), output);
}

ScopedAStatus DefaultVehicleHal::subscribe(const std::shared_ptr<IVehicleCallback>&,
ScopedAStatus DefaultVehicleHal::subscribe(const CallbackType&,
                                           const std::vector<SubscribeOptions>&, int32_t) {
    // TODO(b/200737967): implement this.
    return ScopedAStatus::ok();
}

ScopedAStatus DefaultVehicleHal::unsubscribe(const std::shared_ptr<IVehicleCallback>&,
                                             const std::vector<int32_t>&) {
ScopedAStatus DefaultVehicleHal::unsubscribe(const CallbackType&, const std::vector<int32_t>&) {
    // TODO(b/200737967): implement this.
    return ScopedAStatus::ok();
}

ScopedAStatus DefaultVehicleHal::returnSharedMemory(const std::shared_ptr<IVehicleCallback>&,
                                                    int64_t) {
ScopedAStatus DefaultVehicleHal::returnSharedMemory(const CallbackType&, int64_t) {
    // TODO(b/200737967): implement this.
    return ScopedAStatus::ok();
}
+290 −17

File changed.

Preview size limit exceeded, changes collapsed.