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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
|
/*
Copyright (c) 2016, 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 <algorithm>
#include <numeric>
#include <string>
#include "utils/macro.h"
#include "application_manager/commands/command_request_impl.h"
#include "application_manager/app_service_manager.h"
#include "application_manager/application_manager.h"
#include "application_manager/message_helper.h"
#include "application_manager/rpc_service.h"
#include "smart_objects/smart_object.h"
#include "smart_objects/enum_schema_item.h"
namespace application_manager {
namespace commands {
std::string MergeInfos(const ResponseInfo& first_info,
const std::string& first_str,
const ResponseInfo& second_info,
const std::string& second_str) {
if ((first_info.interface_state == HmiInterfaces::STATE_NOT_AVAILABLE) &&
(second_info.interface_state != HmiInterfaces::STATE_NOT_AVAILABLE) &&
!second_str.empty()) {
return second_str;
}
if ((second_info.interface_state == HmiInterfaces::STATE_NOT_AVAILABLE) &&
(first_info.interface_state != HmiInterfaces::STATE_NOT_AVAILABLE) &&
!first_str.empty()) {
return first_str;
}
return MergeInfos(first_str, second_str);
}
std::string MergeInfos(const std::string& first, const std::string& second) {
return first + ((!first.empty() && !second.empty()) ? ", " : "") + second;
}
std::string MergeInfos(const std::string& first,
const std::string& second,
const std::string& third) {
std::string result = MergeInfos(first, second);
return MergeInfos(result, third);
}
const std::string CreateInfoForUnsupportedResult(
HmiInterfaces::InterfaceID interface) {
switch (interface) {
case (HmiInterfaces::InterfaceID::HMI_INTERFACE_VR): {
return "VR is not supported by system";
}
case (HmiInterfaces::InterfaceID::HMI_INTERFACE_TTS): {
return "TTS is not supported by system";
}
case (HmiInterfaces::InterfaceID::HMI_INTERFACE_UI): {
return "UI is not supported by system";
}
case (HmiInterfaces::InterfaceID::HMI_INTERFACE_Navigation): {
return "Navigation is not supported by system";
}
case (HmiInterfaces::InterfaceID::HMI_INTERFACE_VehicleInfo): {
return "VehicleInfo is not supported by system";
}
case (HmiInterfaces::InterfaceID::HMI_INTERFACE_RC): {
return "RC is not supported by system";
}
default:
#ifdef ENABLE_LOG
CREATE_LOGGERPTR_LOCAL(logger, "Commands");
LOG4CXX_WARN(logger,
"Could not create info because"
" interface isn't valid. Interface is:"
<< static_cast<int32_t>(interface));
#endif // ENABLE_LOG
return "";
}
}
bool CommandRequestImpl::CheckResult(const ResponseInfo& first,
const ResponseInfo& second) const {
if (first.is_ok && second.is_unsupported_resource) {
return true;
}
if (first.is_ok && second.is_not_used) {
return true;
}
if (first.is_ok && second.is_ok) {
return true;
}
return false;
}
bool IsResultCodeWarning(const ResponseInfo& first,
const ResponseInfo& second) {
const bool first_is_ok_second_is_warn =
(first.is_ok || first.is_not_used) &&
hmi_apis::Common_Result::WARNINGS == second.result_code;
const bool both_warnings =
hmi_apis::Common_Result::WARNINGS == first.result_code &&
hmi_apis::Common_Result::WARNINGS == second.result_code;
return first_is_ok_second_is_warn || both_warnings;
}
struct DisallowedParamsInserter {
DisallowedParamsInserter(smart_objects::SmartObject& response,
mobile_apis::VehicleDataResultCode::eType code)
: response_(response), code_(code) {}
bool operator()(const std::string& param) {
smart_objects::SmartObjectSPtr disallowed_param =
std::make_shared<smart_objects::SmartObject>(
smart_objects::SmartType_Map);
auto rpc_spec_vehicle_data = MessageHelper::vehicle_data();
auto vehicle_data = rpc_spec_vehicle_data.find(param);
auto vehicle_data_type =
vehicle_data == rpc_spec_vehicle_data.end()
? mobile_apis::VehicleDataType::VEHICLEDATA_OEM_CUSTOM_DATA
: vehicle_data->second;
(*disallowed_param)[strings::data_type] = vehicle_data_type;
(*disallowed_param)[strings::result_code] = code_;
response_[strings::msg_params][param.c_str()] = *disallowed_param;
return true;
}
private:
smart_objects::SmartObject& response_;
mobile_apis::VehicleDataResultCode::eType code_;
};
ResponseInfo::ResponseInfo()
: result_code(hmi_apis::Common_Result::INVALID_ENUM)
, interface(HmiInterfaces::HMI_INTERFACE_INVALID_ENUM)
, interface_state(HmiInterfaces::STATE_NOT_RESPONSE)
, is_ok(false)
, is_unsupported_resource(false)
, is_not_used(false) {}
ResponseInfo::ResponseInfo(const hmi_apis::Common_Result::eType result,
const HmiInterfaces::InterfaceID hmi_interface,
ApplicationManager& application_manager)
: result_code(result)
, interface(hmi_interface)
, interface_state(HmiInterfaces::STATE_NOT_RESPONSE)
, is_ok(false)
, is_unsupported_resource(false)
, is_not_used(false) {
using namespace helpers;
interface_state =
application_manager.hmi_interfaces().GetInterfaceState(hmi_interface);
is_ok = CommandRequestImpl::IsHMIResultSuccess(result_code);
is_not_used = hmi_apis::Common_Result::INVALID_ENUM == result_code;
is_unsupported_resource =
hmi_apis::Common_Result::UNSUPPORTED_RESOURCE == result_code;
}
CommandRequestImpl::CommandRequestImpl(
const MessageSharedPtr& message,
ApplicationManager& application_manager,
rpc_service::RPCService& rpc_service,
HMICapabilities& hmi_capabilities,
policy::PolicyHandlerInterface& policy_handler)
: CommandImpl(message,
application_manager,
rpc_service,
hmi_capabilities,
policy_handler)
, EventObserver(application_manager.event_dispatcher())
, current_state_(kAwaitingHMIResponse)
, hash_update_mode_(kSkipHashUpdate)
, is_success_result_(false) {}
CommandRequestImpl::~CommandRequestImpl() {
UpdateHash();
}
bool CommandRequestImpl::Init() {
return true;
}
bool CommandRequestImpl::CheckPermissions() {
return CheckAllowedParameters(Command::CommandSource::SOURCE_MOBILE);
}
bool CommandRequestImpl::CleanUp() {
return true;
}
void CommandRequestImpl::Run() {}
void CommandRequestImpl::onTimeOut() {
LOG4CXX_AUTO_TRACE(logger_);
unsubscribe_from_all_hmi_events();
unsubscribe_from_all_mobile_events();
{
// FIXME (dchmerev@luxoft.com): atomic_xchg fits better
sync_primitives::AutoLock auto_lock(state_lock_);
if (kCompleted == current_state_) {
LOG4CXX_DEBUG(logger_, "current_state_ = kCompleted");
// don't send timeout if request completed
return;
}
current_state_ = kTimedOut;
}
smart_objects::SmartObjectSPtr response =
MessageHelper::CreateNegativeResponse(connection_key(),
function_id(),
correlation_id(),
mobile_api::Result::GENERIC_ERROR);
AddTimeOutComponentInfoToMessage(*response);
rpc_service_.ManageMobileCommand(response, SOURCE_SDL);
}
void CommandRequestImpl::on_event(const event_engine::Event& event) {}
void CommandRequestImpl::on_event(const event_engine::MobileEvent& event) {}
void CommandRequestImpl::SendResponse(
const bool success,
const mobile_apis::Result::eType& result_code,
const char* info,
const smart_objects::SmartObject* response_params,
const std::vector<uint8_t> binary_data) {
LOG4CXX_AUTO_TRACE(logger_);
{
sync_primitives::AutoLock auto_lock(state_lock_);
if (kTimedOut == current_state_) {
// don't send response if request timeout expired
return;
}
current_state_ = kCompleted;
}
smart_objects::SmartObjectSPtr result =
std::make_shared<smart_objects::SmartObject>();
smart_objects::SmartObject& response = *result;
response[strings::params][strings::message_type] = MessageType::kResponse;
response[strings::params][strings::correlation_id] = correlation_id();
response[strings::params][strings::protocol_type] =
CommandImpl::mobile_protocol_type_;
response[strings::params][strings::protocol_version] =
CommandImpl::protocol_version_;
response[strings::params][strings::connection_key] = connection_key();
response[strings::params][strings::function_id] = function_id();
if (!binary_data.empty()) {
response[strings::params][strings::binary_data] = binary_data;
}
if (response_params) {
response[strings::msg_params] = *response_params;
}
if (info && *info != '\0') {
response[strings::msg_params][strings::info] = std::string(info);
}
// Add disallowed parameters and info from request back to response with
// appropriate reasons (VehicleData result codes)
if (result_code != mobile_apis::Result::APPLICATION_NOT_REGISTERED &&
result_code != mobile_apis::Result::INVALID_DATA) {
const mobile_apis::FunctionID::eType& id =
static_cast<mobile_apis::FunctionID::eType>(function_id());
if ((id == mobile_apis::FunctionID::SubscribeVehicleDataID) ||
(id == mobile_apis::FunctionID::UnsubscribeVehicleDataID)) {
AddDisallowedParameters(response);
AddDisallowedParametersToInfo(response);
} else if (id == mobile_apis::FunctionID::GetVehicleDataID) {
AddDisallowedParametersToInfo(response);
}
}
response[strings::msg_params][strings::success] = success;
if ((result_code == mobile_apis::Result::SUCCESS ||
result_code == mobile_apis::Result::WARNINGS) &&
!warning_info().empty()) {
response[strings::msg_params][strings::info] =
(info && *info != '\0') ? std::string(info) + "\n" + warning_info()
: warning_info();
response[strings::msg_params][strings::result_code] =
mobile_apis::Result::WARNINGS;
} else {
response[strings::msg_params][strings::result_code] = result_code;
}
is_success_result_ = success;
rpc_service_.ManageMobileCommand(result, SOURCE_SDL);
}
smart_objects::SmartObject CreateUnsupportedResourceResponse(
const hmi_apis::FunctionID::eType function_id,
const uint32_t hmi_correlation_id,
HmiInterfaces::InterfaceID interface) {
smart_objects::SmartObject response(smart_objects::SmartType_Map);
smart_objects::SmartObject& params = response[strings::params];
params[strings::message_type] = MessageType::kResponse;
params[strings::correlation_id] = hmi_correlation_id;
params[strings::protocol_type] = CommandImpl::hmi_protocol_type_;
params[strings::protocol_version] = CommandImpl::protocol_version_;
params[strings::function_id] = function_id;
params[hmi_response::code] = hmi_apis::Common_Result::UNSUPPORTED_RESOURCE;
smart_objects::SmartObject& msg_params = response[strings::msg_params];
msg_params[strings::info] = CreateInfoForUnsupportedResult(interface);
return response;
}
bool CommandRequestImpl::ProcessHMIInterfacesAvailability(
const uint32_t hmi_correlation_id,
const hmi_apis::FunctionID::eType& function_id) {
LOG4CXX_AUTO_TRACE(logger_);
HmiInterfaces& hmi_interfaces = application_manager_.hmi_interfaces();
HmiInterfaces::InterfaceID interface =
hmi_interfaces.GetInterfaceFromFunction(function_id);
DCHECK(interface != HmiInterfaces::HMI_INTERFACE_INVALID_ENUM);
const HmiInterfaces::InterfaceState state =
hmi_interfaces.GetInterfaceState(interface);
if (HmiInterfaces::STATE_NOT_AVAILABLE == state) {
event_engine::Event event(function_id);
event.set_smart_object(CreateUnsupportedResourceResponse(
function_id, hmi_correlation_id, interface));
event.raise(application_manager_.event_dispatcher());
return false;
}
return true;
}
void CommandRequestImpl::UpdateHash() {
LOG4CXX_AUTO_TRACE(logger_);
if (hash_update_mode_ == kSkipHashUpdate) {
LOG4CXX_DEBUG(logger_, "Hash update is disabled for " << function_id());
return;
}
if (HmiInterfaces::InterfaceState::STATE_NOT_RESPONSE ==
application_manager_.hmi_interfaces().GetInterfaceState(
HmiInterfaces::InterfaceID::HMI_INTERFACE_UI)) {
LOG4CXX_ERROR(logger_,
"UI interface has not responded. Hash won't be updated.");
return;
}
if (!is_success_result_) {
LOG4CXX_WARN(logger_, "Command is not succeeded. Hash won't be updated.");
return;
}
ApplicationSharedPtr application =
application_manager_.application(connection_key());
if (!application) {
LOG4CXX_ERROR(logger_,
"Application with connection key "
<< connection_key()
<< " not found. Not able to update hash.");
return;
}
LOG4CXX_DEBUG(
logger_,
"Updating hash for application with connection key "
<< connection_key() << " while processing function id "
<< MessageHelper::StringifiedFunctionID(
static_cast<mobile_api::FunctionID::eType>(function_id())));
application->UpdateHash();
}
void CommandRequestImpl::SendProviderRequest(
const mobile_apis::FunctionID::eType& mobile_function_id,
const hmi_apis::FunctionID::eType& hmi_function_id,
const smart_objects::SmartObject* msg,
bool use_events) {
LOG4CXX_AUTO_TRACE(logger_);
bool hmi_destination = false;
ApplicationSharedPtr app;
// Default error code and error message
std::string error_msg = "No app service provider available";
mobile_apis::Result::eType error_code =
mobile_apis::Result::DATA_NOT_AVAILABLE;
if ((*msg)[strings::msg_params].keyExists(strings::service_type)) {
std::string service_type =
(*msg)[strings::msg_params][strings::service_type].asString();
application_manager_.GetAppServiceManager().GetProviderByType(
service_type, true, app, hmi_destination);
error_msg = "No app service provider with serviceType: " + service_type +
" is available";
error_code = mobile_apis::Result::DATA_NOT_AVAILABLE;
} else if ((*msg)[strings::msg_params].keyExists(strings::service_id)) {
std::string service_id =
(*msg)[strings::msg_params][strings::service_id].asString();
application_manager_.GetAppServiceManager().GetProviderByID(
service_id, true, app, hmi_destination);
error_msg = "No app service provider with serviceId: " + service_id +
" is available";
error_code = mobile_apis::Result::INVALID_ID;
}
if (hmi_destination) {
LOG4CXX_DEBUG(logger_, "Sending Request to HMI Provider");
application_manager_.IncreaseForwardedRequestTimeout(connection_key(),
correlation_id());
SendHMIRequest(hmi_function_id, &(*msg)[strings::msg_params], use_events);
return;
}
if (!app) {
LOG4CXX_DEBUG(logger_, "Invalid App Provider pointer");
SendResponse(false, error_code, error_msg.c_str());
return;
}
if (connection_key() == app->app_id()) {
SendResponse(false,
mobile_apis::Result::IGNORED,
"Consumer app is same as producer app");
return;
}
smart_objects::SmartObjectSPtr new_msg =
std::make_shared<smart_objects::SmartObject>();
smart_objects::SmartObject& request = *new_msg;
request[strings::params] = (*msg)[strings::params];
request[strings::msg_params] = (*msg)[strings::msg_params];
request[strings::params][strings::connection_key] = app->app_id();
application_manager_.IncreaseForwardedRequestTimeout(connection_key(),
correlation_id());
SendMobileRequest(mobile_function_id, new_msg, use_events);
}
void CommandRequestImpl::SendMobileRequest(
const mobile_apis::FunctionID::eType& function_id,
smart_objects::SmartObjectSPtr msg,
bool use_events) {
smart_objects::SmartObject& request = *msg;
const uint32_t mobile_correlation_id =
application_manager_.GetNextMobileCorrelationID();
request[strings::params][strings::correlation_id] = mobile_correlation_id;
request[strings::params][strings::message_type] = MessageType::kRequest;
if (use_events) {
LOG4CXX_DEBUG(logger_,
"SendMobileRequest subscribe_on_event "
<< function_id << " " << mobile_correlation_id);
subscribe_on_event(function_id, mobile_correlation_id);
}
if (!rpc_service_.ManageMobileCommand(msg, SOURCE_SDL)) {
LOG4CXX_ERROR(logger_, "Unable to send request to mobile");
}
}
uint32_t CommandRequestImpl::SendHMIRequest(
const hmi_apis::FunctionID::eType& function_id,
const smart_objects::SmartObject* msg_params,
bool use_events) {
smart_objects::SmartObjectSPtr result =
std::make_shared<smart_objects::SmartObject>();
const uint32_t hmi_correlation_id =
application_manager_.GetNextHMICorrelationID();
smart_objects::SmartObject& request = *result;
request[strings::params][strings::message_type] = MessageType::kRequest;
request[strings::params][strings::function_id] = function_id;
request[strings::params][strings::correlation_id] = hmi_correlation_id;
request[strings::params][strings::protocol_version] =
CommandImpl::protocol_version_;
request[strings::params][strings::protocol_type] =
CommandImpl::hmi_protocol_type_;
if (msg_params) {
request[strings::msg_params] = *msg_params;
}
if (use_events) {
LOG4CXX_DEBUG(logger_,
"SendHMIRequest subscribe_on_event " << function_id << " "
<< hmi_correlation_id);
subscribe_on_event(function_id, hmi_correlation_id);
}
if (ProcessHMIInterfacesAvailability(hmi_correlation_id, function_id)) {
if (!rpc_service_.ManageHMICommand(result, SOURCE_SDL_TO_HMI)) {
LOG4CXX_ERROR(logger_, "Unable to send request");
SendResponse(false, mobile_apis::Result::OUT_OF_MEMORY);
}
} else {
LOG4CXX_DEBUG(logger_, "Interface is not available");
}
return hmi_correlation_id;
}
void CommandRequestImpl::CreateHMINotification(
const hmi_apis::FunctionID::eType& function_id,
const ns_smart::SmartObject& msg_params) const {
smart_objects::SmartObjectSPtr result =
std::make_shared<smart_objects::SmartObject>();
if (!result) {
LOG4CXX_ERROR(logger_, "Memory allocation failed.");
return;
}
smart_objects::SmartObject& notify = *result;
notify[strings::params][strings::message_type] =
static_cast<int32_t>(application_manager::MessageType::kNotification);
notify[strings::params][strings::protocol_version] =
CommandImpl::protocol_version_;
notify[strings::params][strings::protocol_type] =
CommandImpl::hmi_protocol_type_;
notify[strings::params][strings::function_id] = function_id;
notify[strings::msg_params] = msg_params;
if (!rpc_service_.ManageHMICommand(result, SOURCE_SDL_TO_HMI)) {
LOG4CXX_ERROR(logger_, "Unable to send HMI notification");
}
}
mobile_apis::Result::eType CommandRequestImpl::GetMobileResultCode(
const hmi_apis::Common_Result::eType& hmi_code) const {
mobile_apis::Result::eType mobile_result = mobile_apis::Result::GENERIC_ERROR;
switch (hmi_code) {
case hmi_apis::Common_Result::SUCCESS: {
mobile_result = mobile_apis::Result::SUCCESS;
break;
}
case hmi_apis::Common_Result::UNSUPPORTED_REQUEST: {
mobile_result = mobile_apis::Result::UNSUPPORTED_REQUEST;
break;
}
case hmi_apis::Common_Result::UNSUPPORTED_RESOURCE: {
mobile_result = mobile_apis::Result::UNSUPPORTED_RESOURCE;
break;
}
case hmi_apis::Common_Result::DISALLOWED: {
mobile_result = mobile_apis::Result::DISALLOWED;
break;
}
case hmi_apis::Common_Result::REJECTED: {
mobile_result = mobile_apis::Result::REJECTED;
break;
}
case hmi_apis::Common_Result::ABORTED: {
mobile_result = mobile_apis::Result::ABORTED;
break;
}
case hmi_apis::Common_Result::IGNORED: {
mobile_result = mobile_apis::Result::IGNORED;
break;
}
case hmi_apis::Common_Result::RETRY: {
mobile_result = mobile_apis::Result::RETRY;
break;
}
case hmi_apis::Common_Result::IN_USE: {
mobile_result = mobile_apis::Result::IN_USE;
break;
}
case hmi_apis::Common_Result::DATA_NOT_AVAILABLE: {
mobile_result = mobile_apis::Result::VEHICLE_DATA_NOT_AVAILABLE;
break;
}
case hmi_apis::Common_Result::TIMED_OUT: {
mobile_result = mobile_apis::Result::TIMED_OUT;
break;
}
case hmi_apis::Common_Result::INVALID_DATA: {
mobile_result = mobile_apis::Result::INVALID_DATA;
break;
}
case hmi_apis::Common_Result::CHAR_LIMIT_EXCEEDED: {
mobile_result = mobile_apis::Result::CHAR_LIMIT_EXCEEDED;
break;
}
case hmi_apis::Common_Result::INVALID_ID: {
mobile_result = mobile_apis::Result::INVALID_ID;
break;
}
case hmi_apis::Common_Result::DUPLICATE_NAME: {
mobile_result = mobile_apis::Result::DUPLICATE_NAME;
break;
}
case hmi_apis::Common_Result::APPLICATION_NOT_REGISTERED: {
mobile_result = mobile_apis::Result::APPLICATION_NOT_REGISTERED;
break;
}
case hmi_apis::Common_Result::WRONG_LANGUAGE: {
mobile_result = mobile_apis::Result::WRONG_LANGUAGE;
break;
}
case hmi_apis::Common_Result::OUT_OF_MEMORY: {
mobile_result = mobile_apis::Result::OUT_OF_MEMORY;
break;
}
case hmi_apis::Common_Result::TOO_MANY_PENDING_REQUESTS: {
mobile_result = mobile_apis::Result::TOO_MANY_PENDING_REQUESTS;
break;
}
case hmi_apis::Common_Result::NO_APPS_REGISTERED: {
mobile_result = mobile_apis::Result::APPLICATION_NOT_REGISTERED;
break;
}
case hmi_apis::Common_Result::NO_DEVICES_CONNECTED: {
mobile_result = mobile_apis::Result::APPLICATION_NOT_REGISTERED;
break;
}
case hmi_apis::Common_Result::WARNINGS: {
mobile_result = mobile_apis::Result::WARNINGS;
break;
}
case hmi_apis::Common_Result::GENERIC_ERROR: {
mobile_result = mobile_apis::Result::GENERIC_ERROR;
break;
}
case hmi_apis::Common_Result::USER_DISALLOWED: {
mobile_result = mobile_apis::Result::USER_DISALLOWED;
break;
}
case hmi_apis::Common_Result::SAVED: {
mobile_result = mobile_apis::Result::SAVED;
break;
}
case hmi_apis::Common_Result::READ_ONLY: {
mobile_result = mobile_apis::Result::READ_ONLY;
break;
}
default: {
LOG4CXX_ERROR(logger_, "Unknown HMI result code " << hmi_code);
break;
}
}
return mobile_result;
}
bool CommandRequestImpl::CheckAllowedParameters(
const Command::CommandSource source) {
LOG4CXX_AUTO_TRACE(logger_);
// RegisterAppInterface should always be allowed
if (mobile_apis::FunctionID::RegisterAppInterfaceID ==
static_cast<mobile_apis::FunctionID::eType>(function_id())) {
return true;
}
return CommandImpl::CheckAllowedParameters(source);
}
bool CommandRequestImpl::CheckHMICapabilities(
const mobile_apis::ButtonName::eType button) const {
LOG4CXX_AUTO_TRACE(logger_);
using namespace smart_objects;
using namespace mobile_apis;
if (!hmi_capabilities_.is_ui_cooperating()) {
LOG4CXX_ERROR(logger_, "UI is not supported by HMI");
return false;
}
auto button_capabilities = hmi_capabilities_.button_capabilities();
if (!button_capabilities) {
LOG4CXX_ERROR(logger_, "Invalid button capabilities object");
return false;
}
for (size_t i = 0; i < button_capabilities->length(); ++i) {
const SmartObject& capabilities = (*button_capabilities)[i];
const ButtonName::eType current_button = static_cast<ButtonName::eType>(
capabilities.getElement(hmi_response::button_name).asInt());
if (current_button == button) {
LOG4CXX_DEBUG(logger_,
"Button capabilities for " << button << " was found");
return true;
}
}
LOG4CXX_DEBUG(logger_,
"Button capabilities for " << button << " was not found");
return false;
}
void CommandRequestImpl::AddDisallowedParameterToInfoString(
std::string& info, const std::string& param) const {
// prepare disallowed params enumeration for response info string
if (info.empty()) {
info = "\'" + param + "\'";
} else {
info = info + "," + " " + "\'" + param + "\'";
}
}
void CommandRequestImpl::AddDisallowedParametersToInfo(
smart_objects::SmartObject& response) const {
std::string info;
RPCParams::const_iterator it =
removed_parameters_permissions_.disallowed_params.begin();
for (; it != removed_parameters_permissions_.disallowed_params.end(); ++it) {
AddDisallowedParameterToInfoString(info, (*it));
}
it = removed_parameters_permissions_.undefined_params.begin();
for (; it != removed_parameters_permissions_.undefined_params.end(); ++it) {
AddDisallowedParameterToInfoString(info, (*it));
}
if (!info.empty()) {
info += " disallowed by policies.";
if (!response[strings::msg_params][strings::info].asString().empty()) {
// If we already have info add info about disallowed params to it
response[strings::msg_params][strings::info] =
response[strings::msg_params][strings::info].asString() + " " + info;
} else {
response[strings::msg_params][strings::info] = info;
}
}
}
void CommandRequestImpl::AddDisallowedParameters(
smart_objects::SmartObject& response) {
DisallowedParamsInserter disallowed_inserter(
response, mobile_apis::VehicleDataResultCode::VDRC_USER_DISALLOWED);
std::for_each(removed_parameters_permissions_.disallowed_params.begin(),
removed_parameters_permissions_.disallowed_params.end(),
disallowed_inserter);
DisallowedParamsInserter undefined_inserter(
response, mobile_apis::VehicleDataResultCode::VDRC_DISALLOWED);
std::for_each(removed_parameters_permissions_.undefined_params.begin(),
removed_parameters_permissions_.undefined_params.end(),
undefined_inserter);
}
bool CommandRequestImpl::HasDisallowedParams() const {
return ((!removed_parameters_permissions_.disallowed_params.empty()) ||
(!removed_parameters_permissions_.undefined_params.empty()));
}
bool CommandRequestImpl::IsMobileResultSuccess(
const mobile_apis::Result::eType result_code) {
LOG4CXX_AUTO_TRACE(logger_);
using namespace helpers;
return Compare<mobile_apis::Result::eType, EQ, ONE>(
result_code,
mobile_apis::Result::SUCCESS,
mobile_apis::Result::WARNINGS,
mobile_apis::Result::WRONG_LANGUAGE,
mobile_apis::Result::RETRY,
mobile_apis::Result::SAVED,
mobile_apis::Result::TRUNCATED_DATA);
}
bool CommandRequestImpl::IsHMIResultSuccess(
const hmi_apis::Common_Result::eType result_code) {
LOG4CXX_AUTO_TRACE(logger_);
using namespace helpers;
return Compare<hmi_apis::Common_Result::eType, EQ, ONE>(
result_code,
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::TRUNCATED_DATA);
}
bool CommandRequestImpl::PrepareResultForMobileResponse(
hmi_apis::Common_Result::eType result_code,
HmiInterfaces::InterfaceID interface) const {
LOG4CXX_AUTO_TRACE(logger_);
if (IsHMIResultSuccess(result_code)) {
return true;
}
const HmiInterfaces::InterfaceState state =
application_manager_.hmi_interfaces().GetInterfaceState(interface);
if ((hmi_apis::Common_Result::UNSUPPORTED_RESOURCE == result_code) &&
(HmiInterfaces::STATE_NOT_AVAILABLE != state)) {
return true;
}
return false;
}
bool CommandRequestImpl::PrepareResultForMobileResponse(
ResponseInfo& out_first, ResponseInfo& out_second) const {
LOG4CXX_AUTO_TRACE(logger_);
bool result =
CheckResult(out_first, out_second) || CheckResult(out_second, out_first);
return result;
}
void CommandRequestImpl::GetInfo(
const smart_objects::SmartObject& response_from_hmi,
std::string& out_info) {
if (response_from_hmi[strings::msg_params].keyExists(strings::info)) {
if (!response_from_hmi[strings::msg_params][strings::info].empty()) {
out_info =
response_from_hmi[strings::msg_params][strings::info].asString();
}
}
}
mobile_apis::Result::eType CommandRequestImpl::PrepareResultCodeForResponse(
const ResponseInfo& first, const ResponseInfo& second) {
LOG4CXX_AUTO_TRACE(logger_);
mobile_apis::Result::eType result_code = mobile_apis::Result::INVALID_ENUM;
if (IsResultCodeUnsupported(first, second) ||
IsResultCodeUnsupported(second, first)) {
return mobile_apis::Result::UNSUPPORTED_RESOURCE;
}
if (IsResultCodeWarning(first, second) ||
IsResultCodeWarning(second, first)) {
return mobile_apis::Result::WARNINGS;
}
// If response contains erroneous result code SDL need return erroneus
// result code.
hmi_apis::Common_Result::eType first_result =
hmi_apis::Common_Result::INVALID_ENUM;
hmi_apis::Common_Result::eType second_result =
hmi_apis::Common_Result::INVALID_ENUM;
if (!first.is_unsupported_resource) {
first_result = first.result_code;
}
if (!second.is_unsupported_resource) {
second_result = second.result_code;
}
result_code =
MessageHelper::HMIToMobileResult(std::max(first_result, second_result));
return result_code;
}
const CommandParametersPermissions& CommandRequestImpl::parameters_permissions()
const {
return parameters_permissions_;
}
void CommandRequestImpl::StartAwaitForInterface(
const HmiInterfaces::InterfaceID interface_id) {
sync_primitives::AutoLock lock(awaiting_response_interfaces_lock_);
awaiting_response_interfaces_.insert(interface_id);
}
bool CommandRequestImpl::IsInterfaceAwaited(
const HmiInterfaces::InterfaceID& interface_id) const {
sync_primitives::AutoLock lock(awaiting_response_interfaces_lock_);
std::set<HmiInterfaces::InterfaceID>::const_iterator it =
awaiting_response_interfaces_.find(interface_id);
return (it != awaiting_response_interfaces_.end());
}
void CommandRequestImpl::EndAwaitForInterface(
const HmiInterfaces::InterfaceID& interface_id) {
sync_primitives::AutoLock lock(awaiting_response_interfaces_lock_);
std::set<HmiInterfaces::InterfaceID>::const_iterator it =
awaiting_response_interfaces_.find(interface_id);
if (it != awaiting_response_interfaces_.end()) {
awaiting_response_interfaces_.erase(it);
} else {
LOG4CXX_WARN(logger_,
"EndAwaitForInterface called on interface \
which was not put into await state: "
<< interface_id);
}
}
bool CommandRequestImpl::IsResultCodeUnsupported(
const ResponseInfo& first, const ResponseInfo& second) const {
const bool first_ok_second_unsupported =
(first.is_ok || first.is_not_used) && second.is_unsupported_resource;
const bool both_unsupported =
first.is_unsupported_resource && second.is_unsupported_resource;
return first_ok_second_unsupported || both_unsupported;
}
std::string GetComponentNameFromInterface(
const HmiInterfaces::InterfaceID& interface) {
switch (interface) {
case HmiInterfaces::HMI_INTERFACE_Buttons:
return hmi_interface::buttons;
case HmiInterfaces::HMI_INTERFACE_BasicCommunication:
return hmi_interface::basic_communication;
case HmiInterfaces::HMI_INTERFACE_VR:
return hmi_interface::vr;
case HmiInterfaces::HMI_INTERFACE_TTS:
return hmi_interface::tts;
case HmiInterfaces::HMI_INTERFACE_UI:
return hmi_interface::ui;
case HmiInterfaces::HMI_INTERFACE_Navigation:
return hmi_interface::navigation;
case HmiInterfaces::HMI_INTERFACE_VehicleInfo:
return hmi_interface::vehicle_info;
case HmiInterfaces::HMI_INTERFACE_SDL:
return hmi_interface::sdl;
case HmiInterfaces::HMI_INTERFACE_RC:
return hmi_interface::rc;
case HmiInterfaces::HMI_INTERFACE_AppService:
return hmi_interface::app_service;
default:
return "Unknown type";
}
}
const std::string InfoInterfaceSeparator(
const std::string& sum, const HmiInterfaces::InterfaceID container_value) {
return sum.empty()
? GetComponentNameFromInterface(container_value)
: sum + ", " + GetComponentNameFromInterface(container_value);
}
void CommandRequestImpl::AddTimeOutComponentInfoToMessage(
smart_objects::SmartObject& response) const {
using ns_smart_device_link::ns_smart_objects::SmartObject;
LOG4CXX_AUTO_TRACE(logger_);
sync_primitives::AutoLock lock(awaiting_response_interfaces_lock_);
if (awaiting_response_interfaces_.empty()) {
LOG4CXX_ERROR(logger_, "No interfaces awaiting, info param is empty");
return;
}
const std::string not_responding_interfaces_string =
std::accumulate(awaiting_response_interfaces_.begin(),
awaiting_response_interfaces_.end(),
std::string(""),
InfoInterfaceSeparator);
LOG4CXX_DEBUG(
logger_,
"Not responding interfaces string: " << not_responding_interfaces_string);
if (!not_responding_interfaces_string.empty()) {
const std::string component_info =
not_responding_interfaces_string + " component does not respond";
response[strings::msg_params][strings::info] = component_info;
}
}
} // namespace commands
} // namespace application_manager
|