NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
IEEE802.11_Radio_Measurements.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: Kanak *
22* *
23* ----------------------------------------------------------------------------------*/
24
25#include "main.h"
26#include "Medium.h"
27#include "ErrorModel.h"
28#include "PropagationModel.h"
29#include "IEEE802_11.h"
30#include "IEEE802_11_Phy.h"
31
32static FILE* fpRMlog = NULL;
33
34UINT DEVICE_ID_LIST[] = { 0 };//When { 0 } log is written for all WiFi Devices. Specific WiFi Device ID's can be defined
35//for example {1,2,3} log will be written only for WiFi Device ID's 1, 2 and 3 as transmitter/receiver
36
37static bool validate_log(NETSIM_ID nDevID)
38{
39 int count = sizeof(DEVICE_ID_LIST) / sizeof(int);
40 if (count == 1 && DEVICE_ID_LIST[0] == 0) return true;
41 else
42 {
43 for (int i = 0; i < count; i++)
44 {
45 if (DEVICE_ID_LIST[i] == DEVICE_CONFIGID(nDevID))
46 return true;
47 }
48 }
49 return false;
50}
51
52static void init_radio_measurements_log()
53{
54 if (get_protocol_log_status("IEEE_802_11_RADIO_MEASUREMENTS_LOG"))
55 {
56 char s[BUFSIZ];
57 sprintf(s, "%s\\%s", pszIOLogPath, "IEEE_802_11_Radio_Measurements_Log.csv");
58 fpRMlog = fopen(s, "w");
59 if (!fpRMlog)
60 {
61 fnSystemError("Unable to open %s file", s);
62 perror(s);
63 }
64 else
65 {
66 fprintf(fpRMlog, "%s,%s,%s,%s,",
67 "Time(ms)", "Transmitter Name", "Receiver Name", "Distance(m)");
68
69 fprintf(fpRMlog, "%s,%s,%s,",
70 "Packet ID", "Packet Type", "Control Packet Type");
71
72 fprintf(fpRMlog, "%s,%s,%s,%s,",
73 "Tx_Power(dBm)", "Path Loss(dB)", "Shadowing Loss(dB)", "Fading Loss(dB)");
74
75
76 fprintf(fpRMlog, "%s,%s,%s,",
77 "Total Loss(dB)", "Tx Antenna Gain(dB)", "Rx Antenna Gain(dB)");
78
79 fprintf(fpRMlog, "%s,%s,%s,",
80 "Rx_Power(dBm)", "Interference(dBm)", "SNR(dB)");
81
82 fprintf(fpRMlog, "%s,%s,%s,%s,",
83 "SINR(dB)", "NSS", "BER", "MCS Index");
84
85 fprintf(fpRMlog, "\n");
86 if (nDbgFlag) fflush(fpRMlog);
87 }
88 }
89}
90
91static void close_radio_measurements_log()
92{
93 if (fpRMlog)
94 fclose(fpRMlog);
95}
96
97void IEEE_802_11RadioMeasurements_Init()
98{
99 init_radio_measurements_log();
100}
101
102void IEEE_802_11RadioMeasurements_Finish()
103{
104 close_radio_measurements_log();
105}
106
107void IEEE_802_11RadioMeasurements_Log(NetSim_PACKET* packet, NETSIM_ID txid, NETSIM_ID txif, NETSIM_ID rxid, NETSIM_ID rxif)
108{
109 if (fpRMlog == NULL || (!validate_log(txid) && !validate_log(rxid))) { return; }
110
111
112 PPROPAGATION_INFO txinfo = find_propagation_info(txid, txif, rxid, rxif);
113 PIEEE802_11_PHY_VAR txPhy = IEEE802_11_PHY(txid, txif);
114 PIEEE802_11_PHY_VAR rxPhy = IEEE802_11_PHY(rxid, rxif);
115
116 double rxPowerdBm = _propagation_get_received_power_dbm(txinfo, pstruEventDetails->dEventTime);
117 double interference = medium_get_interference(packet, txid, txif, rxid, rxif);
118 double FadingLoss = propagation_get_Fadingloss(txinfo, pstruEventDetails->dEventTime);
119 rxPowerdBm -= FadingLoss;
120 double ber = 0;
121 switch (txPhy->PhyProtocol)
122 {
123 case IEEE_802_11b:
124 if (strcmp(strBERMODEL[txPhy->Ber_Model], "SINR_BER_PER_TABLE") == 0)
125 ber = calculate_BER_Using_Table(txPhy->PHY_TYPE.dsssPhy.modulation, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->MCS, strWLANProtocol[txPhy->PhyProtocol]);
126 else
127 ber = calculate_FEC_BER(txPhy->PHY_TYPE.dsssPhy.modulation, txPhy->PHY_TYPE.dsssPhy.dCodeRate, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->PHY_TYPE.dsssPhy.dDataRate);
128 break;
129 case IEEE_802_11a:
130 case IEEE_802_11g:
131 case IEEE_802_11p:
132 if (strcmp(strBERMODEL[txPhy->Ber_Model], "SINR_BER_PER_TABLE") == 0)
133 ber = calculate_BER_Using_Table(txPhy->PHY_TYPE.ofdmPhy.modulation, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->MCS, strWLANProtocol[txPhy->PhyProtocol]);
134 else
135 ber = calculate_FEC_BER(txPhy->PHY_TYPE.ofdmPhy.modulation, txPhy->PHY_TYPE.ofdmPhy.dCodingRate, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->PHY_TYPE.ofdmPhy.dDataRate);
136 break;
137 case IEEE_802_11n:
138 if (strcmp(strBERMODEL[txPhy->Ber_Model], "SINR_BER_PER_TABLE") == 0)
139 ber = calculate_BER_Using_Table(txPhy->PHY_TYPE.ofdmPhy_11n.modulation, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->MCS, strWLANProtocol[txPhy->PhyProtocol]);
140 else
141 ber = calculate_FEC_BER(txPhy->PHY_TYPE.ofdmPhy_11n.modulation, txPhy->PHY_TYPE.ofdmPhy_11n.dCodingRate, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->PHY_TYPE.ofdmPhy_11n.dDataRate);
142 break;
143 case IEEE_802_11ac:
144 if (strcmp(strBERMODEL[txPhy->Ber_Model], "SINR_BER_PER_TABLE") == 0)
145 ber = calculate_BER_Using_Table(txPhy->PHY_TYPE.ofdmPhy_11ac.modulation, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->MCS, strWLANProtocol[txPhy->PhyProtocol]);
146 else
147 ber = calculate_FEC_BER(txPhy->PHY_TYPE.ofdmPhy_11ac.modulation, txPhy->PHY_TYPE.ofdmPhy_11ac.dCodingRate, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->PHY_TYPE.ofdmPhy_11ac.dDataRate);
148 break;
149 case IEEE_802_11ax:
150 if (strcmp(strBERMODEL[txPhy->Ber_Model], "SINR_BER_PER_TABLE") == 0)
151 ber = calculate_BER_Using_Table(txPhy->PHY_TYPE.ofdmPhy_11ax.modulation, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->MCS, strWLANProtocol[txPhy->PhyProtocol]);
152 else
153 ber = calculate_FEC_BER(txPhy->PHY_TYPE.ofdmPhy_11ax.modulation, txPhy->PHY_TYPE.ofdmPhy_11ax.dCodingRate, rxPowerdBm, interference, txPhy->dChannelBandwidth, txPhy->PHY_TYPE.ofdmPhy_11ax.dDataRate);
154 break;
155 default:
156 fnNetSimError("IEEE802.11--- Unknown protocol %d in %s\n", txPhy->PhyProtocol, __FUNCTION__);
157 break;
158 }
159 char type[BUFSIZ];
160 char ptype[BUFSIZ];
161 fprintf(fpRMlog, "%lf,%s,%s,%lf,",
162 (ldEventTime/MILLISECOND), DEVICE_NAME(txid),
163 DEVICE_NAME(rxid),
164 DEVICE_DISTANCE(txid, rxid));
165
166 fprintf(fpRMlog, "%lld,%s,%s,",
167 packet->nPacketId,
168 fn_NetSim_Config_GetPacketTypeAsString(packet->nPacketType, ptype),
169 fn_NetSim_Config_GetControlPacketType(packet, type));
170
171 fprintf(fpRMlog, "%lf,%lf,%lf,%lf,",
172 MW_TO_DBM(txPhy->nTxPower_mw),
173 propagation_get_Pathloss(txinfo, pstruEventDetails->dEventTime),
174 propagation_get_Shadowloss(txinfo, pstruEventDetails->dEventTime),
175 FadingLoss);
176
177
178 fprintf(fpRMlog, "%lf,%lf,%lf,%lf,%lf,",
179 propagation_get_Totalloss(txinfo, pstruEventDetails->dEventTime),
180 txinfo->txInfo.dTxGain,
181 txinfo->txInfo.dRxGain,
182 rxPowerdBm,
183 interference);
184
185
186 fprintf(fpRMlog, "%lf,%lf,%d,%lf,%d,",
187 calculate_sinr(rxPowerdBm, NEGATIVE_DBM, txPhy->dChannelBandwidth),
188 calculate_sinr(rxPowerdBm, interference, txPhy->dChannelBandwidth),
189 txPhy->NSS,
190 ber,
191 txPhy->MCS);
192
193 fprintf(fpRMlog, "\n");
194 if (nDbgFlag) fflush(fpRMlog);
195}
196