wolfevent.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* wolfevent.c
  2. *
  3. * Copyright (C) 2006-2022 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 = 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. next->prev = prev;
  148. prev->next = next;
  149. }
  150. queue->count--;
  151. return ret;
  152. }
  153. int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter,
  154. WOLF_EVENT** events, int maxEvents, WOLF_EVENT_FLAG flags, int* eventCount)
  155. {
  156. WOLF_EVENT* event;
  157. int ret = 0, count = 0;
  158. if (queue == NULL) {
  159. return BAD_FUNC_ARG;
  160. }
  161. #ifndef SINGLE_THREADED
  162. /* In single threaded mode "event_queue.lock" doesn't exist */
  163. if ((ret = wc_LockMutex(&queue->lock)) != 0) {
  164. return ret;
  165. }
  166. #endif
  167. /* iterrate event queue */
  168. for (event = queue->head; event != NULL; event = event->next)
  169. {
  170. /* optional filter based on context */
  171. if (context_filter == NULL || event->context == context_filter) {
  172. /* poll event */
  173. ret = wolfEvent_Poll(event, flags);
  174. if (ret < 0) break; /* exit for */
  175. /* If event is done then process */
  176. if (event->state == WOLF_EVENT_STATE_DONE) {
  177. /* remove from queue */
  178. ret = wolfEventQueue_Remove(queue, event);
  179. if (ret < 0) break; /* exit for */
  180. /* return pointer in 'events' arg */
  181. if (events) {
  182. events[count] = event; /* return pointer */
  183. }
  184. count++;
  185. /* check to make sure our event list isn't full */
  186. if (events && count >= maxEvents) {
  187. break; /* exit for */
  188. }
  189. }
  190. }
  191. }
  192. #ifndef SINGLE_THREADED
  193. wc_UnLockMutex(&queue->lock);
  194. #endif
  195. /* return number of properly populated events */
  196. if (eventCount) {
  197. *eventCount = count;
  198. }
  199. return ret;
  200. }
  201. int wolfEventQueue_Count(WOLF_EVENT_QUEUE* queue)
  202. {
  203. int ret;
  204. if (queue == NULL) {
  205. return BAD_FUNC_ARG;
  206. }
  207. #ifndef SINGLE_THREADED
  208. /* In single threaded mode "event_queue.lock" doesn't exist */
  209. if ((ret = wc_LockMutex(&queue->lock)) != 0) {
  210. return ret;
  211. }
  212. #endif
  213. ret = queue->count;
  214. #ifndef SINGLE_THREADED
  215. wc_UnLockMutex(&queue->lock);
  216. #endif
  217. return ret;
  218. }
  219. void wolfEventQueue_Free(WOLF_EVENT_QUEUE* queue)
  220. {
  221. if (queue) {
  222. #ifndef SINGLE_THREADED
  223. wc_FreeMutex(&queue->lock);
  224. #endif
  225. }
  226. }
  227. #endif /* HAVE_WOLF_EVENT */