NetSim Source Code Help
Loading...
Searching...
No Matches
Aloha_Phy.c
Go to the documentation of this file.
1/************************************************************************************
2 * Copyright (C) 2020 *
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
19typedef struct stru_packetList
20{
24
26
28{
30 {
32 txPacketList.tail = packet;
33 }
34 else
35 {
36 txPacketList.head = packet;
37 txPacketList.tail = packet;
38 }
39}
40
42{
44 NetSim_PACKET* p = NULL;
45 while (t)
46 {
47 if (t == packet)
48 {
49 if (p)
50 {
52 if (!t->pstruNextPacket)
54 }
55 else
56 {
58 if (!t->pstruNextPacket)
59 txPacketList.tail = NULL;
60 }
61 break;
62 }
63 t = t->pstruNextPacket;
64 }
65 packet->pstruNextPacket = NULL;
66}
67
69{
71 while (p)
72 {
74
76 double interference = substract_power_in_dbm(phy->dInterferencePower, power);
77
78 if ( interference > phy->rx_sensitivity)
80
81 p = p->pstruNextPacket;
82 }
83}
84
86 NETSIM_ID rx,
87 double time,
88 bool isAdd)
89{
90 rx;
91 NETSIM_ID i;
93 for (i = 0; i < NETWORK->nDeviceCount; i++)
94 {
95 if (i + 1 == tx)
96 continue;
97
98 phy = ALOHA_PHY(i + 1, 1);
99 double p = phy->dInterferencePower;
100 if (isAdd)
102 get_received_power(tx, i + 1, time));
103 else
105 get_received_power(tx, i + 1, time));
106 }
107}
108
110{
111 double size=fnGetPacketSize(packet);
112 NETSIM_ID source=packet->nTransmitterId;
113 PALOHA_PHY_VAR phy = ALOHA_PHY(source,1);
114 double dDataRate=phy->data_rate;
115 double dTxTime= size*8.0/dDataRate;
116 return dTxTime;
117}
118
120{
121 NETSIM_ID source = packet->nTransmitterId;
122 NETSIM_ID destination = packet->nReceiverId;
123 double dPropagationDelay = UWAN_calculate_propagation_delay(source, 1,
124 destination, 1,
125 get_aloha_propagation_info(source, destination));
126 return dPropagationDelay;
127}
128
129static void transmit_packet(NetSim_PACKET* packet, bool* flag)
130{
131 PALOHA_PHY_VAR phy = ALOHA_PHY(packet->nReceiverId,1);
132 PALOHA_PHY_VAR phyt = ALOHA_PHY(packet->nTransmitterId,1);
133 double power = get_received_power(packet->nTransmitterId, packet->nReceiverId,
134 packet->pstruPhyData->dArrivalTime);
135
137
138 if(power < phy->rx_sensitivity)
140
141 //Add physical in event
151
152 add_packet_to_txList(packet);
153 if (!*flag)
155 packet->nReceiverId,
156 packet->pstruPhyData->dArrivalTime,
157 true);
158 *flag = true;
160}
161
163{
164 double dTxTime;
165 double dPropagationDelay;
172
173 bool flag = false;
174 if(packet->nReceiverId)
175 {
176 dTxTime = calulate_transmission_time(packet);
177 dPropagationDelay = calculate_propagation_delay(packet);
178 packet->pstruPhyData->dStartTime = packet->pstruPhyData->dArrivalTime + dTxTime;
179 packet->pstruPhyData->dEndTime = packet->pstruPhyData->dStartTime + dPropagationDelay;
180 transmit_packet(packet, &flag);
181 }
182 else
183 {
184 //Broadcast packet
185 NETSIM_ID loop;
186 for(loop=0;loop<NETWORK->nDeviceCount;loop++)
187 {
188 if(loop+1!=packet->nTransmitterId)
189 {
191 //Add physical in event
192 p->nReceiverId=loop+1;
193 dTxTime = calulate_transmission_time(p);
194 dPropagationDelay = calculate_propagation_delay(p);
196 p->pstruPhyData->dEndTime = p->pstruPhyData->dStartTime + dPropagationDelay;
197 transmit_packet(p, &flag);
198 }
199 }
201 }
202 return 0;
203}
204
205static void notify_src(NETSIM_ID tx,
206 bool isError)
207{
208 double acktime = 0;
209 NetSim_EVENTDETAILS pevent;
210 memset(&pevent, 0, sizeof pevent);
211 pevent.dEventTime = pstruEventDetails->dEventTime + acktime;
212 pevent.nDeviceId = tx;
213 pevent.nDeviceType = DEVICE_TYPE(tx);
214 pevent.nEventType = MAC_OUT_EVENT;
215 pevent.nInterfaceId = 1;
217 pevent.nSubEventType = isError ? ALOHA_PACKET_ERROR : ALOHA_PACKET_SUCCESS;
218 fnpAddEvent(&pevent);
219}
220
222{
224 double ber;
226 NETSIM_ID tx = packet->nTransmitterId;
227 NETSIM_ID rx = packet->nReceiverId;
228
229 if (isBroadcastPacket(packet))
230 {
231 if ((tx == 1 && rx == 2) ||
232 rx == 1)
234 packet->pstruPhyData->dArrivalTime, false);
235 }
236 else
237 {
239 packet->pstruPhyData->dArrivalTime, false);
240 }
241
243
245 goto RET_PHYIN;
246
247 ber = UWAN_Calculate_ber(tx, rx,
250 phy->modulation,
251 phy->data_rate * 1000,
252 phy->bandwidth * 1000);
253
255
256 if(packet->nPacketStatus == PacketStatus_Error)
257 goto RET_PHYIN;
258
261
262RET_PHYIN:
264 fn_NetSim_Metrics_Add(packet);
265
267 {
268 notify_src(tx, true);
270 }
271 else
272 {
273 notify_src(tx, false);
274 }
275}
PPROPAGATION_INFO get_aloha_propagation_info(NETSIM_ID TX, NETSIM_ID RX)
double get_received_power(NETSIM_ID TX, NETSIM_ID RX, double time)
#define ALOHA_CURR_PHY
Definition: Aloha.h:74
#define ALOHA_PHY_OVERHEAD
Definition: Aloha.h:38
#define ALOHA_PHY(devid, ifid)
Definition: Aloha.h:73
PACKETLIST txPacketList
Definition: Aloha_Phy.c:25
static void update_interference_power(NETSIM_ID tx, NETSIM_ID rx, double time, bool isAdd)
Definition: Aloha_Phy.c:85
int fn_NetSim_Aloha_PhyOut()
Definition: Aloha_Phy.c:162
static void transmit_packet(NetSim_PACKET *packet, bool *flag)
Definition: Aloha_Phy.c:129
static double calculate_propagation_delay(NetSim_PACKET *packet)
Definition: Aloha_Phy.c:119
struct stru_packetList PACKETLIST
static void add_packet_to_txList(NetSim_PACKET *packet)
Definition: Aloha_Phy.c:27
struct stru_packetList * ptrPACKETLIST
static void remove_packet_from_txList(NetSim_PACKET *packet)
Definition: Aloha_Phy.c:41
static void notify_src(NETSIM_ID tx, bool isError)
Definition: Aloha_Phy.c:205
static double calulate_transmission_time(NetSim_PACKET *packet)
Definition: Aloha_Phy.c:109
void fn_NetSim_Aloha_PhyIn()
Definition: Aloha_Phy.c:221
static void mark_packet_for_collision()
Definition: Aloha_Phy.c:68
unsigned int NETSIM_ID
Definition: Animation.h:45
double add_power_in_dbm(double p1_dbm, double p2_dbm)
double substract_power_in_dbm(double p1_dbm, double p2_dbm)
double fnGetPacketSize(NetSim_PACKET *pstruData)
bool isBroadcastPacket(NetSim_PACKET *packet)
PACKET_STATUS fn_NetSim_Packet_DecideError(double dBER, long double dPacketSize)
@ PacketStatus_Error
Definition: Packet.h:102
@ PacketStatus_NoError
Definition: Packet.h:101
@ PacketStatus_Collided
Definition: Packet.h:103
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
@ MAC_PROTOCOL_ALOHA
Definition: Stack.h:226
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
@ MAC_OUT_EVENT
Definition: Stack.h:106
@ MAC_IN_EVENT
Definition: Stack.h:107
@ PHYSICAL_IN_EVENT
Definition: Stack.h:105
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define fn_NetSim_Metrics_Add(packet)
Definition: Stack.h:998
double UWAN_Calculate_ber(NETSIM_ID tx, NETSIM_ID rx, PPROPAGATION_INFO info, double rxPower, PHY_MODULATION modulation, double dataRate, double bandwidth)
Definition: UWAN.c:184
double UWAN_calculate_propagation_delay(NETSIM_ID tx, NETSIM_ID txi, NETSIM_ID rx, NETSIM_ID rxi, PPROPAGATION_INFO info)
Definition: UWAN.c:117
@ TRX_ON_BUSY
Definition: Wireless.h:77
#define fn_NetSim_Packet_CopyPacket(pstruPacket)
Definition: main.h:182
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fn_NetSim_WritePacketTrace(pstruPacket)
Definition: main.h:188
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
NETSIM_ID nInterfaceId
Definition: Stack.h:751
PACKET_STATUS nPacketStatus
Definition: Packet.h:272
struct stru_NetSim_Packet_PhyLayer * pstruPhyData
Definition: Packet.h:277
NETSIM_ID nReceiverId
Definition: Packet.h:266
struct stru_NetSim_Packet * pstruNextPacket
Definition: Packet.h:278
NETSIM_ID nTransmitterId
Definition: Packet.h:265
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276
double dInterferencePower
Definition: Aloha.h:69
PHY_MODULATION modulation
Definition: Aloha.h:59
PHY_TX_STATUS transmitter_status
Definition: Aloha.h:68
double data_rate
Definition: Aloha.h:64
double bandwidth
Definition: Aloha.h:61
double rx_sensitivity
Definition: Aloha.h:63
NetSim_PACKET * tail
Definition: Aloha_Phy.c:22
NetSim_PACKET * head
Definition: Aloha_Phy.c:21