Queue.hpp 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Queue.hpp:
  3. *
  4. * Queues are used internally to F prime in order to support the messaging between components. The
  5. * Queue class is used to abstract away from the standard OS-based queue, allowing F prime support
  6. * multiple OSes in a consistent way.
  7. *
  8. * Like most items in the OS package, the implementation is done in two parts. One part is the file
  9. * `QueueCommon.cpp`. It contains the shared code for queues regardless of the OS. The other is a
  10. * .cpp file containing the OS specific backends for defined functions. (i.e. Posix/Queue.cpp).
  11. */
  12. #ifndef _Queue_hpp_
  13. #define _Queue_hpp_
  14. #include <FpConfig.hpp>
  15. #include <Fw/Obj/ObjBase.hpp>
  16. #include <Fw/Types/Serializable.hpp>
  17. #include <Os/QueueString.hpp>
  18. namespace Os {
  19. // forward declaration for registry
  20. class QueueRegistry;
  21. class Queue {
  22. public:
  23. enum QueueStatus {
  24. QUEUE_OK, //!< message sent/received okay
  25. QUEUE_NO_MORE_MSGS, //!< If non-blocking, all the messages have been drained.
  26. QUEUE_UNINITIALIZED, //!< Queue wasn't initialized successfully
  27. QUEUE_SIZE_MISMATCH, //!< attempted to send or receive with buffer too large, too small
  28. QUEUE_SEND_ERROR, //!< message send error
  29. QUEUE_RECEIVE_ERROR, //!< message receive error
  30. QUEUE_INVALID_PRIORITY, //!< invalid priority requested
  31. QUEUE_EMPTY_BUFFER, //!< supplied buffer is empty
  32. QUEUE_FULL, //!< queue was full when attempting to send a message
  33. QUEUE_UNKNOWN_ERROR //!< Unexpected error; can't match with returns
  34. };
  35. enum QueueBlocking {
  36. QUEUE_BLOCKING, //!< Queue receive blocks until a message arrives
  37. QUEUE_NONBLOCKING //!< Queue receive always returns even if there is no message
  38. };
  39. Queue();
  40. virtual ~Queue();
  41. QueueStatus create(const Fw::StringBase &name, NATIVE_INT_TYPE depth, NATIVE_INT_TYPE msgSize); //!< create a message queue
  42. // Send serialized buffers
  43. QueueStatus send(const Fw::SerializeBufferBase &buffer, NATIVE_INT_TYPE priority, QueueBlocking block); //!< send a message
  44. QueueStatus receive(Fw::SerializeBufferBase &buffer, NATIVE_INT_TYPE &priority, QueueBlocking block); //!< receive a message
  45. // Send raw buffers
  46. QueueStatus send(const U8* buffer, NATIVE_INT_TYPE size, NATIVE_INT_TYPE priority, QueueBlocking block); //!< send a message
  47. QueueStatus receive(U8* buffer, NATIVE_INT_TYPE capacity, NATIVE_INT_TYPE &actualSize, NATIVE_INT_TYPE &priority, QueueBlocking block); //!< receive a message
  48. NATIVE_INT_TYPE getNumMsgs() const; //!< get the number of messages in the queue
  49. NATIVE_INT_TYPE getMaxMsgs() const; //!< get the maximum number of messages (high watermark)
  50. NATIVE_INT_TYPE getQueueSize() const; //!< get the queue depth (maximum number of messages queue can hold)
  51. NATIVE_INT_TYPE getMsgSize() const; //!< get the message size (maximum message size queue can hold)
  52. const QueueString& getName(); //!< get the queue name
  53. static NATIVE_INT_TYPE getNumQueues(); //!< get the number of queues in the system
  54. #if FW_QUEUE_REGISTRATION
  55. static void setQueueRegistry(QueueRegistry* reg); // !< set the queue registry
  56. #endif
  57. protected:
  58. //! Internal method used for creating allowing alternate implementations to implement different creation
  59. //! behavior without needing to duplicate the majority of the creation functionality.
  60. //! \param name: name of queue
  61. //! \param depth: depth of queue
  62. //! \param msgSize: size of a message stored on queue
  63. //! \return queue creation status
  64. QueueStatus createInternal(const Fw::StringBase &name, NATIVE_INT_TYPE depth, NATIVE_INT_TYPE msgSize); //!< create a message queue
  65. POINTER_CAST m_handle; //!< handle for implementation specific queue
  66. QueueString m_name; //!< queue name
  67. #if FW_QUEUE_REGISTRATION
  68. static QueueRegistry* s_queueRegistry; //!< pointer to registry
  69. #endif
  70. static NATIVE_INT_TYPE s_numQueues; //!< tracks number of queues in the system
  71. private:
  72. Queue(Queue&); //!< Disabled copy constructor
  73. Queue(Queue*); //!< Disabled copy constructor
  74. };
  75. class QueueRegistry {
  76. public:
  77. virtual void regQueue(Queue* obj)=0; //!< method called by queue init() methods to register a new queue
  78. virtual ~QueueRegistry() {}; //!< virtual destructor for registry object
  79. };
  80. }
  81. #endif