Skip to content

Commit

Permalink
Prefer AP interface index from NL80211_CMD_CH_SWITCH_NOTIFY
Browse files Browse the repository at this point in the history
  • Loading branch information
zeertzjq committed Sep 6, 2024
1 parent 5d3a28c commit 2f16e51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Alternatively, build the app on a Linux system with JDK 17 (or above) and Androi

## Usage

Many Qualcomm chips' spectral scan feature can only cover a 40 MHz range centered at the frequency of the current Wi-Fi channel. Therefore, it's better to run this app with hotspot enabled and "Turn off hotspot automatically" disabled. The app will then periodically switch the channel of the hotspot to cover different frequency ranges.
Many Qualcomm chips' spectral scan feature can only cover a 40 MHz range centered at the frequency of the current Wi-Fi channel. Therefore, it's better to run this app with hotspot enabled and "Turn off hotspot automatically" disabled. The app will then periodically switch the channel of the hotspot to cover different frequency ranges. If enabling hotspot before launching the app doesn't work, try enabling hotspot after launching the app instead.

This app will show a spectrogram on the screen, where brighter colors indicate higher FFT magnitudes. A short click on the screen will pause or resume the scanning, while a long click will show a configuration dialog. This app also employs a simple algorithm to detect Bluetooth transmission and estimate its strength. The following is a screenshot of the app in the presence of Bluetooth transmission.

Expand Down
23 changes: 13 additions & 10 deletions app/src/main/cpp/spectral-scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static struct {
struct sockaddr_un saddr_forward;
int sock_forward;
unsigned ifindex;
char ap_ifname[IF_NAMESIZE];
atomic_uint_least32_t ap_ifindex;
int send_fam;
struct nl_sock *nl_sock_send;
struct nl_sock *nl_sock_recv;
Expand All @@ -57,8 +57,7 @@ static struct {
static void handle_sigint(int sig) {}

static void switch_ap_freq(int freq) {
unsigned ap_ifindex = if_nametoindex(state.ap_ifname);
if (ap_ifindex == 0) {
if (state.ap_ifindex == 0) {
LOGE("Can't get AP interface index: %s", strerror(errno));
return;
}
Expand All @@ -75,7 +74,7 @@ static void switch_ap_freq(int freq) {
goto nla_put_failure;
}

NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ap_ifindex);
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, state.ap_ifindex);
NLA_PUT_U32(msg, NL80211_ATTR_CH_SWITCH_COUNT, 1);
NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, (uint32_t)freq);
NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, 0, NULL);
Expand Down Expand Up @@ -151,11 +150,15 @@ static void check_ap_freq() {
continue;
}

const struct nlattr *nla =
nla_find(genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0),
NL80211_ATTR_WIPHY_FREQ);
if (nla != NULL) {
state.ap_freq = nla_get_u32(nla);
const struct nlattr *nla;
int rem;
nla_for_each_attr(nla, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0),
rem) {
if (nla_type(nla) == NL80211_ATTR_IFINDEX) {
state.ap_ifindex = nla_get_u32(nla);
} else if (nla_type(nla) == NL80211_ATTR_WIPHY_FREQ) {
state.ap_freq = nla_get_u32(nla);
}
}
}
}
Expand Down Expand Up @@ -377,7 +380,7 @@ JNIEXPORT void JNICALL Java_com_example_spectral_1plot_ScanService_startScan(
if (ap_ifname[0] == '\0') {
strlcpy(ap_ifname, "wlan1", sizeof(ap_ifname));
}
strlcpy(state.ap_ifname, ap_ifname, sizeof(state.ap_ifname));
state.ap_ifindex = if_nametoindex(ap_ifname);

struct nl_sock *nl_sock_send = nl_socket_alloc();
if (nl_sock_send == NULL) {
Expand Down

0 comments on commit 2f16e51

Please sign in to comment.