socketfifo.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include <string.h>
  29. #include "osi.h"
  30. #include "fifo.h"
  31. #include "socketfifo.h"
  32. /*----------------------------------------------------------------------------
  33. ** Declare private functions
  34. */
  35. static void socketfifo_Push (void * const pvFifo, const void * const pvElement);
  36. static void socketfifo_Pop (void * const pvFifo, void * const pvElement);
  37. /*----------------------------------------------------------------------------
  38. ** Declare private data
  39. */
  40. static FIFO_t *socketfifo;
  41. /*----------------------------------------------------------------------------
  42. ** Define public functions
  43. */
  44. void SOCKETFIFO_Init (FIFO_t *fifo, void *elements, uint32_t maxcount) {
  45. // Initialize global data
  46. socketfifo = fifo;
  47. socketfifo->pvElements = elements;
  48. FIFO_Init (socketfifo, maxcount, socketfifo_Push, socketfifo_Pop);
  49. }
  50. bool SOCKETFIFO_Push (const void * const element) {
  51. return FIFO_bPushElement (socketfifo, element);
  52. }
  53. bool SOCKETFIFO_Pop (void * const element) {
  54. return FIFO_bPopElement (socketfifo, element);
  55. }
  56. bool SOCKETFIFO_Peek (void * const element) {
  57. return FIFO_bPeekElement (socketfifo, element);
  58. }
  59. bool SOCKETFIFO_IsEmpty (void) {
  60. return FIFO_IsEmpty (socketfifo);
  61. }
  62. bool SOCKETFIFO_IsFull (void) {
  63. return FIFO_IsFull (socketfifo);
  64. }
  65. void SOCKETFIFO_Flush (void) {
  66. SocketFifoElement_t element;
  67. while (SOCKETFIFO_Pop(&element)) {
  68. if (element.freedata) {
  69. mem_Free(element.data);
  70. }
  71. }
  72. }
  73. unsigned int SOCKETFIFO_Count (void) {
  74. return socketfifo->uiElementCount;
  75. }
  76. /*----------------------------------------------------------------------------
  77. ** Define private functions
  78. */
  79. static void socketfifo_Push (void * const pvFifo, const void * const pvElement) {
  80. if ((pvFifo != NULL) && (NULL != pvElement)) {
  81. unsigned int uiLast = ((FIFO_t *)pvFifo)->uiLast;
  82. memcpy (&((SocketFifoElement_t *)((FIFO_t *)pvFifo)->pvElements)[uiLast], pvElement, sizeof(SocketFifoElement_t));
  83. }
  84. }
  85. static void socketfifo_Pop (void * const pvFifo, void * const pvElement) {
  86. if ((pvFifo != NULL) && (NULL != pvElement)) {
  87. unsigned int uiFirst = ((FIFO_t *)pvFifo)->uiFirst;
  88. memcpy (pvElement, &((SocketFifoElement_t *)((FIFO_t *)pvFifo)->pvElements)[uiFirst], sizeof(SocketFifoElement_t));
  89. }
  90. }