NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Aloha_Phy.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#include "Aloha_enum.h"
17#include "../UWAN/UWAN.h"
18#include "AdvancedPlots.h"
19#pragma comment(lib,"AdvancedPlots.lib")
20#include "../BatteryModel/BatteryModel.h"
21
22typedef struct stru_packetList
23{
24 NetSim_PACKET* head;
25 NetSim_PACKET* tail;
26}PACKETLIST, *ptrPACKETLIST;
27
28PACKETLIST txPacketList;
29
30static void add_packet_to_txList(NetSim_PACKET* packet)
31{
32 if (txPacketList.head)
33 {
34 txPacketList.tail->pstruNextPacket = packet;
35 txPacketList.tail = packet;
36 }
37 else
38 {
39 txPacketList.head = packet;
40 txPacketList.tail = packet;
41 }
42}
43static bool check_packet_in_txList(NETSIM_ID tx)
44
45{
46 NetSim_PACKET* t = txPacketList.head;
47 while (t)
48 {
49 if (t->nTransmitterId == tx)
50 return true;
51 t = t->pstruNextPacket;
52 }
53 return false;
54}
55
56static void remove_packet_from_txList(NetSim_PACKET* packet)
57{
58 NetSim_PACKET* t = txPacketList.head;
59 NetSim_PACKET* p = NULL;
60 while (t)
61 {
62 if (t == packet)
63 {
64 if (t == txPacketList.head)
65 {
66 txPacketList.head = t->pstruNextPacket;
67 if (!txPacketList.head)
68 txPacketList.tail = NULL;
69 }
70 else
71 {
72 p->pstruNextPacket = t->pstruNextPacket;
73 if (!t->pstruNextPacket)
74 txPacketList.tail = p;
75 }
76 packet->pstruNextPacket = NULL;
77 break;
78 }
79 p = t;
80 t = t->pstruNextPacket;
81 }
82}
83
84static void mark_packet_for_collision()
85{
86 NetSim_PACKET* p = txPacketList.head;
87 while (p)
88 {
89 PALOHA_PHY_VAR phy = ALOHA_PHY(p->nReceiverId, 1);
90
91 double power = get_received_power(p->nTransmitterId, p->nReceiverId, p->pstruPhyData->dArrivalTime);
92 double interference = substract_power_in_dbm(phy->dInterferencePower, power);
93
94 if ( interference > phy->rx_sensitivity)
95 p->nPacketStatus = PacketStatus_Collided;
96
97 p = p->pstruNextPacket;
98 }
99}
100
101static void update_interference_power(NETSIM_ID tx,
102 NETSIM_ID rx,
103 double time,
104 bool isAdd)
105{
106 rx;
107 NETSIM_ID i;
108 PALOHA_PHY_VAR phy;
109 for (i = 0; i < NETWORK->nDeviceCount; i++)
110 {
111 if (i + 1 == tx)
112 continue;
113
114 phy = ALOHA_PHY(i + 1, 1);
115 double p = phy->dInterferencePower;
116 if (isAdd)
117 phy->dInterferencePower = add_power_in_dbm(p,
118 get_received_power(tx, i + 1, time));
119 else
120 phy->dInterferencePower = substract_power_in_dbm(p,
121 get_received_power(tx, i + 1, time));
122 }
123}
124
125static double calulate_transmission_time(NetSim_PACKET* packet)
126{
127 double size=fnGetPacketSize(packet);
128 NETSIM_ID source=packet->nTransmitterId;
129 PALOHA_PHY_VAR phy = ALOHA_PHY(source,1);
130 double dDataRate=phy->data_rate;
131 double dTxTime= size*8.0/dDataRate;
132 return dTxTime;
133}
134
135static double calculate_propagation_delay(NetSim_PACKET* packet)
136{
137 NETSIM_ID source = packet->nTransmitterId;
138 NETSIM_ID destination = packet->nReceiverId;
139 double dPropagationDelay = UWAN_calculate_propagation_delay(source, 1,
140 destination, 1,
141 get_aloha_propagation_info(source, destination));
142 return dPropagationDelay;
143}
144
145static void transmit_packet(NetSim_PACKET* packet, bool* flag)
146{
147 PALOHA_PHY_VAR phy = ALOHA_PHY(packet->nReceiverId,1);
148 PALOHA_PHY_VAR phyt = ALOHA_PHY(packet->nTransmitterId,1);
149 double power = get_received_power(packet->nTransmitterId, packet->nReceiverId,
150 packet->pstruPhyData->dArrivalTime);
151
152 ptrBATTERY battery = phyt->battery;
153
154 if(power < phy->rx_sensitivity)
155 {
156 set_radio_state(pstruEventDetails->nDeviceId, 1, RX_ON_IDLE);
157 packet->nPacketStatus=PacketStatus_Error;
158 }
159 if (battery)
160 {
161 if (phyt->transmitter_status == RX_OFF)
162 return false;
163 }
164 set_radio_state(pstruEventDetails->nDeviceId, 1, TRX_ON_BUSY);
165
166 set_radio_state(packet->nReceiverId, 1, RX_ON_BUSY);
167 //Add physical in event
168 pstruEventDetails->dEventTime=packet->pstruPhyData->dEndTime;
169 packet->pstruPhyData->dEndTime = pstruEventDetails->dEventTime;
170 pstruEventDetails->dPacketSize=packet->pstruPhyData->dPacketSize;
171 pstruEventDetails->nDeviceId=packet->nReceiverId;
172 pstruEventDetails->nDeviceType=DEVICE_TYPE(packet->nReceiverId);
173 pstruEventDetails->nEventType=PHYSICAL_IN_EVENT;
174 pstruEventDetails->nInterfaceId=1;
175 pstruEventDetails->pPacket=packet;
176 fnpAddEvent(pstruEventDetails);
177
178 add_packet_to_txList(packet);
179 if (!*flag)
180 update_interference_power(packet->nTransmitterId,
181 packet->nReceiverId,
182 packet->pstruPhyData->dArrivalTime,
183 true);
184 *flag = true;
185 mark_packet_for_collision();
186}
187
188int fn_NetSim_Aloha_PhyOut()
189{
190 double dTxTime;
191 double dPropagationDelay;
192 double receptionTime;
193 NetSim_PACKET* packet= pstruEventDetails->pPacket;
194 packet->pstruPhyData->dArrivalTime=pstruEventDetails->dEventTime;
195 packet->pstruPhyData->dPayload=packet->pstruMacData->dPacketSize;
196 packet->pstruPhyData->dOverhead=ALOHA_PHY_OVERHEAD;
197 packet->pstruPhyData->dPacketSize=packet->pstruPhyData->dOverhead+packet->pstruPhyData->dPayload;
198 packet->pstruPhyData->dStartTime=pstruEventDetails->dEventTime;
199
200 bool flag = false;
201 if(packet->nReceiverId)
202 {
203 dTxTime = calulate_transmission_time(packet);
204 dPropagationDelay = calculate_propagation_delay(packet);
205 packet->pstruPhyData->dStartTime = packet->pstruPhyData->dArrivalTime + dTxTime;
206 packet->pstruPhyData->dEndTime = packet->pstruPhyData->dStartTime + dPropagationDelay;
207 receptionTime = packet->pstruPhyData->dEndTime - packet->pstruPhyData->dArrivalTime;
208 PALOHA_MAC_VAR mac = ALOHA_CURR_MAC;
209 if (mac->isSlotted)
210 {
211 if (receptionTime > mac->slotlength)
212 {
213 fnNetSimError("The slot length for the scenario has not been set properly, it should be greater than %lf.\n", receptionTime);
214 return 0;
215 }
216 }
217 transmit_packet(packet, &flag);
218 }
219 else
220 {
221 //Broadcast packet
222 NETSIM_ID loop;
223 for(loop=0;loop<NETWORK->nDeviceCount;loop++)
224 {
225 if(loop+1!=packet->nTransmitterId)
226 {
227 NetSim_PACKET* p = fn_NetSim_Packet_CopyPacket(packet);
228 //Add physical in event
229 p->nReceiverId=loop+1;
230 dTxTime = calulate_transmission_time(p);
231 dPropagationDelay = calculate_propagation_delay(p);
232 p->pstruPhyData->dStartTime = p->pstruPhyData->dArrivalTime + dTxTime;
233 p->pstruPhyData->dEndTime = p->pstruPhyData->dStartTime + dPropagationDelay;
234 receptionTime = p->pstruPhyData->dEndTime - p->pstruPhyData->dArrivalTime;
235 PALOHA_MAC_VAR mac = ALOHA_CURR_MAC;
236 if (mac->isSlotted)
237 {
238 if (receptionTime > mac->slotlength)
239 {
240 fnNetSimError("The slot length for the scenario has not been set properly, it should be greater than %lf.\n", receptionTime);
241 return 0;
242 }
243 }
244 transmit_packet(p, &flag);
245 }
246 }
247 fn_NetSim_Packet_FreePacket(packet);
248 }
249 return 0;
250}
251
252static void notify_src(NETSIM_ID tx,
253 bool isError)
254{
255 double acktime = 0;
256 NetSim_EVENTDETAILS pevent;
257 memset(&pevent, 0, sizeof pevent);
258 pevent.dEventTime = pstruEventDetails->dEventTime + acktime;
259 pevent.nDeviceId = tx;
260 pevent.nDeviceType = DEVICE_TYPE(tx);
261 pevent.nEventType = MAC_OUT_EVENT;
262 pevent.nInterfaceId = 1;
263 pevent.nProtocolId = MAC_PROTOCOL_ALOHA;
264 pevent.nSubEventType = isError ? ALOHA_PACKET_ERROR : ALOHA_PACKET_SUCCESS;
265 fnpAddEvent(&pevent);
266}
267
268void fn_NetSim_Aloha_PhyIn()
269{
270 PALOHA_PHY_VAR phy = ALOHA_CURR_PHY;
271 double ber;
272 NetSim_PACKET* packet = pstruEventDetails->pPacket;
273 NETSIM_ID tx = packet->nTransmitterId;
274 NETSIM_ID rx = packet->nReceiverId;
275
276 if (isBroadcastPacket(packet))
277 {
278 if ((tx == 1 && rx == 2) ||
279 rx == 1)
280 update_interference_power(tx, rx,
281 packet->pstruPhyData->dArrivalTime, false);
282 }
283 else
284 {
285 update_interference_power(tx, rx,
286 packet->pstruPhyData->dArrivalTime, false);
287 }
288
289 remove_packet_from_txList(packet);
290
291 set_radio_state(packet->nTransmitterId, 1, RX_ON_IDLE);
292 set_radio_state(packet->nReceiverId, 1, RX_ON_IDLE);
293
294
295 if(pstruEventDetails->pPacket->nPacketStatus == PacketStatus_Collided)
296 goto RET_PHYIN;
297
298 ber = UWAN_Calculate_ber(tx, rx,
299 get_aloha_propagation_info(tx, rx),
300 get_received_power(tx, rx, packet->pstruPhyData->dArrivalTime),
301 phy->modulation,
302 phy->data_rate * 1000,
303 phy->bandwidth * 1000);
304
305 packet->nPacketStatus = fn_NetSim_Packet_DecideError(ber, packet->pstruPhyData->dPacketSize);
306
307 if(packet->nPacketStatus == PacketStatus_Error)
308 goto RET_PHYIN;
309
310 pstruEventDetails->nEventType = MAC_IN_EVENT;
311 fnpAddEvent(pstruEventDetails);
312
313RET_PHYIN:
314 fn_NetSim_WritePacketTrace(packet);
315 fn_NetSim_Metrics_Add(packet);
316 LinkPacketLog();
317 if (pstruEventDetails->pPacket->nPacketStatus != PacketStatus_NoError)
318 {
319 if(!check_packet_in_txList(tx))
320 notify_src(tx, true);
321 fn_NetSim_Packet_FreePacket(packet);
322 }
323 else
324 {
325 if (!check_packet_in_txList(tx))
326 notify_src(tx, false);
327 }
328}