driver.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: driver.h 6566 2008-06-14 01:00:19Z thjc $
00042  *
00043  *  The virtual class from which all driver classes inherit.  this
00044  *  defines the API that all drivers must implement.
00045  */
00046 
00047 #ifndef _DRIVER_H
00048 #define _DRIVER_H
00049 
00050 #include <pthread.h>
00051 
00052 #include <libplayercore/playercommon.h>
00053 #include <libplayercore/message.h>
00054 #include <libplayercore/player.h>
00055 #include <libplayercore/property.h>
00056 
00057 using namespace std;
00058 
00074 #define HANDLE_CAPABILITY_REQUEST(device_addr, queue, hdr, data, cap_type, cap_subtype) \
00075   if(Message::MatchMessage(hdr, PLAYER_MSGTYPE_REQ, PLAYER_CAPABILTIES_REQ, device_addr)) \
00076   { \
00077     player_capabilities_req_t & cap_req = * reinterpret_cast<player_capabilities_req_t *> (data);\
00078     if (cap_req.type == cap_type && cap_req.subtype == cap_subtype) \
00079     { \
00080       Publish(device_addr, queue, PLAYER_MSGTYPE_RESP_ACK, PLAYER_CAPABILTIES_REQ); \
00081       return 0; \
00082     } \
00083   }
00084 
00085 
00086 // Forward declarations
00087 class ConfigFile;
00088 
00097 class Driver
00098 {
00099   private:
00102     pthread_mutex_t accessMutex;
00103 
00104     /* @brief Last error value; useful for returning error codes from
00105     constructors. */
00106     int error;
00107 
00108     PropertyBag propertyBag;
00109 
00110   protected:
00111 
00112     /* @brief Start the driver thread
00113 
00114     This method is usually called from the overloaded Setup() method to
00115     create the driver thread.  This will call Main(). */
00116     virtual void StartThread(void);
00117 
00122     virtual void StopThread(void);
00123 
00126     static void* DummyMain(void *driver);
00127 
00130     static void DummyMainQuit(void *driver);
00131 
00137     int AddInterface(player_devaddr_t addr);
00138 
00146     int AddInterface(player_devaddr_t *addr, ConfigFile * cf, int section, int code, char * key = NULL);
00147 
00148 
00150     void SetError(int code) {this->error = code;}
00151 
00161     bool Wait(double TimeOut=0.0) { return this->InQueue->Wait(TimeOut); }
00162 
00164     int AddFileWatch(int fd, bool ReadWatch = true, bool WriteWatch = false, bool ExceptWatch = true);
00165 
00167     int RemoveFileWatch(int fd, bool ReadWatch = true, bool WriteWatch = false, bool ExceptWatch = true);
00168 
00169 
00170   public:
00175     pthread_t driverthread;
00177     virtual void Lock(void);
00179     virtual void Unlock(void);
00180 
00186     QueuePointer ret_queue;
00187 
00202     virtual void Publish(player_devaddr_t addr,
00203                  QueuePointer &queue,
00204                  uint8_t type,
00205                  uint8_t subtype,
00206                  void* src=NULL,
00207                  size_t deprecated=0,
00208                  double* timestamp=NULL,
00209                  bool copy=true);
00210 
00224      virtual void Publish(player_devaddr_t addr,
00225                   uint8_t type,
00226                   uint8_t subtype,
00227                   void* src=NULL,
00228                   size_t deprecated=0,
00229                   double* timestamp=NULL,
00230                   bool copy=true);
00231 
00232 
00233 
00242     virtual void Publish(QueuePointer &queue,
00243                  player_msghdr_t* hdr,
00244                  void* src,
00245                  bool copy = true);
00246 
00254     virtual void Publish(player_msghdr_t* hdr,
00255                  void* src,
00256                  bool copy = true);
00257 
00258 
00260     player_devaddr_t device_addr;
00261 
00263     int subscriptions;
00264 
00267     int entries;
00268 
00276     bool alwayson;
00277 
00279     QueuePointer InQueue;
00280 
00288     Driver(ConfigFile *cf,
00289            int section,
00290            bool overwrite_cmds,
00291            size_t queue_maxlen,
00292            int interf);
00293 
00301     Driver(ConfigFile *cf,
00302            int section,
00303            bool overwrite_cmds = true,
00304            size_t queue_maxlen = PLAYER_MSGQUEUE_DEFAULT_MAXLEN);
00305 
00307     virtual ~Driver();
00308 
00311     int GetError() { return(this->error); }
00312 
00322     virtual int Subscribe(player_devaddr_t addr);
00323 
00337     virtual int Subscribe(QueuePointer &queue, player_devaddr_t addr) {return 1;};
00338 
00348     virtual int Unsubscribe(player_devaddr_t addr);
00349 
00363     virtual int Unsubscribe(QueuePointer &queue, player_devaddr_t addr) {return 1;};
00364 
00371     virtual int Setup() = 0;
00372 
00379     virtual int Shutdown() = 0;
00380 
00386     virtual void Main(void);
00387 
00392     virtual void MainQuit(void);
00393 
00401     void ProcessMessages(int maxmsgs);
00402 
00407     void ProcessMessages(void);
00408 
00418     virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr * hdr,
00419                                void * data);
00420 
00422     virtual void Update()
00423     {
00424       if(!this->driverthread)
00425         this->ProcessMessages();
00426     }
00427 
00435     virtual int ProcessInternalMessages(QueuePointer& resp_queue,
00436                                         player_msghdr * hdr,
00437                                         void * data);
00438 
00446     virtual bool RegisterProperty(const char *key, Property *prop, ConfigFile* cf, int section);
00447 
00455     virtual bool RegisterProperty(Property *prop, ConfigFile* cf, int section);
00456 };
00457 
00458 
00459 #endif

Last updated 12 September 2005 21:38:45