diff --git a/.cargo/config.toml b/.cargo/config.toml index cebd1c3..fbc68bc 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -13,6 +13,6 @@ build-std = ["std", "panic_abort"] [env] MCU="esp32" # Note: this variable is not used by the pio builder (`cargo build --features pio`) -ESP_IDF_VERSION = "v5.1.1" +ESP_IDF_VERSION = "v5.2.1" CARGO_WORKSPACE_DIR = { value = "", relative = true } \ No newline at end of file diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4bfeafa..c0f94d6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -18,4 +18,4 @@ jobs: default: true ldproxy: true - name: Run cargo check - run: cargo check + run: cargo check --bins --examples diff --git a/.gitignore b/.gitignore index 85af13a..39d9c42 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,4 @@ /target /Cargo.lock -wifi_pass.txt -wifi_ssid.txt -examples/telegram_bot/bot_token.txt -examples/telegram_bot/owner_id.txt +cfg.toml \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 407d84f..d357492 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,26 +26,28 @@ embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf- [dependencies] log = { version = "0.4", default-features = false } -esp-idf-svc = { version = "0.47", default-features = false } -embedded-svc = "0.26.4" +esp-idf-svc = { version = "0.48", default-features = false } +embedded-svc = "0.27.1" anyhow = "1.0.79" bstr = { version = "1.8.0", default-features = false } -esp32-nimble = "0.5.0" +esp32-nimble = "0.6.0" tokio = { version = "*", features = ["rt", "time", "sync","macros"] } lazy_static = "1.4.0" uuid = { version = "1.2.2", default-features = false, features = ["macro-diagnostics"] } rgb565 = "0.1.3" -image = "0.24.8" +image = "0.25" frankenstein = { version = "0.30", default-features = false, features = ["telegram-trait"]} serde = { version = "1", features = ["derive"]} serde_json = { version = "1"} idotmatrix = {git = "https://github.com/Kezii/idotmatrix.git"} -esp-idf-hal = "*" -esp-idf-sys = "*" +esp-idf-hal = "0.43" +esp-idf-sys = "0.34" thiserror = "1.0.56" +toml-cfg = "=0.1.3" + [[package.metadata.esp-idf-sys.extra_components]] component_dirs = "components/esp32-camera" bindings_header = "components/bindings.h" @@ -54,3 +56,4 @@ bindings_module = "camera" [build-dependencies] embuild = "0.31.3" +toml-cfg = "=0.1.3" \ No newline at end of file diff --git a/build.rs b/build.rs index 112ec3f..a7b9290 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,20 @@ +#[toml_cfg::toml_config] +pub struct Config { + #[default("")] + wifi_ssid: &'static str, + #[default("")] + wifi_psk: &'static str, + #[default("")] + bot_token: &'static str, + #[default(0)] + owner_id: i64, +} + fn main() { + // Check if the `cfg.toml` file exists and has been filled out. + if !std::path::Path::new("cfg.toml").exists() { + panic!("You need to create a `cfg.toml` file with your Wi-Fi credentials! Use `cfg.toml.example` as a template."); + } + embuild::espidf::sysenv::output(); } diff --git a/cfg.toml.example b/cfg.toml.example new file mode 100644 index 0000000..817f4ec --- /dev/null +++ b/cfg.toml.example @@ -0,0 +1,5 @@ +[espcam] +wifi_ssid = "FBI Surveillance Van" +wifi_psk = "hunter2" +bot_token = "change_me" +bot_owner_id = 12345678 \ No newline at end of file diff --git a/components_esp32.lock b/components_esp32.lock index 10763e6..6e08dbe 100644 --- a/components_esp32.lock +++ b/components_esp32.lock @@ -3,7 +3,7 @@ dependencies: component_hash: null source: type: idf - version: 5.1.1 + version: 5.2.1 manifest_hash: 3e6980bc5fca475de2a39d94c3ea3944c0528040d862609e810d1d8f5ea7f661 target: esp32 version: 1.0.0 diff --git a/examples/idotmatrix/idotmatrixble.rs b/examples/idotmatrix/idotmatrixble.rs index 63d233a..18f217c 100644 --- a/examples/idotmatrix/idotmatrixble.rs +++ b/examples/idotmatrix/idotmatrixble.rs @@ -1,10 +1,10 @@ use crate::Camera; use anyhow::Result; use bstr::ByteSlice; -use esp32_nimble::{uuid128, BLEClient, BLEDevice, BLEReturnCode}; +use esp32_nimble::{uuid128, BLEClient, BLEDevice, BLEError}; use esp_idf_sys::camera; use espcam::espcam::FrameBuffer; -use image::{ImageBuffer, ImageOutputFormat, Rgb}; +use image::{ImageBuffer, ImageFormat, Rgb}; use log::{error, info}; pub struct IDMBle<'a> { @@ -15,7 +15,7 @@ impl<'a> IDMBle<'a> { pub async fn new( ble_device: &'a BLEDevice, client: &'a mut BLEClient, - ) -> Result { + ) -> Result { let ble_scan = ble_device.get_scan(); info!("Scanning for BLE devices..."); @@ -44,11 +44,11 @@ impl<'a> IDMBle<'a> { Ok(Self { characteristic }) } else { error!("No device found"); - Err(BLEReturnCode::fail().unwrap_err()) + Err(BLEError::fail().unwrap_err()) } } - pub async fn send_data(&mut self, bytes: &[u8]) -> Result<(), BLEReturnCode> { + pub async fn send_data(&mut self, bytes: &[u8]) -> Result<(), BLEError> { for (counter, chunk) in bytes.chunks(512).enumerate() { let succ = self.characteristic.write_value(chunk, true).await; info!("progress: {}%", (counter * chunk.len()) * 100 / bytes.len()); @@ -90,7 +90,7 @@ pub async fn idotmatrix_stream_task(camera: Camera<'_>) -> Result<()> { let mut c = std::io::Cursor::new(Vec::new()); info!("Writing png"); - scaled.write_to(&mut c, ImageOutputFormat::Png).unwrap(); + scaled.write_to(&mut c, ImageFormat::Png).unwrap(); info!("Creating command"); let command = idotmatrix::IDMCommand::UploadPng(c.into_inner()); diff --git a/examples/telegram_bot/main.rs b/examples/telegram_bot/main.rs index 861a216..2435068 100644 --- a/examples/telegram_bot/main.rs +++ b/examples/telegram_bot/main.rs @@ -3,7 +3,7 @@ use anyhow::{bail, Result}; use bot_api::{telegram_post_multipart, Esp32Api}; use esp_idf_hal::gpio::PinDriver; use esp_idf_svc::{eventloop::EspSystemEventLoop, hal::peripherals::Peripherals}; -use espcam::{espcam::Camera, wifi_handler::my_wifi}; +use espcam::{config::get_config, espcam::Camera, wifi_handler::my_wifi}; use frankenstein::{ ForwardMessageParams, GetUpdatesParams, SendChatActionParams, SendMessageParams, TelegramApi, }; @@ -29,10 +29,14 @@ fn main() -> Result<()> { let mut flash_led = PinDriver::output(peripherals.pins.gpio4).unwrap(); flash_led.set_low().unwrap(); - let wifi_ssid = include_str!("../../wifi_ssid.txt"); - let wifi_pass = include_str!("../../wifi_pass.txt"); + let config = get_config(); - let _wifi = match my_wifi(wifi_ssid, wifi_pass, peripherals.modem, sysloop) { + let _wifi = match my_wifi( + config.wifi_ssid, + config.wifi_psk, + peripherals.modem, + sysloop, + ) { Ok(inner) => inner, Err(err) => { bail!("Could not connect to Wi-Fi network: {:?}", err) @@ -63,8 +67,8 @@ fn main() -> Result<()> { let mut bot_state = BotState { should_use_flash: false, public_use: false, - owner_id: include_str!("owner_id.txt").parse().unwrap(), - bot_token: include_str!("bot_token.txt"), + owner_id: config.bot_owner_id, + bot_token: config.bot_token, }; let api = Esp32Api::new(bot_state.bot_token); diff --git a/examples/webserver.rs b/examples/webserver.rs index 3dcd8d3..eef4161 100644 --- a/examples/webserver.rs +++ b/examples/webserver.rs @@ -6,7 +6,7 @@ use esp_idf_svc::{ hal::peripherals::Peripherals, http::{server::EspHttpServer, Method}, }; -use espcam::{espcam::Camera, wifi_handler::my_wifi}; +use espcam::{config::get_config, espcam::Camera, wifi_handler::my_wifi}; fn main() -> Result<()> { esp_idf_svc::sys::link_patches(); @@ -16,10 +16,14 @@ fn main() -> Result<()> { let peripherals = Peripherals::take().unwrap(); - let wifi_ssid = include_str!("../wifi_ssid.txt"); - let wifi_pass = include_str!("../wifi_pass.txt"); + let config = get_config(); - let _wifi = match my_wifi(wifi_ssid, wifi_pass, peripherals.modem, sysloop) { + let _wifi = match my_wifi( + config.wifi_ssid, + config.wifi_psk, + peripherals.modem, + sysloop, + ) { Ok(inner) => inner, Err(err) => { bail!("Could not connect to Wi-Fi network: {:?}", err) @@ -66,13 +70,13 @@ fn main() -> Result<()> { response.write_all("no framebuffer".as_bytes())?; } - Ok(()) + Ok::<(), anyhow::Error>(()) })?; server.fn_handler("/", Method::Get, |request| { let mut response = request.into_ok_response()?; response.write_all("ok".as_bytes())?; - Ok(()) + Ok::<(), anyhow::Error>(()) })?; loop { diff --git a/src/ble.rs b/src/ble.rs index df15437..624ac8f 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -2,7 +2,7 @@ use std::time::Duration; use anyhow::bail; use esp32_nimble::utilities::BleUuid; -use esp32_nimble::{BLEAdvertising, BLEScan, BLEServer, NimbleProperties}; +use esp32_nimble::{BLEAdvertisementData, BLEAdvertising, BLEScan, BLEServer, NimbleProperties}; use lazy_static::lazy_static; use log::{info, warn}; use tokio::select; @@ -52,7 +52,14 @@ pub async fn ble_advertise_task( ); notifying_characteristic.lock().set_value(b"uptime: 0"); - advertising.name(name).add_service_uuid(*UUID_BLE_SERVICE); + // advertising.name(name).add_service_uuid(*UUID_BLE_SERVICE); + advertising + .set_data( + BLEAdvertisementData::new() + .name(name) + .add_service_uuid(*UUID_BLE_SERVICE), + ) + .unwrap(); advertising.start().expect("ble_advertising.start()"); diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..7c7e268 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,15 @@ +#[toml_cfg::toml_config] +pub struct Config { + #[default("")] + wifi_ssid: &'static str, + #[default("")] + wifi_psk: &'static str, + #[default("")] + bot_token: &'static str, + #[default(0)] + bot_owner_id: i64, +} + +pub fn get_config() -> Config { + CONFIG +} diff --git a/src/lib.rs b/src/lib.rs index 14470b0..8b1a790 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod ble; +pub mod config; pub mod espcam; pub mod wifi_handler; diff --git a/src/wifi_handler.rs b/src/wifi_handler.rs index 358df77..ff5d6de 100644 --- a/src/wifi_handler.rs +++ b/src/wifi_handler.rs @@ -51,8 +51,12 @@ pub fn my_wifi( }; wifi.set_configuration(&Configuration::Client(ClientConfiguration { - ssid: ssid.into(), - password: pass.into(), + ssid: ssid + .try_into() + .expect("SSID could not be converted to heapless String"), + password: pass + .try_into() + .expect("Password could not be converted to heapless String"), channel, auth_method, ..Default::default()