VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cucs_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
12 #ifndef _UGCS_VSM_CUCS_PROCESSOR_H_
13 #define _UGCS_VSM_CUCS_PROCESSOR_H_
14 
16 #include <ugcs/vsm/device.h>
19 #include <ugcs/vsm/transport_detector.h>
20 #include <ucs_vsm_proto.h>
21 #include <unordered_set>
22 #include <map>
23 
24 namespace ugcs {
25 namespace vsm {
26 
27 // Declare function to get VSM name. Used when registering VSM with server.
28 // It must be defined in VSM sources (typically in main.cpp) via
29 // the helper #define DEFINE_DEFAULT_VSM_NAME located in ugcs/vsm/defs.h
30 const char*
31 Get_vsm_name();
32 
36 
37 public:
42 
44  template <typename... Args>
45  static Ptr
46  Get_instance(Args &&... args)
47  {
48  return singleton.Get_instance(std::forward<Args>(args)...);
49  }
50 
53  void
55 
57  void
58  Unregister_device(uint32_t handle);
59 
60  void
61  Send_ucs_message(uint32_t handle, Proto_msg_ptr message, uint32_t stream_id = 0);
62 
63  // VSM will not communicate with server version below this:
64  constexpr static uint32_t SUPPORTED_UCS_VERSION_MAJOR = 2;
65  constexpr static uint32_t SUPPORTED_UCS_VERSION_MINOR = 14;
66 
67 private:
69  constexpr static std::chrono::seconds WRITE_TIMEOUT = std::chrono::seconds(60);
70 
72  constexpr static std::chrono::seconds REGISTER_PEER_TIMEOUT = std::chrono::seconds(10);
73 
74  // Protobuf message of size above this is considered an attack.
75  constexpr static size_t PROTO_MAX_MESSAGE_LEN = 1000000;
76 
80  Request_worker::Ptr worker;
81 
83  Request_completion_context::Ptr completion_ctx;
84 
85  uint32_t ucs_id_counter;
86 
87  // VSM will drop UCS connection if there is no messages from the server this long.
88  // If specified, VSM will send regular pings to server.
89  std::chrono::seconds keep_alive_timeout = std::chrono::seconds(0);
90 
91  uint32_t
92  Get_next_id() { return ucs_id_counter++; }
93 
94  typedef struct {
95  size_t stream_id;
96  Io_stream::Ref stream;
97  Socket_address::Ptr address;
98  Optional<uint32_t> ucs_id;
99  Operation_waiter read_waiter;
100 
101  // wireformat reader FSM
102  bool reading_header = true;
103  size_t to_read = 1;
104  size_t message_size = 0;
105  int shift = 0;
106 
107  // This is primary connection with this server.
108  // VSM will use this as primary connection.
109  bool primary = false;
110 
111  // Is this connection able to accept vehicle registrations?
112  bool is_compatible = true;
113 
114  // List of device_ids which has successfully registered on this connection.
115  // Used to avoid sending telemetry to connections which are not ready for it.
116  std::unordered_set<uint32_t> registered_devices;
117 
118  // Map request_id -> device_id used to keep track of pending registrations.
119  std::unordered_map<uint32_t, uint32_t> pending_registrations;
120 
121  // Last time we have received something form this server.
122  std::chrono::time_point<std::chrono::steady_clock> last_message_time;
123  } Server_context;
124 
125  typedef struct {
126  Device::Ptr vehicle;
127  // field_id -> field value
128  std::unordered_map<uint32_t, ugcs::vsm::proto::Telemetry_field> telemetry_cache;
129  std::unordered_map<uint32_t, ugcs::vsm::proto::Command_availability> availability_cache;
130 
131  // created on vehicle register and not updated any more.
132  // I.e. for now vehicle in not allowed to modify its
133  // telemetry, commands, or props after registration
134  ugcs::vsm::proto::Vsm_message registration_message;
135  } Vehicle_context;
136 
138  std::unordered_map<
139  uint32_t,
140  Server_context> ucs_connections;
141 
143  std::unordered_map<
144  uint32_t,
145  Vehicle_context> vehicles;
146 
149  Transport_detector::Ptr ucs_connector;
150 
152  bool transport_detector_on_when_diconnected = false;
153 
155 
156  virtual void
157  On_enable() override;
158 
159  virtual void
160  On_disable() override;
161 
162  void
163  Process_on_disable(Request::Ptr);
164 
165  bool
166  On_timer();
167 
169  void
170  On_incoming_connection(std::string, int, Socket_address::Ptr, Io_stream::Ref);
171 
173  void
174  Schedule_next_read(Server_context& sc);
175 
177  void
178  Read_completed(
180  Io_result,
181  size_t stream_id);
182 
184  void
185  Write_completed(
186  Io_result,
187  size_t stream_id);
188 
189  void
190  On_register_vehicle(Request::Ptr, Device::Ptr);
191 
192  void
193  On_unregister_vehicle(Request::Ptr, uint32_t handle);
194 
195  void
196  On_send_ucs_message(Request::Ptr request, uint32_t handle, Proto_msg_ptr message, uint32_t stream_id);
197 
198  void
199  Send_ucs_message(
200  uint32_t stream_id,
201  ugcs::vsm::proto::Vsm_message& message);
202 
203  void
204  Send_ucs_message_ptr(uint32_t stream_id, Proto_msg_ptr message);
205 
206  // Send message to all connected ucs.
207  // Prefers locally connected.
208  void
209  Broadcast_message_to_ucs(ugcs::vsm::proto::Vsm_message& message);
210 
211  void
212  On_ucs_message(
213  uint32_t stream_id,
214  ugcs::vsm::proto::Vsm_message message);
215 
216  void
217  On_ucs_message_vehicle(
218  uint32_t stream_id,
219  ugcs::vsm::proto::Vsm_message message);
220 
221  void
222  Send_vehicle_registrations(
223  Server_context& ctx);
224 
226  Get_device(uint32_t device_id);
227 
228  void
229  Close_ucs_stream(size_t stream_id);
230 
231  void
232  Notify_device_about_ucs_connections(uint32_t device_id);
233 
235  static Singleton<Cucs_processor> singleton;
236 };
237 
238 } /* namespace vsm */
239 } /* namespace ugcs */
240 #endif /* _UGCS_VSM_CUCS_PROCESSOR_H_ */
Request worker.
Reference_guard< Io_stream::Ptr > Ref
Guard object.
Definition: io_stream.h:71
Io_result
Result of I/O operation.
Definition: io_stream.h:37
std::shared_ptr< Cucs_processor > Ptr
Pointer type.
Definition: cucs_processor.h:35
void Unregister_device(uint32_t handle)
Unregistration of a vehicle instance in the processor.
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
std::shared_ptr< Request_worker > Ptr
Pointer type.
Definition: request_worker.h:25
Request execution context.
Definition: request_context.h:24
Socket processor.
Handles interactions with CUCS.
Definition: cucs_processor.h:34
std::shared_ptr< Device > Ptr
Pointer type.
Definition: device.h:56
Generic container for queued requests.
Definition: request_container.h:30
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: cucs_processor.h:46
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Timer > Ptr
Pointer type.
Definition: timer_processor.h:46
void Register_device(Device::Ptr)
Registration of a vehicle instance in the processor.
Cucs_processor()
Default constructor.