VSM C++ SDK
Vehicle Specific Modules SDK
cucs_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
12 #ifndef CUCS_PROCESSOR_H_
13 #define CUCS_PROCESSOR_H_
14 
16 #include <ugcs/vsm/device.h>
20 #include <ucs_vsm_proto.h>
21 #include <unordered_set>
22 #include <map>
23 
24 namespace ugcs {
25 namespace vsm {
26 
30 public:
35 
37  template <typename... Args>
38  static Ptr
39  Get_instance(Args &&... args)
40  {
41  return singleton.Get_instance(std::forward<Args>(args)...);
42  }
43 
45  void
47 
49  void
50  Unregister_device(uint32_t handle);
51 
52  void
53  Send_ucs_message(uint32_t handle, Proto_msg_ptr message);
54 
55 private:
56 
58  constexpr static std::chrono::seconds WRITE_TIMEOUT = std::chrono::seconds(60);
59 
60  // Protobuf message of size above this is considered an attack.
61  constexpr static size_t PROTO_MAX_MESSAGE_LEN = 1000000;
62 
66  Request_worker::Ptr worker;
67 
69  Request_completion_context::Ptr completion_ctx;
70 
71  uint32_t ucs_id_counter;
72 
73  uint32_t
74  Get_next_id() { return ucs_id_counter++; };
75 
76  typedef struct {
77  size_t stream_id;
79  Optional<uint32_t> ucs_id;
80  Operation_waiter read_waiter;
81 
82  // wireformat reader FSM
83  bool reading_header = true;
84  size_t to_read = 1;
85  size_t message_size = 0;
86  int shift = 0;
87 
88  // This is primary connection with this server.
89  // VSM will use this as primary connection.
90  bool primary = false;
91  } Server_context;
92 
93  typedef struct {
94  Device::Ptr vehicle;
95  // field_id -> field value
96  std::unordered_map<uint32_t, ugcs::vsm::proto::Telemetry_field> telemetry_cache;
97  std::unordered_map<uint32_t, ugcs::vsm::proto::Command_availability> availability_cache;
98 
99  // created on vehicle register and not updated any more.
100  // I.e. for now vehicle in not allowed to modify its
101  // telemetry, commands, or props after registration
102  ugcs::vsm::proto::Vsm_message registration_message;
103  } Vehicle_context;
104 
106  std::unordered_map<
107  uint32_t,
108  Server_context> ucs_connections;
109 
111  std::unordered_map<
112  uint32_t,
113  Vehicle_context> vehicles;
114 
117 
119  Operation_waiter cucs_listener_op;
120 
122  Operation_waiter accept_op;
123 
125  bool transport_detector_on_when_diconnected = false;
126 
127  virtual void
128  On_enable() override;
129 
130  virtual void
131  On_disable() override;
132 
133  void
134  Process_on_disable(Request::Ptr);
135 
137  void
138  On_listening_started(Socket_processor::Socket_listener::Ref listener,
139  Io_result result);
140 
142  void
143  On_incoming_connection(Socket_processor::Stream::Ref stream, Io_result result);
144 
146  void
147  Schedule_next_read(
148  Server_context& sc);
149 
151  void
152  Read_completed(
154  Io_result,
155  size_t stream_id);
156 
158  void
159  Write_completed(
160  Io_result,
161  size_t stream_id);
162 
163  void
164  Start_listening();
165 
166  void
167  Accept_next_connection();
168 
169  void
170  On_register_vehicle(Request::Ptr, Device::Ptr);
171 
172  void
173  On_unregister_vehicle(Request::Ptr, uint32_t handle);
174 
175  void
176  On_send_ucs_message(Request::Ptr request, uint32_t handle, Proto_msg_ptr message);
177 
178  void
179  Send_ucs_message(
180  uint32_t stream_id,
181  ugcs::vsm::proto::Vsm_message& message);
182 
183  void
184  Send_ucs_message_ptr(uint32_t stream_id, Proto_msg_ptr message);
185 
186  // Send message to all connected ucs.
187  // Prefers locally connected.
188  void
189  Broadcast_message_to_ucs(ugcs::vsm::proto::Vsm_message& message);
190 
191  void
192  On_ucs_message(
193  uint32_t stream_id,
194  ugcs::vsm::proto::Vsm_message message);
195 
196  void
197  On_ucs_message_vehicle(
198  uint32_t stream_id,
199  ugcs::vsm::proto::Vsm_message message);
200 
201  void
202  Send_vehicle_registrations(
203  const Server_context& ctx);
204 
206  Get_device(uint32_t device_id);
207 
208  void
209  Close_ucs_stream(size_t stream_id);
210 
212  static Singleton<Cucs_processor> singleton;
213 };
214 
215 } /* namespace vsm */
216 } /* namespace ugcs */
217 #endif /* CUCS_PROCESSOR_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Request worker.
Io_result
Result of I/O operation.
Definition: io_stream.h:25
std::shared_ptr< Cucs_processor > Ptr
Pointer type.
Definition: cucs_processor.h:29
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:28
std::shared_ptr< Device > Ptr
Pointer type.
Definition: device.h:105
Generic container for queued requests.
Definition: request_container.h:31
static Ptr Get_instance(Args &&...args)
Get global or create new processor instance.
Definition: cucs_processor.h:39
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
void Register_device(Device::Ptr)
Registration of a vehicle instance in the processor.
Helper class for implementing singletons.
Definition: singleton.h:69
Cucs_processor()
Default constructor.
Class for synchronizing with request execution.
Definition: operation_waiter.h:24