cs101_queue.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "iec_include.h"
  2. /********************************************
  3. * CS101_Queue
  4. ********************************************/
  5. void
  6. CS101_Queue_initialize(CS101_Queue self, int maxQueueSize)
  7. {
  8. self->entryCounter = 0;
  9. self->firstMsgIndex = 0;
  10. self->lastMsgIndex = 0;
  11. self->size = maxQueueSize;
  12. BufferFrame_initialize(&(self->encodeFrame), NULL, 0);
  13. #if (CS101_MAX_QUEUE_SIZE == -1)
  14. int queueSize = maxQueueSize;
  15. if (maxQueueSize == -1)
  16. queueSize = 100;
  17. self->elements = (CS101_QueueElement) GLOBAL_CALLOC(queueSize, sizeof(struct sCS101_QueueElement));
  18. self->size = queueSize;
  19. #else
  20. self->size = CS101_MAX_QUEUE_SIZE;
  21. #endif
  22. #if (CONFIG_USE_SEMAPHORES == 1)
  23. // Semaphore_create(self->queueLock);
  24. self->queueLock = xSemaphoreCreateMutex();
  25. #endif
  26. }
  27. void
  28. CS101_Queue_dispose(CS101_Queue self)
  29. {
  30. #if (CONFIG_USE_SEMAPHORES == 1)
  31. platform_mutex_destroy(self->queueLock);
  32. #endif
  33. #if (CS101_MAX_QUEUE_SIZE == -1)
  34. GLOBAL_FREEMEM(self->elements);
  35. #endif
  36. }
  37. void
  38. CS101_Queue_lock(CS101_Queue self)
  39. {
  40. #if (CONFIG_USE_SEMAPHORES == 1)
  41. xSemaphoreTake(self->queueLock, portMAX_DELAY);
  42. #endif
  43. }
  44. void
  45. CS101_Queue_unlock(CS101_Queue self)
  46. {
  47. #if (CONFIG_USE_SEMAPHORES == 1)
  48. xSemaphoreGive(self->queueLock);
  49. #endif
  50. }
  51. void
  52. CS101_Queue_enqueue(CS101_Queue self, CS101_ASDU asdu)
  53. {
  54. // CS101_Queue_lock(self);
  55. xSemaphoreTake(self->queueLock, portMAX_DELAY);
  56. int nextIndex;
  57. bool removeEntry = false;
  58. if (self->entryCounter == 0) {
  59. self->firstMsgIndex = 0;
  60. nextIndex = 0;
  61. }
  62. else
  63. nextIndex = self->lastMsgIndex + 1;
  64. if (nextIndex == self->size)
  65. nextIndex = 0;
  66. if (self->entryCounter == self->size)
  67. removeEntry = true;
  68. if (removeEntry == false) {
  69. printf("add entry (nextIndex:%i)\n", nextIndex);
  70. self->lastMsgIndex = nextIndex;
  71. self->entryCounter++;
  72. }
  73. else {
  74. printf("add entry (nextIndex:%i) -> remove oldest\n", nextIndex);
  75. /* remove oldest entry */
  76. self->lastMsgIndex = nextIndex;
  77. int firstIndex = nextIndex + 1;
  78. if (firstIndex == self->size)
  79. firstIndex = 0;
  80. self->firstMsgIndex = firstIndex;
  81. }
  82. self->encodeFrame.buffer = self->elements[nextIndex].buffer;
  83. self->encodeFrame.startSize = 0;
  84. self->encodeFrame.msgSize = 0;
  85. CS101_ASDU_encode(asdu, (Frame)&(self->encodeFrame));
  86. int srcSize = self->encodeFrame.msgSize;
  87. self->elements[nextIndex].size = srcSize;
  88. printf("Events in FIFO: %i (first: %i, last: %i)\n", self->entryCounter,
  89. self->firstMsgIndex, self->lastMsgIndex);
  90. // CS101_Queue_unlock(self);
  91. xSemaphoreGive(self->queueLock);
  92. }
  93. Frame
  94. CS101_Queue_dequeue(CS101_Queue self, Frame resultStorage)
  95. {
  96. Frame frame = NULL;
  97. if (self->entryCounter != 0) {
  98. if (resultStorage) {
  99. frame = resultStorage;
  100. int currentIndex = self->firstMsgIndex;
  101. Frame_appendBytes(frame, self->elements[currentIndex].buffer, self->elements[currentIndex].size);
  102. self->firstMsgIndex = (currentIndex + 1) % self->size;
  103. self->entryCounter--;
  104. }
  105. }
  106. return frame;
  107. }
  108. void
  109. CS101_Queue_flush(CS101_Queue self)
  110. {
  111. xSemaphoreTake(self->queueLock, portMAX_DELAY);
  112. self->entryCounter = 0;
  113. self->firstMsgIndex = 0;
  114. self->lastMsgIndex = 0;
  115. xSemaphoreGive(self->queueLock);
  116. }
  117. bool
  118. CS101_Queue_isFull(CS101_Queue self)
  119. {
  120. return (self->entryCounter == self->size);
  121. }
  122. bool
  123. CS101_Queue_isEmpty(CS101_Queue self)
  124. {
  125. return (self->entryCounter == 0);
  126. }