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 }
116 
117 /*-----------------------------------------------------------------------------------------*/
119  return pcktQueue->isEmpty;
120 }
121 
122 /*-----------------------------------------------------------------------------------------*/
124 
125  if (pcktQueue->isEmpty == 1)
126  return 0;
127 
128  if (pcktQueue->oldestItem < pcktQueue->nextFreeItem)
129  return (CrFwCounterU1_t)(pcktQueue->nextFreeItem - pcktQueue->oldestItem);
130 
131  return (CrFwCounterU1_t)(pcktQueue->size - (pcktQueue->oldestItem - pcktQueue->nextFreeItem));
132 }
133 
134 /*-----------------------------------------------------------------------------------------*/
136  return pcktQueue->size;
137 }
unsigned char CrFwCounterU1_t
Type used for unsigned integers with a "short" range.
void CrFwPcktQueueReset(CrFwPcktQueue_t pcktQueue)
Reset the packet queue.
Definition: CrFwPcktQueue.c:73
CrFwBool_t CrFwPcktQueueIsEmpty(CrFwPcktQueue_t pcktQueue)
Return 1 if the packet queue is empty and 0 otherwise.
CrFwPckt_t CrFwPcktQueuePop(CrFwPcktQueue_t pcktQueue)
Pop a packet from the packet queue.
Definition: CrFwPcktQueue.c:25
CrFwPckt_t * pckt
The list of packets in the PQ.
unsigned char * CrFwPckt_t
Type for packets (see CrFwPckt.h).
Definition: CrFwConstants.h:38
CrFwBool_t CrFwPcktQueuePush(CrFwPcktQueue_t pcktQueue, CrFwPckt_t pckt)
Push a packet onto the packet queue.
Definition: CrFwPcktQueue.c:52
void CrFwPcktQueueInit(CrFwPcktQueue_t pcktQueue, CrFwCounterU1_t size)
Initializes the packet queue.
Definition: CrFwPcktQueue.c:95
Interface for creating and accessing a report or command packet.
int CrFwBool_t
Type used for boolean values (1 represent "true" and 0 represents "false").
Definition: CrFwConstants.h:32
Descriptor for a Packet Queue (PQ) in an OutStream or InStream.
void CrFwPcktRelease(CrFwPckt_t pckt)
Release function for command or report packets.
Definition: CrFwPckt.c:156
CrFwPckt_t CrFwPcktQueueGetOldest(CrFwPcktQueue_t pcktQueue)
Return the oldest packet from the queue without removing it from the queue.
Definition: CrFwPcktQueue.c:44
CrFwCounterU1_t CrFwPcktQueueGetSize(CrFwPcktQueue_t pcktQueue)
Return the size of the packet queue.
CrFwBool_t isEmpty
Flag indicating whether the PQ is empty.
CrFwCounterU1_t CrFwPcktQueueGetNOfPckts(CrFwPcktQueue_t pcktQueue)
Return the number of packets currently in the packet queue.
Definition and management of packet queues.
CrFwCounterU1_t size
The size of the PQ (the same as the size of the pckt array)
CrFwCounterU1_t oldestItem
The index of the oldest item in the PQ.
CrFwCounterU1_t nextFreeItem
Index of the next free slot in the PQ.
void CrFwPcktQueueShutdown(CrFwPcktQueue_t pcktQueue)
Shutdown the packet queue.
P&P Software GmbH, Copyright 2012-2013, All Rights Reserved