device.h
1/*
2 * Player - One Hell of a Robot Server
3 * Copyright (C) 2000
4 * Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22/********************************************************************
23 *
24 * This library is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with this library; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 *
38 ********************************************************************/
39
40/*
41 * $Id$
42 *
43 * A device is a driver/interface pair.
44 */
45
46#ifndef _DEVICE_H
47#define _DEVICE_H
48
49#if defined (WIN32)
50 #if defined (PLAYER_STATIC)
51 #define PLAYERCORE_EXPORT
52 #elif defined (playercore_EXPORTS)
53 #define PLAYERCORE_EXPORT __declspec (dllexport)
54 #else
55 #define PLAYERCORE_EXPORT __declspec (dllimport)
56 #endif
57#else
58 #define PLAYERCORE_EXPORT
59#endif
60
61#include <libplayerinterface/player.h>
62#include <libplayercore/message.h>
63
64#define LOCALHOST_ADDR 16777343
65
66// Forward declarations
67class Driver;
68
74class PLAYERCORE_EXPORT Device
75{
76 public:
82
85
89 int Subscribe(QueuePointer &sub_queue);
90
94 int Unsubscribe(QueuePointer &sub_queue);
95
109 void PutMsg(QueuePointer &resp_queue,
110 uint8_t type,
111 uint8_t subtype,
112 void* src,
113 size_t deprecated,
114 double* timestamp);
115
125 void PutMsg(QueuePointer &resp_queue,
126 player_msghdr_t* hdr,
127 void* src,
128 bool copy=true);
129
153 uint8_t type,
154 uint8_t subtype,
155 void* src,
156 size_t deprecated,
157 double* timestamp,
158 bool threaded = true);
159
188 uint8_t type,
189 uint8_t subtype,
190 void* src,
191 double timeout = 0,
192 double* timestamp = NULL,
193 bool threaded = true);
194
195
202 player_devaddr_t addr2)
203 {
204 // On some machines, looking up "localhost" gives you
205 // "127.0.0.1", which packs into a 32-bit int as 16777343. On other
206 // machines, it gives you "0.0.0.0", which packs as 0. In order to
207 // be able to do things like play back logfiles made on any machine,
208 // we'll treat these two addresses as identical.
209 return(((addr1.host == addr2.host) ||
210 ((addr1.host == 0) && (addr2.host == LOCALHOST_ADDR)) ||
211 ((addr1.host == LOCALHOST_ADDR) && (addr2.host == 0))) &&
212 (addr1.robot == addr2.robot) &&
213 (addr1.interf == addr2.interf) &&
214 (addr1.index == addr2.index));
215 }
216
219
222
225
226
229
232
235
238
239 private:
242 pthread_mutex_t accessMutex;
244 void Lock(void);
246 void Unlock(void);
247
248
249};
250
251#endif
Encapsulates a device (i.e., a driver bound to an interface)
Definition device.h:75
static bool MatchDeviceAddress(player_devaddr_t addr1, player_devaddr_t addr2)
Compare two addresses.
Definition device.h:201
Message * Request(QueuePointer &resp_queue, uint8_t type, uint8_t subtype, void *src, size_t deprecated, double *timestamp, bool threaded=true)
Make a request of another device.
pthread_mutex_t accessMutex
Mutex used to lock access, via Lock() and Unlock(), to device internals, like the list of subscribed ...
Definition device.h:242
Driver * driver
Pointer to the underlying driver.
Definition device.h:237
void PutMsg(QueuePointer &resp_queue, uint8_t type, uint8_t subtype, void *src, size_t deprecated, double *timestamp)
Send a message to this device.
QueuePointer * queues
Linked list of subscribed queues.
Definition device.h:231
QueuePointer InQueue
Pointer to the underlying driver's queue.
Definition device.h:228
size_t len_queues
Length of queues.
Definition device.h:234
void Lock(void)
Lock access to driver internals.
player_devaddr_t addr
Address for this device.
Definition device.h:221
Device * next
Next entry in the device table (this is a linked-list)
Definition device.h:218
void PutMsg(QueuePointer &resp_queue, player_msghdr_t *hdr, void *src, bool copy=true)
Send a message to this device (short form)
~Device()
Destructor.
int Unsubscribe(QueuePointer &sub_queue)
Unsubscribe the given queue from this device.
void Unlock(void)
Unlock access to driver internals.
Message * TimedRequest(QueuePointer &resp_queue, uint8_t type, uint8_t subtype, void *src, double timeout=0, double *timestamp=NULL, bool threaded=true)
Make a request of another device with a timeout.
Device(player_devaddr_t addr, Driver *driver)
Constructor.
int Subscribe(QueuePointer &sub_queue)
Subscribe the given queue to this device.
Base class for all drivers.
Definition driver.h:109
Reference-counted message objects.
Definition message.h:133
An autopointer for the message queue.
Definition message.h:74
#define PLAYER_MAX_DRIVER_STRING_LEN
Maximum length for a driver name.
Definition player.h:72
A device address.
Definition player.h:146
uint16_t index
Which device of that interface.
Definition player.h:155
uint32_t robot
The "robot" or device collection in which the device resides.
Definition player.h:151
uint32_t host
The "host" on which the device resides.
Definition player.h:148
uint16_t interf
The interface provided by the device; must be one of PLAYER_*_CODE.
Definition player.h:153
Generic message header.
Definition player.h:162