device.h
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2000  
00004  *     Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
00005  *                      
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 /********************************************************************
00023  *
00024  *  This library is free software; you can redistribute it and/or
00025  *  modify it under the terms of the GNU Lesser General Public
00026  *  License as published by the Free Software Foundation; either
00027  *  version 2.1 of the License, or (at your option) any later version.
00028  *
00029  *  This library is distributed in the hope that it will be useful,
00030  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00031  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00032  *  Lesser General Public License for more details.
00033  *
00034  *  You should have received a copy of the GNU Lesser General Public
00035  *  License along with this library; if not, write to the Free Software
00036  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00037  *
00038  ********************************************************************/
00039 
00040 /*
00041  * $Id: device.h 8462 2009-12-16 00:27:55Z gbiggs $
00042  *
00043  * A device is a driver/interface pair.
00044  */
00045 
00046 #ifndef _DEVICE_H
00047 #define _DEVICE_H
00048 
00049 #if defined (WIN32)
00050   #if defined (PLAYER_STATIC)
00051     #define PLAYERCORE_EXPORT
00052   #elif defined (playercore_EXPORTS)
00053     #define PLAYERCORE_EXPORT    __declspec (dllexport)
00054   #else
00055     #define PLAYERCORE_EXPORT    __declspec (dllimport)
00056   #endif
00057 #else
00058   #define PLAYERCORE_EXPORT
00059 #endif
00060 
00061 #include <libplayerinterface/player.h>
00062 #include <libplayercore/message.h>
00063 
00064 #define LOCALHOST_ADDR 16777343
00065 
00066 // Forward declarations
00067 class Driver;
00068 
00074 class PLAYERCORE_EXPORT Device
00075 {
00076   public:
00081     Device(player_devaddr_t addr, Driver *driver);
00082 
00084     ~Device();
00085 
00089     int Subscribe(QueuePointer &sub_queue);
00090 
00094     int Unsubscribe(QueuePointer &sub_queue);
00095 
00109     void PutMsg(QueuePointer &resp_queue,
00110                 uint8_t type, 
00111                 uint8_t subtype,
00112                 void* src, 
00113                 size_t deprecated,
00114                 double* timestamp);
00115 
00125     void PutMsg(QueuePointer &resp_queue,
00126                 player_msghdr_t* hdr,
00127                 void* src,
00128                 bool copy=true);
00129 
00152     Message* Request(QueuePointer &resp_queue,
00153                      uint8_t type,
00154                      uint8_t subtype,
00155                      void* src,
00156                      size_t deprecated,
00157                      double* timestamp,
00158                      bool threaded = true);
00159                      
00187     Message* TimedRequest(QueuePointer &resp_queue,
00188                      uint8_t type,
00189                      uint8_t subtype,
00190                      void* src,
00191                      double timeout = 0,
00192                      double* timestamp = NULL,
00193                      bool threaded = true);
00194                      
00195 
00201     static bool MatchDeviceAddress(player_devaddr_t addr1,
00202                                    player_devaddr_t addr2)
00203     {
00204       // On some machines, looking up "localhost" gives you
00205       // "127.0.0.1", which packs into a 32-bit int as 16777343.  On other
00206       // machines, it gives you "0.0.0.0", which packs as 0.  In order to
00207       // be able to do things like play back logfiles made on any machine,
00208       // we'll treat these two addresses as identical.
00209       return(((addr1.host == addr2.host) ||
00210               ((addr1.host == 0) && (addr2.host == LOCALHOST_ADDR)) ||
00211               ((addr1.host == LOCALHOST_ADDR) && (addr2.host == 0))) &&
00212              (addr1.robot == addr2.robot) &&
00213              (addr1.interf == addr2.interf) &&
00214              (addr1.index == addr2.index));
00215     }
00216 
00218     Device* next;
00219 
00221     player_devaddr_t addr;
00222 
00224     char drivername[PLAYER_MAX_DRIVER_STRING_LEN];
00225 
00226     
00228     QueuePointer InQueue;
00229 
00231     QueuePointer* queues;
00232 
00234     size_t len_queues;
00235 
00237     Driver* driver;
00238 
00239   private:
00242     pthread_mutex_t accessMutex;
00244     void Lock(void); 
00246     void Unlock(void);
00247   
00248     
00249 };
00250 
00251 #endif

Last updated 25 May 2011 21:17:00