blob: 639f548a17564cb0a37ab28b7a3e632d67ea9c99 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* wolfevent.h
2 *
3 * Copyright (C) 2006-2021 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
22#ifndef _WOLF_EVENT_H_
23#define _WOLF_EVENT_H_
24
25#ifdef __cplusplus
26 extern "C" {
27#endif
28
29#ifndef SINGLE_THREADED
30 #include <wolfssl/wolfcrypt/wc_port.h>
31#endif
32
33typedef struct WOLF_EVENT WOLF_EVENT;
34typedef unsigned short WOLF_EVENT_FLAG;
35
36typedef enum WOLF_EVENT_TYPE {
37 WOLF_EVENT_TYPE_NONE,
38#ifdef WOLFSSL_ASYNC_CRYPT
39 WOLF_EVENT_TYPE_ASYNC_WOLFSSL, /* context is WOLFSSL* */
40 WOLF_EVENT_TYPE_ASYNC_WOLFCRYPT, /* context is WC_ASYNC_DEV */
41 WOLF_EVENT_TYPE_ASYNC_FIRST = WOLF_EVENT_TYPE_ASYNC_WOLFSSL,
42 WOLF_EVENT_TYPE_ASYNC_LAST = WOLF_EVENT_TYPE_ASYNC_WOLFCRYPT,
43#endif /* WOLFSSL_ASYNC_CRYPT */
44} WOLF_EVENT_TYPE;
45
46typedef enum WOLF_EVENT_STATE {
47 WOLF_EVENT_STATE_READY,
48 WOLF_EVENT_STATE_PENDING,
49 WOLF_EVENT_STATE_DONE,
50} WOLF_EVENT_STATE;
51
52struct WOLF_EVENT {
53 /* double linked list */
54 WOLF_EVENT* next;
55 WOLF_EVENT* prev;
56
57 void* context;
58 union {
59 void* ptr;
60#ifdef WOLFSSL_ASYNC_CRYPT
61 struct WC_ASYNC_DEV* async;
62#endif
63 } dev;
64#ifdef HAVE_CAVIUM
65 word64 reqId;
66 #ifdef WOLFSSL_NITROX_DEBUG
67 word32 pendCount;
68 #endif
69#endif
70#ifndef WC_NO_ASYNC_THREADING
71 pthread_t threadId;
72#endif
73 int ret; /* Async return code */
74 unsigned int flags;
75 WOLF_EVENT_TYPE type;
76 WOLF_EVENT_STATE state;
77};
78
79enum WOLF_POLL_FLAGS {
80 WOLF_POLL_FLAG_CHECK_HW = 0x01,
81};
82
83typedef struct {
84 WOLF_EVENT* head; /* head of queue */
85 WOLF_EVENT* tail; /* tail of queue */
86#ifndef SINGLE_THREADED
87 wolfSSL_Mutex lock; /* queue lock */
88#endif
89 int count;
90} WOLF_EVENT_QUEUE;
91
92
93#ifdef HAVE_WOLF_EVENT
94
95/* Event */
96WOLFSSL_API int wolfEvent_Init(WOLF_EVENT* event, WOLF_EVENT_TYPE type, void* context);
97WOLFSSL_API int wolfEvent_Poll(WOLF_EVENT* event, WOLF_EVENT_FLAG flags);
98
99/* Event Queue */
100WOLFSSL_API int wolfEventQueue_Init(WOLF_EVENT_QUEUE* queue);
101WOLFSSL_API int wolfEventQueue_Push(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event);
102WOLFSSL_API int wolfEventQueue_Pop(WOLF_EVENT_QUEUE* queue, WOLF_EVENT** event);
103WOLFSSL_API int wolfEventQueue_Poll(WOLF_EVENT_QUEUE* queue, void* context_filter,
104 WOLF_EVENT** events, int maxEvents, WOLF_EVENT_FLAG flags, int* eventCount);
105WOLFSSL_API int wolfEventQueue_Count(WOLF_EVENT_QUEUE* queue);
106WOLFSSL_API void wolfEventQueue_Free(WOLF_EVENT_QUEUE* queue);
107
108/* the queue mutex must be locked prior to calling these */
109WOLFSSL_API int wolfEventQueue_Add(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event);
110WOLFSSL_API int wolfEventQueue_Remove(WOLF_EVENT_QUEUE* queue, WOLF_EVENT* event);
111
112
113#endif /* HAVE_WOLF_EVENT */
114
115
116#ifdef __cplusplus
117 } /* extern "C" */
118#endif
119
120#endif /* _WOLF_EVENT_H_ */