summaryrefslogtreecommitdiff
path: root/src/components/application_manager/include/application_manager/commands/command_request_impl.h
blob: 97e2dce0eb351cbd64c3ffdabfb84e9dbbae1f13 (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
/*
 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.
 */

#ifndef SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_COMMANDS_COMMAND_REQUEST_IMPL_H_
#define SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_COMMANDS_COMMAND_REQUEST_IMPL_H_

#include <memory>

#include "application_manager/application_manager.h"
#include "application_manager/commands/command_impl.h"
#include "application_manager/event_engine/event_observer.h"
#include "application_manager/hmi_interfaces.h"
#include "application_manager/smart_object_keys.h"
#include "interfaces/HMI_API.h"
#include "interfaces/MOBILE_API.h"
#include "smart_objects/smart_object.h"
#include "utils/lock.h"

namespace application_manager {

namespace commands {
/**
 * @brief Class is intended to encapsulate RPC as an object
 **/
class CommandRequestImpl : public CommandImpl,
                           public event_engine::EventObserver {
 public:
  enum RequestState {
    kAwaitingResponse = 0,
    kTimedOut,
    kProcessEvent,
  };

  /**
   * @brief CommandRequestImpl class constructor
   *
   * @param message Incoming SmartObject message
   **/
  CommandRequestImpl(const MessageSharedPtr& message,
                     ApplicationManager& application_manager,
                     rpc_service::RPCService& rpc_service,
                     HMICapabilities& hmi_capabilities,
                     policy::PolicyHandlerInterface& policy_handler);

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

  /**
   * @brief Send request to mobile
   * @param function_id Function id
   * @param msg Request to mobile
   * @param use_event - true if request should be subscribed to an event,
   * otherwise false
   **/
  void SendMobileRequest(const mobile_apis::FunctionID::eType& function_id,
                         smart_objects::SmartObjectSPtr msg,
                         bool use_events);

  /**
   * @brief Execute corresponding command by calling the action on reciever
   **/
  void Run() OVERRIDE;

  virtual void on_event(const event_engine::MobileEvent& event);

  /*
   * @brief Function is called by RequestController when request execution time
   * has exceed it's limit
   *
   */
  void HandleTimeOut() FINAL;

  /**
   * @brief Default EvenObserver's pure virtual method implementation
   *
   * @param event The received event
   */
  void HandleOnEvent(const event_engine::Event& event) FINAL;

  /**
   * @brief Default EvenObserver's pure virtual method implementation
   *
   * @param event The received event
   */
  void HandleOnEvent(const event_engine::MobileEvent& event) FINAL;

  /**
   * @brief Function is called by RequestController when request execution time
   * has exceeded its limit
   * @note default value is required as a lot of requests don't use it
   * as they use automatically generated reason
   */
  virtual void OnTimeOut();

  virtual void on_event(const event_engine::Event&);

  void OnUpdateTimeOut() OVERRIDE;

  bool IncrementReferenceCount() const OVERRIDE;

  bool DecrementReferenceCount() const OVERRIDE;

 protected:
  /**
   * @brief Checks message permissions and parameters according to policy table
   * permissions
   * @param source The source of the command (used to determine if a response
   * should be sent on failure)
   * @return true if the RPC is allowed, false otherwise
   */
  bool CheckAllowedParameters(const Command::CommandSource source);

  /**
   * @brief Adds interface to be awaited by SDL request
   * @param interface_id interface from which SDL expects response in given time
   */
  void StartAwaitForInterface(const HmiInterfaces::InterfaceID& interface_id);

  /**
   * @brief Gets interface awaiting state.
   * @param interface_id interface which SDL awaits for response in given time
   * @return true if SDL awaits for response from given interface in
   * interface_id
   */
  bool IsInterfaceAwaited(const HmiInterfaces::InterfaceID& interface_id) const;

  /**
   * @brief Stops SDL awaiting from given HMI interface
   * @param interface_id interface from which SDL no longer awaits
   * for response in given time
   */
  void EndAwaitForInterface(const HmiInterfaces::InterfaceID& interface_id);

  /**
   * @brief Checks if there some not delivered hmi responses exist
   * @return true if pending responses exist, otherwise - false
   */
  bool IsPendingResponseExist() const;

  /**
   * @brief Checks if there some not delivered hmi responses exist
   * @return true if pending responses exist, otherwise - false
   */

  /**
   * @brief Returns current state of request
   * @return current request state. E.g. kAwaitingResponse, kTimedOut,
   * kResponded
   */
  virtual RequestState current_state() const;

  /**
   * @brief Sets current state of request
   * @param request state to set. E.g. kAwaitingResponse, kTimedOut,
   * kResponded
   */
  virtual void set_current_state(const RequestState state);

  /**
   * @brief This set stores all the interfaces which are awaited by SDL to
   * return a response on some request
   */
  std::set<HmiInterfaces::InterfaceID> awaiting_response_interfaces_;

  mutable sync_primitives::Lock awaiting_response_interfaces_lock_;

  /**
   * @brief This lock is used to guarantee thread safe access to request state
   */
  mutable sync_primitives::RecursiveLock state_lock_;

  /**
   * @brief Current state of request to synchronize its life cycle
   */
  RequestState current_state_;

 private:
  DISALLOW_COPY_AND_ASSIGN(CommandRequestImpl);

  /**
   * @brief Changes request state to "kProcessEvent", retain request instance
   * @return false if request is not ready to handle event right now.
   * If request is succesfully retained returns true
   */
  bool StartOnEventHandling();

  /**
   * @brief Changes request state to "kAwaitingResponse", removes request
   * instance retained before
   */
  void FinalizeOnEventHandling();
};

}  // namespace commands
}  // namespace application_manager
#endif  // SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_COMMANDS_COMMAND_REQUEST_IMPL_H_