2 #include "wilc_oswrapper.h"
3 #include <linux/spinlock.h>
8 * @note copied from FLO glue implementatuion
11 WILC_ErrNo WILC_MsgQueueCreate(WILC_MsgQueueHandle *pHandle,
12 tstrWILC_MsgQueueAttrs *pstrAttrs)
14 spin_lock_init(&pHandle->strCriticalSection);
15 sema_init(&pHandle->hSem, 0);
16 pHandle->pstrMessageList = NULL;
17 pHandle->u32ReceiversCount = 0;
18 pHandle->bExiting = false;
25 * @note copied from FLO glue implementatuion
28 WILC_ErrNo WILC_MsgQueueDestroy(WILC_MsgQueueHandle *pHandle,
29 tstrWILC_MsgQueueAttrs *pstrAttrs)
32 pHandle->bExiting = true;
34 /* Release any waiting receiver thread. */
35 while (pHandle->u32ReceiversCount > 0) {
37 pHandle->u32ReceiversCount--;
40 while (pHandle->pstrMessageList != NULL) {
41 Message *pstrMessge = pHandle->pstrMessageList->pstrNext;
42 WILC_FREE(pHandle->pstrMessageList);
43 pHandle->pstrMessageList = pstrMessge;
52 * @note copied from FLO glue implementatuion
55 WILC_ErrNo WILC_MsgQueueSend(WILC_MsgQueueHandle *pHandle,
56 const void *pvSendBuffer, u32 u32SendBufferSize,
57 tstrWILC_MsgQueueAttrs *pstrAttrs)
59 WILC_ErrNo s32RetStatus = WILC_SUCCESS;
61 Message *pstrMessage = NULL;
63 if ((pHandle == NULL) || (u32SendBufferSize == 0) || (pvSendBuffer == NULL)) {
64 WILC_ERRORREPORT(s32RetStatus, WILC_INVALID_ARGUMENT);
67 if (pHandle->bExiting == true) {
68 WILC_ERRORREPORT(s32RetStatus, WILC_FAIL);
71 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
73 /* construct a new message */
74 pstrMessage = WILC_NEW(Message, 1);
75 WILC_NULLCHECK(s32RetStatus, pstrMessage);
76 pstrMessage->u32Length = u32SendBufferSize;
77 pstrMessage->pstrNext = NULL;
78 pstrMessage->pvBuffer = WILC_MALLOC(u32SendBufferSize);
79 WILC_NULLCHECK(s32RetStatus, pstrMessage->pvBuffer);
80 WILC_memcpy(pstrMessage->pvBuffer, pvSendBuffer, u32SendBufferSize);
83 /* add it to the message queue */
84 if (pHandle->pstrMessageList == NULL) {
85 pHandle->pstrMessageList = pstrMessage;
87 Message *pstrTailMsg = pHandle->pstrMessageList;
88 while (pstrTailMsg->pstrNext != NULL) {
89 pstrTailMsg = pstrTailMsg->pstrNext;
91 pstrTailMsg->pstrNext = pstrMessage;
94 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
98 WILC_CATCH(s32RetStatus)
100 /* error occured, free any allocations */
101 if (pstrMessage != NULL) {
102 if (pstrMessage->pvBuffer != NULL) {
103 WILC_FREE(pstrMessage->pvBuffer);
105 WILC_FREE(pstrMessage);
117 * @note copied from FLO glue implementatuion
120 WILC_ErrNo WILC_MsgQueueRecv(WILC_MsgQueueHandle *pHandle,
121 void *pvRecvBuffer, u32 u32RecvBufferSize,
122 u32 *pu32ReceivedLength,
123 tstrWILC_MsgQueueAttrs *pstrAttrs)
126 Message *pstrMessage;
127 WILC_ErrNo s32RetStatus = WILC_SUCCESS;
129 if ((pHandle == NULL) || (u32RecvBufferSize == 0)
130 || (pvRecvBuffer == NULL) || (pu32ReceivedLength == NULL)) {
131 WILC_ERRORREPORT(s32RetStatus, WILC_INVALID_ARGUMENT);
134 if (pHandle->bExiting == true) {
135 WILC_ERRORREPORT(s32RetStatus, WILC_FAIL);
138 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
139 pHandle->u32ReceiversCount++;
140 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
142 down(&(pHandle->hSem));
144 if (s32RetStatus == WILC_TIMEOUT) {
145 /* timed out, just exit without consumeing the message */
146 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
147 pHandle->u32ReceiversCount--;
148 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
150 /* other non-timeout scenarios */
151 WILC_ERRORCHECK(s32RetStatus);
153 if (pHandle->bExiting) {
154 WILC_ERRORREPORT(s32RetStatus, WILC_FAIL);
157 spin_lock_irqsave(&pHandle->strCriticalSection, flags);
159 pstrMessage = pHandle->pstrMessageList;
160 if (pstrMessage == NULL) {
161 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
162 WILC_ERRORREPORT(s32RetStatus, WILC_FAIL);
164 /* check buffer size */
165 if (u32RecvBufferSize < pstrMessage->u32Length) {
166 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
168 WILC_ERRORREPORT(s32RetStatus, WILC_BUFFER_OVERFLOW);
171 /* consume the message */
172 pHandle->u32ReceiversCount--;
173 WILC_memcpy(pvRecvBuffer, pstrMessage->pvBuffer, pstrMessage->u32Length);
174 *pu32ReceivedLength = pstrMessage->u32Length;
176 pHandle->pstrMessageList = pstrMessage->pstrNext;
178 WILC_FREE(pstrMessage->pvBuffer);
179 WILC_FREE(pstrMessage);
181 spin_unlock_irqrestore(&pHandle->strCriticalSection, flags);
185 WILC_CATCH(s32RetStatus)