@@ -15,39 +15,37 @@ use std::sync::Once;
15
15
use libc:: c_uint;
16
16
17
17
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,
27
26
} ;
28
27
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 ;
42
28
#[ cfg( feature = "dtls" ) ]
43
29
use crate :: {
44
30
error:: { ContextCreationError , EndpointCreationError , IoProcessError } ,
45
31
resource:: { CoapResource , UntypedCoapResource } ,
46
32
session:: session_response_handler,
47
33
} ;
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 ;
49
45
use crate :: transport:: CoapEndpoint ;
50
46
47
+ static COAP_STARTUP_ONCE : Once = Once :: new ( ) ;
48
+
51
49
#[ derive( Debug ) ]
52
50
struct CoapContextInner < ' a > {
53
51
/// Reference to the raw context this context wraps around.
@@ -101,7 +99,9 @@ impl<'a> CoapContext<'a> {
101
99
// TODO this should actually be done before calling _any_ libcoap function, not just the
102
100
// context initialization. Maybe we need to make sure to call this in other places too
103
101
// (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
+ } ) ;
105
105
// SAFETY: Providing null here is fine, the context will just not be bound to an endpoint
106
106
// yet.
107
107
let raw_context = unsafe { coap_new_context ( std:: ptr:: null ( ) ) } ;
@@ -110,7 +110,16 @@ impl<'a> CoapContext<'a> {
110
110
}
111
111
// SAFETY: We checked that raw_context is not null.
112
112
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
+ ) ;
114
123
coap_register_response_handler ( raw_context, Some ( session_response_handler) ) ;
115
124
}
116
125
let inner = CoapLendableFfiRcCell :: new ( CoapContextInner {
@@ -200,9 +209,13 @@ impl<'a> CoapContext<'a> {
200
209
coap_event_t:: COAP_EVENT_XMIT_BLOCK_FAIL => handler. handle_xmit_block_fail ( & mut session) ,
201
210
coap_event_t:: COAP_EVENT_BAD_PACKET => handler. handle_bad_packet ( & mut session) ,
202
211
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
+ } ,
204
215
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
+ } ,
206
219
coap_event_t:: COAP_EVENT_OSCORE_NO_SECURITY => handler. handle_oscore_no_security ( & mut session) ,
207
220
coap_event_t:: COAP_EVENT_OSCORE_INTERNAL_ERROR => handler. handle_oscore_internal_error ( & mut session) ,
208
221
coap_event_t:: COAP_EVENT_OSCORE_DECODE_ERROR => handler. handle_oscore_decode_error ( & mut session) ,
0 commit comments