PX4 Firmware
PX4 Autopilot Software http://px4.io
SubscriptionCallback.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 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 SubscriptionCallback.hpp
36  *
37  */
38 
39 #pragma once
40 
42 #include <containers/List.hpp>
43 #include <px4_platform_common/px4_work_queue/WorkItem.hpp>
44 
45 namespace uORB
46 {
47 
48 // Subscription wrapper class with callbacks on new publications
49 class SubscriptionCallback : public SubscriptionInterval, public ListNode<SubscriptionCallback *>
50 {
51 public:
52  /**
53  * Constructor
54  *
55  * @param meta The uORB metadata (usually from the ORB_ID() macro) for the topic.
56  * @param interval_ms The requested maximum update interval in milliseconds.
57  * @param instance The instance for multi sub.
58  */
59  SubscriptionCallback(const orb_metadata *meta, uint8_t interval_ms = 0, uint8_t instance = 0) :
60  SubscriptionInterval(meta, interval_ms, instance)
61  {
62  }
63 
65  {
67  };
68 
70  {
71  bool ret = false;
72 
74  // registered
75  ret = true;
76 
77  } else {
78  // force topic creation by subscribing with old API
80 
81  // try to register callback again
82  if (_subscription.subscribe()) {
84  ret = true;
85  }
86  }
87 
88  orb_unsubscribe(fd);
89  }
90 
91 
92  return ret;
93  }
94 
96  {
97  if (_subscription.get_node()) {
99  }
100  }
101 
102  virtual void call() = 0;
103 
104 };
105 
106 // Subscription with callback that schedules a WorkItem
108 {
109 public:
110  /**
111  * Constructor
112  *
113  * @param work_item The WorkItem that will be scheduled immediately on new publications.
114  * @param meta The uORB metadata (usually from the ORB_ID() macro) for the topic.
115  * @param instance The instance for multi sub.
116  */
117  SubscriptionCallbackWorkItem(px4::WorkItem *work_item, const orb_metadata *meta, uint8_t instance = 0) :
118  SubscriptionCallback(meta, 0, instance), // interval 0
119  _work_item(work_item)
120  {
121  }
122 
123  virtual ~SubscriptionCallbackWorkItem() = default;
124 
125  void call() override
126  {
127  // schedule immediately if no interval, otherwise check time elapsed
129  _work_item->ScheduleNow();
130  }
131  }
132 
133 private:
134  px4::WorkItem *_work_item;
135 };
136 
137 } // namespace uORB
void unregister_callback(SubscriptionCallback *callback_sub)
SubscriptionCallback(const orb_metadata *meta, uint8_t interval_ms=0, uint8_t instance=0)
Constructor.
uint8_t get_instance() const
An intrusive linked list.
LidarLite * instance
Definition: ll40ls.cpp:65
SubscriptionCallbackWorkItem(px4::WorkItem *work_item, const orb_metadata *meta, uint8_t instance=0)
Constructor.
int orb_unsubscribe(int handle)
Definition: uORB.cpp:85
int fd
Definition: dataman.cpp:146
DeviceNode * get_node()
Object metadata.
Definition: uORB.h:50
orb_id_t get_topic() const
int orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance)
Definition: uORB.cpp:80
bool register_callback(SubscriptionCallback *callback_sub)
__EXPORT hrt_abstime hrt_elapsed_time_atomic(const volatile hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.