NetSim Source Code Help
Loading...
Searching...
No Matches
Satellite_MAC.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2021 *
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#include "Satellite_Frame.h"
28#include "Satellite_HDR.h"
29
30#pragma region FUNCTION_PROTOTYPE
31#pragma endregion
32
33#pragma region SATELLITE_UT_MAC_INIT
35{
38 {
40 }
41 else
42 {
43 mac = calloc(1, sizeof * mac);
44 mac->utId = pd->deviceId;
45 mac->utIf = pd->interfaceId;
47 }
48 return mac;
49}
50
52{
54 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
55
56 NETSIM_ID i;
57 NETSIM_ID s;
58 NETSIM_ID sin;
59
62
65 {
66 mac->satelliteId = s;
67 mac->satelliteName = DEVICE_NAME(s);
68 mac->satelliteIf = sin;
69 return;
70 }
71
72 for (i = 0; i < link->puniDevList.pstrup2MP.nConnectedDeviceCount; i++)
73 {
74 s = link->puniDevList.pstrup2MP.anDevIds[i];
76
77 type = SATELLITE_DEVICETYPE_GET(s, sin);
79 {
80 mac->satelliteId = s;
81 mac->satelliteName = DEVICE_NAME(s);
82 mac->satelliteIf = sin;
83 return;
84 }
85 }
86
87 fnNetSimError("No satellite is connected to UT %d:%d\n", d, in);
88}
89
91{
95
97
98 mac->buffer = satellite_buffer_init(utId, utIf,
99 mac->gatewayId, mac->gatewayIf, mac->bufferSize_bits / 8,
100 rsf->frameConfig->bitsPerFrame / 8);
101
103 utId, utIf);
104}
105#pragma endregion
106
107#pragma region SATELLITE_GW_MAC_INIT
109{
112 {
114 }
115 else
116 {
117 mac = calloc(1, sizeof * mac);
118 mac->gwId = pd->deviceId;
119 mac->gwIf = pd->interfaceId;
121 }
122 return mac;
123}
124
126{
128 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
129
130 NETSIM_ID i;
131 NETSIM_ID s;
132 NETSIM_ID sin;
133
136
139 {
140 mac->satelliteId = s;
141 mac->satelliteName = DEVICE_NAME(s);
142 mac->satelliteIf = sin;
143 return;
144 }
145
146 for (i = 0; i < link->puniDevList.pstrup2MP.nConnectedDeviceCount; i++)
147 {
148 s = link->puniDevList.pstrup2MP.anDevIds[i];
150
151 type = SATELLITE_DEVICETYPE_GET(s, sin);
153 {
154 mac->satelliteId = s;
155 mac->satelliteName = DEVICE_NAME(s);
156 mac->satelliteIf = sin;
157 return;
158 }
159 }
160
161 fnNetSimError("No satellite is connected to GW %d:%d\n", d, in);
162}
163
165{
166 satellite_gw_set_satellite(gwId, gwIf);
167}
168#pragma endregion
169
170#pragma region SATELLITE_MAC_INIT
172{
175 {
176 mac = SATELLITE_MAC_GET(pd->deviceId, pd->interfaceId);
177 }
178 else
179 {
180 mac = calloc(1, sizeof * mac);
181 mac->satelliteId = pd->deviceId;
182 mac->satelliteIf = pd->interfaceId;
184 }
185 return mac;
186}
187
189{
190 superframe_init(d, in);
191}
192#pragma endregion
193
194#pragma region SATELLITE_UT_ASSOCIATION
196 NETSIM_ID utId, NETSIM_ID utIf)
197{
199 if (mac->nAssocUTCount == 0) return NULL;
200
201 UINT i;
202 for (i = 0; i < mac->nAssocUTCount; i++)
203 {
204 if (mac->utAssocInfo[i]->utId == utId &&
205 mac->utAssocInfo[i]->utIf == utIf)
206 return mac->utAssocInfo[i];
207 }
208 return NULL;
209}
210
212 NETSIM_ID utId, NETSIM_ID utIf)
213{
214 ptrSATELLITE_UTASSOCINFO info = satellite_utassocinfo_find(gwId, gwIf, utId, utIf);
215 if (info) return; //UT is already associated
216
218 if (mac->satelliteId == 0) satellite_gw_set_satellite(gwId, gwIf);
220
221 info = calloc(1, sizeof * info);
222 info->utId = utId;
223 info->utIf = utIf;
224 info->buffer = satellite_buffer_init(utId, utIf, gwId, gwIf, mac->bufferSize_bits / 8, fsf->frameConfig->bitsPerFrame / 8);
225
226 if (mac->nAssocUTCount)
227 mac->utAssocInfo = realloc(mac->utAssocInfo, ((size_t)mac->nAssocUTCount + 1) * (sizeof * mac->utAssocInfo));
228 else
229 mac->utAssocInfo = calloc(1, sizeof * mac->utAssocInfo);
230
231 mac->utAssocInfo[mac->nAssocUTCount] = info;
232 mac->nAssocUTCount++;
233
234 print_satellite_log("UT %d-%d is associated with gateway %d-%d\n",
235 utId, utIf, gwId, gwIf);
236}
237
238#pragma endregion
239
240#pragma region SATELLITE_UTMAC_PACKET_PROCESSING
242{
245 NetSim_BUFFER* buffer = DEVICE_ACCESSBUFFER(d, in);
247
248 while (fn_NetSim_GetBufferStatus(buffer))
249 {
250 NetSim_PACKET* packet;
251 packet = fn_NetSim_Packet_GetPacketFromBuffer(buffer, 1);
252
256
257 satellite_hdr_init(d, in, packet);
259 }
260}
261#pragma endregion
262
263#pragma region SATELLITE_GWMAC_PACKET_PROCESSING
265{
268 NetSim_BUFFER* buffer = DEVICE_ACCESSBUFFER(d, in);
269
270 while (fn_NetSim_GetBufferStatus(buffer))
271 {
272 NetSim_PACKET* packet;
273 packet = fn_NetSim_Packet_GetPacketFromBuffer(buffer, 1);
274
276 NETSIM_ID utId;
277 NETSIM_ID utIf;
278 utId = fn_NetSim_Stack_GetDeviceId_asIP(nextHop, &utIf);
279
281
285
286 satellite_hdr_init(d, in, packet);
287 satellite_buffer_add_packet(info->buffer, packet);
288
289 }
290}
291#pragma endregion
292
293#pragma region SATELLITE_MAC_PACKET_PROCESSING
295{
297 switch (pd->deviceType)
298 {
301 break;
304 break;
305 default:
306 fnNetSimError("Unknwon device type in %s\n", __FUNCTION__);
307 break;
308 }
309}
310
312 NetSim_PACKET* packet, double time)
313{
314 NetSim_EVENTDETAILS pevent;
315 memset(&pevent, 0, sizeof pevent);
316 pevent.dEventTime = time;
317 pevent.dPacketSize = packet->pstruMacData->dPacketSize;
318 if (packet->pstruAppData)
319 {
321 pevent.nSegmentId = packet->pstruAppData->nSegmentId;
322 }
323 pevent.nDeviceId = d;
324 pevent.nInterfaceId = in;
325 pevent.nDeviceType = DEVICE_TYPE(d);
327 pevent.nPacketId = packet->nPacketId;
329 pevent.pPacket = packet;
330 fnpAddEvent(&pevent);
331}
332
334{
337 double time = pstruEventDetails->dEventTime;
339 SATELLITE_HDR_REMOVE(packet);
340 forward_to_network(d, in, packet, time);
341}
342#pragma endregion
343
344#pragma region BUFFER_LIST
346 ptrSUPERFRAME sf)
347{
348 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
349 NETSIM_ID g = 0, gin = 0;
350 NETSIM_ID i, j;
351 for (i = 0; i < link->puniDevList.pstrup2MP.nConnectedDeviceCount-1; i++)
352 {
353 g = link->puniDevList.pstrup2MP.anDevIds[i];
357 {
359 for (j = 0; j < mac->nAssocUTCount; j++)
360 {
361 if (sf->bufferCount)
362 sf->buffers = realloc(sf->buffers, (sf->bufferCount + 1) * (sizeof * sf->buffers));
363 else
364 sf->buffers = calloc(1, sizeof * sf->buffers);
365 sf->buffers[sf->bufferCount] = mac->utAssocInfo[j]->buffer;
366 sf->bufferCount++;
367 }
368 }
369 }
370}
371
373 ptrSUPERFRAME sf)
374{
375 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
376 NETSIM_ID u, uin;
377 NETSIM_ID i;
378 for (i = 0; i < link->puniDevList.pstrup2MP.nConnectedDeviceCount - 1; i++)
379 {
380 u = link->puniDevList.pstrup2MP.anDevIds[i];
384 continue;
385
387 if (sf->bufferCount)
388 sf->buffers = realloc(sf->buffers, (sf->bufferCount + 1) * (sizeof * sf->buffers));
389 else
390 sf->buffers = calloc(1, sizeof * sf->buffers);
391 sf->buffers[sf->bufferCount] = mac->buffer;
392 sf->bufferCount++;
393 }
394}
395
397 ptrSUPERFRAME sf)
398{
399 if (sf->linkType == LINKTYPE_FORWARD)
401 else if (sf->linkType == LINKTYPE_RETURN)
403 else
404 {
405 fnNetSimError("Unknwon link type %d in function %s\n",
406 sf->linkType, __FUNCTION__);
407 }
408}
409#pragma endregion
unsigned int NETSIM_ID
Definition: Animation.h:45
#define UINT
Definition: Linux.h:38
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define realloc(p, s)
Definition: Memory.h:32
#define calloc(c, s)
Definition: Memory.h:29
bool fn_NetSim_GetBufferStatus(NetSim_BUFFER *pstruBuffer)
Definition: Scheduling.c:41
NetSim_PACKET * fn_NetSim_Packet_GetPacketFromBuffer(NetSim_BUFFER *pstruBuffer, int nFlag)
enum enum_SATELLITE_DEVICE_TYPE SATELLITE_DEVICETYPE
@ LINKTYPE_FORWARD
Definition: Satellite.h:61
@ LINKTYPE_RETURN
Definition: Satellite.h:62
void print_satellite_log(char *format,...)
void satellite_hdr_init(NETSIM_ID d, NETSIM_ID in, NetSim_PACKET *packet)
Definition: Satellite_HDR.c:28
#define SATELLITE_MAC_GET(d, i)
Definition: Satellite.h:108
@ SATELLITE_DEVICETYPE_SATELLITE_GATEWAY
Definition: Satellite.h:52
@ SATELLITE_DEVICETYPE_USER_TERMINAL
Definition: Satellite.h:50
@ SATELLITE_DEVICETYPE_SATELLITE
Definition: Satellite.h:51
#define SATELLITE_PROTOCOLDATA_CURRENT()
Definition: Satellite.h:92
#define SATELLITE_MAC_SET(d, i, data)
Definition: Satellite.h:115
bool SATELLITE_LAYER_DATA_IsInitialized(NETSIM_ID d, NETSIM_ID in, SATELLITE_DEVICETYPE devType, SATELLITE_LAYER layer)
#define SATELLITE_UTMAC_SET(d, i, data)
Definition: Satellite.h:117
@ SATELLITE_LAYER_MAC
Definition: Satellite.h:68
#define SATELLITE_GWMAC_GET(d, i)
Definition: Satellite.h:112
SATELLITE_DEVICETYPE SATELLITE_DEVICETYPE_GET(NETSIM_ID d, NETSIM_ID in)
#define SATELLITE_GWMAC_SET(d, i, data)
Definition: Satellite.h:119
#define SATELLITE_UTMAC_GET(d, i)
Definition: Satellite.h:110
ptrSUPERFRAME satellite_get_return_superframe(NETSIM_ID d, NETSIM_ID in)
ptrSUPERFRAME satellite_get_forward_superframe(NETSIM_ID d, NETSIM_ID in)
void superframe_init(NETSIM_ID d, NETSIM_ID in)
void SATELLITE_HDR_REMOVE(NetSim_PACKET *packet)
Definition: Satellite_HDR.c:70
void satellite_ut_set_gateway(ptrSATELLITE_UT_MAC utMac)
void satellite_handle_mac_out()
ptrSATELLITE_GW_MAC satellite_gw_mac_alloc(ptrSATELLITE_PROTODATA pd)
static void satellite_gwmac_handle_mac_out()
ptrSATELLITE_UT_MAC satellite_ut_mac_alloc(ptrSATELLITE_PROTODATA pd)
Definition: Satellite_MAC.c:34
static void forward_to_network(NETSIM_ID d, NETSIM_ID in, NetSim_PACKET *packet, double time)
static void form_bufferlist_for_returnlink(NETSIM_ID d, NETSIM_ID in, ptrSUPERFRAME sf)
ptrSATELLITE_UTASSOCINFO satellite_utassocinfo_find(NETSIM_ID gwId, NETSIM_ID gwIf, NETSIM_ID utId, NETSIM_ID utIf)
static void satellite_ut_set_satellite(NETSIM_ID d, NETSIM_ID in)
Definition: Satellite_MAC.c:51
void satellite_form_bufferList(NETSIM_ID d, NETSIM_ID in, ptrSUPERFRAME sf)
void satellite_UT_MAC_init(NETSIM_ID utId, NETSIM_ID utIf)
Definition: Satellite_MAC.c:90
static void satellite_gw_set_satellite(NETSIM_ID d, NETSIM_ID in)
static void form_bufferlist_for_forwardlink(NETSIM_ID d, NETSIM_ID in, ptrSUPERFRAME sf)
ptrSATELLITE_MAC satellite_mac_alloc(ptrSATELLITE_PROTODATA pd)
void satellite_handle_mac_in()
void satellite_GW_MAC_init(NETSIM_ID gwId, NETSIM_ID gwIf)
void satellite_assoc_ut(NETSIM_ID gwId, NETSIM_ID gwIf, NETSIM_ID utId, NETSIM_ID utIf)
static void satellite_utmac_hadle_mac_out()
void satellite_mac_init(NETSIM_ID d, NETSIM_ID in)
ptrSATELLITE_BUFFER satellite_buffer_init(NETSIM_ID utId, NETSIM_ID utIf, NETSIM_ID gwId, NETSIM_ID gwIf, double sizeInBytes, double maxUnitSizeInBytes)
bool satellite_buffer_add_packet(ptrSATELLITE_BUFFER buffer, NetSim_PACKET *packet)
NETWORK_LAYER_PROTOCOL fn_NetSim_Stack_GetNWProtocol(NETSIM_ID nDeviceId)
#define DEVICE_NAME(DeviceId)
Definition: Stack.h:774
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
@ MAC_PROTOCOL_SATELLITE
Definition: Stack.h:234
@ NETWORK_IN_EVENT
Definition: Stack.h:109
#define DEVICE_ACCESSBUFFER(DeviceId, InterfaceId)
Definition: Stack.h:794
#define DEVICE_MACVAR(DeviceId, InterfaceId)
Definition: Stack.h:798
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_PHYLAYER(DeviceId, InterfaceId)
Definition: Stack.h:788
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asIP(NETSIM_IPAddress ip, NETSIM_ID *nInterfaceId)
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
NETSIM_ID nApplicationId
Definition: Stack.h:752
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nDeviceId
Definition: Stack.h:750
long long int nPacketId
Definition: Stack.h:755
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
NETSIM_ID nInterfaceId
Definition: Stack.h:751
MAC_LAYER_PROTOCOL nMACProtocol
Definition: Packet.h:223
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
long long int nPacketId
Definition: Packet.h:256
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276
SATELLITE_DEVICETYPE deviceType
Definition: Satellite.h:85
ptrSATELLITE_BUFFER buffer
Definition: Satellite_MAC.h:51
ptrSATELLITE_UTASSOCINFO * utAssocInfo
Definition: Satellite_MAC.h:78
NETSIM_ID satelliteIf
Definition: Satellite_MAC.h:85
NETSIM_ID satelliteId
Definition: Satellite_MAC.h:84
ptrSATELLITE_BUFFER buffer
Definition: Satellite_MAC.h:59
ptrFRAMECONF frameConfig
ptrSATELLITE_BUFFER * buffers