NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Generic_RateAdaptation.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* 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 "IEEE802_11.h"
26#include "IEEE802_11_Phy.h"
27
28#define RATE_UP_INDEX 19
29#define RATE_DOWN_INDEX 3
30typedef struct
31{
32 unsigned int maxPhyindex;
33 unsigned int minPhyindex;
34 unsigned int* currentPhyindex;
35 int* currentDroppedCount;
36 int* currentReceivedCount;
37}GENERIC_RATE_ADAPTATION,*PGENERIC_RATE_ADAPTATION;
38
39static PGENERIC_RATE_ADAPTATION get_rate_adaptation_data(NETSIM_ID devid,NETSIM_ID ifid)
40{
41 return (PGENERIC_RATE_ADAPTATION)(IEEE802_11_PHY(devid,ifid)->rateAdaptationData);
42}
43
44static void set_rate_adaptation_data(NETSIM_ID devid,NETSIM_ID ifid,PGENERIC_RATE_ADAPTATION rate)
45{
46 IEEE802_11_PHY(devid,ifid)->rateAdaptationData = rate;
47}
48
49void free_rate_adaptation_data(PIEEE802_11_PHY_VAR phy)
50{
51 PGENERIC_RATE_ADAPTATION rate = (PGENERIC_RATE_ADAPTATION)phy->rateAdaptationData;
52 if(rate)
53 {
54 free(rate->currentDroppedCount);
55 free(rate->currentPhyindex);
56 free(rate->currentReceivedCount);
57 free(rate);
58 phy->rateAdaptationData=NULL;
59 }
60}
61
62unsigned int get_max_phy_index(NETSIM_ID devid,NETSIM_ID ifid)
63{
64 PIEEE802_11_PHY_VAR phy = IEEE802_11_PHY(devid,ifid);
65 switch(phy->PhyProtocol)
66 {
67 case IEEE_802_11a:
68 case IEEE_802_11g:
69 case IEEE_802_11p:
70 return get_ofdm_phy_max_index();
71 case IEEE_802_11b:
72 return get_dsss_phy_max_index();
73 case IEEE_802_11n:
74 case IEEE_802_11ac:
75 return get_ht_phy_max_index(phy->PhyProtocol,phy->nGuardInterval);
76 case IEEE_802_11ax:
77 return get_he_phy_max_index(phy->PhyProtocol,phy->nGuardInterval);
78 default:
79 fnNetSimError("Unknown phy protocol %d in %s.\n",phy->PhyProtocol,__FUNCTION__);
80 return 0;
81 }
82}
83
84static unsigned int get_min_phy_index(NETSIM_ID devid,NETSIM_ID ifid)
85{
86 PIEEE802_11_PHY_VAR phy = IEEE802_11_PHY(devid,ifid);
87 switch(phy->PhyProtocol)
88 {
89 case IEEE_802_11a:
90 case IEEE_802_11g:
91 case IEEE_802_11p:
92 return get_ofdm_phy_min_index();
93 case IEEE_802_11b:
94 return get_dsss_phy_min_index();
95 case IEEE_802_11n:
96 case IEEE_802_11ac:
97 return get_ht_phy_min_index(phy->PhyProtocol,phy->nGuardInterval);
98 case IEEE_802_11ax:
99 return get_he_phy_min_index(phy->PhyProtocol,phy->nGuardInterval);
100 default:
101 fnNetSimError("Unknown phy protocol %d in %s.\n",phy->PhyProtocol,__FUNCTION__);
102 return 0;
103 }
104}
105
106void Generic_Rate_adaptation_init(NETSIM_ID nDevId,NETSIM_ID nifid)
107{
108 NETSIM_ID i;
109 PGENERIC_RATE_ADAPTATION rate=(PGENERIC_RATE_ADAPTATION)calloc(1,sizeof* rate);
110 set_rate_adaptation_data(nDevId,nifid,rate);
111
112 rate->currentDroppedCount=(int*)calloc(NETWORK->nDeviceCount+1,sizeof* rate->currentDroppedCount);
113 rate->currentPhyindex=(unsigned int*)calloc(NETWORK->nDeviceCount+1,sizeof* rate->currentPhyindex);
114 rate->currentReceivedCount=(int*)calloc(NETWORK->nDeviceCount+1,sizeof* rate->currentReceivedCount);
115
116 rate->maxPhyindex = get_max_phy_index(nDevId,nifid);
117 rate->minPhyindex = get_min_phy_index(nDevId,nifid);
118 for(i=0;i<=NETWORK->nDeviceCount;i++)
119 rate->currentPhyindex[i]=rate->maxPhyindex;
120}
121
122void packet_drop_notify(NETSIM_ID devid,NETSIM_ID ifid,NETSIM_ID rcvid)
123{
124 PGENERIC_RATE_ADAPTATION rate = get_rate_adaptation_data(devid,ifid);
125
126 rate->currentDroppedCount[rcvid]++;
127 //printf("\nDrop count -- %d\n",rate->currentDroppedCount[rcvid]);
128 if(rate->currentDroppedCount[rcvid] > RATE_DOWN_INDEX)
129 {
130 if(rate->currentPhyindex[rcvid] > rate->minPhyindex)
131 rate->currentPhyindex[rcvid]--;
132 rate->currentDroppedCount[rcvid]=0;
133 //printf("\nIndex for %d to %d is %d\n",devid,rcvid,rate->currentPhyindex[rcvid]);
134 }
135 rate->currentReceivedCount[rcvid] = 0;
136}
137
138void packet_recv_notify(NETSIM_ID devid,NETSIM_ID ifid,NETSIM_ID rcvid)
139{
140 PGENERIC_RATE_ADAPTATION rate = get_rate_adaptation_data(devid,ifid);
141 rate->currentReceivedCount[rcvid]++;
142 //printf("\nReceive count -- %d\n",rate->currentReceivedCount[rcvid]);
143 if(rate->currentReceivedCount[rcvid] > RATE_UP_INDEX)
144 {
145 if(rate->currentPhyindex[rcvid] < rate->maxPhyindex)
146 rate->currentPhyindex[rcvid]++;
147 rate->currentReceivedCount[rcvid]=0;
148 //printf("\nIndex for %d to %d is %d\n",devid,rcvid,rate->currentPhyindex[rcvid]);
149 }
150 rate->currentDroppedCount[rcvid] = 0;
151}
152
153unsigned int get_rate_index(NETSIM_ID devid,NETSIM_ID ifid,NETSIM_ID rcvid)
154{
155 return get_rate_adaptation_data(devid,ifid)->currentPhyindex[rcvid];
156}