Skip to content

Commit 5b47b8f

Browse files
committed
fix(sys|lib): coap_context_set_block_mode() on esp32, update esp-idf-sys
1 parent 1fc360f commit 5b47b8f

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

libcoap-sys/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@ client = []
6565
epoll = []
6666

6767
[dependencies]
68-
openssl-sys = {version = "^0.9.74", optional = true}
69-
mbedtls-sys-auto = {version = "^2.26", optional = true}
68+
openssl-sys = { version = "^0.9.74", optional = true }
69+
mbedtls-sys-auto = { version = "^2.26", optional = true }
7070
libc = "^0.2.126"
71-
tinydtls-sys = {version = "^0.2.0", default-features = false, optional = true}
71+
tinydtls-sys = { version = "^0.2.0", default-features = false, optional = true }
7272

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

7676
[build-dependencies]
77-
bindgen = "^0.63"
77+
bindgen = "0.69.4"
7878
autotools = "^0.2.3"
7979
fs_extra = "^1.2"
8080
pkg-config = "^0.3.24"
8181
regex = "1.10.5"
82-
embuild = { version = "0.31.3", features = ["bindgen", "espidf", "cmake"]}
82+
embuild = { version = "0.32.0", features = ["bindgen", "espidf", "cmake"] }
8383
version-compare = "0.2.0"
8484

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

8888
[[package.metadata.esp-idf-sys.extra_components]]
89-
remote_component = { name = "espressif/coap", version = "4.3.4~3"}
89+
remote_component = { name = "espressif/coap", version = "4.3.4~3" }
9090
bindings_header = "src/wrapper.h"

libcoap-sys/build.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,6 @@ fn main() {
547547
bindgen_builder = bindgen_builder
548548
.header("src/wrapper.h")
549549
.default_enum_style(EnumVariation::Rust { non_exhaustive: true })
550-
.rustfmt_bindings(false)
551550
// Causes invalid syntax for some reason, so we have to disable it.
552551
.generate_comments(false)
553552
.dynamic_link_require_all(true)
@@ -579,7 +578,7 @@ fn main() {
579578
// the included headers seem to come from our built version.
580579
// Should be fine though, as we already printed `cargo:rerun-if-changed=src/libcoap/` at the
581580
// start of the file.
582-
bindgen_builder = bindgen_builder.parse_callbacks(Box::new(bindgen::CargoCallbacks));
581+
bindgen_builder = bindgen_builder.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));
583582
}
584583
let bindings = bindgen_builder.generate().expect("unable to generate bindings");
585584

libcoap/src/context.rs

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,37 @@ use std::sync::Once;
1515
use libc::c_uint;
1616

1717
use libcoap_sys::{
18-
coap_add_resource, coap_bin_const_t, coap_can_exit, coap_context_get_csm_max_message_size,
19-
coap_context_get_csm_timeout, coap_context_get_max_handshake_sessions, coap_context_get_max_idle_sessions,
20-
coap_context_get_session_timeout, coap_context_set_block_mode, coap_context_set_csm_max_message_size,
21-
coap_context_set_csm_timeout, coap_context_set_keepalive, coap_context_set_max_handshake_sessions,
22-
coap_context_set_max_idle_sessions, coap_context_set_psk2, coap_context_set_session_timeout, coap_context_t,
23-
coap_dtls_spsk_info_t, coap_dtls_spsk_t, coap_event_t, coap_free_context, coap_get_app_data, coap_io_process,
24-
coap_new_context, coap_proto_t, coap_register_event_handler, coap_register_response_handler, coap_set_app_data,
25-
COAP_BLOCK_SINGLE_BODY, COAP_BLOCK_USE_LIBCOAP, COAP_DTLS_SPSK_SETUP_VERSION, COAP_IO_WAIT,
26-
coap_startup
18+
coap_add_resource, coap_bin_const_t, COAP_BLOCK_SINGLE_BODY, COAP_BLOCK_USE_LIBCOAP,
19+
coap_can_exit, coap_context_get_csm_max_message_size, coap_context_get_csm_timeout,
20+
coap_context_get_max_handshake_sessions, coap_context_get_max_idle_sessions, coap_context_get_session_timeout,
21+
coap_context_set_block_mode, coap_context_set_csm_max_message_size, coap_context_set_csm_timeout,
22+
coap_context_set_keepalive, coap_context_set_max_handshake_sessions, coap_context_set_max_idle_sessions, coap_context_set_psk2,
23+
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,
24+
coap_free_context, coap_get_app_data, coap_io_process, COAP_IO_WAIT, coap_new_context,
25+
coap_proto_t, coap_register_event_handler, coap_register_response_handler, coap_set_app_data, coap_startup,
2726
};
2827

29-
static COAP_STARTUP_ONCE: Once = Once::new();
30-
31-
#[cfg(feature = "dtls")]
32-
use crate::crypto::{dtls_server_id_callback, dtls_server_sni_callback, CoapServerCryptoProvider};
33-
#[cfg(feature = "dtls")]
34-
use crate::crypto::{CoapCryptoProviderResponse, CoapCryptoPskIdentity, CoapCryptoPskInfo};
35-
use crate::event::{event_handler_callback, CoapEventHandler};
36-
use crate::mem::{CoapLendableFfiRcCell, CoapLendableFfiWeakCell, DropInnerExclusively};
37-
38-
use crate::session::CoapSessionCommon;
39-
40-
use crate::session::CoapServerSession;
41-
use crate::session::CoapSession;
4228
#[cfg(feature = "dtls")]
4329
use crate::{
4430
error::{ContextCreationError, EndpointCreationError, IoProcessError},
4531
resource::{CoapResource, UntypedCoapResource},
4632
session::session_response_handler,
4733
};
48-
34+
#[cfg(feature = "dtls")]
35+
use crate::crypto::{CoapServerCryptoProvider, dtls_server_id_callback};
36+
#[cfg(feature = "dtls")]
37+
use crate::crypto::{CoapCryptoProviderResponse, CoapCryptoPskIdentity, CoapCryptoPskInfo};
38+
#[cfg(not(feature = "dtls_tinydtls"))]
39+
use crate::crypto::dtls_server_sni_callback;
40+
use crate::event::{CoapEventHandler, event_handler_callback};
41+
use crate::mem::{CoapLendableFfiRcCell, CoapLendableFfiWeakCell, DropInnerExclusively};
42+
use crate::session::CoapServerSession;
43+
use crate::session::CoapSession;
44+
use crate::session::CoapSessionCommon;
4945
use crate::transport::CoapEndpoint;
5046

47+
static COAP_STARTUP_ONCE: Once = Once::new();
48+
5149
#[derive(Debug)]
5250
struct CoapContextInner<'a> {
5351
/// Reference to the raw context this context wraps around.
@@ -101,7 +99,9 @@ impl<'a> CoapContext<'a> {
10199
// TODO this should actually be done before calling _any_ libcoap function, not just the
102100
// context initialization. Maybe we need to make sure to call this in other places too
103101
// (e.g. if a resource is initialized before a context is created).
104-
COAP_STARTUP_ONCE.call_once(|| unsafe { coap_startup(); });
102+
COAP_STARTUP_ONCE.call_once(|| unsafe {
103+
coap_startup();
104+
});
105105
// SAFETY: Providing null here is fine, the context will just not be bound to an endpoint
106106
// yet.
107107
let raw_context = unsafe { coap_new_context(std::ptr::null()) };
@@ -110,7 +110,16 @@ impl<'a> CoapContext<'a> {
110110
}
111111
// SAFETY: We checked that raw_context is not null.
112112
unsafe {
113-
coap_context_set_block_mode(raw_context, (COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY).into());
113+
coap_context_set_block_mode(
114+
raw_context,
115+
// In some versions of libcoap, bindgen infers COAP_BLOCK_USE_LIBCOAP and
116+
// COAP_BLOCK_SINGLE_BODY to be u32, while the function parameter is u8.
117+
// Therefore, we use `try_into()` to convert to the right type, and panic if this is
118+
// not possible (should never happen)
119+
(COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY)
120+
.try_into()
121+
.expect("coap_context_set_block_mode() flags have invalid type for function"),
122+
);
114123
coap_register_response_handler(raw_context, Some(session_response_handler));
115124
}
116125
let inner = CoapLendableFfiRcCell::new(CoapContextInner {
@@ -200,9 +209,13 @@ impl<'a> CoapContext<'a> {
200209
coap_event_t::COAP_EVENT_XMIT_BLOCK_FAIL => handler.handle_xmit_block_fail(&mut session),
201210
coap_event_t::COAP_EVENT_BAD_PACKET => handler.handle_bad_packet(&mut session),
202211
coap_event_t::COAP_EVENT_MSG_RETRANSMITTED => handler.handle_msg_retransmitted(&mut session),
203-
coap_event_t::COAP_EVENT_OSCORE_DECRYPTION_FAILURE => handler.handle_oscore_decryption_failure(&mut session),
212+
coap_event_t::COAP_EVENT_OSCORE_DECRYPTION_FAILURE => {
213+
handler.handle_oscore_decryption_failure(&mut session)
214+
},
204215
coap_event_t::COAP_EVENT_OSCORE_NOT_ENABLED => handler.handle_oscore_not_enabled(&mut session),
205-
coap_event_t::COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD => handler.handle_oscore_no_protected_payload(&mut session),
216+
coap_event_t::COAP_EVENT_OSCORE_NO_PROTECTED_PAYLOAD => {
217+
handler.handle_oscore_no_protected_payload(&mut session)
218+
},
206219
coap_event_t::COAP_EVENT_OSCORE_NO_SECURITY => handler.handle_oscore_no_security(&mut session),
207220
coap_event_t::COAP_EVENT_OSCORE_INTERNAL_ERROR => handler.handle_oscore_internal_error(&mut session),
208221
coap_event_t::COAP_EVENT_OSCORE_DECODE_ERROR => handler.handle_oscore_decode_error(&mut session),

0 commit comments

Comments
 (0)