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  uint32_t
88  Get_next_id() { return ucs_id_counter++; }
89 
90  typedef struct {
91  size_t stream_id;
92  Io_stream::Ref stream;
93  Socket_address::Ptr address;
94  Optional<uint32_t> ucs_id;
95  Operation_waiter read_waiter;
96 
97  // wireformat reader FSM
98  bool reading_header = true;
99  size_t to_read = 1;
100  size_t message_size = 0;
101  int shift = 0;
102 
103  // This is primary connection with this server.
104  // VSM will use this as primary connection.
105  bool primary = false;
106 
107  // Is this connection able to accept vehicle registrations?
108  bool is_compatible = true;
109 
110  // List of device_ids which has successfully registered on this connection.
111  // Used to avoid sending telemetry to connections which are not ready for it.
112  std::unordered_set<uint32_t> registered_devices;
113 
114  // Map request_id -> device_id used to keep track of pending registrations.
115  std::unordered_map<uint32_t, uint32_t> pending_registrations;
116 
117  // Last time we have received something form this server.
118  std::chrono::time_point<std::chrono::steady_clock> last_message_time;
119  } Server_context;
120 
121  typedef struct {
122  Device::Ptr vehicle;
123  // field_id -> field value
124  std::unordered_map<uint32_t, ugcs::vsm::proto::Telemetry_field> telemetry_cache;
125  std::unordered_map<uint32_t, ugcs::vsm::proto::Command_availability> availability_cache;
126 
127  // created on vehicle register and not updated any more.
128  // I.e. for now vehicle in not allowed to modify its
129  // telemetry, commands, or props after registration
130  ugcs::vsm::proto::Vsm_message registration_message;
131  } Vehicle_context;
132 
134  std::unordered_map<
135  uint32_t,
136  Server_context> ucs_connections;
137 
139  std::unordered_map<
140  uint32_t,
141  Vehicle_context> vehicles;
142 
145  Transport_detector::Ptr ucs_connector;
146 
148  bool transport_detector_on_when_diconnected = false;
149 
151 
152  virtual void
153  On_enable() override;
154 
155  virtual void
156  On_disable() override;
157 
158  void
159  Process_on_disable(Request::Ptr);
160 
161  bool
162  On_timer();
163 
165  void
166  On_incoming_connection(std::string, int, Socket_address::Ptr, Io_stream::Ref);
167 
169  void
170  Schedule_next_read(Server_context& sc);
171 
173  void
174  Read_completed(
176  Io_result,
177  size_t stream_id);
178 
180  void
181  Write_completed(
182  Io_result,
183  size_t stream_id);
184 
185  void
186  On_register_vehicle(Request::Ptr, Device::Ptr);
187 
188  void
189  On_unregister_vehicle(Request::Ptr, uint32_t handle);
190 
191  void
192  On_send_ucs_message(Request::Ptr request, uint32_t handle, Proto_msg_ptr message, uint32_t stream_id);
193 
194  void
195  Send_ucs_message(
196  uint32_t stream_id,
197  ugcs::vsm::proto::Vsm_message& message);
198 
199  void
200  Send_ucs_message_ptr(uint32_t stream_id, Proto_msg_ptr message);
201 
202  // Send message to all connected ucs.
203  // Prefers locally connected.
204  void
205  Broadcast_message_to_ucs(ugcs::vsm::proto::Vsm_message& message);
206 
207  void
208  On_ucs_message(
209  uint32_t stream_id,
210  ugcs::vsm::proto::Vsm_message message);
211 
212  void
213  On_ucs_message_vehicle(
214  uint32_t stream_id,
215  ugcs::vsm::proto::Vsm_message message);
216 
217  void
218  Send_vehicle_registrations(
219  Server_context& ctx);
220 
222  Get_device(uint32_t device_id);
223 
224  void
225  Close_ucs_stream(size_t stream_id);
226 
228  static Singleton<Cucs_processor> singleton;
229 };
230 
231 } /* namespace vsm */
232 } /* namespace ugcs */
233 #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:46
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.