mqtt_ringbuffer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef MQTT_RINGBUFFER_H
  2. #define MQTT_RINGBUFFER_H
  3. #include <pthread.h>
  4. /** * The size of a ring buffer.
  5. * Due to the design only <tt> RING_BUFFER_SIZE-1 </tt> items
  6. * can be contained in the buffer.
  7. * The buffer size must be a power of two.
  8. */
  9. #define MQTT_RING_BUFFER_SIZE 0x100
  10. #if (MQTT_RING_BUFFER_SIZE & (MQTT_RING_BUFFER_SIZE - 1)) != 0
  11. #error "RING_BUFFER_SIZE must be a power of two"
  12. #endif
  13. /**
  14. * Used as a modulo operator
  15. * as <tt> a % b = (a & (b ? 1)) </tt>
  16. * where \c a is a positive index in the buffer and
  17. * \c b is the (power of two) size of the buffer.
  18. */
  19. #define MQTT_RING_BUFFER_MASK (MQTT_RING_BUFFER_SIZE-1)
  20. /** * The type which is used to hold the size
  21. * and the indicies of the buffer.
  22. * Must be able to fit \c RING_BUFFER_SIZE .
  23. */
  24. //typedef uint8_t ring_buffer_size_t;
  25. typedef int mqtt_ringbuffer_size_t;
  26. typedef struct tag_mqtt_ringbuffer_element{
  27. int idx;
  28. int cmd;
  29. int val;
  30. char sztopic[128];
  31. char szpayload[8000];
  32. }mqtt_ringbuffer_element_t;
  33. /**
  34. * Simplifies the use of <tt>struct ring_buffer_t</tt>.
  35. */
  36. typedef struct mqtt_ringbuffer_t mqtt_ringbuffer_t;
  37. /**
  38. * Structure which holds a ring buffer.
  39. * The buffer contains a buffer array
  40. * as well as metadata for the ring buffer.
  41. */
  42. struct mqtt_ringbuffer_t {
  43. /** Buffer memory. */
  44. mqtt_ringbuffer_element_t buffer[MQTT_RING_BUFFER_SIZE];
  45. /** Index of tail. */
  46. mqtt_ringbuffer_size_t tail_index;
  47. /** Index of head. */
  48. mqtt_ringbuffer_size_t head_index;
  49. };
  50. void mqtt_ringbuffer_init(mqtt_ringbuffer_t *buffer);
  51. void mqtt_ringbuffer_queue(mqtt_ringbuffer_t *buffer, mqtt_ringbuffer_element_t data);
  52. void mqtt_ringbuffer_queue_arr(mqtt_ringbuffer_t *buffer, const mqtt_ringbuffer_element_t*data, mqtt_ringbuffer_size_t size);
  53. mqtt_ringbuffer_size_t mqtt_ringbuffer_dequeue(mqtt_ringbuffer_t *buffer, mqtt_ringbuffer_element_t* data);
  54. mqtt_ringbuffer_size_t mqtt_ringbuffer_dequeue_arr(mqtt_ringbuffer_t *buffer, mqtt_ringbuffer_element_t* data, mqtt_ringbuffer_size_t len);
  55. mqtt_ringbuffer_size_t mqtt_ringbuffer_peek(mqtt_ringbuffer_t *buffer, mqtt_ringbuffer_element_t* data, mqtt_ringbuffer_size_t index);
  56. mqtt_ringbuffer_size_t mqtt_ringbuffer_num_items(mqtt_ringbuffer_t *buffer);
  57. mqtt_ringbuffer_size_t mqtt_ringbuffer_size(mqtt_ringbuffer_t *buffer);
  58. mqtt_ringbuffer_size_t mqtt_ringbuffer_is_empty(mqtt_ringbuffer_t *buffer);
  59. mqtt_ringbuffer_size_t mqtt_ringbuffer_is_full(mqtt_ringbuffer_t *buffer);
  60. #endif /* MQTT_RINGBUFFER_H */