]>
Commit | Line | Data |
---|---|---|
c5c77ba1 JK |
1 | |
2 | ||
c5c77ba1 JK |
3 | #include "fifo_buffer.h" |
4 | ||
5 | ||
6 | ||
4e4467fd | 7 | u32 FIFO_InitBuffer(tHANDLE *hBuffer, u32 u32BufferLength) |
c5c77ba1 | 8 | { |
4e4467fd | 9 | u32 u32Error = 0; |
c5c77ba1 JK |
10 | tstrFifoHandler *pstrFifoHandler = WILC_MALLOC (sizeof (tstrFifoHandler)); |
11 | if (pstrFifoHandler) { | |
12 | WILC_memset (pstrFifoHandler, 0, sizeof (tstrFifoHandler)); | |
13 | pstrFifoHandler->pu8Buffer = WILC_MALLOC (u32BufferLength); | |
14 | if (pstrFifoHandler->pu8Buffer) { | |
c5c77ba1 JK |
15 | pstrFifoHandler->u32BufferLength = u32BufferLength; |
16 | WILC_memset (pstrFifoHandler->pu8Buffer, 0, u32BufferLength); | |
17 | /* create semaphore */ | |
83383ea3 | 18 | sema_init(&pstrFifoHandler->SemBuffer, 1); |
c5c77ba1 JK |
19 | *hBuffer = pstrFifoHandler; |
20 | } else { | |
21 | *hBuffer = NULL; | |
22 | u32Error = 1; | |
23 | } | |
24 | } else { | |
25 | u32Error = 1; | |
26 | } | |
27 | return u32Error; | |
28 | } | |
4e4467fd | 29 | u32 FIFO_DeInit(tHANDLE hFifo) |
c5c77ba1 | 30 | { |
4e4467fd | 31 | u32 u32Error = 0; |
c5c77ba1 JK |
32 | tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; |
33 | if (pstrFifoHandler) { | |
34 | if (pstrFifoHandler->pu8Buffer) { | |
35 | WILC_FREE (pstrFifoHandler->pu8Buffer); | |
36 | } else { | |
37 | u32Error = 1; | |
38 | } | |
39 | ||
c5c77ba1 JK |
40 | WILC_FREE (pstrFifoHandler); |
41 | } else { | |
42 | u32Error = 1; | |
43 | } | |
44 | return u32Error; | |
45 | } | |
46 | ||
4e4467fd | 47 | u32 FIFO_ReadBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToRead, u32 *pu32BytesRead) |
c5c77ba1 | 48 | { |
4e4467fd | 49 | u32 u32Error = 0; |
c5c77ba1 JK |
50 | tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; |
51 | if (pstrFifoHandler && pu32BytesRead) { | |
52 | if (pstrFifoHandler->u32TotalBytes) { | |
83383ea3 | 53 | down(&pstrFifoHandler->SemBuffer); |
c5c77ba1 | 54 | |
83383ea3 AB |
55 | if (u32BytesToRead > pstrFifoHandler->u32TotalBytes) { |
56 | *pu32BytesRead = pstrFifoHandler->u32TotalBytes; | |
c5c77ba1 | 57 | } else { |
83383ea3 | 58 | *pu32BytesRead = u32BytesToRead; |
c5c77ba1 | 59 | } |
83383ea3 AB |
60 | if ((pstrFifoHandler->u32ReadOffset + u32BytesToRead) <= pstrFifoHandler->u32BufferLength) { |
61 | WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset, | |
62 | *pu32BytesRead); | |
63 | /* update read offset and total bytes */ | |
64 | pstrFifoHandler->u32ReadOffset += u32BytesToRead; | |
65 | pstrFifoHandler->u32TotalBytes -= u32BytesToRead; | |
66 | ||
67 | } else { | |
4e4467fd | 68 | u32 u32FirstPart = |
83383ea3 AB |
69 | pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32ReadOffset; |
70 | WILC_memcpy(pu8Buffer, pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32ReadOffset, | |
71 | u32FirstPart); | |
72 | WILC_memcpy(pu8Buffer + u32FirstPart, pstrFifoHandler->pu8Buffer, | |
73 | u32BytesToRead - u32FirstPart); | |
74 | /* update read offset and total bytes */ | |
75 | pstrFifoHandler->u32ReadOffset = u32BytesToRead - u32FirstPart; | |
76 | pstrFifoHandler->u32TotalBytes -= u32BytesToRead; | |
77 | } | |
78 | up(&pstrFifoHandler->SemBuffer); | |
c5c77ba1 JK |
79 | } else { |
80 | u32Error = 1; | |
81 | } | |
82 | } else { | |
83 | u32Error = 1; | |
84 | } | |
85 | return u32Error; | |
86 | } | |
87 | ||
72ed4dc7 | 88 | u32 FIFO_WriteBytes(tHANDLE hFifo, u8 *pu8Buffer, u32 u32BytesToWrite, bool bForceOverWrite) |
c5c77ba1 | 89 | { |
4e4467fd | 90 | u32 u32Error = 0; |
c5c77ba1 JK |
91 | tstrFifoHandler *pstrFifoHandler = (tstrFifoHandler *) hFifo; |
92 | if (pstrFifoHandler) { | |
93 | if (u32BytesToWrite < pstrFifoHandler->u32BufferLength) { | |
94 | if ((pstrFifoHandler->u32TotalBytes + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength || | |
95 | bForceOverWrite) { | |
83383ea3 AB |
96 | down(&pstrFifoHandler->SemBuffer); |
97 | if ((pstrFifoHandler->u32WriteOffset + u32BytesToWrite) <= pstrFifoHandler->u32BufferLength) { | |
98 | WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer, | |
99 | u32BytesToWrite); | |
100 | /* update read offset and total bytes */ | |
101 | pstrFifoHandler->u32WriteOffset += u32BytesToWrite; | |
102 | pstrFifoHandler->u32TotalBytes += u32BytesToWrite; | |
c5c77ba1 | 103 | |
83383ea3 | 104 | } else { |
4e4467fd | 105 | u32 u32FirstPart = |
83383ea3 AB |
106 | pstrFifoHandler->u32BufferLength - pstrFifoHandler->u32WriteOffset; |
107 | WILC_memcpy(pstrFifoHandler->pu8Buffer + pstrFifoHandler->u32WriteOffset, pu8Buffer, | |
108 | u32FirstPart); | |
109 | WILC_memcpy(pstrFifoHandler->pu8Buffer, pu8Buffer + u32FirstPart, | |
110 | u32BytesToWrite - u32FirstPart); | |
111 | /* update read offset and total bytes */ | |
112 | pstrFifoHandler->u32WriteOffset = u32BytesToWrite - u32FirstPart; | |
113 | pstrFifoHandler->u32TotalBytes += u32BytesToWrite; | |
c5c77ba1 | 114 | } |
83383ea3 AB |
115 | /* if data overwriten */ |
116 | if (pstrFifoHandler->u32TotalBytes > pstrFifoHandler->u32BufferLength) { | |
117 | /* adjust read offset to the oldest data available */ | |
118 | pstrFifoHandler->u32ReadOffset = pstrFifoHandler->u32WriteOffset; | |
119 | /* data availabe is the buffer length */ | |
120 | pstrFifoHandler->u32TotalBytes = pstrFifoHandler->u32BufferLength; | |
121 | } | |
122 | up(&pstrFifoHandler->SemBuffer); | |
c5c77ba1 JK |
123 | } else { |
124 | u32Error = 1; | |
125 | } | |
126 | } else { | |
127 | u32Error = 1; | |
128 | } | |
129 | } else { | |
130 | u32Error = 1; | |
131 | } | |
132 | return u32Error; | |
83383ea3 | 133 | } |