summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/rc_rpc_plugin/src/commands/mobile/release_interior_vehicle_data_module_request.cc
blob: e7532636b2ae0795adffa1ca36e166af91188b2f (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
/*
 * Copyright (c) 2019, 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 <string>

#include "application_manager/rpc_plugins/rc_rpc_plugin/include/rc_rpc_plugin/resource_allocation_manager.h"
#include "rc_rpc_plugin/commands/mobile/release_interior_vehicle_data_module_request.h"
#include "rc_rpc_plugin/rc_module_constants.h"
#include "smart_objects/enum_schema_item.h"

namespace rc_rpc_plugin {
using namespace application_manager;
namespace commands {

SDL_CREATE_LOG_VARIABLE("Commands")

namespace {
struct ResponseParams {
  std::string response_info;
  std::string module_type;
  std::string module_id;
  mobile_apis::Result::eType result_code;
  uint32_t app_id;
  bool success_result;
};

void PrepareResponseResult(
    ResponseParams& response_params_out,
    rc_rpc_plugin::ResourceReleasedState::eType& released_result) {
  std::stringstream ss;
  auto info_inserter = [&ss, response_params_out](std::string info) {
    ss << "Module [" << response_params_out.module_type << ":"
       << response_params_out.module_id << "] " << info;
  };
  switch (released_result) {
    case rc_rpc_plugin::ResourceReleasedState::eType::IS_RELEASED: {
      response_params_out.success_result = true;
      response_params_out.result_code = mobile_apis::Result::eType::SUCCESS;
      info_inserter("is released successfully.");
      break;
    }
    case rc_rpc_plugin::ResourceReleasedState::eType::IS_ALLOCATED: {
      response_params_out.success_result = false;
      response_params_out.result_code = mobile_apis::Result::eType::REJECTED;
      info_inserter("is allocated to a different application.");
      break;
    }
    case rc_rpc_plugin::ResourceReleasedState::eType::NOT_ALLOCATED: {
      response_params_out.success_result = false;
      response_params_out.result_code = mobile_apis::Result::eType::IGNORED;
      info_inserter("is not allocated to any application.");
      break;
    }
  }
  response_params_out.response_info = ss.str();
}
}  // namespace

ReleaseInteriorVehicleDataModuleRequest::
    ReleaseInteriorVehicleDataModuleRequest(
        const application_manager::commands::MessageSharedPtr& message,
        const RCCommandParams& params)
    : RCCommandRequest(message, params) {}

bool ReleaseInteriorVehicleDataModuleRequest::ProcessCapabilities() {
  SDL_LOG_AUTO_TRACE();
  const auto rc_capability = hmi_capabilities_.rc_capability();

  const std::string module_type = ModuleType();
  const std::string module_id = ModuleId();
  const ModuleUid module(module_type, module_id);
  if (rc_capability &&
      !rc_capabilities_manager_.CheckIfModuleExistsInCapabilities(module)) {
    SDL_LOG_WARN("Accessing not supported module: " << module_type << " "
                                                    << module_id);
    SendResponse(false,
                 mobile_apis::Result::UNSUPPORTED_RESOURCE,
                 "Accessing not supported module");
    return false;
  }
  return true;
}

void ReleaseInteriorVehicleDataModuleRequest::Execute() {
  SDL_LOG_AUTO_TRACE();

  if (!ProcessCapabilities()) {
    return;
  }

  const std::string module_type = ModuleType();
  const std::string module_id = ModuleId();

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

  if (!app) {
    SDL_LOG_ERROR("Application is not registered");
    SendResponse(false, mobile_apis::Result::APPLICATION_NOT_REGISTERED);
    return;
  }

  const uint32_t app_id = app->app_id();

  ResourceReleasedState::eType released_result =
      resource_allocation_manager_.ReleaseResource(
          module_type, module_id, app_id);

  bool success_result = true;
  mobile_apis::Result::eType result_code = mobile_apis::Result::eType::SUCCESS;
  std::string response_info;
  ResponseParams response_params{response_info,
                                 ModuleType(),
                                 module_id,
                                 result_code,
                                 app_id,
                                 success_result};
  PrepareResponseResult(response_params, released_result);

  if (response_params.success_result) {
    resource_allocation_manager_.SendOnRCStatusNotifications(
        NotificationTrigger::MODULE_ALLOCATION, app);
  }

  SDL_LOG_DEBUG("Resource for module: "
                << ModuleType() << " with id: " << module_id
                << " was released with result " << std::boolalpha
                << response_params.success_result
                << " and result_code: " << response_params.result_code);

  SendResponse(response_params.success_result,
               response_params.result_code,
               response_params.response_info.c_str());
}

std::string ReleaseInteriorVehicleDataModuleRequest::ModuleType() const {
  SDL_LOG_AUTO_TRACE();
  mobile_apis::ModuleType::eType module_type =
      static_cast<mobile_apis::ModuleType::eType>(
          (*message_)[app_mngr::strings::msg_params]
                     [message_params::kModuleType]
                         .asUInt());

  const char* str;
  const bool ok = ns_smart_device_link::ns_smart_objects::EnumConversionHelper<
      mobile_apis::ModuleType::eType>::EnumToCString(module_type, &str);
  return ok ? str : "unknown";
}

std::string ReleaseInteriorVehicleDataModuleRequest::ModuleId() const {
  SDL_LOG_AUTO_TRACE();
  auto msg_params = (*message_)[app_mngr::strings::msg_params];
  if (msg_params.keyExists(message_params::kModuleId)) {
    return msg_params[message_params::kModuleId].asString();
  }
  const std::string module_id =
      rc_capabilities_manager_.GetDefaultModuleIdFromCapabilities(ModuleType());
  return module_id;
}

ReleaseInteriorVehicleDataModuleRequest::
    ~ReleaseInteriorVehicleDataModuleRequest() {}

}  // namespace commands
}  // namespace rc_rpc_plugin