diff options
Diffstat (limited to 'lcmtype/lcm.h')
-rw-r--r-- | lcmtype/lcm.h | 316 |
1 files changed, 316 insertions, 0 deletions
diff --git a/lcmtype/lcm.h b/lcmtype/lcm.h new file mode 100644 index 0000000..d3762e9 --- /dev/null +++ b/lcmtype/lcm.h | |||
@@ -0,0 +1,316 @@ | |||
1 | #ifndef __lightweight_comunications_h__ | ||
2 | #define __lightweight_comunications_h__ | ||
3 | |||
4 | #ifdef __cplusplus | ||
5 | extern "C" { | ||
6 | #endif | ||
7 | |||
8 | #include <stdint.h> | ||
9 | |||
10 | #include "eventlog.h" | ||
11 | |||
12 | #define LCM_MAX_MESSAGE_SIZE (1 << 28) | ||
13 | |||
14 | #define LCM_MAX_CHANNEL_NAME_LENGTH 63 | ||
15 | |||
16 | #ifdef WIN32 | ||
17 | #define LCM_API_FUNCTION __declspec(dllexport) | ||
18 | #else | ||
19 | #define LCM_API_FUNCTION | ||
20 | #endif | ||
21 | |||
22 | /** | ||
23 | * @defgroup LcmC C API Reference | ||
24 | * | ||
25 | * THe %LCM C API provides classes and data structures for communicating with | ||
26 | * other %LCM clients, as well as reading and writing %LCM log files. | ||
27 | * | ||
28 | */ | ||
29 | |||
30 | /** | ||
31 | * @defgroup LcmC_lcm_t lcm_t | ||
32 | * @ingroup LcmC | ||
33 | * @brief Publish and receive messages | ||
34 | * | ||
35 | * All %LCM functions are internally synchronized and thread-safe. | ||
36 | * | ||
37 | * <tt> #include <lcm.h> </tt> | ||
38 | * | ||
39 | * Linking: <tt> `pkg-config --libs lcm` </tt> | ||
40 | * @{ | ||
41 | */ | ||
42 | |||
43 | /** | ||
44 | * Opaque data structure containing the LCM context. | ||
45 | */ | ||
46 | typedef struct _lcm_t lcm_t; | ||
47 | |||
48 | /** | ||
49 | * An opaque data structure that identifies an LCM subscription. | ||
50 | */ | ||
51 | typedef struct _lcm_subscription_t lcm_subscription_t; | ||
52 | |||
53 | /** | ||
54 | * Received messages are passed to user programs using this data structure. | ||
55 | * Each instance represents one message. | ||
56 | */ | ||
57 | typedef struct _lcm_recv_buf_t lcm_recv_buf_t; | ||
58 | struct _lcm_recv_buf_t { | ||
59 | /** | ||
60 | * the data received (raw bytes) | ||
61 | */ | ||
62 | void *data; | ||
63 | /** | ||
64 | * the length of the data received (in bytes) | ||
65 | */ | ||
66 | uint32_t data_size; | ||
67 | /** | ||
68 | * timestamp (micrseconds since the epoch) at which the first data | ||
69 | * bytes of the message were received. | ||
70 | */ | ||
71 | int64_t recv_utime; | ||
72 | /** | ||
73 | * pointer to the lcm_t struct that owns this buffer | ||
74 | */ | ||
75 | lcm_t *lcm; | ||
76 | }; | ||
77 | |||
78 | /** | ||
79 | * @brief Callback function prototype. | ||
80 | * | ||
81 | * Pass instances of this to lcm_subscribe() | ||
82 | * | ||
83 | * @param rbuf the message timestamp and payload | ||
84 | * @param channel the channel the message was received on | ||
85 | * @param user_data the user-specified parameter passed to lcm_subscribe() | ||
86 | */ | ||
87 | typedef void (*lcm_msg_handler_t)(const lcm_recv_buf_t *rbuf, | ||
88 | const char *channel, void *user_data); | ||
89 | |||
90 | /** | ||
91 | * @brief Constructor | ||
92 | * | ||
93 | * Allocates and initializes a lcm_t. %provider must be either | ||
94 | * NULL, or a string of the form | ||
95 | * | ||
96 | * <tt>"provider://network?option1=value1&option2=value2&...&optionN=valueN"</tt> | ||
97 | * | ||
98 | * @param provider Initializationg string specifying the LCM network provider. | ||
99 | * If this is NULL, and the environment variable "LCM_DEFAULT_URL" is defined, | ||
100 | * then the environment variable is used instead. If this is NULL and the | ||
101 | * environment variable is not defined, then default settings are used. | ||
102 | * | ||
103 | * The currently supported providers are: | ||
104 | * | ||
105 | * @verbatim | ||
106 | udpm:// | ||
107 | UDP Multicast provider | ||
108 | network can be of the form "multicast_address:port". Either the | ||
109 | multicast address or the port may be ommitted for the default. | ||
110 | |||
111 | options: | ||
112 | recv_buf_size = N | ||
113 | size of the kernel UDP receive buffer to request. Defaults to | ||
114 | operating system defaults | ||
115 | |||
116 | ttl = N | ||
117 | time to live of transmitted packets. Default 0 | ||
118 | |||
119 | examples: | ||
120 | "udpm://239.255.76.67:7667" | ||
121 | Default initialization string | ||
122 | |||
123 | "udpm://239.255.76.67:7667?ttl=1" | ||
124 | Sets the multicast TTL to 1 so that packets published will enter | ||
125 | the local network. | ||
126 | @endverbatim | ||
127 | * | ||
128 | * @verbatim | ||
129 | file:// | ||
130 | LCM Log file-based provider | ||
131 | network should be the path to the log file | ||
132 | |||
133 | Events are read from or written to the log file. In read mode, events | ||
134 | are generated from the log file in real-time, or at the rate specified | ||
135 | by the speed option. In write mode, events published to the LCM instance | ||
136 | will be written to the log file in real-time. | ||
137 | |||
138 | options: | ||
139 | speed = N | ||
140 | Scale factor controlling the playback speed of the log file. | ||
141 | Defaults to 1. If less than or equal to zero, then the events | ||
142 | in the log file are played back as fast as possible. Events are | ||
143 | never skipped in read mode, so actual playback speed may be slower | ||
144 | than requested, depending on the handlers. | ||
145 | |||
146 | mode = r | w | ||
147 | Specifies the log file mode. Defaults to 'r' | ||
148 | |||
149 | start_timestamp = USEC | ||
150 | Seeks to USEC microseconds in the logfile, where USEC is given in | ||
151 | microseconds since 00:00:00 UTC on 1 January 1970. If USEC is | ||
152 | before the first event, then playback begins at the start of the | ||
153 | log file. If it is after the last event, calls to lcm_handle will | ||
154 | return -1. | ||
155 | |||
156 | examples: | ||
157 | "file:///home/albert/path/to/logfile" | ||
158 | Loads the file "/home/albert/path/to/logfile" as an LCM event | ||
159 | source. | ||
160 | |||
161 | "file:///home/albert/path/to/logfile?speed=4" | ||
162 | Loads the file "/home/albert/path/to/logfile" as an LCM event | ||
163 | source. Events are played back at 4x speed. | ||
164 | |||
165 | @endverbatim | ||
166 | * | ||
167 | * @return a newly allocated lcm_t instance, or NULL on failure. Free with | ||
168 | * lcm_destroy() when no longer needed. | ||
169 | */ | ||
170 | LCM_API_FUNCTION | ||
171 | lcm_t *lcm_create(const char *provider); | ||
172 | |||
173 | /** | ||
174 | * @brief Destructor | ||
175 | */ | ||
176 | LCM_API_FUNCTION | ||
177 | void lcm_destroy(lcm_t *lcm); | ||
178 | |||
179 | /** | ||
180 | * @brief Returns a file descriptor or socket that can be used with | ||
181 | * @c select(), @c poll(), or other event loops for asynchronous | ||
182 | * notification of incoming messages. | ||
183 | * | ||
184 | * Each LCM instance has a file descriptor that can be used to asynchronously | ||
185 | * receive notification when incoming messages have been received. This file | ||
186 | * descriptor can typically be incorporated into an application event loop | ||
187 | * (e.g., GTK+, QT, etc.) For an example using select(), see | ||
188 | * examples/c/listener-async.c | ||
189 | * | ||
190 | * @return a file descriptor suitable for use with select, poll, etc. | ||
191 | */ | ||
192 | LCM_API_FUNCTION | ||
193 | int lcm_get_fileno(lcm_t *lcm); | ||
194 | |||
195 | /** | ||
196 | * @brief Subscribe a callback function to a channel, without automatic message | ||
197 | * decoding. | ||
198 | * | ||
199 | * In general, you probably don't want to use this function, as it does not | ||
200 | * automatically decode messages. Instead, use the message-specific subscribe | ||
201 | * function generated by @c lcm-gen. Use this function only when you want to | ||
202 | * work with the raw message itself. TODO link to example or more details. | ||
203 | * | ||
204 | * The callback function will be invoked during calls to lcm_handle() any time | ||
205 | * a message on the specified channel is received. Multiple callbacks can be | ||
206 | * subscribed for the same channel. | ||
207 | * | ||
208 | * @param lcm the LCM object | ||
209 | * @param channel the channel to listen on. This can also be a GLib regular | ||
210 | * expression, and is treated as a regex implicitly surrounded | ||
211 | * by '^' and '$' | ||
212 | * @param handler the callback function to be invoked when a message is | ||
213 | * received on the specified channel | ||
214 | * @param userdata this will be passed to the callback function | ||
215 | * | ||
216 | * @return a lcm_subscription_t to identify the new subscription, | ||
217 | * which can be passed to lcm_unsubscribe(). The lcm_t instance owns | ||
218 | * the subscription object. | ||
219 | */ | ||
220 | LCM_API_FUNCTION | ||
221 | lcm_subscription_t *lcm_subscribe(lcm_t *lcm, const char *channel, | ||
222 | lcm_msg_handler_t handler, void *userdata); | ||
223 | |||
224 | /** | ||
225 | * @brief Unsubscribe a message handler. | ||
226 | * | ||
227 | * In general, you probably don't want to use this function. Instead, use the | ||
228 | * message-specific unsubscribe function generated by @c lcm-gen. Use this | ||
229 | * function only when you want to work with the raw message itself. TODO link | ||
230 | * to example or more details. | ||
231 | * | ||
232 | * The callback function for the subscription will no longer be | ||
233 | * invoked when messages on the corresponding channel are received. After this | ||
234 | * function returns, @c handler is no longer valid and should not be used | ||
235 | * anymore. | ||
236 | * | ||
237 | * @return 0 on success, or -1 if @c handler is not a valid subscription. | ||
238 | */ | ||
239 | LCM_API_FUNCTION | ||
240 | int lcm_unsubscribe(lcm_t *lcm, lcm_subscription_t *handler); | ||
241 | |||
242 | /** | ||
243 | * @brief Publish a message, specified as a raw byte buffer. | ||
244 | * | ||
245 | * In general, you probably don't want to use this function, as it does not | ||
246 | * automatically encode messages. Instead, use the message-specific publish | ||
247 | * function generated by @c lcm-gen. Use this function only when you want to | ||
248 | * publish raw byte buffers. TODO link to example or more details. | ||
249 | * | ||
250 | * @param lcm The %LCM object | ||
251 | * @param channel The channel to publish on | ||
252 | * @param data The raw byte buffer | ||
253 | * @param datalen Size of the byte buffer | ||
254 | * | ||
255 | * @return 0 on success, -1 on failure. | ||
256 | */ | ||
257 | LCM_API_FUNCTION | ||
258 | int lcm_publish(lcm_t *lcm, const char *channel, const void *data, | ||
259 | unsigned int datalen); | ||
260 | |||
261 | /** | ||
262 | * @brief Wait for and dispatch the next incoming message. | ||
263 | * | ||
264 | * Message handlers are invoked one at a time from the thread that calls this | ||
265 | * function, and in the order that they were subscribed. | ||
266 | * | ||
267 | * This function waits indefinitely. If you want timeout behavior, (e.g., wait | ||
268 | * 100ms for a message) then consider using lcm_get_fileno() together with | ||
269 | * select() or poll() | ||
270 | * | ||
271 | * Recursive calls to lcm_handle are not allowed -- do not call lcm_handle from | ||
272 | * within a message handler. All other functions are okay (e.g., it is okay to | ||
273 | * call lcm_publish from within a message handler). | ||
274 | * | ||
275 | * @param lcm the %LCM object | ||
276 | * | ||
277 | * @return 0 normally, or -1 when an error has occurred. | ||
278 | */ | ||
279 | LCM_API_FUNCTION | ||
280 | int lcm_handle(lcm_t *lcm); | ||
281 | |||
282 | /** | ||
283 | * @brief Adjusts the maximum number of received messages that can be queued up | ||
284 | * for a subscription. | ||
285 | * | ||
286 | * In general, you probably don't want to use this function. Instead, use the | ||
287 | * message-specific set_queue_capacity function generated by @c lcm-gen. Use | ||
288 | * this function only when you want to work with untyped subscriptions. TODO | ||
289 | * link to example or more details. | ||
290 | * | ||
291 | * Setting this to a low number may reduce overall latency at the expense of | ||
292 | * dropping more messages. Conversely, setting this to a high number may drop | ||
293 | * fewer messages at the expense of increased latency. A value of 0 indicates | ||
294 | * no limit, and should be used carefully. | ||
295 | * | ||
296 | * @param handler the subscription object | ||
297 | * @param num_messages the maximum queue size, in messages. The default is 30. | ||
298 | * | ||
299 | */ | ||
300 | LCM_API_FUNCTION | ||
301 | int lcm_subscription_set_queue_capacity(lcm_subscription_t *handler, | ||
302 | int num_messages); | ||
303 | |||
304 | #define LCM_MAJOR_VERSION 1 | ||
305 | #define LCM_MINOR_VERSION 0 | ||
306 | #define LCM_MICRO_VERSION 0 | ||
307 | |||
308 | /** | ||
309 | * @} | ||
310 | */ | ||
311 | |||
312 | #ifdef __cplusplus | ||
313 | } | ||
314 | #endif | ||
315 | |||
316 | #endif | ||