diff --git a/fan-controller/build.rs b/fan-controller/build.rs index aaa840d..c7328a4 100644 --- a/fan-controller/build.rs +++ b/fan-controller/build.rs @@ -51,6 +51,10 @@ fn ensure_memory_x_file() -> Result<(), BuildError> { fn setup_wifi_credentials() -> Result<(), BuildError> { const WIFI_NETWORK: &str = "FAN_CONTROL_WIFI_NETWORK"; const WIFI_PASSWORD: &str = "FAN_CONTROL_WIFI_PASSWORD"; + const MQTT_BROKER_USERNAME: &str = "FAN_CONTROL_MQTT_BROKER_USERNAME"; + const MQTT_BROKER_PASSWORD: &str = "FAN_CONTROL_MQTT_BROKER_PASSWORD"; + const MQTT_BROKER_ADDRESS: &str = "FAN_CONTROL_MQTT_BROKER_ADDRESS"; + const MQTT_BROKER_PORT: &str = "FAN_CONTROL_MQTT_BROKER_PORT"; dotenvy::dotenv()?; let wifi_network = @@ -58,8 +62,21 @@ fn setup_wifi_credentials() -> Result<(), BuildError> { let wifi_password = env::var(WIFI_PASSWORD).map_err(|error| BuildError::MissingEnvVar(error, WIFI_PASSWORD))?; + let mqtt_broker_username = env::var(MQTT_BROKER_USERNAME) + .map_err(|error| BuildError::MissingEnvVar(error, MQTT_BROKER_USERNAME))?; + let mqtt_broker_password = env::var(MQTT_BROKER_PASSWORD) + .map_err(|error| BuildError::MissingEnvVar(error, MQTT_BROKER_PASSWORD))?; + let mqtt_broker_address = env::var(MQTT_BROKER_ADDRESS) + .map_err(|error| BuildError::MissingEnvVar(error, MQTT_BROKER_ADDRESS))?; + let mqtt_broker_port = env::var(MQTT_BROKER_PORT) + .map_err(|error| BuildError::MissingEnvVar(error, MQTT_BROKER_PORT))?; + println!("cargo:rustc-env=FAN_CONTROL_WIFI_NETWORK={wifi_network}"); println!("cargo:rustc-env=FAN_CONTROL_WIFI_PASSWORD={wifi_password}"); + println!("cargo:rustc-env=FAN_CONTROL_MQTT_BROKER_USERNAME={mqtt_broker_username}"); + println!("cargo:rustc-env=FAN_CONTROL_MQTT_BROKER_PASSWORD={mqtt_broker_password}"); + println!("cargo:rustc-env=FAN_CONTROL_MQTT_BROKER_ADDRESS={mqtt_broker_address}"); + println!("cargo:rustc-env=FAN_CONTROL_MQTT_BROKER_PORT={mqtt_broker_port}"); Ok(()) } diff --git a/fan-controller/src/configuration.rs b/fan-controller/src/configuration.rs index f3162c4..9e4edb5 100644 --- a/fan-controller/src/configuration.rs +++ b/fan-controller/src/configuration.rs @@ -1,6 +1,25 @@ use embassy_net::IpAddress; use embassy_time::Duration; +/// Thanks! +/// - https://users.rust-lang.org/t/pass-numeric-compile-time-arguments/59093/2 +/// - https://www.reddit.com/r/rust/comments/10ol38k/comment/j6jrpvd +const fn parse_u16(string: &str) -> u16 { + let mut out: u16 = 0; + let mut index: usize = 0; + while index < string.len() { + out *= 10; + let byte = string.as_bytes()[index]; + assert!( + b'0' <= byte && byte <= b'9', + "invalid digit. Needs to be a number", + ); + out += (byte - b'0') as u16; + index += 1; + } + out +} + //TODO make configurable /// Don't put credentials in the source code pub(crate) const WIFI_NETWORK: &str = env!("FAN_CONTROL_WIFI_NETWORK"); @@ -10,10 +29,10 @@ pub(crate) const WIFI_NETWORK: &str = env!("FAN_CONTROL_WIFI_NETWORK"); pub(crate) const WIFI_PASSWORD: &str = env!("FAN_CONTROL_WIFI_PASSWORD"); //TODO make configurable -pub(crate) const MQTT_BROKER_ADDRESS: &str = "homeassistant"; +pub(crate) const MQTT_BROKER_ADDRESS: &str = env!("FAN_CONTROL_MQTT_BROKER_ADDRESS"); //TODO make configurable -pub(crate) const MQTT_BROKER_PORT: u16 = 1883; +pub(crate) const MQTT_BROKER_PORT: u16 = parse_u16(env!("FAN_CONTROL_MQTT_BROKER_PORT")); //TODO make configurable /// The broker IP address can be configured manually. It will be used instead of the [MQTT_BROKER_ADDRESS] @@ -30,8 +49,8 @@ pub(crate) struct MqttBrokerCredentials<'a> { } pub(crate) const MQTT_BROKER_CREDENTIALS: MqttBrokerCredentials = MqttBrokerCredentials { - username: "fancontroller", - password: b"test", + username: env!("FAN_CONTROL_MQTT_BROKER_USERNAME"), + password: env!("FAN_CONTROL_MQTT_BROKER_PASSWORD").as_bytes(), }; //TODO make configurable diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index 58ce5ea..82d41fe 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -1,7 +1,5 @@ #![no_std] #![no_main] -//TODO remove this 🥴 -#![allow(warnings)] use configuration::DISCOVERY_TOPIC; use core::convert::Infallible;