NetSim Source Code Help
Loading...
Searching...
No Matches
Satellite_PropagationModel.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 "SATELLITE.h"
26#include "Satellite_PHY.h"
27#include "Satellite_MAC.h"
28#include "Satellite_Frame.h"
29
30#pragma region FUNCTIONPROTOTYPE
32 PTX_INFO info, LINKTYPE linkType);
34 PTX_INFO info, LINKTYPE linkType);
36 PPROPAGATION prop);
37#pragma endregion
38
39#pragma region PROPAGATIONMODEL_INIT
41{
44
45 TX_INFO txInfo;
47
48 PROPAGATION prop;
49 fill_propagation_from_link(d, in, &prop);
50
52 mac->satelliteId, mac->satelliteIf,
53 NULL, &txInfo, &prop);
54
57 d, in,
58 NULL, &txInfo, &prop);
59}
60
62{
65
66 TX_INFO txInfo;
68
69 PROPAGATION prop;
70 fill_propagation_from_link(d, in, &prop);
71
73 mac->satelliteId, mac->satelliteIf,
74 NULL, &txInfo, &prop);
75
78 d, in,
79 NULL, &txInfo, &prop);
80}
81#pragma endregion
82
83#pragma region PROPAGATIONMODEL_HELPER
85 PTX_INFO info, LINKTYPE linkType)
86{
87 memset(info, 0, sizeof * info);
90
91 NETSIM_ID s = mac->satelliteId;
92 NETSIM_ID sin = mac->satelliteIf;
95
96 if (linkType == LINKTYPE_FORWARD)
97 {
99 info->dRxGain = phy->rxAntennaGain_db;
100 info->dTxGain = sphy->txAntennaGain_db;
101 info->dTxPower_dbm = sphy->txPower_dbm;
102 info->dTxPower_mw = sphy->txPower_mw;
104 }
105 else if (linkType == LINKTYPE_RETURN)
106 {
108 info->dTxGain = phy->txAntennaGain_db;
109 info->dRxGain = sphy->rxAntennaGain_db;
110 info->dTxPower_dbm = phy->txPower_dbm;
111 info->dTxPower_mw = phy->txPower_mw;
113 }
114
115 info->d0 = 1;
116 info->dTx_Rx_Distance = DEVICE_DISTANCE(d, s);
117}
118
120 PPROPAGATION prop)
121{
122 memset(prop, 0, sizeof * prop);
123 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
124 memcpy(prop, link->puniMedProp.pstruWirelessLink.propagation, sizeof * prop);
125 prop->pathlossVar.d0 = 1;
126}
127
129 PTX_INFO info, LINKTYPE linkType)
130{
131 memset(info, 0, sizeof * info);
134
135 NETSIM_ID s = mac->satelliteId;
136 NETSIM_ID sin = mac->satelliteIf;
137 ptrSATELLITE_MAC smac = SATELLITE_MAC_GET(s, sin);
138 ptrSATELLITE_PHY sphy = SATELLITE_PHY_GET(s, sin);
139
140 if (linkType == LINKTYPE_FORWARD)
141 {
143 info->dTxGain = phy->txAntennaGain_db;
144 info->dRxGain = sphy->rxAntennaGain_db;
145 info->dTxPower_dbm = phy->txPower_dbm;
146 info->dTxPower_mw = phy->txPower_mw;
148 }
149 else if (linkType == LINKTYPE_RETURN)
150 {
152 info->dRxGain = phy->rxAntennaGain_db;
153 info->dTxGain = sphy->txAntennaGain_db;
154 info->dTxPower_dbm = sphy->txPower_dbm;
155 info->dTxPower_mw = sphy->txPower_mw;
157 }
158
159 info->d0 = 1;
160 info->dTx_Rx_Distance = DEVICE_DISTANCE(d, s);
161}
162#pragma endregion
163
164#pragma region PROPAGATIONMODEL_CALCULATE
166{
170}
171
173{
177}
178#pragma endregion
179
180#pragma region SATELLITE_PACKET_ERROR
182{
183 if (isUT(t, ti))
184 {
187 }
188 else if (isGW(t, ti))
189 {
192 }
193 else
194 {
195 if (isUT(r, ri))
196 {
199 }
200 else if (isGW(r, ri))
201 {
204 }
205 }
206 return 0;
207}
208
210{
211 if (isUT(t, ti))
212 {
215 }
216 else if (isGW(t, ti))
217 {
220 }
221 else
222 {
223 if (isUT(r, ri))
224 {
227 }
228 else if (isGW(r, ri))
229 {
232 }
233 }
234 return 0;
235}
236
237#define BOLTZMAAN_CONSTANT 1.38064852e-23 // m2 kg s-2 K-1
239{
240 double noise_w = 0;
241 if (isUT(t, ti))
242 {
246 }
247 else if (isGW(t, ti))
248 {
252 }
253 else
254 {
255 if (isUT(r, ri))
256 {
260 }
261 else if (isGW(r, ri))
262 {
266 }
267 }
268
269 return MW_TO_DBM(noise_w * 1000);
270}
271
272static double satellite_calculate_snr(double pdbm, double ndbm)
273{
274 return MW_TO_DBM(DBM_TO_MW(pdbm) / DBM_TO_MW(ndbm));
275}
276
278{
279 if (isUT(t, ti))
280 {
282 return phy->returnLinkSuperFrame;
283 }
284 else if (isGW(t, ti))
285 {
287 return phy->forwardLinkSuperFrame;
288 }
289 else
290 {
291 if (isUT(r, ri))
292 {
294 return phy->forwardLinkSuperFrame;
295 }
296 else if (isGW(r, ri))
297 {
299 return phy->returnLinkSuperFrame;
300 }
301 }
302 return NULL;
303}
304
305static double satellite_calculate_ber(ptrSUPERFRAME sf, double snr)
306{
308 if (cc->berModel == BERMODEL_Fixed)
309 return cc->berValue;
310 else if (cc->berModel == BERMODEL_File)
311 return calculate_ber(snr, cc->berTable, cc->berTableLen);
312 else if (cc->berModel == BERMODEL_Model)
313 {
314 double dataRate = sf->slotConfig->bitsPerSlot / sf->slotConfig->slotLength_us;
315 return Calculate_ber_by_calculation(snr, cc->modulation, dataRate, cc->allocatedBandwidth_Hz / MHZ);
316 }
317 return 0;
318}
319
320static PACKET_STATUS satellite_isPacketErrored(ptrSUPERFRAME sf, double bytes, double snr)
321{
322 double ber = satellite_calculate_ber(sf, snr);
323 return fn_NetSim_Packet_DecideError(ber, bytes);
324}
325
326#ifdef SATELLITE_PROPAGATION_LOG
327typedef struct stru_fileinfo
328{
329 NETSIM_ID t;
330 NETSIM_ID ti;
331 NETSIM_ID r;
332 NETSIM_ID ri;
333
334 FILE* fp;
335 struct stru_fileinfo* next;
336}FILEINFO,*ptrFILEINFO;
337ptrFILEINFO fileInfo = NULL;
338
339static ptrFILEINFO create_new_log_file(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
340{
341 ptrFILEINFO f = calloc(1, sizeof * f);
342 f->t = t;
343 f->ti = ti;
344 f->r = r;
345 f->ri = ri;
346
347 char s[BUFSIZ];
348 sprintf(s, "%s/log/SatPropLog_%d_%d_%d_%d.csv",
349 pszIOPath, t, ti, r, ri);
350 f->fp = fopen(s, "w");
351 if (!f->fp)
352 {
353 fnSystemError("Unable to open %s\n", s);
354 perror(s);
355 free(f);
356 return NULL;
357 }
358 fprintf(f->fp, "Time(Microseconds),ReceivedPower(dBm),FadingLoss(dB),Noise(dBm),SNR(dB)\n");
359 ptrFILEINFO tf = fileInfo;
360 if (tf)
361 {
362 while (tf->next) tf = tf->next;
363 tf->next = f;
364 }
365 else
366 {
367 fileInfo = f;
368 }
369 return f;
370}
371
372static FILE* find_log_file(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
373{
374 ptrFILEINFO f = fileInfo;
375 while (f)
376 {
377 if (f->t == t && f->ti == ti && f->r == r && f->ri == ri)
378 return f->fp;
379 f = f->next;
380 }
381
382 f = create_new_log_file(t, ti, r, ri);
383 return f->fp;
384}
385
386static void log_power(NETSIM_ID t, NETSIM_ID ti,NETSIM_ID r,NETSIM_ID ri,
387 double p, double f, double n, double snr)
388{
389 FILE* fp = find_log_file(t, ti, r, ri);
390 fprintf(fp, "%lf,%lf,%lf,%lf,%lf,\n",
391 ldEventTime, p, f, n, snr);
392 fflush(fp);
393}
394#endif
395
397 NetSim_PACKET* packet)
398{
399 double pdbm = satellite_get_rx_power_dbm(t, ti, r, ri);
400 double fdb = satellite_get_fading_loss_db(t, ti, r, ri);
401 pdbm -= fdb;
402
403 double ndbm = satellite_calculate_noise(t, ti, r, ri);
404 double snr = satellite_calculate_snr(pdbm, ndbm);
405
406#ifdef SATELLITE_PROPAGATION_LOG
407 log_power(t, ti, r, ri, pdbm + fdb, fdb, ndbm, snr);
408#endif
409 double bytes = packet->pstruPhyData->dPacketSize;
410
411 ptrSUPERFRAME sf = find_superframe(t, ti, r, ri);
412 packet->nPacketStatus = satellite_isPacketErrored(sf, bytes, snr);
413}
414#pragma endregion
unsigned int NETSIM_ID
Definition: Animation.h:45
double Calculate_ber_by_calculation(double sinr, PHY_MODULATION modulation, double dataRate_mbps, double bandwidth_mHz)
double calculate_ber(double snr, BER ber_table[], size_t table_len)
#define fnSystemError(x,...)
Definition: Linux.h:55
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
PACKET_STATUS fn_NetSim_Packet_DecideError(double dBER, long double dPacketSize)
#define MW_TO_DBM(mw)
#define DBM_TO_MW(dbm)
void _propagation_calculate_received_power(PPROPAGATION_INFO info, double time)
double _propagation_get_received_power_dbm(PPROPAGATION_INFO info, double time)
Used to the received power in dBm over time.
#define MHZ
double _propagation_calculate_fadingloss(PPROPAGATION_INFO info)
Used to calculate the fading loss. Fading model and parameter must be set in PROPAGATION structure wi...
PPROPAGATION_INFO propagation_create_propagation_info(NETSIM_ID txId, NETSIM_ID txIf, NETSIM_ID rxId, NETSIM_ID rxIf, char *uniqueId, PTX_INFO txInfo, PPROPAGATION propagation)
#define SATELLITE_PHY_GET(d, i)
Definition: Satellite.h:107
@ LINKTYPE_FORWARD
Definition: Satellite.h:61
@ LINKTYPE_RETURN
Definition: Satellite.h:62
#define SATELLITE_UTPHY_GET(d, i)
Definition: Satellite.h:109
#define isGW(d, i)
Definition: Satellite.h:123
#define SATELLITE_GWPHY_GET(d, i)
Definition: Satellite.h:111
#define SATELLITE_MAC_GET(d, i)
Definition: Satellite.h:108
#define SATELLITE_GWMAC_GET(d, i)
Definition: Satellite.h:112
#define isUT(d, i)
Definition: Satellite.h:122
#define SATELLITE_UTMAC_GET(d, i)
Definition: Satellite.h:110
enum enum_linktype LINKTYPE
@ BERMODEL_Model
@ BERMODEL_File
@ BERMODEL_Fixed
static void create_tx_info_for_ut(NETSIM_ID d, NETSIM_ID in, PTX_INFO info, LINKTYPE linkType)
static double satellite_get_fading_loss_db(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
void satellite_check_for_packet_error(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri, NetSim_PACKET *packet)
void satellite_propagation_ut_calculate_rxpower(NETSIM_ID d, NETSIM_ID in, double time)
static ptrSUPERFRAME find_superframe(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
void satellite_propagation_gw_calculate_rxpower(NETSIM_ID d, NETSIM_ID in, double time)
void satellite_propgation_gw_init(NETSIM_ID d, NETSIM_ID in)
static double satellite_calculate_noise(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
void satellite_propgation_ut_init(NETSIM_ID d, NETSIM_ID in)
static double satellite_get_rx_power_dbm(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
static void create_tx_info_for_gw(NETSIM_ID d, NETSIM_ID in, PTX_INFO info, LINKTYPE linkType)
static double satellite_calculate_ber(ptrSUPERFRAME sf, double snr)
#define BOLTZMAAN_CONSTANT
static void fill_propagation_from_link(NETSIM_ID d, NETSIM_ID in, PPROPAGATION prop)
static double satellite_calculate_snr(double pdbm, double ndbm)
static PACKET_STATUS satellite_isPacketErrored(ptrSUPERFRAME sf, double bytes, double snr)
EXPORTED double ldEventTime
Definition: Stack.h:838
EXPORTED char * pszIOPath
Definition: Stack.h:842
#define DEVICE_DISTANCE(d1, d2)
Definition: Stack.h:814
#define DEVICE_PHYLAYER(DeviceId, InterfaceId)
Definition: Stack.h:788
uint f(uint state, uchar key[])
Definition: des.c:172
enum enum_PacketStatus PACKET_STATUS
Definition: main.h:128
PACKET_STATUS nPacketStatus
Definition: Packet.h:272
struct stru_NetSim_Packet_PhyLayer * pstruPhyData
Definition: Packet.h:277
PPROPAGATION_INFO rxPropagationInfo
Definition: Satellite_PHY.h:88
PPROPAGATION_INFO txPropagationInfo
Definition: Satellite_PHY.h:87
ptrSUPERFRAME forwardFLinkSuperFrame
Definition: Satellite_MAC.h:88
ptrSUPERFRAME returnLinkSuperFrame
Definition: Satellite_MAC.h:87
ptrSUPERFRAME returnLinkSuperFrame
Definition: Satellite_PHY.h:42
ptrSUPERFRAME forwardLinkSuperFrame
Definition: Satellite_PHY.h:43
PPROPAGATION_INFO rxPropagationInfo
Definition: Satellite_PHY.h:69
PPROPAGATION_INFO txPropagationInfo
Definition: Satellite_PHY.h:68
PHY_MODULATION modulation
ptrCARRIERCONF carrierConfig
double dTx_Rx_Distance
double dCentralFrequency