summaryrefslogtreecommitdiff
path: root/src/components/remote_control/include/remote_control/commands/base_command_request.h
blob: 64128b971ef391e0a5d53d114618099f7ed13d88 (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
/*
 Copyright (c) 2017, 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.
 */

#ifndef SRC_COMPONENTS_REMOTE_CONTROL_INCLUDE_REMOTE_CONTROL_COMMANDS_BASE_COMMAND_REQUEST_H_
#define SRC_COMPONENTS_REMOTE_CONTROL_INCLUDE_REMOTE_CONTROL_COMMANDS_BASE_COMMAND_REQUEST_H_

#include "remote_control/commands/command.h"
#include "remote_control/event_engine/event_observer.h"
#include "application_manager/message.h"
#include "application_manager/service.h"
#include "utils/logger.h"
#include "interfaces/HMI_API.h"
#include "remote_control/rc_app_extension.h"
#include "json/json.h"
#include "remote_control/remote_plugin_interface.h"

namespace remote_control {

namespace commands {

// Forward declaration to make this struct friend to BaseCommandRequest
struct OnDriverAnswerCallback;

/**
 * @brief Base command class for requests
 */
class BaseCommandRequest
    : public Command,
      public rc_event_engine::EventObserver<application_manager::MessagePtr,
                                            std::string> {
 public:
  /**
   * @brief BaseCommandRequest class constructor
   *
   * @param message Message from mobile
   **/
  BaseCommandRequest(const application_manager::MessagePtr& message,
                     RemotePluginInterface& rc_module);

  /**
   * @brief BaseCommandRequest class destructor
   */
  virtual ~BaseCommandRequest();

  /**
   * @brief BaseCommandRequest on timeout reaction
   */
  virtual void OnTimeout();

  void Run();
  void on_event(const rc_event_engine::Event<application_manager::MessagePtr,
                                             std::string>& event);

  /**
   * @brief Generates correct request to HMI
   * @param function_id request ID
   * @param msg_params json with message params
   * @return generated request shared ptr
   */
  application_manager::MessagePtr CreateHmiRequest(
      const char* function_id, const Json::Value& message_params);

  /**
   * @brief Prepares response for sending to mobile
   * Adds necessary fields to message
   * @param success true if successful; false, if failed
   * @param result_code Mobile result code in string ("SUCCESS", "INVALID_DATA",
   * e.t.c)
   * @param info Provides additional human readable info regarding the
   *result(may be empty)
   */
  void PrepareResponse(const bool success,
                       const char* result_code,
                       const std::string& info);

 protected:
  application_manager::MessagePtr message_;
  Json::Value response_params_;

  /**
   * @brief AcquireResource try to allocate resource for application
   * In case if allocation of resource is not required, return ALLOWED by
   * default.
   * This method should be overrided in RPCs that requires resource allocation
   * @return result of resource allocation, in case if allocation os not
   * required, return ALLOWED
   */
  virtual AcquireResult::eType AcquireResource(const Json::Value&) {
    return AcquireResult::ALLOWED;
  }

  /**
   * @brief IsResourceFree check resource state
   * This is default implementation which has to be redefined for RPCs which
   * need to manage the resources
   * @param module_type Resource name
   * @return True if free, otherwise - false
   */
  virtual bool IsResourceFree(const std::string& module_type) const {
    UNUSED(module_type);
    return true;
  }

  /**
   * @brief SetResourceState changes state of resource
   * This is default implementation which has to be redefined for RPCs which
   * need to manage the resources
   * @param Message containing type of module to extract
   * @param State to set for resource
   */
  virtual void SetResourceState(const Json::Value&,
                                const ResourceState::eType) {}

  /**
   * @brief Get extension for specified application. If extension doesn't exist,
   * it will be created
   * @param app pointer to application
   * @return pointer to extension
   */
  RCAppExtensionPtr GetAppExtension(
      application_manager::ApplicationSharedPtr app) const;

  /**
   * @brief Converts HMI result code to string with mobile result code
   *
   * @param hmi_code HMI result code
   * @return String with mobile result code
   */
  const char* GetMobileResultCode(
      const hmi_apis::Common_Result::eType& hmi_code) const;

  /**
   * @brief Sends Mobile response
   * @param success true if successful; false, if failed
   * @param result_code Mobile result code in string ("SUCCESS", "INVALID_DATA",
   *e.t.c)
   * @param info Provides additional human readable info regarding the
   *result(may be empty)
   */
  void SendResponse(const bool success,
                    const char* result_code,
                    const std::string& info);

  /**
   * @brief Parse result code from response
   *
   * @param message Response from HMI or Can
   * @param result_code Outgoing param with mobile result code in string
   *("SUCCESS", "INVALID_DATA", e.t.c)
   * @param info Outgoing param with additional human readable info regarding
   *the result(may be empty)
   * @return true if it is success response? otherwise false
   */
  bool ParseResultCode(const Json::Value& value,
                       std::string& result_code,
                       std::string& info);

  /**
     * @brief Sends request to HMI
     * @param message_to_send to send
     */
  void SendMessageToHMI(const application_manager::MessagePtr& message_to_send);

  /**
   * @brief Sends request to CAN or HMI
   * @param function_id request ID
   * @param msg_params json with message params
   */
  void SendRequest(const char* function_id, const Json::Value& message_params);

  application_manager::ApplicationSharedPtr app() {
    DCHECK(app_);
    return app_;
  }

  /**
   * @brief executes specific logic of children classes
   */
  void virtual Execute() = 0;

  /**
   * @brief Validates request by xml schema
   */
  bool Validate();

  /*
   * @brief Parses incoming string into Json
   * @param parsed_mgs Resulting json object (must be valid pointer)
   * @returns True if json string was valid false otherwise.
   */
  virtual bool ParseJsonString(Json::Value* parsed_msg);

  /**
   * @brief Interface method that is called whenever new event received
   * @param event The received event
   */
  void virtual OnEvent(
      const rc_event_engine::Event<application_manager::MessagePtr,
                                   std::string>& event) = 0;

  virtual std::string ModuleType(const Json::Value& message);
  virtual std::vector<std::string> ControlData(const Json::Value& message);
  virtual application_manager::TypeAccess CheckModule(
      const Json::Value& message);

  bool auto_allowed() const {
    return auto_allowed_;
  }

  void set_auto_allowed(bool value) {
    auto_allowed_ = value;
  }

  application_manager::ServicePtr service() {
    return service_;
  }

  void set_disallowed_info(const std::string& info) {
    disallowed_info_ = info;
  }

 private:
  /**
   * @brief CheckPolicyPermissions checks RPC permissions defined in policy
   * table
   * @return True if RPC is allowed, otherwise - false
   */
  bool CheckPolicyPermissions();

  /**
   * @brief CheckDriverConsent checks driver consent defined in policy table
   * @return True if no consent is required, otherwise - false
   */
  bool CheckDriverConsent();

  /**
   * @brief AcquireResources checks whether resource status is busy or not and
   * then tries to acquire this resource. In case driver consent is required -
   * sends consent request to HMI.
   * @return True in case of resource is free and successfully acquired,
   * otherwise false
   */
  bool AcquireResources();
  inline bool IsAutoAllowed(application_manager::TypeAccess access) const;
  void SendDisallowed(application_manager::TypeAccess access);
  void SendGetUserConsent(const Json::Value& value);
  void ProcessAccessResponse(
      const rc_event_engine::Event<application_manager::MessagePtr,
                                   std::string>& event);

  const Json::Value msg_json_;
  application_manager::ApplicationSharedPtr app_;
  application_manager::ServicePtr service_;
  bool auto_allowed_;
  std::string disallowed_info_;

  friend struct OnDriverAnswerCallback;
};

}  // namespace commands

}  // namespace remote_control

#endif  // SRC_COMPONENTS_REMOTE_CONTROL_INCLUDE_REMOTE_CONTROL_COMMANDS_BASE_COMMAND_REQUEST_H_