summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/sdl_rpc_plugin/src/commands/mobile/add_command_request.cc
blob: ddd2c9db35b010207070011c78d85dbbcb10b928 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*

 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 "sdl_rpc_plugin/commands/mobile/add_command_request.h"

#include <string>

#include "application_manager/application.h"
#include "application_manager/message_helper.h"
#include "application_manager/resumption/resume_ctrl.h"
#include "utils/custom_string.h"
#include "utils/file_system.h"
#include "utils/helpers.h"

namespace sdl_rpc_plugin {
using namespace application_manager;

namespace commands {

SDL_CREATE_LOG_VARIABLE("Commands")

namespace custom_str = utils::custom_string;

AddCommandRequest::AddCommandRequest(
    const application_manager::commands::MessageSharedPtr& message,
    ApplicationManager& application_manager,
    rpc_service::RPCService& rpc_service,
    HMICapabilities& hmi_capabilities,
    policy::PolicyHandlerInterface& policy_handler)
    : RequestFromMobileImpl(message,
                            application_manager,
                            rpc_service,
                            hmi_capabilities,
                            policy_handler)
    , ui_result_(hmi_apis::Common_Result::INVALID_ENUM)
    , vr_result_(hmi_apis::Common_Result::INVALID_ENUM)
    , ui_is_sent_(false)
    , vr_is_sent_(false) {}

AddCommandRequest::~AddCommandRequest() {}

void AddCommandRequest::OnTimeOut() {
  SDL_LOG_AUTO_TRACE();
  RemoveCommand();
  RequestFromMobileImpl::OnTimeOut();
}

bool AddCommandRequest::Init() {
  hash_update_mode_ = HashUpdateMode::kDoHashUpdate;
  return true;
}

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

  ApplicationSharedPtr app = application_manager_.application(
      (*message_)[strings::params][strings::connection_key].asUInt());

  if (!app) {
    SDL_LOG_ERROR("No application associated with session key");
    SendResponse(false, mobile_apis::Result::APPLICATION_NOT_REGISTERED);
    return;
  }

  if ((*message_)[strings::msg_params].keyExists(strings::cmd_icon)) {
    mobile_apis::Result::eType verification_result = MessageHelper::VerifyImage(
        (*message_)[strings::msg_params][strings::cmd_icon],
        app,
        application_manager_);

    if (mobile_apis::Result::INVALID_DATA == verification_result) {
      SDL_LOG_ERROR("MessageHelper::VerifyImage return "
                    << verification_result);
      SendResponse(false, verification_result);
      return;
    }
  }

  if ((*message_)[strings::msg_params].keyExists(strings::secondary_image)) {
    mobile_apis::Result::eType verification_result = MessageHelper::VerifyImage(
        (*message_)[strings::msg_params][strings::secondary_image],
        app,
        application_manager_);

    if (mobile_apis::Result::INVALID_DATA == verification_result) {
      SDL_LOG_ERROR("MessageHelper::VerifyImage return "
                    << verification_result);
      SendResponse(false, verification_result);
      return;
    }
  }

  if (!((*message_)[strings::msg_params].keyExists(strings::cmd_id))) {
    SDL_LOG_ERROR("INVALID_DATA");
    SendResponse(false, mobile_apis::Result::INVALID_DATA);
    return;
  }

  const auto command_id = static_cast<uint32_t>(
      (*message_)[strings::msg_params][strings::cmd_id].asUInt());

  const auto command = app->FindCommand(command_id);

  if (smart_objects::SmartType_Null != command.getType()) {
    SDL_LOG_ERROR("INVALID_ID");
    SendResponse(false, mobile_apis::Result::INVALID_ID);
    return;
  }

  bool data_exist = false;

  if ((*message_)[strings::msg_params].keyExists(strings::menu_params)) {
    if (((*message_)[strings::msg_params][strings::menu_params].keyExists(
            hmi_request::parent_id)) &&
        (0 != (*message_)[strings::msg_params][strings::menu_params]
                         [hmi_request::parent_id]
                             .asUInt())) {
      if (!CheckCommandParentId(app)) {
        SendResponse(
            false, mobile_apis::Result::INVALID_ID, "Parent ID doesn't exist");
        return;
      }
    }
    data_exist = true;
  }

  if (((*message_)[strings::msg_params].keyExists(strings::vr_commands)) &&
      ((*message_)[strings::msg_params][strings::vr_commands].length() > 0)) {
    if (!CheckCommandVRSynonym(app)) {
      SendResponse(false, mobile_apis::Result::DUPLICATE_NAME);
      return;
    }

    data_exist = true;
  }

  if (!data_exist) {
    SDL_LOG_ERROR("INVALID_DATA");
    SendResponse(false, mobile_apis::Result::INVALID_DATA);
    return;
  }

  if (IsWhiteSpaceExist()) {
    SDL_LOG_ERROR("Incoming add command has contains \t\n \\t \\n");
    SendResponse(false, mobile_apis::Result::INVALID_DATA);
    return;
  }

  const uint32_t internal_consecutive_number = application_manager::commands::
      CommandImpl::CalcCommandInternalConsecutiveNumber(app);
  app->AddCommand(internal_consecutive_number,
                  (*message_)[strings::msg_params]);

  smart_objects::SmartObject ui_msg_params =
      smart_objects::SmartObject(smart_objects::SmartType_Map);

  if ((*message_)[strings::msg_params].keyExists(strings::menu_params)) {
    ui_msg_params[strings::cmd_id] =
        (*message_)[strings::msg_params][strings::cmd_id];
    ui_msg_params[strings::menu_params] =
        (*message_)[strings::msg_params][strings::menu_params];

    ui_msg_params[strings::app_id] = app->app_id();

    if (((*message_)[strings::msg_params].keyExists(strings::cmd_icon)) &&
        ((*message_)[strings::msg_params][strings::cmd_icon].keyExists(
            strings::value)) &&
        (0 < (*message_)[strings::msg_params][strings::cmd_icon][strings::value]
                 .length())) {
      ui_msg_params[strings::cmd_icon] =
          (*message_)[strings::msg_params][strings::cmd_icon];
    }

    ui_is_sent_ = true;
    StartAwaitForInterface(HmiInterfaces::HMI_INTERFACE_UI);

    if (((*message_)[strings::msg_params].keyExists(
            strings::secondary_image)) &&
        ((*message_)[strings::msg_params][strings::secondary_image].keyExists(
            strings::value)) &&
        (0 < (*message_)[strings::msg_params][strings::secondary_image]
                        [strings::value]
                            .length())) {
      ui_msg_params[strings::secondary_image] =
          (*message_)[strings::msg_params][strings::secondary_image];
    }
  }

  smart_objects::SmartObject vr_msg_params =
      smart_objects::SmartObject(smart_objects::SmartType_Map);
  if ((*message_)[strings::msg_params].keyExists(strings::vr_commands)) {
    vr_msg_params[strings::cmd_id] =
        (*message_)[strings::msg_params][strings::cmd_id];
    vr_msg_params[strings::vr_commands] =
        (*message_)[strings::msg_params][strings::vr_commands];
    vr_msg_params[strings::app_id] = app->app_id();

    vr_msg_params[strings::type] = hmi_apis::Common_VRCommandType::Command;
    vr_msg_params[strings::grammar_id] = app->get_grammar_id();

    vr_is_sent_ = true;
    StartAwaitForInterface(HmiInterfaces::HMI_INTERFACE_VR);
  }

  if (IsInterfaceAwaited(HmiInterfaces::HMI_INTERFACE_UI)) {
    SendHMIRequest(hmi_apis::FunctionID::UI_AddCommand, &ui_msg_params, true);
  }

  if (IsInterfaceAwaited(HmiInterfaces::HMI_INTERFACE_VR)) {
    SendHMIRequest(hmi_apis::FunctionID::VR_AddCommand, &vr_msg_params, true);
  }
}

bool AddCommandRequest::CheckCommandVRSynonym(ApplicationConstSharedPtr app) {
  if (!app) {
    return false;
  }

  const DataAccessor<CommandsMap> accessor = app->commands_map();
  const CommandsMap& commands = accessor.GetData();
  CommandsMap::const_iterator it = commands.begin();

  for (; commands.end() != it; ++it) {
    if (!(*it->second).keyExists(strings::vr_commands)) {
      continue;
    }

    for (size_t i = 0; i < (*it->second)[strings::vr_commands].length(); ++i) {
      for (size_t j = 0;
           j < (*message_)[strings::msg_params][strings::vr_commands].length();
           ++j) {
        const custom_str::CustomString& vr_cmd_i =
            (*it->second)[strings::vr_commands][i].asCustomString();
        const custom_str::CustomString& vr_cmd_j =
            (*message_)[strings::msg_params][strings::vr_commands][j]
                .asCustomString();

        if (vr_cmd_i.CompareIgnoreCase(vr_cmd_j)) {
          SDL_LOG_INFO(
              "AddCommandRequest::CheckCommandVRSynonym"
              " received command vr synonym already exist");
          return false;
        }
      }
    }
  }
  return true;
}

bool AddCommandRequest::CheckCommandParentId(ApplicationConstSharedPtr app) {
  if (!app) {
    return false;
  }

  const int32_t parent_id =
      (*message_)[strings::msg_params][strings::menu_params]
                 [hmi_request::parent_id]
                     .asInt();
  smart_objects::SmartObject parent = app->FindSubMenu(parent_id);

  if (smart_objects::SmartType_Null == parent.getType()) {
    SDL_LOG_INFO(
        "AddCommandRequest::CheckCommandParentId received"
        " submenu doesn't exist");
    return false;
  }
  return true;
}

// TODO(AKUTSAN) APPLINK-26973: Refactor AddCommandRequest
void AddCommandRequest::on_event(const event_engine::Event& event) {
  SDL_LOG_AUTO_TRACE();
  using namespace helpers;

  const smart_objects::SmartObject& message = event.smart_object();

  ApplicationSharedPtr application =
      application_manager_.application(connection_key());

  if (!application) {
    SDL_LOG_ERROR("NULL pointer");
    return;
  }

  const uint32_t cmd_id =
      (*message_)[strings::msg_params][strings::cmd_id].asUInt();
  smart_objects::SmartObject msg_param(smart_objects::SmartType_Map);
  msg_param[strings::cmd_id] = cmd_id;
  msg_param[strings::app_id] = application->app_id();

  switch (event.id()) {
    case hmi_apis::FunctionID::UI_AddCommand: {
      SDL_LOG_INFO("Received UI_AddCommand event");
      EndAwaitForInterface(HmiInterfaces::HMI_INTERFACE_UI);
      ui_result_ = static_cast<hmi_apis::Common_Result::eType>(
          message[strings::params][hmi_response::code].asInt());
      GetInfo(message, ui_info_);
      if (hmi_apis::Common_Result::SUCCESS != ui_result_) {
        (*message_)[strings::msg_params].erase(strings::menu_params);
      }
      break;
    }
    case hmi_apis::FunctionID::VR_AddCommand: {
      SDL_LOG_INFO("Received VR_AddCommand event");
      EndAwaitForInterface(HmiInterfaces::HMI_INTERFACE_VR);
      vr_result_ = static_cast<hmi_apis::Common_Result::eType>(
          message[strings::params][hmi_response::code].asInt());
      GetInfo(message, vr_info_);
      if (hmi_apis::Common_Result::SUCCESS != vr_result_) {
        (*message_)[strings::msg_params].erase(strings::vr_commands);
      }
      break;
    }
    default: {
      SDL_LOG_ERROR("Received unknown event" << event.id());
      return;
    }
  }

  if (IsPendingResponseExist()) {
    SDL_LOG_DEBUG("Command still wating for HMI response");
    return;
  }

  smart_objects::SmartObject msg_params(smart_objects::SmartType_Map);
  msg_params[strings::cmd_id] =
      (*message_)[strings::msg_params][strings::cmd_id];
  msg_params[strings::app_id] = application->app_id();

  mobile_apis::Result::eType result_code = mobile_apis::Result::INVALID_ENUM;

  const bool is_vr_invalid_unsupported =
      Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
          vr_result_,
          hmi_apis::Common_Result::INVALID_ENUM,
          hmi_apis::Common_Result::UNSUPPORTED_RESOURCE);

  const bool is_ui_invalid_unsupported =
      Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
          ui_result_,
          hmi_apis::Common_Result::INVALID_ENUM,
          hmi_apis::Common_Result::UNSUPPORTED_RESOURCE);
  const bool is_vr_unsupported =
      vr_result_ == hmi_apis::Common_Result::UNSUPPORTED_RESOURCE;
  const bool is_ui_unsupported =
      ui_result_ == hmi_apis::Common_Result::UNSUPPORTED_RESOURCE;

  const bool is_no_ui_error = Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
      ui_result_,
      hmi_apis::Common_Result::SUCCESS,
      hmi_apis::Common_Result::WARNINGS,
      hmi_apis::Common_Result::WRONG_LANGUAGE,
      hmi_apis::Common_Result::RETRY,
      hmi_apis::Common_Result::SAVED,
      hmi_apis::Common_Result::UNSUPPORTED_RESOURCE);

  const bool is_no_vr_error = Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
      vr_result_,
      hmi_apis::Common_Result::SUCCESS,
      hmi_apis::Common_Result::WARNINGS,
      hmi_apis::Common_Result::WRONG_LANGUAGE,
      hmi_apis::Common_Result::RETRY,
      hmi_apis::Common_Result::SAVED,
      hmi_apis::Common_Result::UNSUPPORTED_RESOURCE);

  bool result = (is_no_ui_error && is_no_vr_error) ||
                (is_no_ui_error && is_vr_invalid_unsupported) ||
                (is_no_vr_error && is_ui_invalid_unsupported);

  SDL_LOG_DEBUG("calculated result " << ui_result_ << " " << is_no_ui_error
                                     << " " << is_no_vr_error);
  const bool is_vr_or_ui_warning =
      Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
          hmi_apis::Common_Result::WARNINGS, ui_result_, vr_result_);

  const bool is_vr_or_ui_unsupported =
      Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
          hmi_apis::Common_Result::UNSUPPORTED_RESOURCE,
          ui_result_,
          vr_result_);

  const bool is_vr_and_ui_unsupported =
      Compare<hmi_apis::Common_Result::eType, EQ, ALL>(
          hmi_apis::Common_Result::UNSUPPORTED_RESOURCE,
          ui_result_,
          vr_result_);

  if (!result && hmi_apis::Common_Result::REJECTED == ui_result_) {
    result_code = MessageHelper::HMIToMobileResult(ui_result_);
  } else if (result && is_vr_or_ui_unsupported) {
    result_code = mobile_apis::Result::UNSUPPORTED_RESOURCE;
  } else if (is_vr_or_ui_warning) {
    result_code = mobile_apis::Result::WARNINGS;
  } else {
    result_code =
        MessageHelper::HMIToMobileResult(std::max(ui_result_, vr_result_));
    if (hmi_apis::Common_Result::UNSUPPORTED_RESOURCE == ui_result_) {
      result_code = MessageHelper::HMIToMobileResult(vr_result_);
    }
    if (hmi_apis::Common_Result::UNSUPPORTED_RESOURCE == vr_result_) {
      result_code = MessageHelper::HMIToMobileResult(ui_result_);
    }
    SDL_LOG_DEBUG("HMIToMobileResult " << result_code);
  }

  if (BothSend() && hmi_apis::Common_Result::SUCCESS == vr_result_) {
    const bool is_ui_not_ok = Compare<hmi_apis::Common_Result::eType, NEQ, ALL>(
        ui_result_,
        hmi_apis::Common_Result::SUCCESS,
        hmi_apis::Common_Result::WARNINGS,
        hmi_apis::Common_Result::UNSUPPORTED_RESOURCE);

    if (is_ui_not_ok) {
      result_code = ui_result_ == hmi_apis::Common_Result::REJECTED
                        ? mobile_apis::Result::REJECTED
                        : mobile_apis::Result::GENERIC_ERROR;

      msg_params[strings::grammar_id] = application->get_grammar_id();
      msg_params[strings::type] = hmi_apis::Common_VRCommandType::Command;

      SendHMIRequest(hmi_apis::FunctionID::VR_DeleteCommand, &msg_params);
      application->RemoveCommand(cmd_id);
      result = false;
      SDL_LOG_DEBUG("Result " << result);
    }
  }

  if (BothSend() && hmi_apis::Common_Result::SUCCESS == ui_result_ &&
      !is_no_vr_error &&
      hmi_apis::Common_Result::UNSUPPORTED_RESOURCE != vr_result_) {
    result_code = vr_result_ == hmi_apis::Common_Result::REJECTED
                      ? mobile_apis::Result::REJECTED
                      : mobile_apis::Result::GENERIC_ERROR;

    SendHMIRequest(hmi_apis::FunctionID::UI_DeleteCommand, &msg_params);

    application->RemoveCommand(cmd_id);
    result = false;
    SDL_LOG_DEBUG("Result " << result);
  }

  HmiInterfaces::InterfaceState ui_interface_state =
      application_manager_.hmi_interfaces().GetInterfaceState(
          HmiInterfaces::HMI_INTERFACE_UI);
  HmiInterfaces::InterfaceState vr_interface_state =
      application_manager_.hmi_interfaces().GetInterfaceState(
          HmiInterfaces::HMI_INTERFACE_VR);

  if (!BothSend() &&
      ((is_vr_unsupported &&
        HmiInterfaces::STATE_NOT_AVAILABLE == vr_interface_state) ||
       (is_ui_unsupported &&
        HmiInterfaces::STATE_NOT_AVAILABLE == ui_interface_state))) {
    SDL_LOG_DEBUG("!BothSend() && is_vr_or_ui_unsupported");
    result = false;
  }

  if (is_vr_and_ui_unsupported) {
    SDL_LOG_DEBUG("UI and VR interface both unsupported");
    result = false;
  }

  if (result) {
    application->help_prompt_manager().OnVrCommandAdded(
        cmd_id, (*message_)[strings::msg_params], false);
  } else {
    RemoveCommand();
  }

  const std::string info = GenerateMobileResponseInfo();
  SendResponse(result,
               result_code,
               info.empty() ? NULL : info.c_str(),
               &(message[strings::msg_params]));
}

bool AddCommandRequest::IsWhiteSpaceExist() {
  SDL_LOG_AUTO_TRACE();
  const char* str = NULL;

  if ((*message_)[strings::msg_params].keyExists(strings::menu_params)) {
    str = (*message_)[strings::msg_params][strings::menu_params]
                     [strings::menu_name]
                         .asCharArray();
    if (!CheckSyntax(str)) {
      SDL_LOG_ERROR("Invalid menu name syntax check failed.");
      return true;
    }
  }

  if ((*message_)[strings::msg_params].keyExists(strings::vr_commands)) {
    const size_t len =
        (*message_)[strings::msg_params][strings::vr_commands].length();

    for (size_t i = 0; i < len; ++i) {
      str = (*message_)[strings::msg_params][strings::vr_commands][i]
                .asCharArray();
      if (!CheckSyntax(str)) {
        SDL_LOG_ERROR("Invalid vr_commands syntax check failed");
        return true;
      }
    }
  }

  if ((*message_)[strings::msg_params].keyExists(strings::cmd_icon)) {
    str = (*message_)[strings::msg_params][strings::cmd_icon][strings::value]
              .asCharArray();
    if (!CheckSyntax(str)) {
      SDL_LOG_ERROR("Invalid cmd_icon value syntax check failed");
      return true;
    }
  }

  if ((*message_)[strings::msg_params].keyExists(strings::secondary_image)) {
    str = (*message_)[strings::msg_params][strings::secondary_image]
                     [strings::value]
                         .asCharArray();
    if (!CheckSyntax(str)) {
      SDL_LOG_ERROR("Invalid secondaryImage value syntax check failed");
      return true;
    }
  }
  return false;
}

bool AddCommandRequest::BothSend() const {
  return ui_is_sent_ && vr_is_sent_;
}

const std::string AddCommandRequest::GenerateMobileResponseInfo() {
  // In case if vr_result_ is UNSUPPORTED_RESOURCE vr_info should be on the
  // first place
  // In case if ui_result_ is UNSUPPORTED_RESOURCE ui_info should be on the
  // first place
  // Other way order is doesn't matter

  HmiInterfaces& hmi_interfaces = application_manager_.hmi_interfaces();
  HmiInterfaces::InterfaceState ui_interface_state =
      hmi_interfaces.GetInterfaceState(HmiInterfaces::HMI_INTERFACE_UI);

  HmiInterfaces::InterfaceState vr_interface_state =
      hmi_interfaces.GetInterfaceState(HmiInterfaces::HMI_INTERFACE_VR);

  if ((ui_interface_state == HmiInterfaces::STATE_NOT_AVAILABLE) &&
      (vr_interface_state != HmiInterfaces::STATE_NOT_AVAILABLE) &&
      !vr_info_.empty()) {
    return vr_info_;
  }

  if ((vr_interface_state == HmiInterfaces::STATE_NOT_AVAILABLE) &&
      (ui_interface_state != HmiInterfaces::STATE_NOT_AVAILABLE) &&
      !ui_info_.empty()) {
    return ui_info_;
  }

  return app_mngr::commands::MergeInfos(ui_info_, vr_info_);
}

void AddCommandRequest::RemoveCommand() {
  SDL_LOG_AUTO_TRACE();
  ApplicationSharedPtr app = application_manager_.application(connection_key());
  if (app.use_count() == 0) {
    SDL_LOG_ERROR("No application associated with session key");
    return;
  }

  const uint32_t cmd_id =
      (*message_)[strings::msg_params][strings::cmd_id].asUInt();
  smart_objects::SmartObject msg_params(smart_objects::SmartType_Map);
  msg_params[strings::cmd_id] = cmd_id;
  msg_params[strings::app_id] = app->app_id();

  app->RemoveCommand(cmd_id);

  if (BothSend() && (IsInterfaceAwaited(HmiInterfaces::HMI_INTERFACE_VR) &&
                     IsInterfaceAwaited(HmiInterfaces::HMI_INTERFACE_UI))) {
    // in case we have send bth UI and VR and no one respond
    // we have nothing to remove from HMI so no DeleteCommand expected
    return;
  }

  if (BothSend() && IsInterfaceAwaited(HmiInterfaces::HMI_INTERFACE_VR)) {
    SendHMIRequest(hmi_apis::FunctionID::UI_DeleteCommand, &msg_params);
  }

  if (BothSend() && IsInterfaceAwaited(HmiInterfaces::HMI_INTERFACE_UI)) {
    msg_params[strings::grammar_id] = app->get_grammar_id();
    msg_params[strings::type] = hmi_apis::Common_VRCommandType::Command;
    SendHMIRequest(hmi_apis::FunctionID::VR_DeleteCommand, &msg_params);
  }
}

}  // namespace commands

}  // namespace sdl_rpc_plugin