Firmware
Subscription.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2017 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 
39 #pragma once
40 
41 #include <uORB/uORB.h>
42 #include <containers/List.hpp>
43 #include <systemlib/err.h>
44 #include <px4_defines.h>
45 
46 namespace uORB
47 {
48 
54 {
55 public:
65  SubscriptionBase(const struct orb_metadata *meta, unsigned interval = 0, unsigned instance = 0);
66  virtual ~SubscriptionBase();
67 
68  // no copy, assignment, move, move assignment
69  SubscriptionBase(const SubscriptionBase &) = delete;
70  SubscriptionBase &operator=(const SubscriptionBase &) = delete;
72  SubscriptionBase &operator=(SubscriptionBase &&) = delete;
73 
77  bool updated();
78 
83  bool update(void *data);
84 
85  int getHandle() const { return _handle; }
86 
87  const orb_metadata *getMeta() const { return _meta; }
88 
89  unsigned getInstance() const { return _instance; }
90 
91 protected:
92  const struct orb_metadata *_meta;
93 
94  unsigned _instance;
95 
96  int _handle{-1};
97 };
98 
103 
107 class __EXPORT SubscriptionNode : public SubscriptionBase, public ListNode<SubscriptionNode *>
108 {
109 public:
121  SubscriptionNode(const struct orb_metadata *meta, unsigned interval = 0, unsigned instance = 0,
122  List<SubscriptionNode *> *list = nullptr);
123 
124  virtual ~SubscriptionNode() override = default;
125 
130  virtual bool update() = 0;
131 
135  virtual bool forcedUpdate() = 0;
136 
137 };
138 
142 template<class T>
144 {
145 public:
156  Subscription(const struct orb_metadata *meta, unsigned interval = 0, unsigned instance = 0,
157  List<SubscriptionNode *> *list = nullptr):
158  SubscriptionNode(meta, interval, instance, list),
159  _data() // initialize data structure to zero
160  {
161  forcedUpdate();
162  }
163 
164  ~Subscription() override = default;
165 
166  // no copy, assignment, move, move assignment
167  Subscription(const Subscription &) = delete;
168  Subscription &operator=(const Subscription &) = delete;
169  Subscription(Subscription &&) = delete;
170  Subscription &operator=(Subscription &&) = delete;
171 
175  bool update() override
176  {
177  return SubscriptionBase::update((void *)(&_data));
178  }
179 
180  bool forcedUpdate() override
181  {
182  return orb_copy(_meta, _handle, &_data) == PX4_OK;
183  }
184 
185  /*
186  * This function gets the T struct data
187  * */
188  const T &get() const
189  {
190  return _data;
191  }
192 
193 private:
194  T _data;
195 };
196 
197 } // namespace uORB
An intrusive linked list.
Definition: List.hpp:45
bool update(void *data)
Update the struct.
Definition: Subscription.cpp:76
bool forcedUpdate() override
Like update(), but does not check first if there is data available.
Definition: Subscription.hpp:180
bool update() override
Create an update function that uses the embedded struct.
Definition: Subscription.hpp:175
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Definition: uORB.cpp:90
Definition: uORBFastRpcChannel.hpp:44
Generally used magic defines.
The subscription base class as a list node.
Definition: Subscription.hpp:107
Definition: List.hpp:59
Definition: I2C.hpp:51
API for the uORB lightweight object broker.
Base subscription wrapper class, used in list traversal of various subscriptions. ...
Definition: Subscription.hpp:53
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
Object metadata.
Definition: uORB.h:50
Subscription wrapper class.
Definition: Subscription.hpp:143
Subscription(const struct orb_metadata *meta, unsigned interval=0, unsigned instance=0, List< SubscriptionNode *> *list=nullptr)
Constructor.
Definition: Subscription.hpp:156