int (*compar)(const void *, const void *) = wpa_scan_result_compar; qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),compar); // qsort函数介绍 static int wpa_scan_result_compar(const void *a, const void *b) //这个函数用于scan result结果降序排序 { #define MIN(a,b) a < b ? a : b struct wpa_scan_res **_wa = (void *) a; //scan result的二级指针 struct wpa_scan_res **_wb = (void *) b; struct wpa_scan_res *wa = *_wa; //scan result的一级指针 struct wpa_scan_res *wb = *_wb; int wpa_a, wpa_b; int snr_a, snr_b, snr_a_full, snr_b_full; /* WPA/WPA2 support preferred */ wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL || wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL; wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL || wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL; if (wpa_b && !wpa_a) //这里先比较是否有WPA_IE_VENDOR_TYPE和WLAN_EID_RSN这两个IE,谁有就谁排在前面,如果都没有,就不将他们看作排序因子 return 1; //返回正数表示升序,反会负表示逆序,返回0表示相等,所以正数时表示a比b小,负数时表示a比b大 if (!wpa_b && wpa_a) return -1; //这里返回负数,表示将a放在b的后面 /* privacy support preferred */ if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 && //如果有标志位IEEE80211_CAP_PRIVACY的,就排在前面 (wb->caps & IEEE80211_CAP_PRIVACY)) return 1; if ((wa->caps & IEEE80211_CAP_PRIVACY) && (wb->caps & IEEE80211_CAP_PRIVACY) == 0) return -1; if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) { snr_a_full = wa->snr; snr_a = MIN(wa->snr, GREAT_SNR); //如果信噪比大于30,那么信噪比snr_a的值就赋值为30,所以当wa->snr大于30的时候,snr_a_full是真实值,snr_a是阈值 snr_b_full = wb->snr; snr_b = MIN(wb->snr, GREAT_SNR); //通过log可知,其实排序的时候没有用到信噪比这个因子 } else { /* Level is not in dBm, so we can't calculate * SNR. Just use raw level (units unknown). */ snr_a = snr_a_full = wa->level; //level表示信号强度,一般用百分比表示,和信噪比的算法不一样 snr_b = snr_b_full = wb->level; } /* if SNR is close, decide by max rate or frequency band */ if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) || //如果两者的level相差在5以内,就认为两者是相等的 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) { //如果两者的信号质量相差在10以内,就认为两者是相等的 if (wa->est_throughput != wb->est_throughput) //Enrollment over Secure Transport, 我们没有用这种协议,所以两者都是0, 非排序因子 return wb->est_throughput - wa->est_throughput; if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq)) //如果两者都是5G,那就不用比较了,如果其中一个是5G,另外一个不是,那么5G的排在前面 return IS_5GHZ(wa->freq) ? -1 : 1; } /* all things being equal, use SNR; if SNRs are * identical, use quality values since some drivers may only report * that value and leave the signal level zero */ if (snr_b_full == snr_a_full) //如果运行到这里还没有return,就表示前面的那些参数都是相等的,所以这里看看两者的真是信噪比是否相等 return wb->qual - wa->qual; //如果相等,那就比信号质量,信号质量高的排在前面 return snr_b_full - snr_a_full; //如果不相等,那就比真实信噪比,信噪比高的排在前面 #undef MIN }
作者:lee244868149 发表于2017/3/25 18:15:59 原文链接
阅读:38 评论:0 查看评论