NetSim Source Code Help
Loading...
Searching...
No Matches
P2P_Phy.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 "P2P.h"
26#include "P2P_Enum.h"
27
28#define LIGHT_SPEED 299792458.0
29
31{
33 return (d / LIGHT_SPEED)*SECOND;
34}
35
36#define calculate_tx_time(size,rate) ((size * 8.0) / rate)
37
38static void add_phy_in(NetSim_PACKET* packet,
39 NETSIM_ID d,
40 NETSIM_ID in,
42 NETSIM_ID ci)
43{
44 packet->nTransmitterId = d;
45 packet->nReceiverId = c;
47 pevent.dEventTime = packet->pstruPhyData->dEndTime;
48 pevent.dPacketSize = packet->pstruPhyData->dPacketSize;
49 if (packet->pstruAppData)
50 {
52 pevent.nSegmentId = packet->pstruAppData->nSegmentId;
53 }
54 else
55 {
56 pevent.nApplicationId = 0;
57 pevent.nSegmentId = 0;
58 }
59 pevent.nDeviceId = c;
60 pevent.nDeviceType = DEVICE_TYPE(c);
62 pevent.nInterfaceId = ci;
63 pevent.nPacketId = packet->nPacketId;
64 pevent.nProtocolId = DEVICE_MACLAYER(c, ci)->nMacProtocolId;
65 pevent.nSubEventType = 0;
66 pevent.pPacket = packet;
67 pevent.szOtherDetails = NULL;
68 fnpAddEvent(&pevent);
69}
70
71static double transmit_over_wired(NetSim_PACKET* packet, NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
72{
73 NETSIM_ID c, ci;
74 NetSim_PHYSICALLAYER* phy = DEVICE_PHYLAYER(nDeviceId, nInterfaceId);
75 NetSim_LINKS* link = phy->pstruNetSimLinks;
76 fn_NetSim_Stack_GetConnectedDevice(nDeviceId, nInterfaceId, &c, &ci);
77 double dDataRate = link->puniMedProp.pstruWiredLink.dDataRateUp;
78 double dPropagationDelay = link->puniMedProp.pstruWiredLink.dPropagationDelayUp;
79
80 double txTime = calculate_tx_time(packet->pstruPhyData->dPacketSize,
81 dDataRate);
82 if (packet->pstruMacData->dEndTime <= phy->dLastPacketEndTime)
84 else
86 packet->pstruPhyData->dStartTime = packet->pstruPhyData->dArrivalTime + txTime;
87 packet->pstruPhyData->dEndTime = packet->pstruPhyData->dStartTime + dPropagationDelay;
88 add_phy_in(packet, nDeviceId, nInterfaceId, c, ci);
90 return phy->dLastPacketEndTime;
91}
92
93static double wireless_transmit(NetSim_PACKET* packet,
94 NETSIM_ID d,
95 NETSIM_ID in,
97 NETSIM_ID ci)
98{
100 ptrP2P_NODE_PHY sphy = P2P_PHY(d, in);
101 ptrP2P_NODE_PHY dphy = P2P_PHY(c, ci);
102
103 double txtime = calculate_tx_time(packet->pstruPhyData->dPacketSize, sphy->dDataRate);
104 if (packet->pstruMacData->dEndTime <= phy->dLastPacketEndTime)
106 else
108
109 double time = packet->pstruPhyData->dArrivalTime;
110
111 phy->dLastPacketEndTime = time + txtime;
112
113 double pdbm = propagation_get_received_power_dbm(propagationHandle, d, in, c, ci, time);
114 if (pdbm < dphy->dReceiverSensitivity)
115 {
117 return time + txtime;
118 }
119
120 double dPropagationDelay = get_propagation_delay(d, c);
121 packet->pstruPhyData->dStartTime = time + txtime;
122 packet->pstruPhyData->dEndTime = packet->pstruPhyData->dStartTime + dPropagationDelay;
123 add_phy_in(packet, d, in, c, ci);
125 return packet->pstruPhyData->dStartTime;
126}
127
129{
130 NETSIM_ID c, ci;
131 c = packet->nReceiverId;
133 return wireless_transmit(packet, d, in, c, ci);
134}
135
137{
138 double time = packet->pstruPhyData->dArrivalTime;
139 double t;
140 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
141 NETSIM_ID i;
142 NETSIM_ID c, ci;
144 {
145 for (i = 0; i < link->puniDevList.pstrup2MP.nConnectedDeviceCount; i++)
146 {
147 c = link->puniDevList.pstrup2MP.anDevIds[i];
150 t = wireless_transmit(p, d, in, c, ci);
151 if (t > time)
152 time = t;
153 }
155 }
156 else
157 {
160 t = wireless_transmit(packet, d, in, c, ci);
161 if (t > time)
162 time = t;
163 }
164 return time;
165}
166
168{
169 NETSIM_ID c, ci;
170 c = packet->nReceiverId;
172 return wireless_transmit(packet, d, in, c, ci);
173}
174
176{
177 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
178 NETSIM_ID i;
179 NETSIM_ID c, ci;
180 double time = packet->pstruPhyData->dArrivalTime;
181 double torg = DEVICE_PHYLAYER(d, in)->dLastPacketEndTime;
182 for (i = 0; i < link->puniDevList.pstruMP2MP.nConnectedDeviceCount; i++)
183 {
184 if (d == link->puniDevList.pstruMP2MP.anDevIds[i])
185 continue;
186 c = link->puniDevList.pstruMP2MP.anDevIds[i];
189 DEVICE_PHYLAYER(d, in)->dLastPacketEndTime = torg;
190 double t = wireless_transmit(p, d, in, c, ci);
191 if (t > time)
192 time = t;
193 }
194 DEVICE_PHYLAYER(d, in)->dLastPacketEndTime = time;
196 return time;
197}
198
200{
201 NETSIM_ID c, ci;
202 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
203 if (link->nLinkType == LinkType_P2P)
204 {
206 return wireless_transmit(packet, d, in, c, ci);
207 }
208 else if(link->nLinkType == LinkType_P2MP)
209 {
210 if (packet->nReceiverId)
211 return wireless_P2MP_unicast(packet, d, in);
212 else
213 return wireless_P2MP_broadcast(packet, d, in);
214 }
215 else if (link->nLinkType == LinkType_MP2MP)
216 {
217 if (packet->nReceiverId)
218 return wireless_MP2MP_unicast(packet, d, in);
219 else
220 return wireless_MP2MP_broadcast(packet, d, in);
221 }
222 return packet->pstruPhyData->dArrivalTime;
223}
224
225static double fnTransmitPacket(NetSim_PACKET* pPacket, NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
226{
227 NetSim_LINKS* link = DEVICE_PHYLAYER(nDeviceId, nInterfaceId)->pstruNetSimLinks;
228 if (link->isLinkFailureMode)
229 {
231 {
234 }
235 }
236
237 if (link->nLinkMedium == PHY_MEDIUM_WIRED)
238 return transmit_over_wired(pPacket, nDeviceId, nInterfaceId);
239 else
240 return transmit_over_wireless(pPacket, nDeviceId, nInterfaceId);
241}
242
244{
249 pstruPacket->pstruPhyData->dArrivalTime = ldEventTime;
250 pstruPacket->pstruPhyData->dOverhead = 0;
251 pstruPacket->pstruPhyData->dPayload = pstruPacket->pstruMacData->dPacketSize;
252 pstruPacket->pstruPhyData->dPacketSize = pstruPacket->pstruPhyData->dOverhead + pstruPacket->pstruPhyData->dPayload;
253 //Transmit the packet.
254 double time = fnTransmitPacket(pstruEventDetails->pPacket, d, in);
255
257 //Prepare event details for MAC out
266 if (p)
267 {
269 if (p->pstruAppData)
270 {
273 }
274 else
275 {
278 }
279 }
280 pstruEventDetails->nSubEventType = P2P_MAC_IDLE;
281 //Add MAC out Event
283 return 0;
284}
285
287{
288 NETSIM_ID c = packet->nTransmitterId;
290 ptrP2P_NODE_PHY phy = P2P_PHY(c, ci);
292 double pb = calculate_BER(phy->modulation,
293 rxPower, NEGATIVE_DBM,
294 phy->dBandwidth);
295
297}
298
300{
301 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
302 double pb = link->puniMedProp.pstruWiredLink.dErrorRateUp;
304}
305
307{
311 if (isP2PWireless(d,in))
312 packet->nPacketStatus = P2P_Wireless_CalculateError(d, in, packet);
313 else
314 packet->nPacketStatus = P2P_Wired_CalculateError(d, in, packet);
315}
316
318{
320 fnValidatePacket(packet);
321
323
325 //Place the packet to mac layer
326 //Write mac in event
330 fn_NetSim_Metrics_Add(packet);
331
332 if (packet->nPacketStatus == PacketStatus_NoError)
333 {
334 //Prepare the mac in event details
337 //Add mac in event
339 }
340 else
341 {
344 }
345 return 0;
346}
unsigned int NETSIM_ID
Definition: Animation.h:45
double calculate_BER(PHY_MODULATION modulation, double dReceivedPower_dBm, double dInterferencePower_dBm, double dBandwidth_MHz)
#define c
#define isP2PWireless(d, i)
Definition: P2P.h:79
PROPAGATION_HANDLE propagationHandle
Definition: P2P.h:81
#define P2P_PHY(devid, ifid)
Definition: P2P.h:76
static double wireless_MP2MP_broadcast(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in)
Definition: P2P_Phy.c:175
double get_propagation_delay(NETSIM_ID i, NETSIM_ID j)
Definition: P2P_Phy.c:30
int P2P_PhyOut_Handler()
Definition: P2P_Phy.c:243
static void P2P_Calculate_Error()
Definition: P2P_Phy.c:306
#define calculate_tx_time(size, rate)
Definition: P2P_Phy.c:36
static double wireless_transmit(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in, NETSIM_ID c, NETSIM_ID ci)
Definition: P2P_Phy.c:93
#define LIGHT_SPEED
Definition: P2P_Phy.c:28
static void add_phy_in(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in, NETSIM_ID c, NETSIM_ID ci)
Definition: P2P_Phy.c:38
static double transmit_over_wireless(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in)
Definition: P2P_Phy.c:199
int P2P_PhyIn_Handler()
Definition: P2P_Phy.c:317
PACKET_STATUS P2P_Wired_CalculateError(NETSIM_ID d, NETSIM_ID in, NetSim_PACKET *packet)
Definition: P2P_Phy.c:299
static double wireless_P2MP_unicast(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in)
Definition: P2P_Phy.c:128
static double wireless_MP2MP_unicast(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in)
Definition: P2P_Phy.c:167
static double fnTransmitPacket(NetSim_PACKET *pPacket, NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
Definition: P2P_Phy.c:225
PACKET_STATUS P2P_Wireless_CalculateError(NETSIM_ID d, NETSIM_ID in, NetSim_PACKET *packet)
Definition: P2P_Phy.c:286
static double wireless_P2MP_broadcast(NetSim_PACKET *packet, NETSIM_ID d, NETSIM_ID in)
Definition: P2P_Phy.c:136
static double transmit_over_wired(NetSim_PACKET *packet, NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
Definition: P2P_Phy.c:71
int fnValidatePacket(const NetSim_PACKET *pstruPacket)
NetSim_PACKET * fn_NetSim_Packet_GetPacketFromBuffer(NetSim_BUFFER *pstruBuffer, int nFlag)
PACKET_STATUS fn_NetSim_Packet_DecideError(double dBER, long double dPacketSize)
@ PacketStatus_NoError
Definition: Packet.h:101
#define propagation_get_received_power_dbm(h, t, ti, r, ri, time)
#define NEGATIVE_DBM
EXPORTED double ldEventTime
Definition: Stack.h:838
NETSIM_ID fn_NetSim_Stack_GetConnectedDevice(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NETSIM_ID *nConnectedDevId, NETSIM_ID *nConnectedInterfaceId)
#define DEVICE_MACLAYER(DeviceId, InterfaceId)
Definition: Stack.h:786
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
@ PHY_MEDIUM_WIRED
Definition: Stack.h:243
MAC_LAYER_PROTOCOL fn_NetSim_Stack_GetMacProtocol(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
#define SECOND
Definition: Stack.h:60
@ MAC_OUT_EVENT
Definition: Stack.h:106
@ MAC_IN_EVENT
Definition: Stack.h:107
@ PHYSICAL_IN_EVENT
Definition: Stack.h:105
#define DEVICE_ACCESSBUFFER(DeviceId, InterfaceId)
Definition: Stack.h:794
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
NETSIM_ID fn_NetSim_Stack_GetConnectedInterface(NETSIM_ID devId, NETSIM_ID ifid, NETSIM_ID conId)
#define DEVICE_PHYLAYER(DeviceId, InterfaceId)
Definition: Stack.h:788
@ LinkType_P2P
Definition: Stack.h:315
@ LinkType_P2MP
Definition: Stack.h:316
@ LinkType_MP2MP
Definition: Stack.h:317
#define DEVICE_POSITION(DeviceId)
Definition: Stack.h:790
@ LINKSTATE_DOWN
Definition: Stack.h:324
#define fn_NetSim_Metrics_Add(packet)
Definition: Stack.h:998
double fn_NetSim_Utilities_CalculateDistance(NetSim_COORDINATES *coordinate1, NetSim_COORDINATES *coordinates2)
#define fn_NetSim_Packet_CopyPacket(pstruPacket)
Definition: main.h:182
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
enum enum_PacketStatus PACKET_STATUS
Definition: main.h:128
#define fn_NetSim_WritePacketTrace(pstruPacket)
Definition: main.h:188
#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 nSubEventType
Definition: Stack.h:757
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
long long int nPacketId
Definition: Packet.h:256
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
PACKET_STATUS nPacketStatus
Definition: Packet.h:272
struct stru_NetSim_Packet_PhyLayer * pstruPhyData
Definition: Packet.h:277
NETSIM_ID nReceiverId
Definition: Packet.h:266
NETSIM_ID nTransmitterId
Definition: Packet.h:265
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276
struct stru_NetSim_Links * pstruNetSimLinks
Definition: Stack.h:549
double dBandwidth
Definition: P2P.h:65
PHY_MODULATION modulation
Definition: P2P.h:69
double dDataRate
Definition: P2P.h:70