packet_queue.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. MSP430 Emulator
  3. Copyright (C) 2020 Rudolf Geosits (rgeosits@live.esu.edu)
  4. "MSP430 Emulator" is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. "MSP430 Emulator" is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "packet_queue.h"
  16. void init_packet_queue (Emulator *emu)
  17. {
  18. Server *s = emu->debugger->server;
  19. s->pending_packets_head = NULL;
  20. s->pending_packets_tail = NULL;
  21. s->packets_queued = 0;
  22. s->spin_lock = false;
  23. }
  24. void destroy_packet_queue (Emulator *emu)
  25. {
  26. }
  27. bool packet_queue_full (Emulator *emu)
  28. {
  29. Server *s = emu->debugger->server;
  30. return false;
  31. }
  32. bool packet_queue_empty (Emulator *emu)
  33. {
  34. Server *s = emu->debugger->server;
  35. if (s->packets_queued > 0 && s->pending_packets_head != NULL) {
  36. return false;
  37. }
  38. else {
  39. return true;
  40. }
  41. }
  42. void packet_enqueue (Emulator *emu, void *item, size_t size,
  43. uint8_t opcode)
  44. {
  45. Server *s = emu->debugger->server;
  46. Packet *head, *tail, *cur;
  47. void *heap_item;
  48. while (s->spin_lock);
  49. s->spin_lock = true;
  50. if (s->pending_packets_head == NULL) {
  51. head = tail = s->pending_packets_head = s->pending_packets_tail =
  52. (Packet *) calloc(1, sizeof(Packet));
  53. // copy item data onto the heap so it won't go out of scope
  54. heap_item = calloc(1, size);
  55. memcpy(heap_item, (const void *)item, size);
  56. head->message = heap_item;
  57. head->length = size;
  58. head->opcode = opcode;
  59. head->next = NULL;
  60. }
  61. else {
  62. //printf("IN ELSE OF PACKET_ENQUEUE\n");
  63. cur = s->pending_packets_head;
  64. while (cur->next != NULL) {
  65. cur = cur->next;
  66. }
  67. cur->next = tail = (Packet *) calloc(1, sizeof(Packet));
  68. // copy item data onto the heap so it won't go out of scope
  69. heap_item = calloc(1, size);
  70. memcpy(heap_item, (const void *)item, size);
  71. cur->next->message = heap_item;
  72. cur->next->length = size;
  73. cur->next->opcode = opcode;
  74. cur->next->next = NULL;
  75. }
  76. s->packets_queued++;
  77. s->spin_lock = false;
  78. return;
  79. }
  80. void print_packet_queue (Emulator *emu)
  81. {
  82. Server *s = emu->debugger->server;
  83. Packet *cur = s->pending_packets_head;
  84. uint32_t packet = 0;
  85. printf("packets: %u\n", s->packets_queued);
  86. while (cur != NULL) {
  87. printf("I have %s, len %u, PACKET #%u\n",
  88. (char *)cur->message, (unsigned int)cur->length, packet++);
  89. cur = cur->next;
  90. }
  91. }
  92. Packet packet_dequeue (Emulator *emu)
  93. {
  94. Server *s = emu->debugger->server;
  95. Packet *head, *tail, *saved, ret;
  96. while (s->spin_lock);
  97. s->spin_lock = true;
  98. //printf("Count:%d\n", s->packets_queued);
  99. //printf("in packet_dequeue(), head = s->pending_packets_head = %p\n",
  100. //s->pending_packets_head);
  101. head = s->pending_packets_head;
  102. //printf("head = %p\n", head);
  103. if ( packet_queue_empty(emu) ) {
  104. printf("EMPTY QUEUE...\n");
  105. return ret;
  106. }
  107. s->pending_packets_head = head->next;
  108. //if (s->packets_queued == 2) {
  109. //sleep(2);
  110. //}
  111. ret.message = head->message;
  112. ret.length = head->length;
  113. ret.opcode = head->opcode;
  114. ret.next = NULL;
  115. //free(head->message);
  116. free(head);
  117. s->packets_queued--;
  118. s->spin_lock = false;
  119. return ret;
  120. }