VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
subsystem.h
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
5 #ifndef _UGCS_VSM_SUBSYSTEM_H_
6 #define _UGCS_VSM_SUBSYSTEM_H_
7 
8 #include <ugcs/vsm/property.h>
9 
10 #include <memory>
11 #include <unordered_map>
12 #include <unordered_set>
13 
14 namespace ugcs {
15 namespace vsm {
16 
17 uint32_t
18 Get_unique_id();
19 
20 typedef std::shared_ptr<proto::Vsm_message> Proto_msg_ptr;
21 
22 class Vsm_command: public std::enable_shared_from_this<Vsm_command>
23 {
25 
26 public:
27  Vsm_command(std::string name, bool in_mission);
28 
29  Property::Ptr
30  Add_parameter(
31  std::string,
32  proto::Field_semantic semantic = proto::FIELD_SEMANTIC_DEFAULT);
33 
34  Property::Ptr
35  Add_parameter(std::string name, Property::Value_type type);
36 
37  // Fill registration message
38  void
39  Register(proto::Register_command* command);
40 
41  // Fill availability message
42  void
43  Set_capabilities(proto::Command_availability* msg);
44 
45  void
46  Set_enabled(bool is_enabled = true);
47 
48  void
49  Set_available(bool is_available = true);
50 
51  int
52  Get_id() {return command_id;}
53 
54  bool
55  Is_capability_state_dirty() {return capability_state_dirty;}
56 
57  // create a list of command param values from proto message
58  Property_list
59  Build_parameter_list(const proto::Device_command &cmd);
60 
61  //Create command message with given arguments
62  void
63  Build_command(proto::Device_command *cmd, Property_list args = Property_list());
64 
65  std::string
66  Get_name()
67  {return name;}
68 
69  bool
70  Is_mission_item()
71  {return in_mission;}
72 
73 private:
74  uint32_t command_id = 0;
75  std::unordered_map<int, Property::Ptr> parameters;
76  std::string name;
77 
78  bool in_mission = false;
79 
80  bool is_available = false;
81  bool is_enabled = false;
82  bool capability_state_dirty = true;
83  friend class Device;
84 };
85 
86 class Subsystem: public std::enable_shared_from_this<Subsystem>
87 {
89 
90 public:
91  Subsystem(proto::Subsystem_type);
92 
94  Subsystem(const Subsystem &) = delete;
95 
96  // Create/replace property value. By default type is derived from value.
97  template<typename Type>
98  Property::Ptr
99  Set_property(
100  const std::string& name,
101  Type value,
102  proto::Field_semantic semantic = proto::FIELD_SEMANTIC_DEFAULT)
103  {
104  auto it = properties.find(name);
105  if (it == properties.end()) {
106  auto f = Property::Create(name, value, semantic);
107  properties.emplace(name, f);
108  return f;
109  } else {
110  it->second->Set_value(value);
111  return it->second;
112  }
113  }
114 
116  Add_telemetry(
117  const std::string& name,
118  proto::Field_semantic sem = proto::FIELD_SEMANTIC_DEFAULT,
119  uint32_t timeout = 0); // timeout in seconds. default: do not specify timeout
120 
122  Add_telemetry(
123  const std::string& name,
124  Property::Value_type type,
125  uint32_t timeout = 0); // timeout in seconds. default: do not specify timeout
126 
127  // Remove previously added telemetry field.
128  // Invalidates the t_field.
129  void
130  Remove_telemetry(Property::Ptr& t_field);
131 
133  Add_command(
134  const std::string& name,
135  bool in_mission);
136 
137  // Populate subsystem registration message
138  void
139  Register(proto::Register_subsystem* msg);
140 
141  proto::Subsystem_type type;
142 
143  std::unordered_map<std::string, Property::Ptr> properties;
144  // All registered telemetry fields.
145  std::vector<Property::Ptr> telemetry_fields;
146  // All commands supported by device
147  std::unordered_map<int, Vsm_command::Ptr> commands;
148 
149  std::vector<Subsystem::Ptr> subsystems;
150 };
151 
152 } /* namespace vsm */
153 } /* namespace ugcs */
154 
155 #endif /* _UGCS_VSM_SUBSYSTEM_H_ */
static Ptr Create(Args &&...args)
Create an instance.
Definition: property.h:18
std::shared_ptr< Property > Ptr
Pointer type.
Definition: property.h:18
Definition: property.h:16
Definition: subsystem.h:86
std::shared_ptr< Vsm_command > Ptr
Pointer type.
Definition: subsystem.h:24
Definition: subsystem.h:22
std::shared_ptr< Subsystem > Ptr
Pointer type.
Definition: subsystem.h:88
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
T value
Stored value (in wire byte order).
Definition: endian.h:376