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

Commit 2558bf60 authored by Yu Shan's avatar Yu Shan
Browse files

Allow using property name in debug command.

Add stringToPropertyId to VehicleUtils and use that in
FakeVehicleHardware to support commands like "--set PERF_VEHICLE_SPEED".

Test: atest VehicleUtilsTest FakeVehicleHardwareTest
Bug: 342260344
Merged-In: If317929376eb18bb0c28f87c4d7a3321c58d72b1

Change-Id: If317929376eb18bb0c28f87c4d7a3321c58d72b1
parent e63470d7
Loading
Loading
Loading
Loading
+12 −10
Original line number Original line Diff line number Diff line
@@ -269,16 +269,6 @@ class FakeVehicleHardware : public IVehicleHardware {
    std::string dumpInjectEvent(const std::vector<std::string>& options);
    std::string dumpInjectEvent(const std::vector<std::string>& options);
    std::string dumpSubscriptions();
    std::string dumpSubscriptions();


    template <typename T>
    android::base::Result<T> safelyParseInt(int index, const std::string& s) {
        T out;
        if (!::android::base::ParseInt(s, &out)) {
            return android::base::Error() << android::base::StringPrintf(
                           "non-integer argument at index %d: %s\n", index, s.c_str());
        }
        return out;
    }
    android::base::Result<float> safelyParseFloat(int index, const std::string& s);
    std::vector<std::string> getOptionValues(const std::vector<std::string>& options,
    std::vector<std::string> getOptionValues(const std::vector<std::string>& options,
                                             size_t* index);
                                             size_t* index);
    android::base::Result<aidl::android::hardware::automotive::vehicle::VehiclePropValue>
    android::base::Result<aidl::android::hardware::automotive::vehicle::VehiclePropValue>
@@ -325,6 +315,18 @@ class FakeVehicleHardware : public IVehicleHardware {
            const aidl::android::hardware::automotive::vehicle::VehiclePropConfig&
            const aidl::android::hardware::automotive::vehicle::VehiclePropConfig&
                    vehiclePropConfig,
                    vehiclePropConfig,
            int32_t areaId);
            int32_t areaId);
    template <typename T>
    static android::base::Result<T> safelyParseInt(int index, const std::string& s) {
        T out;
        if (!::android::base::ParseInt(s, &out)) {
            return android::base::Error() << android::base::StringPrintf(
                           "non-integer argument at index %d: %s\n", index, s.c_str());
        }
        return out;
    }
    static android::base::Result<float> safelyParseFloat(int index, const std::string& s);
    static android::base::Result<int32_t> parsePropId(const std::vector<std::string>& options,
                                                      size_t index);
};
};


}  // namespace fake
}  // namespace fake
+13 −3
Original line number Original line Diff line number Diff line
@@ -1892,6 +1892,16 @@ Result<void> FakeVehicleHardware::checkArgumentsSize(const std::vector<std::stri
                                   minSize, size);
                                   minSize, size);
}
}


Result<int32_t> FakeVehicleHardware::parsePropId(const std::vector<std::string>& options,
                                                 size_t index) {
    const std::string& propIdStr = options[index];
    auto result = stringToPropId(propIdStr);
    if (result.ok()) {
        return result;
    }
    return safelyParseInt<int32_t>(index, propIdStr);
}

std::string FakeVehicleHardware::dumpSpecificProperty(const std::vector<std::string>& options) {
std::string FakeVehicleHardware::dumpSpecificProperty(const std::vector<std::string>& options) {
    if (auto result = checkArgumentsSize(options, /*minSize=*/2); !result.ok()) {
    if (auto result = checkArgumentsSize(options, /*minSize=*/2); !result.ok()) {
        return getErrorMsg(result);
        return getErrorMsg(result);
@@ -1902,7 +1912,7 @@ std::string FakeVehicleHardware::dumpSpecificProperty(const std::vector<std::str
    size_t size = options.size();
    size_t size = options.size();
    std::string msg = "";
    std::string msg = "";
    for (size_t i = 1; i < size; ++i) {
    for (size_t i = 1; i < size; ++i) {
        auto propResult = safelyParseInt<int32_t>(i, options[i]);
        auto propResult = parsePropId(options, i);
        if (!propResult.ok()) {
        if (!propResult.ok()) {
            msg += getErrorMsg(propResult);
            msg += getErrorMsg(propResult);
            continue;
            continue;
@@ -1938,9 +1948,9 @@ Result<VehiclePropValue> FakeVehicleHardware::parsePropOptions(
    // --set/get/inject-event PROP [-f f1 f2...] [-i i1 i2...] [-i64 i1 i2...] [-s s1 s2...]
    // --set/get/inject-event PROP [-f f1 f2...] [-i i1 i2...] [-i64 i1 i2...] [-s s1 s2...]
    // [-b b1 b2...] [-a a] [-t timestamp]
    // [-b b1 b2...] [-a a] [-t timestamp]
    size_t optionIndex = 1;
    size_t optionIndex = 1;
    auto result = safelyParseInt<int32_t>(optionIndex, options[optionIndex]);
    auto result = parsePropId(options, optionIndex);
    if (!result.ok()) {
    if (!result.ok()) {
        return Error() << StringPrintf("Property value: \"%s\" is not a valid int: %s\n",
        return Error() << StringPrintf("Property ID/Name: \"%s\" is not valid: %s\n",
                                       options[optionIndex].c_str(), getErrorMsg(result).c_str());
                                       options[optionIndex].c_str(), getErrorMsg(result).c_str());
    }
    }
    VehiclePropValue prop = {};
    VehiclePropValue prop = {};
+19 −4
Original line number Original line Diff line number Diff line
@@ -2613,6 +2613,23 @@ TEST_F(FakeVehicleHardwareTest, testDumpSpecificProperties) {
                                           prop1.c_str(), prop2.c_str(), prop2.c_str())));
                                           prop1.c_str(), prop2.c_str(), prop2.c_str())));
}
}


TEST_F(FakeVehicleHardwareTest, testDumpSpecificPropertiesWithName) {
    std::vector<std::string> options;
    options.push_back("--get");
    std::string prop1 = "INFO_FUEL_CAPACITY";
    std::string prop2 = "TIRE_PRESSURE";
    int prop1Int = toInt(VehicleProperty::INFO_FUEL_CAPACITY);
    int prop2Int = toInt(VehicleProperty::TIRE_PRESSURE);
    options.push_back(prop1);
    options.push_back(prop2);
    DumpResult result = getHardware()->dump(options);
    ASSERT_FALSE(result.callerShouldDumpState);
    ASSERT_NE(result.buffer, "");
    ASSERT_THAT(result.buffer,
                ContainsRegex(StringPrintf("1:.*prop: %d.*\n2-0:.*prop: %d.*\n2-1:.*prop: %d.*\n",
                                           prop1Int, prop2Int, prop2Int)));
}

TEST_F(FakeVehicleHardwareTest, testDumpSpecificPropertiesInvalidProp) {
TEST_F(FakeVehicleHardwareTest, testDumpSpecificPropertiesInvalidProp) {
    std::vector<std::string> options;
    std::vector<std::string> options;
    options.push_back("--get");
    options.push_back("--get");
@@ -2766,6 +2783,7 @@ std::vector<SetPropTestCase> GenSetPropParams() {
    std::string infoMakeProperty = std::to_string(toInt(VehicleProperty::INFO_MAKE));
    std::string infoMakeProperty = std::to_string(toInt(VehicleProperty::INFO_MAKE));
    return {
    return {
            {"success_set_string", {"--set", infoMakeProperty, "-s", CAR_MAKE}, true},
            {"success_set_string", {"--set", infoMakeProperty, "-s", CAR_MAKE}, true},
            {"success_set_with_name", {"--set", "INFO_MAKE", "-s", CAR_MAKE}, true},
            {"success_set_bytes", {"--set", infoMakeProperty, "-b", "0xdeadbeef"}, true},
            {"success_set_bytes", {"--set", infoMakeProperty, "-b", "0xdeadbeef"}, true},
            {"success_set_bytes_caps", {"--set", infoMakeProperty, "-b", "0xDEADBEEF"}, true},
            {"success_set_bytes_caps", {"--set", infoMakeProperty, "-b", "0xDEADBEEF"}, true},
            {"success_set_int", {"--set", infoMakeProperty, "-i", "2147483647"}, true},
            {"success_set_int", {"--set", infoMakeProperty, "-i", "2147483647"}, true},
@@ -2790,10 +2808,7 @@ std::vector<SetPropTestCase> GenSetPropParams() {
             false,
             false,
             "No values specified"},
             "No values specified"},
            {"fail_unknown_options", {"--set", infoMakeProperty, "-abcd"}, false, "Unknown option"},
            {"fail_unknown_options", {"--set", infoMakeProperty, "-abcd"}, false, "Unknown option"},
            {"fail_invalid_property",
            {"fail_invalid_property", {"--set", "not_valid", "-s", CAR_MAKE}, false, "not valid"},
             {"--set", "not valid", "-s", CAR_MAKE},
             false,
             "not a valid int"},
            {"fail_duplicate_string",
            {"fail_duplicate_string",
             {"--set", infoMakeProperty, "-s", CAR_MAKE, "-s", CAR_MAKE},
             {"--set", infoMakeProperty, "-s", CAR_MAKE, "-s", CAR_MAKE},
             false,
             false,
+4 −0
Original line number Original line Diff line number Diff line
@@ -328,11 +328,15 @@ struct PropIdAreaIdHash {
    }
    }
};
};


// This is for debug purpose only.
inline std::string propIdToString(int32_t propId) {
inline std::string propIdToString(int32_t propId) {
    return toString(
    return toString(
            static_cast<aidl::android::hardware::automotive::vehicle::VehicleProperty>(propId));
            static_cast<aidl::android::hardware::automotive::vehicle::VehicleProperty>(propId));
}
}


// This is for debug purpose only.
android::base::Result<int32_t> stringToPropId(const std::string& propName);

template <typename T>
template <typename T>
void roundToNearestResolution(std::vector<T>& arrayToSanitize, float resolution) {
void roundToNearestResolution(std::vector<T>& arrayToSanitize, float resolution) {
    if (resolution == 0) {
    if (resolution == 0) {
+41 −0
Original line number Original line Diff line number Diff line
@@ -16,14 +16,18 @@


#include "VehicleUtils.h"
#include "VehicleUtils.h"


#include <unordered_map>

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


using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::toString;
using ::aidl::android::hardware::automotive::vehicle::VehicleAreaConfig;
using ::aidl::android::hardware::automotive::vehicle::VehicleAreaConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyGroup;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyGroup;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
@@ -31,6 +35,39 @@ using ::android::base::Error;
using ::android::base::Result;
using ::android::base::Result;
using ::ndk::ScopedAStatus;
using ::ndk::ScopedAStatus;


namespace {

class PropertyIdByNameSingleton {
  public:
    static PropertyIdByNameSingleton& getInstance() {
        static PropertyIdByNameSingleton instance;
        return instance;
    }

    Result<int32_t> getPropertyId(const std::string& name) {
        auto it = mPropertyIdByName.find(name);
        if (it == mPropertyIdByName.end()) {
            return Error();
        }
        return it->second;
    }

    PropertyIdByNameSingleton(PropertyIdByNameSingleton const&) = delete;
    void operator=(PropertyIdByNameSingleton const&) = delete;

  private:
    std::unordered_map<std::string, int32_t> mPropertyIdByName;

    PropertyIdByNameSingleton() {
        constexpr auto values = ndk::internal::enum_values<VehicleProperty>;
        for (unsigned int i = 0; i < values.size(); i++) {
            mPropertyIdByName.emplace(toString(values[i]), toInt(values[i]));
        }
    }
};

}  // namespace

Result<void> checkPropValue(const VehiclePropValue& value, const VehiclePropConfig* config) {
Result<void> checkPropValue(const VehiclePropValue& value, const VehiclePropConfig* config) {
    int32_t property = value.prop;
    int32_t property = value.prop;
    VehiclePropertyType type = getPropType(property);
    VehiclePropertyType type = getPropType(property);
@@ -213,6 +250,10 @@ std::string VhalError::print() const {
    return aidl::android::hardware::automotive::vehicle::toString(mCode);
    return aidl::android::hardware::automotive::vehicle::toString(mCode);
}
}


Result<int32_t> stringToPropId(const std::string& propName) {
    return PropertyIdByNameSingleton::getInstance().getPropertyId(propName);
}

}  // namespace vehicle
}  // namespace vehicle
}  // namespace automotive
}  // namespace automotive
}  // namespace hardware
}  // namespace hardware
Loading