Skip to content

Commit 6225309

Browse files
authored
Merge pull request #8 from KreAch3R/arguments
Add --keepalive argument
2 parents 38a633c + fff4094 commit 6225309

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ OPTIONS:
155155
-i, --iface <IFACE>
156156
WLAN / Wi-Fi Hotspot interface [default: wlan0]
157157
158+
-k, --keepalive
159+
Keep alive mode: BLE adapter doesn't turn off after successful connection, so that the
160+
phone can remain connected (used in special configurations)
161+
158162
-l, --legacy
159163
Enable legacy mode
160164

src/bluetooth.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct BluetoothState {
5555
handle_aa: ProfileHandle,
5656
handle_hsp: JoinHandle<Result<ProfileHandle>>,
5757
handle_agent: AgentHandle,
58+
keepalive: bool,
5859
}
5960

6061
pub async fn get_cpu_serial_number_suffix() -> Result<String> {
@@ -71,6 +72,7 @@ async fn power_up_and_wait_for_connection(
7172
advertise: bool,
7273
btalias: Option<String>,
7374
connect: Option<Address>,
75+
keepalive: bool,
7476
) -> Result<(BluetoothState, Stream)> {
7577
// setting BT alias for further use
7678
let alias = match btalias {
@@ -220,6 +222,7 @@ async fn power_up_and_wait_for_connection(
220222
handle_aa,
221223
handle_hsp: task_hsp,
222224
handle_agent,
225+
keepalive,
223226
};
224227

225228
Ok((state, stream))
@@ -328,8 +331,12 @@ pub async fn bluetooth_stop(state: BluetoothState) -> Result<()> {
328331
}
329332
}
330333

331-
state.adapter.set_powered(false).await?;
332-
info!("{} 💤 Bluetooth adapter powered off", NAME);
334+
if state.keepalive {
335+
info!("{} 💤 Bluetooth adapter stays on", NAME);
336+
} else {
337+
state.adapter.set_powered(false).await?;
338+
info!("{} 💤 Bluetooth adapter powered off", NAME);
339+
}
333340

334341
Ok(())
335342
}
@@ -340,13 +347,15 @@ pub async fn bluetooth_setup_connection(
340347
connect: Option<Address>,
341348
wifi_config: WifiConfig,
342349
tcp_start: Arc<Notify>,
350+
keepalive: bool,
343351
) -> Result<BluetoothState> {
344352
use WifiInfoResponse::WifiInfoResponse;
345353
use WifiStartRequest::WifiStartRequest;
346354
let mut stage = 1;
347355
let mut started;
348356

349-
let (state, mut stream) = power_up_and_wait_for_connection(advertise, btalias, connect).await?;
357+
let (state, mut stream) =
358+
power_up_and_wait_for_connection(advertise, btalias, connect, keepalive).await?;
350359

351360
info!("{} 📲 Sending parameters via bluetooth to phone...", NAME);
352361
let mut start_req = WifiStartRequest::new();

src/main.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ struct Args {
7575
/// BLE device name
7676
#[clap(short, long)]
7777
btalias: Option<String>,
78+
79+
/// Keep alive mode: BLE adapter doesn't turn off after successful connection,
80+
/// so that the phone can remain connected (used in special configurations)
81+
#[clap(short, long)]
82+
keepalive: bool,
7883
}
7984

8085
#[derive(Clone)]
@@ -179,6 +184,7 @@ async fn tokio_main(
179184
hostapd_conf: PathBuf,
180185
connect: Option<Address>,
181186
udc: Option<String>,
187+
keepalive: bool,
182188
need_restart: Arc<Notify>,
183189
tcp_start: Arc<Notify>,
184190
) {
@@ -205,6 +211,7 @@ async fn tokio_main(
205211
connect,
206212
wifi_conf.clone(),
207213
tcp_start.clone(),
214+
keepalive,
208215
)
209216
.await
210217
{
@@ -289,6 +296,7 @@ fn main() {
289296
args.hostapd_conf,
290297
args.connect,
291298
args.udc,
299+
args.keepalive,
292300
need_restart,
293301
tcp_start,
294302
)

0 commit comments

Comments
 (0)