Skip to content

Commit 2e11eef

Browse files
committed
Parse Wi-Fi Hotspot information from system
AA Wireless Dongle and derivatives work by creating a Wi-Fi hotspot by setting values in /etc/hostapd/hostapd.conf and relying on the host system having static interface named "wlan0" and ip "10.0.0.1" Instead of having these values hardcoded, parse hostapd.conf and check the "wlan0" interface for its IPv4 address, and use those. In an upcoming commit, we should become able to specify the interface.
1 parent 9aa213e commit 2e11eef

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

Cargo.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ simplelog = { version = "0.11.2", features = ["paris", "ansi_term"] }
2727
clap = { version = "3.0.13", features = ["derive"] }
2828
humantime = "2.1.0"
2929
log = "0.4.22"
30+
simple_config_parser = "1.0.0"
31+
netif = "0.1.6"
3032

3133
[build-dependencies]
3234
protoc-bin-vendored = "3.1.0"

src/bluetooth.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bluer::{
88
};
99
use futures::StreamExt;
1010
use simplelog::*;
11+
use simple_config_parser::Config;
1112
use std::sync::Arc;
1213
use std::time::{Duration, Instant};
1314
use tokio::io::AsyncReadExt;
@@ -36,10 +37,10 @@ const HSP_HS_UUID: Uuid = Uuid::from_u128(0x0000110800001000800000805f9b34fb);
3637
const HSP_AG_UUID: Uuid = Uuid::from_u128(0x0000111200001000800000805f9b34fb);
3738
const BT_ALIAS: &str = "WirelessAADongle";
3839

39-
const WLAN_IFACE: &str = "wlan0";
40-
const WLAN_IP_ADDR: &str = "10.0.0.1";
41-
const WLAN_SSID: &str = "AAWirelessDongle";
42-
const WLAN_WPA_KEY: &str = "ConnectAAWirelessDongle";
40+
const DEFAULT_WLAN_IFACE: &str = "wlan0";
41+
const DEFAULT_WLAN_ADDR: &str = "10.0.0.1";
42+
43+
const HOSTAPD_FILE: &str = "/etc/hostapd/hostapd.conf";
4344

4445
#[derive(Debug, Clone, PartialEq)]
4546
#[repr(u16)]
@@ -345,21 +346,52 @@ pub async fn bluetooth_setup_connection(
345346
let mut stage = 1;
346347
let mut started;
347348

349+
let mut wlan_iface = String::from(DEFAULT_WLAN_IFACE);
350+
let mut wlan_ip_addr = String::from(DEFAULT_WLAN_ADDR);
351+
348352
let (state, mut stream) = power_up_and_wait_for_connection(advertise, connect).await?;
349353

354+
// Get UP interface and IP
355+
for ifa in netif::up().unwrap() {
356+
match ifa.name() {
357+
DEFAULT_WLAN_IFACE => {
358+
debug!("Found WLAN interface: {:?}", ifa);
359+
// IPv4 Address contains None scope_id, while IPv6 contains Some
360+
match ifa.scope_id() { None => {
361+
wlan_ip_addr = ifa.address().to_string();
362+
break;
363+
}
364+
_ => (),
365+
}
366+
}
367+
_ => (),
368+
}
369+
}
370+
371+
// Create a new config from hostapd.conf
372+
let hostapd = Config::new()
373+
.file(HOSTAPD_FILE)
374+
.unwrap();
375+
376+
// read SSID and WPA_KEY
377+
let wlan_ssid = &hostapd.get_str("ssid").unwrap();
378+
let wlan_wpa_key = &hostapd.get_str("wpa_passphrase").unwrap();
379+
350380
info!("{} 📲 Sending parameters via bluetooth to phone...", NAME);
351381
let mut start_req = WifiStartRequest::new();
352-
start_req.set_ip_address(String::from(WLAN_IP_ADDR));
382+
info!("{} 🛜 Sending Host IP Address: {}", NAME, wlan_ip_addr);
383+
start_req.set_ip_address(String::from(wlan_ip_addr));
353384
start_req.set_port(TCP_SERVER_PORT);
354385
send_message(&mut stream, stage, MessageId::WifiStartRequest, start_req).await?;
355386
stage += 1;
356387
started = Instant::now();
357388
read_message(&mut stream, stage, MessageId::WifiInfoRequest, started).await?;
358389

359390
let mut info = WifiInfoResponse::new();
360-
info.set_ssid(String::from(WLAN_SSID));
361-
info.set_key(String::from(WLAN_WPA_KEY));
362-
let bssid = mac_address::mac_address_by_name(WLAN_IFACE)
391+
info.set_ssid(String::from(wlan_ssid));
392+
info.set_key(String::from(wlan_wpa_key));
393+
info!("{} 🛜 Sending Host SSID and Password: {}, {}", NAME, wlan_ssid, wlan_wpa_key);
394+
let bssid = mac_address::mac_address_by_name(&wlan_iface)
363395
.unwrap()
364396
.unwrap()
365397
.to_string();

0 commit comments

Comments
 (0)