NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
GSM.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 * Author: Shashi Kant Suman *
12 * *
13 * ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "Cellular.h"
16#include "../Application/Application.h"
17#include "AdvancedPlots.h"
18#pragma comment(lib,"AdvancedPlots.lib")
19
20/* Function prototype */
21_declspec(dllexport) int fn_NetSim_GSM_Init_F(struct stru_NetSim_Network *NETWORK_Formal,
22 NetSim_EVENTDETAILS *pstruEventDetails_Formal,
23 char *pszAppPath_Formal,
24 char *pszWritePath_Formal,
25 int nVersion_Type,
26 void **fnPointer);
27_declspec(dllexport) int fn_NetSim_GSM_Configure_F(void** var);
28
29_declspec(dllexport) int fn_NetSim_GSM_Metrics_F(char* szMetrics);
30
31
32
33
34
35/** This function is used to initialize the GSM parameters in a network */
36_declspec(dllexport) int fn_NetSim_GSM_Init(struct stru_NetSim_Network *NETWORK_Formal,
37 NetSim_EVENTDETAILS *pstruEventDetails_Formal,
38 char *pszAppPath_Formal,
39 char *pszWritePath_Formal,
40 int nVersion_Type,
41 void **fnPointer)
42{
43 init_linkpacketlog();
44 NETWORK=NETWORK_Formal;
45 pstruEventDetails=pstruEventDetails_Formal;
46 return fn_NetSim_GSM_Init_F(NETWORK_Formal,
47 pstruEventDetails_Formal,
48 pszAppPath_Formal,
49 pszWritePath_Formal,
50 nVersion_Type,
51 fnPointer);
52}
53/** This function is called by the NetworkStack.dll, while configuring the Network for GSM protocol */
54_declspec(dllexport) int fn_NetSim_GSM_Configure(void** var)
55{
56 return fn_NetSim_GSM_Configure_F(var);
57}
58/**
59This function is called by NetworkStack.dll, which inturn calls the
60Cellular run function which is present in Cellular.c
61 */
62_declspec (dllexport) int fn_NetSim_GSM_Run()
63{
64 return fn_NetSim_Cellular_Run();
65}
66/**
67This function is called by NetworkStack.dll, while writing the evnt trace
68to get the sub event as a string.
69*/
70_declspec (dllexport) char *fn_NetSim_GSM_Trace(int nSubEvent)
71{
72 switch(nSubEvent%100)
73 {
74 case Subevent_DropCall:
75 return "DropCall";
76 case Subevent_TxNextBurst:
77 return "TxNextBurst";
78 default:
79 return "GSM_UnknownEvent";
80 }
81}
82/**
83This function is called by NetworkStack.dll, to free the GSM protocol control packets.
84*/
85_declspec(dllexport) int fn_NetSim_GSM_FreePacket(NetSim_PACKET* pstruPacket)
86{
87 return fn_NetSim_Cellular_FreePacket(pstruPacket);
88}
89/**
90This function is called by NetworkStack.dll, to copy the GSM protocol
91related information to a new packet
92*/
93_declspec(dllexport) int fn_NetSim_GSM_CopyPacket(NetSim_PACKET* pstruDestPacket,NetSim_PACKET* pstruSrcPacket)
94{
95 return fn_NetSim_Cellular_CopyPacket(pstruDestPacket,pstruSrcPacket);
96}
97/**
98This function writes the GSM metrics in Metrics.txt
99*/
100_declspec(dllexport) int fn_NetSim_GSM_Metrics(char* szMetrics)
101{
102 return fn_NetSim_GSM_Metrics_F(szMetrics);
103}
104/**
105This function is used to configure the packet trace
106*/
107_declspec(dllexport) char* fn_NetSim_GSM_ConfigPacketTrace()
108{
109 return "";
110}
111/**
112This function is used to write the packet trace
113*/
114_declspec(dllexport) int fn_NetSim_GSM_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
115{
116 return 1;
117}
118/**
119This function is called by NetworkStack.dll, once simulation ends, to free the
120allocated memory
121*/
122_declspec(dllexport) int fn_NetSim_GSM_Finish()
123{
124 LinkPacketLog_close();
125 return 1;
126}
127/** This function is used to add a packet to the MAC buffer of the mobile station */
128int fn_NetSim_Cellular_AddPacketToBuffer(NetSim_PACKET* packet,NETSIM_ID nDeviceId,NETSIM_ID nInterfaceId)
129{
130 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(nDeviceId,nInterfaceId);
131 fn_NetSim_Cellular_FormBurst(packet,MSMac);
132 return 1;
133}
134/** This function is used to allocate the channels */
135int fn_NetSim_Cellular_AllocateChannel(NetSim_EVENTDETAILS* pstruEventDetails,NetSim_PACKET* packet)
136{
137 ptrAPPLICATION_INFO* appInfo=(ptrAPPLICATION_INFO*)NETWORK->appInfo;
138 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
139 NETSIM_ID nApplicationId=packet->pstruAppData->nApplicationId;
140 if(MSMac->nMSStatusFlag!=Status_IDLE && MSMac->nMSStatusFlag != Status_CallEnd)
141 {
142 if(MSMac->nApplicationId!=nApplicationId)
143 {
144 unsigned int nProtocol=pstruEventDetails->nProtocolId;
145 NetSim_EVENTDETAILS pevent;
146 APP_CALL_INFO* info=appInfo[nApplicationId-1]->appData;
147 memcpy(&pevent,pstruEventDetails,sizeof pevent);
148 //Notify application layer
149 info->fn_BlockCall(appInfo[nApplicationId-1],
150 packet->nSourceId,
151 get_first_dest_from_packet(packet),
152 pstruEventDetails->dEventTime);
153 pstruEventDetails->nProtocolId=nProtocol;
154 //Delete the packet from access buffer
155 fn_NetSim_Packet_FreePacket(packet);
156 MSMac->MSMetrics.nCallBlocked++;
157 memcpy(pstruEventDetails,&pevent,sizeof* pstruEventDetails);
158 }
159 }
160 else
161 {
162 NetSim_PACKET* request;
163 Cellular_PACKET* cellularPacket=calloc(1,sizeof* cellularPacket);
164 Cellular_CHANNEL_REQUEST* requestData=calloc(1,sizeof* requestData);
165 MSMac->nMSStatusFlag=Status_ChannelRequested;
166 MSMac->nApplicationId=nApplicationId;
167 MSMac->nSourceFlag=1;
168 request=fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
169 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_ChannelRequest),
170 pstruEventDetails->nDeviceId,
171 MSMac->nBTSId,
172 1,
173 pstruEventDetails->nProtocolId);
174 request->pstruMacData->Packet_MACProtocol=cellularPacket;
175 cellularPacket->controlData=requestData;
176 requestData->nApplicationId=nApplicationId;
177 requestData->nMSId=pstruEventDetails->nDeviceId;
178 requestData->nDestId= get_first_dest_from_packet(packet);
179 requestData->nRequestType=PacketType_ChannelRequest;
180 //Add packet to physical out
181 pstruEventDetails->dPacketSize=1;
182 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
183 pstruEventDetails->nPacketId=0;
184 pstruEventDetails->pPacket=request;
185 fnpAddEvent(pstruEventDetails);
186 fn_NetSim_Cellular_AddPacketToBuffer(packet,pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
187 MSMac->MSMetrics.nChannelRequestSent++;
188 }
189 return 1;
190}
191/** This function is called whenever PHYSICAL OUT event is triggered at the base station */
192int fn_NetSim_GSM_BS_PhyOut()
193{
194 Cellular_BS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
195 NetSim_PACKET* packet = pstruEventDetails->pPacket;
196 Cellular_PACKET* gsmPacket=packet->pstruMacData->Packet_MACProtocol;
197 unsigned int nTimeSlot=gsmPacket->nTimeSlot;
198 double dStartTime;
199 double dDataRate=DATA_RATE;
200 packet->pstruPhyData->dArrivalTime=pstruEventDetails->dEventTime;
201 dStartTime=fn_NetSim_GSM_GetPacketStartTime(pstruEventDetails->dEventTime,nTimeSlot);
202
203 packet->pstruPhyData->dOverhead=0;
204 packet->pstruPhyData->dPacketSize=packet->pstruMacData->dPacketSize;
205 packet->pstruPhyData->dPayload=packet->pstruMacData->dPacketSize;
206 packet->pstruPhyData->dStartTime=dStartTime;
207 packet->pstruPhyData->dEndTime=dStartTime+fnGetPacketSize(packet)*8/dDataRate;
208 pstruEventDetails->dEventTime=packet->pstruPhyData->dEndTime;
209 pstruEventDetails->nDeviceId=packet->nReceiverId;
210 pstruEventDetails->nDeviceType=DEVICE_TYPE(packet->nReceiverId);
211 pstruEventDetails->nEventType=PHYSICAL_IN_EVENT;
212 pstruEventDetails->nInterfaceId=1;
213 fnpAddEvent(pstruEventDetails);
214 //Call packet trace function
215 fn_NetSim_WritePacketTrace(packet);
216 fn_NetSim_Metrics_Add(packet);
217 return 1;
218}
219/** This function is called at the end of a call */
220int fn_NetSim_Cellular_SendCallend(NETSIM_ID nMSID,NETSIM_ID nMSInterface,NETSIM_ID nDestinationId,double time)
221{
222 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(nMSID,nMSInterface);
223 NETSIM_ID nApplicationId=MSMac->nApplicationId;
224 NetSim_PACKET* packet=fn_NetSim_Cellular_createPacket(time,
225 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_CallEnd),
226 nMSID,
227 nDestinationId,
228 1,
229 pstruEventDetails->nProtocolId);
230 Cellular_PACKET* gsmPacket=calloc(1,sizeof* gsmPacket);
231 gsmPacket->nApplicationId=nApplicationId;
232 gsmPacket->nTimeSlot=MSMac->pstruAllocatedChannel->nTimeSlot;
233 packet->pstruMacData->Packet_MACProtocol=gsmPacket;
234 //Add physical out event
235 pstruEventDetails->dEventTime=time;
236 pstruEventDetails->dPacketSize=1;
237 pstruEventDetails->nApplicationId=nApplicationId;
238 pstruEventDetails->nDeviceId=nMSID;
239 pstruEventDetails->nDeviceType=MOBILESTATION;
240 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
241 pstruEventDetails->nInterfaceId=nMSInterface;
242 pstruEventDetails->nPacketId=0;
243 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
244 pstruEventDetails->nSegmentId=0;
245 pstruEventDetails->nSubEventType=0;
246 pstruEventDetails->pPacket=packet;
247 fnpAddEvent(pstruEventDetails);
248 //Release the channel
249 fn_NetSim_Cellular_MS_SendChannelRelease(MSMac->pstruAllocatedChannel,nMSID,nMSInterface,time+GSM_CARRIER_LENGTH);
250 if(MSMac->nMSStatusFlag==Status_CallInProgress)
251 MSMac->nMSStatusFlag=Status_IDLE;
252 else
253 MSMac->nMSStatusFlag=Status_CallEnd;
254 return 1;
255}