conditional compilation w features, try firmware mqtt reconn loop

This commit is contained in:
Evan Feenstra
2022-06-11 13:15:47 -07:00
parent dc816934dd
commit 5a49be61b3
9 changed files with 61 additions and 101 deletions

View File

@@ -2,7 +2,33 @@
These notes were tested for macOS These notes were tested for macOS
### deps ### find your esp GCC
Find the path to your `riscv32-esp-elf-gcc` binary within the `.embuild` dir:
`export CC=$PWD/.embuild/espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc`
### build test
`cargo build --features pingpong`
### build release
`cargo build --release`
### flash
`espflash target/riscv32imc-esp-espidf/debug/sphinx-key`
### monitor
```sh
ls /dev/tty.*
ls /dev/cu.*
espmonitor /dev/tty.usbserial-1420
```
# dependencies
`cd sphinx-key` `cd sphinx-key`
@@ -16,7 +42,7 @@ These notes were tested for macOS
##### python 3.7 or higher is required ##### python 3.7 or higher is required
##### cargo sub-commands ##### cargo sub-commands:
`cargo install cargo-generate` `cargo install cargo-generate`
@@ -26,22 +52,6 @@ These notes were tested for macOS
`cargo install espmonitor` `cargo install espmonitor`
### build
`cargo build`
### flash
`espflash target/riscv32imc-esp-espidf/debug/sphinx-key`
### monitor
```sh
ls /dev/tty.*
ls /dev/cu.*
espmonitor /dev/tty.usbserial-1420
```
### clear NVS ### clear NVS
espflash target/riscv32imc-esp-espidf/debug/clear espflash target/riscv32imc-esp-espidf/debug/clear
@@ -60,30 +70,6 @@ nightly
`cargo build` `cargo build`
### build with CC option
In this new esp-rs repo, find the path to your `riscv32-esp-elf-gcc` binary within the `.embuild` dir:
`export CC=/Users/evanfeenstra/code/sphinx-key/sphinx-key/sphinx-key/.embuild/espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc`
### get std features of vls:
Download a local copy of the `validating-lightning-signer` repo in the parent directory of this repo.
`git clone https://gitlab.com/lightning-signer/validating-lightning-signer.git`
in validating-lightning-signer/vls-protocol-signer/Cargo.toml `[features]`
add: `vls-std = ["vls-protocol/std"]`
### build sphinx-key
then in the sphinx-key dir, with the CC variable set as above:
`cargo build`
and flash using the instructions further above
### to tell sphinx-key where to find the MQTT broker: ### to tell sphinx-key where to find the MQTT broker:
clear the NVS with instructions above if sphinx-key has stale Wifi creds.\ clear the NVS with instructions above if sphinx-key has stale Wifi creds.\

View File

@@ -53,6 +53,7 @@ pub fn start_broker(
let metrics = consolelink::request_metrics(console.clone(), client_id.clone()); let metrics = consolelink::request_metrics(console.clone(), client_id.clone());
if let Some(c) = metrics_to_status(metrics, client_connected) { if let Some(c) = metrics_to_status(metrics, client_connected) {
client_connected = c; client_connected = c;
log::info!("connection status changed to: {}", c);
status_sender status_sender
.send(c) .send(c)
.await .await

View File

@@ -13,10 +13,13 @@ debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z" opt-level = "z"
[features] [features]
default = ["std"]
std = ["sphinx-key-signer"]
pio = ["esp-idf-sys/pio"] pio = ["esp-idf-sys/pio"]
pingpong = []
[dependencies] [dependencies]
sphinx-key-signer = { path = "../signer" } sphinx-key-signer = { path = "../signer", optional = true }
esp-idf-sys = { version = "0.31.5", features = ["binstart"] } esp-idf-sys = { version = "0.31.5", features = ["binstart"] }
embedded-svc = { version = "0.21.2" } embedded-svc = { version = "0.21.2" }
esp-idf-svc = "0.41" esp-idf-svc = "0.41"
@@ -26,8 +29,8 @@ anyhow = {version = "1", features = ["backtrace"]}
log = "0.4" log = "0.4"
url = "2" url = "2"
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"
serde = "1.0.137" serde = { version = "1.0.137", default-features = false }
serde_json = "1.0.81" serde_json = { vercion= "1.0.81", default-features = false }
[build-dependencies] [build-dependencies]
embuild = "0.29" embuild = "0.29"

View File

@@ -1,3 +1,3 @@
cargo build cargo build --features pingpong
espflash target/riscv32imc-esp-espidf/debug/sphinx-key espflash target/riscv32imc-esp-espidf/debug/sphinx-key
espmonitor /dev/tty.usbserial-1420 espmonitor /dev/tty.usbserial-1420

View File

@@ -32,11 +32,13 @@ pub fn make_client(broker: &str, client_id: &str) -> Result<(
}; };
let b = format!("mqtt://{}", broker); let b = format!("mqtt://{}", broker);
println!("===> CONNECT TO {}", b);
// let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?; // let (mut client, mut connection) = EspMqttClient::new_with_conn(b, &conf)?;
let cc = loop { let cc = loop {
match EspMqttClient::new_with_conn(b.clone(), &conf) { let broker_url = b.clone();
info!("===> CONNECT TO {}", &broker_url);
match EspMqttClient::new_with_conn(broker_url, &conf) {
Ok(c_c) => { Ok(c_c) => {
info!("EspMqttClient::new_with_conn finished");
break c_c break c_c
}, },
Err(_) => { Err(_) => {
@@ -65,26 +67,26 @@ pub fn start_listening(
match msg { match msg {
Err(e) => match e.to_string().as_ref() { Err(e) => match e.to_string().as_ref() {
"ESP_FAIL" => { "ESP_FAIL" => {
error!("THE ESP BROKE!"); error!("ESP_FAIL msg!");
}, },
_ => error!("Unknown error: {}", e), _ => error!("Unknown error: {}", e),
}, },
Ok(msg) => { Ok(msg) => {
match msg { match msg {
Event::BeforeConnect => warn!("RECEIVED BEFORE CONNECT MESSAGE"), Event::BeforeConnect => info!("RECEIVED BEFORE CONNECT MESSAGE"),
Event::Connected(flag) => { Event::Connected(flag) => {
if flag { if flag {
warn!("RECEIVED CONNECTED = TRUE MESSAGE"); info!("RECEIVED CONNECTED = TRUE MESSAGE");
} else { } else {
warn!("RECEIVED CONNECTED = FALSE MESSAGE"); info!("RECEIVED CONNECTED = FALSE MESSAGE");
} }
}, },
Event::Disconnected => warn!("RECEIVED DISCONNECTION MESSAGE"), Event::Disconnected => warn!("RECEIVED DISCONNECTION MESSAGE"),
Event::Subscribed(_mes_id) => warn!("RECEIVED SUBSCRIBED MESSAGE"), Event::Subscribed(_mes_id) => info!("RECEIVED SUBSCRIBED MESSAGE"),
Event::Unsubscribed(_mes_id) => warn!("RECEIVED UNSUBSCRIBED MESSAGE"), Event::Unsubscribed(_mes_id) => info!("RECEIVED UNSUBSCRIBED MESSAGE"),
Event::Published(_mes_id) => warn!("RECEIVED PUBLISHED MESSAGE"), Event::Published(_mes_id) => info!("RECEIVED PUBLISHED MESSAGE"),
Event::Received(msg) => tx.send(msg.data().to_vec()).expect("could send to TX"), Event::Received(msg) => tx.send(msg.data().to_vec()).expect("could send to TX"),
Event::Deleted(_mes_id) => warn!("RECEIVED DELETED MESSAGE"), Event::Deleted(_mes_id) => info!("RECEIVED DELETED MESSAGE"),
} }
}, },
} }

View File

@@ -1,6 +1,6 @@
use crate::conn::mqtt::RETURN_TOPIC; use crate::conn::mqtt::RETURN_TOPIC;
use sphinx_key_signer::{self, InitResponse, PubKey}; use sphinx_key_signer::{self, InitResponse, PubKey};
use std::sync::{mpsc}; use std::sync::mpsc;
use esp_idf_sys; use esp_idf_sys;
use embedded_svc::httpd::Result; use embedded_svc::httpd::Result;
@@ -10,6 +10,7 @@ use esp_idf_svc::mqtt::client::*;
use esp_idf_sys::EspError; use esp_idf_sys::EspError;
use log::*; use log::*;
#[cfg(not(feature = "pingpong"))]
pub fn make_event_loop(mut mqtt: EspMqttClient<ConnState<MessageImpl, EspError>>, rx: mpsc::Receiver<Vec<u8>>) -> Result<()> { pub fn make_event_loop(mut mqtt: EspMqttClient<ConnState<MessageImpl, EspError>>, rx: mpsc::Receiver<Vec<u8>>) -> Result<()> {
// initialize the RootHandler // initialize the RootHandler
@@ -29,7 +30,8 @@ pub fn make_event_loop(mut mqtt: EspMqttClient<ConnState<MessageImpl, EspError>>
Ok(()) Ok(())
} }
pub fn make_test_event_loop(mut mqtt: EspMqttClient<ConnState<MessageImpl, EspError>>, rx: mpsc::Receiver<Vec<u8>>) -> Result<()> { #[cfg(feature = "pingpong")]
pub fn make_event_loop(mut mqtt: EspMqttClient<ConnState<MessageImpl, EspError>>, rx: mpsc::Receiver<Vec<u8>>) -> Result<()> {
info!("About to subscribe to the mpsc channel"); info!("About to subscribe to the mpsc channel");
while let Ok(msg_bytes) = rx.recv() { while let Ok(msg_bytes) = rx.recv() {

View File

@@ -1,3 +1,2 @@
pub mod events; pub mod events;
pub mod config; pub mod config;
pub mod mode;

View File

@@ -1,32 +0,0 @@
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,
}
}
}

View File

@@ -3,7 +3,7 @@ mod conn;
mod core; mod core;
mod periph; mod periph;
use crate::core::{events::*, config::*, mode::Mode}; use crate::core::{events::*, config::*};
use crate::periph::led::Led; use crate::periph::led::Led;
use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
@@ -17,11 +17,14 @@ use esp_idf_svc::nvs_storage::EspNvsStorage;
use embedded_svc::storage::Storage; use embedded_svc::storage::Storage;
use embedded_svc::wifi::Wifi; use embedded_svc::wifi::Wifi;
const MODE: Option<&'static str> = option_env!("MODE"); #[cfg(not(feature = "pingpong"))]
const CLIENT_ID: &str = "sphinx-1";
#[cfg(feature = "pingpong")]
const CLIENT_ID: &str = "test-1";
fn main() -> Result<()> { 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, // 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. // or else some patches to the runtime implemented by esp-idf-sys might not link properly.
esp_idf_sys::link_patches(); esp_idf_sys::link_patches();
@@ -40,17 +43,13 @@ fn main() -> Result<()> {
let (tx, rx) = mpsc::channel(); 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 // _conn needs to stay in scope or its dropped
let (mqtt, connection) = conn::mqtt::make_client(&exist.broker, client_id)?; let (mqtt, connection) = conn::mqtt::make_client(&exist.broker, CLIENT_ID)?;
let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;
// this blocks forever... the "main thread" // this blocks forever... the "main thread"
log::info!(">>>>>>>>>>> blocking forever..."); log::info!(">>>>>>>>>>> blocking forever...");
make_test_event_loop(mqtt_client, rx)?; make_event_loop(mqtt_client, rx)?;
let mut blue = Led::new(0x000001, 100); let mut blue = Led::new(0x000001, 100);
println!("{:?}", wifi.get_status()); println!("{:?}", wifi.get_status());