NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Cellular.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/**
21This function is called by NetworkStack.dll, whenever the event gets triggered
22inside the NetworkStack.dll for CDMA or GSM protocols. This is the main function for GSM / CDMA.
23It processes MAC_OUT,MAC_IN, PHYSICAL_OUT, PHYSICAL_IN and TIMER events for mobile station
24and base station.
25 */
26int fn_NetSim_Cellular_Run()
27{
28 switch(pstruEventDetails->nEventType)
29 {
30 case MAC_OUT_EVENT:
31 {
32 switch(pstruEventDetails->nDeviceType)
33 {
34 case MOBILESTATION:
35 {
36 NetSim_BUFFER* buffer=DEVICE_MAC_NW_INTERFACE(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId)->pstruAccessBuffer;
37 do
38 {
39 NetSim_PACKET* packet=fn_NetSim_Packet_GetPacketFromBuffer(buffer,1);
40 NETSIM_ID nAppId=packet->pstruAppData->nApplicationId;
41 if(isCellularChannelAllocated(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId,nAppId))
42 fn_NetSim_Cellular_AddPacketToBuffer(packet,pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
43 else
44 {
45 ((Cellular_MS_MAC*)DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId))->MSMetrics.nCallGenerated++;
46 fn_NetSim_Cellular_AllocateChannel(pstruEventDetails,packet);
47 }
48 }while(fn_NetSim_GetBufferStatus(buffer));
49 }
50 break;
51 }
52 }
53 break;
54 case MAC_IN_EVENT:
55 {
56 switch(pstruEventDetails->nDeviceType)
57 {
58 case MOBILESTATION:
59 {
60 NetSim_PACKET* packet=pstruEventDetails->pPacket;
61 switch(packet->nControlDataType)
62 {
63 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelUngranted):
64 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelGranted):
65 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelUngranted):
66 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelGranted):
67 fn_NetSim_Cellular_ChannelResponse(packet);
68 fn_NetSim_Packet_FreePacket(packet);
69 break;
70 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_CallRequest):
71 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_CallRequest):
72 fn_NetSim_Cellular_MS_ProcessCallRequest();
73 break;
74 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_CallAccepted):
75 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_CallRejected):
76 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_CallAccepted):
77 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_CallRejected):
78 fn_NetSim_Cellular_MS_ProcessCallResponse();
79 break;
80 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_CallEnd):
81 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_CallEnd):
82 {
83 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
84 if(MSMac->pstruAllocatedChannel)
85 fn_NetSim_Cellular_MS_SendChannelRelease(MSMac->pstruAllocatedChannel,pstruEventDetails->nDeviceId,
86 pstruEventDetails->nInterfaceId,
87 pstruEventDetails->dEventTime);
88 MSMac->nApplicationId=0;
89 MSMac->nSourceFlag=0;
90 MSMac->nMSStatusFlag=Status_IDLE;
91 }
92 break;
93 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_DropCall):
94 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_DropCall):
95 fn_NetSim_Cellular_DropCall();
96 break;
97 default:
98 fn_NetSim_Cellular_MS_ReassembleBurst();
99 break;
100 }
101 }
102 break;
103 case BASESTATION:
104 {
105 if(NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->ppstruInterfaceList[pstruEventDetails->nInterfaceId-1]->pstruPhysicalLayer->nPhyMedium==PHY_MEDIUM_WIRELESS)
106 {
107 NetSim_PACKET* packet=pstruEventDetails->pPacket;
108 switch(packet->nControlDataType)
109 {
110 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelRequestForHandover):
111 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelRequest):
112 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelRequestForIncoming):
113 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelRequestForHandover):
114 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelRequest):
115 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelRequestForIncoming):
116 fn_NetSim_Cellular_allocateChannel(packet);
117 break;
118 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelRelease):
119 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelRelease):
120 fn_NetSim_Cellular_BS_ReleaseChannel();
121 break;
122 default:
123 fn_NetSim_Cellular_ForwardToMSC();
124 break;
125 }
126 }
127 else
128 {
129 if(DEVICE_MACLAYER(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId)->nMacProtocolId == MAC_PROTOCOL_GSM)
130 {
131 int flag=0;
132 fn_NetSim_Cellular_BS_AssignTimeSlot(pstruEventDetails->pPacket,pstruEventDetails->nDeviceId);
133 if(pstruEventDetails->pPacket->pstruAppData && pstruEventDetails->pPacket->pstruAppData->nApplicationId)
134 flag = isCellularChannelAllocated(get_first_dest_from_packet(pstruEventDetails->pPacket),1,pstruEventDetails->pPacket->pstruAppData->nApplicationId);
135 if(flag || pstruEventDetails->pPacket->nControlDataType/100 == MAC_PROTOCOL_GSM)
136 {
137 pstruEventDetails->pPacket->nTransmitterId=pstruEventDetails->nDeviceId;
138 pstruEventDetails->pPacket->nReceiverId= get_first_dest_from_packet(pstruEventDetails->pPacket);
139 pstruEventDetails->nInterfaceId=1;
140 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
141 fnpAddEvent(pstruEventDetails);
142 }
143 else
144 {
145 //Drop the packet
146 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
147 }
148 }
149 else
150 {
151 pstruEventDetails->pPacket->nTransmitterId=pstruEventDetails->nDeviceId;
152 pstruEventDetails->pPacket->nReceiverId= get_first_dest_from_packet(pstruEventDetails->pPacket);
153 pstruEventDetails->nInterfaceId=1;
154 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
155 fnpAddEvent(pstruEventDetails);
156 }
157 }
158 }
159 break;
160 case MSC:
161 {
162 fn_NetSim_Cellular_Msc_ProcessPacket();
163 }
164 break;
165 }
166 }
167 break;
168 case PHYSICAL_OUT_EVENT:
169 {
170 switch(pstruEventDetails->nDeviceType)
171 {
172 case MOBILESTATION:
173 {
174 fn_NetSim_Cellular_MS_PhyOut();
175 }
176 break;
177 case BASESTATION:
178 if(NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->ppstruInterfaceList[pstruEventDetails->nInterfaceId-1]->pstruPhysicalLayer->nPhyMedium==PHY_MEDIUM_WIRELESS)
179 fn_NetSim_GSM_BS_PhyOut();
180 else if(NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->ppstruInterfaceList[pstruEventDetails->nInterfaceId-1]->pstruPhysicalLayer->nPhyMedium==PHY_MEDIUM_WIRED)
181 fn_NetSim_Cellular_TransmitOnwireline();
182 else
183 {
184 fnNetSimError("Unknown phy medium %d for device %d Interface %d",NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->ppstruInterfaceList[pstruEventDetails->nInterfaceId-1]->pstruPhysicalLayer->nPhyMedium,pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
185 }
186 break;
187 case MSC:
188 fn_NetSim_Cellular_TransmitOnwireline();
189 break;
190 }
191 }
192 break;
193 case PHYSICAL_IN_EVENT:
194 {
195 LinkPacketLog();
196 pstruEventDetails->nEventType=MAC_IN_EVENT;
197 fnpAddEvent(pstruEventDetails);
198 }
199 break;
200 case TIMER_EVENT:
201 {
202 switch(pstruEventDetails->nSubEventType)
203 {
204 case CELLULAR_SUBEVENT(MAC_PROTOCOL_CDMA,Subevent_DropCall):
205 case CELLULAR_SUBEVENT(MAC_PROTOCOL_GSM,Subevent_DropCall):
206 fn_NetSim_Cellular_DropCall();
207 break;
208 case CELLULAR_SUBEVENT(MAC_PROTOCOL_GSM,Subevent_TxNextBurst):
209 case CELLULAR_SUBEVENT(MAC_PROTOCOL_CDMA,Subevent_TxNextBurst):
210 {
211 NetSim_PACKET**** list=pstruEventDetails->szOtherDetails;
212 list[pstruEventDetails->pPacket->pstruAppData->nApplicationId][pstruEventDetails->pPacket->nSourceId-1][get_first_dest_from_packet(pstruEventDetails->pPacket)-1]=list[pstruEventDetails->pPacket->pstruAppData->nApplicationId][pstruEventDetails->pPacket->nSourceId-1][get_first_dest_from_packet(pstruEventDetails->pPacket)-1]->pstruNextPacket;
213 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
214 pstruEventDetails->nSubEventType=0;
215 pstruEventDetails->szOtherDetails=NULL;
216 fnpAddEvent(pstruEventDetails);
217 }
218 break;
219 default:
220 fnNetSimError("Unknown timer event for cellular protocol");
221 break;
222 }
223 }
224 break;
225 default:
226 fnNetSimError("Unknown event type for cellular protocol");
227 break;
228 }
229 return 1;
230}
231/**
232This function is used to allocate the channels.
233Channel allocation deals with the allocation of channels to cells in a cellular network.
234Once the channels are allocated, cells may then allow users within the cell to communicate
235via the available channels.
236*/
237int fn_NetSim_Cellular_allocateChannel(NetSim_PACKET* packet)
238{
239 Cellular_CHANNEL_RESPONSE* response=calloc(1,sizeof* response);
240 Cellular_PACKET* gsmPacket=packet->pstruMacData->Packet_MACProtocol;
241 Cellular_CHANNEL_REQUEST* request=gsmPacket->controlData;
242 Cellular_BS_MAC* BSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
243 Cellular_CHANNEL* channel=BSMac->pstruChannelList;
244 NetSim_PACKET* returnPacket;
245 unsigned int nPacketType;
246 while(channel)
247 {
248 if(channel->nAllocationFlag == 0 &&
249 channel->nChannelType==ChannelType_TRAFFICCHANNEL)
250 {
251 channel->nAllocationFlag=1;
252 channel->nApplicationId=request->nApplicationId;
253 channel->nMSId=request->nMSId;
254 channel->nDestId=request->nDestId;
255 break;
256 }
257 channel=channel->pstru_NextChannel;
258 }
259
260 if(channel)
261 {
262 response->channel=channel;
263 response->nAllocationFlag=1;
264 nPacketType=CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_ChannelGranted);
265 }
266 else
267 {
268 response->channel=calloc(1,sizeof* response->channel);
269 response->channel->nApplicationId=request->nApplicationId;
270 response->channel->nMSId=request->nMSId;
271 response->channel->nDestId=request->nDestId;
272 nPacketType=CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_ChannelUngranted);
273 }
274 returnPacket=fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
275 nPacketType,
276 pstruEventDetails->nDeviceId,
277 request->nMSId,
278 1,
279 pstruEventDetails->nProtocolId);
280 gsmPacket=calloc(1,sizeof* gsmPacket);
281 gsmPacket->controlData=response;
282 returnPacket->pstruMacData->Packet_MACProtocol=gsmPacket;
283 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
284 pstruEventDetails->pPacket=returnPacket;
285 fnpAddEvent(pstruEventDetails);
286 return 1;
287}
288/**
289This functio is called to perform the channel response functionality
290This function determines if the call is accepted or rejected based on channel availability
291*/
292int fn_NetSim_Cellular_ChannelResponse(NetSim_PACKET* packet)
293{
294 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
295 Cellular_PACKET* gsmPacket=packet->pstruMacData->Packet_MACProtocol;
296 Cellular_CHANNEL_RESPONSE* response=gsmPacket->controlData;
297 if(response->nAllocationFlag==1 && MSMac->nMSStatusFlag==Status_ChannelRequested)
298 {
299 NetSim_PACKET* packet;
300 Cellular_PACKET* gsmPacket;
301 Cellular_CHANNEL* channel=response->channel;
302 MSMac->pstruAllocatedChannel=channel;
303 packet = fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
304 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_CallRequest),
305 channel->nMSId,
306 channel->nDestId,
307 1,
308 pstruEventDetails->nProtocolId);
309 gsmPacket=calloc(1,sizeof* gsmPacket);
310 gsmPacket->nApplicationId=channel->nApplicationId;
311 packet->pstruMacData->Packet_MACProtocol=gsmPacket;
312 pstruEventDetails->dPacketSize=fnGetPacketSize(packet);
313 pstruEventDetails->nApplicationId=0;
314 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
315 pstruEventDetails->nPacketId=0;
316 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
317 pstruEventDetails->nSegmentId=0;
318 pstruEventDetails->pPacket=packet;
319 fnpAddEvent(pstruEventDetails);
320 MSMac->nMSStatusFlag=Status_CallRequested;
321 MSMac->MSMetrics.nCallRequestSent++;
322 }
323 else if(response->nAllocationFlag==1 && MSMac->nMSStatusFlag==Status_ChannelRequestedForIncoming)
324 {
325 //Generate call accepted message
326 NetSim_PACKET* packet;
327 Cellular_PACKET* gsmPacket;
328 Cellular_CHANNEL* channel=response->channel;
329 MSMac->pstruAllocatedChannel=channel;
330 packet = fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
331 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_CallAccepted),
332 channel->nMSId,
333 channel->nDestId,
334 1,
335 pstruEventDetails->nProtocolId);
336 gsmPacket=calloc(1,sizeof* gsmPacket);
337 gsmPacket->nApplicationId=channel->nApplicationId;
338 gsmPacket->nTimeSlot=channel->nTimeSlot;
339 packet->pstruMacData->Packet_MACProtocol=gsmPacket;
340 pstruEventDetails->dPacketSize=fnGetPacketSize(packet);
341 pstruEventDetails->nApplicationId=0;
342 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
343 pstruEventDetails->nPacketId=0;
344 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
345 pstruEventDetails->nSegmentId=0;
346 pstruEventDetails->pPacket=packet;
347 fnpAddEvent(pstruEventDetails);
348 MSMac->nMSStatusFlag=Status_CallInProgress;
349 MSMac->MSMetrics.nCallAccepted++;
350 }
351 else if(MSMac->nMSStatusFlag==Status_ChannelRequestedForHandover)
352 {
353 fn_NetSim_Cellular_ChannelResponseForHandover();
354 }
355 else if(response->nAllocationFlag==0 && MSMac->nMSStatusFlag==Status_ChannelRequested)
356 {
357 unsigned int nProtocol=pstruEventDetails->nProtocolId;
358 Cellular_CHANNEL* channel=response->channel;
359 NetSim_PACKET* packet=MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1];
360 ptrAPPLICATION_INFO* appInfo=(ptrAPPLICATION_INFO*)NETWORK->appInfo;
361 APP_CALL_INFO* info=appInfo[packet->pstruAppData->nApplicationId-1]->appData;
362 info->fn_BlockCall(appInfo[packet->pstruAppData->nApplicationId-1],
363 packet->nSourceId,
364 get_first_dest_from_packet(packet),
365 pstruEventDetails->dEventTime);
366 pstruEventDetails->nProtocolId=nProtocol;
367 while(packet)
368 {
369 NetSim_PACKET* temp=packet->pstruNextPacket;
370 fn_NetSim_Packet_FreePacket(packet);
371 packet=temp;
372 }
373 MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1]=NULL;
374 MSMac->nMSStatusFlag=Status_IDLE;
375 MSMac->nSourceFlag=0;
376 MSMac->nApplicationId=0;
377 MSMac->MSMetrics.nCallBlocked++;
378 }
379 else if(response->nAllocationFlag==0 && MSMac->nMSStatusFlag==Status_ChannelRequestedForIncoming)
380 {
381 //Generate call rejected message
382 NetSim_PACKET* packet;
383 Cellular_PACKET* gsmPacket;
384 Cellular_CHANNEL* channel=response->channel;
385 MSMac->pstruAllocatedChannel=NULL;
386 packet = fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
387 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_CallRejected),
388 channel->nMSId,
389 channel->nDestId,
390 1,
391 pstruEventDetails->nProtocolId);
392 gsmPacket=calloc(1,sizeof* gsmPacket);
393 gsmPacket->nApplicationId=channel->nApplicationId;
394 gsmPacket->nTimeSlot=0;
395 packet->pstruMacData->Packet_MACProtocol=gsmPacket;
396 pstruEventDetails->dPacketSize=fnGetPacketSize(packet);
397 pstruEventDetails->nApplicationId=0;
398 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
399 pstruEventDetails->nPacketId=0;
400 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
401 pstruEventDetails->nSegmentId=0;
402 pstruEventDetails->pPacket=packet;
403 fnpAddEvent(pstruEventDetails);
404 MSMac->nMSStatusFlag=Status_IDLE;
405 MSMac->nApplicationId=0;
406 MSMac->MSMetrics.nCallRejected++;
407 }
408 else if(MSMac->nMSStatusFlag==Status_CallEnd)
409 {
410 //Free the channel
411 Cellular_CHANNEL* channel=response->channel;
412 channel->nAllocationFlag=0;
413 channel->nApplicationId=0;
414 channel->nDestId=0;
415 channel->nMSId=0;
416 MSMac->nMSStatusFlag=Status_IDLE;
417 }
418 else
419 {
420 //assert(false);//Unknown condition
421 }
422 return 1;
423}
424/**
425This functio is called to forward the packet to the MSC
426*/
427int fn_NetSim_Cellular_ForwardToMSC()
428{
429 pstruEventDetails->nInterfaceId=2;
430 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
431 fnpAddEvent(pstruEventDetails);
432 return 0;
433}
434/**
435This function is used to tranmit packet on wired link
436*/
437
438int fn_NetSim_Cellular_TransmitOnwireline()
439{
440 NETSIM_ID nConnectedDevice,nConnectedInterface,nLinkId;
441 double dDataRate;
442 NetSim_PACKET* packet=pstruEventDetails->pPacket;
443 nLinkId=fn_NetSim_Stack_GetConnectedDevice(pstruEventDetails->nDeviceId,
444 pstruEventDetails->nInterfaceId,
445 &(nConnectedDevice),
446 &(nConnectedInterface));
447 dDataRate = NETWORK->ppstruNetSimLinks[nLinkId-1]->puniMedProp.pstruWiredLink.dDataRateDown;
448 packet->pstruPhyData->dArrivalTime=pstruEventDetails->dEventTime;
449 packet->pstruPhyData->dEndTime=pstruEventDetails->dEventTime+fnGetPacketSize(packet)*8/dDataRate;
450 packet->pstruPhyData->dStartTime=pstruEventDetails->dEventTime;
451 packet->nTransmitterId=pstruEventDetails->nDeviceId;
452 packet->nReceiverId=nConnectedDevice;
453 pstruEventDetails->dEventTime=packet->pstruPhyData->dEndTime;
454 pstruEventDetails->nDeviceId=nConnectedDevice;
455 pstruEventDetails->nDeviceType=DEVICE_TYPE(nConnectedDevice);
456 pstruEventDetails->nEventType=PHYSICAL_IN_EVENT;
457 pstruEventDetails->nInterfaceId=nConnectedInterface;
458 fnpAddEvent(pstruEventDetails);
459 //Call packet trace function
460 fn_NetSim_WritePacketTrace(packet);
461 fn_NetSim_Metrics_Add(packet);
462 return 1;
463}
464/**
465The VisitorLocationRegister(VLR) contains a copy of most of the data stored at the HomeLocationRegister(HLR). It is, however,
466temporary data which exists for only as long as the subscriber is “active”
467in the particular area covered by the VLR. The VLR provides a local database for
468the subscribers wherever they are physically located within a PLMN, this may or may not
469be the “home” system. This function eliminates the need for excessive and time-consuming
470references to the “home” HLR database.
471
472*/
473int fn_NetSim_Cellular_VLR(NETSIM_ID nMSCId,NETSIM_ID nId,NETSIM_ID* nBTSId,NETSIM_ID* nInterfaceId)
474{
475 DEVVAR_MSC* devVar=NETWORK->ppstruDeviceList[nMSCId-1]->deviceVar;
476 if(devVar->VLRList[nId-1])
477 {
478 *nBTSId=devVar->VLRList[nId-1]->nBTSId;
479 *nInterfaceId=devVar->VLRList[nId-1]->nInterfaceId;
480 }
481 else
482 {
483 *nBTSId=0;
484 *nInterfaceId=0;
485 }
486 return 0;
487}
488/** This function is used to process the packet at the MSC */
489int fn_NetSim_Cellular_Msc_ProcessPacket()
490{
491 NETSIM_ID nBTSId,nInterfaceId;
492 NetSim_PACKET* packet=pstruEventDetails->pPacket;
493 NETSIM_ID nSourceId=packet->nSourceId;
494 NETSIM_ID nDestinationId= get_first_dest_from_packet(packet);
495 //Contact VLR list
496 fn_NetSim_Cellular_VLR(pstruEventDetails->nDeviceId,nDestinationId,&nBTSId,&nInterfaceId);
497 if(nBTSId)
498 {
499 packet->nTransmitterId=pstruEventDetails->nDeviceId;
500 packet->nReceiverId=nBTSId;
501 pstruEventDetails->nInterfaceId=nInterfaceId;
502 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
503 fnpAddEvent(pstruEventDetails);
504 }
505 else
506 assert(false);
507 return 1;
508}
509/** This function is used to process the call request at the mobile station */
510int fn_NetSim_Cellular_MS_ProcessCallRequest()
511{
512 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
513 NetSim_PACKET* packet=pstruEventDetails->pPacket;
514 NETSIM_ID nSouceId= get_first_dest_from_packet(packet);
515 NETSIM_ID nDestId=packet->nSourceId;
516 MSMac->MSMetrics.nCallRequestReceived++;
517 if(MSMac->nMSStatusFlag == Status_IDLE || MSMac->nMSStatusFlag==Status_CallEnd)
518 {
519 NetSim_PACKET* request;
520 Cellular_PACKET* gsmPacket=calloc(1,sizeof* gsmPacket);
521 Cellular_CHANNEL_REQUEST* requestData=calloc(1,sizeof* requestData);
522 MSMac->nMSStatusFlag=Status_ChannelRequested;
523 MSMac->nApplicationId=((Cellular_PACKET*)packet->pstruMacData->Packet_MACProtocol)->nApplicationId;
524 request=fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
525 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_ChannelRequestForIncoming),
526 pstruEventDetails->nDeviceId,
527 MSMac->nBTSId,
528 1,
529 pstruEventDetails->nProtocolId);
530 request->pstruMacData->Packet_MACProtocol=gsmPacket;
531 gsmPacket->controlData=requestData;
532 requestData->nApplicationId=((Cellular_PACKET*)packet->pstruMacData->Packet_MACProtocol)->nApplicationId;;
533 requestData->nMSId=pstruEventDetails->nDeviceId;
534 requestData->nDestId=nDestId;
535 requestData->nRequestType=PacketType_ChannelRequestForIncoming;
536 //Add packet to physical out
537 pstruEventDetails->dPacketSize=1;
538 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
539 pstruEventDetails->nPacketId=0;
540 pstruEventDetails->pPacket=request;
541 fnpAddEvent(pstruEventDetails);
542 MSMac->nMSStatusFlag=Status_ChannelRequestedForIncoming;
543 MSMac->MSMetrics.nChannelRequestSent++;
544 }
545 else
546 {
547 //Call rejected
548 //Generate call rejected message
549 NetSim_PACKET* response;
550 Cellular_PACKET* gsmPacket;
551 response = fn_NetSim_Cellular_createPacket(pstruEventDetails->dEventTime,
552 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId, PacketType_CallRejected),
553 get_first_dest_from_packet(packet),
554 packet->nSourceId,
555 1,
556 pstruEventDetails->nProtocolId);
557 gsmPacket = calloc(1, sizeof* gsmPacket);
558 gsmPacket->nApplicationId = ((Cellular_PACKET*)packet->pstruMacData->Packet_MACProtocol)->nApplicationId;
559 gsmPacket->nTimeSlot=0;
560 response->pstruMacData->Packet_MACProtocol=gsmPacket;
561 pstruEventDetails->dPacketSize=fnGetPacketSize(packet);
562 pstruEventDetails->nApplicationId=0;
563 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
564 pstruEventDetails->nPacketId=0;
565 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
566 pstruEventDetails->nSegmentId=0;
567 pstruEventDetails->pPacket=response;
568 fnpAddEvent(pstruEventDetails);
569 MSMac->MSMetrics.nCallRejected++;
570 }
571 return 1;
572}
573/** This function is used to process the call response at the mobile station */
574int fn_NetSim_Cellular_MS_ProcessCallResponse()
575{
576 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
577 switch(pstruEventDetails->pPacket->nControlDataType)
578 {
579 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_CallAccepted):
580 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_CallAccepted):
581 {
582 Cellular_CHANNEL* channel=MSMac->pstruAllocatedChannel;
583 NetSim_PACKET* packet=MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1];
584 Cellular_PACKET* gsmPacket=packet->pstruMacData->Packet_MACProtocol;
585 MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1] = MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1]->pstruNextPacket;
586 MSMac->nMSStatusFlag=Status_CallInProgress;
587 packet->pstruMacData->dEndTime=pstruEventDetails->dEventTime;
588 if(pstruEventDetails->nProtocolId==MAC_PROTOCOL_GSM)
589 packet->pstruMacData->dOverhead=GSM_OVERHEAD;
590 else
591 packet->pstruMacData->dOverhead=0;
592 packet->pstruMacData->dPacketSize=packet->pstruMacData->dOverhead+packet->pstruMacData->dPayload;
593 packet->pstruMacData->dStartTime=pstruEventDetails->dEventTime;
594 gsmPacket=packet->pstruMacData->Packet_MACProtocol;
595 gsmPacket->nTimeSlot=channel->nTimeSlot;
596 //Write physical out event
597 pstruEventDetails->dPacketSize=fnGetPacketSize(packet);
598 pstruEventDetails->nApplicationId=packet->pstruAppData->nApplicationId;
599 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
600 pstruEventDetails->nPacketId=packet->nPacketId;
601 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
602 pstruEventDetails->nSegmentId=packet->pstruAppData->nSegmentId;
603 //Free old packet
604 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
605 pstruEventDetails->pPacket=packet;
606 fnpAddEvent(pstruEventDetails);
607 }
608 break;
609 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_CallRejected):
610 case CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_CallRejected):
611 {
612 NetSim_PACKET* current=pstruEventDetails->pPacket;
613 unsigned int nProtocol=pstruEventDetails->nProtocolId;
614 NETSIM_ID nMSId = pstruEventDetails->nDeviceId;
615 NETSIM_ID nMSInterface=pstruEventDetails->nInterfaceId;
616 double time=pstruEventDetails->dEventTime;
617 Cellular_CHANNEL* channel=MSMac->pstruAllocatedChannel;
618 NetSim_PACKET* packet=MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1];
619 ptrAPPLICATION_INFO* appInfo=(ptrAPPLICATION_INFO*)NETWORK->appInfo;
620 APP_CALL_INFO* info=appInfo[packet->pstruAppData->nApplicationId-1]->appData;
621 info->fn_BlockCall(appInfo[packet->pstruAppData->nApplicationId-1],
622 packet->nSourceId,
623 get_first_dest_from_packet(packet),
624 pstruEventDetails->dEventTime);
625 pstruEventDetails->nProtocolId=nProtocol;
626 fn_NetSim_Cellular_MS_SendChannelRelease(channel,nMSId,nMSInterface,time);
627 while(packet)
628 {
629 NetSim_PACKET* temp=packet->pstruNextPacket;
630 fn_NetSim_Packet_FreePacket(packet);
631 packet=temp;
632 }
633 MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1]=NULL;
634 MSMac->nMSStatusFlag=Status_IDLE;
635 MSMac->MSMetrics.nCallBlocked++;
636 fn_NetSim_Packet_FreePacket(current);
637 }
638 break;
639 }
640 return 1;
641}
642/** This function runs at base station to assign the time slots */
643int fn_NetSim_Cellular_BS_AssignTimeSlot(NetSim_PACKET* packet,NETSIM_ID nBTSId)
644{
645 Cellular_PACKET* gsmPacket=packet->pstruMacData->Packet_MACProtocol;
646 NETSIM_ID nApplicationId=gsmPacket->nApplicationId;
647 gsmPacket->nTimeSlot=0;
648 if(!nApplicationId)
649 {
650 assert(false);
651 }
652 else
653 {
654 Cellular_BS_MAC* BSMac=DEVICE_MACVAR(nBTSId,1);
655 Cellular_CHANNEL* channel=BSMac->pstruChannelList;
656 while(channel)
657 {
658 if(channel->nAllocationFlag==1 && channel->nApplicationId==nApplicationId
659 && channel->nMSId == get_first_dest_from_packet(packet))
660 {
661 gsmPacket->nTimeSlot=channel->nTimeSlot;
662 break;
663 }
664 channel=channel->pstru_NextChannel;
665 }
666 }
667 return 1;
668}
669/** This function is used by the mobile station to send the request for channel release to the base station */
670int fn_NetSim_Cellular_MS_SendChannelRelease(Cellular_CHANNEL* channel,NETSIM_ID nMSId,NETSIM_ID nMSInterface,double time)
671{
672 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(nMSId,nMSInterface);
673 NetSim_PACKET* packet = fn_NetSim_Cellular_createPacket(time,
674 CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_ChannelRelease),
675 channel->nMSId,
676 MSMac->nBTSId,
677 1,
678 pstruEventDetails->nProtocolId);
679 Cellular_PACKET* gsmPacket=calloc(1,sizeof* gsmPacket);
680 gsmPacket->nApplicationId=channel->nApplicationId;
681 gsmPacket->nTimeSlot=0;
682 packet->pstruMacData->Packet_MACProtocol=gsmPacket;
683 //Write physical out event
684 pstruEventDetails->dEventTime=time;
685 pstruEventDetails->dPacketSize=1;
686 pstruEventDetails->nApplicationId=0;
687 pstruEventDetails->nDeviceId=nMSId;
688 pstruEventDetails->nDeviceType=DEVICE_TYPE(nMSId);
689 pstruEventDetails->nEventType=PHYSICAL_OUT_EVENT;
690 pstruEventDetails->nInterfaceId=nMSInterface;
691 pstruEventDetails->nPacketId=0;
692 pstruEventDetails->nProtocolId=pstruEventDetails->nProtocolId;
693 pstruEventDetails->nSegmentId=0;
694 pstruEventDetails->nSubEventType=0;
695 pstruEventDetails->pPacket=packet;
696 fnpAddEvent(pstruEventDetails);
697 MSMac->pstruAllocatedChannel=NULL;
698 return 1;
699}
700/** This funtion is used by the base station to release the assigned channels */
701int fn_NetSim_Cellular_BS_ReleaseChannel()
702{
703 Cellular_BS_MAC* BSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
704 Cellular_CHANNEL* channel=BSMac->pstruChannelList;
705 NETSIM_ID nApplicationId=((Cellular_PACKET*)pstruEventDetails->pPacket->pstruMacData->Packet_MACProtocol)->nApplicationId;
706 NETSIM_ID nMSId=pstruEventDetails->pPacket->nSourceId;
707 while(channel)
708 {
709 if(channel->nAllocationFlag==1 && channel->nMSId==nMSId && channel->nApplicationId==nApplicationId)
710 {
711 channel->nAllocationFlag=0;
712 channel->nApplicationId=0;
713 channel->nDestId=0;
714 channel->nMSId=0;
715 break;
716 }
717 channel=channel->pstru_NextChannel;
718 }
719 return 1;
720}
721/** This funcion is called at the mobile station when the PHYSICAL_OUT event is triggered */
722int fn_NetSim_Cellular_MS_PhyOut()
723{
724 NETSIM_ID nMSID=pstruEventDetails->nDeviceId;
725 NETSIM_ID nMSInterface=pstruEventDetails->nInterfaceId;
726 Cellular_MS_MAC* MSMac=DEVICE_MACVAR(pstruEventDetails->nDeviceId,pstruEventDetails->nInterfaceId);
727 NetSim_PACKET* packet = pstruEventDetails->pPacket;
728 Cellular_PACKET* gsmPacket=packet->pstruMacData->Packet_MACProtocol;
729 unsigned int nTimeSlot=gsmPacket->nTimeSlot;
730 double dStartTime;
731 double dDataRate=DATA_RATE;
732 packet->pstruPhyData->dArrivalTime=pstruEventDetails->dEventTime;
733 if(pstruEventDetails->nProtocolId==MAC_PROTOCOL_GSM)
734 dStartTime=fn_NetSim_GSM_GetPacketStartTime(pstruEventDetails->dEventTime,nTimeSlot);
735 else
736 dStartTime=pstruEventDetails->dEventTime;
737 packet->pstruPhyData->dOverhead=0;
738 packet->pstruPhyData->dPacketSize=packet->pstruMacData->dPacketSize;
739 packet->pstruPhyData->dPayload=packet->pstruMacData->dPacketSize;
740 packet->pstruPhyData->dStartTime=dStartTime;
741 packet->pstruPhyData->dEndTime=dStartTime+fnGetPacketSize(packet)*8/dDataRate;
742 if(packet->nControlDataType == CELLULAR_PACKET_TYPE(MAC_PROTOCOL_GSM,PacketType_ChannelRequestForHandover) ||
743 packet->nControlDataType == CELLULAR_PACKET_TYPE(MAC_PROTOCOL_CDMA,PacketType_ChannelRequestForHandover))
744 packet->nReceiverId=MSMac->nNewBTS;
745 else
746 packet->nReceiverId=MSMac->nBTSId;
747 packet->nTransmitterId=pstruEventDetails->nDeviceId;
748 pstruEventDetails->dEventTime=packet->pstruPhyData->dEndTime;
749 pstruEventDetails->nDeviceId=packet->nReceiverId;
750 pstruEventDetails->nDeviceType=BASESTATION;
751 pstruEventDetails->nEventType=PHYSICAL_IN_EVENT;
752 pstruEventDetails->nInterfaceId=MSMac->nBTSInterface;
753 fnpAddEvent(pstruEventDetails);
754 DEVICE_PHYLAYER(nMSID,nMSInterface)->dLastPacketEndTime = pstruEventDetails->dEventTime;
755 //Call packet trace function
756 fn_NetSim_WritePacketTrace(packet);
757 fn_NetSim_Metrics_Add(packet);
758 //Check for next packet
759 if(packet->nControlDataType==CELLULAR_PACKET_TYPE(pstruEventDetails->nProtocolId,PacketType_DropCall))
760 {
761 pstruEventDetails->dPacketSize=0;
762 pstruEventDetails->nApplicationId=0;
763 pstruEventDetails->nDeviceId=nMSID;
764 pstruEventDetails->nDeviceType=MOBILESTATION;
765 pstruEventDetails->nEventType=TIMER_EVENT;
766 pstruEventDetails->nInterfaceId=nMSInterface;
767 pstruEventDetails->nPacketId=0;
768 pstruEventDetails->nSegmentId=0;
769 pstruEventDetails->nSubEventType=CELLULAR_SUBEVENT(pstruEventDetails->nProtocolId,Subevent_DropCall);
770 pstruEventDetails->pPacket=NULL;
771 fnpAddEvent(pstruEventDetails);
772 }
773 else if(MSMac->pstruAllocatedChannel &&
774 MSMac->pstruPacketList &&
775 MSMac->pstruPacketList[pstruEventDetails->nApplicationId][packet->nSourceId-1][get_first_dest_from_packet(packet)-1])
776 {
777 //Transmit next packet
778 Cellular_PACKET* gsmPacket;
779 Cellular_CHANNEL* channel=MSMac->pstruAllocatedChannel;
780 NetSim_PACKET* packet=MSMac->pstruPacketList[channel->nApplicationId][channel->nMSId-1][channel->nDestId-1];
781 packet->pstruMacData->dEndTime=pstruEventDetails->dEventTime;
782 if(pstruEventDetails->nProtocolId==MAC_PROTOCOL_GSM)
783 packet->pstruMacData->dOverhead=GSM_OVERHEAD;
784 else
785 packet->pstruMacData->dOverhead=0;
786 packet->pstruMacData->dPacketSize=packet->pstruMacData->dOverhead+packet->pstruMacData->dPayload;
787 packet->pstruMacData->dStartTime=pstruEventDetails->dEventTime;
788 gsmPacket=packet->pstruMacData->Packet_MACProtocol;
789 gsmPacket->nTimeSlot=channel->nTimeSlot;
790 //Write physical out event
791 if(pstruEventDetails->nProtocolId==MAC_PROTOCOL_GSM)
792 pstruEventDetails->dEventTime+=577*8;
793 pstruEventDetails->dPacketSize=fnGetPacketSize(packet);
794 pstruEventDetails->nApplicationId=packet->pstruAppData->nApplicationId;
795 pstruEventDetails->nEventType=TIMER_EVENT;
796 pstruEventDetails->nDeviceId=nMSID;
797 pstruEventDetails->nInterfaceId=nMSInterface;
798 pstruEventDetails->nDeviceType=MOBILESTATION;
799 pstruEventDetails->nPacketId=packet->nPacketId;
800 pstruEventDetails->nSegmentId=packet->pstruAppData->nSegmentId;
801 pstruEventDetails->pPacket=packet;
802 pstruEventDetails->nSubEventType=CELLULAR_SUBEVENT(pstruEventDetails->nProtocolId,Subevent_TxNextBurst);
803 pstruEventDetails->szOtherDetails=MSMac->pstruPacketList;
804 fnpAddEvent(pstruEventDetails);
805 }
806 else if(packet->pstruAppData && packet->pstruAppData->nAppEndFlag==1 && gsmPacket->isLast==1)
807 {
808 //Add call end event
809 fn_NetSim_Cellular_SendCallend(nMSID,
810 nMSInterface,
811 get_first_dest_from_packet(packet),
812 pstruEventDetails->dEventTime);
813 }
814 return 1;
815}
816
CELLULAR_CHANNEL_TYPE nChannelType
Definition Cellular.h:137