26#include "Satellite_MAC.h"
28ptrSATELLITE_BUFFER satellite_buffer_init(NETSIM_ID utId, NETSIM_ID utIf,
29 NETSIM_ID gwId, NETSIM_ID gwIf,
30 double sizeInBytes,
double maxUnitSizeInBytes)
32 ptrSATELLITE_BUFFER buff = calloc(1,
sizeof * buff);
37 buff->maxSizeInBytes = sizeInBytes;
38 buff->maxUnitSizeInBytes = maxUnitSizeInBytes;
42void satellite_buffer_setMaxUnitSizeInBytes(ptrSATELLITE_BUFFER buffer,
double maxUnitSizeInBytes)
44 buffer->maxUnitSizeInBytes = maxUnitSizeInBytes;
47bool satellite_buffer_add_packet(ptrSATELLITE_BUFFER buffer, NetSim_PACKET* packet)
49 if (buffer->maxUnitSizeInBytes < packet->pstruMacData->dPayload)
51 fn_NetSim_Packet_FreePacket(packet);
55 if (buffer->sizeInBytes + packet->pstruMacData->dPayload > buffer->maxSizeInBytes)
57 fn_NetSim_Packet_FreePacket(packet);
63 buffer->tail->pstruNextPacket = packet;
64 buffer->tail = packet;
68 buffer->head = packet;
69 buffer->tail = packet;
73 buffer->sizeInBytes += packet->pstruMacData->dPayload;
77NetSim_PACKET* satellite_buffer_remove_packet(ptrSATELLITE_BUFFER buffer)
79 if (!buffer->head)
return NULL;
81 NetSim_PACKET* packet = buffer->head;
82 buffer->head = packet->pstruNextPacket;
83 packet->pstruNextPacket = NULL;
85 if (!buffer->head) buffer->tail = NULL;
87 buffer->sizeInBytes -= packet->pstruMacData->dPayload;
93NetSim_PACKET* satellite_buffer_head_packet(ptrSATELLITE_BUFFER buffer)
98#pragma region SLOT_ALLOCATION
99static void calculate_slot_reqd_per_buffer(ptrSATELLITE_BUFFER buf, UINT bitsPerSlot)
103 double bufbits = (UINT)buf->sizeInBytes * 8;
104 buf->slotReqd = (UINT)ceil(bufbits / bitsPerSlot);
107static void calculate_slot_reqd(ptrSUPERFRAME sf)
110 for (i = 0; i < sf->bufferCount; i++)
112 calculate_slot_reqd_per_buffer(sf->buffers[i], sf->slotConfig->bitsPerSlot);
113 if (sf->linkType == LINKTYPE_FORWARD)
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);
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);
131static void sort_buffer(UINT count, ptrSATELLITE_BUFFER* buffers)
133 ptrSATELLITE_BUFFER temp = NULL;
135 for (i = 0; i < count; i++)
137 for (j = i; j < count; j++)
139 if (buffers[j]->rank > buffers[i]->rank)
142 buffers[i] = buffers[j];
149static void init_buffer_rank(ptrSUPERFRAME sf)
152 for (i = 0; i < sf->bufferCount; i++)
154 if(sf->buffers[i]->slotReqd)
155 sf->buffers[i]->rank += 1;
156 sf->buffers[i]->allocatedSlotCount = 0;
160static void allocate_slot_count(ptrSUPERFRAME sf, ptrFRAME fr)
164 UINT slotCount = sf->frameConfig->slotCountInFrame;
165 for (i = 0; i < sf->bufferCount; i++)
167 sf->buffers[i]->allocatedSlotCount = min(sf->buffers[i]->slotReqd, slotCount);
168 slotCount -= sf->buffers[i]->allocatedSlotCount;
169 if (slotCount == 0)
break;
173static UINT calculate_slot_reqd_for_packet(
double size, UINT bitsPerSlot)
175 return (UINT)ceil(size * 8.0 / bitsPerSlot);
178static void schedule_packet(ptrSUPERFRAME sf, ptrFRAME fr)
182 for (i = 0; i < sf->bufferCount; i++)
184 ptrSATELLITE_BUFFER buf = sf->buffers[i];
185 if (!buf->allocatedSlotCount)
break;
187 slcount = buf->allocatedSlotCount;
192 if (slcount == 0)
break;
194 UINT slreqd = calculate_slot_reqd_for_packet(h->pstruMacData->dPayload, sf->slotConfig->bitsPerSlot);
195 if (slreqd > slcount)
break;
197 buf->head = h->pstruNextPacket;
198 if (buf->head == NULL)buf->tail = NULL;
200 buf->sizeInBytes -= h->pstruMacData->dPayload;
201 h->pstruNextPacket = NULL;
205 fr->tail->pstruNextPacket = h;
217static void update_rank(ptrSUPERFRAME sf,ptrFRAME fr)
221 UINT slotCount = sf->frameConfig->slotCountInFrame;
222 for (i = 0; i < sf->bufferCount; i++)
224 ptrSATELLITE_BUFFER buf = sf->buffers[i];
225 buf->rank -= ((buf->allocatedSlotCount * 1.0) / slotCount);
229void satellite_allocate_slot(NETSIM_ID d, NETSIM_ID in,
230 ptrSUPERFRAME sf, ptrFRAME fr)
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);
243 satellite_log_remove_tab();