summaryrefslogtreecommitdiff
path: root/src/components/application_manager/rpc_plugins/rc_rpc_plugin/include/rc_rpc_plugin/rc_app_extension.h
blob: 8b2162c9ab3e3b6e0badcac9771503c0276af38c (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
/*
 * Copyright (c) 2020, 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_RPC_PLUGINS_RC_RPC_PLUGIN_INCLUDE_RC_RPC_PLUGIN_RC_APP_EXTENSION_H_
#define SRC_COMPONENTS_APPLICATION_MANAGER_RPC_PLUGINS_RC_RPC_PLUGIN_INCLUDE_RC_RPC_PLUGIN_RC_APP_EXTENSION_H_

#include <memory>
#include <set>
#include <string>

#include "application_manager/app_extension.h"
#include "application_manager/application.h"
#include "utils/macro.h"

namespace rc_rpc_plugin {

typedef std::pair<std::string, std::string> ModuleUid;

struct Grid {
  int32_t col_;
  int32_t row_;
  int32_t level_;
  int32_t colspan_;
  int32_t rowspan_;
  int32_t levelspan_;

  Grid()
      : col_(0), row_(0), level_(0), colspan_(0), rowspan_(0), levelspan_(0) {}

  Grid(int32_t col,
       int32_t row,
       int32_t level,
       int32_t colspan,
       int32_t rowspan,
       int32_t levelspan)
      : col_(col)
      , row_(row)
      , level_(level)
      , colspan_(colspan)
      , rowspan_(rowspan)
      , levelspan_(levelspan) {}

  Grid& operator=(const Grid& grid) {
    col_ = grid.col_;
    row_ = grid.row_;
    level_ = grid.level_;
    colspan_ = grid.colspan_;
    rowspan_ = grid.rowspan_;
    levelspan_ = grid.levelspan_;

    return *this;
  }

  bool operator==(const Grid& grid) const {
    return col_ == grid.col_ && row_ == grid.row_ && level_ == grid.level_ &&
           colspan_ == grid.colspan_ && rowspan_ == grid.rowspan_ &&
           levelspan_ == grid.levelspan_;
  }

  /**
   * @brief LevelIntersectionExists checks if the grids have an
   * intersection by levels.
   * @param grid with which to check intersection
   * @return true if intersection exists, otherwise - false
   */
  bool LevelIntersectionExists(const Grid& grid) const {
    const int32_t top_level = (level_ + levelspan_) - 1;
    const int32_t grid_top_level = (grid.level_ + grid.levelspan_) - 1;

    const int32_t min_level = std::max(level_, grid.level_);
    const int32_t max_level = std::min(top_level, grid_top_level);
    if ((max_level - min_level) < 0) {
      return false;
    }
    return true;
  }

  /**
   * @brief IntersectionExists checks if the grids have an
   * intersection. Grid can be represented by its bottom-left
   * and top-right coordinates like a rectangle. First, the
   * coordinates of the intersection are calculated, then checked
   * that the rectangle which is formed by the intersections of two
   * grids has non-zero width and height.
   * @param grid with which to check intersection
   * @return true if intersection exists, otherwise - false
   */
  bool IntersectionExists(const Grid& grid) const {
    if (!LevelIntersectionExists(grid)) {
      return false;
    }

    const int32_t right_top_col = (col_ + colspan_) - 1;
    const int32_t right_top_row = (row_ + rowspan_) - 1;
    const int32_t grid_right_top_col = (grid.col_ + grid.colspan_) - 1;
    const int32_t grid_right_top_row = (grid.row_ + grid.rowspan_) - 1;

    const int32_t left = std::max(col_, grid.col_);
    const int32_t bottom = std::max(row_, grid.row_);
    const int32_t right = std::min(right_top_col, grid_right_top_col);
    const int32_t top = std::min(right_top_row, grid_right_top_row);

    const int32_t width = right - left;
    const int32_t height = top - bottom;

    if ((width < 0) || (height < 0)) {
      return false;
    }
    return true;
  }
};

class RCRPCPlugin;

class RCAppExtension : public application_manager::AppExtension {
 public:
  explicit RCAppExtension(application_manager::AppExtensionUID uid,
                          RCRPCPlugin& plugin,
                          application_manager::Application& application);
  ~RCAppExtension();

  /**
   * @brief Subscribe to OnInteriorVehicleDataNotification
   * @param module interior data specification(zone, data type)
   */
  void SubscribeToInteriorVehicleData(const ModuleUid& module);

  /**
   * @brief Unsubscribe from OnInteriorVehicleDataNotification
   * @param module interior data specification(zone, data type)
   */
  void UnsubscribeFromInteriorVehicleData(const ModuleUid& module);

  void UnsubscribeFromInteriorVehicleDataOfType(const std::string& module_type);

  /**
   * @brief UnsubscribeFromInteriorVehicleData removes all subscriptions for
   * interior data
   */
  void UnsubscribeFromInteriorVehicleData();

  /**
   * @brief Check if application subscribed to OnInteriorVehicleDataNotification
   * @param module interior data specification(zone, data type)
   */
  bool IsSubscribedToInteriorVehicleData(const ModuleUid& module);

  bool IsSubscribedToInteriorVehicleDataOfType(const std::string& module_type);

  /**
   * @brief get list of subscriptions of application
   * @return list of subscriptions of application
   */
  std::set<ModuleUid> InteriorVehicleDataSubscriptions() const;

  /**
   * @brief AddPendingSubscription adds pending subscription
   * @param module interior data specification(zone, data type)
   * @return true in case of pending subscription is successful added, otherwise
   * false
   */
  bool AddPendingSubscription(const ModuleUid& module);

  /**
   * @brief RemovePendingSubscription removes some particular pending
   * subscription
   * @param module interior data specification(zone, data type)
   */
  void RemovePendingSubscription(const ModuleUid& module);

  /**
   * @brief RemovePendingSubscriptions removes all pending subscriptions
   */
  void RemovePendingSubscriptions();

  /**
   * @brief PendingSubscriptions list of preliminary subscriptoins
   * That will be moved to subscriptions as soon as HMI will respond with
   * success.
   * Used for resumption to keep list of preliminary subscriptions and wait for
   * HMI response
   * @return list of preliminary subscriptions
   */
  std::set<ModuleUid> PendingSubscriptions();

  /**
   * @brief GetUserLocation
   * @return grid of user location
   */
  Grid GetUserLocation() const;

  /**
   * @brief SetUserLocation sets user location
   * from object to the grid
   * @param user_location smart object for user_location
   */
  void SetUserLocation(
      const ns_smart_device_link::ns_smart_objects::SmartObject& user_location);

  /**
   * @brief SetUserLocation sets user location from grid
   * @param grid grid of user_location
   */
  void SetUserLocation(const Grid& grid);

 private:
  /**
   * @brief Checks if the application's pointer is valid and update the
   * application's hash in this case
   */
  void UpdateHash();

  std::set<ModuleUid> subscribed_interior_vehicle_data_;

  std::set<ModuleUid> pending_subscriptions_;

  Grid user_location_;

  RCRPCPlugin& plugin_;

  /**
   * ApplicationSharedPtr needed for updating application's hash
   */
  application_manager::Application& application_;

  // AppExtension interface
 public:
  void SaveResumptionData(smart_objects::SmartObject& resumption_data) OVERRIDE;

  void ProcessResumption(const smart_objects::SmartObject& saved_app) OVERRIDE;

  void RevertResumption(
      const smart_objects::SmartObject& resumption_data) OVERRIDE;
};

typedef std::shared_ptr<RCAppExtension> RCAppExtensionPtr;

}  //  namespace rc_rpc_plugin

#endif  // SRC_COMPONENTS_APPLICATION_MANAGER_RPC_PLUGINS_RC_RPC_PLUGIN_INCLUDE_RC_RPC_PLUGIN_RC_APP_EXTENSION_H_