Add toString and formatter for PropIdAreaId.
This helps printing out debug string for PropIdAreaId struct.
Flag: EXEMPT hal
Bug: 382563296
Test: atest VehicleHalVehicleUtilsTest
Change-Id: I54613982506a804cf7b05e03f9f100595c1a874a
diff --git a/automotive/vehicle/aidl/impl/current/utils/common/include/VehicleUtils.h b/automotive/vehicle/aidl/impl/current/utils/common/include/VehicleUtils.h
index 90a7c46..5b19100 100644
--- a/automotive/vehicle/aidl/impl/current/utils/common/include/VehicleUtils.h
+++ b/automotive/vehicle/aidl/impl/current/utils/common/include/VehicleUtils.h
@@ -310,6 +310,12 @@
return toScopedAStatus(result, getErrorCode(result), additionalErrorMsg);
}
+// This is for debug purpose only.
+inline std::string propIdToString(int32_t propId) {
+ return toString(
+ static_cast<aidl::android::hardware::automotive::vehicle::VehicleProperty>(propId));
+}
+
struct PropIdAreaId {
int32_t propId;
int32_t areaId;
@@ -317,6 +323,11 @@
inline bool operator==(const PropIdAreaId& other) const {
return areaId == other.areaId && propId == other.propId;
}
+
+ // This is for debug purpose only.
+ inline std::string toString() const {
+ return fmt::format("{{propId: {}, areaId: {}}}", propIdToString(propId), areaId);
+ }
};
struct PropIdAreaIdHash {
@@ -329,12 +340,6 @@
};
// This is for debug purpose only.
-inline std::string propIdToString(int32_t propId) {
- return toString(
- 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);
// This is for debug purpose only. Converts an area's name to its enum definition.
@@ -362,4 +367,21 @@
} // namespace hardware
} // namespace android
+// Formatter must not be defined inside our namespace.
+template <>
+struct fmt::formatter<android::hardware::automotive::vehicle::PropIdAreaId> {
+ template <typename ParseContext>
+ constexpr auto parse(ParseContext& ctx) {
+ return ctx.begin();
+ }
+
+ template <typename FormatContext>
+ auto format(const android::hardware::automotive::vehicle::PropIdAreaId& p,
+ FormatContext& ctx) const {
+ return fmt::format_to(ctx.out(), "{{propId: {}, areaId: {}}}",
+ android::hardware::automotive::vehicle::propIdToString(p.propId),
+ p.areaId);
+ }
+};
+
#endif // android_hardware_automotive_vehicle_aidl_impl_utils_common_include_VehicleUtils_H_
diff --git a/automotive/vehicle/aidl/impl/current/utils/common/test/VehicleUtilsTest.cpp b/automotive/vehicle/aidl/impl/current/utils/common/test/VehicleUtilsTest.cpp
index 1048877..8278376 100644
--- a/automotive/vehicle/aidl/impl/current/utils/common/test/VehicleUtilsTest.cpp
+++ b/automotive/vehicle/aidl/impl/current/utils/common/test/VehicleUtilsTest.cpp
@@ -787,6 +787,29 @@
ASSERT_FALSE(result.ok());
}
+TEST(VehicleUtilsTest, testPropIdAreaIdToString) {
+ PropIdAreaId propIdAreaId = {
+ .propId = toInt(VehicleProperty::PERF_VEHICLE_SPEED),
+ .areaId = 0,
+ };
+
+ ASSERT_EQ(propIdAreaId.toString(), "{propId: PERF_VEHICLE_SPEED, areaId: 0}");
+}
+
+TEST(VehicleUtilsTest, testPropIdAreaIdFormatter) {
+ PropIdAreaId propIdAreaId1 = {
+ .propId = toInt(VehicleProperty::PERF_VEHICLE_SPEED),
+ .areaId = 0,
+ };
+ PropIdAreaId propIdAreaId2 = {
+ .propId = toInt(VehicleProperty::HVAC_FAN_SPEED),
+ .areaId = 1,
+ };
+
+ ASSERT_EQ(fmt::format("{}", std::vector<PropIdAreaId>{propIdAreaId1, propIdAreaId2}),
+ "[{propId: PERF_VEHICLE_SPEED, areaId: 0}, {propId: HVAC_FAN_SPEED, areaId: 1}]");
+}
+
class InvalidPropValueTest : public testing::TestWithParam<InvalidPropValueTestCase> {};
INSTANTIATE_TEST_SUITE_P(InvalidPropValueTests, InvalidPropValueTest,