27#include "AdvancedPlots.h"
28#pragma comment(lib,"AdvancedPlots.lib")
31static void add_event_link_up();
32static int fn_NetSim_P2P_CalulateReceivedPower();
33int P2P_MacOut_Handler();
34int P2P_MacIn_Handler();
35int P2P_PhyOut_Handler();
36int P2P_PhyIn_Handler();
43_declspec (dllexport)
int fn_NetSim_P2P_Run()
45 switch (pstruEventDetails->nEventType)
53 case PHYSICAL_OUT_EVENT:
56 case PHYSICAL_IN_EVENT:
61 switch (pstruEventDetails->nSubEventType)
64 notify_interface_up(pstruEventDetails->nDeviceId, pstruEventDetails->nInterfaceId);
67 notify_interface_down(pstruEventDetails->nDeviceId, pstruEventDetails->nInterfaceId);
76PHY_MODULATION getModulationFromString(
char* val)
78 if (!_stricmp(val,
"QPSK"))
79 return Modulation_QPSK;
80 else if (!_stricmp(val,
"BPSK"))
81 return Modulation_BPSK;
82 else if (!_stricmp(val,
"16QAM"))
83 return Modulation_16_QAM;
84 else if (!_stricmp(val,
"64QAM"))
85 return Modulation_64_QAM;
86 else if (!_stricmp(val,
"GMSK"))
87 return Modulation_GMSK;
90 fnNetSimError(
"Unknown modulation %s for P2P protocol. Assigning QPSK\n", val);
91 return Modulation_QPSK;
95static void configure_wireless_P2P(NETSIM_ID d, NETSIM_ID in,
void* xmlNetSimNode)
98 ptrP2P_NODE_PHY phy = P2P_PHY(d, in);
101 phy = (ptrP2P_NODE_PHY)calloc(1,
sizeof* phy);
102 DEVICE_PHYVAR(d, in) = phy;
104 phy->iswireless =
true;
106 phy->dCenteralFrequency = fn_NetSim_Config_read_Frequency(xmlNetSimNode,
"CENTRAL_FREQUENCY", P2P_CENTRAL_FREQUENCY_DEFAULT,
"MHz");
108 phy->dBandwidth = fn_NetSim_Config_read_Frequency(xmlNetSimNode,
"BANDWIDTH", P2P_BANDWIDTH_DEFAULT,
"MHz");
110 phy->dTXPower = fn_NetSim_Config_read_txPower(xmlNetSimNode,
"TX_POWER", P2P_TX_POWER_DEFAULT,
"mW");
112 phy->dDataRate = fn_NetSim_Config_read_dataRate(xmlNetSimNode,
"DATA_RATE", P2P_DATA_RATE_DEFAULT,
"mbps");
114 phy->dReceiverSensitivity = fn_NetSim_Config_read_txPower(xmlNetSimNode,
"RECEIVER_SENSITIVITY", P2P_RECEIVER_SENSITIVITY_DBM_DEFAULT,
"dBM");
117 getXmlVar(&szVal, MODULATION_TECHNIQUE, xmlNetSimNode, 1, _STRING, P2P);
118 phy->modulation = getModulationFromString(szVal);
120 getXmlVar(&phy->dAntennaHeight, ANTENNA_HEIGHT, xmlNetSimNode, 1, _DOUBLE, P2P);
121 getXmlVar(&phy->dAntennaGain, ANTENNA_GAIN, xmlNetSimNode, 1, _DOUBLE, P2P);
122 getXmlVar(&phy->d0, D0, xmlNetSimNode, 0, _DOUBLE, P2P);
123 getXmlVar(&phy->pld0, PL_D0, xmlNetSimNode, 0, _DOUBLE, P2P);
130_declspec(dllexport)
int fn_NetSim_P2P_Configure(
void** var)
135 NETSIM_ID nInterfaceId;
136 LAYER_TYPE nLayerType;
140 xmlNetSimNode = var[2];
141 if (!strcmp(tag,
"PROTOCOL_PROPERTY"))
144 nDeviceId = *((NETSIM_ID*)var[3]);
145 nInterfaceId = *((NETSIM_ID*)var[4]);
146 nLayerType = *((LAYER_TYPE*)var[5]);
152 szVal = fn_NetSim_xmlConfig_GetVal(xmlNetSimNode,
"MAC_ADDRESS", 1);
155 DEVICE_MACLAYER(nDeviceId, nInterfaceId)->szMacAddress = STR_TO_MAC(szVal);
159 ptrP2P_NODE_MAC mac = P2P_MAC(nDeviceId, nInterfaceId);
162 mac = (ptrP2P_NODE_MAC)calloc(1,
sizeof * mac);
163 DEVICE_MACVAR(nDeviceId, nInterfaceId) = mac;
169 getXmlVar(&szVal, CONNECTION_MEDIUM, xmlNetSimNode, 1, _STRING, P2P);
170 if (!_stricmp(szVal,
"wireless"))
171 configure_wireless_P2P(nDeviceId, nInterfaceId, xmlNetSimNode);
176 fnNetSimError(
"%d layer is not implemented for P2P protocol\n", nLayerType);
186_declspec (dllexport)
int fn_NetSim_P2P_Init()
188 init_linkpacketlog();
190 fn_NetSim_P2P_CalulateReceivedPower();
198_declspec(dllexport)
int fn_NetSim_P2P_Finish()
200 LinkPacketLog_close();
201 for (UINT i = 0; i < NETWORK->nDeviceCount; i++)
203 if (DEVICE(i + 1) == NULL)
continue;
204 for (UINT j = 0; j < DEVICE(i + 1)->nNumOfInterface; j++)
206 if (isVirtualInterface(i + 1, j + 1))
return -1;
207 if (DEVICE_INTERFACE(i + 1, j + 1) == NULL)
continue;
208 if (DEVICE_MACPROTOCOL(i + 1, j + 1) != MAC_PROTOCOL_P2P)
continue;
210 ptrP2P_NODE_MAC mac = P2P_MAC(i + 1, j + 1);
213 ptrP2P_NODE_PHY phy = P2P_PHY(i + 1, j + 1);
224_declspec (dllexport)
char *fn_NetSim_P2P_Trace(
int nSubEvent)
226 return GetStringP2P_Subevent(nSubEvent);
232_declspec(dllexport)
int fn_NetSim_P2P_FreePacket(NetSim_PACKET* pstruPacket)
241_declspec(dllexport)
int fn_NetSim_P2P_CopyPacket(NetSim_PACKET* pstruDestPacket, NetSim_PACKET* pstruSrcPacket)
251_declspec(dllexport)
int fn_NetSim_P2P_Metrics(PMETRICSWRITER metricsWriter)
260_declspec(dllexport)
char* fn_NetSim_P2P_ConfigPacketTrace(
const void* xmlNetSimNode)
269_declspec(dllexport)
int fn_NetSim_P2P_WritePacketTrace(NetSim_PACKET* pstruPacket,
char** ppszTrace)
276static void p2p_gettxinfo(NETSIM_ID nTxId,
277 NETSIM_ID nTxInterface,
279 NETSIM_ID nRxInterface,
282 ptrP2P_NODE_PHY txphy = (ptrP2P_NODE_PHY)DEVICE_PHYVAR(nTxId, nTxInterface);
283 ptrP2P_NODE_PHY rxphy = (ptrP2P_NODE_PHY)DEVICE_PHYVAR(nRxId, nRxInterface);
285 Txinfo->dCentralFrequency = txphy->dCenteralFrequency;
286 Txinfo->dRxAntennaHeight = rxphy->dAntennaHeight;
287 Txinfo->dRxGain = rxphy->dAntennaGain;
288 Txinfo->dTxAntennaHeight = txphy->dAntennaHeight;
289 Txinfo->dTxGain = txphy->dAntennaGain;
290 Txinfo->dTxPower_mw = (double)txphy->dTXPower;
291 Txinfo->dTxPower_dbm = MW_TO_DBM(Txinfo->dTxPower_mw);
292 Txinfo->dTx_Rx_Distance = DEVICE_DISTANCE(nTxId, nRxId);
293 Txinfo->d0 = txphy->d0;
296static bool CheckFrequencyInterfrence(
double dFrequency1,
double dFrequency2,
double bandwidth)
298 if (dFrequency1 > dFrequency2)
300 if ((dFrequency1 - dFrequency2) >= bandwidth)
307 if ((dFrequency2 - dFrequency1) >= bandwidth)
314static bool check_interference(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
316 ptrP2P_NODE_PHY tp = (ptrP2P_NODE_PHY)DEVICE_PHYVAR(t, ti);
317 ptrP2P_NODE_PHY rp = (ptrP2P_NODE_PHY)DEVICE_PHYVAR(r, ri);
318 return CheckFrequencyInterfrence(tp->dCenteralFrequency, rp->dCenteralFrequency, tp->dBandwidth);
321static int p2p_CalculateReceivedPower(NETSIM_ID tx, NETSIM_ID txi, NETSIM_ID rx, NETSIM_ID rxi)
325 PTX_INFO info = get_tx_info(propagationHandle, tx, txi, rx, rxi);
326 info->dTx_Rx_Distance = DEVICE_DISTANCE(tx, rx);
327 propagation_calculate_received_power(propagationHandle,
328 tx, txi, rx, rxi, pstruEventDetails->dEventTime);
332static int fn_NetSim_P2P_CalulateReceivedPower()
334 NETSIM_ID t, ti, r, ri;
336 propagationHandle = propagation_init(MAC_PROTOCOL_P2P, NULL,
337 p2p_gettxinfo, check_interference);
339 for (t = 0; t < NETWORK->nDeviceCount; t++)
341 for (ti = 0; ti < DEVICE(t + 1)->nNumOfInterface; ti++)
343 if (!isP2PConfigured(t + 1, ti + 1))
345 if (!isP2PWireless(t + 1, ti + 1))
347 for (r = 0; r < NETWORK->nDeviceCount; r++)
349 for (ri = 0; ri < DEVICE(r + 1)->nNumOfInterface; ri++)
351 if (!isP2PConfigured(r + 1, ri + 1))
353 if (!isP2PWireless(r + 1, ri + 1))
355 p2p_CalculateReceivedPower(t + 1, ti + 1, r + 1, ri + 1);
363int fn_NetSim_P2P_LinkStateChanged(NETSIM_ID linkId,
366 NetSim_LINKS* link = NETWORK->ppstruNetSimLinks[linkId-1];
367 NETSIM_ID d1 = link->puniDevList.pstruP2P.nFirstDeviceId;
368 NETSIM_ID d2 = link->puniDevList.pstruP2P.nSecondDeviceId;
369 NETSIM_ID i1 = link->puniDevList.pstruP2P.nFirstInterfaceId;
370 NETSIM_ID i2 = link->puniDevList.pstruP2P.nSecondInterfaceId;
373 if (newState == LINKSTATE_UP)
374 subevent = P2P_LINK_UP;
376 subevent = P2P_LINK_DOWN;
377 NetSim_EVENTDETAILS pevent;
378 memset(&pevent, 0,
sizeof pevent);
380 if (isP2PConfigured(d1, i1))
382 pevent.dEventTime = pstruEventDetails->dEventTime;
383 pevent.nDeviceId = d1;
384 pevent.nDeviceType = DEVICE_TYPE(d1);
385 pevent.nEventType = TIMER_EVENT;
386 pevent.nInterfaceId = i1;
387 pevent.nProtocolId = MAC_PROTOCOL_P2P;
388 pevent.nSubEventType = subevent;
389 fnpAddEvent(&pevent);
392 if (isP2PConfigured(d2, i2))
394 pevent.dEventTime = pstruEventDetails->dEventTime;
395 pevent.nDeviceId = d2;
396 pevent.nDeviceType = DEVICE_TYPE(d2);
397 pevent.nEventType = TIMER_EVENT;
398 pevent.nInterfaceId = i2;
399 pevent.nProtocolId = MAC_PROTOCOL_P2P;
400 pevent.nSubEventType = subevent;
401 fnpAddEvent(&pevent);
406static void add_event_link_up()
410 NetSim_EVENTDETAILS pevent;
411 memset(&pevent, 0,
sizeof pevent);
413 for (d = 0; d < NETWORK->nDeviceCount; d++)
415 for (i = 0; i < DEVICE(d + 1)->nNumOfInterface; i++)
417 if(!isP2PConfigured(d+1,i+1))
421 fn_NetSim_Link_RegisterLinkFailureCallback(DEVICE_PHYLAYER(d + 1, i + 1)->pstruNetSimLinks->nLinkId,
422 fn_NetSim_P2P_LinkStateChanged);
423 pevent.nDeviceId = d + 1;
424 pevent.nDeviceType = DEVICE_TYPE(d + 1);
425 pevent.nEventType = TIMER_EVENT;
426 pevent.nInterfaceId = i + 1;
427 pevent.nProtocolId = MAC_PROTOCOL_P2P;
428 pevent.nSubEventType = P2P_LINK_UP;
429 fnpAddEvent(&pevent);