summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/rc_rpc_plugin/src/rc_helpers.cc
blob: 8cf20948cf997034474441e726d55f5bca336d6c (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "rc_rpc_plugin/rc_helpers.h"
#include "application_manager/commands/command_impl.h"
#include "application_manager/message.h"
#include "application_manager/smart_object_keys.h"
#include "rc_rpc_plugin/rc_module_constants.h"
#include "rc_rpc_plugin/rc_rpc_plugin.h"

namespace rc_rpc_plugin {
CREATE_LOGGERPTR_GLOBAL(logger_, "RemoteControlModule");

const std::vector<std::string> RCHelpers::buttons_climate() {
  std::vector<std::string> data;
  data.push_back(enums_value::kACMax);
  data.push_back(enums_value::kAC);
  data.push_back(enums_value::kRecirculate);
  data.push_back(enums_value::kFanUp);
  data.push_back(enums_value::kFanDown);
  data.push_back(enums_value::kTempUp);
  data.push_back(enums_value::kTempDown);
  data.push_back(enums_value::kDefrostMax);
  data.push_back(enums_value::kDefrost);
  data.push_back(enums_value::kDefrostRear);
  data.push_back(enums_value::kUpperVent);
  data.push_back(enums_value::kLowerVent);
  return data;
}

const std::vector<std::string> RCHelpers::buttons_radio() {
  std::vector<std::string> data;
  data.push_back(enums_value::kVolumeUp);
  data.push_back(enums_value::kVolumeDown);
  data.push_back(enums_value::kEject);
  data.push_back(enums_value::kSource);
  data.push_back(enums_value::kShuffle);
  data.push_back(enums_value::kRepeat);
  return data;
}

const RCHelpers::ButtonsMap RCHelpers::buttons_map() {
  using namespace mobile_apis;

  ButtonsMap buttons_map;
  buttons_map[enums_value::kACMax] = ButtonName::AC_MAX;
  buttons_map[enums_value::kAC] = ButtonName::AC;
  buttons_map[enums_value::kRecirculate] = ButtonName::RECIRCULATE;
  buttons_map[enums_value::kFanUp] = ButtonName::FAN_UP;
  buttons_map[enums_value::kFanDown] = ButtonName::FAN_DOWN;
  buttons_map[enums_value::kTempUp] = ButtonName::TEMP_UP;
  buttons_map[enums_value::kTempDown] = ButtonName::TEMP_DOWN;
  buttons_map[enums_value::kDefrostMax] = ButtonName::DEFROST_MAX;
  buttons_map[enums_value::kDefrost] = ButtonName::DEFROST;
  buttons_map[enums_value::kDefrostRear] = ButtonName::DEFROST_REAR;
  buttons_map[enums_value::kUpperVent] = ButtonName::UPPER_VENT;
  buttons_map[enums_value::kLowerVent] = ButtonName::LOWER_VENT;
  buttons_map[enums_value::kVolumeUp] = ButtonName::VOLUME_UP;
  buttons_map[enums_value::kVolumeDown] = ButtonName::VOLUME_DOWN;
  buttons_map[enums_value::kEject] = ButtonName::EJECT;
  buttons_map[enums_value::kSource] = ButtonName::SOURCE;
  buttons_map[enums_value::kShuffle] = ButtonName::SHUFFLE;
  buttons_map[enums_value::kRepeat] = ButtonName::REPEAT;

  return buttons_map;
}

std::vector<std::string> RCHelpers::GetModuleReadOnlyParams(
    const std::string& module_type) {
  using namespace message_params;
  std::vector<std::string> module_ro_params;
  if (enums_value::kClimate == module_type) {
    module_ro_params.push_back(kCurrentTemperature);
  } else if (enums_value::kRadio == module_type) {
    module_ro_params.push_back(kRdsData);
    module_ro_params.push_back(kAvailableHDs);
    module_ro_params.push_back(kAvailableHdChannels);
    module_ro_params.push_back(kSignalStrength);
    module_ro_params.push_back(kSignalChangeThreshold);
    module_ro_params.push_back(kState);
    module_ro_params.push_back(kSisData);
  } else if (enums_value::kLight == module_type) {
    module_ro_params.push_back(kLightStatus);
  }

  return module_ro_params;
}

rc_rpc_types::ModuleIdConsentVector RCHelpers::FillModuleConsents(
    const std::string& module_type,
    const std::vector<std::string>& module_ids,
    const std::vector<bool> allowed) {
  using namespace rc_rpc_types;
  if (module_ids.size() != allowed.size()) {
    return rc_rpc_types::ModuleIdConsentVector();
  }

  rc_rpc_types::ModuleIdConsentVector module_consents;
  std::time_t current_date = std::time(0);
  size_t array_size = module_ids.size();

  for (size_t i = 0; i < array_size; ++i) {
    rc_rpc_types::ModuleIdConsent module_consent;
    module_consent.module_id = {module_type, module_ids[i]};
    module_consent.consent =
        allowed[i] ? ModuleConsent::CONSENTED : ModuleConsent::NOT_CONSENTED;
    module_consent.date_of_consent = current_date;

    module_consents.push_back(module_consent);
  }
  return module_consents;
}

std::vector<std::string> RCHelpers::RetrieveModuleIds(
    const ns_smart_device_link::ns_smart_objects::SmartObject& moduleIds) {
  std::vector<std::string> module_ids;
  for (const auto& module_id : (*moduleIds.asArray())) {
    module_ids.push_back(module_id.asString());
  }
  return module_ids;
}

std::vector<bool> RCHelpers::RetrieveModuleConsents(
    const ns_smart_device_link::ns_smart_objects::SmartObject& consents) {
  std::vector<bool> module_consents;
  for (const auto& allowed_item : (*consents.asArray())) {
    module_consents.push_back(allowed_item.asBool());
  }
  return module_consents;
}

const std::function<std::string(const std::string& module_type)>
RCHelpers::GetModuleTypeToDataMapping() {
  auto mapping_lambda = [](const std::string& module_type) -> std::string {
    static std::map<std::string, std::string> mapping = {
        {enums_value::kClimate, message_params::kClimateControlData},
        {enums_value::kRadio, message_params::kRadioControlData},
        {enums_value::kSeat, message_params::kSeatControlData},
        {enums_value::kAudio, message_params::kAudioControlData},
        {enums_value::kLight, message_params::kLightControlData},
        {enums_value::kHmiSettings, message_params::kHmiSettingsControlData}};
    auto it = mapping.find(module_type);
    if (mapping.end() == it) {
      LOG4CXX_ERROR(logger_, "Unknown module type" << module_type);
      return std::string();
    }
    return it->second;
  };

  return mapping_lambda;
}

const std::function<std::string(const std::string& module_type)>
RCHelpers::GetModuleTypeToCapabilitiesMapping() {
  auto mapping_lambda = [](const std::string& module_type) -> std::string {
    static std::map<std::string, std::string> mapping = {
        {enums_value::kClimate, strings::kclimateControlCapabilities},
        {enums_value::kRadio, strings::kradioControlCapabilities},
        {enums_value::kSeat, strings::kseatControlCapabilities},
        {enums_value::kAudio, strings::kaudioControlCapabilities},
        {enums_value::kLight, strings::klightControlCapabilities},
        {enums_value::kHmiSettings, strings::khmiSettingsControlCapabilities}};
    auto it = mapping.find(module_type);
    if (mapping.end() == it) {
      LOG4CXX_ERROR(logger_, "Unknown module type" << module_type);
      return std::string();
    }
    return it->second;
  };

  return mapping_lambda;
}

const std::vector<std::string> RCHelpers::GetModuleTypesList() {
  using namespace enums_value;
  return {kClimate, kRadio, kSeat, kAudio, kLight, kHmiSettings};
}

RCAppExtensionPtr RCHelpers::GetRCExtension(
    application_manager::Application& app) {
  auto extension_interface = app.QueryInterface(RCRPCPlugin::kRCPluginID);
  auto extension =
      std::static_pointer_cast<RCAppExtension>(extension_interface);
  return extension;
}

smart_objects::SmartObjectSPtr RCHelpers::CreateUnsubscribeRequestToHMI(
    const ModuleUid& module, const uint32_t correlation_id) {
  using namespace smart_objects;
  namespace commands = application_manager::commands;
  namespace am_strings = application_manager::strings;

  SmartObjectSPtr message = std::make_shared<SmartObject>(SmartType_Map);
  SmartObject& params = (*message)[am_strings::params];
  SmartObject& msg_params = (*message)[am_strings::msg_params];

  params[am_strings::message_type] =
      static_cast<int>(application_manager::kRequest);
  params[am_strings::protocol_version] =
      commands::CommandImpl::protocol_version_;
  params[am_strings::protocol_type] = commands::CommandImpl::hmi_protocol_type_;
  params[am_strings::correlation_id] = correlation_id;
  params[am_strings::function_id] =
      hmi_apis::FunctionID::RC_GetInteriorVehicleData;
  msg_params[message_params::kSubscribe] = false;
  msg_params[message_params::kModuleType] = module.first;
  msg_params[message_params::kModuleId] = module.second;
  return message;
}

std::vector<application_manager::ApplicationSharedPtr>
RCHelpers::AppsSubscribedToModule(
    application_manager::ApplicationManager& app_mngr,
    const ModuleUid& module) {
  std::vector<application_manager::ApplicationSharedPtr> result;
  auto rc_apps = RCRPCPlugin::GetRCApplications(app_mngr);
  for (auto& app : rc_apps) {
    auto rc_ext = RCHelpers::GetRCExtension(*app);
    DCHECK_OR_RETURN(rc_ext, result);
    if (rc_ext->IsSubscribedToInteriorVehicleData(module)) {
      result.push_back(app);
    }
  }
  return result;
}

std::vector<application_manager::ApplicationSharedPtr>
RCHelpers::AppsSubscribedToModuleType(
    application_manager::ApplicationManager& app_mngr,
    const std::string& module_type) {
  std::vector<application_manager::ApplicationSharedPtr> result;
  auto rc_apps = RCRPCPlugin::GetRCApplications(app_mngr);
  for (auto& app : rc_apps) {
    auto rc_ext = RCHelpers::GetRCExtension(*app);
    DCHECK_OR_RETURN(rc_ext, result);
    if (rc_ext->IsSubscribedToInteriorVehicleDataOfType(module_type)) {
      result.push_back(app);
    }
  }
  return result;
}

RCHelpers::AppsModuleTypes RCHelpers::GetApplicationsAllowedModuleTypes(
    app_mngr::ApplicationManager& app_mngr) {
  auto apps_list = RCRPCPlugin::GetRCApplications(app_mngr);
  RCHelpers::AppsModuleTypes result;
  for (auto& app_ptr : apps_list) {
    std::vector<std::string> allowed_modules;
    app_mngr.GetPolicyHandler().GetModuleTypes(app_ptr->policy_app_id(),
                                               &allowed_modules);
    std::sort(allowed_modules.begin(), allowed_modules.end());
    result[app_ptr] = allowed_modules;
  }
  return result;
}

void RCHelpers::RemoveRedundantGPSDataFromIVDataMsg(
    smart_objects::SmartObject& msg_params) {
  using namespace message_params;
  using namespace application_manager::strings;

  LOG4CXX_AUTO_TRACE(logger_);
  auto& module_data = msg_params[kModuleData];
  if (!module_data.keyExists(kRadioControlData) ||
      !module_data[kRadioControlData].keyExists(kSisData) ||
      !module_data[kRadioControlData][kSisData].keyExists(station_location)) {
    return;
  }

  auto& location_data =
      module_data[kRadioControlData][kSisData][station_location];
  auto new_location_data =
      smart_objects::SmartObject(smart_objects::SmartType_Map);
  new_location_data[latitude_degrees] = location_data[latitude_degrees];
  new_location_data[longitude_degrees] = location_data[longitude_degrees];
  if (location_data.keyExists(altitude)) {
    new_location_data[altitude] = location_data[altitude];

    location_data = new_location_data;
  }
}

smart_objects::SmartObject RCHelpers::MergeModuleData(
    const smart_objects::SmartObject& data1,
    const smart_objects::SmartObject& data2) {
  if (data1.getType() != smart_objects::SmartType::SmartType_Map ||
      data2.getType() != smart_objects::SmartType::SmartType_Map) {
    return data2;
  }

  smart_objects::SmartObject result = data1;

  for (auto it = data2.map_begin(); it != data2.map_end(); ++it) {
    const std::string& key = it->first;
    smart_objects::SmartObject& value = it->second;
    if (!result.keyExists(key) || value.getType() != result[key].getType()) {
      result[key] = value;
      continue;
    }

    // Merge maps and arrays with `id` param included, replace other types
    if (value.getType() == smart_objects::SmartType::SmartType_Map) {
      value = MergeModuleData(result[key], value);
    } else if (value.getType() == smart_objects::SmartType::SmartType_Array) {
      value = MergeArray(result[key], value);
    }
    result[key] = value;
  }
  return result;
}

smart_objects::SmartObject RCHelpers::MergeArray(
    const smart_objects::SmartObject& data1,
    const smart_objects::SmartObject& data2) {
  // Merge data only in the case where each value in the array is an Object with
  // an ID included, otherwise replace

  if (data1.getType() != smart_objects::SmartType::SmartType_Array ||
      data2.getType() != smart_objects::SmartType::SmartType_Array ||
      data2.empty()) {
    return data2;
  }

  auto& data2_array = *data2.asArray();
  for (const auto& data_item : data2_array) {
    if (data_item.getType() != smart_objects::SmartType_Map ||
        !data_item.keyExists(application_manager::strings::id)) {
      return data2;
    }
  }

  smart_objects::SmartObject result = data1;
  smart_objects::SmartArray* result_array = result.asArray();

  auto find_by_id = [](smart_objects::SmartArray* array,
                       const smart_objects::SmartObject& id)
      -> smart_objects::SmartArray::iterator {
    return std::find_if(array->begin(),
                        array->end(),
                        [&id](const smart_objects::SmartObject& obj) -> bool {
                          return id == obj[application_manager::strings::id];
                        });
  };

  auto merge = [&result_array,
                &find_by_id](const smart_objects::SmartObject& data) -> void {
    auto element_id = data[application_manager::strings::id];
    auto result_it = find_by_id(result_array, element_id);

    if (result_array->end() != result_it) {
      *result_it = RCHelpers::MergeModuleData(*result_it, data);
    } else {
      result_array->push_back(data);
    }
  };

  for (const auto& data : data2_array) {
    merge(data);
  }

  return result;
}

}  // namespace rc_rpc_plugin