| 1 | 30 | unneback | /*
 | 
      
         | 2 |  |  |   ------------------------------------------------------------------------
 | 
      
         | 3 |  |  |   $Id: rtemsMessageQueue.h,v 1.2 2001-09-27 12:02:05 chris Exp $
 | 
      
         | 4 |  |  |   ------------------------------------------------------------------------
 | 
      
         | 5 |  |  |  
 | 
      
         | 6 |  |  |   COPYRIGHT (c) 1997
 | 
      
         | 7 |  |  |   Objective Design Systems Ltd Pty (ODS)
 | 
      
         | 8 |  |  |   All rights reserved (R) Objective Design Systems Ltd Pty
 | 
      
         | 9 |  |  |  
 | 
      
         | 10 |  |  |   The license and distribution terms for this file may be found in the
 | 
      
         | 11 |  |  |   file LICENSE in this distribution or at
 | 
      
         | 12 |  |  |   http://www.OARcorp.com/rtems/license.html.
 | 
      
         | 13 |  |  |  
 | 
      
         | 14 |  |  |   ------------------------------------------------------------------------
 | 
      
         | 15 |  |  |  
 | 
      
         | 16 |  |  |   rtemsMessageQueue class.
 | 
      
         | 17 |  |  |  
 | 
      
         | 18 |  |  |   This class allows the user to create a RTEMS message queue, or to
 | 
      
         | 19 |  |  |   access and manage an already existing message queue.
 | 
      
         | 20 |  |  |  
 | 
      
         | 21 |  |  |   The first constructor with the message queue parameters creates a
 | 
      
         | 22 |  |  |   RTEMS message queue object. The destructor of this object also
 | 
      
         | 23 |  |  |   deletes the message queue object. The last status code should be
 | 
      
         | 24 |  |  |   checked after construction to see if the create completed
 | 
      
         | 25 |  |  |   successfully.
 | 
      
         | 26 |  |  |  
 | 
      
         | 27 |  |  |   The second constructor connects to an existing message queue
 | 
      
         | 28 |  |  |   object. The last status code should be checked after construction to
 | 
      
         | 29 |  |  |   see if the message queue existed.
 | 
      
         | 30 |  |  |  
 | 
      
         | 31 |  |  |   The third constructor is a copy constructor. Connects to an existing
 | 
      
         | 32 |  |  |   object which is in scope.
 | 
      
         | 33 |  |  |  
 | 
      
         | 34 |  |  |   The fourth constructor allows for the message queue to be created
 | 
      
         | 35 |  |  |   after construction, or to connect to a message queue later.
 | 
      
         | 36 |  |  |  
 | 
      
         | 37 |  |  |   ------------------------------------------------------------------------ */
 | 
      
         | 38 |  |  |  
 | 
      
         | 39 |  |  | #if !defined(_rtemsMessageQueue_h_)
 | 
      
         | 40 |  |  | #define _rtemsMessageQueue_h_
 | 
      
         | 41 |  |  |  
 | 
      
         | 42 |  |  | #include <rtems++/rtemsStatusCode.h>
 | 
      
         | 43 |  |  |  
 | 
      
         | 44 |  |  | /* ----
 | 
      
         | 45 |  |  |     rtemsMessageQueue
 | 
      
         | 46 |  |  | */
 | 
      
         | 47 |  |  |  
 | 
      
         | 48 |  |  | class rtemsMessageQueue
 | 
      
         | 49 |  |  |   : public rtemsStatusCode
 | 
      
         | 50 |  |  | {
 | 
      
         | 51 |  |  | public:
 | 
      
         | 52 |  |  |   // attribute a message queue can have
 | 
      
         | 53 |  |  |   enum WaitMode { wait_by_fifo = RTEMS_FIFO,
 | 
      
         | 54 |  |  |                   wait_by_priority = RTEMS_PRIORITY };
 | 
      
         | 55 |  |  |   enum Scope { local = RTEMS_LOCAL,
 | 
      
         | 56 |  |  |                global = RTEMS_GLOBAL };
 | 
      
         | 57 |  |  |  
 | 
      
         | 58 |  |  |   // only the first 4 characters of the name are taken  
 | 
      
         | 59 |  |  |  
 | 
      
         | 60 |  |  |   // creates a message queue
 | 
      
         | 61 |  |  |   rtemsMessageQueue(const char* name,
 | 
      
         | 62 |  |  |                     const rtems_unsigned32 count,
 | 
      
         | 63 |  |  |                     const rtems_unsigned32 max_message_size,
 | 
      
         | 64 |  |  |                     const WaitMode wait_mode = wait_by_fifo,
 | 
      
         | 65 |  |  |                     const Scope scope = local);
 | 
      
         | 66 |  |  |  
 | 
      
         | 67 |  |  |   // connects to a message queue
 | 
      
         | 68 |  |  |   rtemsMessageQueue(const char *name, const rtems_unsigned32 node = RTEMS_SEARCH_ALL_NODES);
 | 
      
         | 69 |  |  |  
 | 
      
         | 70 |  |  |   // copy and default constructors
 | 
      
         | 71 |  |  |   rtemsMessageQueue(const rtemsMessageQueue& message_queue);
 | 
      
         | 72 |  |  |   rtemsMessageQueue();
 | 
      
         | 73 |  |  |  
 | 
      
         | 74 |  |  |   // only the creator's destructor will delete the actual object
 | 
      
         | 75 |  |  |   virtual ~rtemsMessageQueue();
 | 
      
         | 76 |  |  |  
 | 
      
         | 77 |  |  |   // create or destroy (delete) the message queue
 | 
      
         | 78 |  |  |   virtual const rtems_status_code create(const char* name,
 | 
      
         | 79 |  |  |                                          const rtems_unsigned32 count,
 | 
      
         | 80 |  |  |                                          const rtems_unsigned32 max_message_size,
 | 
      
         | 81 |  |  |                                          const WaitMode wait_mode = wait_by_fifo,
 | 
      
         | 82 |  |  |                                          const Scope scope = local);
 | 
      
         | 83 |  |  |   virtual const rtems_status_code destroy();
 | 
      
         | 84 |  |  |  
 | 
      
         | 85 |  |  |   // connect to an existing message queue object, will not be the owner
 | 
      
         | 86 |  |  |   const rtemsMessageQueue& operator=(const rtemsMessageQueue& message_queue);
 | 
      
         | 87 |  |  |   virtual const rtems_status_code connect(const char *name,
 | 
      
         | 88 |  |  |                                           const rtems_unsigned32 node = RTEMS_SEARCH_ALL_NODES);
 | 
      
         | 89 |  |  |  
 | 
      
         | 90 |  |  |   // send a message of size from the buffer
 | 
      
         | 91 |  |  |   inline const rtems_status_code send(const void *buffer,
 | 
      
         | 92 |  |  |                                       const rtems_unsigned32 size);
 | 
      
         | 93 |  |  |   inline const rtems_status_code urgent(const void *buffer,
 | 
      
         | 94 |  |  |                                         const rtems_unsigned32 size);
 | 
      
         | 95 |  |  |   inline const rtems_status_code broadcast(const void *buffer,
 | 
      
         | 96 |  |  |                                            const rtems_unsigned32 size,
 | 
      
         | 97 |  |  |                                            rtems_unsigned32& count);
 | 
      
         | 98 |  |  |  
 | 
      
         | 99 |  |  |   // receive a message of size, the timeout is in micro-secs
 | 
      
         | 100 |  |  |   inline const rtems_status_code receive(const void *buffer,
 | 
      
         | 101 |  |  |                                          rtems_unsigned32& size,
 | 
      
         | 102 |  |  |                                          rtems_interval micro_secs = RTEMS_NO_TIMEOUT,
 | 
      
         | 103 |  |  |                                          bool wait = true);
 | 
      
         | 104 |  |  |  
 | 
      
         | 105 |  |  |   // flush a message queue, returning the number of messages dropped
 | 
      
         | 106 |  |  |   inline const rtems_status_code flush(rtems_unsigned32& size);
 | 
      
         | 107 |  |  |  
 | 
      
         | 108 |  |  |   // object id, and name
 | 
      
         | 109 |  |  |   const rtems_id id_is() const { return id; }
 | 
      
         | 110 |  |  |   const rtems_name name_is() const { return name; }
 | 
      
         | 111 |  |  |   const char *name_string() const { return name_str; }
 | 
      
         | 112 |  |  |  
 | 
      
         | 113 |  |  | private:
 | 
      
         | 114 |  |  |  
 | 
      
         | 115 |  |  |   // make this object reference an invalid RTEMS object
 | 
      
         | 116 |  |  |   void make_invalid();
 | 
      
         | 117 |  |  |  
 | 
      
         | 118 |  |  |   // message queue name
 | 
      
         | 119 |  |  |   rtems_name name;
 | 
      
         | 120 |  |  |   char name_str[5];
 | 
      
         | 121 |  |  |  
 | 
      
         | 122 |  |  |   // owner, true if this object owns the message queue
 | 
      
         | 123 |  |  |   // will delete the message queue when it destructs
 | 
      
         | 124 |  |  |   bool owner;
 | 
      
         | 125 |  |  |  
 | 
      
         | 126 |  |  |   // the rtems id, object handle
 | 
      
         | 127 |  |  |   rtems_id id;
 | 
      
         | 128 |  |  | };
 | 
      
         | 129 |  |  |  
 | 
      
         | 130 |  |  | const rtems_status_code rtemsMessageQueue::send(const void *buffer,
 | 
      
         | 131 |  |  |                                                 const rtems_unsigned32 size)
 | 
      
         | 132 |  |  | {
 | 
      
         | 133 |  |  |   return set_status_code(rtems_message_queue_send(id, (void*) buffer, size));
 | 
      
         | 134 |  |  | }
 | 
      
         | 135 |  |  |  
 | 
      
         | 136 |  |  | const rtems_status_code rtemsMessageQueue::urgent(const void *buffer,
 | 
      
         | 137 |  |  |                                                   const rtems_unsigned32 size)
 | 
      
         | 138 |  |  | {
 | 
      
         | 139 |  |  |   return set_status_code(rtems_message_queue_urgent(id, (void*) buffer, size));
 | 
      
         | 140 |  |  | }
 | 
      
         | 141 |  |  |  
 | 
      
         | 142 |  |  | const rtems_status_code rtemsMessageQueue::broadcast(const void *buffer,
 | 
      
         | 143 |  |  |                                                      const rtems_unsigned32 size,
 | 
      
         | 144 |  |  |                                                      rtems_unsigned32& count)
 | 
      
         | 145 |  |  | {
 | 
      
         | 146 |  |  |   return set_status_code(rtems_message_queue_broadcast(id,
 | 
      
         | 147 |  |  |                                                        (void*) buffer,
 | 
      
         | 148 |  |  |                                                        size,
 | 
      
         | 149 |  |  |                                                        &count));
 | 
      
         | 150 |  |  | }
 | 
      
         | 151 |  |  |  
 | 
      
         | 152 |  |  | const rtems_status_code rtemsMessageQueue::receive(const void *buffer,
 | 
      
         | 153 |  |  |                                                    rtems_unsigned32& size,
 | 
      
         | 154 |  |  |                                                    rtems_interval micro_secs,
 | 
      
         | 155 |  |  |                                                    bool wait)
 | 
      
         | 156 |  |  | {
 | 
      
         | 157 |  |  |   rtems_interval usecs =
 | 
      
         | 158 |  |  |     micro_secs && (micro_secs < _TOD_Microseconds_per_tick) ?
 | 
      
         | 159 |  |  |     _TOD_Microseconds_per_tick : micro_secs;
 | 
      
         | 160 |  |  |   return set_status_code(rtems_message_queue_receive(id,
 | 
      
         | 161 |  |  |                                                      (void*) buffer,
 | 
      
         | 162 |  |  |                                                      &size,
 | 
      
         | 163 |  |  |                                                      wait ? RTEMS_WAIT : RTEMS_NO_WAIT,
 | 
      
         | 164 |  |  |                                                      TOD_MICROSECONDS_TO_TICKS(usecs)));
 | 
      
         | 165 |  |  | }
 | 
      
         | 166 |  |  |  
 | 
      
         | 167 |  |  | const rtems_status_code rtemsMessageQueue::flush(rtems_unsigned32& count)
 | 
      
         | 168 |  |  | {
 | 
      
         | 169 |  |  |   return set_status_code(rtems_message_queue_flush(id, &count));
 | 
      
         | 170 |  |  | }
 | 
      
         | 171 |  |  |  
 | 
      
         | 172 |  |  | #endif  // _rtemsMessageQueue_h_
 | 
      
         | 173 |  |  |  
 | 
      
         | 174 |  |  |  
 | 
      
         | 175 |  |  |  
 | 
      
         | 176 |  |  |  
 |