diff options
author | Prachi Hande <prachihande@google.com> | 2019-08-08 17:10:30 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-08-08 17:10:30 -0700 |
commit | 007e8a139e7ae4799825ddaaff012e6a3269a0e7 (patch) | |
tree | 3ef172f0d7612113b908967353668c66f847c415 /automotive | |
parent | 7424ecaba7326fbf005e23847f3a9f1a6f158059 (diff) | |
parent | 4066574002c31334086431399d362caa6fa5c81d (diff) | |
download | platform_hardware_interfaces-007e8a139e7ae4799825ddaaff012e6a3269a0e7.tar.gz platform_hardware_interfaces-007e8a139e7ae4799825ddaaff012e6a3269a0e7.tar.bz2 platform_hardware_interfaces-007e8a139e7ae4799825ddaaff012e6a3269a0e7.zip |
VmsUtils: Update existing methods to parse subscriptions response messages.
am: 4066574002
Change-Id: I2708ebc93f485eff8c98016b489261834203c86e
Diffstat (limited to 'automotive')
3 files changed, 115 insertions, 57 deletions
diff --git a/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VmsUtils.h b/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VmsUtils.h index d689e62061..8ee3c545dc 100644 --- a/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VmsUtils.h +++ b/automotive/vehicle/2.0/default/common/include/vhal_v2_0/VmsUtils.h @@ -153,7 +153,7 @@ std::unique_ptr<VehiclePropValue> createOfferingMessage(const VmsOffers& offers) std::unique_ptr<VehiclePropValue> createAvailabilityRequest(); // Creates a VehiclePropValue containing a message of type -// VmsMessageType.AVAILABILITY_REQUEST. +// VmsMessageType.SUBSCRIPTIONS_REQUEST. std::unique_ptr<VehiclePropValue> createSubscriptionsRequest(); // Creates a VehiclePropValue containing a message of type VmsMessageType.DATA. @@ -202,21 +202,21 @@ int32_t parsePublisherIdResponse(const VehiclePropValue& publisher_id_response); // Returns true if the new sequence number is greater than the last seen // sequence number. -bool isSequenceNumberNewer(const VehiclePropValue& subscription_change, +bool isSequenceNumberNewer(const VehiclePropValue& subscriptions_state, const int last_seen_sequence_number); // Returns sequence number of the message. -int32_t getSequenceNumberForSubscriptionsState(const VehiclePropValue& subscription_change); +int32_t getSequenceNumberForSubscriptionsState(const VehiclePropValue& subscriptions_state); -// Takes a subscription change message and returns the layers that have active +// Takes a subscriptions state message and returns the layers that have active // subscriptions of the layers that are offered by your HAL client/publisher. // -// A publisher can use this function when receiving a subscription change message -// to determine which layers to publish data on. +// A publisher can use this function when receiving a subscriptions response or subscriptions +// change message to determine which layers to publish data on. // The caller of this function can optionally decide to not consume these layers // if the subscription change has the sequence number less than the last seen // sequence number. -std::vector<VmsLayer> getSubscribedLayers(const VehiclePropValue& subscription_change, +std::vector<VmsLayer> getSubscribedLayers(const VehiclePropValue& subscriptions_state, const VmsOffers& offers); // Takes an availability change message and returns true if the parsed message implies that diff --git a/automotive/vehicle/2.0/default/common/src/VmsUtils.cpp b/automotive/vehicle/2.0/default/common/src/VmsUtils.cpp index d346206c35..9eba905901 100644 --- a/automotive/vehicle/2.0/default/common/src/VmsUtils.cpp +++ b/automotive/vehicle/2.0/default/common/src/VmsUtils.cpp @@ -194,32 +194,35 @@ int32_t parsePublisherIdResponse(const VehiclePropValue& publisher_id_response) return -1; } -bool isSequenceNumberNewer(const VehiclePropValue& subscription_change, +bool isSequenceNumberNewer(const VehiclePropValue& subscriptions_state, const int last_seen_sequence_number) { - return (isValidVmsMessage(subscription_change) && - parseMessageType(subscription_change) == VmsMessageType::SUBSCRIPTIONS_CHANGE && - subscription_change.value.int32Values.size() > kSubscriptionStateSequenceNumberIndex && - subscription_change.value.int32Values[kSubscriptionStateSequenceNumberIndex] > + return (isValidVmsMessage(subscriptions_state) && + (parseMessageType(subscriptions_state) == VmsMessageType::SUBSCRIPTIONS_CHANGE || + parseMessageType(subscriptions_state) == VmsMessageType::SUBSCRIPTIONS_RESPONSE) && + subscriptions_state.value.int32Values.size() > kSubscriptionStateSequenceNumberIndex && + subscriptions_state.value.int32Values[kSubscriptionStateSequenceNumberIndex] > last_seen_sequence_number); } -int32_t getSequenceNumberForSubscriptionsState(const VehiclePropValue& subscription_change) { - if (isValidVmsMessage(subscription_change) && - parseMessageType(subscription_change) == VmsMessageType::SUBSCRIPTIONS_CHANGE && - subscription_change.value.int32Values.size() > kSubscriptionStateSequenceNumberIndex) { - return subscription_change.value.int32Values[kSubscriptionStateSequenceNumberIndex]; +int32_t getSequenceNumberForSubscriptionsState(const VehiclePropValue& subscriptions_state) { + if (isValidVmsMessage(subscriptions_state) && + (parseMessageType(subscriptions_state) == VmsMessageType::SUBSCRIPTIONS_CHANGE || + parseMessageType(subscriptions_state) == VmsMessageType::SUBSCRIPTIONS_RESPONSE) && + subscriptions_state.value.int32Values.size() > kSubscriptionStateSequenceNumberIndex) { + return subscriptions_state.value.int32Values[kSubscriptionStateSequenceNumberIndex]; } return -1; } -std::vector<VmsLayer> getSubscribedLayers(const VehiclePropValue& subscription_change, +std::vector<VmsLayer> getSubscribedLayers(const VehiclePropValue& subscriptions_state, const VmsOffers& offers) { - if (isValidVmsMessage(subscription_change) && - parseMessageType(subscription_change) == VmsMessageType::SUBSCRIPTIONS_CHANGE && - subscription_change.value.int32Values.size() > kSubscriptionStateSequenceNumberIndex) { - const int32_t num_of_layers = subscription_change.value.int32Values[toInt( + if (isValidVmsMessage(subscriptions_state) && + (parseMessageType(subscriptions_state) == VmsMessageType::SUBSCRIPTIONS_CHANGE || + parseMessageType(subscriptions_state) == VmsMessageType::SUBSCRIPTIONS_RESPONSE) && + subscriptions_state.value.int32Values.size() > kSubscriptionStateSequenceNumberIndex) { + const int32_t num_of_layers = subscriptions_state.value.int32Values[toInt( VmsSubscriptionsStateIntegerValuesIndex::NUMBER_OF_LAYERS)]; - const int32_t num_of_associated_layers = subscription_change.value.int32Values[toInt( + const int32_t num_of_associated_layers = subscriptions_state.value.int32Values[toInt( VmsSubscriptionsStateIntegerValuesIndex ::NUMBER_OF_ASSOCIATED_LAYERS)]; std::unordered_set<VmsLayer, VmsLayer::VmsLayerHashFunction> offered_layers; @@ -231,9 +234,9 @@ std::vector<VmsLayer> getSubscribedLayers(const VehiclePropValue& subscription_c int current_index = toInt(VmsSubscriptionsStateIntegerValuesIndex::SUBSCRIPTIONS_START); // Add all subscribed layers which are offered by the current publisher. for (int i = 0; i < num_of_layers; i++) { - VmsLayer layer = VmsLayer(subscription_change.value.int32Values[current_index], - subscription_change.value.int32Values[current_index + 1], - subscription_change.value.int32Values[current_index + 2]); + VmsLayer layer = VmsLayer(subscriptions_state.value.int32Values[current_index], + subscriptions_state.value.int32Values[current_index + 1], + subscriptions_state.value.int32Values[current_index + 2]); if (offered_layers.find(layer) != offered_layers.end()) { subscribed_layers.push_back(layer); } @@ -243,15 +246,15 @@ std::vector<VmsLayer> getSubscribedLayers(const VehiclePropValue& subscription_c // For this, we need to check if the associated layer has a publisher ID which is // same as that of the current publisher. for (int i = 0; i < num_of_associated_layers; i++) { - VmsLayer layer = VmsLayer(subscription_change.value.int32Values[current_index], - subscription_change.value.int32Values[current_index + 1], - subscription_change.value.int32Values[current_index + 2]); + VmsLayer layer = VmsLayer(subscriptions_state.value.int32Values[current_index], + subscriptions_state.value.int32Values[current_index + 1], + subscriptions_state.value.int32Values[current_index + 2]); current_index += kLayerSize; if (offered_layers.find(layer) != offered_layers.end()) { - int32_t num_of_publisher_ids = subscription_change.value.int32Values[current_index]; + int32_t num_of_publisher_ids = subscriptions_state.value.int32Values[current_index]; current_index++; for (int j = 0; j < num_of_publisher_ids; j++) { - if (subscription_change.value.int32Values[current_index] == + if (subscriptions_state.value.int32Values[current_index] == offers.publisher_id) { subscribed_layers.push_back(layer); } diff --git a/automotive/vehicle/2.0/default/tests/VmsUtils_test.cpp b/automotive/vehicle/2.0/default/tests/VmsUtils_test.cpp index 7189212505..8b547f1733 100644 --- a/automotive/vehicle/2.0/default/tests/VmsUtils_test.cpp +++ b/automotive/vehicle/2.0/default/tests/VmsUtils_test.cpp @@ -214,57 +214,82 @@ TEST(VmsUtilsTest, invalidPublisherIdResponse) { EXPECT_EQ(parsePublisherIdResponse(*message), -1); } -TEST(VmsUtilsTest, validSequenceNumberForSubscriptionsState) { +TEST(VmsUtilsTest, validSequenceNumberForSubscriptionsChange) { auto message = createBaseVmsMessage(2); message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234}; EXPECT_EQ(getSequenceNumberForSubscriptionsState(*message), 1234); } +TEST(VmsUtilsTest, validSequenceNumberForSubscriptionsResponse) { + auto message = createBaseVmsMessage(2); + message->value.int32Values = + hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234}; + EXPECT_EQ(getSequenceNumberForSubscriptionsState(*message), 1234); +} + TEST(VmsUtilsTest, invalidSubscriptionsState) { auto message = createBaseVmsMessage(1); EXPECT_EQ(getSequenceNumberForSubscriptionsState(*message), -1); } -TEST(VmsUtilsTest, newSequenceNumberForExistingSmallerNumber) { +TEST(VmsUtilsTest, newSequenceNumberForExistingSmallerNumberForChange) { auto message = createBaseVmsMessage(2); message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234}; EXPECT_TRUE(isSequenceNumberNewer(*message, 1233)); } -TEST(VmsUtilsTest, newSequenceNumberForExistingGreaterNumber) { +TEST(VmsUtilsTest, newSequenceNumberForExistingSmallerNumberForResponse) { + auto message = createBaseVmsMessage(2); + message->value.int32Values = + hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234}; + EXPECT_TRUE(isSequenceNumberNewer(*message, 1233)); +} + +TEST(VmsUtilsTest, newSequenceNumberForExistingGreaterNumberForChange) { auto message = createBaseVmsMessage(2); message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234}; EXPECT_FALSE(isSequenceNumberNewer(*message, 1235)); } -TEST(VmsUtilsTest, newSequenceNumberForSameNumber) { +TEST(VmsUtilsTest, newSequenceNumberForExistingGreaterNumberForResponse) { + auto message = createBaseVmsMessage(2); + message->value.int32Values = + hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234}; + EXPECT_FALSE(isSequenceNumberNewer(*message, 1235)); +} + +TEST(VmsUtilsTest, newSequenceNumberForSameNumberForChange) { auto message = createBaseVmsMessage(2); message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), 1234}; EXPECT_FALSE(isSequenceNumberNewer(*message, 1234)); } -TEST(VmsUtilsTest, subscribedLayers) { +TEST(VmsUtilsTest, newSequenceNumberForSameNumberForResponse) { + auto message = createBaseVmsMessage(2); + message->value.int32Values = + hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_RESPONSE), 1234}; + EXPECT_FALSE(isSequenceNumberNewer(*message, 1234)); +} + +void testSubscribedLayers(VmsMessageType type) { VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1), {VmsLayer(4, 1, 1)}), VmsLayerOffering(VmsLayer(2, 0, 1))}}; auto message = createBaseVmsMessage(2); - message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), + message->value.int32Values = hidl_vec<int32_t>{toInt(type), 1234, // sequence number 2, // number of layers 1, // number of associated layers 1, // layer 1 - 0, - 1, + 0, 1, 4, // layer 2 - 1, - 1, + 1, 1, 2, // associated layer - 0, - 1, + 0, 1, 2, // number of publisher IDs 111, // publisher IDs 123}; @@ -275,10 +300,18 @@ TEST(VmsUtilsTest, subscribedLayers) { EXPECT_EQ(result.at(1), VmsLayer(2, 0, 1)); } -TEST(VmsUtilsTest, subscribedLayersWithDifferentSubtype) { +TEST(VmsUtilsTest, subscribedLayersForChange) { + testSubscribedLayers(VmsMessageType::SUBSCRIPTIONS_CHANGE); +} + +TEST(VmsUtilsTest, subscribedLayersForResponse) { + testSubscribedLayers(VmsMessageType::SUBSCRIPTIONS_RESPONSE); +} + +void testSubscribedLayersWithDifferentSubtype(VmsMessageType type) { VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1))}}; auto message = createBaseVmsMessage(2); - message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), + message->value.int32Values = hidl_vec<int32_t>{toInt(type), 1234, // sequence number 1, // number of layers 0, // number of associated layers @@ -289,36 +322,58 @@ TEST(VmsUtilsTest, subscribedLayersWithDifferentSubtype) { EXPECT_TRUE(getSubscribedLayers(*message, offers).empty()); } -TEST(VmsUtilsTest, subscribedLayersWithDifferentVersion) { +TEST(VmsUtilsTest, subscribedLayersWithDifferentSubtypeForChange) { + testSubscribedLayersWithDifferentSubtype(VmsMessageType::SUBSCRIPTIONS_CHANGE); +} + +TEST(VmsUtilsTest, subscribedLayersWithDifferentSubtypeForResponse) { + testSubscribedLayersWithDifferentSubtype(VmsMessageType::SUBSCRIPTIONS_RESPONSE); +} + +void subscribedLayersWithDifferentVersion(VmsMessageType type) { VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1))}}; auto message = createBaseVmsMessage(2); - message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), - 1234, // sequence number - 1, // number of layers - 0, // number of associated layers - 1, // layer 1 - 0, - 2}; // different version + message->value.int32Values = hidl_vec<int32_t>{toInt(type), + 1234, // sequence number + 1, // number of layers + 0, // number of associated layers + 1, // layer 1 + 0, 2}; // different version EXPECT_TRUE(isValidVmsMessage(*message)); EXPECT_TRUE(getSubscribedLayers(*message, offers).empty()); } -TEST(VmsUtilsTest, subscribedLayersWithDifferentPublisherId) { +TEST(VmsUtilsTest, subscribedLayersWithDifferentVersionForChange) { + subscribedLayersWithDifferentVersion(VmsMessageType::SUBSCRIPTIONS_CHANGE); +} + +TEST(VmsUtilsTest, subscribedLayersWithDifferentVersionForResponse) { + subscribedLayersWithDifferentVersion(VmsMessageType::SUBSCRIPTIONS_RESPONSE); +} + +void subscribedLayersWithDifferentPublisherId(VmsMessageType type) { VmsOffers offers = {123, {VmsLayerOffering(VmsLayer(1, 0, 1))}}; auto message = createBaseVmsMessage(2); - message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::SUBSCRIPTIONS_CHANGE), + message->value.int32Values = hidl_vec<int32_t>{toInt(type), 1234, // sequence number 0, // number of layers 1, // number of associated layers 1, // associated layer 1 - 0, - 1, + 0, 1, 1, // number of publisher IDs 234}; // publisher ID 1 EXPECT_TRUE(isValidVmsMessage(*message)); EXPECT_TRUE(getSubscribedLayers(*message, offers).empty()); } +TEST(VmsUtilsTest, subscribedLayersWithDifferentPublisherIdForChange) { + subscribedLayersWithDifferentPublisherId(VmsMessageType::SUBSCRIPTIONS_CHANGE); +} + +TEST(VmsUtilsTest, subscribedLayersWithDifferentPublisherIdForResponse) { + subscribedLayersWithDifferentPublisherId(VmsMessageType::SUBSCRIPTIONS_RESPONSE); +} + TEST(VmsUtilsTest, serviceNewlyStarted) { auto message = createBaseVmsMessage(2); message->value.int32Values = hidl_vec<int32_t>{toInt(VmsMessageType::AVAILABILITY_CHANGE), 0}; |