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<ugcs::vsm::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  ugcs::vsm::proto::Field_semantic semantic = ugcs::vsm::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(ugcs::vsm::proto::Register_command* command);
40 
41  // Fill availability message
42  void
43  Set_capabilities(ugcs::vsm::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 ugcs::vsm::proto::Device_command &cmd);
60 
61  std::string
62  Get_name()
63  {return name;}
64 
65  bool
66  Is_mission_item()
67  {return in_mission;}
68 
69 private:
70  uint32_t command_id = 0;
71  std::unordered_map<int, Property::Ptr> parameters;
72  std::string name;
73 
74  bool in_mission = false;
75 
76  bool is_available = false;
77  bool is_enabled = false;
78  bool capability_state_dirty = true;
79  friend class Device;
80 };
81 
82 class Subsystem: public std::enable_shared_from_this<Subsystem>
83 {
85 
86 public:
87  Subsystem(proto::Subsystem_type);
88 
90  Subsystem(const Subsystem &) = delete;
91 
92  // Create/replace property value. By default type is derived from value.
93  template<typename Type>
94  Property::Ptr
95  Set_property(
96  const std::string& name,
97  Type value,
98  proto::Field_semantic semantic = proto::FIELD_SEMANTIC_DEFAULT)
99  {
100  auto it = properties.find(name);
101  if (it == properties.end()) {
102  auto f = Property::Create(name, value, semantic);
103  properties.emplace(name, f);
104  return f;
105  } else {
106  it->second->Set_value(value);
107  return it->second;
108  }
109  }
110 
112  Add_telemetry(
113  const std::string& name,
114  ugcs::vsm::proto::Field_semantic sem = ugcs::vsm::proto::FIELD_SEMANTIC_DEFAULT,
115  uint32_t timeout = 0); // timeout in seconds. default: do not specify timeout
116 
118  Add_telemetry(
119  const std::string& name,
120  Property::Value_type type,
121  uint32_t timeout = 0); // timeout in seconds. default: do not specify timeout
122 
123  // Remove previously added telemetry field.
124  // Invalidates the t_field.
125  void
126  Remove_telemetry(Property::Ptr& t_field);
127 
129  Add_command(
130  const std::string& name,
131  bool in_mission);
132 
133  // Populate subsystem registration message
134  void
135  Register(ugcs::vsm::proto::Register_subsystem* msg);
136 
137  proto::Subsystem_type type;
138 
139  std::unordered_map<std::string, Property::Ptr> properties;
140  // All registered telemetry fields.
141  std::vector<Property::Ptr> telemetry_fields;
142  // All commands supported by device
143  std::unordered_map<int, Vsm_command::Ptr> commands;
144 
145  std::vector<Subsystem::Ptr> subsystems;
146 };
147 
148 } /* namespace vsm */
149 } /* namespace ugcs */
150 
151 #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:82
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:84
#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:356