Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --keepalive argument #8

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ OPTIONS:
-i, --iface <IFACE>
WLAN / Wi-Fi Hotspot interface [default: wlan0]

-k, --keepalive
Keep alive mode: BLE adapter doesn't turn off after successful connection, so that the
phone can remain connected (used in special configurations)

-l, --legacy
Enable legacy mode

Expand Down
15 changes: 12 additions & 3 deletions src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub struct BluetoothState {
handle_aa: ProfileHandle,
handle_hsp: JoinHandle<Result<ProfileHandle>>,
handle_agent: AgentHandle,
keepalive: bool,
}

pub async fn get_cpu_serial_number_suffix() -> Result<String> {
Expand All @@ -71,6 +72,7 @@ async fn power_up_and_wait_for_connection(
advertise: bool,
btalias: Option<String>,
connect: Option<Address>,
keepalive: bool,
) -> Result<(BluetoothState, Stream)> {
// setting BT alias for further use
let alias = match btalias {
Expand Down Expand Up @@ -220,6 +222,7 @@ async fn power_up_and_wait_for_connection(
handle_aa,
handle_hsp: task_hsp,
handle_agent,
keepalive,
};

Ok((state, stream))
Expand Down Expand Up @@ -328,8 +331,12 @@ pub async fn bluetooth_stop(state: BluetoothState) -> Result<()> {
}
}

state.adapter.set_powered(false).await?;
info!("{} 💤 Bluetooth adapter powered off", NAME);
if state.keepalive {
info!("{} 💤 Bluetooth adapter stays on", NAME);
} else {
state.adapter.set_powered(false).await?;
info!("{} 💤 Bluetooth adapter powered off", NAME);
}

Ok(())
}
Expand All @@ -340,13 +347,15 @@ pub async fn bluetooth_setup_connection(
connect: Option<Address>,
wifi_config: WifiConfig,
tcp_start: Arc<Notify>,
keepalive: bool,
) -> Result<BluetoothState> {
use WifiInfoResponse::WifiInfoResponse;
use WifiStartRequest::WifiStartRequest;
let mut stage = 1;
let mut started;

let (state, mut stream) = power_up_and_wait_for_connection(advertise, btalias, connect).await?;
let (state, mut stream) =
power_up_and_wait_for_connection(advertise, btalias, connect, keepalive).await?;

info!("{} 📲 Sending parameters via bluetooth to phone...", NAME);
let mut start_req = WifiStartRequest::new();
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ struct Args {
/// BLE device name
#[clap(short, long)]
btalias: Option<String>,

/// Keep alive mode: BLE adapter doesn't turn off after successful connection,
/// so that the phone can remain connected (used in special configurations)
#[clap(short, long)]
keepalive: bool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -179,6 +184,7 @@ async fn tokio_main(
hostapd_conf: PathBuf,
connect: Option<Address>,
udc: Option<String>,
keepalive: bool,
need_restart: Arc<Notify>,
tcp_start: Arc<Notify>,
) {
Expand All @@ -205,6 +211,7 @@ async fn tokio_main(
connect,
wifi_conf.clone(),
tcp_start.clone(),
keepalive,
)
.await
{
Expand Down Expand Up @@ -289,6 +296,7 @@ fn main() {
args.hostapd_conf,
args.connect,
args.udc,
args.keepalive,
need_restart,
tcp_start,
)
Expand Down
Loading