driver.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37  *
38  ********************************************************************/
39 
40 /*
41  * $Id: driver.h 8004 2009-07-13 14:03:44Z thjc $
42  *
43  * The virtual class from which all driver classes inherit. this
44  * defines the API that all drivers must implement.
45  */
46 
47 #ifndef _DRIVER_H
48 #define _DRIVER_H
49 
50 #if defined (WIN32)
51  #if defined (PLAYER_STATIC)
52  #define PLAYERCORE_EXPORT
53  #elif defined (playercore_EXPORTS)
54  #define PLAYERCORE_EXPORT __declspec (dllexport)
55  #else
56  #define PLAYERCORE_EXPORT __declspec (dllimport)
57  #endif
58 #else
59  #define PLAYERCORE_EXPORT
60 #endif
61 
62 #include <pthread.h>
63 
64 #include <libplayercommon/playercommon.h>
65 #include <libplayercore/message.h>
66 #include <libplayerinterface/player.h>
67 #include <libplayercore/property.h>
68 
69 //using namespace std;
70 
86 #define HANDLE_CAPABILITY_REQUEST(device_addr, queue, hdr, data, cap_type, cap_subtype) \
87  if(Message::MatchMessage(hdr, PLAYER_MSGTYPE_REQ, PLAYER_CAPABILTIES_REQ, device_addr)) \
88  { \
89  player_capabilities_req_t & cap_req = * reinterpret_cast<player_capabilities_req_t *> (data);\
90  if (cap_req.type == cap_type && cap_req.subtype == cap_subtype) \
91  { \
92  Publish(device_addr, queue, PLAYER_MSGTYPE_RESP_ACK, PLAYER_CAPABILTIES_REQ); \
93  return 0; \
94  } \
95  }
96 
97 
98 // Forward declarations
99 class ConfigFile;
100 
108 class PLAYERCORE_EXPORT Driver
109 {
110  private:
111  /* @brief Last error value; useful for returning error codes from
112  constructors. */
113  int error;
114 
115  PropertyBag propertyBag;
116 
119  public:
120  bool HasSubscriptions();
121 
122  protected:
123 
129  int AddInterface(player_devaddr_t addr);
130 
138  int AddInterface(player_devaddr_t *addr, ConfigFile * cf, int section, int code, const char * key = NULL);
139 
140 
142  void SetError(int code) {this->error = code;}
143 
153  virtual bool Wait(double TimeOut=0.0);
154 
156  int AddFileWatch(int fd, bool ReadWatch = true, bool WriteWatch = false, bool ExceptWatch = true);
157 
159  int RemoveFileWatch(int fd, bool ReadWatch = true, bool WriteWatch = false, bool ExceptWatch = true);
160 
161 
162  private:
165  pthread_mutex_t accessMutex;
167  pthread_mutex_t subscriptionMutex;
168  protected:
171  virtual void Lock(void);
173  virtual void Unlock(void);
174 
176  virtual void SubscriptionLock(void);
178  virtual void SubscriptionUnlock(void);
179 
183  virtual void TestCancel() {};
184 
185 
186  public:
192  QueuePointer ret_queue;
193 
208  virtual void Publish(player_devaddr_t addr,
209  QueuePointer &queue,
210  uint8_t type,
211  uint8_t subtype,
212  void* src=NULL,
213  size_t deprecated=0,
214  double* timestamp=NULL,
215  bool copy=true);
216 
230  virtual void Publish(player_devaddr_t addr,
231  uint8_t type,
232  uint8_t subtype,
233  void* src=NULL,
234  size_t deprecated=0,
235  double* timestamp=NULL,
236  bool copy=true);
237 
238 
239 
248  virtual void Publish(QueuePointer &queue,
249  player_msghdr_t* hdr,
250  void* src,
251  bool copy = true);
252 
260  virtual void Publish(player_msghdr_t* hdr,
261  void* src,
262  bool copy = true);
263 
264 
267 
270  int entries;
271 
279  bool alwayson;
280 
283 
291  Driver(ConfigFile *cf,
292  int section,
293  bool overwrite_cmds,
294  size_t queue_maxlen,
295  int interf);
296 
304  Driver(ConfigFile *cf,
305  int section,
306  bool overwrite_cmds = true,
307  size_t queue_maxlen = PLAYER_MSGQUEUE_DEFAULT_MAXLEN);
308 
310  virtual ~Driver();
311 
314  int GetError() { return(this->error); }
315 
325  virtual int Subscribe(player_devaddr_t addr);
326 
340  virtual int Subscribe(QueuePointer &queue, player_devaddr_t addr) {return 1;};
341 
351  virtual int Unsubscribe(player_devaddr_t addr);
352 
366  virtual int Unsubscribe(QueuePointer &queue, player_devaddr_t addr) {return 1;};
367 
374  virtual int Terminate();
375 
376 
383  virtual int Setup() {return 0;};
384 
390  virtual int Shutdown() {return 0;};
391 
399  void ProcessMessages(int maxmsgs);
400 
405  void ProcessMessages(void);
406 
416  virtual int ProcessMessage(QueuePointer &resp_queue, player_msghdr * hdr,
417  void * data);
418 
420  virtual void Update()
421  {
422  this->ProcessMessages();
423  }
424 
432  virtual int ProcessInternalMessages(QueuePointer& resp_queue,
433  player_msghdr * hdr,
434  void * data);
435 
443  virtual bool RegisterProperty(const char *key, Property *prop, ConfigFile* cf, int section);
444 
452  virtual bool RegisterProperty(Property *prop, ConfigFile* cf, int section);
453 };
454 
455 typedef enum player_thread_state
456 {
457  PLAYER_THREAD_STATE_STOPPED,
458  PLAYER_THREAD_STATE_RUNNING,
459  PLAYER_THREAD_STATE_STOPPING,
460  PLAYER_THREAD_STATE_RESTARTING
461 } player_thread_state_t;
462 
463 class PLAYERCORE_EXPORT PlayerBarrier
464 {
465 public:
466  PlayerBarrier()
467  {
468  pthread_mutex_init(&barrierMutex,NULL);
469  pthread_cond_init(&barrierCond,NULL);
470  barrierValue = 0;
471  }
472  ~PlayerBarrier()
473  {
474  pthread_mutex_destroy(&barrierMutex);
475  pthread_cond_destroy(&barrierCond);
476  };
477 
478  int SetValue(int Value)
479  {
480  return barrierValue = Value;
481  };
482 
483  int Wait()
484  {
485  pthread_mutex_lock(&barrierMutex);
486  assert(barrierValue);
487  if (--barrierValue)
488  pthread_cond_wait(&barrierCond,&barrierMutex);
489  else
490  pthread_cond_broadcast(&barrierCond);
491  pthread_mutex_unlock(&barrierMutex);
492  return 0;
493  };
494 private:
497  pthread_mutex_t barrierMutex;
498 
499  int barrierValue;
500 
501  pthread_cond_t barrierCond;
502 
503 };
504 
505 
506 
544 class PLAYERCORE_EXPORT ThreadedDriver : public Driver
545 {
546  protected:
547 
548  /* @brief Start the driver thread
549 
550  This method is usually called from the overloaded Setup() method to
551  create the driver thread. This will call Main(). */
552  virtual void StartThread(void);
553 
558  virtual void StopThread(void);
559 
562  static void* DummyMain(void *driver);
563 
566  static void DummyMainQuit(void *driver);
567 
568  private:
572  pthread_t driverthread;
573 
576  player_thread_state_t ThreadState;
577  bool SetupSuccessful;
578 
581 
582  protected:
586  void TestCancel();
587 
588 
589  public:
590 
598  int section,
599  bool overwrite_cmds,
600  size_t queue_maxlen,
601  int interf);
602 
611  int section,
612  bool overwrite_cmds = true,
613  size_t queue_maxlen = PLAYER_MSGQUEUE_DEFAULT_MAXLEN);
614 
616  virtual ~ThreadedDriver();
617 
624  virtual int Setup();
625 
632  virtual int Shutdown();
633 
640  virtual int Terminate();
641 
647  virtual void Main(void) = 0;
648 
650  virtual int MainSetup(void) {return 0;};
651 
656  virtual void MainQuit(void) {};
657 
667  bool Wait(double TimeOut=0.0);
668 
669  virtual void Update()
670  {};
671 
672 };
673 
674 
675 #endif
Class for loading configuration file information.
Definition: configfile.h:196
Generic message header.
Definition: player.h:157
int GetError()
Get last error value.
Definition: driver.h:314
virtual void TestCancel()
enable thread cancellation and test for cancellation
Definition: driver.h:183
#define PLAYER_MSGQUEUE_DEFAULT_MAXLEN
Default maximum length for a message queue.
Definition: player.h:72
PlayerBarrier SetupBarrier
Barrier to synchronise threads on setup.
Definition: driver.h:580
virtual void Update()
Update non-threaded drivers.
Definition: driver.h:669
virtual bool Wait(double TimeOut=0.0)
Wait for new data to arrive on the driver&#39;s queue.
virtual int MainSetup(void)
Sets up the resources needed by the driver thread.
Definition: driver.h:650
virtual void Update()
Update non-threaded drivers.
Definition: driver.h:420
A device address.
Definition: player.h:141
virtual int Terminate()
Terminate the driver.
virtual int Subscribe(QueuePointer &queue, player_devaddr_t addr)
Subscribe to this driver.
Definition: driver.h:340
virtual int Shutdown()
Finalize the driver.
Definition: driver.h:390
Base class for drivers which oeprate with a thread.
Definition: driver.h:544
pthread_mutex_t subscriptionMutex
Mutex used to protect the subscription count for the driver.
Definition: driver.h:167
An autopointer for the message queue.
Definition: message.h:73
virtual void MainQuit(void)
Cleanup method for driver thread (called when main exits)
Definition: driver.h:656
virtual int Setup()
Initialize the driver.
Definition: driver.h:383
int entries
Total number of entries in the device table using this driver.
Definition: driver.h:270
bool alwayson
Always on flag.
Definition: driver.h:279
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
QueuePointer InQueue
Queue for all incoming messages for this driver.
Definition: driver.h:282
void SetError(int code)
Set/reset error code.
Definition: driver.h:142
virtual int Unsubscribe(QueuePointer &queue, player_devaddr_t addr)
Unsubscribe from this driver.
Definition: driver.h:366
player_thread_state_t ThreadState
TODO: insert state machine here
Definition: driver.h:576
Base class for all drivers.
Definition: driver.h:108
pthread_mutex_t barrierMutex
barrier to make sure StartThread doesnt return until cleanup handlers etc have been installed...
Definition: driver.h:493
Definition: driver.h:463
pthread_t driverthread
The driver&#39;s thread.
Definition: driver.h:572
Property base class.
Definition: property.h:59
int subscriptions
Number of subscriptions to this driver.
Definition: driver.h:118
Property bag class: stores registered properties.
Definition: property.h:207
player_devaddr_t device_addr
Default device address (single-interface drivers)
Definition: driver.h:266

Last updated 12 September 2005 21:38:45