driver.h
/*
* Player - One Hell of a Robot Server
* Copyright (C) 2000
* Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/********************************************************************
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
********************************************************************/
/*
* $Id: driver.h 8004 2009-07-13 14:03:44Z thjc $
*
* The virtual class from which all driver classes inherit. this
* defines the API that all drivers must implement.
*/
#ifndef _DRIVER_H
#define _DRIVER_H
#if defined (WIN32)
#if defined (PLAYER_STATIC)
#define PLAYERCORE_EXPORT
#elif defined (playercore_EXPORTS)
#define PLAYERCORE_EXPORT __declspec (dllexport)
#else
#define PLAYERCORE_EXPORT __declspec (dllimport)
#endif
#else
#define PLAYERCORE_EXPORT
#endif
#include <pthread.h>
#include <libplayercommon/playercommon.h>
#include <libplayercore/message.h>
#include <libplayerinterface/player.h>
#include <libplayercore/property.h>
//using namespace std;
#define HANDLE_CAPABILITY_REQUEST(device_addr, queue, hdr, data, cap_type, cap_subtype) \
if(Message::MatchMessage(hdr, PLAYER_MSGTYPE_REQ, PLAYER_CAPABILTIES_REQ, device_addr)) \
{ \
player_capabilities_req_t & cap_req = * reinterpret_cast<player_capabilities_req_t *> (data);\
if (cap_req.type == cap_type && cap_req.subtype == cap_subtype) \
{ \
Publish(device_addr, queue, PLAYER_MSGTYPE_RESP_ACK, PLAYER_CAPABILTIES_REQ); \
return 0; \
} \
}
// Forward declarations
class ConfigFile;
class PLAYERCORE_EXPORT Driver
{
private:
/* @brief Last error value; useful for returning error codes from
constructors. */
int error;
PropertyBag propertyBag;
int subscriptions;
public:
bool HasSubscriptions();
protected:
int AddInterface(player_devaddr_t addr);
int AddInterface(player_devaddr_t *addr, ConfigFile * cf, int section, int code, const char * key = NULL);
void SetError(int code) {this->error = code;}
virtual bool Wait(double TimeOut=0.0);
int AddFileWatch(int fd, bool ReadWatch = true, bool WriteWatch = false, bool ExceptWatch = true);
int RemoveFileWatch(int fd, bool ReadWatch = true, bool WriteWatch = false, bool ExceptWatch = true);
private:
pthread_mutex_t accessMutex;
pthread_mutex_t subscriptionMutex;
protected:
virtual void Lock(void);
virtual void Unlock(void);
virtual void SubscriptionLock(void);
virtual void SubscriptionUnlock(void);
virtual void TestCancel() {};
public:
QueuePointer ret_queue;
virtual void Publish(player_devaddr_t addr,
QueuePointer &queue,
uint8_t type,
uint8_t subtype,
void* src=NULL,
size_t deprecated=0,
double* timestamp=NULL,
bool copy=true);
virtual void Publish(player_devaddr_t addr,
uint8_t type,
uint8_t subtype,
void* src=NULL,
size_t deprecated=0,
double* timestamp=NULL,
bool copy=true);
virtual void Publish(QueuePointer &queue,
player_msghdr_t* hdr,
void* src,
bool copy = true);
virtual void Publish(player_msghdr_t* hdr,
void* src,
bool copy = true);
player_devaddr_t device_addr;
int entries;
bool alwayson;
QueuePointer InQueue;
Driver(ConfigFile *cf,
int section,
bool overwrite_cmds,
size_t queue_maxlen,
int interf);
Driver(ConfigFile *cf,
int section,
bool overwrite_cmds = true,
size_t queue_maxlen = PLAYER_MSGQUEUE_DEFAULT_MAXLEN);
virtual ~Driver();
int GetError() { return(this->error); }
virtual int Subscribe(player_devaddr_t addr);
virtual int Subscribe(QueuePointer &queue, player_devaddr_t addr) {return 1;};
virtual int Unsubscribe(player_devaddr_t addr);
virtual int Unsubscribe(QueuePointer &queue, player_devaddr_t addr) {return 1;};
virtual int Terminate();
virtual int Setup() {return 0;};
virtual int Shutdown() {return 0;};
void ProcessMessages(int maxmsgs);
void ProcessMessages(void);
virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr * hdr,
void * data);
virtual void Update()
{
this->ProcessMessages();
}
virtual int ProcessInternalMessages(QueuePointer& resp_queue,
player_msghdr * hdr,
void * data);
virtual bool RegisterProperty(const char *key, Property *prop, ConfigFile* cf, int section);
virtual bool RegisterProperty(Property *prop, ConfigFile* cf, int section);
};
typedef enum player_thread_state
{
PLAYER_THREAD_STATE_STOPPED,
PLAYER_THREAD_STATE_RUNNING,
PLAYER_THREAD_STATE_STOPPING,
PLAYER_THREAD_STATE_RESTARTING
} player_thread_state_t;
class PLAYERCORE_EXPORT PlayerBarrier
{
public:
PlayerBarrier()
{
pthread_mutex_init(&barrierMutex,NULL);
pthread_cond_init(&barrierCond,NULL);
barrierValue = 0;
}
~PlayerBarrier()
{
pthread_mutex_destroy(&barrierMutex);
pthread_cond_destroy(&barrierCond);
};
int SetValue(int Value)
{
return barrierValue = Value;
};
int Wait()
{
pthread_mutex_lock(&barrierMutex);
assert(barrierValue);
if (--barrierValue)
pthread_cond_wait(&barrierCond,&barrierMutex);
else
pthread_cond_broadcast(&barrierCond);
pthread_mutex_unlock(&barrierMutex);
return 0;
};
private:
pthread_mutex_t barrierMutex;
int barrierValue;
pthread_cond_t barrierCond;
};
class PLAYERCORE_EXPORT ThreadedDriver : public Driver
{
protected:
/* @brief Start the driver thread
This method is usually called from the overloaded Setup() method to
create the driver thread. This will call Main(). */
virtual void StartThread(void);
virtual void StopThread(void);
static void* DummyMain(void *driver);
static void DummyMainQuit(void *driver);
private:
pthread_t driverthread;
player_thread_state_t ThreadState;
bool SetupSuccessful;
PlayerBarrier SetupBarrier;
protected:
void TestCancel();
public:
ThreadedDriver(ConfigFile *cf,
int section,
bool overwrite_cmds,
size_t queue_maxlen,
int interf);
ThreadedDriver(ConfigFile *cf,
int section,
bool overwrite_cmds = true,
size_t queue_maxlen = PLAYER_MSGQUEUE_DEFAULT_MAXLEN);
virtual ~ThreadedDriver();
virtual int Setup();
virtual int Shutdown();
virtual int Terminate();
virtual void Main(void) = 0;
virtual int MainSetup(void) {return 0;};
virtual void MainQuit(void) {};
bool Wait(double TimeOut=0.0);
virtual void Update()
{};
};
#endif
ConfigFile
Class for loading configuration file information.
Definition: configfile.h:196
player_msghdr
Generic message header.
Definition: player.h:157
Driver::GetError
int GetError()
Get last error value.
Definition: driver.h:314
Driver::TestCancel
virtual void TestCancel()
enable thread cancellation and test for cancellation
Definition: driver.h:183
PLAYER_MSGQUEUE_DEFAULT_MAXLEN
#define PLAYER_MSGQUEUE_DEFAULT_MAXLEN
Default maximum length for a message queue.
Definition: player.h:72
ThreadedDriver::SetupBarrier
PlayerBarrier SetupBarrier
Barrier to synchronise threads on setup.
Definition: driver.h:580
ThreadedDriver::Update
virtual void Update()
Update non-threaded drivers.
Definition: driver.h:669
Driver::Wait
virtual bool Wait(double TimeOut=0.0)
Wait for new data to arrive on the driver's queue.
ThreadedDriver::MainSetup
virtual int MainSetup(void)
Sets up the resources needed by the driver thread.
Definition: driver.h:650
Driver::Update
virtual void Update()
Update non-threaded drivers.
Definition: driver.h:420
player_devaddr
A device address.
Definition: player.h:141
Driver::Terminate
virtual int Terminate()
Terminate the driver.
Driver::Subscribe
virtual int Subscribe(QueuePointer &queue, player_devaddr_t addr)
Subscribe to this driver.
Definition: driver.h:340
Driver::Shutdown
virtual int Shutdown()
Finalize the driver.
Definition: driver.h:390
ThreadedDriver
Base class for drivers which oeprate with a thread.
Definition: driver.h:544
Driver::subscriptionMutex
pthread_mutex_t subscriptionMutex
Mutex used to protect the subscription count for the driver.
Definition: driver.h:167
QueuePointer
An autopointer for the message queue.
Definition: message.h:73
ThreadedDriver::MainQuit
virtual void MainQuit(void)
Cleanup method for driver thread (called when main exits)
Definition: driver.h:656
Driver::Setup
virtual int Setup()
Initialize the driver.
Definition: driver.h:383
Driver::entries
int entries
Total number of entries in the device table using this driver.
Definition: driver.h:270
Driver::alwayson
bool alwayson
Always on flag.
Definition: driver.h:279
Driver::accessMutex
pthread_mutex_t accessMutex
Mutex used to lock access, via Lock() and Unlock(), to driver internals, like the list of subscribed ...
Definition: driver.h:165
Driver::InQueue
QueuePointer InQueue
Queue for all incoming messages for this driver.
Definition: driver.h:282
Driver::SetError
void SetError(int code)
Set/reset error code.
Definition: driver.h:142
Driver::Unsubscribe
virtual int Unsubscribe(QueuePointer &queue, player_devaddr_t addr)
Unsubscribe from this driver.
Definition: driver.h:366
ThreadedDriver::ThreadState
player_thread_state_t ThreadState
TODO: insert state machine here
Definition: driver.h:576
Driver
Base class for all drivers.
Definition: driver.h:108
PlayerBarrier
Definition: driver.h:463
ThreadedDriver::driverthread
pthread_t driverthread
The driver's thread.
Definition: driver.h:572
Property
Property base class.
Definition: property.h:59
Driver::subscriptions
int subscriptions
Number of subscriptions to this driver.
Definition: driver.h:118
PropertyBag
Property bag class: stores registered properties.
Definition: property.h:207
Driver::device_addr
player_devaddr_t device_addr
Default device address (single-interface drivers)
Definition: driver.h:266

Last updated 12 September 2005 21:38:45