From 168bb4c8bcea59baec8fcf108a0defb80cb8fd3b Mon Sep 17 00:00:00 2001
From: Benjamin Koch <bbbsnowball@gmail.com>
Date: Sun, 11 Jun 2023 05:20:17 +0200
Subject: [PATCH] add build time to program info

---
 firmware/rust1/Cargo.lock         | 148 ++++++++++++++++++++++++++++++
 firmware/rust1/Cargo.toml         |   2 +
 firmware/rust1/src/bin/heizung.rs |  25 +++--
 3 files changed, 169 insertions(+), 6 deletions(-)

diff --git a/firmware/rust1/Cargo.lock b/firmware/rust1/Cargo.lock
index 003f717..2d1304a 100644
--- a/firmware/rust1/Cargo.lock
+++ b/firmware/rust1/Cargo.lock
@@ -42,6 +42,21 @@ dependencies = [
  "memchr",
 ]
 
+[[package]]
+name = "android-tzdata"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
+
+[[package]]
+name = "android_system_properties"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
+dependencies = [
+ "libc",
+]
+
 [[package]]
 name = "arrayvec"
 version = "0.7.2"
@@ -183,6 +198,25 @@ dependencies = [
  "generic-array 0.14.7",
 ]
 
+[[package]]
+name = "build-time"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1219c19fc29b7bfd74b7968b420aff5bc951cf517800176e795d6b2300dd382"
+dependencies = [
+ "chrono",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.15",
+]
+
+[[package]]
+name = "bumpalo"
+version = "3.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
+
 [[package]]
 name = "byte-slice-cast"
 version = "0.3.5"
@@ -219,6 +253,18 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
+[[package]]
+name = "chrono"
+version = "0.4.26"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec837a71355b28f6556dbd569b37b3f363091c0bd4b2e735674521b4c5fd9bc5"
+dependencies = [
+ "android-tzdata",
+ "iana-time-zone",
+ "num-traits",
+ "winapi",
+]
+
 [[package]]
 name = "cipher"
 version = "0.2.5"
@@ -248,6 +294,12 @@ dependencies = [
  "unicode-width",
 ]
 
+[[package]]
+name = "core-foundation-sys"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
+
 [[package]]
 name = "cortex-m"
 version = "0.7.7"
@@ -1100,6 +1152,7 @@ name = "heizung"
 version = "0.1.0"
 dependencies = [
  "bitvec",
+ "build-time",
  "byte-slice-cast 1.2.2",
  "cortex-m",
  "cortex-m-rt",
@@ -1151,6 +1204,29 @@ version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
 
+[[package]]
+name = "iana-time-zone"
+version = "0.1.57"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2fad5b825842d2b38bd206f3e81d6957625fd7f0a361e345c30e01a0ae2dd613"
+dependencies = [
+ "android_system_properties",
+ "core-foundation-sys",
+ "iana-time-zone-haiku",
+ "js-sys",
+ "wasm-bindgen",
+ "windows",
+]
+
+[[package]]
+name = "iana-time-zone-haiku"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
+dependencies = [
+ "cc",
+]
+
 [[package]]
 name = "ident_case"
 version = "1.0.1"
@@ -1199,6 +1275,15 @@ dependencies = [
  "either",
 ]
 
+[[package]]
+name = "js-sys"
+version = "0.3.63"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2f37a4a5928311ac501dee68b3c7613a1037d0edb30c8e5427bd832d55d1b790"
+dependencies = [
+ "wasm-bindgen",
+]
+
 [[package]]
 name = "lalrpop"
 version = "0.19.12"
@@ -2007,6 +2092,60 @@ version = "0.11.0+wasi-snapshot-preview1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
 
+[[package]]
+name = "wasm-bindgen"
+version = "0.2.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5bba0e8cb82ba49ff4e229459ff22a191bbe9a1cb3a341610c9c33efc27ddf73"
+dependencies = [
+ "cfg-if",
+ "wasm-bindgen-macro",
+]
+
+[[package]]
+name = "wasm-bindgen-backend"
+version = "0.2.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19b04bc93f9d6bdee709f6bd2118f57dd6679cf1176a1af464fca3ab0d66d8fb"
+dependencies = [
+ "bumpalo",
+ "log",
+ "once_cell",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.15",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-macro"
+version = "0.2.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "14d6b024f1a526bb0234f52840389927257beb670610081360e5a03c5df9c258"
+dependencies = [
+ "quote",
+ "wasm-bindgen-macro-support",
+]
+
+[[package]]
+name = "wasm-bindgen-macro-support"
+version = "0.2.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e128beba882dd1eb6200e1dc92ae6c5dbaa4311aa7bb211ca035779e5efc39f8"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.15",
+ "wasm-bindgen-backend",
+ "wasm-bindgen-shared",
+]
+
+[[package]]
+name = "wasm-bindgen-shared"
+version = "0.2.86"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed9d5b4305409d1fc9482fee2d7f9bcbf24b3972bf59817ef757e23982242a93"
+
 [[package]]
 name = "winapi"
 version = "0.3.9"
@@ -2038,6 +2177,15 @@ version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 
+[[package]]
+name = "windows"
+version = "0.48.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
+dependencies = [
+ "windows-targets 0.48.0",
+]
+
 [[package]]
 name = "windows-sys"
 version = "0.45.0"
diff --git a/firmware/rust1/Cargo.toml b/firmware/rust1/Cargo.toml
index 9936fa4..766d58e 100644
--- a/firmware/rust1/Cargo.toml
+++ b/firmware/rust1/Cargo.toml
@@ -65,5 +65,7 @@ zerocopy = "0.6.1"
 static_assertions = "1.1.0"
 bitvec = { version = "1", features = ["atomic"], default-features = false }
 
+build-time = "0.1.3"
+
 [profile.release]
 debug = true
diff --git a/firmware/rust1/src/bin/heizung.rs b/firmware/rust1/src/bin/heizung.rs
index 9c00da0..7e65f6b 100644
--- a/firmware/rust1/src/bin/heizung.rs
+++ b/firmware/rust1/src/bin/heizung.rs
@@ -738,16 +738,28 @@ pub static PROGRAM_INFO: [u8; 4096] = {
     // We can fill in some values from info that is provided by Cargo.
     // see https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
 
-    ProgramInfoBuilder::new(PROGRAM_INFO_FLASH_OFFSET, PROGRAM_INFO_TABLE_SIZE, PROGRAM_INFO_COPY_TABLE_SIZE)
+    let mut info =
+        ProgramInfoBuilder::new(PROGRAM_INFO_FLASH_OFFSET, PROGRAM_INFO_TABLE_SIZE, PROGRAM_INFO_COPY_TABLE_SIZE)
         .program_name("subraum-heizung")
         .program_description(env!("CARGO_PKG_DESCRIPTION"))
         .program_version_string(env!("CARGO_PKG_VERSION"))
         .program_url(env!("CARGO_PKG_HOMEPAGE"))
         .board("subraum-heizung base v1.0")
         .sdk_version("Embassy")
-        .program_build_attribute(build_type)
-        //FIXME should be based on what is selected for embassy-rp crate
-        .boot2_name("boot2_w25q080")
+        .program_build_attribute(build_type);
+
+    //FIXME should be based on what is selected for embassy-rp crate
+    info = info.boot2_name("boot2_w25q080");
+
+    let build_timestamp = build_time::build_time_utc!("%s");
+    if build_timestamp.len() == 1 && build_timestamp.as_bytes()[0] == '0' as u8 {
+        // We are building under NixOS or some other system that doesn't let us get the real time so omit it.
+    } else {
+        let local_build_time = build_time::build_time_local!("%Y-%m-%d %H:%M:%S %:z");
+        info = info.program_build_date_string(local_build_time);
+    }
+
+    info = info
         .pins_with_func(PinFunction::UART, &[16, 17])
         .pins_with_func(PinFunction::USB, &[10])
         .pins_with_func(PinFunction::I2C, &[12, 13])
@@ -782,6 +794,7 @@ pub static PROGRAM_INFO: [u8; 4096] = {
         ])
         .program_feature_group(['C', '3'], 0x27a7c59b, "Modbus")
         .feature_utf8(&DEFAULT_MODBUS_CONFIG_STRING.to_slice::<{DEFAULT_MODBUS_CONFIG_STRING.len()}>())
-        .group_done()
-        .build()
+        .group_done();
+
+    info.build()
 };
-- 
GitLab