NetSim Source Code Help
Loading...
Searching...
No Matches
P2P.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//Function prototype
29static void add_event_link_up();
35
36/**
37This function is called by NetworkStack.dll, whenever the event gets triggered
38inside the NetworkStack.dll for the Mac/Phy layer P2P protocol
39It includes MAC_OUT, MAC_IN, PHY_OUT, PHY_IN and TIMER_EVENT.
40*/
42{
44 {
45 case MAC_OUT_EVENT:
47 break;
48 case MAC_IN_EVENT:
50 break;
53 break;
56 break;
57 case TIMER_EVENT:
58 {
60 {
61 case P2P_LINK_UP:
63 break;
64 case P2P_LINK_DOWN:
66 break;
67 }
68 }
69 break;
70 }
71 return 0;
72}
73
75{
76 if (!_stricmp(val, "QPSK"))
77 return Modulation_QPSK;
78 else if (!_stricmp(val, "BPSK"))
79 return Modulation_BPSK;
80 else if (!_stricmp(val, "16QAM"))
81 return Modulation_16_QAM;
82 else if (!_stricmp(val, "64QAM"))
83 return Modulation_64_QAM;
84 else if (!_stricmp(val, "GMSK"))
85 return Modulation_GMSK;
86 else
87 {
88 fnNetSimError("Unknown modulation %s for P2P protocol. Assigning QPSK\n", val);
89 return Modulation_QPSK;
90 }
91}
92
93static void configure_wireless_P2P(NETSIM_ID d, NETSIM_ID in, void* xmlNetSimNode)
94{
95 char* szVal;
96 ptrP2P_NODE_PHY phy = P2P_PHY(d, in);
97 if (!phy)
98 {
99 phy = (ptrP2P_NODE_PHY)calloc(1, sizeof* phy);
100 DEVICE_PHYVAR(d, in) = phy;
101 }
102 phy->iswireless = true;
103 //Get the CENTRAL_FREQUENCY
104 phy->dCenteralFrequency = fn_NetSim_Config_read_Frequency(xmlNetSimNode, "CENTRAL_FREQUENCY", P2P_CENTRAL_FREQUENCY_DEFAULT, "MHz");
105
106 phy->dBandwidth = fn_NetSim_Config_read_Frequency(xmlNetSimNode, "BANDWIDTH", P2P_BANDWIDTH_DEFAULT, "MHz");
107
108 phy->dTXPower = fn_NetSim_Config_read_txPower(xmlNetSimNode, "TX_POWER", P2P_TX_POWER_DEFAULT, "mW");
109
110 phy->dDataRate = fn_NetSim_Config_read_dataRate(xmlNetSimNode, "DATA_RATE", P2P_DATA_RATE_DEFAULT, "mbps");
111
112 phy->dReceiverSensitivity = fn_NetSim_Config_read_txPower(xmlNetSimNode, "RECEIVER_SENSITIVITY", P2P_RECEIVER_SENSITIVITY_DBM_DEFAULT, "dBM");
113
114 //Get the modulation
115 getXmlVar(&szVal, MODULATION_TECHNIQUE, xmlNetSimNode, 1, _STRING, P2P);
117
118 getXmlVar(&phy->dAntennaHeight, ANTENNA_HEIGHT, xmlNetSimNode, 1, _DOUBLE, P2P);
119 getXmlVar(&phy->dAntennaGain, ANTENNA_GAIN, xmlNetSimNode, 1, _DOUBLE, P2P);
120 getXmlVar(&phy->d0, D0, xmlNetSimNode, 1, _DOUBLE, P2P);
121 getXmlVar(&phy->pld0, PL_D0, xmlNetSimNode, 0, _DOUBLE, P2P);
122}
123
124/**
125This function is called by NetworkStack.dll, while configuring the device
126Mac/Phy layer for P2P protocol.
127*/
128_declspec(dllexport) int fn_NetSim_P2P_Configure(void** var)
129{
130 char* tag;
131 void* xmlNetSimNode;
132 NETSIM_ID nDeviceId;
133 NETSIM_ID nInterfaceId;
134 LAYER_TYPE nLayerType;
135 char* szVal;
136
137 tag = (char*)var[0];
138 xmlNetSimNode = var[2];
139 if (!strcmp(tag, "PROTOCOL_PROPERTY"))
140 {
141 NETWORK = (struct stru_NetSim_Network*)var[1];
142 nDeviceId = *((NETSIM_ID*)var[3]);
143 nInterfaceId = *((NETSIM_ID*)var[4]);
144 nLayerType = *((LAYER_TYPE*)var[5]);
145 switch (nLayerType)
146 {
147 case MAC_LAYER:
148 {
149 //Get the MAC address
150 szVal = fn_NetSim_xmlConfig_GetVal(xmlNetSimNode, "MAC_ADDRESS", 1);
151 if (szVal)
152 {
153 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->szMacAddress = STR_TO_MAC(szVal);
154 free(szVal);
155 }
156
157 ptrP2P_NODE_MAC mac = P2P_MAC(nDeviceId, nInterfaceId);
158 if (!mac)
159 {
160 mac = (ptrP2P_NODE_MAC)calloc(1, sizeof * mac);
161 DEVICE_MACVAR(nDeviceId, nInterfaceId) = mac;
162 }
163 }
164 break;
165 case PHYSICAL_LAYER:
166 {
167 getXmlVar(&szVal, CONNECTION_MEDIUM, xmlNetSimNode, 1, _STRING, P2P);
168 if (!_stricmp(szVal, "wireless"))
169 configure_wireless_P2P(nDeviceId, nInterfaceId, xmlNetSimNode);
170 free(szVal);
171 }
172 break;
173 default:
174 fnNetSimError("%d layer is not implemented for P2P protocol\n", nLayerType);
175 break;
176 }
177 }
178 return 0;
179}
180
181/**
182This function initializes the P2P parameters.
183*/
184_declspec (dllexport) int fn_NetSim_P2P_Init(struct stru_NetSim_Network *NETWORK_Formal,
186 char *pszAppPath_Formal,
188 int nVersion_Type,
189 void **fnPointer)
190{
193 return 0;
194}
195
196/**
197This function is called by NetworkStack.dll, once simulation end to free the
198allocated memory for the network.
199*/
201{
202 for (int i = 0; i < NETWORK->nDeviceCount; i++)
203 {
204 if (DEVICE(i + 1) == NULL) continue;
205 for (int j = 0; j < DEVICE(i + 1)->nNumOfInterface; j++)
206 {
207 if (DEVICE_INTERFACE(i + 1, j + 1) == NULL) continue;
208 if (DEVICE_MACPROTOCOL(i + 1, j + 1) != MAC_PROTOCOL_P2P) continue;
209
210 ptrP2P_NODE_MAC mac = P2P_MAC(i + 1, j + 1);
211 free(mac);
212
213 ptrP2P_NODE_PHY phy = P2P_PHY(i + 1, j + 1);
214 free(phy);
215 }
216 }
217 return 0;
218}
219
220/**
221This function is called by NetworkStack.dll, while writing the event trace
222to get the sub event as a string.
223*/
224_declspec (dllexport) char *fn_NetSim_P2P_Trace(int nSubEvent)
225{
226 return GetStringP2P_Subevent(nSubEvent);
227}
228
229/**
230This function is called by NetworkStack.dll, to free the P2P protocol
231*/
233{
234 return 0;
235}
236
237/**
238This function is called by NetworkStack.dll, to copy the P2P protocol from source packet to destination.
239*/
240_declspec(dllexport) int fn_NetSim_P2P_CopyPacket(NetSim_PACKET* pstruDestPacket, NetSim_PACKET* pstruSrcPacket)
241{
242 return 0;
243}
244
245/**
246This function write the Metrics in Metrics.txt
247*/
248_declspec(dllexport) int fn_NetSim_P2P_Metrics(PMETRICSWRITER metricsWriter)
249{
250 return 0;
251}
252
253/**
254This function will return the string to write packet trace heading.
255*/
256_declspec(dllexport) char* fn_NetSim_P2P_ConfigPacketTrace(const void* xmlNetSimNode)
257{
258 return "";
259}
260
261/**
262This function will return the string to write packet trace.
263*/
264_declspec(dllexport) int fn_NetSim_P2P_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
265{
266 return 0;
267}
268
269static void p2p_gettxinfo(NETSIM_ID nTxId,
270 NETSIM_ID nTxInterface,
271 NETSIM_ID nRxId,
272 NETSIM_ID nRxInterface,
273 PTX_INFO Txinfo)
274{
275 ptrP2P_NODE_PHY txphy = (ptrP2P_NODE_PHY)DEVICE_PHYVAR(nTxId, nTxInterface);
276 ptrP2P_NODE_PHY rxphy = (ptrP2P_NODE_PHY)DEVICE_PHYVAR(nRxId, nRxInterface);
277
278 Txinfo->dCentralFrequency = txphy->dCenteralFrequency;
279 Txinfo->dRxAntennaHeight = rxphy->dAntennaHeight;
280 Txinfo->dRxGain = rxphy->dAntennaGain;
281 Txinfo->dTxAntennaHeight = txphy->dAntennaHeight;
282 Txinfo->dTxGain = txphy->dAntennaGain;
283 Txinfo->dTxPower_mw = (double)txphy->dTXPower;
284 Txinfo->dTxPower_dbm = MW_TO_DBM(Txinfo->dTxPower_mw);
285 Txinfo->dTx_Rx_Distance = DEVICE_DISTANCE(nTxId, nRxId);
286 Txinfo->d0 = txphy->d0;
287}
288
289static bool CheckFrequencyInterfrence(double dFrequency1, double dFrequency2, double bandwidth)
290{
291 if (dFrequency1 > dFrequency2)
292 {
293 if ((dFrequency1 - dFrequency2) >= bandwidth)
294 return false; // no interference
295 else
296 return true; // interference
297 }
298 else
299 {
300 if ((dFrequency2 - dFrequency1) >= bandwidth)
301 return false; // no interference
302 else
303 return true; // interference
304 }
305}
306
308{
312}
313
315{
316 //Distance will change between Tx and Rx due to mobility.
317 // So update the power from both side
318 PTX_INFO info = get_tx_info(propagationHandle, tx, txi, rx, rxi);
319 info->dTx_Rx_Distance = DEVICE_DISTANCE(tx, rx);
321 tx, txi, rx, rxi, pstruEventDetails->dEventTime);
322 return 1;
323}
324
326{
327 NETSIM_ID t, ti, r, ri;
328
331
332 for (t = 0; t < NETWORK->nDeviceCount; t++)
333 {
334 for (ti = 0; ti < DEVICE(t + 1)->nNumOfInterface; ti++)
335 {
336 if (!isP2PConfigured(t + 1, ti + 1))
337 continue;
338 if (!isP2PWireless(t + 1, ti + 1))
339 continue;
340 for (r = 0; r < NETWORK->nDeviceCount; r++)
341 {
342 for (ri = 0; ri < DEVICE(r + 1)->nNumOfInterface; ri++)
343 {
344 if (!isP2PConfigured(r + 1, ri + 1))
345 continue;
346 if (!isP2PWireless(r + 1, ri + 1))
347 continue;
348 p2p_CalculateReceivedPower(t + 1, ti + 1, r + 1, ri + 1);
349 }
350 }
351 }
352 }
353 return 0;
354}
355
357 LINK_STATE newState)
358{
359 NetSim_LINKS* link = NETWORK->ppstruNetSimLinks[linkId-1];
364
365 NETSIM_ID subevent;
366 if (newState == LINKSTATE_UP)
367 subevent = P2P_LINK_UP;
368 else
369 subevent = P2P_LINK_DOWN;
370 NetSim_EVENTDETAILS pevent;
371 memset(&pevent, 0, sizeof pevent);
372
373 if (isP2PConfigured(d1, i1))
374 {
376 pevent.nDeviceId = d1;
377 pevent.nDeviceType = DEVICE_TYPE(d1);
378 pevent.nEventType = TIMER_EVENT;
379 pevent.nInterfaceId = i1;
381 pevent.nSubEventType = subevent;
382 fnpAddEvent(&pevent);
383 }
384
385 if (isP2PConfigured(d2, i2))
386 {
388 pevent.nDeviceId = d2;
389 pevent.nDeviceType = DEVICE_TYPE(d2);
390 pevent.nEventType = TIMER_EVENT;
391 pevent.nInterfaceId = i2;
393 pevent.nSubEventType = subevent;
394 fnpAddEvent(&pevent);
395 }
396 return 0;
397}
398
399static void add_event_link_up()
400{
401 NETSIM_ID d, i;
402
403 NetSim_EVENTDETAILS pevent;
404 memset(&pevent, 0, sizeof pevent);
405
406 for (d = 0; d < NETWORK->nDeviceCount; d++)
407 {
408 for (i = 0; i < DEVICE(d + 1)->nNumOfInterface; i++)
409 {
410 if(!isP2PConfigured(d+1,i+1))
411 continue;
412
413 //Register link failure model callback
414 fn_NetSim_Link_RegisterLinkFailureCallback(DEVICE_PHYLAYER(d + 1, i + 1)->pstruNetSimLinks->nLinkId,
416 pevent.nDeviceId = d + 1;
417 pevent.nDeviceType = DEVICE_TYPE(d + 1);
418 pevent.nEventType = TIMER_EVENT;
419 pevent.nInterfaceId = i + 1;
421 pevent.nSubEventType = P2P_LINK_UP;
422 fnpAddEvent(&pevent);
423 }
424 }
425}
enum enum_802_22_Modulation MODULATION_TECHNIQUE
Definition: 802_22.h:113
unsigned int NETSIM_ID
Definition: Animation.h:45
LINK_STATE
Definition: CSMACD.h:33
#define _stricmp
Definition: Linux.h:127
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
void * PMETRICSWRITER
Definition: MetricsWriter.h:27
int P2P_PhyOut_Handler()
Definition: P2P_Phy.c:243
static int fn_NetSim_P2P_CalulateReceivedPower()
Definition: P2P.c:325
int P2P_MacIn_Handler()
Definition: P2P_Mac.c:92
int fn_NetSim_P2P_LinkStateChanged(NETSIM_ID linkId, LINK_STATE newState)
Definition: P2P.c:356
static bool check_interference(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
Definition: P2P.c:307
int fn_NetSim_P2P_Finish()
Definition: P2P.c:200
int fn_NetSim_P2P_FreePacket(NetSim_PACKET *pstruPacket)
Definition: P2P.c:232
int fn_NetSim_P2P_Init(struct stru_NetSim_Network *NETWORK_Formal, NetSim_EVENTDETAILS *pstruEventDetails_Formal, char *pszAppPath_Formal, char *pszWritePath_Formal, int nVersion_Type, void **fnPointer)
Definition: P2P.c:184
char * fn_NetSim_P2P_Trace(int nSubEvent)
Definition: P2P.c:224
int P2P_PhyIn_Handler()
Definition: P2P_Phy.c:317
static void configure_wireless_P2P(NETSIM_ID d, NETSIM_ID in, void *xmlNetSimNode)
Definition: P2P.c:93
int fn_NetSim_P2P_CopyPacket(NetSim_PACKET *pstruDestPacket, NetSim_PACKET *pstruSrcPacket)
Definition: P2P.c:240
static void add_event_link_up()
Definition: P2P.c:399
int fn_NetSim_P2P_Configure(void **var)
Definition: P2P.c:128
int fn_NetSim_P2P_Run()
Definition: P2P.c:41
char * fn_NetSim_P2P_ConfigPacketTrace(const void *xmlNetSimNode)
Definition: P2P.c:256
static void p2p_gettxinfo(NETSIM_ID nTxId, NETSIM_ID nTxInterface, NETSIM_ID nRxId, NETSIM_ID nRxInterface, PTX_INFO Txinfo)
Definition: P2P.c:269
int fn_NetSim_P2P_Metrics(PMETRICSWRITER metricsWriter)
Definition: P2P.c:248
static bool CheckFrequencyInterfrence(double dFrequency1, double dFrequency2, double bandwidth)
Definition: P2P.c:289
PHY_MODULATION getModulationFromString(char *val)
Definition: P2P.c:74
int P2P_MacOut_Handler()
Definition: P2P_Mac.c:28
int fn_NetSim_P2P_WritePacketTrace(NetSim_PACKET *pstruPacket, char **ppszTrace)
Definition: P2P.c:264
static int p2p_CalculateReceivedPower(NETSIM_ID tx, NETSIM_ID txi, NETSIM_ID rx, NETSIM_ID rxi)
Definition: P2P.c:314
struct stru_p2p_node_mac * ptrP2P_NODE_MAC
#define P2P_TX_POWER_DEFAULT
Definition: P2P.h:53
#define isP2PWireless(d, i)
Definition: P2P.h:79
#define P2P_BANDWIDTH_DEFAULT
Definition: P2P.h:51
#define P2P_DATA_RATE_DEFAULT
Definition: P2P.h:54
PROPAGATION_HANDLE propagationHandle
Definition: P2P.h:81
struct stru_P2P_NODE_PHY * ptrP2P_NODE_PHY
#define P2P_RECEIVER_SENSITIVITY_DBM_DEFAULT
Definition: P2P.h:55
#define P2P_MAC(devid, ifid)
Definition: P2P.h:44
#define P2P_PHY(devid, ifid)
Definition: P2P.h:76
#define isP2PConfigured(d, i)
Definition: P2P.h:78
#define P2P_CENTRAL_FREQUENCY_DEFAULT
Definition: P2P.h:52
#define MW_TO_DBM(mw)
#define get_tx_info(h, t, ti, r, ri)
PROPAGATION_HANDLE propagation_init(MAC_LAYER_PROTOCOL protocol, __CALLBACK__ check_protocol_configure check, __CALLBACK__ fnGetTxInfo fnpGetTxInfo, __CALLBACK__ fnCheckInterface checkInterference)
#define propagation_calculate_received_power(h, t, ti, r, ri, time)
NetSim_EVENTDETAILS * pstruEventDetails_Formal
Definition: RIP.h:178
NetSim_EVENTDETAILS char * pszAppPath_Formal
Definition: RIP.h:178
NetSim_EVENTDETAILS char char int nVersion_Type
Definition: RIP.h:178
NetSim_EVENTDETAILS char char * pszWritePath_Formal
Definition: RIP.h:178
NetSim_EVENTDETAILS char char int void ** fnPointer
Definition: RIP.h:178
void notify_interface_down(NETSIM_ID d, NETSIM_ID in)
#define DEVICE(DeviceId)
Definition: Stack.h:769
PNETSIM_MACADDRESS STR_TO_MAC(char *mac)
double fn_NetSim_Config_read_Frequency(void *xmlNetSimNode, const char *name, double defaultval, const char *returnUnit)
#define DEVICE_PHYVAR(DeviceId, InterfaceId)
Definition: Stack.h:797
#define DEVICE_MACLAYER(DeviceId, InterfaceId)
Definition: Stack.h:786
char * fn_NetSim_xmlConfig_GetVal(void *xmlNetSimNode, const char *szName, int flag)
#define getXmlVar(var, name, xmlNode, flag, type, protocol)
Definition: Stack.h:1046
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
@ _STRING
Definition: Stack.h:1033
@ _DOUBLE
Definition: Stack.h:1031
double fn_NetSim_Config_read_txPower(void *xmlNetSimNode, const char *name, double defaultTxPower, const char *returnUnit)
#define DEVICE_MACPROTOCOL(DeviceId, InterfaceId)
Definition: Stack.h:787
@ MAC_PROTOCOL_P2P
Definition: Stack.h:214
#define DEVICE_DISTANCE(d1, d2)
Definition: Stack.h:814
int fn_NetSim_Link_RegisterLinkFailureCallback(NETSIM_ID linkId, int(*fnLinkCallBack)(NETSIM_ID, LINK_STATE))
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
@ TIMER_EVENT
Definition: Stack.h:114
@ PHYSICAL_OUT_EVENT
Definition: Stack.h:104
@ MAC_OUT_EVENT
Definition: Stack.h:106
@ MAC_IN_EVENT
Definition: Stack.h:107
@ PHYSICAL_IN_EVENT
Definition: Stack.h:105
double fn_NetSim_Config_read_dataRate(void *xmlNetSimNode, const char *name, double defaultDataRate, const char *returnUnit)
@ PHYSICAL_LAYER
Definition: Stack.h:94
@ MAC_LAYER
Definition: Stack.h:95
void notify_interface_up(NETSIM_ID d, NETSIM_ID in)
#define DEVICE_MACVAR(DeviceId, InterfaceId)
Definition: Stack.h:798
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
#define DEVICE_PHYLAYER(DeviceId, InterfaceId)
Definition: Stack.h:788
@ LINKSTATE_UP
Definition: Stack.h:323
enum enum_Modulation PHY_MODULATION
@ Modulation_GMSK
Definition: Wireless.h:43
@ Modulation_BPSK
Definition: Wireless.h:35
@ Modulation_64_QAM
Definition: Wireless.h:40
@ Modulation_QPSK
Definition: Wireless.h:36
@ Modulation_16_QAM
Definition: Wireless.h:38
enum enum_LayerType LAYER_TYPE
Definition: main.h:136
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
NETSIM_ID nInterfaceId
Definition: Stack.h:751
struct stru_NetSim_Links ** ppstruNetSimLinks
Definition: Stack.h:738
double dBandwidth
Definition: P2P.h:65
double pld0
Definition: P2P.h:74
bool iswireless
Definition: P2P.h:64
PHY_MODULATION modulation
Definition: P2P.h:69
double d0
Definition: P2P.h:73
double dTXPower
Definition: P2P.h:67
double dReceiverSensitivity
Definition: P2P.h:68
double dAntennaGain
Definition: P2P.h:72
double dCenteralFrequency
Definition: P2P.h:66
double dDataRate
Definition: P2P.h:70
double dAntennaHeight
Definition: P2P.h:71
double dTxAntennaHeight
double dTx_Rx_Distance
double dCentralFrequency
double dRxAntennaHeight