wolfevent.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* wolfevent.c
  2. *
  3. * Copyright (C) 2006-2024 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef HAVE_WOLF_EVENT
  26. #include <wolfssl/internal.h>
  27. #include <wolfssl/error-ssl.h>
  28. #include <wolfssl/wolfcrypt/error-crypt.h>
  29. #include <wolfssl/wolfcrypt/wolfevent.h>
  30. int wolfEvent_Init(WOLF_EVENT* event, WOLF_EVENT_TYPE type, void* context)
  31. {
  32. if (event == NULL) {
  33. return BAD_FUNC_ARG;
  34. }
  35. if (event->state == WOLF_EVENT_STATE_PENDING) {
  36. WOLFSSL_MSG("Event already pending!");
  37. return BAD_COND_E;
  38. }
  39. XMEMSET(event, 0, sizeof(WOLF_EVENT));
  40. event->type = type;
  41. event->context = context;
  42. return 0;
  43. }
  44. int wolfEvent_Poll(WOLF_EVENT* event, WOLF_EVENT_FLAG flags)
  45. {
  46. int ret = WC_NO_ERR_TRACE(BAD_COND_E);
  47. /* Check hardware */
  48. #ifdef WOLFSSL_ASYNC_CRYPT
  49. if (event->type >= WOLF_EVENT_TYPE_ASYNC_FIRST &&
  50. event->type <= WOLF_EVENT_TYPE_ASYNC_LAST)
  51. {
  52. ret = wolfAsync_EventPoll(event, flags);
  53. }
  54. #endif /* WOLFSSL_ASYNC_CRYPT */
  55. return ret;
  56. }
  57. int wolfEventQueue_Init(WOLF_EVENT_QUEUE* queue)
  58. {
  59. int ret = 0;
  60. if (queue == NULL) {
  61. return BAD_FUNC_ARG;
  62. }
  63. XMEMSET(queue, 0, sizeof(WOLF_EVENT_QUEUE));
  64. #ifndef SINGLE_THREADED
  65. ret = wc_InitMutex(&queue->lock);
  66. #endif
  67. return ret;
  68. }
  69. int wolfEventQueue_Push(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event)
  70. {
  71. int ret;
  72. if (queue == NULL || event == NULL) {
  73. return BAD_FUNC_ARG;
  74. }
  75. #ifndef SINGLE_THREADED
  76. if ((ret = wc_LockMutex(&queue->lock)) != 0) {
  77. return ret;
  78. }
  79. #endif
  80. ret = wolfEventQueue_Add(queue, event);
  81. #ifndef SINGLE_THREADED
  82. wc_UnLockMutex(&queue->lock);
  83. #endif
  84. return ret;
  85. }
  86. int wolfEventQueue_Pop(WOLF_EVENT_QUEUE* queue, WOLF_EVENT** event)
  87. {
  88. int ret = 0;
  89. if (queue == NULL || event == NULL) {
  90. return BAD_FUNC_ARG;
  91. }
  92. #ifndef SINGLE_THREADED
  93. /* In single threaded mode "event_queue.lock" doesn't exist */
  94. if ((ret = wc_LockMutex(&queue->lock)) != 0) {
  95. return ret;
  96. }
  97. #endif
  98. /* Pop first item off queue */
  99. *event = queue->head;
  100. ret = wolfEventQueue_Remove(queue, *event);
  101. #ifndef SINGLE_THREADED
  102. wc_UnLockMutex(&queue->lock);
  103. #endif
  104. return ret;
  105. }
  106. /* assumes queue is locked by caller */
  107. int wolfEventQueue_Add(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event)
  108. {
  109. if (queue == NULL || event == NULL) {
  110. return BAD_FUNC_ARG;
  111. }
  112. event->next = NULL; /* added to end */
  113. event->prev = NULL;
  114. if (queue->tail == NULL) {
  115. queue->head = event;
  116. }
  117. else {
  118. queue->tail->next = event;
  119. event->prev = queue->tail;
  120. }
  121. queue->tail = event; /* add to the end either way */
  122. queue->count++;
  123. return 0;
  124. }
  125. /* assumes queue is locked by caller */
  126. int wolfEventQueue_Remove(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event)
  127. {
  128. int ret = 0;
  129. if (queue == NULL || event == NULL) {
  130. return BAD_FUNC_ARG;
  131. }
  132. if (event == queue->head && event == queue->tail) {
  133. queue->head = NULL;
  134. queue->tail = NULL;
  135. }
  136. else if (event == queue->head) {
  137. queue->head = event->next;
  138. queue->head->prev = NULL;
  139. }
  140. else if (event == queue->tail) {
  141. queue->tail = event->prev;
  142. queue->tail->next = NULL;
  143. }
  144. else {
  145. WOLF_EVENT* next = event->next;
  146. WOLF_EVENT* prev = event->prev;
  147. if ((next == NULL) || (prev == NULL)) {
  148. ret = BAD_STATE_E;
  149. } else {
  150. next->prev = prev;
  151. prev->next = next;
  152. }
  153. }
  154. queue->count--;
  155. return ret;
  156. }
  157. int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter,
  158. WOLF_EVENT** events, int maxEvents, WOLF_EVENT_FLAG flags, int* eventCount)
  159. {
  160. WOLF_EVENT* event;
  161. int ret = 0, count = 0;
  162. if (queue == NULL) {
  163. return BAD_FUNC_ARG;
  164. }
  165. #ifndef SINGLE_THREADED
  166. /* In single threaded mode "event_queue.lock" doesn't exist */
  167. if ((ret = wc_LockMutex(&queue->lock)) != 0) {
  168. return ret;
  169. }
  170. #endif
  171. /* iterate event queue */
  172. for (event = queue->head; event != NULL; event = event->next)
  173. {
  174. /* optional filter based on context */
  175. if (context_filter == NULL || event->context == context_filter) {
  176. /* poll event */
  177. ret = wolfEvent_Poll(event, flags);
  178. if (ret < 0) break; /* exit for */
  179. /* If event is done then process */
  180. if (event->state == WOLF_EVENT_STATE_DONE) {
  181. /* remove from queue */
  182. ret = wolfEventQueue_Remove(queue, event);
  183. if (ret < 0) break; /* exit for */
  184. /* return pointer in 'events' arg */
  185. if (events) {
  186. events[count] = event; /* return pointer */
  187. }
  188. count++;
  189. /* check to make sure our event list isn't full */
  190. if (events && count >= maxEvents) {
  191. break; /* exit for */
  192. }
  193. }
  194. }
  195. }
  196. #ifndef SINGLE_THREADED
  197. wc_UnLockMutex(&queue->lock);
  198. #endif
  199. /* return number of properly populated events */
  200. if (eventCount) {
  201. *eventCount = count;
  202. }
  203. return ret;
  204. }
  205. int wolfEventQueue_Count(WOLF_EVENT_QUEUE* queue)
  206. {
  207. int ret;
  208. if (queue == NULL) {
  209. return BAD_FUNC_ARG;
  210. }
  211. #ifndef SINGLE_THREADED
  212. /* In single threaded mode "event_queue.lock" doesn't exist */
  213. if ((ret = wc_LockMutex(&queue->lock)) != 0) {
  214. return ret;
  215. }
  216. #endif
  217. ret = queue->count;
  218. #ifndef SINGLE_THREADED
  219. wc_UnLockMutex(&queue->lock);
  220. #endif
  221. return ret;
  222. }
  223. void wolfEventQueue_Free(WOLF_EVENT_QUEUE* queue)
  224. {
  225. if (queue) {
  226. #ifndef SINGLE_THREADED
  227. wc_FreeMutex(&queue->lock);
  228. #endif
  229. }
  230. }
  231. #endif /* HAVE_WOLF_EVENT */