Skip to content

Commit

Permalink
pass object to store as reference
Browse files Browse the repository at this point in the history
  • Loading branch information
npmenard committed Jan 8, 2025
1 parent 53637d7 commit 312ff5f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion micro-rdk-ffi/src/ffi/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions micro-rdk-server/esp32/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
Expand All @@ -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(),
)
Expand Down
2 changes: 1 addition & 1 deletion micro-rdk-server/native/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
10 changes: 5 additions & 5 deletions micro-rdk/src/common/conn/viam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down
40 changes: 20 additions & 20 deletions micro-rdk/src/common/credentials_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ impl From<CertificateResponse> for TlsCertificate {
pub trait WifiCredentialStorage {
type Error: Error + Debug + Into<ServerError>;
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<WifiCredentials, Self::Error>;
fn reset_wifi_credentials(&self) -> Result<(), Self::Error>;
}

pub trait RobotConfigurationStorage {
type Error: Error + Debug + Into<ServerError>;
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<RobotCredentials, Self::Error>;
fn reset_robot_credentials(&self) -> Result<(), Self::Error>;

Expand All @@ -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<TlsCertificate, Self::Error>;
fn reset_tls_certificate(&self) -> Result<(), Self::Error>;
}
Expand All @@ -136,7 +136,7 @@ pub trait OtaMetadataStorage {
type Error: Error + Debug + Into<ServerError>;
fn has_ota_metadata(&self) -> bool;
fn get_ota_metadata(&self) -> Result<OtaMetadata, Self::Error>;
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>;
}

Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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)),
)
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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> {
Expand All @@ -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<RobotConfig, Self::Error> {
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion micro-rdk/src/common/ota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl<S: OtaMetadataStorage> OtaService<S> {

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()))?;
Expand Down
5 changes: 3 additions & 2 deletions micro-rdk/src/common/provisioning/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -331,7 +331,8 @@ where
fn set_smart_machine_credentials(&self, body: Bytes) -> Result<Bytes, ServerError> {
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();
Expand Down
18 changes: 12 additions & 6 deletions micro-rdk/src/esp32/nvs_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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(())
}

Expand All @@ -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(())
Expand Down
4 changes: 2 additions & 2 deletions templates/project/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
Expand All @@ -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(),
)
Expand Down

0 comments on commit 312ff5f

Please sign in to comment.