summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/commands/mobile/system_request.cc
blob: 2e38f567145cdc11df8bf7347af1ccc7953ec7f1 (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
/*

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 "application_manager/commands/mobile/system_request.h"

#include <vector>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <sstream>
#include "application_manager/policies/policy_handler_interface.h"
#include "interfaces/MOBILE_API.h"
#include "utils/file_system.h"
#include "formatters/CFormatterJsonBase.h"

#include "utils/helpers.h"
#include "utils/custom_string.h"
#include "utils/json_utils.h"

#if defined(_MSC_VER)
#define snprintf _snprintf_s
#endif

namespace application_manager {
CREATE_LOGGERPTR_LOCAL(logger_, "ApplicationManager")
namespace {

#ifdef ENABLE_LOG
const char* kQueryAppsValidationFailedPrefix =
    ":QUERY_APPS_VALIDATION_FAILED: ";
#endif

const unsigned int kVrSynonymLengthMax = 40U;
const unsigned int kVrSynonymLengthMin = 1U;
const unsigned int kVrArraySizeMax = 100U;
const unsigned int kVrArraySizeMin = 1U;

class QueryAppsDataValidator {
 public:
  typedef std::set<std::string> SynonymsSet;
  typedef std::map<std::string, SynonymsSet> SynonymsMap;

  QueryAppsDataValidator(const smart_objects::SmartObject& object,
                         const ApplicationManager& manager)
      : data_(object), manager_(manager) {}

  bool Validate() const {
    LOGGER_AUTO_TRACE(logger_);
    if (!data_.isValid()) {
      LOGGER_ERROR(logger_,
                   kQueryAppsValidationFailedPrefix
                       << "QueryApps response is not valid.");
      return false;
    }
    if (!HasResponseKey()) {
      return false;
    }
    return true;
  }

 private:
  bool HasResponseKey() const {
    if (!data_.keyExists(json::response)) {
      LOGGER_WARN(logger_,
                  kQueryAppsValidationFailedPrefix
                      << "QueryApps response does not contain '"
                      << json::response << "' parameter.");
      return false;
    }
    return true;
  }

  bool ValidateSynonymsAtLanguage(const smart_objects::SmartObject& language,
                                  const std::string& language_name,
                                  SynonymsMap& synonyms_map) const {
    if (!language[language_name].keyExists(json::vrSynonyms)) {
      LOGGER_WARN(logger_,
                  kQueryAppsValidationFailedPrefix
                      << "'languages.vrSynonyms' doesn't exist");
      return false;
    }
    const smart_objects::SmartArray* synonyms_array =
        language[language_name][json::vrSynonyms].asArray();
    if (!synonyms_array) {
      LOGGER_WARN(logger_,
                  kQueryAppsValidationFailedPrefix
                      << "vrSynonyms is not array.");
      return false;
    }
    const size_t synonyms_array_size = synonyms_array->size();
    if (synonyms_array_size < kVrArraySizeMin) {
      LOGGER_WARN(logger_,
                  kQueryAppsValidationFailedPrefix
                      << "vrSynomyms array has [" << synonyms_array_size
                      << "] size < allowed min size [" << kVrArraySizeMin
                      << "]");
      return false;
    }
    if (synonyms_array_size > kVrArraySizeMax) {
      LOGGER_WARN(logger_,
                  kQueryAppsValidationFailedPrefix
                      << "vrSynomyms array size [" << synonyms_array_size
                      << "] exceeds maximum allowed size [" << kVrArraySizeMax
                      << "]");
      return false;
    }

    for (std::size_t idx = 0; idx < synonyms_array_size; ++idx) {
      const smart_objects::SmartObject& synonym = (*synonyms_array)[idx];
      const std::string vrSynonym = synonym.asString();
      if (vrSynonym.length() > kVrSynonymLengthMax) {
        LOGGER_WARN(logger_,
                    kQueryAppsValidationFailedPrefix
                        << "vrSYnomym item [" << idx << "] exceeds max length ["
                        << vrSynonym.length() << "]>[" << kVrSynonymLengthMax
                        << "]");
        return false;
      }
      if (vrSynonym.length() < kVrSynonymLengthMin) {
        LOGGER_WARN(logger_,
                    kQueryAppsValidationFailedPrefix
                        << "vrSYnomym item [" << idx << "] length ["
                        << vrSynonym.length() << "] is less then min length ["
                        << kVrSynonymLengthMin << "] allowed.");
        return false;
      }
      // Verify duplicates
      SynonymsMap::iterator synonyms_map_iter =
          synonyms_map.find(language_name);
      if (synonyms_map_iter != synonyms_map.end()) {
        if (!(*synonyms_map_iter).second.insert(vrSynonym).second) {
          LOGGER_WARN(logger_,
                      kQueryAppsValidationFailedPrefix
                          << "vrSYnomym item already defined ["
                          << vrSynonym.c_str() << "] for language ["
                          << language_name << "]");
          return false;
        }
      }
    }
    return true;
  }

  const smart_objects::SmartObject& data_;
  const ApplicationManager& manager_;

  DISALLOW_COPY_AND_ASSIGN(QueryAppsDataValidator);
};
}

namespace commands {
namespace custom_str = utils::custom_string;

uint32_t SystemRequest::index = 0;

const std::string kSYNC = "SYNC";
const std::string kIVSU = "IVSU";

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

SystemRequest::~SystemRequest() {}

void SystemRequest::Run() {
  LOGGER_AUTO_TRACE(logger_);

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

  if (!(application.valid())) {
    LOGGER_ERROR(logger_, "NULL pointer");
    SendResponse(false, mobile_apis::Result::APPLICATION_NOT_REGISTERED);
    return;
  }

  const mobile_apis::RequestType::eType request_type =
      static_cast<mobile_apis::RequestType::eType>(
          (*message_)[strings::msg_params][strings::request_type].asInt());

  const policy::PolicyHandlerInterface& policy_handler =
      application_manager_.GetPolicyHandler();
  if (!policy_handler.IsRequestTypeAllowed(application->policy_app_id(),
                                           request_type)) {
    SendResponse(false, mobile_apis::Result::DISALLOWED);
    return;
  }

  std::string file_name;
  if ((*message_)[strings::msg_params].keyExists(strings::file_name)) {
    file_name = (*message_)[strings::msg_params][strings::file_name].asString();
  } else {
    file_name = kSYNC;
  }

  bool is_system_file = std::string::npos != file_name.find(kSYNC) ||
                        std::string::npos != file_name.find(kIVSU);

  // to avoid override existing file
  if (is_system_file) {
    const uint8_t max_size = 255;
    char buf[max_size] = {'\0'};
    snprintf(buf, max_size - 1, "%d%s", index++, file_name.c_str());
    file_name = buf;
  }

  std::vector<uint8_t> binary_data;
  std::string binary_data_folder;
  if ((*message_)[strings::params].keyExists(strings::binary_data)) {
    binary_data = (*message_)[strings::params][strings::binary_data].asBinary();
    binary_data_folder =
        application_manager_.get_settings().system_files_path();
  } else {
    binary_data_folder = file_system::ConcatPath(
        application_manager_.get_settings().app_storage_folder(),
        application->folder_name());
    binary_data_folder += file_system::GetPathDelimiter();
  }

  std::string file_dst_path = file_system::ConcatPath(
      application_manager_.get_settings().system_files_path(), file_name);

  if ((*message_)[strings::params].keyExists(strings::binary_data)) {
    LOGGER_DEBUG(
        logger_,
        "Binary data is present. Trying to save it to: " << binary_data_folder);
    if (mobile_apis::Result::SUCCESS !=
        (application_manager_.SaveBinary(
            binary_data, binary_data_folder, file_name, 0))) {
      LOGGER_DEBUG(logger_, "Binary data can't be saved.");
      SendResponse(false, mobile_apis::Result::GENERIC_ERROR);
      return;
    }
  } else {
    std::string app_full_file_path = binary_data_folder;
    app_full_file_path += file_name;

    LOGGER_DEBUG(logger_,
                 "Binary data is not present. Trying to find file "
                     << file_name << " within previously saved app file in "
                     << binary_data_folder);

    const AppFile* file = application->GetFile(app_full_file_path);
    if (!file || !file->is_download_complete ||
        !file_system::MoveFile(app_full_file_path, file_dst_path)) {
      LOGGER_DEBUG(logger_, "Binary data not found.");

      std::string origin_file_name;
      if ((*message_)[strings::msg_params].keyExists(strings::file_name)) {
        origin_file_name =
            (*message_)[strings::msg_params][strings::file_name].asString();
      }
      if (!(mobile_apis::RequestType::HTTP == request_type &&
            0 == origin_file_name.compare(kIVSU))) {
        LOGGER_DEBUG(logger_, "Binary data required. Reject");
        SendResponse(false, mobile_apis::Result::REJECTED);
        return;
      }
      LOGGER_DEBUG(logger_, "IVSU does not require binary data. Continue");
    }
    processing_file_ = file_dst_path;
  }

  LOGGER_DEBUG(logger_, "Binary data ok.");

  if (mobile_apis::RequestType::QUERY_APPS == request_type) {
    using namespace NsSmartDeviceLink::NsJSONHandler::Formatters;
    using namespace utils::json;

    smart_objects::SmartObject sm_object;
    std::string json_string(binary_data.begin(), binary_data.end());

    JsonValue::ParseResult parse_result = JsonValue::Parse(json_string);
    if (!parse_result.second) {
      LOGGER_DEBUG(logger_, "Unable to parse query_app json file.");
      return;
    }
    JsonValue& root_json = parse_result.first;

    CFormatterJsonBase::jsonValueToObj(root_json, sm_object);

    if (!ValidateQueryAppData(sm_object)) {
      SendResponse(false, mobile_apis::Result::GENERIC_ERROR);
      return;
    }

    application_manager_.ProcessQueryApp(sm_object, connection_key());
    SendResponse(true, mobile_apis::Result::SUCCESS);
    return;
  }

  smart_objects::SmartObject msg_params =
      smart_objects::SmartObject(smart_objects::SmartType_Map);
  if (std::string::npos != file_name.find(kIVSU)) {
    msg_params[strings::file_name] = file_name;
  } else {
    msg_params[strings::file_name] = file_dst_path;
  }

  if (mobile_apis::RequestType::PROPRIETARY != request_type) {
    msg_params[strings::app_id] = (application->policy_app_id());
  }
  msg_params[strings::request_type] =
      (*message_)[strings::msg_params][strings::request_type];
  SendHMIRequest(hmi_apis::FunctionID::BasicCommunication_SystemRequest,
                 &msg_params,
                 true);
}

void SystemRequest::on_event(const event_engine::Event& event) {
  LOGGER_AUTO_TRACE(logger_);
  using namespace helpers;

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

  switch (event.id()) {
    case hmi_apis::FunctionID::BasicCommunication_SystemRequest: {
      mobile_apis::Result::eType result_code =
          GetMobileResultCode(static_cast<hmi_apis::Common_Result::eType>(
              message[strings::params][hmi_response::code].asUInt()));

      const bool result = Compare<mobile_api::Result::eType, EQ, ONE>(
          result_code,
          mobile_api::Result::SUCCESS,
          mobile_api::Result::WARNINGS);

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

      if (!(application.valid())) {
        LOGGER_ERROR(logger_, "NULL pointer");
        return;
      }

      if (!processing_file_.empty()) {
        file_system::DeleteFile(processing_file_);
        processing_file_.clear();
      }
      SendResponse(result, result_code, NULL, &(message[strings::msg_params]));
      break;
    }
    default: {
      LOGGER_ERROR(logger_, "Received unknown event" << event.id());
      return;
    }
  }
}

bool SystemRequest::ValidateQueryAppData(
    const smart_objects::SmartObject& data) const {
  if (!data.isValid()) {
    LOGGER_ERROR(logger_,
                 kQueryAppsValidationFailedPrefix
                     << "QueryApps response is not valid.");
    return false;
  }
  if (!data.keyExists(json::response)) {
    LOGGER_ERROR(logger_,
                 kQueryAppsValidationFailedPrefix
                     << "QueryApps response does not contain '"
                     << json::response << "' parameter.");
    return false;
  }

  QueryAppsDataValidator validator(data, application_manager_);
  return validator.Validate();
}

}  // namespace commands

}  // namespace application_manager