summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/commands/mobile/send_location_request.cc
blob: 54664a87712e206cfeeab15bddb6cc62d54698a5 (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
/*

 Copyright (c) 2013, 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 "application_manager/commands/mobile/send_location_request.h"
#include "application_manager/message_helper.h"
#include "utils/custom_string.h"

namespace application_manager {

namespace commands {

SendLocationRequest::SendLocationRequest(
    const MessageSharedPtr& message, ApplicationManager& application_manager)
    : CommandRequestImpl(message, application_manager) {}

SendLocationRequest::~SendLocationRequest() {}

void SendLocationRequest::Run() {
  using namespace hmi_apis;
  using smart_objects::SmartObject;
  LOG4CXX_AUTO_TRACE(logger_);

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

  if (!app) {
    LOG4CXX_ERROR(logger_,
                  "An application with connection key "
                      << connection_key() << " is not registered.");
    SendResponse(false, mobile_apis::Result::APPLICATION_NOT_REGISTERED);
    return;
  }

  smart_objects::SmartObject& msg_params = (*message_)[strings::msg_params];
  if (msg_params.keyExists(strings::delivery_mode)) {
    const RPCParams& allowed_params = parameters_permissions().allowed_params;

    if (helpers::in_range(allowed_params, strings::delivery_mode)) {
      msg_params.erase(strings::delivery_mode);
    }
  }

  std::vector<Common_TextFieldName::eType> fields_to_check;
  if (msg_params.keyExists(strings::location_name)) {
    fields_to_check.push_back(Common_TextFieldName::locationName);
  }
  if (msg_params.keyExists(strings::location_description)) {
    fields_to_check.push_back(Common_TextFieldName::locationDescription);
  }
  if (msg_params.keyExists(strings::address_lines)) {
    fields_to_check.push_back(Common_TextFieldName::addressLines);
  }
  if (msg_params.keyExists(strings::phone_number)) {
    fields_to_check.push_back(Common_TextFieldName::phoneNumber);
  }

  if (!CheckHMICapabilities(fields_to_check)) {
    SendResponse(false, mobile_apis::Result::UNSUPPORTED_RESOURCE);
    return;
  }

  if (IsWhiteSpaceExist()) {
    LOG4CXX_ERROR(logger_, "Strings contain invalid characters");
    SendResponse(false, mobile_apis::Result::INVALID_DATA);
    return;
  }

  if (msg_params.keyExists(strings::address)) {
    const utils::custom_string::CustomString& address =
        msg_params[strings::address].asCustomString();
    if (address.empty()) {
      msg_params.erase(strings::address);
    }
  }

  if (!CheckFieldsCompatibility()) {
    LOG4CXX_ERROR(logger_, "CheckFieldsCompatibility failed");
    SendResponse(false, mobile_apis::Result::INVALID_DATA);
    return;
  }

  if (msg_params.keyExists(strings::location_image)) {
    mobile_apis::Result::eType verification_result =
        mobile_apis::Result::SUCCESS;
    verification_result = MessageHelper::VerifyImage(
        (*message_)[strings::msg_params][strings::location_image],
        app,
        application_manager_);
    if (mobile_apis::Result::SUCCESS != verification_result) {
      LOG4CXX_ERROR(logger_, "VerifyImage INVALID_DATA!");
      SendResponse(false, verification_result);
      return;
    }
  }

  SmartObject request_msg_params = SmartObject(smart_objects::SmartType_Map);
  request_msg_params = msg_params;
  request_msg_params[strings::app_id] = app->hmi_app_id();
  StartAwaitForInterface(HmiInterfaces::HMI_INTERFACE_Navigation);
  SendHMIRequest(
      hmi_apis::FunctionID::Navigation_SendLocation, &request_msg_params, true);
}

void SendLocationRequest::on_event(const event_engine::Event& event) {
  LOG4CXX_AUTO_TRACE(logger_);
  using namespace hmi_apis;
  const smart_objects::SmartObject& message = event.smart_object();
  if (hmi_apis::FunctionID::Navigation_SendLocation == event.id()) {
    LOG4CXX_INFO(logger_, "Received Navigation_SendLocation event");
    EndAwaitForInterface(HmiInterfaces::HMI_INTERFACE_Navigation);
    const Common_Result::eType result_code = static_cast<Common_Result::eType>(
        message[strings::params][hmi_response::code].asInt());
    std::string response_info;
    GetInfo(message, response_info);
    const bool result = PrepareResultForMobileResponse(
        result_code, HmiInterfaces::HMI_INTERFACE_Navigation);
    SendResponse(result,
                 MessageHelper::HMIToMobileResult(result_code),
                 response_info.empty() ? NULL : response_info.c_str(),
                 &(message[strings::params]));
    return;
  }
  LOG4CXX_ERROR(logger_, "Received unknown event" << event.id());
}

bool SendLocationRequest::CheckFieldsCompatibility() {
  const smart_objects::SmartObject& msg_params =
      (*message_)[strings::msg_params];
  MessageHelper::PrintSmartObject(msg_params);
  const bool longitude_degrees_exist =
      msg_params.keyExists(strings::longitude_degrees);
  const bool latitude_degrees_exist =
      msg_params.keyExists(strings::latitude_degrees);
  const bool address_exist = msg_params.keyExists(strings::address);

  if (latitude_degrees_exist ^ longitude_degrees_exist) {
    LOG4CXX_DEBUG(logger_,
                  "latitude and longitude should be provided only in pair");
    return false;
  }

  if (!address_exist && !longitude_degrees_exist && !latitude_degrees_exist) {
    LOG4CXX_DEBUG(logger_,
                  "address or latitude/longtitude should should be provided");
    return false;
  }
  return true;
}
void insert_if_contains(
    const smart_objects::SmartObject& msg_params,
    const std::string& param_key,
    std::vector<utils::custom_string::CustomString>& output_vector) {
  if (msg_params.keyExists(param_key)) {
    output_vector.push_back(msg_params[param_key].asCustomString());
  }
}

bool SendLocationRequest::IsWhiteSpaceExist() {
  LOG4CXX_AUTO_TRACE(logger_);
  std::vector<utils::custom_string::CustomString> fields_to_check;
  const smart_objects::SmartObject& msg_params =
      (*message_)[strings::msg_params];
  insert_if_contains(msg_params, strings::location_name, fields_to_check);
  insert_if_contains(
      msg_params, strings::location_description, fields_to_check);
  insert_if_contains(msg_params, strings::phone_number, fields_to_check);

  if (msg_params.keyExists(strings::address_lines)) {
    const smart_objects::SmartArray* al_array =
        msg_params[strings::address_lines].asArray();
    smart_objects::SmartArray::const_iterator it_al = al_array->begin();
    smart_objects::SmartArray::const_iterator it_al_end = al_array->end();
    for (; it_al != it_al_end; ++it_al) {
      const utils::custom_string::CustomString& val = (*it_al).asCustomString();
      fields_to_check.push_back(val);
    }
  }

  if (msg_params.keyExists(strings::address)) {
    const smart_objects::SmartObject& address_so = msg_params[strings::address];
    insert_if_contains(address_so, strings::country_name, fields_to_check);
    insert_if_contains(address_so, strings::country_code, fields_to_check);
    insert_if_contains(address_so, strings::postal_code, fields_to_check);
    insert_if_contains(
        address_so, strings::administrative_area, fields_to_check);
    insert_if_contains(address_so, strings::locality, fields_to_check);
    insert_if_contains(address_so, strings::sub_locality, fields_to_check);
    insert_if_contains(address_so, strings::thoroughfare, fields_to_check);
    insert_if_contains(address_so, strings::sub_thoroughfare, fields_to_check);
  }

  std::vector<utils::custom_string::CustomString>::iterator it =
      fields_to_check.begin();
  for (; it != fields_to_check.end(); ++it) {
    const std::string& str = it->AsMBString();
    if (!CheckSyntax(str, false)) {
      LOG4CXX_ERROR(logger_,
                    "string '" << str << "'' contains invalid characters");
      return true;
    }
  }
  return false;
}

bool SendLocationRequest::CheckHMICapabilities(
    std::vector<hmi_apis::Common_TextFieldName::eType>& fields_names) {
  using namespace smart_objects;
  using namespace hmi_apis;
  if (fields_names.empty()) {
    return true;
  }

  const HMICapabilities& hmi_capabilities =
      application_manager_.hmi_capabilities();
  if (!hmi_capabilities.is_ui_cooperating()) {
    LOG4CXX_ERROR(logger_, "UI is not supported.");
    return false;
  }

  if (hmi_capabilities.display_capabilities()) {
    const SmartObject disp_cap = (*hmi_capabilities.display_capabilities());
    const SmartObject& text_fields =
        disp_cap.getElement(hmi_response::text_fields);
    const size_t len = text_fields.length();
    for (size_t i = 0; i < len; ++i) {
      const SmartObject& text_field = text_fields[i];
      const Common_TextFieldName::eType filed_name =
          static_cast<Common_TextFieldName::eType>(
              text_field.getElement(strings::name).asInt());
      const std::vector<Common_TextFieldName::eType>::iterator it =
          std::find(fields_names.begin(), fields_names.end(), filed_name);
      if (it != fields_names.end()) {
        fields_names.erase(it);
      }
    }
  }

  if (!fields_names.empty()) {
    LOG4CXX_ERROR(logger_, "Some fields are not supported by capabilities");
    return false;
  }
  return true;
}

}  // namespace commands

}  // namespace application_manager