Skip to content

Commit

Permalink
Merge pull request #123 from card-io-ecg/ota
Browse files Browse the repository at this point in the history
OTA updates
  • Loading branch information
bugadani committed Sep 27, 2023
2 parents b84c2d5 + d89b65e commit 8e19acd
Show file tree
Hide file tree
Showing 14 changed files with 632 additions and 60 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ log = { version = "0.4.18", default-features = false, features = [
logger = { path = "logger" }
signal-processing = { path = "signal-processing" }
norfs = { git = "https://github.com/card-io-ecg/norfs.git", rev = "eef8221" }
norfs-driver = { git = "https://github.com/card-io-ecg/norfs.git", rev = "eef8221" }
norfs-esp32s3 = { git = "https://github.com/card-io-ecg/norfs.git", rev = "eef8221" }
object-chain = "0.1.3"
bad-server = { path = "bad-server" }
Expand Down Expand Up @@ -120,6 +121,7 @@ embassy-net = { workspace = true }
embassy-sync = { workspace = true }

norfs = { workspace = true }
norfs-driver = { workspace = true }
norfs-esp32s3 = { workspace = true, optional = true, features = [
"critical-section",
] }
Expand All @@ -134,6 +136,7 @@ defmt = { workspace = true, optional = true }
smoltcp = { version = "0.10.0", default-features = false, features = [
"dns-max-server-count-2",
] }
crc = "3.0.1"

[patch.crates-io]
esp32s3-hal = { git = "https://github.com/esp-rs/esp-hal.git", rev = "24edf7bdc4788469a592e2fe6cf2580443101971" }
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn main() {
.expect("Not a valid utf8 string")
.trim();

println!("cargo:rustc-env=COMMIT_HASH={git_hash_str}");
println!("cargo:rustc-env=FW_VERSION={pkg_version}-{git_hash_str}");

#[allow(unused_mut)]
Expand Down
1 change: 1 addition & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
syn = { version = "2.0.15", features = ["full", "extra-traits"] }
esp-idf-part = "0.4"
quote = "1.0.9"
darling = "0.20.1"
proc-macro2 = "1.0.29"
Expand Down
35 changes: 13 additions & 22 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
extern crate proc_macro;

use darling::ast::NestedMeta;
use proc_macro::TokenStream;

use syn::{
parse::{Parse, ParseBuffer},
punctuated::Punctuated,
Token,
};

mod filter;
mod norfs_partition;
mod task;

struct Args {
meta: Vec<NestedMeta>,
}

impl Parse for Args {
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
let meta = Punctuated::<NestedMeta, Token![,]>::parse_terminated(input)?;
Ok(Args {
meta: meta.into_iter().collect(),
})
}
}

/// Declares an async task that can be run by `embassy-executor`. The optional `pool_size` parameter can be used to specify how
/// many concurrent tasks can be spawned (default is 1) for the function.
///
Expand Down Expand Up @@ -57,14 +38,24 @@ impl Parse for Args {
/// ```
#[proc_macro_attribute]
pub fn task(args: TokenStream, item: TokenStream) -> TokenStream {
let args = syn::parse_macro_input!(args as Args);
let args = syn::parse_macro_input!(args as task::Args);
let f = syn::parse_macro_input!(item as syn::ItemFn);

task::run(&args.meta, f).into()
task::run(args, f).into()
}

#[proc_macro]
pub fn designfilt(item: TokenStream) -> TokenStream {
let spec = syn::parse_macro_input!(item as filter::FilterSpec);
filter::run(spec).into()
}

#[proc_macro_attribute]
pub fn partition(args: TokenStream, item: TokenStream) -> TokenStream {
let tokens = item.clone();

let args = syn::parse_macro_input!(args as norfs_partition::Args);
let s = syn::parse_macro_input!(tokens as syn::ItemStruct);

norfs_partition::implement(args, s, item.into()).into()
}
41 changes: 41 additions & 0 deletions macros/src/norfs_partition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use esp_idf_part::PartitionTable;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
parse::{Parse, ParseBuffer},
ItemStruct, LitStr,
};

pub struct Args {
name: String,
}

impl Parse for Args {
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
input
.parse::<LitStr>()
.map(|name| Args { name: name.value() })
}
}

pub fn implement(args: Args, item_struct: ItemStruct, input: TokenStream) -> TokenStream {
let Args { name } = args;

let struct_name = item_struct.ident;

let csv = std::fs::read_to_string("partitions.csv").unwrap();
let table = PartitionTable::try_from_str(csv).unwrap();
let part = table.find(&name).expect("No partition found");

let offset = part.offset() as usize;
let size = part.size() as usize;

quote! {
#input

impl InternalPartition for #struct_name {
const OFFSET: usize = #offset;
const SIZE: usize = #size;
}
}
}
28 changes: 23 additions & 5 deletions macros/src/task.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
use darling::{export::NestedMeta, FromMeta};
use darling::{ast::NestedMeta, FromMeta};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::{parse_quote, Expr, ExprLit, ItemFn, Lit, LitInt, ReturnType, Type};
use syn::{
parse::{Parse, ParseBuffer},
parse_quote,
punctuated::Punctuated,
Expr, ExprLit, ItemFn, Lit, LitInt, ReturnType, Token, Type,
};

pub struct Args {
meta: Vec<NestedMeta>,
}

impl Parse for Args {
fn parse(input: &ParseBuffer) -> syn::Result<Self> {
let meta = Punctuated::<NestedMeta, Token![,]>::parse_terminated(input)?;
Ok(Args {
meta: meta.into_iter().collect(),
})
}
}

#[derive(Debug, FromMeta)]
struct Args {
struct ProcessedArgs {
#[darling(default)]
pool_size: Option<syn::Expr>,
}

pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> TokenStream {
let args = match Args::from_list(args) {
pub fn run(args: Args, f: syn::ItemFn) -> TokenStream {
let args = match ProcessedArgs::from_list(&args.meta) {
Ok(args) => args,
Err(e) => return e.write_errors(),
};
Expand Down
41 changes: 41 additions & 0 deletions src/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ pub mod hardware;
pub mod config;
pub mod drivers;
pub mod initialized;
pub mod ota;
pub mod startup;
pub mod storage;
pub mod utils;
pub mod wifi;

use alloc::boxed::Box;
use embassy_net::tcp::client::TcpClientState;
use esp_backtrace as _;

#[cfg(feature = "esp32s2")]
Expand All @@ -26,10 +29,48 @@ pub use esp32s3_hal as hal;

pub use hardware::*;

use crate::{
board::{
initialized::Board,
wifi::sta::{ConnectionState, Sta},
},
states::display_message,
};

pub struct MiscPins {
pub vbus_detect: VbusDetect,
pub chg_status: ChargerStatus,
}

pub struct HttpClientResources {
pub client_state: TcpClientState<1, 1024, 1024>,
pub rx_buffer: [u8; 1024],
}

impl HttpClientResources {
pub fn new_boxed() -> Box<Self> {
Box::new(Self {
client_state: TcpClientState::new(),
rx_buffer: [0; 1024],
})
}
}

pub async fn wait_for_connection(sta: &Sta, board: &mut Board) -> bool {
debug!("Waiting for network connection");
if sta.connection_state() != ConnectionState::Connected {
while sta.wait_for_state_change().await == ConnectionState::Connecting {
display_message(board, "Connecting...").await;
}

if sta.connection_state() != ConnectionState::Connected {
debug!("No network connection");
return false;
}
}

true
}

pub const DEFAULT_BACKEND_URL: &str = "http://stingray-prime-monkey.ngrok-free.app/";
pub const LOW_BATTERY_PERCENTAGE: u8 = 5;
Loading

0 comments on commit 8e19acd

Please sign in to comment.