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  * $Id: driver.h,v 1.17 2006/03/17 21:22:05 gerkey Exp $
00025  *  
00026  *  The virtual class from which all driver classes inherit.  this
00027  *  defines the API that all drivers must implement.
00028  */
00029 
00030 #ifndef _DRIVER_H
00031 #define _DRIVER_H
00032 
00033 #include <pthread.h>
00034 
00035 #include <libplayercore/playercommon.h>
00036 #include <libplayercore/message.h>
00037 #include <libplayercore/player.h>
00038 
00039 // Forward declarations
00040 class ConfigFile;
00041 class Driver;
00042 
00051 class Driver
00052 {
00053   private:
00056     pthread_mutex_t accessMutex;
00057 
00058     /* @brief Last error value; useful for returning error codes from
00059     constructors. */
00060     int error;
00061 
00062   protected:
00063 
00064     /* @brief Start the driver thread
00065     
00066     This method is usually called from the overloaded Setup() method to
00067     create the driver thread.  This will call Main(). */
00068     virtual void StartThread(void);
00069 
00074     virtual void StopThread(void);
00075 
00078     static void* DummyMain(void *driver);
00079 
00082     static void DummyMainQuit(void *driver);
00083 
00089     int AddInterface(player_devaddr_t addr);
00090     
00092     void SetError(int code) {this->error = code;}
00093 
00099     void Wait() { this->InQueue->Wait(); }
00100     
00101   public:
00106     pthread_t driverthread;
00108     virtual void Lock(void);
00110     virtual void Unlock(void);
00111 
00117     MessageQueue* ret_queue;
00118 
00132     void Publish(player_devaddr_t addr, 
00133                  MessageQueue* queue, 
00134                  uint8_t type, 
00135                  uint8_t subtype,
00136                  void* src=NULL, 
00137                  size_t len=0,
00138                  double* timestamp=NULL);
00139 
00148     void Publish(MessageQueue* queue, 
00149                  player_msghdr_t* hdr,
00150                  void* src);
00151 
00152 
00154     player_devaddr_t device_addr;
00155         
00157     int subscriptions;
00158 
00161     int entries;
00162 
00170     bool alwayson;
00171 
00173     MessageQueue* InQueue;
00174 
00182     Driver(ConfigFile *cf, 
00183            int section, 
00184            bool overwrite_cmds, 
00185            size_t queue_maxlen, 
00186            int interf);
00187 
00195     Driver(ConfigFile *cf, 
00196            int section,
00197            bool overwrite_cmds = true, 
00198            size_t queue_maxlen = PLAYER_MSGQUEUE_DEFAULT_MAXLEN);
00199 
00201     virtual ~Driver();
00202 
00205     int GetError() { return(this->error); }
00206     
00216     virtual int Subscribe(player_devaddr_t addr);
00217 
00227     virtual int Unsubscribe(player_devaddr_t addr);
00228 
00235     virtual int Setup() = 0;
00236 
00243     virtual int Shutdown() = 0;
00244 
00250     virtual void Main(void);
00251 
00256     virtual void MainQuit(void);
00257 
00265     void ProcessMessages(int maxmsgs);
00266 
00271     void ProcessMessages(void);
00272         
00282     virtual int ProcessMessage(MessageQueue* resp_queue, player_msghdr * hdr, 
00283                                void * data);
00284 
00286     virtual void Update() 
00287     {
00288       if(!this->driverthread)
00289         this->ProcessMessages();
00290     }
00291 };
00292 
00293 
00294 #endif

Last updated 12 September 2005 21:38:45