summaryrefslogtreecommitdiff
path: root/src/components/policy/policy_regular/include/policy/policy_types.h
blob: af1d119e7a163d0ab62db20720dc70dd7d656e48 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 Copyright (c) 2013, 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_POLICY_POLICY_EXTERNAL_INCLUDE_POLICY_POLICY_TYPES_H_
#define SRC_COMPONENTS_POLICY_POLICY_EXTERNAL_INCLUDE_POLICY_POLICY_TYPES_H_

#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include "utils/shared_ptr.h"
#include "utils/helpers.h"
#include "transport_manager/common.h"

namespace policy {

// TODO(PV): specify errors
enum PolicyErrorEnum {};

const std::string kDefaultDeviceMacAddress = "00:00:00:00:00:00";
const std::string kDefaultDeviceName = "MyDevice";
const std::string kDefaultDeviceConnectionType = "UNKNOWN";

/**
 * @brief Constants for special ids in application policies section of
 * policy table
 */
const std::string kPreDataConsentId = "pre_DataConsent";
const std::string kDefaultId = "default";
const std::string kDeviceId = "device";

/*
 *@brief Policy Services specifies Users of Updates
 * received from cloud through mobile device
 */
enum PolicyServiceTypes { SERVICE_NONE = 0, IVSU = 0x04, POLICY = 0x07 };

/*
 * @brief Status of policy table update
 */
enum PolicyTableStatus {
  StatusUpToDate = 0,
  StatusUpdatePending,
  StatusUpdateRequired,
  StatusUnknown
};

// Code generator uses String class name, so this typedef was renamed to PTSring
typedef std::string PTString;
typedef std::vector<uint8_t> BinaryMessage;
typedef utils::SharedPtr<BinaryMessage> BinaryMessageSptr;

typedef std::string HMILevel;
typedef std::string Parameter;
typedef std::string RpcName;
typedef std::set<std::string> RPCParams;

typedef std::map<std::string, std::set<policy::HMILevel> > HMIPermissions;
struct ParameterPermissions
    : std::map<std::string, std::set<policy::Parameter> > {
  ParameterPermissions()
      : any_parameter_allowed(false)
      , any_parameter_disallowed_by_user(false)
      , any_parameter_disallowed_by_policy(false) {}
  bool any_parameter_allowed;
  bool any_parameter_disallowed_by_user;
  bool any_parameter_disallowed_by_policy;
};

struct RpcPermissions {
  HMIPermissions hmi_permissions;
  ParameterPermissions parameter_permissions;
};

typedef std::map<RpcName, RpcPermissions> Permissions;

/**
 * @brief Typedef for use with AllowApp request/notification
 */
typedef std::vector<std::string> PermissionsList;

/**
 * @brief Typedef for getting initial application data, e.g. nickname list
 */
typedef std::vector<std::string> StringArray;

enum PermitResult { kRpcAllowed = 0, kRpcDisallowed, kRpcUserDisallowed };

/**
  * @struct Stores result of check:
  * if HMI Level was allowed for RPC to work in
  * and list of parameters allowed for RPC if specified in PT.
  */
struct CheckPermissionResult {
  CheckPermissionResult() : hmi_level_permitted(kRpcDisallowed) {}

  PermitResult hmi_level_permitted;
  RPCParams list_of_allowed_params;
  RPCParams list_of_disallowed_params;
  RPCParams list_of_undefined_params;

  bool HasParameter(const PTString& parameter) {
    const bool is_allowed =
        helpers::in_range(list_of_allowed_params, parameter);
    const bool is_disallowed =
        helpers::in_range(list_of_disallowed_params, parameter);
    const bool is_undefined =
        helpers::in_range(list_of_undefined_params, parameter);

    return is_allowed || is_disallowed || is_undefined;
  }

  bool DisallowedInclude(const RPCParams& parameters) {
    if (parameters.empty()) {
      return false;
    }
    return std::includes(list_of_disallowed_params.begin(),
                         list_of_disallowed_params.end(),
                         parameters.begin(),
                         parameters.end());
  }

  bool IsAnyAllowed(const RPCParams& parameters) {
    if (parameters.empty()) {
      return true;
    }
    return list_of_allowed_params.end() !=
           std::find_first_of(list_of_allowed_params.begin(),
                              list_of_allowed_params.end(),
                              parameters.begin(),
                              parameters.end());
  }
};

/**
  @struct Holds Url string and optional policy app id.
  */
struct EndpointData {
  explicit EndpointData(const std::string& url_string = "")
      : app_id("default") {
    if (false == url_string.empty()) {
      url.push_back(url_string);
    }
  }
  std::vector<std::string> url;
  std::string app_id;
};

typedef std::vector<EndpointData> EndpointUrls;

/**
 * @brief Struct contains device data to be used for dialogs, generation of IDs
 */
struct DeviceParams {
  DeviceParams()
      : device_name(kDefaultDeviceName)
      , device_mac_address(kDefaultDeviceMacAddress)
      , device_connection_type(kDefaultDeviceConnectionType)
      , device_handle(0) {}

  std::string device_name;
  std::string device_mac_address;
  std::string device_connection_type;
  transport_manager::DeviceHandle device_handle;
};

/**
 * @brief User consent for device data usage
 */
enum DeviceConsent {
  kDeviceAllowed = 0,
  kDeviceDisallowed,
  kDeviceHasNoConsent
};

/**
 * @brief Struct contains parameters, which can be received during application
 * registration and should be stored in policy table
 */
struct DeviceInfo {
  DeviceInfo() : max_number_rfcom_ports(0) {}

  std::string hardware;
  std::string firmware_rev;
  std::string os;
  std::string os_ver;
  std::string carrier;
  uint32_t max_number_rfcom_ports;
  std::string connection_type;

  void AdoptDeviceType(const std::string& deviceType) {
    connection_type = "USB_serial_number";
    using namespace helpers;
    static const std::string bluetooth("BLUETOOTH");
    static const std::string wifi("WIFI");
    if (Compare<std::string, EQ, ONE>(deviceType, bluetooth, wifi)) {
      connection_type.assign("BTMAC");
    }
  }
};

/**
 * @brief User consent for functional group
 */
enum GroupConsent { kGroupAllowed = 0, kGroupDisallowed, kGroupUndefined };

/**
 * @brief Contains user permission for RPC functional group with specific name
 * and id from DB
 */
struct FunctionalGroupPermission {
  FunctionalGroupPermission() : group_id(0), state(kGroupUndefined) {}

  bool operator==(const FunctionalGroupPermission& rhs) {
    if (this->group_id == rhs.group_id &&
        this->group_alias == rhs.group_alias &&
        this->group_name == rhs.group_name) {
      return true;
    }
    return false;
  }

  std::string group_alias;
  std::string group_name;
  int32_t group_id;
  GroupConsent state;
};

/**
 * @brief Stores data to be sent to HMI on application permissions change
 */
struct AppPermissions {
  AppPermissions(const std::string& app_id)
      : application_id(app_id)
      , isAppPermissionsRevoked(false)
      , appRevoked(false)
      , appPermissionsConsentNeeded(false)
      , appUnauthorized(false)
      , requestTypeChanged(false) {}

  std::string application_id;
  bool isAppPermissionsRevoked;
  std::vector<policy::FunctionalGroupPermission> appRevokedPermissions;
  bool appRevoked;
  bool appPermissionsConsentNeeded;
  bool appUnauthorized;
  bool isSDLAllowed;
  std::string priority;
  DeviceParams deviceInfo;
  bool requestTypeChanged;
  std::vector<std::string> requestType;
};

/**
 * @brief Contains parameters for user-defined consent for appication
 * functional groups on given device
 */
struct PermissionConsent {
  std::string device_id;
  std::string policy_app_id;
  std::vector<FunctionalGroupPermission> group_permissions;
  std::string consent_source;
};

/**
 * @brief Contain data for GetUserFriendyMessage response
 */
struct UserFriendlyMessage {
  std::string message_code;
  std::string tts;
  std::string label;
  std::string line1;
  std::string line2;
  std::string text_body;
};

/**
 * @brief Types of functional groups in policy table
 */
enum GroupType {
  kTypeDefault = 0,   // groups assigned to 'default' permissions section
  kTypeAllowed,       // groups allowed by user for specific application
  kTypeDisallowed,    // groups disallowed by user for specific application
  kTypeUnconsented,   // groups disallowed by default but consent may be changed
                      // by user
  kTypePreconsented,  // groups allowed for specific application without
                      // user consent by default (could be changed by user)
  kTypeGeneral,       // groups assigned to specific application
  kTypePreDataConsented,  // groups assigned to 'pre_DataConsent' permissions
                          // section
  kTypeDevice             // groups assigned to 'device' permissions section
};

/**
 * @brief Array of functional group id from DB
 */
typedef std::vector<int32_t> FunctionalGroupIDs;

/**
 * @brief Array of functional group ids sorted by types
 */
typedef std::map<GroupType, FunctionalGroupIDs> FunctionalIdType;

/**
 * @brief Array of functional group ids binded to user_consent_prompt (e.g.
 * VehicleData) and group name (e.g. VehicleData-4)
 */
typedef std::map<uint32_t, std::pair<std::string, std::string> >
    FunctionalGroupNames;

/**
 * @brief Array of device ids, which are an identifiers in policy table
 */
typedef std::vector<std::string> DeviceIds;

/**
 * @brief Counters that calculated on receiving of succesful update
 */
enum Counters { KILOMETERS, DAYS_AFTER_EPOCH };

/**
 * @struct Vehicle information
 */
struct VehicleInfo {
  std::string vehicle_make;
  std::string vehicle_model;
  std::string vehicle_year;
};

/**
 * @brief The MetaInfo information
 */
struct MetaInfo {
  std::string ccpu_version;
  std::string wers_country_code;
  std::string language;
};

/**
 * @brief The index of the application, the index of its URL
 * and the policy application id from the Endpoints vector
 * that will be sent on the next OnSystemRequest retry sequence
 */
struct RetrySequenceURL {
  uint32_t app_idx_;
  uint32_t url_idx_;
  std::string policy_app_id_;
  RetrySequenceURL(uint32_t app, uint32_t url, const std::string& app_id)
      : app_idx_(app), url_idx_(url), policy_app_id_(app_id) {}
  RetrySequenceURL() : app_idx_(0), url_idx_(0) {}
};

/**
 * @brief Index of the application, index of its URL
 * from the Endpoints vector
 */
typedef std::pair<uint32_t, uint32_t> AppIdURL;

/**
 * @brief Represents ExternalConsent entity status received from the system
 */
enum EntityStatus { kStatusOn, kStatusOff };

/**
 * @brief The ExternalConsentStatusItem struct represents external user consent
 * settings item
 */
struct ExternalConsentStatusItem {
  ExternalConsentStatusItem(const uint32_t type,
                            const uint32_t id,
                            const EntityStatus status)
      : entity_type_(type), entity_id_(id), status_(status) {}

  bool operator==(const ExternalConsentStatusItem& rhs) const {
    return (entity_type_ == rhs.entity_type_) && (entity_id_ == rhs.entity_id_);
  }

  bool operator<(const ExternalConsentStatusItem& rhs) const {
    return (entity_type_ < rhs.entity_type_) || (entity_id_ < rhs.entity_id_);
  }

  const uint32_t entity_type_;
  const uint32_t entity_id_;
  const EntityStatus status_;
};

struct ExternalConsentStatusItemSorter {
  bool operator()(const ExternalConsentStatusItem& lhs,
                  const ExternalConsentStatusItem& rhs) const {
    return (lhs.entity_type_ < rhs.entity_type_) ||
           (lhs.entity_id_ < rhs.entity_id_);
  }
};

/**
 * @brief Customer connectivity settings status
 */
typedef std::set<ExternalConsentStatusItem, ExternalConsentStatusItemSorter>
    ExternalConsentStatus;

/**
 * @brief GroupsByExternalConsentStatus represents list of group names, which
 * has mapped ExternalConsent item (entity type + entity id) in their
 * disallowed_by_external_consent_ containers. Boolean value represents
 * whether ExternalConsent item has been found in
 * disallowed_by_external_consent_ON or in disallowed_by_external_consent_OFF
 * container
 */
typedef std::map<ExternalConsentStatusItem,
                 std::vector<std::pair<std::string, bool> > >
    GroupsByExternalConsentStatus;

/**
 * @brief GroupsNames represents groups names from policy table -> functional
 * groupings groups container
 */
typedef std::set<std::string> GroupsNames;

typedef std::string ApplicationId;
typedef std::string DeviceId;

/**
 * @brief Link of device to application
 */
typedef std::pair<policy::DeviceId, policy::ApplicationId> Link;

/**
 * @brief Collection of links
 */
typedef std::set<Link> ApplicationsLinks;

/**
 * @brief Represents possible result codes for policy table update check
 */
enum PermissionsCheckResult {
  RESULT_NO_CHANGES,
  RESULT_APP_REVOKED,
  RESULT_NICKNAME_MISMATCH,
  RESULT_PERMISSIONS_REVOKED,
  RESULT_CONSENT_NEEDED,
  RESULT_CONSENT_NOT_REQIURED,
  RESULT_PERMISSIONS_REVOKED_AND_CONSENT_NEEDED,
  RESULT_REQUEST_TYPE_CHANGED
};

/**
 * @brief Per application collection of results done by checking policy table
 * update
 */
typedef std::set<std::pair<std::string, PermissionsCheckResult> >
    CheckAppPolicyResults;

/**
 * @brief Index of the application, index of its URL
 * from the Endpoints vector
 */
typedef std::pair<uint32_t, uint32_t> AppIdURL;

}  //  namespace policy

#endif  // SRC_COMPONENTS_POLICY_POLICY_EXTERNAL_INCLUDE_POLICY_POLICY_TYPES_H_