@@ -47,18 +47,24 @@ pub struct SingleHandlerListener<'a, H> {
47
47
listener : gcoap_listener_t ,
48
48
}
49
49
50
+ /// A combination of the coap_resource_t and gcoap_listener_t structs with only a single resource
51
+ /// (Compared to many resources, this allows easier creation in Rust at the expense of larger
52
+ /// memory consumption and slower lookups in Gcoap).
53
+ ///
54
+ /// A listener `l` can be hooked into the global Gcoap registry using `scope(|x| { x.register(l)
55
+ /// })`.
50
56
impl < ' a , H > SingleHandlerListener < ' a , H >
51
57
where
52
58
H : ' a + Handler
53
59
{
54
- pub fn new ( path : & ' a CStr , handler : & ' a mut H ) -> Self
60
+ pub fn new ( path : & ' a CStr , methods : u32 , handler : & ' a mut H ) -> Self
55
61
{
56
62
SingleHandlerListener {
57
63
_phantom : PhantomData ,
58
64
resource : coap_resource_t {
59
65
path : path. as_ptr ( ) ,
60
66
handler : Some ( Self :: call_handler) ,
61
- methods : 0xff , // A good handler checks anyway
67
+ methods : methods ,
62
68
context : handler as * mut _ as * mut c_void ,
63
69
} ,
64
70
listener : gcoap_listener_t {
@@ -405,77 +411,3 @@ impl PacketBuffer {
405
411
}
406
412
}
407
413
}
408
-
409
- /// A single registerable resource. It wraps the two distinct concepts of a gcoap_listener and a
410
- /// gcoap_resource into a single entity, thus avoiding the issues with a LIMIT present in the shell
411
- /// module as well as the need to sort the resources by path, at the expense of being a wasteful
412
- /// linked list.
413
- pub struct Resource < ' a , R > {
414
- callback : R ,
415
- // This is redundant with the pointer stored in the listener, but correctly captures its
416
- // lifetime.
417
- _path : & ' a CStr ,
418
- resources : [ coap_resource_t ; 1 ] ,
419
- listener : gcoap_listener_t ,
420
- registered : bool , //
421
- }
422
-
423
- impl < ' a , R > Resource < ' a , R >
424
- // R must be Send because it'll be executed in the gcoap thread
425
- where
426
- R : Send + FnMut ( & mut PacketBuffer ) -> isize ,
427
- {
428
- pub fn new ( path : & ' a CStr , methods : u32 , callback : R ) -> Self {
429
- Resource {
430
- callback,
431
- _path : path,
432
- resources : [ coap_resource_t {
433
- path : path. as_ptr ( ) ,
434
- methods : methods,
435
- handler : None ,
436
- context : 0 as * mut _ ,
437
- } ] ,
438
- listener : gcoap_listener_t {
439
- resources : 0 as * const _ ,
440
- resources_len : 1 ,
441
- next : 0 as * mut _ ,
442
- } ,
443
- registered : false ,
444
- }
445
- }
446
-
447
- unsafe extern "C" fn call_handler (
448
- pkt : * mut coap_pkt_t ,
449
- buf : * mut u8 ,
450
- len : usize ,
451
- context : * mut c_void ,
452
- ) -> isize {
453
- let s = context as * mut Resource < ' a , R > ;
454
- let s = & mut * s;
455
- let cb = & mut s. callback ;
456
- let mut pb = PacketBuffer { pkt, buf, len } ;
457
- cb ( & mut pb)
458
- }
459
-
460
- // FIXME: Make sure this stays pinned while registered.
461
- pub fn register ( & mut self ) {
462
- // Set up all the internal links to make the listener valid
463
- self . resources [ 0 ] . handler = Some ( Self :: call_handler) ;
464
- self . resources [ 0 ] . context = self as * mut _ as * mut c_void ;
465
- self . listener . resources = self . resources . as_ptr ( ) ;
466
-
467
- unsafe { gcoap_register_listener ( & mut self . listener ) } ;
468
-
469
- self . registered = true ;
470
- }
471
- }
472
-
473
- /// Resources can not be dropped, for they can not be unregistered from gcoap (there is no
474
- /// gcoap_unregister_listener function).
475
- impl < ' a , R > Drop for Resource < ' a , R > {
476
- fn drop ( & mut self ) {
477
- if self . registered {
478
- panic ! ( "Registered resources must never be dropped" )
479
- }
480
- }
481
- }
0 commit comments