NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Component 2/Aloha/PropagationModel.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 * Author: Shashi Kant Suman *
12 * *
13 * ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "Aloha.h"
16
18{
19 NETSIM_ID TX_ID;
20 NETSIM_ID RX_ID;
21 PPROPAGATION_INFO Propagation_Info;
22 _ptr_ele ele;
23}propagation_info_for_list, * ptrpropagation_info_for_list;
24ptrpropagation_info_for_list propagation_info_list;
25
26#define PPROPAGATION_INFO_ALLOC() (struct stru_propagation_info_for_list*)list_alloc(sizeof(struct stru_propagation_info_for_list),offsetof(struct stru_propagation_info_for_list,ele))
27#define PPROPAGATION_INFO_ADD(info,e) (LIST_ADD_LAST(&(info),(e)))
28#define PPROPAGATION_INFO_NEXT(entity) (LIST_NEXT(entity))
29#define PPROPAGATION_INFO_REMOVE(ls, mem) (LIST_FREE((void**)(ls),(mem)))
30
31void ALOHA_gettxinfo(NETSIM_ID nTxId,
32 NETSIM_ID nTxInterface,
33 NETSIM_ID nRxId,
34 NETSIM_ID nRxInterface,
35 PTX_INFO Txinfo)
36{
37 PALOHA_PHY_VAR txphy = ALOHA_PHY(nTxId, nTxInterface);
38 PALOHA_PHY_VAR rxphy = ALOHA_PHY(nRxId, nRxInterface);
39
40 Txinfo->dCentralFrequency = txphy->frequency;
41 Txinfo->dRxAntennaHeight = rxphy->dAntennaHeight;
42 Txinfo->dRxGain = rxphy->dAntennaGain;
43 Txinfo->dTxAntennaHeight = txphy->dAntennaHeight;
44 Txinfo->dTxGain = txphy->dAntennaGain;
45 Txinfo->dTxPower_mw = (double)txphy->tx_power;
46 Txinfo->dTxPower_dbm = MW_TO_DBM(Txinfo->dTxPower_mw);
47 Txinfo->dTx_Rx_Distance = DEVICE_DISTANCE(nTxId, nRxId);
48}
49
50static void fill_propagation_from_link(NETSIM_ID d, NETSIM_ID in, PPROPAGATION prop)
51{
52 memset(prop, 0, sizeof * prop);
53 NetSim_LINKS* link = DEVICE_PHYLAYER(d, in)->pstruNetSimLinks;
54 memcpy(prop, link->puniMedProp.pstruWirelessLink.propagation, sizeof * prop);
55 prop->pathlossVar.d0 = 1;
56}
57
58bool CheckFrequencyInterfrence(double dFrequency1, double dFrequency2, double bandwidth)
59{
60 if (dFrequency1 > dFrequency2)
61 {
62 if ((dFrequency1 - dFrequency2) >= bandwidth)
63 return false; // no interference
64 else
65 return true; // interference
66 }
67 else
68 {
69 if ((dFrequency2 - dFrequency1) >= bandwidth)
70 return false; // no interference
71 else
72 return true; // interference
73 }
74}
75
76bool check_interference(NETSIM_ID t, NETSIM_ID ti, NETSIM_ID r, NETSIM_ID ri)
77{
78 PALOHA_PHY_VAR tp = ALOHA_PHY(t, ti);
79 PALOHA_PHY_VAR rp = ALOHA_PHY(r, ri);
80 return CheckFrequencyInterfrence(tp->frequency, rp->frequency, tp->bandwidth);
81}
82
83ptrpropagation_info_for_list fn_NetSim_Aloha_InitPropagationModel(NETSIM_ID tx, NETSIM_ID rx)
84{
85 ptrpropagation_info_for_list entry = PPROPAGATION_INFO_ALLOC();
86 entry->TX_ID = tx;
87 entry->RX_ID = rx;
88 TX_INFO txInfo;
89 ALOHA_gettxinfo(tx, 1, rx, 1, &txInfo);
90 PROPAGATION prop;
91 fill_propagation_from_link(tx, 1, &prop);
92 entry->Propagation_Info = propagation_create_propagation_info(tx, 1, rx, 1, NULL, &txInfo, &prop);
93 return entry;
94}
95
96PPROPAGATION_INFO find_propagation_info(NETSIM_ID tx, NETSIM_ID rx)
97{
98 ptrpropagation_info_for_list data = propagation_info_list;
99 while (data)
100 {
101 if (data->TX_ID == tx && data->RX_ID == rx)
102 {
103 data->Propagation_Info->txInfo.dTx_Rx_Distance = DEVICE_DISTANCE(tx, rx);
104 return data->Propagation_Info;
105 }
106 else {
107 data = PPROPAGATION_INFO_NEXT(data);
108 }
109 }
110 return 0;
111}
112
113double get_received_power(NETSIM_ID TX,NETSIM_ID RX, double time)
114{
115 return _propagation_get_received_power_dbm(find_propagation_info(TX, RX), time);
116}
117
118double get_rx_power_callbackhandler(NETSIM_ID tx, NETSIM_ID txi,
119 NETSIM_ID rx, NETSIM_ID rxi,
120 double time)
121{
122 return DBM_TO_MW(_propagation_get_received_power_dbm(find_propagation_info(tx, rx), time));
123}
124
125PPROPAGATION_INFO get_aloha_propagation_info(NETSIM_ID TX, NETSIM_ID RX)
126{
127 return find_propagation_info(TX, RX);
128}
129
130int fn_NetSim_Aloha_FreePropagationInfo()
131{
132 NETSIM_ID t, ti;
133
134 for (t = 0; t < NETWORK->nDeviceCount; t++)
135 {
136 for (ti = 0; ti < DEVICE(t + 1)->nNumOfInterface; ti++)
137 {
138 ptrpropagation_info_for_list data = propagation_info_list;
139 while (data)
140 {
141 ptrpropagation_info_for_list temp = PPROPAGATION_INFO_NEXT(data);
142 PPROPAGATION_INFO_REMOVE(&propagation_info_list, data);
143 data = temp;
144 }
145 }
146 }
147 return 0;
148}
149
150static void calculate_received_power(PPROPAGATION_INFO info)
151{
152 info->txInfo.Tx_X = DEVICE_POSITION(info->nTxId)->X;
153 info->txInfo.Tx_Y = DEVICE_POSITION(info->nTxId)->Y;
154 info->txInfo.Tx_Z = DEVICE_POSITION(info->nTxId)->Z;
155
156 info->txInfo.Rx_X = DEVICE_POSITION(info->nRxId)->X;
157 info->txInfo.Rx_Y = DEVICE_POSITION(info->nRxId)->Y;
158 info->txInfo.Rx_Z = DEVICE_POSITION(info->nRxId)->Z;
159
160 _propagation_calculate_received_power(info, pstruEventDetails->dEventTime);
161}
162
163int fn_NetSim_Aloha_CalulateReceivedPower()
164{
165 NETSIM_ID i,j;
166 for(i=0;i<NETWORK->nDeviceCount;i++)
167 {
168 for (j = 0; j < NETWORK->nDeviceCount; j++)
169 {
170 ptrpropagation_info_for_list entry;
171 entry = fn_NetSim_Aloha_InitPropagationModel(i + 1, j + 1);
172 PPROPAGATION_INFO_ADD(propagation_info_list, entry);
173 calculate_received_power(entry->Propagation_Info);
174 }
175 }
176 return 0;
177}
178
179int fn_NetSim_Aloha_handleMobility(NETSIM_ID d)
180{
181 for (NETSIM_ID i = 0; i < NETWORK->nDeviceCount; i++)
182 {
183 if (i + 1 == d) { continue; }
184 calculate_received_power(find_propagation_info(d, i + 1));
185 calculate_received_power(find_propagation_info(i + 1, d));
186 }
187 return 0;
188}