Merge changes I52651729,Ice39f059,I5e29e971,I8d32d1eb into main
* changes:
Support VUR in FakeVehicleHardware.
Override subscribe/unsubscribe.
Avoid holding lock while calling callback.
Add subscribe/unsubscribe to IVehicleHardware.
diff --git a/automotive/vehicle/aidl/impl/default_config/JsonConfigLoader/src/JsonConfigLoader.cpp b/automotive/vehicle/aidl/impl/default_config/JsonConfigLoader/src/JsonConfigLoader.cpp
index 39ce10e..82dc8a6 100644
--- a/automotive/vehicle/aidl/impl/default_config/JsonConfigLoader/src/JsonConfigLoader.cpp
+++ b/automotive/vehicle/aidl/impl/default_config/JsonConfigLoader/src/JsonConfigLoader.cpp
@@ -275,6 +275,16 @@
}
template <>
+Result<bool> JsonValueParser::convertValueToType<bool>(const std::string& fieldName,
+ const Json::Value& value) {
+ if (!value.isBool()) {
+ return Error() << "The value: " << value << " for field: " << fieldName
+ << " is not in correct type, expect bool";
+ }
+ return value.asBool();
+}
+
+template <>
Result<int32_t> JsonValueParser::convertValueToType<int32_t>(const std::string& fieldName,
const Json::Value& value) {
if (!value.isInt()) {
@@ -531,6 +541,12 @@
tryParseJsonValueToVariable(jsonAreaConfig, "maxFloatValue", /*optional=*/true,
&areaConfig.maxFloatValue, errors);
+ // By default we support variable update rate for all properties except it is explicitly
+ // disabled.
+ areaConfig.supportVariableUpdateRate = true;
+ tryParseJsonValueToVariable(jsonAreaConfig, "supportVariableUpdateRate", /*optional=*/true,
+ &areaConfig.supportVariableUpdateRate, errors);
+
std::vector<int64_t> supportedEnumValues;
tryParseJsonArrayToVariable(jsonAreaConfig, "supportedEnumValues", /*optional=*/true,
&supportedEnumValues, errors);
@@ -585,6 +601,16 @@
if (errors->size() != initialErrorCount) {
return std::nullopt;
}
+
+ // If there is no area config, by default we allow variable update rate, so we have to add
+ // a global area config.
+ if (configDecl.config.areaConfigs.size() == 0) {
+ VehicleAreaConfig areaConfig = {
+ .areaId = 0,
+ .supportVariableUpdateRate = true,
+ };
+ configDecl.config.areaConfigs.push_back(std::move(areaConfig));
+ }
return configDecl;
}
diff --git a/automotive/vehicle/aidl/impl/default_config/config/DefaultProperties.json b/automotive/vehicle/aidl/impl/default_config/config/DefaultProperties.json
index 6c8d59c..d3bb60c 100644
--- a/automotive/vehicle/aidl/impl/default_config/config/DefaultProperties.json
+++ b/automotive/vehicle/aidl/impl/default_config/config/DefaultProperties.json
@@ -3579,7 +3579,13 @@
"property": "VehicleProperty::WATCHDOG_TERMINATED_PROCESS"
},
{
- "property": "VehicleProperty::VHAL_HEARTBEAT"
+ "property": "VehicleProperty::VHAL_HEARTBEAT",
+ "areas": [
+ {
+ "areaId": 0,
+ "supportVariableUpdateRate": false
+ }
+ ]
},
{
"property": "VehicleProperty::CLUSTER_SWITCH_UI",
@@ -3641,6 +3647,12 @@
0,
16
],
+ "areas": [
+ {
+ "areaId": 0,
+ "supportVariableUpdateRate": false
+ }
+ ],
"comment": "configArray specifies it consists of int64[2] and byte[16]."
},
{
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h b/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
index 844bea5..718f68e 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/include/FakeVehicleHardware.h
@@ -91,9 +91,13 @@
void registerOnPropertySetErrorEvent(
std::unique_ptr<const PropertySetErrorCallback> callback) override;
- // Update the sample rate for the [propId, areaId] pair.
- aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate(
- int32_t propId, int32_t areaId, float sampleRate) override;
+ // Subscribe to a new [propId, areaId] or change the update rate.
+ aidl::android::hardware::automotive::vehicle::StatusCode subscribe(
+ aidl::android::hardware::automotive::vehicle::SubscribeOptions options) override;
+
+ // Unsubscribe to a [propId, areaId].
+ aidl::android::hardware::automotive::vehicle::StatusCode unsubscribe(int32_t propId,
+ int32_t areaId) override;
protected:
// mValuePool is also used in mServerSidePropStore.
@@ -154,6 +158,7 @@
mRecurrentActions GUARDED_BY(mLock);
std::unordered_map<PropIdAreaId, VehiclePropValuePool::RecyclableType, PropIdAreaIdHash>
mSavedProps GUARDED_BY(mLock);
+ std::unordered_set<PropIdAreaId, PropIdAreaIdHash> mSubOnChangePropIdAreaIds GUARDED_BY(mLock);
// PendingRequestHandler is thread-safe.
mutable PendingRequestHandler<GetValuesCallback,
aidl::android::hardware::automotive::vehicle::GetValueRequest>
@@ -176,7 +181,8 @@
void storePropInitialValue(const ConfigDeclaration& config);
// The callback that would be called when a vehicle property value change happens.
void onValueChangeCallback(
- const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
+ const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value)
+ EXCLUDES(mLock);
// Load the config files in format '*.json' from the directory and parse the config files
// into a map from property ID to ConfigDeclarations.
void loadPropConfigsFromDir(const std::string& dirPath,
@@ -262,6 +268,11 @@
void generateVendorConfigs(
std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig>&) const;
+ aidl::android::hardware::automotive::vehicle::StatusCode subscribePropIdAreaIdLocked(
+ int32_t propId, int32_t areaId, float sampleRateHz, bool enableVariableUpdateRate,
+ const aidl::android::hardware::automotive::vehicle::VehiclePropConfig&
+ vehiclePropConfig) REQUIRES(mLock);
+
static aidl::android::hardware::automotive::vehicle::VehiclePropValue createHwInputKeyProp(
aidl::android::hardware::automotive::vehicle::VehicleHwKeyInputAction action,
int32_t keyCode, int32_t targetDisplay);
@@ -275,6 +286,10 @@
static std::string genFakeDataHelp();
static std::string parseErrMsg(std::string fieldName, std::string value, std::string type);
+ static bool isVariableUpdateRateSupported(
+ const aidl::android::hardware::automotive::vehicle::VehiclePropConfig&
+ vehiclePropConfig,
+ int32_t areaId);
};
} // namespace fake
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
index ee24fbd..cb8e51f 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/src/FakeVehicleHardware.cpp
@@ -60,6 +60,8 @@
using ::aidl::android::hardware::automotive::vehicle::SetValueRequest;
using ::aidl::android::hardware::automotive::vehicle::SetValueResult;
using ::aidl::android::hardware::automotive::vehicle::StatusCode;
+using ::aidl::android::hardware::automotive::vehicle::SubscribeOptions;
+using ::aidl::android::hardware::automotive::vehicle::toString;
using ::aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReport;
using ::aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReq;
using ::aidl::android::hardware::automotive::vehicle::VehicleArea;
@@ -67,6 +69,7 @@
using ::aidl::android::hardware::automotive::vehicle::VehiclePropConfig;
using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyAccess;
+using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyChangeMode;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyGroup;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyStatus;
using ::aidl::android::hardware::automotive::vehicle::VehiclePropertyType;
@@ -1926,43 +1929,109 @@
mOnPropertySetErrorCallback = std::move(callback);
}
-StatusCode FakeVehicleHardware::updateSampleRate(int32_t propId, int32_t areaId, float sampleRate) {
- // DefaultVehicleHal makes sure that sampleRate must be within minSampleRate and maxSampleRate.
- // For fake implementation, we would write the same value with a new timestamp into propStore
- // at sample rate.
- std::scoped_lock<std::mutex> lockGuard(mLock);
+StatusCode FakeVehicleHardware::subscribe(SubscribeOptions options) {
+ int32_t propId = options.propId;
+ auto configResult = mServerSidePropStore->getConfig(propId);
+ if (!configResult.ok()) {
+ ALOGE("subscribe: property: %" PRId32 " is not supported", propId);
+ return StatusCode::INVALID_ARG;
+ }
+
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ for (int areaId : options.areaIds) {
+ if (StatusCode status = subscribePropIdAreaIdLocked(propId, areaId, options.sampleRate,
+ options.enableVariableUpdateRate,
+ *configResult.value());
+ status != StatusCode::OK) {
+ return status;
+ }
+ }
+ return StatusCode::OK;
+}
+
+bool FakeVehicleHardware::isVariableUpdateRateSupported(const VehiclePropConfig& vehiclePropConfig,
+ int32_t areaId) {
+ for (size_t i = 0; i < vehiclePropConfig.areaConfigs.size(); i++) {
+ const auto& areaConfig = vehiclePropConfig.areaConfigs[i];
+ if (areaConfig.areaId != areaId) {
+ continue;
+ }
+ if (areaConfig.supportVariableUpdateRate) {
+ return true;
+ }
+ break;
+ }
+ return false;
+}
+
+StatusCode FakeVehicleHardware::subscribePropIdAreaIdLocked(
+ int32_t propId, int32_t areaId, float sampleRateHz, bool enableVariableUpdateRate,
+ const VehiclePropConfig& vehiclePropConfig) {
+ PropIdAreaId propIdAreaId{
+ .propId = propId,
+ .areaId = areaId,
+ };
+ switch (vehiclePropConfig.changeMode) {
+ case VehiclePropertyChangeMode::STATIC:
+ ALOGW("subscribe to a static property, do nothing.");
+ return StatusCode::OK;
+ case VehiclePropertyChangeMode::ON_CHANGE:
+ mSubOnChangePropIdAreaIds.insert(std::move(propIdAreaId));
+ return StatusCode::OK;
+ case VehiclePropertyChangeMode::CONTINUOUS:
+ if (sampleRateHz == 0.f) {
+ ALOGE("Must not use sample rate 0 for a continuous property");
+ return StatusCode::INTERNAL_ERROR;
+ }
+ if (mRecurrentActions.find(propIdAreaId) != mRecurrentActions.end()) {
+ mRecurrentTimer->unregisterTimerCallback(mRecurrentActions[propIdAreaId]);
+ }
+ int64_t intervalInNanos = static_cast<int64_t>(1'000'000'000. / sampleRateHz);
+
+ // For continuous properties, we must generate a new onPropertyChange event
+ // periodically according to the sample rate.
+ auto eventMode = VehiclePropertyStore::EventMode::ALWAYS;
+ if (isVariableUpdateRateSupported(vehiclePropConfig, areaId) &&
+ enableVariableUpdateRate) {
+ eventMode = VehiclePropertyStore::EventMode::ON_VALUE_CHANGE;
+ }
+ auto action = std::make_shared<RecurrentTimer::Callback>([this, propId, areaId,
+ eventMode] {
+ // Refresh the property value. In real implementation, this should poll the latest
+ // value from vehicle bus. Here, we are just refreshing the existing value with a
+ // new timestamp.
+ auto result = getValue(VehiclePropValue{
+ .areaId = areaId,
+ .prop = propId,
+ .value = {},
+ });
+ if (!result.ok()) {
+ // Failed to read current value, skip refreshing.
+ return;
+ }
+ result.value()->timestamp = elapsedRealtimeNano();
+
+ mServerSidePropStore->writeValue(std::move(result.value()), /*updateStatus=*/true,
+ eventMode);
+ });
+ mRecurrentTimer->registerTimerCallback(intervalInNanos, action);
+ mRecurrentActions[propIdAreaId] = action;
+ return StatusCode::OK;
+ }
+}
+
+StatusCode FakeVehicleHardware::unsubscribe(int32_t propId, int32_t areaId) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
PropIdAreaId propIdAreaId{
.propId = propId,
.areaId = areaId,
};
if (mRecurrentActions.find(propIdAreaId) != mRecurrentActions.end()) {
mRecurrentTimer->unregisterTimerCallback(mRecurrentActions[propIdAreaId]);
+ mRecurrentActions.erase(propIdAreaId);
}
- if (sampleRate == 0) {
- return StatusCode::OK;
- }
- int64_t interval = static_cast<int64_t>(1'000'000'000. / sampleRate);
- auto action = std::make_shared<RecurrentTimer::Callback>([this, propId, areaId] {
- // Refresh the property value. In real implementation, this should poll the latest value
- // from vehicle bus. Here, we are just refreshing the existing value with a new timestamp.
- auto result = getValue(VehiclePropValue{
- .areaId = areaId,
- .prop = propId,
- .value = {},
- });
- if (!result.ok()) {
- // Failed to read current value, skip refreshing.
- return;
- }
- result.value()->timestamp = elapsedRealtimeNano();
- // For continuous properties, we must generate a new onPropertyChange event periodically
- // according to the sample rate.
- mServerSidePropStore->writeValue(std::move(result.value()), /*updateStatus=*/true,
- VehiclePropertyStore::EventMode::ALWAYS);
- });
- mRecurrentTimer->registerTimerCallback(interval, action);
- mRecurrentActions[propIdAreaId] = action;
+ mSubOnChangePropIdAreaIds.erase(propIdAreaId);
return StatusCode::OK;
}
@@ -1971,6 +2040,23 @@
return;
}
+ PropIdAreaId propIdAreaId{
+ .propId = value.prop,
+ .areaId = value.areaId,
+ };
+
+ {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ if (mRecurrentActions.find(propIdAreaId) == mRecurrentActions.end() &&
+ mSubOnChangePropIdAreaIds.find(propIdAreaId) == mSubOnChangePropIdAreaIds.end()) {
+ if (FAKE_VEHICLEHARDWARE_DEBUG) {
+ ALOGD("The updated property value: %s is not subscribed, ignore",
+ value.toString().c_str());
+ }
+ return;
+ }
+ }
+
std::vector<VehiclePropValue> updatedValues;
updatedValues.push_back(value);
(*mOnPropertyChangeCallback)(std::move(updatedValues));
diff --git a/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp b/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp
index cf9beee..0432500 100644
--- a/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp
+++ b/automotive/vehicle/aidl/impl/fake_impl/hardware/test/FakeVehicleHardwareTest.cpp
@@ -72,6 +72,7 @@
using ::aidl::android::hardware::automotive::vehicle::SetValueRequest;
using ::aidl::android::hardware::automotive::vehicle::SetValueResult;
using ::aidl::android::hardware::automotive::vehicle::StatusCode;
+using ::aidl::android::hardware::automotive::vehicle::SubscribeOptions;
using ::aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReport;
using ::aidl::android::hardware::automotive::vehicle::VehicleApPowerStateReq;
using ::aidl::android::hardware::automotive::vehicle::VehicleApPowerStateShutdownParam;
@@ -94,6 +95,7 @@
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::IsSubsetOf;
+using ::testing::UnorderedElementsAre;
using ::testing::WhenSortedBy;
using std::chrono::milliseconds;
@@ -149,6 +151,15 @@
mHardware = std::move(hardware);
}
+ static SubscribeOptions newSubscribeOptions(int32_t propId, int32_t areaId,
+ float sampleRateHz) {
+ SubscribeOptions options;
+ options.areaIds = {areaId};
+ options.propId = propId;
+ options.sampleRate = sampleRateHz;
+ return options;
+ }
+
StatusCode setValues(const std::vector<SetValueRequest>& requests) {
{
std::scoped_lock<std::mutex> lockGuard(mLock);
@@ -336,6 +347,13 @@
return mEventCount[propIdAreaId];
}
+ void subscribe(int32_t propId, int32_t areaId, float sampleRateHz) {
+ ASSERT_EQ(StatusCode::OK,
+ getHardware()->subscribe(newSubscribeOptions(propId, areaId, sampleRateHz)))
+ << "failed to subscribe to propId: " << propId << "areaId: " << areaId
+ << ", sampleRateHz: " << sampleRateHz;
+ }
+
static void addSetValueRequest(std::vector<SetValueRequest>& requests,
std::vector<SetValueResult>& expectedResults, int64_t requestId,
const VehiclePropValue& value, StatusCode expectedStatus) {
@@ -370,24 +388,24 @@
}
std::vector<VehiclePropValue> getTestPropValues() {
- VehiclePropValue fuelCapacity = {
- .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY),
- .value = {.floatValues = {1.0}},
+ VehiclePropValue oilLevel = {
+ .prop = toInt(VehicleProperty::ENGINE_OIL_LEVEL),
+ .value = {.int32Values = {1}},
};
- VehiclePropValue leftTirePressure = {
- .prop = toInt(VehicleProperty::TIRE_PRESSURE),
+ VehiclePropValue leftHvacTemp = {
+ .prop = toInt(VehicleProperty::HVAC_TEMPERATURE_CURRENT),
.value = {.floatValues = {170.0}},
- .areaId = WHEEL_FRONT_LEFT,
+ .areaId = SEAT_1_LEFT,
};
- VehiclePropValue rightTirePressure = {
- .prop = toInt(VehicleProperty::TIRE_PRESSURE),
+ VehiclePropValue rightHvacTemp = {
+ .prop = toInt(VehicleProperty::HVAC_TEMPERATURE_CURRENT),
.value = {.floatValues = {180.0}},
- .areaId = WHEEL_FRONT_RIGHT,
+ .areaId = SEAT_1_RIGHT,
};
- return {fuelCapacity, leftTirePressure, rightTirePressure};
+ return {oilLevel, leftHvacTemp, rightHvacTemp};
}
struct PropValueCmp {
@@ -437,6 +455,29 @@
ASSERT_EQ(configs.size(), helper.loadConfigDeclarations().size());
}
+TEST_F(FakeVehicleHardwareTest, testGetAllPropertyConfigs_defaultSupportVUR) {
+ std::vector<VehiclePropConfig> configs = getHardware()->getAllPropertyConfigs();
+
+ for (const auto& config : configs) {
+ bool expectedSupportVUR = true;
+ if (config.prop == toInt(VehicleProperty::VHAL_HEARTBEAT) ||
+ config.prop == toInt(VehicleProperty::CLUSTER_HEARTBEAT)) {
+ expectedSupportVUR = false;
+ }
+ EXPECT_GE(config.areaConfigs.size(), 1u)
+ << "expect at least one area config, including global area config, propId: "
+ << config.prop;
+ if (config.areaConfigs.size() == 0) {
+ continue;
+ }
+ for (const auto& areaConfig : config.areaConfigs) {
+ EXPECT_EQ(areaConfig.supportVariableUpdateRate, expectedSupportVUR)
+ << "unexpected supportVariableUpdateRate for propId: " << config.prop
+ << ", areaId: " << areaConfig.areaId;
+ }
+ }
+}
+
TEST_F(FakeVehicleHardwareTest, testGetDefaultValues) {
std::vector<GetValueRequest> getValueRequests;
std::vector<GetValueResult> expectedGetValueResults;
@@ -559,17 +600,13 @@
ASSERT_THAT(getSetValueResults(), ContainerEq(expectedResults));
}
-TEST_F(FakeVehicleHardwareTest, testRegisterOnPropertyChangeEvent) {
- // We have already registered this callback in Setup, here we are registering again.
- auto callback = std::make_unique<IVehicleHardware::PropertyChangeCallback>(
- [this](const std::vector<VehiclePropValue>& values) { onPropertyChangeEvent(values); });
- getHardware()->registerOnPropertyChangeEvent(std::move(callback));
-
+TEST_F(FakeVehicleHardwareTest, testSetValues_getUpdateEvents) {
auto testValues = getTestPropValues();
std::vector<SetValueRequest> requests;
std::vector<SetValueResult> expectedResults;
int64_t requestId = 1;
for (auto& value : testValues) {
+ subscribe(value.prop, value.areaId, /*sampleRateHz=*/0);
addSetValueRequest(requests, expectedResults, requestId++, value, StatusCode::OK);
}
int64_t timestamp = elapsedRealtimeNano();
@@ -1624,27 +1661,30 @@
return info.param.name;
});
-TEST_F(FakeVehicleHardwareTest, testSetWaitForVhalAfterCarServiceCrash) {
- int32_t propId = toInt(VehicleProperty::AP_POWER_STATE_REPORT);
+TEST_F(FakeVehicleHardwareTest, testSetWaitForVhal_alwaysTriggerEvents) {
+ int32_t powerReq = toInt(VehicleProperty::AP_POWER_STATE_REQ);
+ subscribe(powerReq, /*areaId*/ 0, /*sampleRateHz*/ 0);
+
+ int32_t powerReport = toInt(VehicleProperty::AP_POWER_STATE_REPORT);
VehiclePropValue request = VehiclePropValue{
- .prop = propId,
+ .prop = powerReport,
.value.int32Values = {toInt(VehicleApPowerStateReport::WAIT_FOR_VHAL)},
};
- ASSERT_EQ(setValue(request), StatusCode::OK) << "failed to set property " << propId;
+ ASSERT_EQ(setValue(request), StatusCode::OK) << "failed to set property " << powerReport;
// Clear existing events.
clearChangedProperties();
// Simulate a Car Service crash, Car Service would restart and send the message again.
- ASSERT_EQ(setValue(request), StatusCode::OK) << "failed to set property " << propId;
+ ASSERT_EQ(setValue(request), StatusCode::OK) << "failed to set property " << powerReport;
std::vector<VehiclePropValue> events = getChangedProperties();
// Even though the state is already ON, we should receive another ON event.
- ASSERT_EQ(events.size(), 1u);
+ ASSERT_EQ(events.size(), 1u) << "failed to receive on-change events AP_POWER_STATE_REQ ON";
// Erase the timestamp for comparison.
events[0].timestamp = 0;
auto expectedValue = VehiclePropValue{
- .prop = toInt(VehicleProperty::AP_POWER_STATE_REQ),
+ .prop = powerReq,
.status = VehiclePropertyStatus::AVAILABLE,
.value.int32Values = {toInt(VehicleApPowerStateReq::ON), 0},
};
@@ -2015,6 +2055,22 @@
},
},
};
+
+ // First subscribe to all the properties that we will change.
+ for (auto& enabledToErrorStateProps : adasEnabledPropToAdasPropWithErrorState) {
+ std::unordered_set<int32_t> expectedChangedPropIds(enabledToErrorStateProps.second.begin(),
+ enabledToErrorStateProps.second.end());
+ expectedChangedPropIds.insert(enabledToErrorStateProps.first);
+
+ for (int32_t propId : expectedChangedPropIds) {
+ int32_t areaId = 0;
+ if (propId == toInt(VehicleProperty::BLIND_SPOT_WARNING_STATE)) {
+ areaId = toInt(VehicleAreaMirror::DRIVER_LEFT);
+ }
+ subscribe(propId, areaId, /*sampleRateHz*/ 0);
+ }
+ }
+
for (auto& enabledToErrorStateProps : adasEnabledPropToAdasPropWithErrorState) {
int32_t adasEnabledPropertyId = enabledToErrorStateProps.first;
StatusCode status =
@@ -2095,9 +2151,16 @@
}
TEST_F(FakeVehicleHardwareTest, testSwitchUser) {
+ SubscribeOptions options;
+ int32_t propSwitchUser = toInt(VehicleProperty::SWITCH_USER);
+ options.propId = propSwitchUser;
+ options.areaIds = {0, 1};
+ ASSERT_EQ(StatusCode::OK, getHardware()->subscribe(options))
+ << "failed to subscribe to propId: " << propSwitchUser;
+
// This is the same example as used in User HAL Emulation doc.
VehiclePropValue valueToSet = {
- .prop = toInt(VehicleProperty::SWITCH_USER),
+ .prop = propSwitchUser,
.areaId = 1,
.value.int32Values = {666, 3, 2},
};
@@ -2108,7 +2171,7 @@
// Simulate a request from Android side.
VehiclePropValue switchUserRequest = {
- .prop = toInt(VehicleProperty::SWITCH_USER),
+ .prop = propSwitchUser,
.areaId = 0,
.value.int32Values = {666, 3},
};
@@ -2138,7 +2201,7 @@
events[0].timestamp = 0;
auto expectedValue = VehiclePropValue{
.areaId = 0,
- .prop = toInt(VehicleProperty::SWITCH_USER),
+ .prop = propSwitchUser,
.value.int32Values =
{
// Request ID
@@ -2153,6 +2216,13 @@
}
TEST_F(FakeVehicleHardwareTest, testCreateUser) {
+ SubscribeOptions options;
+ int32_t propCreateUser = toInt(VehicleProperty::CREATE_USER);
+ options.propId = propCreateUser;
+ options.areaIds = {0, 1};
+ ASSERT_EQ(StatusCode::OK, getHardware()->subscribe(options))
+ << "failed to subscribe to propId: " << propCreateUser;
+
// This is the same example as used in User HAL Emulation doc.
VehiclePropValue valueToSet = {
.prop = toInt(VehicleProperty::CREATE_USER),
@@ -2166,7 +2236,7 @@
// Simulate a request from Android side.
VehiclePropValue createUserRequest = {
- .prop = toInt(VehicleProperty::CREATE_USER),
+ .prop = propCreateUser,
.areaId = 0,
.value.int32Values = {666},
};
@@ -2195,7 +2265,7 @@
events[0].timestamp = 0;
auto expectedValue = VehiclePropValue{
.areaId = 0,
- .prop = toInt(VehicleProperty::CREATE_USER),
+ .prop = propCreateUser,
.value.int32Values =
{
// Request ID
@@ -2208,9 +2278,16 @@
}
TEST_F(FakeVehicleHardwareTest, testInitialUserInfo) {
+ SubscribeOptions options;
+ int32_t propInitialUserInfo = toInt(VehicleProperty::INITIAL_USER_INFO);
+ options.propId = propInitialUserInfo;
+ options.areaIds = {0, 1};
+ ASSERT_EQ(StatusCode::OK, getHardware()->subscribe(options))
+ << "failed to subscribe to propId: " << propInitialUserInfo;
+
// This is the same example as used in User HAL Emulation doc.
VehiclePropValue valueToSet = {
- .prop = toInt(VehicleProperty::INITIAL_USER_INFO),
+ .prop = propInitialUserInfo,
.areaId = 1,
.value.int32Values = {666, 1, 11},
};
@@ -2221,7 +2298,7 @@
// Simulate a request from Android side.
VehiclePropValue initialUserInfoRequest = {
- .prop = toInt(VehicleProperty::INITIAL_USER_INFO),
+ .prop = propInitialUserInfo,
.areaId = 0,
.value.int32Values = {3},
};
@@ -2238,7 +2315,7 @@
events[0].timestamp = 0;
auto expectedValue = VehiclePropValue{
.areaId = 0,
- .prop = toInt(VehicleProperty::INITIAL_USER_INFO),
+ .prop = propInitialUserInfo,
.value.int32Values = {3, 1, 11},
};
EXPECT_EQ(events[0], expectedValue);
@@ -2253,7 +2330,7 @@
events[0].timestamp = 0;
expectedValue = VehiclePropValue{
.areaId = 0,
- .prop = toInt(VehicleProperty::INITIAL_USER_INFO),
+ .prop = propInitialUserInfo,
.value.int32Values =
{
// Request ID
@@ -2395,13 +2472,14 @@
}
TEST_F(FakeVehicleHardwareTest, testDumpInjectEvent) {
- int32_t prop = toInt(VehicleProperty::PERF_VEHICLE_SPEED);
+ int32_t prop = toInt(VehicleProperty::ENGINE_OIL_LEVEL);
std::string propIdStr = std::to_string(prop);
+ subscribe(prop, /*areaId*/ 0, /*sampleRateHz*/ 0);
+
int64_t timestamp = elapsedRealtimeNano();
- // Inject an event with float value 123.4 and timestamp.
DumpResult result = getHardware()->dump(
- {"--inject-event", propIdStr, "-f", "123.4", "-t", std::to_string(timestamp)});
+ {"--inject-event", propIdStr, "-i", "1234", "-t", std::to_string(timestamp)});
ASSERT_FALSE(result.callerShouldDumpState);
ASSERT_THAT(result.buffer,
@@ -2412,7 +2490,7 @@
ASSERT_EQ(events.size(), 1u);
auto event = events[0];
ASSERT_EQ(event.timestamp, timestamp);
- ASSERT_EQ(event.value.floatValues, std::vector<float>({123.4}));
+ ASSERT_EQ(event.value.int32Values, std::vector<int32_t>({1234}));
}
TEST_F(FakeVehicleHardwareTest, testDumpInvalidOptions) {
@@ -2755,9 +2833,13 @@
});
TEST_F(FakeVehicleHardwareTest, testDebugGenFakeDataLinear) {
- // Start a fake linear data generator for vehicle speed at 0.1s interval.
+ // Start a fake linear data generator for engine oil level at 0.1s interval.
// range: 0 - 100, current value: 30, step: 20.
- std::string propIdString = StringPrintf("%d", toInt(VehicleProperty::PERF_VEHICLE_SPEED));
+ int32_t prop = toInt(VehicleProperty::ENGINE_OIL_LEVEL);
+
+ subscribe(prop, /*areaId*/ 0, /*sampleRateHz*/ 0);
+
+ std::string propIdString = StringPrintf("%d", prop);
std::vector<std::string> options = {"--genfakedata", "--startlinear", propIdString,
/*middleValue=*/"50",
/*currentValue=*/"30",
@@ -2770,15 +2852,14 @@
ASSERT_FALSE(result.callerShouldDumpState);
ASSERT_THAT(result.buffer, HasSubstr("successfully"));
- ASSERT_TRUE(waitForChangedProperties(toInt(VehicleProperty::PERF_VEHICLE_SPEED), 0, /*count=*/5,
- milliseconds(1000)))
+ ASSERT_TRUE(waitForChangedProperties(prop, 0, /*count=*/5, milliseconds(1000)))
<< "not enough events generated for linear data generator";
int32_t value = 30;
auto events = getChangedProperties();
for (size_t i = 0; i < 5; i++) {
- ASSERT_EQ(1u, events[i].value.floatValues.size());
- EXPECT_EQ(static_cast<float>(value), events[i].value.floatValues[0]);
+ ASSERT_EQ(1u, events[i].value.int32Values.size());
+ EXPECT_EQ(value, events[i].value.int32Values[0]);
value = (value + 20) % 100;
}
@@ -2794,7 +2875,7 @@
std::this_thread::sleep_for(std::chrono::milliseconds(200));
// There should be no new events generated.
- EXPECT_EQ(0u, getEventCount(toInt(VehicleProperty::PERF_VEHICLE_SPEED), 0));
+ EXPECT_EQ(0u, getEventCount(prop, 0));
}
std::string getTestFilePath(const char* filename) {
@@ -2803,6 +2884,8 @@
}
TEST_F(FakeVehicleHardwareTest, testDebugGenFakeDataJson) {
+ subscribe(toInt(VehicleProperty::GEAR_SELECTION), /*areaId*/ 0, /*sampleRateHz*/ 0);
+
std::vector<std::string> options = {"--genfakedata", "--startjson", "--path",
getTestFilePath("prop.json"), "2"};
@@ -2829,6 +2912,8 @@
}
TEST_F(FakeVehicleHardwareTest, testDebugGenFakeDataJsonByContent) {
+ subscribe(toInt(VehicleProperty::GEAR_SELECTION), /*areaId*/ 0, /*sampleRateHz*/ 0);
+
std::vector<std::string> options = {
"--genfakedata", "--startjson", "--content",
"[{\"timestamp\":1000000,\"areaId\":0,\"value\":8,\"prop\":289408000}]", "1"};
@@ -2903,8 +2988,11 @@
}
TEST_F(FakeVehicleHardwareTest, testDebugGenFakeDataKeyPress) {
+ int32_t propHwKeyInput = toInt(VehicleProperty::HW_KEY_INPUT);
std::vector<std::string> options = {"--genfakedata", "--keypress", "1", "2"};
+ subscribe(propHwKeyInput, /*areaId*/ 0, /*sampleRateHz*/ 0);
+
DumpResult result = getHardware()->dump(options);
ASSERT_FALSE(result.callerShouldDumpState);
@@ -2912,8 +3000,8 @@
auto events = getChangedProperties();
ASSERT_EQ(2u, events.size());
- EXPECT_EQ(toInt(VehicleProperty::HW_KEY_INPUT), events[0].prop);
- EXPECT_EQ(toInt(VehicleProperty::HW_KEY_INPUT), events[1].prop);
+ EXPECT_EQ(propHwKeyInput, events[0].prop);
+ EXPECT_EQ(propHwKeyInput, events[1].prop);
ASSERT_EQ(3u, events[0].value.int32Values.size());
ASSERT_EQ(3u, events[1].value.int32Values.size());
EXPECT_EQ(toInt(VehicleHwKeyInputAction::ACTION_DOWN), events[0].value.int32Values[0]);
@@ -2925,8 +3013,11 @@
}
TEST_F(FakeVehicleHardwareTest, testDebugGenFakeDataKeyInputV2) {
+ int32_t propHwKeyInputV2 = toInt(VehicleProperty::HW_KEY_INPUT_V2);
std::vector<std::string> options = {"--genfakedata", "--keyinputv2", "1", "2", "3", "4", "5"};
+ subscribe(propHwKeyInputV2, /*areaId*/ 1, /*sampleRateHz*/ 0);
+
DumpResult result = getHardware()->dump(options);
ASSERT_FALSE(result.callerShouldDumpState);
@@ -2944,6 +3035,7 @@
}
TEST_F(FakeVehicleHardwareTest, testDebugGenFakeDataMotionInput) {
+ int32_t propHwMotionInput = toInt(VehicleProperty::HW_MOTION_INPUT);
std::vector<std::string> options = {"--genfakedata",
"--motioninput",
"1",
@@ -2966,6 +3058,8 @@
"65.5",
"76.6"};
+ subscribe(propHwMotionInput, /*areaId*/ 1, /*sampleRateHz*/ 0);
+
DumpResult result = getHardware()->dump(options);
ASSERT_FALSE(result.callerShouldDumpState);
@@ -2973,7 +3067,7 @@
auto events = getChangedProperties();
ASSERT_EQ(1u, events.size());
- EXPECT_EQ(toInt(VehicleProperty::HW_MOTION_INPUT), events[0].prop);
+ EXPECT_EQ(propHwMotionInput, events[0].prop);
ASSERT_EQ(9u, events[0].value.int32Values.size());
EXPECT_EQ(2, events[0].value.int32Values[0]);
EXPECT_EQ(3, events[0].value.int32Values[1]);
@@ -3014,23 +3108,27 @@
ASSERT_EQ(result.value().value.byteValues, std::vector<uint8_t>({0x04, 0x03, 0x02, 0x01}));
}
-TEST_F(FakeVehicleHardwareTest, testUpdateSampleRate) {
+TEST_F(FakeVehicleHardwareTest, testSubscribeUnsubscribe_continuous) {
int32_t propSpeed = toInt(VehicleProperty::PERF_VEHICLE_SPEED);
int32_t propSteering = toInt(VehicleProperty::PERF_STEERING_ANGLE);
int32_t areaId = 0;
- getHardware()->updateSampleRate(propSpeed, areaId, 5);
+
+ auto status = getHardware()->subscribe(newSubscribeOptions(propSpeed, areaId, 5));
+ ASSERT_EQ(status, StatusCode::OK) << "failed to subscribe";
ASSERT_TRUE(waitForChangedProperties(propSpeed, areaId, /*count=*/5, milliseconds(1500)))
<< "not enough events generated for speed";
- getHardware()->updateSampleRate(propSteering, areaId, 10);
+ status = getHardware()->subscribe(newSubscribeOptions(propSteering, areaId, 10));
+ ASSERT_EQ(status, StatusCode::OK) << "failed to subscribe";
ASSERT_TRUE(waitForChangedProperties(propSteering, areaId, /*count=*/10, milliseconds(1500)))
<< "not enough events generated for steering";
int64_t timestamp = elapsedRealtimeNano();
// Disable refreshing for propSpeed.
- getHardware()->updateSampleRate(propSpeed, areaId, 0);
+ status = getHardware()->unsubscribe(propSpeed, areaId);
+ ASSERT_EQ(status, StatusCode::OK) << "failed to unsubscribe";
clearChangedProperties();
ASSERT_TRUE(waitForChangedProperties(propSteering, areaId, /*count=*/5, milliseconds(1500)))
@@ -3043,12 +3141,99 @@
}
}
+TEST_F(FakeVehicleHardwareTest, testSubscribe_enableVUR) {
+ int32_t propSpeed = toInt(VehicleProperty::PERF_VEHICLE_SPEED);
+ int32_t areaId = 0;
+ SubscribeOptions options;
+ options.propId = propSpeed;
+ options.areaIds = {areaId};
+ options.enableVariableUpdateRate = true;
+ options.sampleRate = 5;
+ int64_t timestamp = elapsedRealtimeNano();
+
+ auto status = getHardware()->subscribe(options);
+ ASSERT_EQ(status, StatusCode::OK) << "failed to subscribe";
+
+ status = setValue({
+ .prop = propSpeed,
+ .areaId = 0,
+ .value.floatValues = {1.1f},
+ });
+ ASSERT_EQ(status, StatusCode::OK) << "failed to set speed";
+
+ status = setValue({
+ .prop = propSpeed,
+ .areaId = 0,
+ .value.floatValues = {1.2f},
+ });
+ ASSERT_EQ(status, StatusCode::OK) << "failed to set speed";
+
+ ASSERT_TRUE(waitForChangedProperties(propSpeed, areaId, /*count=*/2, milliseconds(100)))
+ << "not enough events generated for speed";
+ auto updatedValues = getChangedProperties();
+ std::unordered_set<float> gotValues;
+ for (auto& value : updatedValues) {
+ EXPECT_GE(value.timestamp, timestamp) << "timestamp must be updated";
+ EXPECT_EQ(value.prop, propSpeed) << "propId must be correct";
+ EXPECT_EQ(value.areaId, areaId) << "areaId must be correct";
+ gotValues.insert(value.value.floatValues[0]);
+ }
+ EXPECT_THAT(gotValues, UnorderedElementsAre(1.1f, 1.2f))
+ << "must only receive property event for changed value";
+}
+
+TEST_F(FakeVehicleHardwareTest, testSubscribeUnusubscribe_onChange) {
+ int32_t propHvac = toInt(VehicleProperty::HVAC_TEMPERATURE_SET);
+ int32_t areaId = SEAT_1_LEFT;
+
+ auto status = getHardware()->subscribe(newSubscribeOptions(propHvac, areaId, 0));
+ ASSERT_EQ(status, StatusCode::OK) << "failed to subscribe";
+
+ status = setValue({
+ .prop = propHvac,
+ .areaId = areaId,
+ .value.floatValues = {20.0f},
+ });
+ ASSERT_EQ(status, StatusCode::OK) << "failed to set hvac value";
+
+ ASSERT_TRUE(waitForChangedProperties(propHvac, areaId, /*count=*/1, milliseconds(100)))
+ << "not enough on change events generated for hvac";
+ clearChangedProperties();
+
+ status = setValue({
+ .prop = propHvac,
+ .areaId = areaId,
+ .value.floatValues = {21.0f},
+ });
+ ASSERT_EQ(status, StatusCode::OK) << "failed to set hvac value";
+
+ ASSERT_TRUE(waitForChangedProperties(propHvac, areaId, /*count=*/1, milliseconds(100)))
+ << "not enough on change events generated for hvac";
+ clearChangedProperties();
+
+ status = getHardware()->unsubscribe(propHvac, areaId);
+ ASSERT_EQ(status, StatusCode::OK);
+
+ status = setValue({
+ .prop = propHvac,
+ .areaId = areaId,
+ .value.floatValues = {22.0f},
+ });
+ ASSERT_EQ(status, StatusCode::OK) << "failed to set hvac value";
+
+ ASSERT_FALSE(waitForChangedProperties(propHvac, areaId, /*count=*/1, milliseconds(100)))
+ << "must not receive on change events if the propId, areaId is unsubscribed";
+}
+
TEST_F(FakeVehicleHardwareTest, testSetHvacTemperatureValueSuggestion) {
float CELSIUS = static_cast<float>(toInt(VehicleUnit::CELSIUS));
float FAHRENHEIT = static_cast<float>(toInt(VehicleUnit::FAHRENHEIT));
+ int32_t propHvacTempValueSuggest = toInt(VehicleProperty::HVAC_TEMPERATURE_VALUE_SUGGESTION);
+
+ subscribe(propHvacTempValueSuggest, HVAC_ALL, /*sampleRateHz*/ 0);
VehiclePropValue floatArraySizeFour = {
- .prop = toInt(VehicleProperty::HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {0, CELSIUS, 0, 0},
};
@@ -3056,14 +3241,14 @@
EXPECT_EQ(status, StatusCode::OK);
VehiclePropValue floatArraySizeZero = {
- .prop = toInt(VehicleProperty::HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
};
status = setValue(floatArraySizeZero);
EXPECT_EQ(status, StatusCode::INVALID_ARG);
VehiclePropValue floatArraySizeFive = {
- .prop = toInt(VehicleProperty::HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {0, CELSIUS, 0, 0, 0},
};
@@ -3071,7 +3256,7 @@
EXPECT_EQ(status, StatusCode::INVALID_ARG);
VehiclePropValue invalidUnit = {
- .prop = toInt(VehicleProperty::HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {0, 0, 0, 0},
};
@@ -3102,9 +3287,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInCelsius, CELSIUS, 0, 0},
},
@@ -3112,9 +3295,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInCelsius, CELSIUS,
minTempInCelsius,
@@ -3127,9 +3308,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInFahrenheit, FAHRENHEIT,
0, 0},
@@ -3138,9 +3317,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInFahrenheit, FAHRENHEIT,
minTempInCelsius,
@@ -3153,9 +3330,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInCelsius, CELSIUS, 0, 0},
},
@@ -3163,9 +3338,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInCelsius, CELSIUS,
maxTempInCelsius,
@@ -3178,9 +3351,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInFahrenheit, FAHRENHEIT,
0, 0},
@@ -3189,9 +3360,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInFahrenheit, FAHRENHEIT,
maxTempInCelsius,
@@ -3204,9 +3373,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInCelsius - 1, CELSIUS, 0,
0},
@@ -3215,9 +3382,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInCelsius - 1, CELSIUS,
minTempInCelsius,
@@ -3230,9 +3395,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInFahrenheit - 1,
FAHRENHEIT, 0, 0},
@@ -3241,9 +3404,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInFahrenheit - 1,
FAHRENHEIT, minTempInCelsius,
@@ -3256,9 +3417,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInCelsius + 1, CELSIUS, 0,
0},
@@ -3267,9 +3426,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInCelsius + 1, CELSIUS,
maxTempInCelsius,
@@ -3282,9 +3439,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInFahrenheit + 1,
FAHRENHEIT, 0, 0},
@@ -3293,9 +3448,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {maxTempInFahrenheit + 1,
FAHRENHEIT, maxTempInCelsius,
@@ -3308,9 +3461,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInCelsius +
incrementInCelsius * 2.5f,
@@ -3320,9 +3471,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues =
{minTempInCelsius + incrementInCelsius * 2.5f,
@@ -3338,9 +3487,7 @@
.valuesToSet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues = {minTempInFahrenheit +
incrementInFahrenheit *
@@ -3351,9 +3498,7 @@
.expectedValuesToGet =
{
VehiclePropValue{
- .prop = toInt(
- VehicleProperty::
- HVAC_TEMPERATURE_VALUE_SUGGESTION),
+ .prop = propHvacTempValueSuggest,
.areaId = HVAC_ALL,
.value.floatValues =
{minTempInFahrenheit +
diff --git a/automotive/vehicle/aidl/impl/hardware/include/IVehicleHardware.h b/automotive/vehicle/aidl/impl/hardware/include/IVehicleHardware.h
index 3fecbd9..b813b11 100644
--- a/automotive/vehicle/aidl/impl/hardware/include/IVehicleHardware.h
+++ b/automotive/vehicle/aidl/impl/hardware/include/IVehicleHardware.h
@@ -82,35 +82,6 @@
const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&
requests) const = 0;
- // Update the sampling rate for the specified property and the specified areaId (0 for global
- // property) if server supports it. The property must be a continuous property.
- // {@code sampleRate} means that for this specific property, the server must generate at least
- // this many OnPropertyChange events per seconds.
- // A sampleRate of 0 means the property is no longer subscribed and server does not need to
- // generate any onPropertyEvent for this property.
- // This would be called if sample rate is updated for a subscriber, a new subscriber is added
- // or an existing subscriber is removed. For example:
- // 1. We have no subscriber for speed.
- // 2. A new subscriber is subscribing speed for 10 times/s, updsateSampleRate would be called
- // with sampleRate as 10. The impl is now polling vehicle speed from bus 10 times/s.
- // 3. A new subscriber is subscribing speed for 5 times/s, because it is less than 10
- // times/sec, updateSampleRate would not be called.
- // 4. The initial subscriber is removed, updateSampleRate would be called with sampleRate as
- // 5, because now it only needs to report event 5times/sec. The impl can now poll vehicle
- // speed 5 times/s. If the impl is still polling at 10 times/s, that is okay as long as
- // the polling rate is larger than 5times/s. DefaultVehicleHal would ignore the additional
- // events.
- // 5. The second subscriber is removed, updateSampleRate would be called with sampleRate as 0.
- // The impl can optionally disable the polling for vehicle speed.
- //
- // If the impl is always polling at {@code maxSampleRate} as specified in config, then this
- // function can be a no-op.
- virtual aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate(
- [[maybe_unused]] int32_t propId, [[maybe_unused]] int32_t areaId,
- [[maybe_unused]] float sampleRate) {
- return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
- }
-
// Dump debug information in the server.
virtual DumpResult dump(const std::vector<std::string>& options) = 0;
@@ -145,6 +116,85 @@
// By default batching is disabled.
return std::chrono::nanoseconds(0);
}
+
+ // A [propId, areaId] is newly subscribed or the update rate is changed.
+ //
+ // The 'options' contains the property ID, area ID and sample rate in Hz.
+ //
+ // For continuous property, the sample rate is never 0 and indicates the new sample rate (or
+ // the initial sample rate if this property was not subscribed before).
+ //
+ // For on-change property, the sample rate is always 0 and must be ignored.
+ //
+ // A subscription from VHAL client might not necessarily trigger this function.
+ // DefaultVehicleHal will aggregate all the subscriptions from all the clients and notify
+ // IVehicleHardware if new subscriptions are required or sample rate is updated.
+ //
+ // For example:
+ // 1. VHAL initially have no subscriber for speed.
+ // 2. A new subscriber is subscribing speed for 10 times/s, 'subscribe' is called
+ // with sampleRate as 10. The impl is now polling vehicle speed from bus 10 times/s.
+ // 3. A new subscriber is subscribing speed for 5 times/s, because it is less than 10
+ // times/sec, 'subscribe' is not called.
+ // 4. The initial subscriber is removed, 'subscribe' is called with sampleRate as
+ // 5, because now it only needs to report event 5times/sec. The impl can now poll vehicle
+ // speed 5 times/s. If the impl is still polling at 10 times/s, that is okay as long as
+ // the polling rate is larger than 5times/s. DefaultVehicleHal would ignore the additional
+ // events.
+ // 5. The second subscriber is removed, 'unsubscribe' is called.
+ // The impl can optionally disable the polling for vehicle speed.
+ //
+ // It is recommended to only deliver the subscribed property events to DefaultVehicleHal to
+ // improve performance. However, even if unsubscribed property events are delivered, they
+ // will be filtered out by DefaultVehicleHal.
+ //
+ // For continuous property, if the impl is always polling at {@code maxSampleRate} as specified
+ // in config, then this function can be a no-op.
+ //
+ // For on-change property, if the impl is always subscribing to all on-change properties, then
+ // this function can be no-op.
+ virtual aidl::android::hardware::automotive::vehicle::StatusCode subscribe(
+ [[maybe_unused]] aidl::android::hardware::automotive::vehicle::SubscribeOptions
+ options) {
+ return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
+ }
+
+ // A [propId, areaId] is unsubscribed. This applies for both continuous or on-change property.
+ virtual aidl::android::hardware::automotive::vehicle::StatusCode unsubscribe(
+ [[maybe_unused]] int32_t propId, [[maybe_unused]] int32_t areaId) {
+ return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
+ }
+
+ // This function is deprecated, subscribe/unsubscribe should be used instead.
+ //
+ // Update the sampling rate for the specified property and the specified areaId (0 for global
+ // property) if server supports it. The property must be a continuous property.
+ // {@code sampleRate} means that for this specific property, the server must generate at least
+ // this many OnPropertyChange events per seconds.
+ // A sampleRate of 0 means the property is no longer subscribed and server does not need to
+ // generate any onPropertyEvent for this property.
+ // This would be called if sample rate is updated for a subscriber, a new subscriber is added
+ // or an existing subscriber is removed. For example:
+ // 1. We have no subscriber for speed.
+ // 2. A new subscriber is subscribing speed for 10 times/s, updateSampleRate would be called
+ // with sampleRate as 10. The impl is now polling vehicle speed from bus 10 times/s.
+ // 3. A new subscriber is subscribing speed for 5 times/s, because it is less than 10
+ // times/sec, updateSampleRate would not be called.
+ // 4. The initial subscriber is removed, updateSampleRate would be called with sampleRate as
+ // 5, because now it only needs to report event 5times/sec. The impl can now poll vehicle
+ // speed 5 times/s. If the impl is still polling at 10 times/s, that is okay as long as
+ // the polling rate is larger than 5times/s. DefaultVehicleHal would ignore the additional
+ // events.
+ // 5. The second subscriber is removed, updateSampleRate would be called with sampleRate as 0.
+ // The impl can optionally disable the polling for vehicle speed.
+ //
+ // If the impl is always polling at {@code maxSampleRate} as specified in config, then this
+ // function can be a no-op.
+ virtual aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate(
+ [[maybe_unused]] int32_t propId, [[maybe_unused]] int32_t areaId,
+ [[maybe_unused]] float sampleRate) {
+ return aidl::android::hardware::automotive::vehicle::StatusCode::OK;
+ }
};
} // namespace vehicle
diff --git a/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h b/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
index 3d25cd3..b74dff5 100644
--- a/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
+++ b/automotive/vehicle/aidl/impl/utils/common/include/VehiclePropertyStore.h
@@ -92,7 +92,7 @@
// used as the key.
void registerProperty(
const aidl::android::hardware::automotive::vehicle::VehiclePropConfig& config,
- TokenFunction tokenFunc = nullptr);
+ TokenFunction tokenFunc = nullptr) EXCLUDES(mLock);
// Stores provided value. Returns error if config wasn't registered. If 'updateStatus' is
// true, the 'status' in 'propValue' would be stored. Otherwise, if this is a new value,
@@ -102,44 +102,47 @@
// 'EventMode' controls whether the 'OnValueChangeCallback' will be called for this operation.
VhalResult<void> writeValue(VehiclePropValuePool::RecyclableType propValue,
bool updateStatus = false,
- EventMode mode = EventMode::ON_VALUE_CHANGE);
+ EventMode mode = EventMode::ON_VALUE_CHANGE) EXCLUDES(mLock);
// Remove a given property value from the property store. The 'propValue' would be used to
// generate the key for the value to remove.
void removeValue(
- const aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+ const aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue)
+ EXCLUDES(mLock);
// Remove all the values for the property.
- void removeValuesForProperty(int32_t propId);
+ void removeValuesForProperty(int32_t propId) EXCLUDES(mLock);
// Read all the stored values.
- std::vector<VehiclePropValuePool::RecyclableType> readAllValues() const;
+ std::vector<VehiclePropValuePool::RecyclableType> readAllValues() const EXCLUDES(mLock);
// Read all the values for the property.
- ValuesResultType readValuesForProperty(int32_t propId) const;
+ ValuesResultType readValuesForProperty(int32_t propId) const EXCLUDES(mLock);
// Read the value for the requested property. Returns {@code StatusCode::NOT_AVAILABLE} if the
// value has not been set yet. Returns {@code StatusCode::INVALID_ARG} if the property is
// not configured.
ValueResultType readValue(
- const aidl::android::hardware::automotive::vehicle::VehiclePropValue& request) const;
+ const aidl::android::hardware::automotive::vehicle::VehiclePropValue& request) const
+ EXCLUDES(mLock);
// Read the value for the requested property. Returns {@code StatusCode::NOT_AVAILABLE} if the
// value has not been set yet. Returns {@code StatusCode::INVALID_ARG} if the property is
// not configured.
- ValueResultType readValue(int32_t prop, int32_t area = 0, int64_t token = 0) const;
+ ValueResultType readValue(int32_t prop, int32_t area = 0, int64_t token = 0) const
+ EXCLUDES(mLock);
// Get all property configs.
std::vector<aidl::android::hardware::automotive::vehicle::VehiclePropConfig> getAllConfigs()
- const;
+ const EXCLUDES(mLock);
// Get the property config for the requested property.
android::base::Result<const aidl::android::hardware::automotive::vehicle::VehiclePropConfig*,
VhalError>
- getConfig(int32_t propId) const;
+ getConfig(int32_t propId) const EXCLUDES(mLock);
// Set a callback that would be called when a property value has been updated.
- void setOnValueChangeCallback(const OnValueChangeCallback& callback);
+ void setOnValueChangeCallback(const OnValueChangeCallback& callback) EXCLUDES(mLock);
inline std::shared_ptr<VehiclePropValuePool> getValuePool() { return mValuePool; }
diff --git a/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h b/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h
index c94bad6..546421e 100644
--- a/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h
+++ b/automotive/vehicle/aidl/impl/utils/common/include/VehicleUtils.h
@@ -329,6 +329,11 @@
}
};
+inline std::string propIdToString(int32_t propId) {
+ return toString(
+ static_cast<aidl::android::hardware::automotive::vehicle::VehicleProperty>(propId));
+}
+
} // namespace vehicle
} // namespace automotive
} // namespace hardware
diff --git a/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp b/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp
index 646dc0e..3fd2aa8 100644
--- a/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/src/VehiclePropertyStore.cpp
@@ -108,51 +108,62 @@
VhalResult<void> VehiclePropertyStore::writeValue(VehiclePropValuePool::RecyclableType propValue,
bool updateStatus,
VehiclePropertyStore::EventMode eventMode) {
- std::scoped_lock<std::mutex> g(mLock);
-
- int32_t propId = propValue->prop;
-
- VehiclePropertyStore::Record* record = getRecordLocked(propId);
- if (record == nullptr) {
- return StatusError(StatusCode::INVALID_ARG) << "property: " << propId << " not registered";
- }
-
- if (!isGlobalProp(propId) && getAreaConfig(*propValue, record->propConfig) == nullptr) {
- return StatusError(StatusCode::INVALID_ARG)
- << "no config for property: " << propId << " area: " << propValue->areaId;
- }
-
- VehiclePropertyStore::RecordId recId = getRecordIdLocked(*propValue, *record);
bool valueUpdated = true;
- if (auto it = record->values.find(recId); it != record->values.end()) {
- const VehiclePropValue* valueToUpdate = it->second.get();
- int64_t oldTimestamp = valueToUpdate->timestamp;
- VehiclePropertyStatus oldStatus = valueToUpdate->status;
- // propValue is outdated and drops it.
- if (oldTimestamp > propValue->timestamp) {
+ VehiclePropValue updatedValue;
+ OnValueChangeCallback onValueChangeCallback = nullptr;
+ {
+ std::scoped_lock<std::mutex> g(mLock);
+
+ int32_t propId = propValue->prop;
+
+ VehiclePropertyStore::Record* record = getRecordLocked(propId);
+ if (record == nullptr) {
return StatusError(StatusCode::INVALID_ARG)
- << "outdated timestamp: " << propValue->timestamp;
- }
- if (!updateStatus) {
- propValue->status = oldStatus;
+ << "property: " << propId << " not registered";
}
- valueUpdated = (valueToUpdate->value != propValue->value ||
- valueToUpdate->status != propValue->status ||
- valueToUpdate->prop != propValue->prop ||
- valueToUpdate->areaId != propValue->areaId);
- } else if (!updateStatus) {
- propValue->status = VehiclePropertyStatus::AVAILABLE;
+ if (!isGlobalProp(propId) && getAreaConfig(*propValue, record->propConfig) == nullptr) {
+ return StatusError(StatusCode::INVALID_ARG)
+ << "no config for property: " << propId << " area ID: " << propValue->areaId;
+ }
+
+ VehiclePropertyStore::RecordId recId = getRecordIdLocked(*propValue, *record);
+ if (auto it = record->values.find(recId); it != record->values.end()) {
+ const VehiclePropValue* valueToUpdate = it->second.get();
+ int64_t oldTimestampNanos = valueToUpdate->timestamp;
+ VehiclePropertyStatus oldStatus = valueToUpdate->status;
+ // propValue is outdated and drops it.
+ if (oldTimestampNanos > propValue->timestamp) {
+ return StatusError(StatusCode::INVALID_ARG)
+ << "outdated timestampNanos: " << propValue->timestamp;
+ }
+ if (!updateStatus) {
+ propValue->status = oldStatus;
+ }
+
+ valueUpdated = (valueToUpdate->value != propValue->value ||
+ valueToUpdate->status != propValue->status ||
+ valueToUpdate->prop != propValue->prop ||
+ valueToUpdate->areaId != propValue->areaId);
+ } else if (!updateStatus) {
+ propValue->status = VehiclePropertyStatus::AVAILABLE;
+ }
+
+ record->values[recId] = std::move(propValue);
+
+ if (eventMode == EventMode::NEVER) {
+ return {};
+ }
+ updatedValue = *(record->values[recId]);
+ if (mOnValueChangeCallback == nullptr) {
+ return {};
+ }
+ onValueChangeCallback = mOnValueChangeCallback;
}
- record->values[recId] = std::move(propValue);
-
- if (eventMode == EventMode::NEVER) {
- return {};
- }
-
- if ((eventMode == EventMode::ALWAYS || valueUpdated) && mOnValueChangeCallback != nullptr) {
- mOnValueChangeCallback(*(record->values[recId]));
+ // Invoke the callback outside the lock to prevent dead-lock.
+ if (eventMode == EventMode::ALWAYS || valueUpdated) {
+ onValueChangeCallback(updatedValue);
}
return {};
}
diff --git a/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp b/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp
index fea5034..625652e 100644
--- a/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp
+++ b/automotive/vehicle/aidl/impl/utils/common/test/VehiclePropertyStoreTest.cpp
@@ -509,6 +509,24 @@
ASSERT_EQ(updatedValue.prop, INVALID_PROP_ID);
}
+TEST_F(VehiclePropertyStoreTest, testPropertyChangeCallbackUseVehiclePropertyStore_noDeadLock) {
+ VehiclePropValue fuelCapacity = {
+ .prop = toInt(VehicleProperty::INFO_FUEL_CAPACITY),
+ .value = {.floatValues = {1.0}},
+ };
+
+ std::vector<VehiclePropConfig> configs;
+
+ mStore->setOnValueChangeCallback(
+ [this, &configs]([[maybe_unused]] const VehiclePropValue& value) {
+ configs = mStore->getAllConfigs();
+ });
+
+ ASSERT_RESULT_OK(mStore->writeValue(mValuePool->obtain(fuelCapacity), /*updateStatus=*/true,
+ VehiclePropertyStore::EventMode::ALWAYS));
+ ASSERT_EQ(configs.size(), static_cast<size_t>(2));
+}
+
} // namespace vehicle
} // namespace automotive
} // namespace hardware
diff --git a/automotive/vehicle/aidl/impl/vhal/include/SubscriptionManager.h b/automotive/vehicle/aidl/impl/vhal/include/SubscriptionManager.h
index b91895e..057da35 100644
--- a/automotive/vehicle/aidl/impl/vhal/include/SubscriptionManager.h
+++ b/automotive/vehicle/aidl/impl/vhal/include/SubscriptionManager.h
@@ -119,7 +119,7 @@
mutable std::mutex mLock;
std::unordered_map<PropIdAreaId, std::unordered_map<ClientIdType, CallbackType>,
PropIdAreaIdHash>
- mClientsByPropIdArea GUARDED_BY(mLock);
+ mClientsByPropIdAreaId GUARDED_BY(mLock);
std::unordered_map<ClientIdType, std::unordered_set<PropIdAreaId, PropIdAreaIdHash>>
mSubscribedPropsByClient GUARDED_BY(mLock);
std::unordered_map<PropIdAreaId, ContSubConfigs, PropIdAreaIdHash> mContSubConfigsByPropIdArea
@@ -128,12 +128,21 @@
VhalResult<void> addContinuousSubscriberLocked(const ClientIdType& clientId,
const PropIdAreaId& propIdAreaId,
float sampleRateHz) REQUIRES(mLock);
+ VhalResult<void> addOnChangeSubscriberLocked(const PropIdAreaId& propIdAreaId) REQUIRES(mLock);
+ // Removes the subscription client for the continuous [propId, areaId].
VhalResult<void> removeContinuousSubscriberLocked(const ClientIdType& clientId,
const PropIdAreaId& propIdAreaId)
REQUIRES(mLock);
+ // Removes one subscription client for the on-change [propId, areaId].
+ VhalResult<void> removeOnChangeSubscriberLocked(const PropIdAreaId& propIdAreaId)
+ REQUIRES(mLock);
- VhalResult<void> updateContSubConfigs(const PropIdAreaId& PropIdAreaId,
- const ContSubConfigs& newConfig) REQUIRES(mLock);
+ VhalResult<void> updateContSubConfigsLocked(const PropIdAreaId& PropIdAreaId,
+ const ContSubConfigs& newConfig) REQUIRES(mLock);
+
+ VhalResult<void> unsubscribePropIdAreaIdLocked(SubscriptionManager::ClientIdType clientId,
+ const PropIdAreaId& propIdAreaId)
+ REQUIRES(mLock);
// Checks whether the manager is empty. For testing purpose.
bool isEmpty();
diff --git a/automotive/vehicle/aidl/impl/vhal/src/SubscriptionManager.cpp b/automotive/vehicle/aidl/impl/vhal/src/SubscriptionManager.cpp
index b3c2693..a7c797b 100644
--- a/automotive/vehicle/aidl/impl/vhal/src/SubscriptionManager.cpp
+++ b/automotive/vehicle/aidl/impl/vhal/src/SubscriptionManager.cpp
@@ -16,6 +16,7 @@
#include "SubscriptionManager.h"
+#include <VehicleUtils.h>
#include <android-base/stringprintf.h>
#include <utils/Log.h>
#include <utils/SystemClock.h>
@@ -29,10 +30,6 @@
namespace {
-constexpr float ONE_SECOND_IN_NANO = 1'000'000'000.;
-
-} // namespace
-
using ::aidl::android::hardware::automotive::vehicle::IVehicleCallback;
using ::aidl::android::hardware::automotive::vehicle::StatusCode;
using ::aidl::android::hardware::automotive::vehicle::SubscribeOptions;
@@ -43,13 +40,26 @@
using ::android::base::StringPrintf;
using ::ndk::ScopedAStatus;
+constexpr float ONE_SECOND_IN_NANOS = 1'000'000'000.;
+
+SubscribeOptions newSubscribeOptions(int32_t propId, int32_t areaId, float sampleRateHz) {
+ SubscribeOptions subscribedOptions;
+ subscribedOptions.propId = propId;
+ subscribedOptions.areaIds = {areaId};
+ subscribedOptions.sampleRate = sampleRateHz;
+
+ return subscribedOptions;
+}
+
+} // namespace
+
SubscriptionManager::SubscriptionManager(IVehicleHardware* vehicleHardware)
: mVehicleHardware(vehicleHardware) {}
SubscriptionManager::~SubscriptionManager() {
std::scoped_lock<std::mutex> lockGuard(mLock);
- mClientsByPropIdArea.clear();
+ mClientsByPropIdAreaId.clear();
mSubscribedPropsByClient.clear();
}
@@ -62,10 +72,10 @@
if (sampleRateHz <= 0) {
return Error() << "invalid sample rate, must be a positive number";
}
- if (sampleRateHz <= (ONE_SECOND_IN_NANO / static_cast<float>(INT64_MAX))) {
+ if (sampleRateHz <= (ONE_SECOND_IN_NANOS / static_cast<float>(INT64_MAX))) {
return Error() << "invalid sample rate: " << sampleRateHz << ", too small";
}
- intervalNanos = static_cast<int64_t>(ONE_SECOND_IN_NANO / sampleRateHz);
+ intervalNanos = static_cast<int64_t>(ONE_SECOND_IN_NANOS / sampleRateHz);
return intervalNanos;
}
@@ -95,12 +105,31 @@
return mMaxSampleRateHz;
}
+VhalResult<void> SubscriptionManager::addOnChangeSubscriberLocked(
+ const PropIdAreaId& propIdAreaId) {
+ if (mClientsByPropIdAreaId.find(propIdAreaId) != mClientsByPropIdAreaId.end()) {
+ // This propId, areaId is already subscribed, ignore the request.
+ return {};
+ }
+
+ int32_t propId = propIdAreaId.propId;
+ int32_t areaId = propIdAreaId.areaId;
+ if (auto status = mVehicleHardware->subscribe(
+ newSubscribeOptions(propId, areaId, /*updateRateHz=*/0));
+ status != StatusCode::OK) {
+ return StatusError(status)
+ << StringPrintf("failed subscribe for prop: %s, areaId: %" PRId32,
+ propIdToString(propId).c_str(), areaId);
+ }
+ return {};
+}
+
VhalResult<void> SubscriptionManager::addContinuousSubscriberLocked(
const ClientIdType& clientId, const PropIdAreaId& propIdAreaId, float sampleRateHz) {
// Make a copy so that we don't modify 'mContSubConfigsByPropIdArea' on failure cases.
ContSubConfigs newConfig = mContSubConfigsByPropIdArea[propIdAreaId];
newConfig.addClient(clientId, sampleRateHz);
- return updateContSubConfigs(propIdAreaId, newConfig);
+ return updateContSubConfigsLocked(propIdAreaId, newConfig);
}
VhalResult<void> SubscriptionManager::removeContinuousSubscriberLocked(
@@ -108,11 +137,28 @@
// Make a copy so that we don't modify 'mContSubConfigsByPropIdArea' on failure cases.
ContSubConfigs newConfig = mContSubConfigsByPropIdArea[propIdAreaId];
newConfig.removeClient(clientId);
- return updateContSubConfigs(propIdAreaId, newConfig);
+ return updateContSubConfigsLocked(propIdAreaId, newConfig);
}
-VhalResult<void> SubscriptionManager::updateContSubConfigs(const PropIdAreaId& propIdAreaId,
- const ContSubConfigs& newConfig) {
+VhalResult<void> SubscriptionManager::removeOnChangeSubscriberLocked(
+ const PropIdAreaId& propIdAreaId) {
+ if (mClientsByPropIdAreaId[propIdAreaId].size() > 1) {
+ // After unsubscribing this client, there is still client subscribed, so do nothing.
+ return {};
+ }
+
+ int32_t propId = propIdAreaId.propId;
+ int32_t areaId = propIdAreaId.areaId;
+ if (auto status = mVehicleHardware->unsubscribe(propId, areaId); status != StatusCode::OK) {
+ return StatusError(status)
+ << StringPrintf("failed unsubscribe for prop: %s, areaId: %" PRId32,
+ propIdToString(propId).c_str(), areaId);
+ }
+ return {};
+}
+
+VhalResult<void> SubscriptionManager::updateContSubConfigsLocked(const PropIdAreaId& propIdAreaId,
+ const ContSubConfigs& newConfig) {
if (newConfig.getMaxSampleRateHz() ==
mContSubConfigsByPropIdArea[propIdAreaId].getMaxSampleRateHz()) {
mContSubConfigsByPropIdArea[propIdAreaId] = newConfig;
@@ -123,10 +169,27 @@
int32_t areaId = propIdAreaId.areaId;
if (auto status = mVehicleHardware->updateSampleRate(propId, areaId, newRateHz);
status != StatusCode::OK) {
- return StatusError(status) << StringPrintf("failed to update sample rate for prop: %" PRId32
- ", area"
- ": %" PRId32 ", sample rate: %f HZ",
- propId, areaId, newRateHz);
+ return StatusError(status)
+ << StringPrintf("failed to update sample rate for prop: %s, areaId: %" PRId32
+ ", sample rate: %f HZ",
+ propIdToString(propId).c_str(), areaId, newRateHz);
+ }
+ if (newRateHz != 0) {
+ if (auto status =
+ mVehicleHardware->subscribe(newSubscribeOptions(propId, areaId, newRateHz));
+ status != StatusCode::OK) {
+ return StatusError(status) << StringPrintf(
+ "failed subscribe for prop: %s, areaId"
+ ": %" PRId32 ", sample rate: %f HZ",
+ propIdToString(propId).c_str(), areaId, newRateHz);
+ }
+ } else {
+ if (auto status = mVehicleHardware->unsubscribe(propId, areaId); status != StatusCode::OK) {
+ return StatusError(status) << StringPrintf(
+ "failed unsubscribe for prop: %s, areaId"
+ ": %" PRId32,
+ propIdToString(propId).c_str(), areaId);
+ }
}
mContSubConfigsByPropIdArea[propIdAreaId] = newConfig;
return {};
@@ -163,21 +226,53 @@
.propId = propId,
.areaId = areaId,
};
+ VhalResult<void> result;
if (isContinuousProperty) {
- if (auto result = addContinuousSubscriberLocked(clientId, propIdAreaId,
- option.sampleRate);
- !result.ok()) {
- return result;
- }
+ result = addContinuousSubscriberLocked(clientId, propIdAreaId, option.sampleRate);
+ } else {
+ result = addOnChangeSubscriberLocked(propIdAreaId);
+ }
+
+ if (!result.ok()) {
+ return result;
}
mSubscribedPropsByClient[clientId].insert(propIdAreaId);
- mClientsByPropIdArea[propIdAreaId][clientId] = callback;
+ mClientsByPropIdAreaId[propIdAreaId][clientId] = callback;
}
}
return {};
}
+VhalResult<void> SubscriptionManager::unsubscribePropIdAreaIdLocked(
+ SubscriptionManager::ClientIdType clientId, const PropIdAreaId& propIdAreaId) {
+ if (mContSubConfigsByPropIdArea.find(propIdAreaId) != mContSubConfigsByPropIdArea.end()) {
+ // This is a subscribed continuous property.
+ if (auto result = removeContinuousSubscriberLocked(clientId, propIdAreaId); !result.ok()) {
+ return result;
+ }
+ } else {
+ if (mClientsByPropIdAreaId.find(propIdAreaId) == mClientsByPropIdAreaId.end()) {
+ ALOGW("Unsubscribe: The property: %s, areaId: %" PRId32
+ " was not previously subscribed, do nothing",
+ propIdToString(propIdAreaId.propId).c_str(), propIdAreaId.areaId);
+ return {};
+ }
+ // This is an on-change property.
+ if (auto result = removeOnChangeSubscriberLocked(propIdAreaId); !result.ok()) {
+ return result;
+ }
+ }
+
+ auto& clients = mClientsByPropIdAreaId[propIdAreaId];
+ clients.erase(clientId);
+ if (clients.empty()) {
+ mClientsByPropIdAreaId.erase(propIdAreaId);
+ mContSubConfigsByPropIdArea.erase(propIdAreaId);
+ }
+ return {};
+}
+
VhalResult<void> SubscriptionManager::unsubscribe(SubscriptionManager::ClientIdType clientId,
const std::vector<int32_t>& propIds) {
std::scoped_lock<std::mutex> lockGuard(mLock);
@@ -186,39 +281,27 @@
return StatusError(StatusCode::INVALID_ARG)
<< "No property was subscribed for the callback";
}
- std::unordered_set<int32_t> subscribedPropIds;
- for (auto const& propIdAreaId : mSubscribedPropsByClient[clientId]) {
- subscribedPropIds.insert(propIdAreaId.propId);
- }
+ std::vector<PropIdAreaId> propIdAreaIdsToUnsubscribe;
+ std::unordered_set<int32_t> propIdSet;
for (int32_t propId : propIds) {
- if (subscribedPropIds.find(propId) == subscribedPropIds.end()) {
- return StatusError(StatusCode::INVALID_ARG)
- << "property ID: " << propId << " is not subscribed";
+ propIdSet.insert(propId);
+ }
+ auto& subscribedPropIdsAreaIds = mSubscribedPropsByClient[clientId];
+ for (const auto& propIdAreaId : subscribedPropIdsAreaIds) {
+ if (propIdSet.find(propIdAreaId.propId) != propIdSet.end()) {
+ propIdAreaIdsToUnsubscribe.push_back(propIdAreaId);
}
}
- auto& propIdAreaIds = mSubscribedPropsByClient[clientId];
- auto it = propIdAreaIds.begin();
- while (it != propIdAreaIds.end()) {
- int32_t propId = it->propId;
- if (std::find(propIds.begin(), propIds.end(), propId) != propIds.end()) {
- if (auto result = removeContinuousSubscriberLocked(clientId, *it); !result.ok()) {
- return result;
- }
-
- auto& clients = mClientsByPropIdArea[*it];
- clients.erase(clientId);
- if (clients.empty()) {
- mClientsByPropIdArea.erase(*it);
- mContSubConfigsByPropIdArea.erase(*it);
- }
- it = propIdAreaIds.erase(it);
- } else {
- it++;
+ for (const auto& propIdAreaId : propIdAreaIdsToUnsubscribe) {
+ if (auto result = unsubscribePropIdAreaIdLocked(clientId, propIdAreaId); !result.ok()) {
+ return result;
}
+ subscribedPropIdsAreaIds.erase(propIdAreaId);
}
- if (propIdAreaIds.empty()) {
+
+ if (subscribedPropIdsAreaIds.empty()) {
mSubscribedPropsByClient.erase(clientId);
}
return {};
@@ -233,16 +316,9 @@
auto& subscriptions = mSubscribedPropsByClient[clientId];
for (auto const& propIdAreaId : subscriptions) {
- if (auto result = removeContinuousSubscriberLocked(clientId, propIdAreaId); !result.ok()) {
+ if (auto result = unsubscribePropIdAreaIdLocked(clientId, propIdAreaId); !result.ok()) {
return result;
}
-
- auto& clients = mClientsByPropIdArea[propIdAreaId];
- clients.erase(clientId);
- if (clients.empty()) {
- mClientsByPropIdArea.erase(propIdAreaId);
- mContSubConfigsByPropIdArea.erase(propIdAreaId);
- }
}
mSubscribedPropsByClient.erase(clientId);
return {};
@@ -258,11 +334,11 @@
.propId = value.prop,
.areaId = value.areaId,
};
- if (mClientsByPropIdArea.find(propIdAreaId) == mClientsByPropIdArea.end()) {
+ if (mClientsByPropIdAreaId.find(propIdAreaId) == mClientsByPropIdAreaId.end()) {
continue;
}
- for (const auto& [_, client] : mClientsByPropIdArea[propIdAreaId]) {
+ for (const auto& [_, client] : mClientsByPropIdAreaId[propIdAreaId]) {
clients[client].push_back(value);
}
}
@@ -280,11 +356,11 @@
.propId = errorEvent.propId,
.areaId = errorEvent.areaId,
};
- if (mClientsByPropIdArea.find(propIdAreaId) == mClientsByPropIdArea.end()) {
+ if (mClientsByPropIdAreaId.find(propIdAreaId) == mClientsByPropIdAreaId.end()) {
continue;
}
- for (const auto& [_, client] : mClientsByPropIdArea[propIdAreaId]) {
+ for (const auto& [_, client] : mClientsByPropIdAreaId[propIdAreaId]) {
clients[client].push_back({
.propId = errorEvent.propId,
.areaId = errorEvent.areaId,
@@ -297,7 +373,7 @@
bool SubscriptionManager::isEmpty() {
std::scoped_lock<std::mutex> lockGuard(mLock);
- return mSubscribedPropsByClient.empty() && mClientsByPropIdArea.empty();
+ return mSubscribedPropsByClient.empty() && mClientsByPropIdAreaId.empty();
}
size_t SubscriptionManager::countClients() {
diff --git a/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.cpp b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.cpp
index 3fae596..b64c0d7 100644
--- a/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.cpp
+++ b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.cpp
@@ -29,6 +29,7 @@
using ::aidl::android::hardware::automotive::vehicle::SetValueRequest;
using ::aidl::android::hardware::automotive::vehicle::SetValueResult;
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::VehiclePropValue;
@@ -88,7 +89,26 @@
return StatusCode::OK;
}
-StatusCode MockVehicleHardware::updateSampleRate(int32_t propId, int32_t areaId, float sampleRate) {
+StatusCode MockVehicleHardware::subscribe(SubscribeOptions options) {
+ for (int32_t areaId : options.areaIds) {
+ if (auto status = subscribePropIdAreaId(options.propId, areaId, options.sampleRate);
+ status != StatusCode::OK) {
+ return status;
+ }
+ }
+ return StatusCode::OK;
+}
+
+StatusCode MockVehicleHardware::subscribePropIdAreaId(int32_t propId, int32_t areaId,
+ float sampleRateHz) {
+ if (sampleRateHz == 0) {
+ // on-change property.
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ mSubOnChangePropIdAreaIds.insert(std::pair<int32_t, int32_t>(propId, areaId));
+ return StatusCode::OK;
+ }
+
+ // continuous property.
std::shared_ptr<std::function<void()>> action;
{
@@ -97,9 +117,6 @@
// Remove the previous action register for this [propId, areaId].
mRecurrentTimer->unregisterTimerCallback(mRecurrentActions[propId][areaId]);
}
- if (sampleRate == 0) {
- return StatusCode::OK;
- }
// We are sure 'propertyChangeCallback' would be alive because we would unregister timer
// before destroying 'this' which owns mPropertyChangeCallback.
@@ -107,8 +124,8 @@
action = std::make_shared<std::function<void()>>([propertyChangeCallback, propId, areaId] {
std::vector<VehiclePropValue> values = {
{
- .prop = propId,
.areaId = areaId,
+ .prop = propId,
},
};
(*propertyChangeCallback)(values);
@@ -119,11 +136,45 @@
// In mock implementation, we generate a new property change event for this property at sample
// rate.
- int64_t interval = static_cast<int64_t>(1'000'000'000. / sampleRate);
+ int64_t interval = static_cast<int64_t>(1'000'000'000. / sampleRateHz);
mRecurrentTimer->registerTimerCallback(interval, action);
return StatusCode::OK;
}
+StatusCode MockVehicleHardware::unsubscribe(int32_t propId, int32_t areaId) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ // For on-change property.
+ mSubOnChangePropIdAreaIds.erase(std::make_pair(propId, areaId));
+ // for continuous property.
+ if (mRecurrentActions[propId][areaId] != nullptr) {
+ // Remove the previous action register for this [propId, areaId].
+ mRecurrentTimer->unregisterTimerCallback(mRecurrentActions[propId][areaId]);
+ mRecurrentActions[propId].erase(areaId);
+ if (mRecurrentActions[propId].empty()) {
+ mRecurrentActions.erase(propId);
+ }
+ }
+ return StatusCode::OK;
+}
+
+std::set<std::pair<int32_t, int32_t>> MockVehicleHardware::getSubscribedOnChangePropIdAreaIds() {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ std::set<std::pair<int32_t, int32_t>> propIdAreaIds;
+ propIdAreaIds = mSubOnChangePropIdAreaIds;
+ return propIdAreaIds;
+}
+
+std::set<std::pair<int32_t, int32_t>> MockVehicleHardware::getSubscribedContinuousPropIdAreaIds() {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+ std::set<std::pair<int32_t, int32_t>> propIdAreaIds;
+ for (const auto& [propId, actionByAreaId] : mRecurrentActions) {
+ for (const auto& [areaId, _] : actionByAreaId) {
+ propIdAreaIds.insert(std::make_pair(propId, areaId));
+ }
+ }
+ return propIdAreaIds;
+}
+
void MockVehicleHardware::registerOnPropertyChangeEvent(
std::unique_ptr<const PropertyChangeCallback> callback) {
std::scoped_lock<std::mutex> lockGuard(mLock);
diff --git a/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.h b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.h
index 3ce18c5..e0d2d66 100644
--- a/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.h
+++ b/automotive/vehicle/aidl/impl/vhal/test/MockVehicleHardware.h
@@ -29,6 +29,7 @@
#include <list>
#include <memory>
#include <mutex>
+#include <set>
#include <thread>
#include <unordered_map>
#include <vector>
@@ -59,8 +60,10 @@
void registerOnPropertyChangeEvent(
std::unique_ptr<const PropertyChangeCallback> callback) override;
void registerOnPropertySetErrorEvent(std::unique_ptr<const PropertySetErrorCallback>) override;
- aidl::android::hardware::automotive::vehicle::StatusCode updateSampleRate(
- int32_t propId, int32_t areaId, float sampleRate) override;
+ aidl::android::hardware::automotive::vehicle::StatusCode subscribe(
+ aidl::android::hardware::automotive::vehicle::SubscribeOptions options) override;
+ aidl::android::hardware::automotive::vehicle::StatusCode unsubscribe(int32_t propId,
+ int32_t areaId) override;
std::chrono::nanoseconds getPropertyOnChangeEventBatchingWindow() override;
// Test functions.
@@ -90,6 +93,9 @@
void sendOnPropertySetErrorEvent(const std::vector<SetValueErrorEvent>& errorEvents);
void setPropertyOnChangeEventBatchingWindow(std::chrono::nanoseconds window);
+ std::set<std::pair<int32_t, int32_t>> getSubscribedOnChangePropIdAreaIds();
+ std::set<std::pair<int32_t, int32_t>> getSubscribedContinuousPropIdAreaIds();
+
private:
mutable std::mutex mLock;
mutable std::condition_variable mCv;
@@ -114,6 +120,7 @@
const std::vector<aidl::android::hardware::automotive::vehicle::GetValueRequest>&)>
mGetValueResponder GUARDED_BY(mLock);
std::chrono::nanoseconds mEventBatchingWindow GUARDED_BY(mLock) = std::chrono::nanoseconds(0);
+ std::set<std::pair<int32_t, int32_t>> mSubOnChangePropIdAreaIds GUARDED_BY(mLock);
template <class ResultType>
aidl::android::hardware::automotive::vehicle::StatusCode returnResponse(
@@ -126,6 +133,8 @@
const std::vector<RequestType>& requests,
std::list<std::vector<RequestType>>* storedRequests,
std::list<std::vector<ResultType>>* storedResponses) const REQUIRES(mLock);
+ aidl::android::hardware::automotive::vehicle::StatusCode subscribePropIdAreaId(
+ int32_t propId, int32_t areaId, float sampleRateHz);
DumpResult mDumpResult;
diff --git a/automotive/vehicle/aidl/impl/vhal/test/SubscriptionManagerTest.cpp b/automotive/vehicle/aidl/impl/vhal/test/SubscriptionManagerTest.cpp
index 5464304..049ca8b 100644
--- a/automotive/vehicle/aidl/impl/vhal/test/SubscriptionManagerTest.cpp
+++ b/automotive/vehicle/aidl/impl/vhal/test/SubscriptionManagerTest.cpp
@@ -114,6 +114,8 @@
void clearEvents() { return getCallback()->clearEvents(); }
+ std::shared_ptr<MockVehicleHardware> getHardware() { return mHardware; }
+
private:
std::unique_ptr<SubscriptionManager> mManager;
std::shared_ptr<PropertyCallback> mCallback;
@@ -132,6 +134,9 @@
auto result = getManager()->subscribe(getCallbackClient(), options, true);
ASSERT_TRUE(result.ok()) << "failed to subscribe: " << result.error().message();
+ ASSERT_THAT(getHardware()->getSubscribedContinuousPropIdAreaIds(),
+ UnorderedElementsAre(std::pair<int32_t, int32_t>(0, 0)));
+
std::this_thread::sleep_for(std::chrono::seconds(1));
// Theoretically trigger 10 times, but check for at least 9 times to be stable.
@@ -240,6 +245,8 @@
result = getManager()->unsubscribe(getCallbackClient()->asBinder().get());
ASSERT_TRUE(result.ok()) << "failed to unsubscribe: " << result.error().message();
+ ASSERT_EQ(getHardware()->getSubscribedContinuousPropIdAreaIds().size(), 0u);
+
// Wait for the last events to come.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
@@ -316,7 +323,7 @@
EXPECT_TRUE(getEvents().empty());
}
-TEST_F(SubscriptionManagerTest, testUnsubscribeFailure) {
+TEST_F(SubscriptionManagerTest, testUnsubscribeUnsubscribedPropId) {
std::vector<SubscribeOptions> options = {
{
.propId = 0,
@@ -334,14 +341,21 @@
// Property ID: 2 was not subscribed.
result = getManager()->unsubscribe(getCallbackClient()->asBinder().get(),
std::vector<int32_t>({0, 1, 2}));
- ASSERT_FALSE(result.ok()) << "unsubscribe an unsubscribed property must fail";
+ ASSERT_TRUE(result.ok()) << "unsubscribe an unsubscribed property must do nothing";
- // Since property 0 and property 1 was not unsubscribed successfully, we should be able to
- // unsubscribe them again.
- result = getManager()->unsubscribe(getCallbackClient()->asBinder().get(),
- std::vector<int32_t>({0, 1}));
- ASSERT_TRUE(result.ok()) << "a failed unsubscription must not unsubscribe any properties"
- << result.error().message();
+ std::vector<VehiclePropValue> updatedValues = {
+ {
+ .prop = 0,
+ .areaId = 0,
+ },
+ {
+ .prop = 1,
+ .areaId = 0,
+ },
+ };
+ auto clients = getManager()->getSubscribedClients(std::vector<VehiclePropValue>(updatedValues));
+
+ ASSERT_EQ(clients.size(), 0u) << "all subscribed properties must be unsubscribed";
}
TEST_F(SubscriptionManagerTest, testSubscribeOnchange) {
@@ -370,6 +384,11 @@
ASSERT_TRUE(result.ok()) << "failed to subscribe: " << result.error().message();
result = getManager()->subscribe(client2, options2, false);
ASSERT_TRUE(result.ok()) << "failed to subscribe: " << result.error().message();
+ ASSERT_THAT(getHardware()->getSubscribedOnChangePropIdAreaIds(),
+ UnorderedElementsAre(std::pair<int32_t, int32_t>(0, 0),
+ std::pair<int32_t, int32_t>(0, 1),
+ std::pair<int32_t, int32_t>(1, 0)));
+ ASSERT_EQ(getHardware()->getSubscribedContinuousPropIdAreaIds().size(), 0u);
std::vector<VehiclePropValue> updatedValues = {
{
@@ -483,6 +502,8 @@
auto clients = getManager()->getSubscribedClients(std::vector<VehiclePropValue>(updatedValues));
ASSERT_THAT(clients[getCallbackClient()], ElementsAre(updatedValues[1]));
+ ASSERT_THAT(getHardware()->getSubscribedOnChangePropIdAreaIds(),
+ UnorderedElementsAre(std::pair<int32_t, int32_t>(1, 0)));
}
TEST_F(SubscriptionManagerTest, testCheckSampleRateHzValid) {