1
0
Fork 0

Initial import

This commit is contained in:
Daniele Tricoli 2023-03-03 04:06:49 +01:00
parent 64b899f0df
commit 345b3874f3
4 changed files with 154 additions and 0 deletions

103
Cargo.lock generated
View File

@ -264,6 +264,31 @@ dependencies = [
"libc",
]
[[package]]
name = "crossterm"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67"
dependencies = [
"bitflags",
"crossterm_winapi",
"libc",
"mio",
"parking_lot",
"signal-hook",
"signal-hook-mio",
"winapi",
]
[[package]]
name = "crossterm_winapi"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ae1b35a484aa10e07fe0638d02301c5ad24de82d310ccbd2f3693da5f09bf1c"
dependencies = [
"winapi",
]
[[package]]
name = "dashmap"
version = "5.4.0"
@ -301,6 +326,12 @@ dependencies = [
"tokio",
]
[[package]]
name = "dyn-clone"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b0cf012f1230e43cd00ebb729c6bb58707ecfa8ad08b52ef3a4ccd2697fc30"
[[package]]
name = "either"
version = "1.8.1"
@ -509,6 +540,22 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "inquire"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3a94f0659efe59329832ba0452d3ec753145fc1fb12a8e1d60de4ccf99f5364"
dependencies = [
"bitflags",
"crossterm",
"dyn-clone",
"lazy_static",
"newline-converter",
"thiserror",
"unicode-segmentation",
"unicode-width",
]
[[package]]
name = "io-lifetimes"
version = "1.0.5"
@ -629,6 +676,7 @@ dependencies = [
"clap",
"futures",
"indicatif",
"inquire",
"pretty_env_logger",
"tokio",
"uuid",
@ -661,6 +709,15 @@ dependencies = [
"windows-sys 0.45.0",
]
[[package]]
name = "newline-converter"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f71d09d5c87634207f894c6b31b6a2b2c64ea3bdcf71bd5599fdbbe1600c00f"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
@ -698,6 +755,16 @@ version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.7"
@ -880,6 +947,36 @@ dependencies = [
"syn",
]
[[package]]
name = "signal-hook"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "732768f1176d21d09e076c23a93123d40bba92d50c4058da34d45c8de8e682b9"
dependencies = [
"libc",
"signal-hook-registry",
]
[[package]]
name = "signal-hook-mio"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af"
dependencies = [
"libc",
"mio",
"signal-hook",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1"
dependencies = [
"libc",
]
[[package]]
name = "slab"
version = "0.4.8"
@ -1017,6 +1114,12 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "unicode-segmentation"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
[[package]]
name = "unicode-width"
version = "0.1.10"

View File

@ -11,6 +11,7 @@ btleplug = "0.10.4"
clap = { version = "4.1.6", features = ["derive"] }
futures = "0.3.26"
indicatif = { version = "0.17.3", features = ["tokio"] }
inquire = "0.5.3"
pretty_env_logger = "0.4.0"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
uuid = "1.3.0"

View File

@ -7,6 +7,8 @@ use btleplug::api::{
};
use btleplug::platform::{Adapter, Manager, Peripheral};
use futures::stream::StreamExt;
use inquire::ui::{Color, RenderConfig, Styled};
use inquire::Text;
use tokio::time;
use uuid::Uuid;
@ -164,3 +166,42 @@ pub async fn send(
Ok(())
}
pub async fn repl(adapter_name: String, address: String) -> Result<(), Box<dyn Error>> {
let device = find_device_by_address(adapter_name, address).await?;
device.connect().await?;
if device.is_connected().await? {
device.discover_services().await?;
let chars = device.characteristics();
let tx_char = chars
.iter()
.find(|c| c.uuid == NORDIC_UART_TX_CHAR_UUID)
.ok_or("Unable to find TX characteric")?;
let rx_char = chars
.iter()
.find(|c| c.uuid == NORDIC_UART_RX_CHAR_UUID)
.ok_or("Unable to find RX characteric")?;
device.subscribe(&rx_char).await?;
let mut notification_stream = device.notifications().await?;
while let Some(data) = notification_stream.next().await {
let text = str::from_utf8(&data.value)?;
println!("{}", text.trim_end());
}
let text = tokio::spawn(get_input());
device
.write(&tx_char, text.await?.as_bytes(), WriteType::WithoutResponse)
.await?;
device.disconnect().await?;
}
Ok(())
}
async fn get_input() -> String {
let prompt_prefix = Styled::new("Φ]").with_fg(Color::White);
let default: RenderConfig = RenderConfig::default();
let mine = default.with_prompt_prefix(prompt_prefix);
Text::new("")
.with_render_config(mine)
.prompt()
.unwrap_or("".to_string())
}

View File

@ -31,6 +31,12 @@ enum Commands {
#[arg(required = true)]
text: Vec<String>,
},
/// Send messages to a device
Repl {
/// device's address
#[arg(long)]
device: String,
},
}
#[tokio::main]
@ -53,6 +59,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
Some(Commands::Send { device, text }) => {
ble::send(cli.adapter, device.clone(), text.join(" ")).await?;
}
Some(Commands::Repl { device }) => {
ble::repl(cli.adapter, device.clone()).await?;
}
None => {}
}