Skip to content

Commit

Permalink
fix(sys|lib): coap_context_set_block_mode() on esp32, update esp-idf-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsastrix committed Jul 2, 2024
1 parent 1fc360f commit 5b47b8f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
14 changes: 7 additions & 7 deletions libcoap-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ client = []
epoll = []

[dependencies]
openssl-sys = {version = "^0.9.74", optional = true}
mbedtls-sys-auto = {version = "^2.26", optional = true}
openssl-sys = { version = "^0.9.74", optional = true }
mbedtls-sys-auto = { version = "^2.26", optional = true }
libc = "^0.2.126"
tinydtls-sys = {version = "^0.2.0", default-features = false, optional = true}
tinydtls-sys = { version = "^0.2.0", default-features = false, optional = true }

[target.'cfg(target_os="espidf")'.dependencies]
esp-idf-sys = { version = "0.34.1"}
esp-idf-sys = { version = "0.35.0" }

[build-dependencies]
bindgen = "^0.63"
bindgen = "0.69.4"
autotools = "^0.2.3"
fs_extra = "^1.2"
pkg-config = "^0.3.24"
regex = "1.10.5"
embuild = { version = "0.31.3", features = ["bindgen", "espidf", "cmake"]}
embuild = { version = "0.32.0", features = ["bindgen", "espidf", "cmake"] }
version-compare = "0.2.0"

[package.metadata.docs.rs]
features = ["dtls", "dtls_backend_openssl", "vendored"]

[[package.metadata.esp-idf-sys.extra_components]]
remote_component = { name = "espressif/coap", version = "4.3.4~3"}
remote_component = { name = "espressif/coap", version = "4.3.4~3" }
bindings_header = "src/wrapper.h"
3 changes: 1 addition & 2 deletions libcoap-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ fn main() {
bindgen_builder = bindgen_builder
.header("src/wrapper.h")
.default_enum_style(EnumVariation::Rust { non_exhaustive: true })
.rustfmt_bindings(false)
// Causes invalid syntax for some reason, so we have to disable it.
.generate_comments(false)
.dynamic_link_require_all(true)
Expand Down Expand Up @@ -579,7 +578,7 @@ fn main() {
// the included headers seem to come from our built version.
// Should be fine though, as we already printed `cargo:rerun-if-changed=src/libcoap/` at the
// start of the file.
bindgen_builder = bindgen_builder.parse_callbacks(Box::new(bindgen::CargoCallbacks));
bindgen_builder = bindgen_builder.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));
}
let bindings = bindgen_builder.generate().expect("unable to generate bindings");

Expand Down
67 changes: 40 additions & 27 deletions libcoap/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,37 @@ use std::sync::Once;
use libc::c_uint;

use libcoap_sys::{
coap_add_resource, coap_bin_const_t, coap_can_exit, coap_context_get_csm_max_message_size,
coap_context_get_csm_timeout, coap_context_get_max_handshake_sessions, coap_context_get_max_idle_sessions,
coap_context_get_session_timeout, coap_context_set_block_mode, coap_context_set_csm_max_message_size,
coap_context_set_csm_timeout, coap_context_set_keepalive, coap_context_set_max_handshake_sessions,
coap_context_set_max_idle_sessions, coap_context_set_psk2, coap_context_set_session_timeout, coap_context_t,
coap_dtls_spsk_info_t, coap_dtls_spsk_t, coap_event_t, coap_free_context, coap_get_app_data, coap_io_process,
coap_new_context, coap_proto_t, coap_register_event_handler, coap_register_response_handler, coap_set_app_data,
COAP_BLOCK_SINGLE_BODY, COAP_BLOCK_USE_LIBCOAP, COAP_DTLS_SPSK_SETUP_VERSION, COAP_IO_WAIT,
coap_startup
coap_add_resource, coap_bin_const_t, COAP_BLOCK_SINGLE_BODY, COAP_BLOCK_USE_LIBCOAP,
coap_can_exit, coap_context_get_csm_max_message_size, coap_context_get_csm_timeout,
coap_context_get_max_handshake_sessions, coap_context_get_max_idle_sessions, coap_context_get_session_timeout,
coap_context_set_block_mode, coap_context_set_csm_max_message_size, coap_context_set_csm_timeout,
coap_context_set_keepalive, coap_context_set_max_handshake_sessions, coap_context_set_max_idle_sessions, coap_context_set_psk2,
coap_context_set_session_timeout, coap_context_t, coap_dtls_spsk_info_t, COAP_DTLS_SPSK_SETUP_VERSION, coap_dtls_spsk_t, coap_event_t,
coap_free_context, coap_get_app_data, coap_io_process, COAP_IO_WAIT, coap_new_context,
coap_proto_t, coap_register_event_handler, coap_register_response_handler, coap_set_app_data, coap_startup,
};

static COAP_STARTUP_ONCE: Once = Once::new();

#[cfg(feature = "dtls")]
use crate::crypto::{dtls_server_id_callback, dtls_server_sni_callback, CoapServerCryptoProvider};
#[cfg(feature = "dtls")]
use crate::crypto::{CoapCryptoProviderResponse, CoapCryptoPskIdentity, CoapCryptoPskInfo};
use crate::event::{event_handler_callback, CoapEventHandler};
use crate::mem::{CoapLendableFfiRcCell, CoapLendableFfiWeakCell, DropInnerExclusively};

use crate::session::CoapSessionCommon;

use crate::session::CoapServerSession;
use crate::session::CoapSession;
#[cfg(feature = "dtls")]
use crate::{
error::{ContextCreationError, EndpointCreationError, IoProcessError},
resource::{CoapResource, UntypedCoapResource},
session::session_response_handler,
};

#[cfg(feature = "dtls")]
use crate::crypto::{CoapServerCryptoProvider, dtls_server_id_callback};
#[cfg(feature = "dtls")]
use crate::crypto::{CoapCryptoProviderResponse, CoapCryptoPskIdentity, CoapCryptoPskInfo};
#[cfg(not(feature = "dtls_tinydtls"))]
use crate::crypto::dtls_server_sni_callback;
use crate::event::{CoapEventHandler, event_handler_callback};
use crate::mem::{CoapLendableFfiRcCell, CoapLendableFfiWeakCell, DropInnerExclusively};
use crate::session::CoapServerSession;
use crate::session::CoapSession;
use crate::session::CoapSessionCommon;
use crate::transport::CoapEndpoint;

static COAP_STARTUP_ONCE: Once = Once::new();

#[derive(Debug)]
struct CoapContextInner<'a> {
/// Reference to the raw context this context wraps around.
Expand Down Expand Up @@ -101,7 +99,9 @@ impl<'a> CoapContext<'a> {
// TODO this should actually be done before calling _any_ libcoap function, not just the
// context initialization. Maybe we need to make sure to call this in other places too
// (e.g. if a resource is initialized before a context is created).
COAP_STARTUP_ONCE.call_once(|| unsafe { coap_startup(); });
COAP_STARTUP_ONCE.call_once(|| unsafe {
coap_startup();
});
// SAFETY: Providing null here is fine, the context will just not be bound to an endpoint
// yet.
let raw_context = unsafe { coap_new_context(std::ptr::null()) };
Expand All @@ -110,7 +110,16 @@ impl<'a> CoapContext<'a> {
}
// SAFETY: We checked that raw_context is not null.
unsafe {
coap_context_set_block_mode(raw_context, (COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY).into());
coap_context_set_block_mode(
raw_context,
// In some versions of libcoap, bindgen infers COAP_BLOCK_USE_LIBCOAP and
// COAP_BLOCK_SINGLE_BODY to be u32, while the function parameter is u8.
// Therefore, we use `try_into()` to convert to the right type, and panic if this is
// not possible (should never happen)
(COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY)
.try_into()
.expect("coap_context_set_block_mode() flags have invalid type for function"),
);
coap_register_response_handler(raw_context, Some(session_response_handler));
}
let inner = CoapLendableFfiRcCell::new(CoapContextInner {
Expand Down Expand Up @@ -200,9 +209,13 @@ impl<'a> CoapContext<'a> {
coap_event_t::COAP_EVENT_XMIT_BLOCK_FAIL => handler.handle_xmit_block_fail(&mut session),
coap_event_t::COAP_EVENT_BAD_PACKET => handler.handle_bad_packet(&mut session),
coap_event_t::COAP_EVENT_MSG_RETRANSMITTED => handler.handle_msg_retransmitted(&mut session),
coap_event_t::COAP_EVENT_OSCORE_DECRYPTION_FAILURE => handler.handle_oscore_decryption_failure(&mut session),
coap_event_t::COAP_EVENT_OSCORE_DECRYPTION_FAILURE => {
handler.handle_oscore_decryption_failure(&mut session)
},
coap_event_t::COAP_EVENT_OSCORE_NOT_ENABLED => handler.handle_oscore_not_enabled(&mut session),
coap_event_t::COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD => handler.handle_oscore_no_protected_payload(&mut session),
coap_event_t::COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD => {
handler.handle_oscore_no_protected_payload(&mut session)
},
coap_event_t::COAP_EVENT_OSCORE_NO_SECURITY => handler.handle_oscore_no_security(&mut session),
coap_event_t::COAP_EVENT_OSCORE_INTERNAL_ERROR => handler.handle_oscore_internal_error(&mut session),
coap_event_t::COAP_EVENT_OSCORE_DECODE_ERROR => handler.handle_oscore_decode_error(&mut session),
Expand Down

0 comments on commit 5b47b8f

Please sign in to comment.