From dc816934ddddc9d6dcad103f77bc0f3ad3035767 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Fri, 10 Jun 2022 16:45:15 -0700 Subject: [PATCH] mode from env at compile time, reconn loop, try to compile with real signer --- sphinx-key/src/conn/mqtt.rs | 30 ++++++++++++++---------------- sphinx-key/src/core/mod.rs | 1 + sphinx-key/src/core/mode.rs | 32 ++++++++++++++++++++++++++++++++ sphinx-key/src/main.rs | 13 +++++++++++-- 4 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 sphinx-key/src/core/mode.rs diff --git a/sphinx-key/src/conn/mqtt.rs b/sphinx-key/src/conn/mqtt.rs index e3e059d..ab15db3 100644 --- a/sphinx-key/src/conn/mqtt.rs +++ b/sphinx-key/src/conn/mqtt.rs @@ -4,6 +4,7 @@ use embedded_svc::mqtt::client::utils::Connection as MqttConnection; use esp_idf_svc::mqtt::client::*; use anyhow::Result; use log::*; +use std::time::Duration; use std::thread; use esp_idf_sys::{self}; use esp_idf_sys::EspError; @@ -12,16 +13,15 @@ use std::sync::{mpsc}; pub const TOPIC: &str = "sphinx"; pub const RETURN_TOPIC: &str = "sphinx-return"; -pub const CLIENT_ID: &str = "sphinx-1"; pub const USERNAME: &str = "sphinx-key"; pub const PASSWORD: &str = "sphinx-key-pass"; -pub fn make_client(broker: &str) -> Result<( +pub fn make_client(broker: &str, client_id: &str) -> Result<( EspMqttClient>, - MqttConnection + MqttConnection, )> { let conf = MqttClientConfiguration { - client_id: Some(CLIENT_ID), + client_id: Some(client_id), buffer_size: 2048, task_stack: 12288, username: Some(USERNAME), @@ -34,7 +34,16 @@ pub fn make_client(broker: &str) -> Result<( let b = format!("mqtt://{}", broker); println!("===> CONNECT TO {}", b); // let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?; - let cc = EspMqttClient::new_with_conn(b, &conf)?; + let cc = loop { + match EspMqttClient::new_with_conn(b.clone(), &conf) { + Ok(c_c) => { + break c_c + }, + Err(_) => { + thread::sleep(Duration::from_secs(1)); + } + } + }; // info!("MQTT client started"); @@ -86,19 +95,8 @@ pub fn start_listening( //info!("MQTT connection loop exit"); }); - // log::info!("lock mqtt mutex guard"); - // let mut client = mqtt.lock().unwrap(); - log::info!("SUBSCRIBE TO {}", TOPIC); client.subscribe(TOPIC, QoS::AtMostOnce)?; - log::info!("PUBLISH {} to {}", "READY", RETURN_TOPIC); - client.publish( - RETURN_TOPIC, - QoS::AtMostOnce, - false, - format!("READY").as_bytes(), - )?; - Ok(client) } diff --git a/sphinx-key/src/core/mod.rs b/sphinx-key/src/core/mod.rs index dafb93a..8829ccb 100644 --- a/sphinx-key/src/core/mod.rs +++ b/sphinx-key/src/core/mod.rs @@ -1,2 +1,3 @@ pub mod events; pub mod config; +pub mod mode; \ No newline at end of file diff --git a/sphinx-key/src/core/mode.rs b/sphinx-key/src/core/mode.rs new file mode 100644 index 0000000..dd33b39 --- /dev/null +++ b/sphinx-key/src/core/mode.rs @@ -0,0 +1,32 @@ +use std::fmt; + +pub enum Mode { + Test, + Signer, +} + +impl fmt::Debug for Mode { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Mode::Test => write!(f, "Test"), + Mode::Signer => write!(f, "Signer"), + } + } +} + +impl Mode { + pub fn from_env(mode: Option<&'static str>) -> Self { + match mode { + Some(m) => { + match m { + "TEST" => Self::Test, + "test" => Self::Test, + "SIGNER" => Self::Signer, + "signer" => Self::Signer, + _ => Self::Signer, + } + }, + None => Self::Signer, + } + } +} \ No newline at end of file diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index b850335..190bd31 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -3,7 +3,7 @@ mod conn; mod core; mod periph; -use crate::core::{events::*, config::*}; +use crate::core::{events::*, config::*, mode::Mode}; use crate::periph::led::Led; use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported @@ -17,7 +17,11 @@ use esp_idf_svc::nvs_storage::EspNvsStorage; use embedded_svc::storage::Storage; use embedded_svc::wifi::Wifi; +const MODE: Option<&'static str> = option_env!("MODE"); + fn main() -> Result<()> { + let mode = Mode::from_env(MODE); + println!("MODE? {:?}", mode); // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // or else some patches to the runtime implemented by esp-idf-sys might not link properly. esp_idf_sys::link_patches(); @@ -35,8 +39,13 @@ fn main() -> Result<()> { let wifi = start_wifi_client(default_nvs.clone(), &exist)?; let (tx, rx) = mpsc::channel(); + + let client_id = match mode { + Mode::Signer => "sphinx-1", + Mode::Test => "test-1" + }; // _conn needs to stay in scope or its dropped - let (mqtt, connection) = conn::mqtt::make_client(&exist.broker)?; + let (mqtt, connection) = conn::mqtt::make_client(&exist.broker, client_id)?; let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; // this blocks forever... the "main thread"