dataqueue.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file dataqueue.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef DATAQUEUE_H__
  35. #define DATAQUEUE_H__
  36. #include <rtthread.h>
  37. #define RT_DATAQUEUE_EVENT_UNKNOWN 0x00
  38. #define RT_DATAQUEUE_EVENT_POP 0x01
  39. #define RT_DATAQUEUE_EVENT_PUSH 0x02
  40. #define RT_DATAQUEUE_EVENT_LWM 0x03
  41. struct rt_data_item;
  42. #define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
  43. #define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
  44. /* data queue implementation */
  45. struct rt_data_queue
  46. {
  47. rt_uint16_t size;
  48. rt_uint16_t lwm;
  49. rt_bool_t waiting_lwm;
  50. rt_uint16_t get_index;
  51. rt_uint16_t put_index;
  52. struct rt_data_item *queue;
  53. rt_list_t suspended_push_list;
  54. rt_list_t suspended_pop_list;
  55. /* event notify */
  56. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
  57. };
  58. /**
  59. * DataQueue for DeviceDriver
  60. */
  61. rt_err_t rt_data_queue_init(struct rt_data_queue *queue,
  62. rt_uint16_t size,
  63. rt_uint16_t lwm,
  64. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event));
  65. rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
  66. const void *data_ptr,
  67. rt_size_t data_size,
  68. rt_int32_t timeout);
  69. rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
  70. const void **data_ptr,
  71. rt_size_t *size,
  72. rt_int32_t timeout);
  73. rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
  74. const void **data_ptr,
  75. rt_size_t *size);
  76. void rt_data_queue_reset(struct rt_data_queue *queue);
  77. #endif