summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/rc_rpc_plugin/src/interior_data_manager_impl.cc
blob: c295ad4f1113b86063ff4a62943636623a50a12b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "rc_rpc_plugin/interior_data_manager_impl.h"
#include "application_manager/application_manager.h"
#include "application_manager/rpc_service.h"
#include "rc_rpc_plugin/rc_helpers.h"
#include "rc_rpc_plugin/rc_rpc_plugin.h"

namespace rc_rpc_plugin {
SDL_CREATE_LOG_VARIABLE("RemoteControlModule");

InteriorDataManagerImpl::InteriorDataManagerImpl(
    RCRPCPlugin& rc_plugin,
    InteriorDataCache& cache,
    application_manager::ApplicationManager& app_mngr,
    application_manager::rpc_service::RPCService& rpc_service)
    : rc_plugin_(rc_plugin)
    , cache_(cache)
    , app_mngr_(app_mngr)
    , rpc_service_(rpc_service) {}

void InteriorDataManagerImpl::OnPolicyEvent(plugins::PolicyEvent event) {
  UpdateHMISubscriptionsOnPolicyUpdated();
}

void InteriorDataManagerImpl::OnApplicationEvent(
    plugins::ApplicationEvent event,
    app_mngr::ApplicationSharedPtr application) {
  if (plugins::ApplicationEvent::kApplicationUnregistered == event ||
      plugins::ApplicationEvent::kApplicationExit == event) {
    UpdateHMISubscriptionsOnAppUnregistered(*application);
  }
}

void InteriorDataManagerImpl::OnDisablingRC() {
  SDL_LOG_AUTO_TRACE();
  auto existing_subscription = AppsSubscribedModules();
  std::set<ModuleUid> subscribed_modules;
  for (auto& pair : existing_subscription) {
    auto& app = pair.first;
    auto rc_extension = RCHelpers::GetRCExtension(*app);
    for (const auto& module : pair.second) {
      subscribed_modules.insert(module);
      rc_extension->UnsubscribeFromInteriorVehicleData(module);
    }
  }
  for (auto& module : subscribed_modules) {
    SDL_LOG_TRACE("unsubscribe from module type: " << module.first
                                                   << " id: " << module.second);
    UnsubscribeFromInteriorVehicleData(module);
  }
}

void InteriorDataManagerImpl::OnResumptionRevert(
    const std::set<ModuleUid>& subscriptions) {
  for (const auto& module : subscriptions) {
    UnsubscribeFromInteriorVehicleData(module);
  }
}

void InteriorDataManagerImpl::StoreRequestToHMITime(const ModuleUid& module) {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock autolock(requests_to_hmi_history_lock_);
  requests_to_hmi_history_[module].push_back(date_time::getCurrentTime());
}

bool InteriorDataManagerImpl::CheckRequestsToHMIFrequency(
    const ModuleUid& module) {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock autolock(requests_to_hmi_history_lock_);
  ClearOldRequestsToHMIHistory();
  const auto& history = requests_to_hmi_history_[module];
  const auto limit =
      app_mngr_.get_settings().get_interior_vehicle_data_frequency().first;
  return history.size() < limit;
}

void InteriorDataManagerImpl::UpdateHMISubscriptionsOnPolicyUpdated() {
  auto apps_allowed_module_types =
      RCHelpers::GetApplicationsAllowedModuleTypes(app_mngr_);
  auto apps_subscribed_module_types = AppsSubscribedModuleTypes();
  InteriorDataManagerImpl::AppsModuleTypes apps_disallowed_modules;
  for (auto& pair : apps_subscribed_module_types) {
    auto& allowed = apps_allowed_module_types[pair.first];
    auto& subscribed = pair.second;
    std::vector<std::string> disallowed_modules;
    std::set_difference(subscribed.begin(),
                        subscribed.end(),
                        allowed.begin(),
                        allowed.end(),
                        std::back_inserter(disallowed_modules));

    std::sort(disallowed_modules.begin(), disallowed_modules.end());

    auto unique_result =
        std::unique(disallowed_modules.begin(), disallowed_modules.end());

    disallowed_modules.erase(unique_result, disallowed_modules.end());
    apps_disallowed_modules[pair.first] = disallowed_modules;
  }

  for (auto& pair : apps_disallowed_modules) {
    auto& app = pair.first;
    auto rc_extension = RCHelpers::GetRCExtension(*app);
    for (const auto& module_type : pair.second) {
      rc_extension->UnsubscribeFromInteriorVehicleDataOfType(module_type);
      auto apps_subscribed =
          RCHelpers::AppsSubscribedToModuleType(app_mngr_, module_type);
      if (apps_subscribed.empty()) {
        UnsubscribeFromInteriorVehicleDataOfType(module_type);
      }
    }
  }
}

void InteriorDataManagerImpl::UpdateHMISubscriptionsOnAppUnregistered(
    application_manager::Application& app) {
  SDL_LOG_AUTO_TRACE();
  auto rc_extension = RCHelpers::GetRCExtension(app);
  auto subscribed_data = rc_extension->InteriorVehicleDataSubscriptions();
  rc_extension->UnsubscribeFromInteriorVehicleData();
  for (auto& data : subscribed_data) {
    auto apps_subscribed = RCHelpers::AppsSubscribedToModule(app_mngr_, data);
    if (apps_subscribed.empty()) {
      UnsubscribeFromInteriorVehicleData(data);
    }
    if (apps_subscribed.size() == 1 &&
        apps_subscribed.front()->hmi_app_id() == app.hmi_app_id()) {
      UnsubscribeFromInteriorVehicleData(data);
    }
  }
}

void InteriorDataManagerImpl::UnsubscribeFromInteriorVehicleData(
    const ModuleUid& module) {
  cache_.Remove(module);
  auto unsubscribe_request = RCHelpers::CreateGetInteriorVDRequestToHMI(
      module,
      app_mngr_.GetNextHMICorrelationID(),
      RCHelpers::InteriorDataAction::UNSUBSCRIBE);
  SDL_LOG_DEBUG("Send Unsubscribe from module type: " << module.first << " id: "
                                                      << module.second);
  rpc_service_.ManageHMICommand(unsubscribe_request);
}

void InteriorDataManagerImpl::UnsubscribeFromInteriorVehicleDataOfType(
    const std::string& module_type) {
  const auto& modules = cache_.GetCachedModulesByType(module_type);

  for (const auto& module : modules) {
    cache_.Remove(module);
    auto unsubscribe_request = RCHelpers::CreateGetInteriorVDRequestToHMI(
        module,
        app_mngr_.GetNextHMICorrelationID(),
        RCHelpers::InteriorDataAction::UNSUBSCRIBE);
    SDL_LOG_DEBUG("Send Unsubscribe from module type: "
                  << module.first << " id: " << module.second);
    rpc_service_.ManageHMICommand(unsubscribe_request);
  }
}

void InteriorDataManagerImpl::ClearOldRequestsToHMIHistory() {
  auto limit =
      app_mngr_.get_settings().get_interior_vehicle_data_frequency().second;
  uint32_t time_frame = limit * date_time::MILLISECONDS_IN_SECOND;
  auto lest_that_time_frame_ago = [time_frame](date_time::TimeDuration time) {
    auto span = date_time::calculateTimeSpan(time);
    return span < time_frame;
  };
  for (auto& it : requests_to_hmi_history_) {
    auto& history = it.second;
    auto first_actual =
        std::find_if(history.begin(), history.end(), lest_that_time_frame_ago);
    history.erase(history.begin(), first_actual);
  }
}

InteriorDataManagerImpl::AppsModules
InteriorDataManagerImpl::AppsSubscribedModules() {
  auto apps_list = RCRPCPlugin::GetRCApplications(app_mngr_);
  InteriorDataManagerImpl::AppsModules result;
  for (auto& app_ptr : apps_list) {
    const auto rc_extension = RCHelpers::GetRCExtension(*app_ptr);
    auto app_subscriptions = rc_extension->InteriorVehicleDataSubscriptions();
    result[app_ptr] = std::vector<ModuleUid>(app_subscriptions.size());
    std::copy(app_subscriptions.begin(),
              app_subscriptions.end(),
              result[app_ptr].begin());
  }
  return result;
}

InteriorDataManagerImpl::AppsModuleTypes
InteriorDataManagerImpl::AppsSubscribedModuleTypes() {
  auto apps_list = RCRPCPlugin::GetRCApplications(app_mngr_);
  RCHelpers::AppsModuleTypes result;
  for (auto& app_ptr : apps_list) {
    const auto rc_extension = RCHelpers::GetRCExtension(*app_ptr);
    auto app_subscriptions = rc_extension->InteriorVehicleDataSubscriptions();
    std::vector<std::string> app_module_types;

    std::transform(app_subscriptions.begin(),
                   app_subscriptions.end(),
                   std::back_inserter(app_module_types),
                   [](const ModuleUid& app_subscription) {
                     return app_subscription.first;
                   });

    std::sort(app_module_types.begin(), app_module_types.end());
    result[app_ptr] = app_module_types;
  }
  return result;
}
}  // namespace rc_rpc_plugin