PX4 Firmware
PX4 Autopilot Software http://px4.io
uORB.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012-2015 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 #ifndef _UORB_UORB_H
35 #define _UORB_UORB_H
36 
37 /**
38  * @file uORB.h
39  * API for the uORB lightweight object broker.
40  */
41 
42 #include <sys/types.h>
43 #include <stdint.h>
44 #include <stdbool.h>
45 
46 
47 /**
48  * Object metadata.
49  */
50 struct orb_metadata {
51  const char *o_name; /**< unique object name */
52  const uint16_t o_size; /**< object size */
53  const uint16_t o_size_no_padding; /**< object size w/o padding at the end (for logger) */
54  const char *o_fields; /**< semicolon separated list of fields (with type) */
55 };
56 
57 typedef const struct orb_metadata *orb_id_t;
58 
59 /**
60  * Maximum number of multi topic instances
61  */
62 #define ORB_MULTI_MAX_INSTANCES 4 // This must be < 10 (because it's the last char of the node path)
63 
64 /**
65  * Topic priority.
66  * Relevant for multi-topics / topic groups
67  */
68 enum ORB_PRIO {
69  ORB_PRIO_MIN = 1, // leave 0 free for other purposes, eg. marking an uninitialized value
76 };
77 
78 /**
79  * Generates a pointer to the uORB metadata structure for
80  * a given topic.
81  *
82  * The topic must have been declared previously in scope
83  * with ORB_DECLARE().
84  *
85  * @param _name The name of the topic.
86  */
87 #define ORB_ID(_name) &__orb_##_name
88 
89 /**
90  * Declare (prototype) the uORB metadata for a topic (used by code generators).
91  *
92  * @param _name The name of the topic.
93  */
94 #if defined(__cplusplus)
95 # define ORB_DECLARE(_name) extern "C" const struct orb_metadata __orb_##_name __EXPORT
96 #else
97 # define ORB_DECLARE(_name) extern const struct orb_metadata __orb_##_name __EXPORT
98 #endif
99 
100 /**
101  * Define (instantiate) the uORB metadata for a topic.
102  *
103  * The uORB metadata is used to help ensure that updates and
104  * copies are accessing the right data.
105  *
106  * Note that there must be no more than one instance of this macro
107  * for each topic.
108  *
109  * @param _name The name of the topic.
110  * @param _struct The structure the topic provides.
111  * @param _size_no_padding Struct size w/o padding at the end
112  * @param _fields All fields in a semicolon separated list e.g: "float[3] position;bool armed"
113  */
114 #define ORB_DEFINE(_name, _struct, _size_no_padding, _fields) \
115  const struct orb_metadata __orb_##_name = { \
116  #_name, \
117  sizeof(_struct), \
118  _size_no_padding, \
119  _fields \
120  }; struct hack
121 
123 
124 /**
125  * ORB topic advertiser handle.
126  *
127  * Advertiser handles are global; once obtained they can be shared freely
128  * and do not need to be closed or released.
129  *
130  * This permits publication from interrupt context and other contexts where
131  * a file-descriptor-based handle would not otherwise be in scope for the
132  * publisher.
133  */
134 typedef void *orb_advert_t;
135 
136 /**
137  * @see uORB::Manager::orb_advertise()
138  */
139 extern orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data) __EXPORT;
140 
141 /**
142  * @see uORB::Manager::orb_advertise()
143  */
144 extern orb_advert_t orb_advertise_queue(const struct orb_metadata *meta, const void *data,
145  unsigned int queue_size) __EXPORT;
146 
147 /**
148  * @see uORB::Manager::orb_advertise_multi()
149  */
150 extern orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance,
151  int priority) __EXPORT;
152 
153 /**
154  * @see uORB::Manager::orb_advertise_multi()
155  */
156 extern orb_advert_t orb_advertise_multi_queue(const struct orb_metadata *meta, const void *data, int *instance,
157  int priority, unsigned int queue_size) __EXPORT;
158 
159 /**
160  * @see uORB::Manager::orb_unadvertise()
161  */
162 extern int orb_unadvertise(orb_advert_t handle) __EXPORT;
163 
164 /**
165  * @see uORB::Manager::orb_publish()
166  */
167 extern int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data) __EXPORT;
168 
169 /**
170  * Advertise as the publisher of a topic.
171  *
172  * This performs the initial advertisement of a topic; it creates the topic
173  * node in /obj if required and publishes the initial data.
174  *
175  * @see uORB::Manager::orb_advertise_multi() for meaning of the individual parameters
176  */
177 static inline int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data,
178  int *instance,
179  int priority)
180 {
181  if (!*handle) {
182  *handle = orb_advertise_multi(meta, data, instance, priority);
183 
184  if (*handle) {
185  return 0;
186  }
187 
188  } else {
189  return orb_publish(meta, *handle, data);
190  }
191 
192  return -1;
193 }
194 
195 /**
196  * @see uORB::Manager::orb_subscribe()
197  */
198 extern int orb_subscribe(const struct orb_metadata *meta) __EXPORT;
199 
200 /**
201  * @see uORB::Manager::orb_subscribe_multi()
202  */
203 extern int orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance) __EXPORT;
204 
205 /**
206  * @see uORB::Manager::orb_unsubscribe()
207  */
208 extern int orb_unsubscribe(int handle) __EXPORT;
209 
210 /**
211  * @see uORB::Manager::orb_copy()
212  */
213 extern int orb_copy(const struct orb_metadata *meta, int handle, void *buffer) __EXPORT;
214 
215 /**
216  * @see uORB::Manager::orb_check()
217  */
218 extern int orb_check(int handle, bool *updated) __EXPORT;
219 
220 /**
221  * @see uORB::Manager::orb_stat()
222  */
223 extern int orb_stat(int handle, uint64_t *time) __EXPORT;
224 
225 /**
226  * @see uORB::Manager::orb_exists()
227  */
228 extern int orb_exists(const struct orb_metadata *meta, int instance) __EXPORT;
229 
230 /**
231  * Get the number of published instances of a topic group
232  *
233  * @param meta ORB topic metadata.
234  * @return The number of published instances of this topic
235  */
236 extern int orb_group_count(const struct orb_metadata *meta) __EXPORT;
237 
238 /**
239  * @see uORB::Manager::orb_priority()
240  */
241 extern int orb_priority(int handle, int32_t *priority) __EXPORT;
242 
243 /**
244  * @see uORB::Manager::orb_set_interval()
245  */
246 extern int orb_set_interval(int handle, unsigned interval) __EXPORT;
247 
248 /**
249  * @see uORB::Manager::orb_get_interval()
250  */
251 extern int orb_get_interval(int handle, unsigned *interval) __EXPORT;
252 
254 
255 /* Diverse uORB header defines */ //XXX: move to better location
256 #define ORB_ID_VEHICLE_ATTITUDE_CONTROLS ORB_ID(actuator_controls_0)
257 typedef uint8_t arming_state_t;
258 typedef uint8_t main_state_t;
259 typedef uint8_t hil_state_t;
260 typedef uint8_t navigation_state_t;
261 typedef uint8_t switch_pos_t;
262 
263 #endif /* _UORB_UORB_H */
ORB_PRIO
Topic priority.
Definition: uORB.h:68
const uint16_t o_size_no_padding
object size w/o padding at the end (for logger)
Definition: uORB.h:53
int orb_subscribe(const struct orb_metadata *meta) __EXPORT
Definition: uORB.cpp:75
#define __END_DECLS
Definition: visibility.h:59
orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance, int priority) __EXPORT
Definition: uORB.cpp:53
orb_advert_t orb_advertise_multi_queue(const struct orb_metadata *meta, const void *data, int *instance, int priority, unsigned int queue_size) __EXPORT
Definition: uORB.cpp:59
int orb_exists(const struct orb_metadata *meta, int instance) __EXPORT
Definition: uORB.cpp:105
uint8_t arming_state_t
Definition: uORB.h:257
LidarLite * instance
Definition: ll40ls.cpp:65
int orb_priority(int handle, int32_t *priority) __EXPORT
Definition: uORB.cpp:121
const char * o_name
unique object name
Definition: uORB.h:51
#define __EXPORT
Definition: visibility.h:47
uint8_t navigation_state_t
Definition: uORB.h:260
uint8_t * data
Definition: dataman.cpp:149
#define __BEGIN_DECLS
Definition: visibility.h:58
uint8_t switch_pos_t
Definition: uORB.h:261
int orb_set_interval(int handle, unsigned interval) __EXPORT
Definition: uORB.cpp:126
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
uint8_t main_state_t
Definition: uORB.h:258
int orb_get_interval(int handle, unsigned *interval) __EXPORT
Definition: uORB.cpp:131
orb_advert_t orb_advertise_queue(const struct orb_metadata *meta, const void *data, unsigned int queue_size) __EXPORT
Definition: uORB.cpp:48
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data) __EXPORT
Definition: uORB.cpp:70
Object metadata.
Definition: uORB.h:50
const char * o_fields
semicolon separated list of fields (with type)
Definition: uORB.h:54
int orb_check(int handle, bool *updated) __EXPORT
Definition: uORB.cpp:95
const struct orb_metadata * orb_id_t
Definition: uORB.h:57
int orb_stat(int handle, uint64_t *time) __EXPORT
Definition: uORB.cpp:100
uint8_t hil_state_t
Definition: uORB.h:259
int orb_unadvertise(orb_advert_t handle) __EXPORT
Definition: uORB.cpp:65
int orb_unsubscribe(int handle) __EXPORT
Definition: uORB.cpp:85
const uint16_t o_size
object size
Definition: uORB.h:52
int orb_group_count(const struct orb_metadata *meta) __EXPORT
Get the number of published instances of a topic group.
Definition: uORB.cpp:110
int orb_subscribe_multi(const struct orb_metadata *meta, unsigned instance) __EXPORT
Definition: uORB.cpp:80
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer) __EXPORT
Definition: uORB.cpp:90
orb_advert_t orb_advertise(const struct orb_metadata *meta, const void *data) __EXPORT
Definition: uORB.cpp:43
static int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, int priority)
Advertise as the publisher of a topic.
Definition: uORB.h:177