CORDET Framework - C2 Implementation
CrFwPcktQueue.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include "../CrFwConstants.h"
21 #include "CrFwPckt.h"
22 #include "CrFwPcktQueue.h"
23 
24 /*-----------------------------------------------------------------------------------------*/
26  CrFwCounterU1_t posOldest;
27  CrFwPckt_t oldestPckt;
28 
29  if (!pcktQueue->isEmpty) {
30  posOldest = pcktQueue->oldestItem;
31  oldestPckt = pcktQueue->pckt[posOldest];
32  if (posOldest < (pcktQueue->size-1))
33  pcktQueue->oldestItem++;
34  else
35  pcktQueue->oldestItem = 0;
36  if (pcktQueue->oldestItem == pcktQueue->nextFreeItem)
37  pcktQueue->isEmpty = 1;
38  return oldestPckt;
39  } else
40  return NULL;
41 }
42 
43 /*-----------------------------------------------------------------------------------------*/
45  if (!pcktQueue->isEmpty)
46  return pcktQueue->pckt[pcktQueue->oldestItem];
47  else
48  return NULL;
49 }
50 
51 /*-----------------------------------------------------------------------------------------*/
53  if (pcktQueue->isEmpty == 1) {
54  pcktQueue->pckt[0] = pckt;
55  pcktQueue->nextFreeItem = 1;
56  pcktQueue->oldestItem =0;
57  pcktQueue->isEmpty = 0;
58  return 1;
59  }
60 
61  if (pcktQueue->nextFreeItem == pcktQueue->oldestItem)
62  return 0;
63 
64  pcktQueue->pckt[pcktQueue->nextFreeItem] = pckt;
65  if (pcktQueue->nextFreeItem < (pcktQueue->size-1))
66  pcktQueue->nextFreeItem++;
67  else
68  pcktQueue->nextFreeItem = 0;
69  return 1;
70 }
71 
72 /*-----------------------------------------------------------------------------------------*/
75 
76  if (pcktQueue->isEmpty == 1)
77  return;
78 
79  if (pcktQueue->oldestItem < pcktQueue->nextFreeItem) {
80  for (i=pcktQueue->oldestItem; i<pcktQueue->nextFreeItem; i++)
81  CrFwPcktRelease(pcktQueue->pckt[i]);
82  pcktQueue->isEmpty = 1;
83  return;
84  }
85 
86  for (i=pcktQueue->oldestItem; i<pcktQueue->size; i++)
87  CrFwPcktRelease(pcktQueue->pckt[i]);
88  for (i=0; i<pcktQueue->nextFreeItem; i++)
89  CrFwPcktRelease(pcktQueue->pckt[i]);
90  pcktQueue->isEmpty = 1;
91  return;
92 }
93 
94 /*-----------------------------------------------------------------------------------------*/
97 
98  if (pcktQueue->pckt != NULL)
99  return;
100 
101  pcktQueue->pckt = malloc(size*sizeof(CrFwPckt_t));
102  for (i=0; i<size; i++)
103  pcktQueue->pckt[i] = NULL;
104  pcktQueue->isEmpty = 1;
105  pcktQueue->nextFreeItem = 0;
106  pcktQueue->oldestItem = 0;
107  pcktQueue->size = size;
108 }
109 
110 /*-----------------------------------------------------------------------------------------*/
112  CrFwPcktQueueReset(pcktQueue);
113  free(pcktQueue->pckt);
114  pcktQueue->pckt = NULL;
115  pcktQueue->size = 0;
116 }
117 
118 /*-----------------------------------------------------------------------------------------*/
120  return pcktQueue->isEmpty;
121 }
122 
123 /*-----------------------------------------------------------------------------------------*/
125 
126  if (pcktQueue->isEmpty == 1)
127  return 0;
128 
129  if (pcktQueue->oldestItem < pcktQueue->nextFreeItem)
130  return (CrFwCounterU1_t)(pcktQueue->nextFreeItem - pcktQueue->oldestItem);
131 
132  return (CrFwCounterU1_t)(pcktQueue->size - (pcktQueue->oldestItem - pcktQueue->nextFreeItem));
133 }
134 
135 /*-----------------------------------------------------------------------------------------*/
137  return pcktQueue->size;
138 }
unsigned char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:38
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:32
Interface for creating and accessing a report or command packet.
void CrFwPcktRelease(CrFwPckt_t pckt)
Release function for command or report packets.
Definition: CrFwPckt.c:174
CrFwPckt_t CrFwPcktQueueGetOldest(CrFwPcktQueue_t pcktQueue)
Return the oldest packet from the queue without removing it from the queue.
Definition: CrFwPcktQueue.c:44
CrFwBool_t CrFwPcktQueueIsEmpty(CrFwPcktQueue_t pcktQueue)
Return 1 if the packet queue is empty and 0 otherwise.
CrFwCounterU1_t CrFwPcktQueueGetSize(CrFwPcktQueue_t pcktQueue)
Return the size of the packet queue.
CrFwPckt_t CrFwPcktQueuePop(CrFwPcktQueue_t pcktQueue)
Pop a packet from the packet queue.
Definition: CrFwPcktQueue.c:25
void CrFwPcktQueueReset(CrFwPcktQueue_t pcktQueue)
Reset the packet queue.
Definition: CrFwPcktQueue.c:73
void CrFwPcktQueueInit(CrFwPcktQueue_t pcktQueue, CrFwCounterU1_t size)
Initializes the packet queue.
Definition: CrFwPcktQueue.c:95
CrFwBool_t CrFwPcktQueuePush(CrFwPcktQueue_t pcktQueue, CrFwPckt_t pckt)
Push a packet onto the packet queue.
Definition: CrFwPcktQueue.c:52
void CrFwPcktQueueShutdown(CrFwPcktQueue_t pcktQueue)
Shutdown the packet queue.
CrFwCounterU1_t CrFwPcktQueueGetNOfPckts(CrFwPcktQueue_t pcktQueue)
Return the number of packets currently in the packet queue.
Definition and management of packet queues.
unsigned char CrFwCounterU1_t
Type used for unsigned integers with a "short" range.
Descriptor for a Packet Queue (PQ) in an OutStream or InStream.
CrFwCounterU1_t oldestItem
The index of the oldest item in the PQ.
CrFwPckt_t * pckt
The list of packets in the PQ.
CrFwCounterU1_t nextFreeItem
Index of the next free slot in the PQ.
CrFwCounterU1_t size
The size of the PQ (the same as the size of the pckt array)
CrFwBool_t isEmpty
Flag indicating whether the PQ is empty.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved