blob: eb25f3be3ea1fe16a8e3c2c26aee8cabeade3681 [file] [log] [blame]
danicampora87856452015-02-06 15:35:48 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2015 Daniel Campora
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdint.h>
28#include <stdbool.h>
29#include <string.h>
30
danicampora87856452015-02-06 15:35:48 +010031#include "osi.h"
32#include "fifo.h"
33#include "socketfifo.h"
34
35
36/*----------------------------------------------------------------------------
37 ** Declare private functions
38 */
39static void socketfifo_Push (void * const pvFifo, const void * const pvElement);
40static void socketfifo_Pop (void * const pvFifo, void * const pvElement);
41
42/*----------------------------------------------------------------------------
43 ** Declare private data
44 */
45static FIFO_t *socketfifo;
46
47/*----------------------------------------------------------------------------
48 ** Define public functions
49 */
50void SOCKETFIFO_Init (FIFO_t *fifo, void *elements, uint32_t maxcount) {
51 // Initialize global data
52 socketfifo = fifo;
53 socketfifo->pvElements = elements;
54 FIFO_Init (socketfifo, maxcount, socketfifo_Push, socketfifo_Pop);
55}
56
57bool SOCKETFIFO_Push (const void * const element) {
58 return FIFO_bPushElement (socketfifo, element);
59}
60
61bool SOCKETFIFO_Pop (void * const element) {
62 return FIFO_bPopElement (socketfifo, element);
63}
64
65bool SOCKETFIFO_Peek (void * const element) {
66 return FIFO_bPeekElement (socketfifo, element);
67}
68
69bool SOCKETFIFO_IsEmpty (void) {
70 return FIFO_IsEmpty (socketfifo);
71}
72
73bool SOCKETFIFO_IsFull (void) {
74 return FIFO_IsFull (socketfifo);
75}
76
77void SOCKETFIFO_Flush (void) {
78 SocketFifoElement_t element;
79 while (SOCKETFIFO_Pop(&element)) {
80 if (element.freedata) {
81 mem_Free(element.data);
82 }
83 }
84}
85
86unsigned int SOCKETFIFO_Count (void) {
87 return socketfifo->uiElementCount;
88}
89
90/*----------------------------------------------------------------------------
91 ** Define private functions
92 */
93static void socketfifo_Push (void * const pvFifo, const void * const pvElement) {
94 if ((pvFifo != NULL) && (NULL != pvElement)) {
95 unsigned int uiLast = ((FIFO_t *)pvFifo)->uiLast;
96 memcpy (&((SocketFifoElement_t *)((FIFO_t *)pvFifo)->pvElements)[uiLast], pvElement, sizeof(SocketFifoElement_t));
97 }
98}
99
100static void socketfifo_Pop (void * const pvFifo, void * const pvElement) {
101 if ((pvFifo != NULL) && (NULL != pvElement)) {
102 unsigned int uiFirst = ((FIFO_t *)pvFifo)->uiFirst;
103 memcpy (pvElement, &((SocketFifoElement_t *)((FIFO_t *)pvFifo)->pvElements)[uiFirst], sizeof(SocketFifoElement_t));
104 }
105}
106