From 312ff5fa4d9e25e67b702e576b46d166ecc9c3f7 Mon Sep 17 00:00:00 2001 From: Nicolas Menard Date: Wed, 8 Jan 2025 15:47:18 -0500 Subject: [PATCH] pass object to store as reference --- micro-rdk-ffi/src/ffi/runtime.rs | 2 +- micro-rdk-server/esp32/main.rs | 4 +-- micro-rdk-server/native/main.rs | 2 +- micro-rdk/src/common/conn/viam.rs | 10 +++--- micro-rdk/src/common/credentials_storage.rs | 40 ++++++++++----------- micro-rdk/src/common/ota.rs | 2 +- micro-rdk/src/common/provisioning/server.rs | 5 +-- micro-rdk/src/esp32/nvs_storage.rs | 18 ++++++---- templates/project/src/main.rs | 4 +-- 9 files changed, 47 insertions(+), 40 deletions(-) diff --git a/micro-rdk-ffi/src/ffi/runtime.rs b/micro-rdk-ffi/src/ffi/runtime.rs index 260601e94..8f0816c76 100644 --- a/micro-rdk-ffi/src/ffi/runtime.rs +++ b/micro-rdk-ffi/src/ffi/runtime.rs @@ -269,7 +269,7 @@ pub unsafe extern "C" fn viam_server_start(ctx: *mut viam_server_context) -> via None }.expect("has_robot_config set in cfg, but build-time configuration failed to set robot credentials"); ram_storage - .store_robot_credentials(cloud_conf) + .store_robot_credentials(&cloud_conf) .expect("Failed to store cloud config"); if ROBOT_APP_ADDRESS.is_some() { ram_storage diff --git a/micro-rdk-server/esp32/main.rs b/micro-rdk-server/esp32/main.rs index 76d1e8709..e0f7eacc4 100644 --- a/micro-rdk-server/esp32/main.rs +++ b/micro-rdk-server/esp32/main.rs @@ -82,7 +82,7 @@ mod esp32 { if SSID.is_some() && PASS.is_some() { log::info!("Storing static values from build time wifi configuration to NVS"); storage - .store_wifi_credentials(WifiCredentials::new( + .store_wifi_credentials(&WifiCredentials::new( SSID.unwrap().to_string(), PASS.unwrap().to_string(), )) @@ -96,7 +96,7 @@ mod esp32 { log::info!("Storing static values from build time robot configuration to NVS"); storage .store_robot_credentials( - RobotCredentials::new( + &RobotCredentials::new( ROBOT_ID.unwrap().to_string(), ROBOT_SECRET.unwrap().to_string(), ) diff --git a/micro-rdk-server/native/main.rs b/micro-rdk-server/native/main.rs index f52ef9c07..b2aaa4473 100755 --- a/micro-rdk-server/native/main.rs +++ b/micro-rdk-server/native/main.rs @@ -47,7 +47,7 @@ mod native { log::info!("Storing static values from build time robot configuration"); storage .store_robot_credentials( - RobotCredentials::new( + &RobotCredentials::new( ROBOT_ID.unwrap().to_string(), ROBOT_SECRET.unwrap().to_string(), ) diff --git a/micro-rdk/src/common/conn/viam.rs b/micro-rdk/src/common/conn/viam.rs index 07e09d513..7c4621f02 100644 --- a/micro-rdk/src/common/conn/viam.rs +++ b/micro-rdk/src/common/conn/viam.rs @@ -609,7 +609,7 @@ where .await .map(|cert_resp| { let cert: TlsCertificate = cert_resp.into(); - match self.storage.store_tls_certificate(cert.clone()) { + match self.storage.store_tls_certificate(&cert) { Ok(_) => { log::debug!("stored TLS certificate received by app"); } @@ -1290,7 +1290,7 @@ mod tests { secret: "".to_string(), app_address: LOCALHOST_URI.to_owned(), }; - assert!(ram_storage.store_robot_credentials(creds).is_ok()); + assert!(ram_storage.store_robot_credentials(&creds).is_ok()); let mdns = NativeMdns::new("".to_owned(), network.get_ip()); assert!(mdns.is_ok()); @@ -1350,7 +1350,7 @@ mod tests { secret: "".to_string(), app_address: LOCALHOST_URI.to_owned(), }; - assert!(ram_storage.store_robot_credentials(creds).is_ok()); + assert!(ram_storage.store_robot_credentials(&creds).is_ok()); let mdns = NativeMdns::new("".to_owned(), network.get_ip()); assert!(mdns.is_ok()); @@ -1460,7 +1460,7 @@ mod tests { app_address: LOCALHOST_URI.to_owned(), }; - assert!(ram_storage.store_robot_credentials(creds).is_ok()); + assert!(ram_storage.store_robot_credentials(&creds).is_ok()); let mdns = NativeMdns::new("".to_owned(), network.get_ip()); assert!(mdns.is_ok()); @@ -1787,7 +1787,7 @@ mod tests { _ => panic!("oops expected ipv4"), }; - ram_storage.store_robot_credentials(creds).unwrap(); + ram_storage.store_robot_credentials(&creds).unwrap(); let mut a = ViamServerBuilder::new(ram_storage); let mdns = NativeMdns::new("".to_owned(), Ipv4Addr::new(0, 0, 0, 0)).unwrap(); diff --git a/micro-rdk/src/common/credentials_storage.rs b/micro-rdk/src/common/credentials_storage.rs index adba8b74b..c0352fd82 100644 --- a/micro-rdk/src/common/credentials_storage.rs +++ b/micro-rdk/src/common/credentials_storage.rs @@ -103,7 +103,7 @@ impl From for TlsCertificate { pub trait WifiCredentialStorage { type Error: Error + Debug + Into; fn has_wifi_credentials(&self) -> bool; - fn store_wifi_credentials(&self, creds: WifiCredentials) -> Result<(), Self::Error>; + fn store_wifi_credentials(&self, creds: &WifiCredentials) -> Result<(), Self::Error>; fn get_wifi_credentials(&self) -> Result; fn reset_wifi_credentials(&self) -> Result<(), Self::Error>; } @@ -111,7 +111,7 @@ pub trait WifiCredentialStorage { pub trait RobotConfigurationStorage { type Error: Error + Debug + Into; fn has_robot_credentials(&self) -> bool; - fn store_robot_credentials(&self, cfg: CloudConfig) -> Result<(), Self::Error>; + fn store_robot_credentials(&self, cfg: &CloudConfig) -> Result<(), Self::Error>; fn get_robot_credentials(&self) -> Result; fn reset_robot_credentials(&self) -> Result<(), Self::Error>; @@ -126,7 +126,7 @@ pub trait RobotConfigurationStorage { fn reset_robot_configuration(&self) -> Result<(), Self::Error>; fn has_tls_certificate(&self) -> bool; - fn store_tls_certificate(&self, creds: TlsCertificate) -> Result<(), Self::Error>; + fn store_tls_certificate(&self, creds: &TlsCertificate) -> Result<(), Self::Error>; fn get_tls_certificate(&self) -> Result; fn reset_tls_certificate(&self) -> Result<(), Self::Error>; } @@ -136,7 +136,7 @@ pub trait OtaMetadataStorage { type Error: Error + Debug + Into; fn has_ota_metadata(&self) -> bool; fn get_ota_metadata(&self) -> Result; - fn store_ota_metadata(&self, ota_metadata: OtaMetadata) -> Result<(), Self::Error>; + fn store_ota_metadata(&self, ota_metadata: &OtaMetadata) -> Result<(), Self::Error>; fn reset_ota_metadata(&self) -> Result<(), Self::Error>; } @@ -200,7 +200,7 @@ impl OtaMetadataStorage for RAMStorage { let inner_ref = self.0.lock().unwrap(); inner_ref.ota_metadata.is_some() } - fn store_ota_metadata(&self, ota_metadata: OtaMetadata) -> Result<(), Self::Error> { + fn store_ota_metadata(&self, ota_metadata: &OtaMetadata) -> Result<(), Self::Error> { let mut inner_ref = self.0.lock().unwrap(); let _ = inner_ref.ota_metadata.insert(ota_metadata.clone()); Ok(()) @@ -239,10 +239,10 @@ where |val, s| val.or(s.reset_ota_metadata()), ) } - fn store_ota_metadata(&self, ota_metadata: OtaMetadata) -> Result<(), Self::Error> { + fn store_ota_metadata(&self, ota_metadata: &OtaMetadata) -> Result<(), Self::Error> { self.into_iter().fold( Err::<_, Self::Error>(EmptyStorageCollectionError.into()), - |val, s| val.or(s.store_ota_metadata(ota_metadata.clone())), + |val, s| val.or(s.store_ota_metadata(ota_metadata)), ) } } @@ -253,7 +253,7 @@ impl RobotConfigurationStorage for RAMStorage { let inner_ref = self.0.lock().unwrap(); inner_ref.robot_creds.is_some() } - fn store_robot_credentials(&self, cfg: CloudConfig) -> Result<(), Self::Error> { + fn store_robot_credentials(&self, cfg: &CloudConfig) -> Result<(), Self::Error> { let creds: RobotCredentials = cfg.clone().try_into()?; let mut inner_ref = self.0.lock().unwrap(); let _ = inner_ref.robot_creds.insert(creds); @@ -299,7 +299,7 @@ impl RobotConfigurationStorage for RAMStorage { let inner_ref = self.0.lock().unwrap(); inner_ref.tls_cert.is_some() } - fn store_tls_certificate(&self, creds: TlsCertificate) -> Result<(), Self::Error> { + fn store_tls_certificate(&self, creds: &TlsCertificate) -> Result<(), Self::Error> { let mut inner_ref = self.0.lock().unwrap(); let _ = inner_ref.tls_cert.insert(creds.clone()); Ok(()) @@ -379,10 +379,10 @@ where |val, s| val.or(s.store_app_address(uri)), ) } - fn store_tls_certificate(&self, creds: TlsCertificate) -> Result<(), Self::Error> { + fn store_tls_certificate(&self, creds: &TlsCertificate) -> Result<(), Self::Error> { self.into_iter().fold( Err::<_, Self::Error>(EmptyStorageCollectionError.into()), - |val, s| val.or(s.store_tls_certificate(creds.clone())), + |val, s| val.or(s.store_tls_certificate(creds)), ) } fn store_robot_configuration(&self, cfg: &RobotConfig) -> Result<(), Self::Error> { @@ -391,10 +391,10 @@ where |val, s| val.or(s.store_robot_configuration(cfg)), ) } - fn store_robot_credentials(&self, cfg: CloudConfig) -> Result<(), Self::Error> { + fn store_robot_credentials(&self, cfg: &CloudConfig) -> Result<(), Self::Error> { self.into_iter().fold( Err::<_, Self::Error>(EmptyStorageCollectionError.into()), - |val, s| val.or(s.store_robot_credentials(cfg.clone())), + |val, s| val.or(s.store_robot_credentials(cfg)), ) } fn get_robot_configuration(&self) -> Result { @@ -442,7 +442,7 @@ impl WifiCredentialStorage for RAMStorage { let inner_ref = self.0.lock().unwrap(); inner_ref.wifi_creds.is_some() } - fn store_wifi_credentials(&self, creds: WifiCredentials) -> Result<(), Self::Error> { + fn store_wifi_credentials(&self, creds: &WifiCredentials) -> Result<(), Self::Error> { let mut inner_ref = self.0.lock().unwrap(); let _ = inner_ref.wifi_creds.insert(creds.clone()); Ok(()) @@ -470,10 +470,10 @@ where |val, s| val.or(s.get_wifi_credentials()), ) } - fn store_wifi_credentials(&self, creds: WifiCredentials) -> Result<(), Self::Error> { + fn store_wifi_credentials(&self, creds: &WifiCredentials) -> Result<(), Self::Error> { self.into_iter().fold( Err::<_, Self::Error>(EmptyStorageCollectionError.into()), - |val, s| val.or(s.store_wifi_credentials(creds.clone())), + |val, s| val.or(s.store_wifi_credentials(creds)), ) } fn reset_wifi_credentials(&self) -> Result<(), Self::Error> { @@ -536,7 +536,7 @@ mod tests { assert!(!v.has_robot_credentials()); assert!(ram2 - .store_robot_credentials(CloudConfig { + .store_robot_credentials(&CloudConfig { app_address: "http://downloadramstorage.org".to_owned(), id: "ram2".to_owned(), secret: "secret".to_owned() @@ -549,14 +549,14 @@ mod tests { assert_eq!(cred.robot_id, "ram2"); assert!(ram1 - .store_robot_credentials(CloudConfig { + .store_robot_credentials(&CloudConfig { app_address: "http://downloadramstorage.org".to_owned(), id: "ram1".to_owned(), secret: "secret".to_owned() }) .is_ok()); assert!(ram2 - .store_robot_credentials(CloudConfig { + .store_robot_credentials(&CloudConfig { app_address: "http://downloadramstorage.org".to_owned(), id: "ram2".to_owned(), secret: "secret".to_owned() @@ -574,7 +574,7 @@ mod tests { assert!(!v.has_robot_credentials()); assert!(v - .store_robot_credentials(CloudConfig { + .store_robot_credentials(&CloudConfig { app_address: "http://downloadramstorage.org".to_owned(), id: "vec".to_owned(), secret: "secret".to_owned() diff --git a/micro-rdk/src/common/ota.rs b/micro-rdk/src/common/ota.rs index 3f8604a76..b3321af9e 100644 --- a/micro-rdk/src/common/ota.rs +++ b/micro-rdk/src/common/ota.rs @@ -549,7 +549,7 @@ impl OtaService { log::info!("updating firmware metadata in NVS"); self.storage - .store_ota_metadata(OtaMetadata { + .store_ota_metadata(&OtaMetadata { version: self.pending_version.clone(), }) .map_err(|e| OtaError::Other(e.to_string()))?; diff --git a/micro-rdk/src/common/provisioning/server.rs b/micro-rdk/src/common/provisioning/server.rs index 106c8f7b5..a22ec6127 100644 --- a/micro-rdk/src/common/provisioning/server.rs +++ b/micro-rdk/src/common/provisioning/server.rs @@ -256,7 +256,7 @@ where })?; self.storage - .store_wifi_credentials(creds) + .store_wifi_credentials(&creds) .map_err(|e| ServerError::new(GrpcError::RpcInternal, Some(Box::new(e.into()))))?; let resp = SetNetworkCredentialsResponse::default(); @@ -331,7 +331,8 @@ where fn set_smart_machine_credentials(&self, body: Bytes) -> Result { let creds = SetSmartMachineCredentialsRequest::decode(body).map_err(|_| GrpcError::RpcInternal)?; - self.storage.store_robot_credentials(creds.cloud.unwrap())?; + self.storage + .store_robot_credentials(creds.cloud.as_ref().unwrap())?; let resp = SetSmartMachineCredentialsResponse::default(); let len = resp.encoded_len(); diff --git a/micro-rdk/src/esp32/nvs_storage.rs b/micro-rdk/src/esp32/nvs_storage.rs index 92b2ad277..cc3ded6d4 100644 --- a/micro-rdk/src/esp32/nvs_storage.rs +++ b/micro-rdk/src/esp32/nvs_storage.rs @@ -205,7 +205,7 @@ impl OtaMetadataStorage for NVSStorage { let version = self.get_string(NVS_OTA_VERSION_KEY)?; Ok(OtaMetadata { version }) } - fn store_ota_metadata(&self, ota_metadata: OtaMetadata) -> Result<(), Self::Error> { + fn store_ota_metadata(&self, ota_metadata: &OtaMetadata) -> Result<(), Self::Error> { self.set_string(NVS_OTA_VERSION_KEY, &ota_metadata.version) } fn reset_ota_metadata(&self) -> Result<(), Self::Error> { @@ -244,7 +244,7 @@ impl RobotConfigurationStorage for NVSStorage { self.erase_key(NVS_ROBOT_APP_ADDRESS) } - fn store_robot_credentials(&self, cfg: CloudConfig) -> Result<(), Self::Error> { + fn store_robot_credentials(&self, cfg: &CloudConfig) -> Result<(), Self::Error> { self.set_string(NVS_ROBOT_SECRET_KEY, &cfg.secret)?; self.set_string(NVS_ROBOT_ID_KEY, &cfg.id)?; self.set_string(NVS_ROBOT_APP_ADDRESS, &cfg.app_address)?; @@ -290,9 +290,15 @@ impl RobotConfigurationStorage for NVSStorage { }) } - fn store_tls_certificate(&self, creds: TlsCertificate) -> Result<(), Self::Error> { - self.set_blob(NVS_TLS_CERTIFICATE_KEY, Bytes::from(creds.certificate))?; - self.set_blob(NVS_TLS_PRIVATE_KEY_KEY, Bytes::from(creds.private_key))?; + fn store_tls_certificate(&self, creds: &TlsCertificate) -> Result<(), Self::Error> { + self.set_blob( + NVS_TLS_CERTIFICATE_KEY, + Bytes::from(creds.certificate.clone()), + )?; + self.set_blob( + NVS_TLS_PRIVATE_KEY_KEY, + Bytes::from(creds.private_key.clone()), + )?; Ok(()) } @@ -316,7 +322,7 @@ impl WifiCredentialStorage for NVSStorage { Ok(WifiCredentials { ssid, pwd }) } - fn store_wifi_credentials(&self, creds: WifiCredentials) -> Result<(), Self::Error> { + fn store_wifi_credentials(&self, creds: &WifiCredentials) -> Result<(), Self::Error> { self.set_string(NVS_WIFI_SSID_KEY, &creds.ssid)?; self.set_string(NVS_WIFI_PASSWORD_KEY, &creds.pwd)?; Ok(()) diff --git a/templates/project/src/main.rs b/templates/project/src/main.rs index 84e2fec52..7b7b725eb 100644 --- a/templates/project/src/main.rs +++ b/templates/project/src/main.rs @@ -72,7 +72,7 @@ fn main() { if SSID.is_some() && PASS.is_some() { log::info!("Storing static values from build time wifi configuration to NVS"); storage - .store_wifi_credentials(WifiCredentials::new( + .store_wifi_credentials(&WifiCredentials::new( SSID.unwrap().to_string(), PASS.unwrap().to_string(), )) @@ -87,7 +87,7 @@ fn main() { log::info!("Storing static values from build time robot configuration to NVS"); storage .store_robot_credentials( - RobotCredentials::new( + &RobotCredentials::new( ROBOT_ID.unwrap().to_string(), ROBOT_SECRET.unwrap().to_string(), )