summaryrefslogtreecommitdiff
path: root/src/components/include/security_manager/security_query.h
blob: cdad1c746c093ef0f59b16a6974fe18dbab1c051 (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
/*
 * Copyright (c) 2014, 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_INCLUDE_SECURITY_MANAGER_SECURITY_QUERY_H_
#define SRC_COMPONENTS_INCLUDE_SECURITY_MANAGER_SECURITY_QUERY_H_

#include <stdint.h>
#include <cstddef>
#include <vector>
#include <string>
#include "utils/shared_ptr.h"

namespace security_manager {
/**
 * \brief SecurityQuery is wrapper for handling Mobile messages
 * as security queries
 */
class SecurityQuery {
 public:
  /**
   * \brief QueryType is 1 byte type of income query
   * Equal RPC Type (Ford Binary Header Definition)
   */
  enum QueryType {
    REQUEST = 0x00,
    RESPONSE = 0x10,
    NOTIFICATION = 0x20,
    INVALID_QUERY_TYPE = 0xFF
  };
  /**
   * \brief QueryId is 3 byte identifier of income query
   * Equal RPC Function ID (Ford Binary Header Definition)
   */
  enum QueryId {
    SEND_HANDSHAKE_DATA = 0x1,
    SEND_INTERNAL_ERROR = 0x2,
    INVALID_QUERY_ID = 0xFFFFFF
  };
  /**
   * \brief QueryHeader is 12 byte header of security query
   * Equal Ford Binary Header Definition
   */
  struct QueryHeader {
    QueryHeader();
    QueryHeader(uint8_t queryType,
                uint32_t queryId,
                uint32_t seqNumber = 0,
                uint32_t jsonSize = 0);
    // TODO(EZamakhov): check bitfield correctness on other endianness platform
    uint32_t query_type : 8;
    uint32_t query_id : 24;  // API function identifier
    uint32_t seq_number;     // request sequential number
    uint32_t json_size;
  };

  /**
   * \brief Constructor
   */
  SecurityQuery();
  /**
   * \brief Constructor with header and connection_key
   * \param connection_key Unique key used by other components as session
   * identifier
   * \param header QueryHeader
   */
  SecurityQuery(const QueryHeader& header, const uint32_t connection_key);
  /**
   * \brief Constructor with header, connection_key and query binary data
   * \param connection_key Unique key used by other components as session
   * identifier
   * \param raw_data pointer to binary data array
   * \param raw_data_size size of binary data array
   * \param header QueryHeader
   */
  SecurityQuery(const QueryHeader& header,
                const uint32_t connection_key,
                const uint8_t* const raw_data,
                const size_t raw_data_size);
  /**
   * \brief Serialize income from Mobile Application data
   * as query with header and binary data or json message
   * \param raw_data pointer to binary data array
   * \param raw_data_size size of binary data array
   * \return \c true on correct parse and \c false on wrong size of data
   */
  bool SerializeQuery(const uint8_t* const raw_data,
                      const size_t raw_data_size);
  /**
   * \brief Deserialize query for sending to Mobile Application
   * \return \c vector of uint8_t data (serialized header data and send_data))
   */
  const std::vector<uint8_t> DeserializeQuery() const;
  /**
   * \brief Set binary data. (No header modification)
   * \param binary_data pointer to binary data array
   * \param bin_data_size size of binary data array
   */
  void set_data(const uint8_t* const binary_data, const size_t bin_data_size);
  /**
   * \brief Set json data. (No header modification)
   * \param json_message string with json error
   */
  void set_json_message(const std::string& json_message);
  /**
   * \brief Set connection key
   * \param connection_key Unique key used by other components as session
   * identifier
   */
  void set_connection_key(const uint32_t connection_key);
  /**
   * \brief Set query header
   * \param header of query
   */
  void set_header(const QueryHeader& header);
  /**
   * \brief Get query header
   * \return header of query
   */
  const QueryHeader& get_header() const;
  /**
   * \brief Get query binary data (without header data)
   * \return const pointer to const binary data
   */
  const uint8_t* get_data() const;
  /**
   * \brief Get query binary data size
   * \return size of binary data
   */
  size_t get_data_size() const;
  /**
   * \brief Get json string data (without header data)
   * \return const pointer to const binary data
   */
  const std::string& get_json_message() const;
  /**
   * \brief Get connection key
   * \return Unique key used by other components as session identifier
   */
  uint32_t get_connection_key() const;

 private:
  /**
   *\brief 12 byte header of security query
   * Equal Ford Binary Header Definition
   */
  QueryHeader header_;
  /**
   *\brief nique key used by other components as session identifier
   */
  uint32_t connection_key_;
  /**
   *\brief Binary data of query (without header info)
   */
  std::vector<uint8_t> data_;
  /**
   *\brief JSON (string) value of query
   */
  std::string json_message_;
};
/**
*\brief SmartPointer wrapper
*/
typedef utils::SharedPtr<SecurityQuery> SecurityQueryPtr;
}  // namespace security_manager
#endif  // SRC_COMPONENTS_INCLUDE_SECURITY_MANAGER_SECURITY_QUERY_H_