Skip to content

Commit

Permalink
draft(wifisetup): Sketch wifisetup
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Feb 9, 2024
1 parent a17e3b6 commit ce277a3
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ pub mod completions;
pub mod context;
pub mod tamanu;
pub mod upload;
pub mod wifisetup;

#[derive(Debug, Clone, Subcommand)]
pub enum Action {
Completions(completions::CompletionsArgs),
Tamanu(tamanu::TamanuArgs),
Upload(upload::UploadArgs),
Wifisetup(wifisetup::WifisetupArgs),
}

pub async fn run() -> Result<()> {
Expand All @@ -28,6 +30,7 @@ pub async fn run() -> Result<()> {
(Action::Completions(args), ctx) => completions::run(ctx.with_top(args)).await,
(Action::Tamanu(args), ctx) => tamanu::run(ctx.with_top(args)).await,
(Action::Upload(args), ctx) => upload::run(ctx.with_top(args)).await,
(Action::Wifisetup(args), ctx) => wifisetup::run(ctx.with_top(args)).await,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/actions/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<A, B> Context<A, B> {
)
}

#[allow(dead_code)]
pub fn bar(&self, len: u64) -> ProgressBar {
self.progress.add(
ProgressBar::new(len).with_style(
Expand Down
34 changes: 34 additions & 0 deletions src/actions/wifisetup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use clap::{Parser, Subcommand};
use miette::Result;

use super::Context;

pub mod serve;
pub mod scan;
pub mod connect;
pub mod forget;

/// Configure wifi (using NetowrkManager).
#[derive(Debug, Clone, Parser)]
pub struct WifisetupArgs {
/// Wifisetup subcommand
#[command(subcommand)]
pub action: WifisetupAction,
}

#[derive(Debug, Clone, Subcommand)]
pub enum WifisetupAction {
Serve(serve::ServeArgs),
Scan(scan::ScanArgs),
Connect(connect::ConnectArgs),
Forget(forget::ForgetArgs),
}

pub async fn run(ctx: Context<WifisetupArgs>) -> Result<()> {
match ctx.args_top.action.clone() {
WifisetupAction::Serve(subargs) => serve::run(ctx.with_sub(subargs)).await,
WifisetupAction::Scan(subargs) => scan::run(ctx.with_sub(subargs)).await,
WifisetupAction::Connect(subargs) => connect::run(ctx.with_sub(subargs)).await,
WifisetupAction::Forget(subargs) => forget::run(ctx.with_sub(subargs)).await,
}
}
47 changes: 47 additions & 0 deletions src/actions/wifisetup/connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use clap::Parser;
use miette::Result;
use tracing::instrument;

use crate::actions::Context;

use super::WifisetupArgs;

/// Create a wifi connection.
///
/// This creates a new connection profile. The wifi network doesn't need to be currently
/// broadcasting; it will be used the next time the device is in range.
///
/// If either `--name` or `--id` is provided, and a matching connection profile already exists, it
/// will be updated with the new settings. Otherwise, a new profile will be created.
#[derive(Debug, Clone, Parser)]
pub struct ConnectArgs {
/// SSID of the wifi network.
///
/// Obtain it using `bestool wifisetup scan`.
#[arg(long, value_name = "SSID")]
pub ssid: String,

/// Password for the wifi network.
///
/// Connecting to open wifi networks is not supported as these are insecure.
#[arg(long, value_name = "PASSWORD")]
pub password: String,

/// Name of the connection profile.
///
/// If a profile with this name already exists, it will be updated with the new settings.
#[arg(long, value_name = "NAME")]
pub name: Option<String>,

/// UUID of the connection profile.
///
/// If a profile with this UUID already exists, it will be updated with the new settings.
#[arg(long, value_name = "UUID")]
pub id: Option<String>,
}

#[instrument(skip(ctx))]
pub async fn run(ctx: Context<WifisetupArgs, ConnectArgs>) -> Result<()> {
drop(ctx);
Ok(())
}
32 changes: 32 additions & 0 deletions src/actions/wifisetup/forget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use clap::Parser;
use miette::Result;
use tracing::instrument;

use crate::actions::Context;

use super::WifisetupArgs;

/// Forget a wifi connection.
///
/// This deletes the connection profile and reloads the network configuration. Be careful if you're
/// connected over wifi, as this may disconnect you.
#[derive(Debug, Clone, Parser)]
pub struct ForgetArgs {
/// Name of the connection profile.
///
/// Either this or `--id` must be provided.
#[arg(long, value_name = "NAME", required_unless_present = "id")]
pub name: Option<String>,

/// UUID of the connection profile.
///
/// Either this or `--name` must be provided.
#[arg(long, value_name = "UUID", required_unless_present = "name")]
pub id: Option<String>,
}

#[instrument(skip(ctx))]
pub async fn run(ctx: Context<WifisetupArgs, ForgetArgs>) -> Result<()> {
drop(ctx);
Ok(())
}
51 changes: 51 additions & 0 deletions src/actions/wifisetup/scan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use clap::Parser;
use miette::Result;
use tracing::instrument;

use crate::actions::Context;

use super::WifisetupArgs;

/// Scan for wifi networks.
///
/// This scans for wifi networks and prints the results to stdout. Use `--json` for machine-readable
/// output.
#[derive(Debug, Clone, Parser)]
pub struct ScanArgs {
/// How long to wait for the scan to complete.
///
/// Will wait for the scan to complete, or until this timeout is reached, whichever comes first,
/// then exit.
#[arg(long, value_name = "DURATION", default_value = "10s")]
pub timeout: humantime::Duration,

/// Print output in JSON format.
///
/// Like the human-friendly output, one line is printed per network, as soon as it's detected.
///
/// {"ssid": "MyNetwork", "aps": [{"bssid":"00:11:22:33:44:55", "signal": -50}], "generation": 5, "security": "wpa2", "profile": "uuid"}
///
/// The "profile" field is only present if the network is already configured, and is the UUID of
/// the connection profile.
#[arg(long)]
pub json: bool,

/// Print insecure networks.
///
/// By default, insecure networks are not printed. This is because connecting to open wifi is
/// not supported. Adds a "secure": false field to the JSON output.
#[arg(long)]
pub insecure: bool,

/// Which interface to scan.
///
/// By default, the interface is autodetected.
#[arg(long)]
pub interface: Option<String>,
}

#[instrument(skip(ctx))]
pub async fn run(ctx: Context<WifisetupArgs, ScanArgs>) -> Result<()> {
drop(ctx);
Ok(())
}
32 changes: 32 additions & 0 deletions src/actions/wifisetup/serve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use clap::Parser;
use miette::Result;
use tracing::instrument;

use crate::actions::Context;

use super::WifisetupArgs;

/// Serve a web interface for wifi setup.
///
/// By convention, this is expected to be available at http://hostip/wifisetup.
///
/// This is a simple web interface for configuring wifi. It's intended to be used on devices that
/// don't have a screen or keyboard, and is designed to be used on a phone. It's essentially the
/// same as this CLI, but with a web interface. It's recommended to set a `--password`, otherwise
/// anyone on the same network can reconfigure the device.
#[derive(Debug, Clone, Parser)]
pub struct ServeArgs {
/// Port to listen on.
#[arg(long, value_name = "PORT", default_value = "9209")]
pub port: u16,

/// Password for the web interface.
#[arg(long, value_name = "PASSWORD")]
pub password: Option<String>,
}

#[instrument(skip(ctx))]
pub async fn run(ctx: Context<WifisetupArgs, ServeArgs>) -> Result<()> {
drop(ctx);
Ok(())
}

0 comments on commit ce277a3

Please sign in to comment.