summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/vehicle_info_plugin/src/vehicle_info_plugin.cc
blob: 3017b6712f36cc6831b0f41a102ccf5ef8aeddc0 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 Copyright (c) 2018, Ford Motor Company
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.

 Redistributions in binary form must reproduce the above copyright notice,
 this list of conditions and the following
 disclaimer in the documentation and/or other materials provided with the
 distribution.

 Neither the name of the Ford Motor Company nor the names of its contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 */

#include "vehicle_info_plugin/vehicle_info_plugin.h"
#include "application_manager/message_helper.h"
#include "application_manager/plugin_manager/plugin_keys.h"
#include "application_manager/rpc_handler.h"
#include "application_manager/smart_object_keys.h"
#include "vehicle_info_plugin/custom_vehicle_data_manager_impl.h"
#include "vehicle_info_plugin/vehicle_info_app_extension.h"
#include "vehicle_info_plugin/vehicle_info_command_factory.h"

namespace vehicle_info_plugin {
CREATE_LOGGERPTR_GLOBAL(logger_, "VehicleInfoPlugin")

namespace strings = application_manager::strings;
namespace plugins = application_manager::plugin_manager;
namespace commands = application_manager::commands;

VehicleInfoPlugin::VehicleInfoPlugin() : application_manager_(nullptr) {}

bool VehicleInfoPlugin::Init(
    application_manager::ApplicationManager& app_manager,
    application_manager::rpc_service::RPCService& rpc_service,
    application_manager::HMICapabilities& hmi_capabilities,
    policy::PolicyHandlerInterface& policy_handler,
    resumption::LastState& last_state) {
  UNUSED(last_state);
  application_manager_ = &app_manager;
  custom_vehicle_data_manager_.reset(
      new CustomVehicleDataManagerImpl(policy_handler, rpc_service));
  command_factory_.reset(new vehicle_info_plugin::VehicleInfoCommandFactory(
      app_manager,
      rpc_service,
      hmi_capabilities,
      policy_handler,
      *(custom_vehicle_data_manager_.get())));
  return true;
}

bool VehicleInfoPlugin::IsAbleToProcess(
    const int32_t function_id, const commands::Command::CommandSource source) {
  return command_factory_->IsAbleToProcess(function_id, source);
}

std::string VehicleInfoPlugin::PluginName() {
  return plugins::plugin_names::vehicle_info_rpc_plugin;
}

app_mngr::CommandFactory& VehicleInfoPlugin::GetCommandFactory() {
  return *command_factory_;
}

void VehicleInfoPlugin::OnPolicyEvent(plugins::PolicyEvent event) {
  UnsubscribeFromRemovedVDItems();
  custom_vehicle_data_manager_->OnPolicyEvent(event);
}

void VehicleInfoPlugin::OnApplicationEvent(
    plugins::ApplicationEvent event,
    app_mngr::ApplicationSharedPtr application) {
  if (plugins::ApplicationEvent::kApplicationRegistered == event) {
    application->AddExtension(
        std::make_shared<VehicleInfoAppExtension>(*this, *application));
  } else if ((plugins::ApplicationEvent::kDeleteApplicationData == event) ||
             (plugins::ApplicationEvent::kApplicationUnregistered == event)) {
    DeleteSubscriptions(application);
  }
}

void VehicleInfoPlugin::UnsubscribeFromRemovedVDItems() {
  LOG4CXX_AUTO_TRACE(logger_);
  typedef std::vector<std::string> StringsVector;

  auto get_items_to_unsubscribe = [this]() -> StringsVector {
    StringsVector output_items_list;
    auto applications = application_manager_->applications();
    for (auto& app : applications.GetData()) {
      auto& ext = VehicleInfoAppExtension::ExtractVIExtension(*app);
      auto subscription_names = ext.Subscriptions();
      for (auto& subscription_name : subscription_names) {
        if (custom_vehicle_data_manager_->IsRemovedCustomVehicleDataName(
                subscription_name)) {
          ext.unsubscribeFromVehicleInfo(subscription_name);
          if (!helpers::in_range(output_items_list, subscription_name)) {
            LOG4CXX_DEBUG(logger_,
                          "Vehicle data item "
                              << subscription_name
                              << " has been removed by policy");
            output_items_list.push_back(subscription_name);
          }
        }
      }
    }
    return output_items_list;
  };

  const StringsVector items_to_unsubscribe = get_items_to_unsubscribe();

  if (items_to_unsubscribe.empty()) {
    LOG4CXX_DEBUG(logger_, "There is no data to unsubscribe");
    return;
  }

  auto message = GetUnsubscribeIVIRequest(items_to_unsubscribe);
  application_manager_->GetRPCService().ManageHMICommand(message);
}

void VehicleInfoPlugin::ProcessResumptionSubscription(
    application_manager::Application& app, VehicleInfoAppExtension& ext) {
  LOG4CXX_AUTO_TRACE(logger_);
  smart_objects::SmartObject msg_params =
      smart_objects::SmartObject(smart_objects::SmartType_Map);
  msg_params[strings::app_id] = app.app_id();
  const auto& subscriptions = ext.Subscriptions();
  if (subscriptions.empty()) {
    LOG4CXX_DEBUG(logger_, "No vehicle data to subscribe. Exiting");
    return;
  }

  for (const auto& item : subscriptions) {
    msg_params[item] = true;
  }

  smart_objects::SmartObjectSPtr request =
      application_manager::MessageHelper::CreateModuleInfoSO(
          hmi_apis::FunctionID::VehicleInfo_SubscribeVehicleData,
          *application_manager_);
  (*request)[strings::msg_params] = msg_params;
  application_manager_->GetRPCService().ManageHMICommand(request);
}

application_manager::ApplicationSharedPtr FindAppSubscribedToIVI(
    const std::string& ivi_name,
    application_manager::ApplicationManager& app_mngr) {
  auto applications = app_mngr.applications();

  for (auto& app : applications.GetData()) {
    auto& ext = VehicleInfoAppExtension::ExtractVIExtension(*app);
    if (ext.isSubscribedToVehicleInfo(ivi_name)) {
      return app;
    }
  }
  return application_manager::ApplicationSharedPtr();
}

smart_objects::SmartObjectSPtr VehicleInfoPlugin::GetUnsubscribeIVIRequest(
    const std::vector<std::string>& ivi_names) {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace smart_objects;

  auto msg_params = smart_objects::SmartObject(smart_objects::SmartType_Map);

  auto find_ivi_name = [](const std::string& ivi_name) {
    for (auto item : application_manager::MessageHelper::vehicle_data()) {
      if (ivi_name == item.first) {
        return item.first;
      }
    }
    return std::string();
  };

  for (const auto& ivi_name : ivi_names) {
    // try to find the name in vehicle data types
    std::string key_name = find_ivi_name(ivi_name);

    if (key_name.empty()) {
      // the name hasn't been found in vehicle data types
      if (custom_vehicle_data_manager_->IsValidCustomVehicleDataName(
              ivi_name) ||
          custom_vehicle_data_manager_->IsRemovedCustomVehicleDataName(
              ivi_name)) {
        key_name = ivi_name;
      }
    }

    DCHECK_OR_RETURN(!key_name.empty(), smart_objects::SmartObjectSPtr());
    msg_params[key_name] = true;
  }

  auto message = application_manager::MessageHelper::CreateMessageForHMI(
      hmi_apis::messageType::request,
      application_manager_->GetNextHMICorrelationID());
  DCHECK(message);

  SmartObject& object = *message;
  object[strings::params][strings::function_id] =
      hmi_apis::FunctionID::VehicleInfo_UnsubscribeVehicleData;

  object[strings::msg_params] = msg_params;
  return message;
}

void VehicleInfoPlugin::DeleteSubscriptions(
    application_manager::ApplicationSharedPtr app) {
  auto& ext = VehicleInfoAppExtension::ExtractVIExtension(*app);
  auto subscriptions = ext.Subscriptions();
  std::vector<std::string> ivi_to_unsubscribe;
  for (auto& ivi : subscriptions) {
    ext.unsubscribeFromVehicleInfo(ivi);
    auto still_subscribed_app =
        FindAppSubscribedToIVI(ivi, *application_manager_);
    if (!still_subscribed_app) {
      ivi_to_unsubscribe.push_back(ivi);
    }
  }

  if (!ivi_to_unsubscribe.empty()) {
    auto message = GetUnsubscribeIVIRequest(ivi_to_unsubscribe);
    application_manager_->GetRPCService().ManageHMICommand(message);
  }
}
}  // namespace vehicle_info_plugin

extern "C" __attribute__((visibility("default")))
application_manager::plugin_manager::RPCPlugin*
Create() {
  return new vehicle_info_plugin::VehicleInfoPlugin();
}

extern "C" __attribute__((visibility("default"))) void Delete(
    application_manager::plugin_manager::RPCPlugin* data) {
  delete data;
  DELETE_THREAD_LOGGER(vehicle_info_plugin::logger_);
}