Skip to content

Commit

Permalink
Revert expired button requests if response was received after timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ychernysheva committed Jul 15, 2021
1 parent de6c7aa commit 94c770b
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1163,52 +1163,6 @@ class Application : public virtual InitialApplicationData,
*/
virtual const std::list<AppExtensionPtr>& Extensions() const = 0;

/**
* @brief Get map of pending button subscription requests correlation ids
* to button names
* @return pending button subscriptions map
*/
virtual const ButtonSubscriptionsMap& PendingButtonSubscriptions() const = 0;

/**
* @brief Add pending button subscription request
* @param correlation_id - correlation id of subscription request
* @param button_name - enum value indicating button name
*/
virtual void AddPendingButtonSubscription(
const int32_t correlation_id,
const hmi_apis::Common_ButtonName::eType button_name) = 0;

/**
* @brief Remove pending button subscription request
* @param correlation_id - correlation id of subscription request
*/
virtual void RemovePendingSubscriptionButton(
const int32_t correlation_id) = 0;

/**
* @brief Get map of pending button unsubscription requests correlation ids
* to button names
* @return pending button unsubscriptions map
*/
virtual const ButtonSubscriptionsMap& PendingButtonUnsubscriptions()
const = 0;

/**
* @brief Add pending button unsubscription request
* @param correlation_id - correlation id of unsubscription request
* @param button_name - enum value indicating button name
*/
virtual void AddPendingButtonUnsubscription(
const int32_t correlation_id,
const hmi_apis::Common_ButtonName::eType button_name) = 0;
/**
* @brief Remove pending button unsubscription request
* @param correlation_id - correlation id of unsubscription request
*/
virtual void RemovePendingButtonUnsubscription(
const int32_t correlation_id) = 0;

/**
* @brief Get cloud app endpoint for websocket connection
* @return cloud app endpoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,22 +510,6 @@ class ApplicationImpl : public virtual Application,

const smart_objects::SmartObject& get_user_location() const OVERRIDE;

const ButtonSubscriptionsMap& PendingButtonSubscriptions() const OVERRIDE;

void AddPendingButtonSubscription(
const int32_t correlation_id,
const hmi_apis::Common_ButtonName::eType button_name) OVERRIDE;

void RemovePendingSubscriptionButton(const int32_t correlation_id) OVERRIDE;

const ButtonSubscriptionsMap& PendingButtonUnsubscriptions() const OVERRIDE;

void AddPendingButtonUnsubscription(
const int32_t correlation_id,
const hmi_apis::Common_ButtonName::eType button_name) OVERRIDE;

void RemovePendingButtonUnsubscription(const int32_t correlation_id) OVERRIDE;

protected:
/**
* @brief Clean up application folder. Persistent files will stay
Expand Down Expand Up @@ -596,7 +580,6 @@ class ApplicationImpl : public virtual Application,
sync_primitives::Lock video_streaming_suspended_lock_;
sync_primitives::Lock audio_streaming_suspended_lock_;
sync_primitives::Lock streaming_stop_lock_;
sync_primitives::Lock pending_button_subscription_lock_;

bool is_app_allowed_;
bool is_app_data_resumption_allowed_;
Expand All @@ -617,8 +600,6 @@ class ApplicationImpl : public virtual Application,
std::string bundle_id_;
AppFilesMap app_files_;
std::set<mobile_apis::ButtonName::eType> subscribed_buttons_;
ButtonSubscriptionsMap pending_button_subscriptions_;
ButtonSubscriptionsMap pending_button_unsubscriptions_;
UsageStatistics usage_report_;
HelpPromptManagerImpl help_prompt_manager_impl_;
protocol_handler::MajorProtocolVersion protocol_version_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ enum VRTTSSessionChanging { kVRSessionChanging = 0, kTTSSessionChanging };
typedef std::map<protocol_handler::ServiceType, std::set<uint32_t> >
ServiceStreamingStatusMap;

typedef std::map<const int32_t, ExpiredButtonRequestData>
ExpiredButtonRequestsMap;

struct CommandParametersPermissions;
typedef std::map<std::string, hmi_apis::Common_TransportType::eType>
DeviceTypes;
Expand Down Expand Up @@ -1544,6 +1547,16 @@ class ApplicationManagerImpl
static std::vector<std::string> ConvertRejectedParamList(
const std::vector<std::string>& input);

void AddExpiredButtonRequest(
const uint32_t app_id,
const int32_t corr_id,
const hmi_apis::Common_ButtonName::eType button_name) OVERRIDE;

utils::Optional<ExpiredButtonRequestData> GetExpiredButtonRequestData(
const int32_t corr_id) const OVERRIDE;

void DeleteExpiredButtonRequest(const int32_t corr_id) OVERRIDE;

private:
const ApplicationManagerSettings& settings_;
/**
Expand Down Expand Up @@ -1705,6 +1718,9 @@ class ApplicationManagerImpl
ServiceStreamingStatusMap streaming_application_services_;
sync_primitives::Lock streaming_services_lock_;

mutable sync_primitives::Lock expired_button_requests_lock_;
mutable ExpiredButtonRequestsMap expired_button_requests_;

#ifdef BUILD_TESTS
public:
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,16 @@ void SubscribeButtonRequest::Run() {

subscribe_on_event(hmi_apis::FunctionID::Buttons_SubscribeButton,
correlation_id());

ApplicationSharedPtr app =
application_manager_.application_by_hmi_app(application_id());

if (!app) {
SDL_LOG_ERROR("Application for connection key: " << application_id()
<< " was not found");
return;
}

app->AddPendingButtonSubscription(correlation_id(), button_name_);

SendRequest();
}

void SubscribeButtonRequest::onTimeOut() {
SDL_LOG_AUTO_TRACE();

auto& resume_ctrl = application_manager_.resume_controller();

ApplicationSharedPtr app =
application_manager_.application_by_hmi_app(application_id());

if (!app) {
SDL_LOG_ERROR("Application for app id: " << application_id()
<< " was not found");
return;
}

app->RemovePendingSubscriptionButton(correlation_id());
application_manager_.AddExpiredButtonRequest(
application_id(), correlation_id(), button_name_);

auto& resume_ctrl = application_manager_.resume_controller();
resume_ctrl.HandleOnTimeOut(
correlation_id(),
static_cast<hmi_apis::FunctionID::eType>(function_id()));
Expand Down Expand Up @@ -133,44 +112,25 @@ void SubscribeButtonRequest::on_event(const event_engine::Event& event) {
static_cast<hmi_apis::Common_Result::eType>(
message[strings::params][hmi_response::code].asInt());

const auto pending_button_subscriptions = app->PendingButtonSubscriptions();

const auto it =
pending_button_subscriptions.find(event.smart_object_correlation_id());

const bool is_pending = it != pending_button_subscriptions.end();

const mobile_apis::ButtonName::eType btn_id =
static_cast<mobile_apis::ButtonName::eType>(
(*message_)[strings::msg_params][strings::button_name].asInt());

if (CommandImpl::IsHMIResultSuccess(hmi_result,
HmiInterfaces::HMI_INTERFACE_Buttons) &&
is_pending) {
HmiInterfaces::HMI_INTERFACE_Buttons)) {
app->SubscribeToButton(static_cast<mobile_apis::ButtonName::eType>(btn_id));
app->RemovePendingSubscriptionButton(correlation_id());
} else if (ShouldUnsubscribeIntertally(hmi_result, *app)) {
app->UnsubscribeFromButton(
static_cast<mobile_apis::ButtonName::eType>(btn_id));
app->RemovePendingSubscriptionButton(correlation_id());
} else if (!is_pending) {
smart_objects::SmartObjectSPtr msg =
MessageHelper::CreateButtonSubscriptionHandlingRequestToHmi(
application_id(),
static_cast<hmi_apis::Common_ButtonName::eType>(button_name_),
hmi_apis::FunctionID::Buttons_UnsubscribeButton,
application_manager_);

rpc_service_.SendMessageToHMI(msg);
}
}

bool SubscribeButtonRequest::ShouldUnsubscribeIntertally(
const hmi_apis::Common_Result::eType hmi_result,
const app_mngr::Application& app) const {
return (hmi_result != hmi_apis::Common_Result::SUCCESS &&
hmi_result != hmi_apis::Common_Result::WARNINGS) &&
(app.is_resuming());
return (!CommandImpl::IsHMIResultSuccess(
hmi_result, HmiInterfaces::HMI_INTERFACE_Buttons) &&
app.is_resuming());
}

} // namespace hmi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
#include "sdl_rpc_plugin/commands/hmi/subscribe_button_response.h"
#include "application_manager/event_engine/event.h"
#include "application_manager/message_helper.h"

namespace sdl_rpc_plugin {
using namespace application_manager;
Expand All @@ -55,6 +56,37 @@ SubscribeButtonResponse::~SubscribeButtonResponse() {}

void SubscribeButtonResponse::Run() {
SDL_LOG_AUTO_TRACE();

hmi_apis::Common_Result::eType hmi_result =
static_cast<hmi_apis::Common_Result::eType>(
(*message_)
.getElement(strings::params)
.getElement(hmi_response::code)
.asInt());

const auto expired_request_data =
application_manager_.GetExpiredButtonRequestData(correlation_id());
if (expired_request_data) {
const uint32_t app_id = (*expired_request_data).app_id_;
const auto button_name = (*expired_request_data).button_name_;
application_manager_.DeleteExpiredButtonRequest(correlation_id());

if (!CommandImpl::IsHMIResultSuccess(
hmi_result, HmiInterfaces::HMI_INTERFACE_Buttons)) {
return;
}

smart_objects::SmartObjectSPtr msg =
MessageHelper::CreateButtonSubscriptionHandlingRequestToHmi(
app_id,
button_name,
hmi_apis::FunctionID::Buttons_UnsubscribeButton,
application_manager_);

rpc_service_.SendMessageToHMI(msg);
return;
}

event_engine::Event event(hmi_apis::FunctionID::Buttons_SubscribeButton);
event.set_smart_object(*message_);
event.raise(application_manager_.event_dispatcher());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,40 +71,19 @@ void UnsubscribeButtonRequest::Run() {

subscribe_on_event(hmi_apis::FunctionID::Buttons_UnsubscribeButton,
correlation_id());

ApplicationSharedPtr app =
application_manager_.application_by_hmi_app(application_id());

if (!app) {
SDL_LOG_ERROR("Application for connection key: " << application_id()
<< " was not found");
return;
}

app->AddPendingButtonUnsubscription(correlation_id(), button_name_);

SendRequest();
}

void UnsubscribeButtonRequest::onTimeOut() {
SDL_LOG_AUTO_TRACE();

auto& resume_ctrl = application_manager_.resume_controller();
application_manager_.AddExpiredButtonRequest(
application_id(), correlation_id(), button_name_);

auto& resume_ctrl = application_manager_.resume_controller();
resume_ctrl.HandleOnTimeOut(
correlation_id(),
static_cast<hmi_apis::FunctionID::eType>(function_id()));

ApplicationSharedPtr app =
application_manager_.application_by_hmi_app(application_id());

if (!app) {
SDL_LOG_ERROR("Application for connection key: " << application_id()
<< " was not found");
return;
}

app->RemovePendingButtonUnsubscription(correlation_id());
}

void UnsubscribeButtonRequest::on_event(const event_engine::Event& event) {
Expand All @@ -120,36 +99,17 @@ void UnsubscribeButtonRequest::on_event(const event_engine::Event& event) {
return;
}

const auto pending_button_unsubscriptions =
app->PendingButtonUnsubscriptions();

const auto it =
pending_button_unsubscriptions.find(event.smart_object_correlation_id());

const bool is_pending = it != pending_button_unsubscriptions.end();

hmi_apis::Common_Result::eType hmi_result =
static_cast<hmi_apis::Common_Result::eType>(
message[strings::params][hmi_response::code].asInt());

if (CommandImpl::IsHMIResultSuccess(hmi_result,
HmiInterfaces::HMI_INTERFACE_Buttons) &&
is_pending) {
HmiInterfaces::HMI_INTERFACE_Buttons)) {
const mobile_apis::ButtonName::eType btn_id =
static_cast<mobile_apis::ButtonName::eType>(
(*message_)[strings::msg_params][strings::button_name].asInt());
app->UnsubscribeFromButton(
static_cast<mobile_apis::ButtonName::eType>(btn_id));
app->RemovePendingButtonUnsubscription(correlation_id());
} else if (!is_pending) {
smart_objects::SmartObjectSPtr msg =
MessageHelper::CreateButtonSubscriptionHandlingRequestToHmi(
application_id(),
static_cast<hmi_apis::Common_ButtonName::eType>(button_name_),
hmi_apis::FunctionID::Buttons_SubscribeButton,
application_manager_);

rpc_service_.SendMessageToHMI(msg);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
#include "sdl_rpc_plugin/commands/hmi/unsubscribe_button_response.h"
#include "application_manager/event_engine/event.h"
#include "application_manager/message_helper.h"

namespace sdl_rpc_plugin {
using namespace application_manager;
Expand All @@ -56,6 +57,37 @@ UnsubscribeButtonResponse::~UnsubscribeButtonResponse() {}

void UnsubscribeButtonResponse::Run() {
SDL_LOG_AUTO_TRACE();

hmi_apis::Common_Result::eType hmi_result =
static_cast<hmi_apis::Common_Result::eType>(
(*message_)
.getElement(strings::params)
.getElement(hmi_response::code)
.asInt());

const auto expired_request_data =
application_manager_.GetExpiredButtonRequestData(correlation_id());
if (expired_request_data) {
const uint32_t app_id = (*expired_request_data).app_id_;
const auto button_name = (*expired_request_data).button_name_;
application_manager_.DeleteExpiredButtonRequest(correlation_id());

if (!CommandImpl::IsHMIResultSuccess(
hmi_result, HmiInterfaces::HMI_INTERFACE_Buttons)) {
return;
}

smart_objects::SmartObjectSPtr msg =
MessageHelper::CreateButtonSubscriptionHandlingRequestToHmi(
app_id,
button_name,
hmi_apis::FunctionID::Buttons_SubscribeButton,
application_manager_);

rpc_service_.SendMessageToHMI(msg);
return;
}

event_engine::Event event(hmi_apis::FunctionID::Buttons_UnsubscribeButton);
event.set_smart_object(*message_);
event.raise(application_manager_.event_dispatcher());
Expand Down
Loading

0 comments on commit 94c770b

Please sign in to comment.