Ocean
devices/Device.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_DEVICES_DEVICE_H
9 #define META_OCEAN_DEVICES_DEVICE_H
10 
11 #include "ocean/devices/Devices.h"
12 
13 #include "ocean/base/ObjectRef.h"
14 #include "ocean/base/Timestamp.h"
15 #include "ocean/base/Value.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Devices
21 {
22 
23 /**
24  * This class is the base class for all devices of any type.
25  * @ingroup devices
26  */
27 class OCEAN_DEVICES_EXPORT Device
28 {
29  friend class Factory;
30  friend class Ocean::ObjectRef<Device>;
31 
32  public:
33 
34  /**
35  * Definition of all major device types.
36  */
37  enum MajorType : uint32_t
38  {
39  /// Invalid major type.
40  DEVICE_INVALID = 0u,
41  /// Measurement device.
42  DEVICE_MEASUREMENT = (1u << 0u),
43  /// Sensor device.
44  DEVICE_SENSOR = (1u << 1u) | DEVICE_MEASUREMENT,
45  /// Tracker device.
46  DEVICE_TRACKER = (1u << 2u) | DEVICE_MEASUREMENT
47  };
48 
49  /**
50  * Base definition of a minor type.
51  */
52  enum MinorType : uint32_t
53  {
54  /// Invalid minor type.
55  MINOR_INVALID = 0u
56  };
57 
58  /**
59  * Definition of a class holding the major and minor device type.
60  */
61  class OCEAN_DEVICES_EXPORT DeviceType
62  {
63  public:
64 
65  /**
66  * Creates a new invalid device type.
67  */
68  DeviceType() = default;
69 
70  /**
71  * Creates a new device type with defined major type only.
72  * @param major The major device type
73  */
74  inline explicit DeviceType(const MajorType major);
75 
76  /**
77  * Creates a new device type.
78  * @param major The major device type
79  * @param minor The minor device type
80  */
81  inline DeviceType(const MajorType major, const int minor);
82 
83  /**
84  * Creates a new device type by a given device type and a further minor type.
85  * @param type Device type to be extended
86  * @param minor The minor type extending the already existing minor type
87  */
88  inline DeviceType(const DeviceType& type, const int minor);
89 
90  /**
91  * Returns the major device type.
92  * @return Major device type
93  */
94  inline MajorType majorType() const;
95 
96  /**
97  * Returns the minor device type.
98  * @return Minor device type
99  */
100  inline int minorType() const;
101 
102  /**
103  * Returns whether two device types are equal.
104  * @param type Second device type
105  * @return True, if so
106  */
107  inline bool operator==(const DeviceType& type) const;
108 
109  /**
110  * Returns whether two device types are not equal.
111  * @param type Second device type
112  * @return True, if so
113  */
114  inline bool operator!=(const DeviceType& type) const;
115 
116  /**
117  * Returns whether the right device type is a subset of the left device type.
118  * @param right The right device type
119  * @return True, if so
120  */
121  inline bool operator>=(const DeviceType& right) const;
122 
123  /**
124  * Returns whether this device type holds a valid major and minor type.
125  * @return True, if so
126  */
127  explicit inline operator bool() const;
128 
129  /**
130  * Translates the major and minor devices type from a readable string to a DeviceType object.
131  * @param majorType The major device type to translate
132  * @param minorType The major device type to translate
133  * @return The resulting device type
134  */
135  static DeviceType translateDeviceType(const std::string& majorType, const std::string& minorType);
136 
137  private:
138 
139  /// Major device type.
140  MajorType major_ = DEVICE_INVALID;
141 
142  /// Minor device type.
143  uint32_t minor_ = MINOR_INVALID;
144  };
145 
146  protected:
147 
148  /**
149  * Definition of a subscription id for event callbacks.
150  */
151  typedef unsigned int SubscriptionId;
152 
153  public:
154 
155  /**
156  * Returns the name of this device.
157  * @return Device name
158  */
159  inline const std::string& name() const;
160 
161  /**
162  * Returns the name of the owner library.
163  * @return Library name
164  */
165  virtual const std::string& library() const = 0;
166 
167  /**
168  * Returns whether this device is active.
169  * @return True, if so
170  */
171  virtual bool isStarted() const;
172 
173  /**
174  * Returns whether this device is valid.
175  * @return True, if so
176  */
177  inline bool isValid() const;
178 
179  /**
180  * Returns whether this device can be use exclusively.
181  * @return True, if so
182  */
183  bool isExclusive() const;
184 
185  /**
186  * Starts the device.
187  * @return True, if the device could be started successfully
188  */
189  virtual bool start();
190 
191  /**
192  * Pauses the device.
193  * @return True, if the device could be paused successfully
194  */
195  virtual bool pause();
196 
197  /**
198  * Stops the device.
199  * @return True, if the device could be stopped successfully
200  */
201  virtual bool stop();
202 
203  /**
204  * Sets an abstract parameter of this device.
205  * @param parameter Name of the parameter to set
206  * @param value Abstract value to be set
207  * @return True, if the value has been accepted
208  */
209  virtual bool setParameter(const std::string& parameter, const Value& value);
210 
211  /**
212  * Returns an abstract parameter of this device.
213  * @param parameter Name of the parameter to return the value for
214  * @param value Resulting value
215  * @return True, if succeeded
216  */
217  virtual bool parameter(const std::string& parameter, Value& value);
218 
219  /**
220  * Returns the major and minor type of this device.
221  * @return Device type
222  */
223  inline DeviceType type() const;
224 
225  /**
226  * Translates the major devices type to a readable string.
227  * @param majorType The major device type to translate
228  * @return The readable string, empty if the device type is unknown
229  */
230  static std::string translateMajorType(const MajorType majorType);
231 
232  /**
233  * Translates the major devices type from a readable string to a value.
234  * @param majorType The major device type to translate
235  * @return The translated value
236  */
237  static MajorType translateMajorType(const std::string& majorType);
238 
239  protected:
240 
241  /**
242  * Disabled copy constructor.
243  * @param device Object which would be copied
244  */
245  Device(const Device& device) = delete;
246 
247  /**
248  * Creates a new device by is name.
249  * @param name The name of the device
250  * @param type Major and minor device type of the device
251  */
252  Device(const std::string& name, const DeviceType type);
253 
254  /**
255  * Destructs a device.
256  */
257  virtual ~Device();
258 
259  /**
260  * Disabled copy operator.
261  * @param device Object which would be copied
262  * @return Reference to this object
263  */
264  Device& operator=(const Device& device) = delete;
265 
266  /**
267  * Returns an invalid subscription id.
268  * @return Id of an invalid subscription
269  */
270  static constexpr SubscriptionId invalidSubscriptionId();
271 
272  protected:
273 
274  /// Name of this device.
275  std::string deviceName;
276 
277  /// Major and minor type of this device.
279 
280  /// Flag determining whether this device is valid.
282 
283  /// Device lock.
284  mutable Lock deviceLock;
285 };
286 
288  major_(major),
289  minor_(MINOR_INVALID)
290 {
291  // nothing to do here
292 }
293 
294 inline Device::DeviceType::DeviceType(const MajorType major, const int minor) :
295  major_(major),
296  minor_(minor)
297 {
298  // nothing to do here
299 }
300 
301 inline Device::DeviceType::DeviceType(const DeviceType& type, const int minor) :
302  major_(type.major_),
303  minor_(type.minor_ | minor)
304 {
305  // nothing to do here
306 }
307 
309 {
310  return major_;
311 }
312 
314 {
315  return major_ == type.major_ && minor_ == type.minor_;
316 }
317 
319 {
320  return !(*this == type);
321 }
322 
323 inline bool Device::DeviceType::operator>=(const DeviceType& right) const
324 {
325  static_assert(std::is_same<uint32_t, std::underlying_type<MinorType>::type>::value, "Invalid data type!");
326 
327  return ((major_ & right.majorType()) == right.majorType()) && ((minor_ & uint32_t(right.minorType())) == uint32_t(right.minorType()));
328 }
329 
330 inline Device::DeviceType::operator bool() const
331 {
332  return major_ != DEVICE_INVALID && minor_ != MINOR_INVALID;
333 }
334 
336 {
337  return minor_;
338 }
339 
340 inline const std::string& Device::name() const
341 {
342  return deviceName;
343 }
344 
346 {
347  return deviceType;
348 }
349 
350 inline bool Device::isValid() const
351 {
352  return deviceIsValid;
353 }
354 
356 {
357  return SubscriptionId(-1);
358 }
359 
360 } // namespace Devices
361 
362 } // namespace Ocean
363 
364 #endif // META_OCEAN_DEVICES_DEVICE_H
Definition of a class holding the major and minor device type.
Definition: devices/Device.h:62
uint32_t minor_
Minor device type.
Definition: devices/Device.h:143
bool operator==(const DeviceType &type) const
Returns whether two device types are equal.
Definition: devices/Device.h:313
bool operator!=(const DeviceType &type) const
Returns whether two device types are not equal.
Definition: devices/Device.h:318
DeviceType()=default
Creates a new invalid device type.
int minorType() const
Returns the minor device type.
Definition: devices/Device.h:335
bool operator>=(const DeviceType &right) const
Returns whether the right device type is a subset of the left device type.
Definition: devices/Device.h:323
MajorType majorType() const
Returns the major device type.
Definition: devices/Device.h:308
static DeviceType translateDeviceType(const std::string &majorType, const std::string &minorType)
Translates the major and minor devices type from a readable string to a DeviceType object.
MajorType major_
Major device type.
Definition: devices/Device.h:140
This class is the base class for all devices of any type.
Definition: devices/Device.h:28
std::string deviceName
Name of this device.
Definition: devices/Device.h:275
Device(const std::string &name, const DeviceType type)
Creates a new device by is name.
virtual ~Device()
Destructs a device.
Lock deviceLock
Device lock.
Definition: devices/Device.h:284
virtual bool parameter(const std::string &parameter, Value &value)
Returns an abstract parameter of this device.
bool isValid() const
Returns whether this device is valid.
Definition: devices/Device.h:350
virtual bool isStarted() const
Returns whether this device is active.
Device & operator=(const Device &device)=delete
Disabled copy operator.
virtual bool start()
Starts the device.
DeviceType type() const
Returns the major and minor type of this device.
Definition: devices/Device.h:345
bool deviceIsValid
Flag determining whether this device is valid.
Definition: devices/Device.h:281
MinorType
Base definition of a minor type.
Definition: devices/Device.h:53
@ MINOR_INVALID
Invalid minor type.
Definition: devices/Device.h:55
virtual bool setParameter(const std::string &parameter, const Value &value)
Sets an abstract parameter of this device.
virtual bool pause()
Pauses the device.
virtual bool stop()
Stops the device.
static constexpr SubscriptionId invalidSubscriptionId()
Returns an invalid subscription id.
Definition: devices/Device.h:355
bool isExclusive() const
Returns whether this device can be use exclusively.
static MajorType translateMajorType(const std::string &majorType)
Translates the major devices type from a readable string to a value.
virtual const std::string & library() const =0
Returns the name of the owner library.
MajorType
Definition of all major device types.
Definition: devices/Device.h:38
@ DEVICE_INVALID
Invalid major type.
Definition: devices/Device.h:40
unsigned int SubscriptionId
Definition of a subscription id for event callbacks.
Definition: devices/Device.h:151
static std::string translateMajorType(const MajorType majorType)
Translates the major devices type to a readable string.
Device(const Device &device)=delete
Disabled copy constructor.
const std::string & name() const
Returns the name of this device.
Definition: devices/Device.h:340
DeviceType deviceType
Major and minor type of this device.
Definition: devices/Device.h:278
This class implements a factory able to create instances of devices.
Definition: devices/Factory.h:28
This class implements a recursive lock object.
Definition: Lock.h:31
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
This class implements a type independent value.
Definition: Value.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15