NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Component 11/Satellite/Satellite_buffer.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24#include "main.h"
25#include "SATELLITE.h"
26#include "Satellite_MAC.h"
27
28ptrSATELLITE_BUFFER satellite_buffer_init(NETSIM_ID utId, NETSIM_ID utIf,
29 NETSIM_ID gwId, NETSIM_ID gwIf,
30 double sizeInBytes, double maxUnitSizeInBytes)
31{
32 ptrSATELLITE_BUFFER buff = calloc(1, sizeof * buff);
33 buff->utId = utId;
34 buff->utIf = utIf;
35 buff->gwId = gwId;
36 buff->gwIf = gwIf;
37 buff->maxSizeInBytes = sizeInBytes;
38 buff->maxUnitSizeInBytes = maxUnitSizeInBytes;
39 return buff;
40}
41
42void satellite_buffer_setMaxUnitSizeInBytes(ptrSATELLITE_BUFFER buffer, double maxUnitSizeInBytes)
43{
44 buffer->maxUnitSizeInBytes = maxUnitSizeInBytes;
45}
46
47bool satellite_buffer_add_packet(ptrSATELLITE_BUFFER buffer, NetSim_PACKET* packet)
48{
49 if (buffer->maxUnitSizeInBytes < packet->pstruMacData->dPayload)
50 {
51 fn_NetSim_Packet_FreePacket(packet);
52 return false;
53 }
54
55 if (buffer->sizeInBytes + packet->pstruMacData->dPayload > buffer->maxSizeInBytes)
56 {
57 fn_NetSim_Packet_FreePacket(packet);
58 return false;
59 }
60
61 if (buffer->head)
62 {
63 buffer->tail->pstruNextPacket = packet;
64 buffer->tail = packet;
65 }
66 else
67 {
68 buffer->head = packet;
69 buffer->tail = packet;
70 }
71
72 buffer->count++;
73 buffer->sizeInBytes += packet->pstruMacData->dPayload;
74 return true;
75}
76
77NetSim_PACKET* satellite_buffer_remove_packet(ptrSATELLITE_BUFFER buffer)
78{
79 if (!buffer->head) return NULL;
80
81 NetSim_PACKET* packet = buffer->head;
82 buffer->head = packet->pstruNextPacket;
83 packet->pstruNextPacket = NULL;
84
85 if (!buffer->head) buffer->tail = NULL;
86
87 buffer->sizeInBytes -= packet->pstruMacData->dPayload;
88 buffer->count--;
89
90 return packet;
91}
92
93NetSim_PACKET* satellite_buffer_head_packet(ptrSATELLITE_BUFFER buffer)
94{
95 return buffer->head;
96}
97
98#pragma region SLOT_ALLOCATION
99static void calculate_slot_reqd_per_buffer(ptrSATELLITE_BUFFER buf, UINT bitsPerSlot)
100{
101 buf->slotReqd = 0;
102
103 double bufbits = (UINT)buf->sizeInBytes * 8;
104 buf->slotReqd = (UINT)ceil(bufbits / bitsPerSlot);
105}
106
107static void calculate_slot_reqd(ptrSUPERFRAME sf)
108{
109 UINT i;
110 for (i = 0; i < sf->bufferCount; i++)
111 {
112 calculate_slot_reqd_per_buffer(sf->buffers[i], sf->slotConfig->bitsPerSlot);
113 if (sf->linkType == LINKTYPE_FORWARD)
114 {
115 print_satellite_log("Slot reqd for Buffer %d:%d-->%d:%d is %d\n",
116 sf->buffers[i]->gwId, sf->buffers[i]->gwIf,
117 sf->buffers[i]->utId, sf->buffers[i]->utIf,
118 sf->buffers[i]->slotReqd);
119 }
120 else
121 {
122 print_satellite_log("Slot reqd for Buffer %d:%d-->%d:%d is %d\n",
123 sf->buffers[i]->utId, sf->buffers[i]->utIf,
124 sf->buffers[i]->gwId, sf->buffers[i]->gwIf,
125 sf->buffers[i]->slotReqd);
126
127 }
128 }
129}
130
131static void sort_buffer(UINT count, ptrSATELLITE_BUFFER* buffers)
132{
133 ptrSATELLITE_BUFFER temp = NULL;
134 UINT i, j;
135 for (i = 0; i < count; i++)
136 {
137 for (j = i; j < count; j++)
138 {
139 if (buffers[j]->rank > buffers[i]->rank)
140 {
141 temp = buffers[i];
142 buffers[i] = buffers[j];
143 buffers[j] = temp;
144 }
145 }
146 }
147}
148
149static void init_buffer_rank(ptrSUPERFRAME sf)
150{
151 UINT i;
152 for (i = 0; i < sf->bufferCount; i++)
153 {
154 if(sf->buffers[i]->slotReqd)
155 sf->buffers[i]->rank += 1;
156 sf->buffers[i]->allocatedSlotCount = 0;
157 }
158}
159
160static void allocate_slot_count(ptrSUPERFRAME sf, ptrFRAME fr)
161{
162 fr;
163 UINT i;
164 UINT slotCount = sf->frameConfig->slotCountInFrame;
165 for (i = 0; i < sf->bufferCount; i++)
166 {
167 sf->buffers[i]->allocatedSlotCount = min(sf->buffers[i]->slotReqd, slotCount);
168 slotCount -= sf->buffers[i]->allocatedSlotCount;
169 if (slotCount == 0) break;
170 }
171}
172
173static UINT calculate_slot_reqd_for_packet(double size, UINT bitsPerSlot)
174{
175 return (UINT)ceil(size * 8.0 / bitsPerSlot);
176}
177
178static void schedule_packet(ptrSUPERFRAME sf, ptrFRAME fr)
179{
180 UINT i;
181 UINT slcount;
182 for (i = 0; i < sf->bufferCount; i++)
183 {
184 ptrSATELLITE_BUFFER buf = sf->buffers[i];
185 if (!buf->allocatedSlotCount) break;
186
187 slcount = buf->allocatedSlotCount;
188 NetSim_PACKET* h;
189 while (buf->head)
190 {
191 h = buf->head;
192 if (slcount == 0) break;
193
194 UINT slreqd = calculate_slot_reqd_for_packet(h->pstruMacData->dPayload, sf->slotConfig->bitsPerSlot);
195 if (slreqd > slcount) break;
196 slcount -= slreqd;
197 buf->head = h->pstruNextPacket;
198 if (buf->head == NULL)buf->tail = NULL;
199 buf->count--;
200 buf->sizeInBytes -= h->pstruMacData->dPayload;
201 h->pstruNextPacket = NULL;
202
203 if (fr->head)
204 {
205 fr->tail->pstruNextPacket = h;
206 fr->tail = h;
207 }
208 else
209 {
210 fr->head = h;
211 fr->tail = h;
212 }
213 }
214 }
215}
216
217static void update_rank(ptrSUPERFRAME sf,ptrFRAME fr)
218{
219 fr;
220 UINT i;
221 UINT slotCount = sf->frameConfig->slotCountInFrame;
222 for (i = 0; i < sf->bufferCount; i++)
223 {
224 ptrSATELLITE_BUFFER buf = sf->buffers[i];
225 buf->rank -= ((buf->allocatedSlotCount * 1.0) / slotCount);
226 }
227}
228
229void satellite_allocate_slot(NETSIM_ID d, NETSIM_ID in,
230 ptrSUPERFRAME sf, ptrFRAME fr)
231{
232 d;
233 in;
234 print_satellite_log("Starting slot allocation for Superframe %d, Frame %d, LinkType = %s\n",
235 sf->superFrameId, fr->frameId, strLINKTYPE[sf->linkType]);
236 satellite_log_add_tab();
237 calculate_slot_reqd(sf);
238 init_buffer_rank(sf);
239 sort_buffer(sf->bufferCount, sf->buffers);
240 allocate_slot_count(sf, fr);
241 schedule_packet(sf, fr);
242 update_rank(sf, fr);
243 satellite_log_remove_tab();
244}
245#pragma endregion
246