NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Aloha.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 "../UWAN/UWAN.h"
17#include "AdvancedPlots.h"
18#include "../BatteryModel/BatteryModel.h"
19#pragma comment(lib,"AdvancedPlots.lib")
20
21void send_to_phy();
22void wait_for_random_time();
23int fn_NetSim_Aloha_handleMobility(NETSIM_ID d);
24
25PHY_MODULATION getModulationFromString(char* val)
26{
27 if(!_stricmp(val,"QPSK"))
28 return Modulation_QPSK;
29 else if(!_stricmp(val,"BPSK"))
30 return Modulation_BPSK;
31 else if (!_stricmp(val, "FSK"))
32 return Modulation_FSK;
33 else if(!_stricmp(val,"16QAM"))
34 return Modulation_16_QAM;
35 else if(!_stricmp(val,"64QAM"))
36 return Modulation_64_QAM;
37 else if (!_stricmp(val, "256QAM"))
38 return Modulation_256_QAM;
39 else
40 {
41 fnNetSimError("Unknown modulation %s. Assigning QPSK\n",val);
42 return Modulation_QPSK;
43 }
44}
45
46_declspec(dllexport) int fn_NetSim_Aloha_Configure(void** var)
47{
48 void* xmlNetSimNode;
49 NETSIM_ID nDeviceId;
50 NETSIM_ID nInterfaceId;
51 LAYER_TYPE nLayerType;
52 xmlNetSimNode = var[2];
53 nDeviceId = *((NETSIM_ID*)var[3]);
54 nInterfaceId = *((NETSIM_ID*)var[4]);
55 nLayerType = *((LAYER_TYPE*)var[5]);
56 switch(nLayerType)
57 {
58 case PHYSICAL_LAYER:
59 {
60 PALOHA_PHY_VAR phy = ALOHA_PHY(nDeviceId,nInterfaceId);
61 char* szVal;
62
63 if(!phy)
64 {
65 phy = (PALOHA_PHY_VAR)calloc(1,sizeof *phy);
66 DEVICE_PHYVAR(nDeviceId,nInterfaceId) = phy;
67 }
68
69 phy->frequency = fn_NetSim_Config_read_Frequency(xmlNetSimNode, "FREQUENCY",
70 ALOHA_FREQUENCY_DEFAULT, "MHZ");
71
72 phy->bandwidth = fn_NetSim_Config_read_Frequency(xmlNetSimNode, "BANDWIDTH",
73 ALOHA_BANDWIDTH_DEFAULT, "MHz");
74
75 phy->tx_power = fn_NetSim_Config_read_txPower(xmlNetSimNode, "TX_POWER",
76 ALOHA_TX_POWER_DEFAULT, "MW");
77
78 getXmlVar(&phy->rx_sensitivity,RECEIVER_SENSITIVITY_DBM,xmlNetSimNode,1, _DOUBLE,ALOHA);
79 getXmlVar(&szVal,MODULATION,xmlNetSimNode,1,_STRING,ALOHA);
80 phy->modulation = getModulationFromString(szVal);
81 free(szVal);
82 phy->data_rate = fn_NetSim_Config_read_dataRate(xmlNetSimNode, "DATA_RATE",
83 ALOHA_DATA_RATE_DEFAULT, "MBPS");
84
85 void* pXmlChild = fn_NetSim_xmlGetChildElement(xmlNetSimNode, "POWER", 0);
86 if (pXmlChild)
87 {
88 fn_NetSim_Configure_UWAN_POWER(xmlNetSimNode, nDeviceId,nInterfaceId);
89 pXmlChild = NULL;
90 }
91 }
92 break;
93 case MAC_LAYER:
94 {
95 PALOHA_MAC_VAR mac = ALOHA_MAC(nDeviceId,nInterfaceId);
96 char* szVal;
97
98 if(!mac)
99 {
100 mac = (PALOHA_MAC_VAR)calloc(1,sizeof *mac);
101 DEVICE_MACVAR(nDeviceId,nInterfaceId) = mac;
102 }
103
104 //Get the MAC address
105 szVal = fn_NetSim_xmlConfig_GetVal(xmlNetSimNode,"MAC_ADDRESS",1);
106 if(szVal)
107 {
108 NETWORK->ppstruDeviceList[nDeviceId-1]->ppstruInterfaceList[nInterfaceId-1]->pstruMACLayer->szMacAddress = STR_TO_MAC(szVal);
109 free(szVal);
110 }
111
112 getXmlVar(&mac->slotlength,SLOT_LENGTH,xmlNetSimNode,0, _DOUBLE,ALOHA);
113
114 getXmlVar(&mac->retryLimit, RETRY_LIMIT, xmlNetSimNode, 1, _UINT, ALOHA);
115
116 getXmlVar(&mac->isMACBuffer, IS_MAC_BUFFER, xmlNetSimNode, 1, _BOOL, ALOHA);
117 }
118 break;
119 default:
120 fnNetSimError("%d layer is not implemented for Aloha protocol\n",nLayerType);
121 break;
122 }
123 return 0;
124}
125
126_declspec (dllexport) int fn_NetSim_Aloha_Init()
127{
128 NETSIM_ID i;
129 init_UWAN_log();
130 init_linkpacketlog();
131 fn_NetSim_Aloha_CalulateReceivedPower();
132 fnMobilityRegisterCallBackFunction(fn_NetSim_Aloha_handleMobility);
133 for (i = 0; i < NETWORK->nDeviceCount; i++)
134 {
135 PALOHA_MAC_VAR mac = ALOHA_MAC(i + 1, 1);
136 PALOHA_PHY_VAR phy = ALOHA_PHY(i + 1, 1);
137 if (mac->slotlength)
138 {
139 mac->isSlotted = true;
140 //double t = get_slot_length(phy);
141 //mac->slotlength = max(t, mac->slotlength);
142 }
143 else
144 {
145 mac->slotlength = get_slot_length(phy);
146 }
147
148
149 phy->transmitter_status = RX_ON_IDLE;
150 if(phy->battery)
151 battery_set_mode(phy->battery, RX_ON_IDLE, ldEventTime);
152 phy->dInterferencePower = MW_TO_DBM(0);
153 }
154 return 0;
155}
156
157_declspec (dllexport) int fn_NetSim_Aloha_Run()
158{
159 switch(pstruEventDetails->nEventType)
160 {
161 case MAC_OUT_EVENT:
162 fn_NetSim_Aloha_Handle_MacOut();
163 break;
164 case MAC_IN_EVENT:
165 pstruEventDetails->nEventType = NETWORK_IN_EVENT;
166 fnpAddEvent(pstruEventDetails);
167 break;
168 case PHYSICAL_OUT_EVENT:
169 fn_NetSim_Aloha_PhyOut();
170 break;
171 case PHYSICAL_IN_EVENT:
172 fn_NetSim_Aloha_PhyIn();
173 break;
174 default:
175 fnNetSimError("Unknown event type %d for aloha protocol in %s",pstruEventDetails->nEventType,__FUNCTION__);
176 break;
177 }
178 return 0;
179}
180
181_declspec(dllexport) int fn_NetSim_Aloha_Finish()
182{
183 close_UWAN_log();
184 LinkPacketLog_close();
185 fn_NetSim_Aloha_FreePropagationInfo();
186 return 0;
187}
188
189#include "Aloha_enum.h"
190_declspec (dllexport) char* fn_NetSim_Aloha_Trace(int nSubEvent)
191{
192 return GetStringALOHA_Subevent(nSubEvent);
193}
194_declspec(dllexport) int fn_NetSim_Aloha_FreePacket(NetSim_PACKET* pstruPacket)
195{
196 pstruPacket;
197 return 0;
198}
199
200/**
201 This function is called by NetworkStack.dll, to copy the aloha protocol
202 details from source packet to destination.
203*/
204_declspec(dllexport) int fn_NetSim_Aloha_CopyPacket(NetSim_PACKET* pstruDestPacket,NetSim_PACKET* pstruSrcPacket)
205{
206 pstruDestPacket;
207 pstruSrcPacket;
208 return 0;
209}
210
211/**
212This function write the Metrics
213*/
214_declspec(dllexport) int fn_NetSim_Aloha_Metrics(PMETRICSWRITER metricsWriter)
215{
216 metricsWriter;
217 battery_metrics(metricsWriter);
218 return 0;
219}
220
221/**
222This function will return the string to write packet trace heading.
223*/
224_declspec(dllexport) char* fn_NetSim_Aloha_ConfigPacketTrace()
225{
226 return "";
227}
228
229/**
230 This function will return the string to write packet trace.
231*/
232_declspec(dllexport) char* fn_NetSim_Aloha_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
233{
234 ppszTrace;
235 pstruPacket;
236 return "";
237}
238
239int fn_NetSim_Configure_UWAN_POWER(void* xmlNetSimNode, int nDeviceId, int nInterfaceId)
240{
241 char* szVal;
242 void* pXmlChild;
243 pXmlChild = fn_NetSim_xmlGetChildElement(xmlNetSimNode, "POWER", 0);
244 szVal = fn_NetSim_xmlConfig_GetVal(pXmlChild, "SOURCE", 1);
245 if (szVal)
246 {
247 if (_stricmp(szVal, "Battery"))
248 {
249 free(szVal);
250 return -1;
251 }
252 free(szVal);
253 }
254 PALOHA_PHY_VAR phy = ALOHA_PHY(nDeviceId, nInterfaceId);
255 double dRechargingCurrent = 0;
256 double dVoltage = 0;
257 double dInitialEnergy = 0;
258 double dCurrent;
259 getXmlVar(&dRechargingCurrent, RECHARGING_CURRENT_MA, pXmlChild, 0, _DOUBLE, BATTERY);
260 getXmlVar(&dVoltage, VOLTAGE_V, pXmlChild, 0, _DOUBLE, BATTERY);
261 getXmlVar(&dInitialEnergy, INITIAL_ENERGY, pXmlChild, 0, _DOUBLE, BATTERY);
262 dInitialEnergy *= dVoltage * 3600;
263 phy->battery = battery_init_new(nDeviceId,
264 0,
265 dInitialEnergy,
266 dVoltage,
267 dRechargingCurrent);
268
269 getXmlVar(&dCurrent, TRANSMITTING_CURRENT_MA, pXmlChild, 0, _DOUBLE, BATTERY);
270 battery_add_new_mode(phy->battery, TRX_ON_BUSY, dCurrent, "Transmitting energy(mJ)");
271
272 getXmlVar(&dCurrent, RECEIVING_CURRENT_MA, pXmlChild, 0, _DOUBLE, BATTERY);
273 battery_add_new_mode(phy->battery, RX_ON_BUSY, dCurrent, "Receiving energy(mJ)");
274
275 getXmlVar(&dCurrent, IDLEMODE_CURRENT_MA, pXmlChild, 0, _DOUBLE, BATTERY);
276 battery_add_new_mode(phy->battery, RX_ON_IDLE, dCurrent, "Idle energy(mJ)");
277
278 return 1;
279}
280static bool isChangeRadioIsPermitted(PALOHA_PHY_VAR phy, PHY_TX_STATUS newState)
281{
282 PHY_TX_STATUS oldState = phy->transmitter_status;
283 if (oldState == RX_ON_IDLE)
284 return true;
285 if (oldState == RX_ON_BUSY && newState != RX_ON_IDLE)
286 return false;
287 if (oldState == TRX_ON_BUSY && newState != RX_ON_IDLE)
288 return false;
289 return true;
290}
291
292bool set_radio_state(NETSIM_ID DeviceId, NETSIM_ID InterfaceId, PHY_TX_STATUS state)
293{
294 PALOHA_PHY_VAR phy = ALOHA_PHY(DeviceId, InterfaceId);
295 if (phy == NULL) { return false; }
296 ptrBATTERY battery = phy->battery;
297
298 if (phy->transmitter_status == RX_OFF)
299 return false;
300
301 if (!isChangeRadioIsPermitted(phy, state))
302 return false;
303 bool isChange = true;
304 if (battery)
305 {
306 isChange = battery_set_mode(battery, state, ldEventTime);
307 }
308 if (isChange)
309 {
310 phy->transmitter_status = state;
311 }
312 else
313 {
314 phy->transmitter_status = RX_OFF;
315 battery_set_mode(battery, state, ldEventTime);
316 }
317 return isChange;
318}