Skip to content

Commit

Permalink
Use fallible CString conversion for MQTT PSK too (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhcel authored Nov 24, 2023
1 parent ca1ae02 commit 41f866d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/mqtt/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'a> TryFrom<&'a MqttClientConfiguration<'a>>
}

#[cfg(all(esp_idf_esp_tls_psk_verification, feature = "alloc"))]
let tls_psk_conf = conf.psk.as_ref().map(|psk| psk.into());
let tls_psk_conf = conf.psk.as_ref().map(|psk| psk.try_into()).transpose()?;
#[cfg(not(all(esp_idf_esp_tls_psk_verification, feature = "alloc")))]
let tls_psk_conf = None;

Expand Down Expand Up @@ -324,7 +324,7 @@ impl<'a> TryFrom<&'a MqttClientConfiguration<'a>>
}

#[cfg(all(esp_idf_esp_tls_psk_verification, feature = "alloc"))]
let tls_psk_conf = conf.psk.as_ref().map(|psk| psk.into());
let tls_psk_conf = conf.psk.as_ref().map(|psk| psk.try_into()).transpose()?;
#[cfg(not(all(esp_idf_esp_tls_psk_verification, feature = "alloc")))]
let tls_psk_conf = None;

Expand Down
14 changes: 10 additions & 4 deletions src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Type safe abstraction for esp-tls
#[cfg(all(esp_idf_esp_tls_psk_verification, feature = "alloc"))]
use core::convert::TryFrom;
use core::fmt::Debug;

use crate::private::cstr::{c_char, CStr};
#[cfg(all(esp_idf_esp_tls_psk_verification, feature = "alloc"))]
use crate::sys::EspError;

#[cfg(all(
esp_idf_comp_esp_tls_enabled,
Expand Down Expand Up @@ -49,16 +53,18 @@ impl Debug for TlsPsk {
}

#[cfg(all(esp_idf_esp_tls_psk_verification, feature = "alloc"))]
impl<'a> From<&'a Psk<'a>> for TlsPsk {
fn from(conf: &Psk) -> Self {
impl<'a> TryFrom<&'a Psk<'a>> for TlsPsk {
type Error = EspError;

fn try_from(conf: &Psk) -> Result<Self, EspError> {
let mut cstrs = crate::private::cstr::RawCstrs::new();
let psk = alloc::boxed::Box::new(crate::hal::sys::psk_hint_key_t {
key: conf.key.as_ptr(),
key_size: conf.key.len(),
hint: cstrs.as_ptr(conf.hint),
hint: cstrs.as_ptr(conf.hint)?,
});

TlsPsk { psk, _cstrs: cstrs }
Ok(TlsPsk { psk, _cstrs: cstrs })
}
}

Expand Down

0 comments on commit 41f866d

Please sign in to comment.