PX4 Firmware
PX4 Autopilot Software http://px4.io
Subscription.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file Subscription.hpp
36  *
37  */
38 
39 #pragma once
40 
41 #include <uORB/uORB.h>
42 #include <px4_platform_common/defines.h>
43 
44 #include "uORBDeviceNode.hpp"
45 #include "uORBManager.hpp"
46 #include "uORBUtils.hpp"
47 
48 namespace uORB
49 {
50 
51 class SubscriptionCallback;
52 
53 // Base subscription wrapper class
55 {
56 public:
57 
58  /**
59  * Constructor
60  *
61  * @param meta The uORB metadata (usually from the ORB_ID() macro) for the topic.
62  * @param instance The instance for multi sub.
63  */
64  Subscription(const orb_metadata *meta, uint8_t instance = 0) : _meta(meta), _instance(instance)
65  {
66  subscribe();
67  }
68 
70  {
71  unsubscribe();
72  }
73 
74  bool subscribe();
75  void unsubscribe();
76 
77  bool valid() const { return _node != nullptr; }
78  bool advertised()
79  {
80  if (valid()) {
81  return _node->is_advertised();
82  }
83 
84  // try to initialize
85  if (init()) {
86  // check again if valid
87  if (valid()) {
88  return _node->is_advertised();
89  }
90  }
91 
92  return false;
93  }
94 
95  /**
96  * Check if there is a new update.
97  * */
98  bool updated() { return advertised() ? (_node->published_message_count() != _last_generation) : false; }
99 
100  /**
101  * Update the struct
102  * @param data The uORB message struct we are updating.
103  */
104  bool update(void *dst) { return updated() ? copy(dst) : false; }
105 
106  /**
107  * Check if subscription updated based on timestamp.
108  *
109  * @return true only if topic was updated based on a timestamp and
110  * copied to buffer successfully.
111  * If topic was not updated since last check it will return false but
112  * still copy the data.
113  * If no data available data buffer will be filled with zeros.
114  */
115  bool update(uint64_t *time, void *dst);
116 
117  /**
118  * Copy the struct
119  * @param data The uORB message struct we are updating.
120  */
121  bool copy(void *dst) { return advertised() ? _node->copy(dst, _last_generation) : false; }
122 
123  uint8_t get_instance() const { return _instance; }
124  orb_id_t get_topic() const { return _meta; }
125 
126 protected:
127 
128  friend class SubscriptionCallback;
129 
130  DeviceNode *get_node() { return _node; }
131 
132  bool init();
133 
134  DeviceNode *_node{nullptr};
135  const orb_metadata *_meta{nullptr};
136 
137  /**
138  * Subscription's latest data generation.
139  * Also used to track (and rate limit) subscription
140  * attempts if the topic has not yet been published.
141  */
142  unsigned _last_generation{0};
143  uint8_t _instance{0};
144 };
145 
146 // Subscription wrapper class with data
147 template<class T>
149 {
150 public:
151  /**
152  * Constructor
153  *
154  * @param meta The uORB metadata (usually from the ORB_ID() macro) for the topic.
155  * @param instance The instance for multi sub.
156  */
157  SubscriptionData(const orb_metadata *meta, uint8_t instance = 0) :
158  Subscription(meta, instance)
159  {
160  copy(&_data);
161  }
162 
163  ~SubscriptionData() = default;
164 
165  // no copy, assignment, move, move assignment
166  SubscriptionData(const SubscriptionData &) = delete;
167  SubscriptionData &operator=(const SubscriptionData &) = delete;
168  SubscriptionData(SubscriptionData &&) = delete;
169  SubscriptionData &operator=(SubscriptionData &&) = delete;
170 
171  // update the embedded struct.
172  bool update() { return Subscription::update((void *)(&_data)); }
173 
174  const T &get() const { return _data; }
175 
176 private:
177 
178  T _data{};
179 };
180 
181 } // namespace uORB
API for the uORB lightweight object broker.
SubscriptionData(const orb_metadata *meta, uint8_t instance=0)
Constructor.
uint8_t get_instance() const
LidarLite * instance
Definition: ll40ls.cpp:65
const orb_metadata * _meta
bool valid() const
unsigned published_message_count() const
Per-object device instance.
bool updated()
Check if there is a new update.
bool is_advertised() const
Return true if this topic has been advertised.
unsigned _last_generation
Subscription&#39;s latest data generation.
DeviceNode * get_node()
Object metadata.
Definition: uORB.h:50
orb_id_t get_topic() const
bool copy(void *dst, unsigned &generation)
Copies data and the corresponding generation from a node to the buffer provided.
Subscription(const orb_metadata *meta, uint8_t instance=0)
Constructor.
bool update(void *dst)
Update the struct.
static struct mpu9x50_data _data
IMU measurement data.
bool copy(void *dst)
Copy the struct.
DeviceNode * _node