NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
P2P_Mac.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 "P2P.h"
26#include "P2P_Enum.h"
27
28int P2P_MacOut_Handler()
29{
30 NETSIM_ID d = pstruEventDetails->nDeviceId;
31 NETSIM_ID in = pstruEventDetails->nInterfaceId;
32
33 NetSim_BUFFER* buf = DEVICE_ACCESSBUFFER(d, in);
34 NetSim_PACKET* pstruPacket;
35
36 if (pstruEventDetails->nSubEventType == P2P_MAC_IDLE)
37 {
38 P2P_MAC_SET_IDLE(d, in);
39 pstruEventDetails->nSubEventType = 0;
40 }
41
42 if (P2P_MAC_IS_BUSY(d, in)) return -1; // Mac is busy.
43
44 //Get the packet from the Interface buffer
45 pstruPacket = fn_NetSim_Packet_GetPacketFromBuffer(buf, 1);
46 if (!pstruPacket) return -2;
47
48 fnValidatePacket(pstruPacket);
49
50 P2P_MAC_SET_BUSY(d, in);
51
52 //Place the packet to physical layer
53 //Write physical out event.
54 //Append mac details in packet
55 pstruPacket->pstruMacData->dArrivalTime = ldEventTime;
56 pstruPacket->pstruMacData->dEndTime = ldEventTime;
57 if (pstruPacket->pstruNetworkData)
58 {
59 pstruPacket->pstruMacData->dOverhead = 0;
60 pstruPacket->pstruMacData->dPayload = pstruPacket->pstruNetworkData->dPacketSize;
61 pstruPacket->pstruMacData->dPacketSize = pstruPacket->pstruMacData->dOverhead +
62 pstruPacket->pstruMacData->dPayload;
63 }
64 if (pstruPacket->pstruMacData->Packet_MACProtocol &&
65 !pstruPacket->pstruMacData->dontFree)
66 {
67 fn_NetSim_Packet_FreeMacProtocolData(pstruPacket);
68 pstruPacket->pstruMacData->Packet_MACProtocol = NULL;
69 }
70 pstruPacket->pstruMacData->nMACProtocol = pstruEventDetails->nProtocolId;
71 pstruPacket->pstruMacData->dStartTime = ldEventTime;
72 //Update the event details
73 pstruEventDetails->dEventTime = ldEventTime;
74 pstruEventDetails->nEventType = PHYSICAL_OUT_EVENT;
75 pstruEventDetails->pPacket = pstruPacket;
76 pstruEventDetails->nPacketId = pstruPacket->nPacketId;
77 if (pstruPacket->pstruAppData)
78 {
79 pstruEventDetails->nApplicationId = pstruPacket->pstruAppData->nApplicationId;
80 pstruEventDetails->nSegmentId = pstruPacket->pstruAppData->nSegmentId;
81 }
82 else
83 {
84 pstruEventDetails->nApplicationId = 0;
85 pstruEventDetails->nSegmentId = 0;
86 }
87 //Add physical out event
88 fnpAddEvent(pstruEventDetails);
89 return 0;
90}
91
92int P2P_MacIn_Handler()
93{
94 NETSIM_ID d = pstruEventDetails->nDeviceId;
95 NETSIM_ID in = pstruEventDetails->nInterfaceId;
96 NetSim_PACKET* packet = pstruEventDetails->pPacket;
97 if (DEVICE_NWLAYER(d))
98 {
99 //Add packet to Network in
100 //Prepare event details for network in
101 pstruEventDetails->dEventTime = ldEventTime;
102 pstruEventDetails->nEventType = NETWORK_IN_EVENT;
103 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetNWProtocol(d);
104 //Add network in event
105 fnpAddEvent(pstruEventDetails);
106 }
107 else
108 {
109 bool isTransmitted = false;
110 //Broadcast to all other interface
111 NETSIM_ID i;
112 for (i = 0; i < DEVICE(d)->nNumOfInterface; i++)
113 {
114 if (i + 1 == in)
115 continue;
116
117 NetSim_BUFFER* buf = DEVICE_ACCESSBUFFER(d, i + 1);
118 //Check for buffer
119 if (!fn_NetSim_GetBufferStatus(buf))
120 {
121 //Prepare event details for mac out
122 pstruEventDetails->dEventTime = ldEventTime;
123 pstruEventDetails->nEventType = MAC_OUT_EVENT;
124 pstruEventDetails->nInterfaceId = i + 1;
125 pstruEventDetails->pPacket = NULL;
126 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetMacProtocol(d, i + 1);
127 //Add mac out event
128 fnpAddEvent(pstruEventDetails);
129 }
130 //Add packet to mac buffer
131 fn_NetSim_Packet_AddPacketToList(buf,
132 isTransmitted == false ? packet : fn_NetSim_Packet_CopyPacket(packet), 0);
133 isTransmitted = true;
134 }
135 }
136 return 0;
137}