## 4 Algorithm Design
### Airtime Quantum Calculation  
Based on the analysis in Sections 3.2 and 3.3, the airtime for TCP and UDP flows is calculated using flow type, packet size, wireless rate, and other relevant information. However, our algorithm differentiates between upload and download TCP flows in the airtime calculation method. This difference arises from the relationship between the number of TCP data packets and ACKs, as well as the position of queuing and scheduling algorithms in end-to-end transmission. As discussed in Section 3.3, TCP flows employ delayed acknowledgment, where the receiver sends one ACK for every \(d\) TCP data packets received.  
For download TCP data, the access point sends TCP data, and the stations returns TCP ACKs. When sending data, the AP pre-calculates the airtime required for the return of TCP ACKs and uses this as a basis for airtime fairness scheduling, ensuring fairness for download TCP data, as illustrated in Figure 7. The airtime for transmitting a data frame in this context includes the airtime for one data packet plus the airtime for \(1/d\) TCP ACKs, calculated according to the method and formula in Equation (7).  
For upload TCP data, the stations transmit TCP data, and the access point returns TCP ACKs. By the time the AP sends these ACKs, the TCP data transmission has already been completed. At this stage, the AP adjusts the sending order of TCP ACKs for each TCP flow using an airtime fairness scheduling algorithm to ensure fairness for upload TCP data, as illustrated in Figure 8. The airtime for transmitting TCP ACKs includes the airtime for \(d\) data packets and the airtime for 1 TCP ACK, calculated as follows:  
\[T_{tcp}=\frac{d\times s_{data}+s_{ack}}{r}+(d+1)\times t_{ind} \tag{9}\]  
In conclusion, we can calculate the single transmission airtime for node \(i\), denoted as \(t_{i}\). Next, we analyze the relationship between \(t_{i}\), the number of transmissions \(n_{i}\), the quota \(quantum_{i}\), and the total airtime \(A_{i}\). Our goal is to determine \(n_{i}\) based on \(t_{i}\) for each node, ensuring that the total airtime for each node is equal, i.e.,  
\[\forall(i,j)\in F^{2},A_{i}=A_{j} \tag{10}\]  
Given that the total airtime of different nodes equals the product of the number of frames sent and the average airtime per frame, we have:  
\[A_{i}=t_{i}\times n_{i} \tag{11}\]  
To achieve airtime fairness between different nodes \(i\) and \(j\), the following condition must be met:  
\[\frac{n_{i}}{n_{j}}=\frac{t_{i}}{t_{i}} \tag{12}\]  
By combining Equation (12) with Equation (8) from the queue scheduling algorithm in Section 4.2, we can establish the relationship between the airtime of different nodes and their allocated resource quotas:  
\[\frac{quantum_{i}}{quantum_{j}}=\frac{t_{i}}{t_{j}} \tag{13}\]  
Figure 8: Control logic for airtime fairness in upload scenario.  
Figure 7: Control logic for airtime fairness in download scenario.  
This analysis demonstrates that by accurately calculating the average airtime of each flow's data frames, we can allocate the opportunity to access the wireless channel more effectively, thereby achieving more precise airtime fairness and improving overall throughput. The resource allocation algorithm in this paper is provided in Algorithm 2.  
```
0:\(flow\ features,wifi\ features\)
0:\(quantum\)
1:functionget_quantum(){
2:for(i = 0; i < N; i++) do
3: backoff = 0;
4:for(j = 0; j < R[i]; j++) do
5: backoff += (w[i] - 1)/2\(\times\)(1 - p)\(\times\)pow(p,j);
6:endfor
7: ind = backoff + 2\(\times\)SIFS + 2\(\times\)Slot + s_w_ack/r_min;
8:if'UDP' == get_type(flow[i]) then
9: airtime[i] = ind + s[i]/r[i];
10:endif
11:elseif'TCP' == get_type(flow[i]) then
12:if'UPLOAD' == get_direction(flow[i]) then
13: airtime[i] = (d[i]\(\times\)s[i] + s_tcp_ack[i])/r[i] + (1 + d[i])\(\times\)ind
14:endif
15:else
16: airtime[i] = (s[i] + s_tcp_ack[i]/d[i])/r[i] + (1 + 1/d[i])\(\times\)ind
17:endelse
18:endelse
19:return airtime;
20:endfunction
```  
**Algorithm 2** Quantum calculation algorithm of airtime.