openthread/ot/
srp.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// for now, we allow this for this module because we can't apply it
// specifically to the type `ChangedFlags`, due to a bug in `bitflags!`.
#![allow(missing_docs)]

use crate::ot::DnsTxtEntryIterator;
use crate::prelude_internal::*;

/// Represents the SRP server state.
///
/// Functional equivalent of [`otsys::otSrpServerState`](crate::otsys::otSrpServerState).
#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, num_derive::FromPrimitive)]
#[allow(missing_docs)]
pub enum SrpServerState {
    /// Functional equivalent of
    /// [`otsys::otSrpServerState_OT_SRP_SERVER_STATE_DISABLED`](crate::otsys::otSrpServerState_OT_SRP_SERVER_STATE_DISABLED).
    Disabled = OT_SRP_SERVER_STATE_DISABLED as isize,

    /// Functional equivalent of
    /// [`otsys::otSrpServerState_OT_SRP_SERVER_STATE_RUNNING`](crate::otsys::otSrpServerState_OT_SRP_SERVER_STATE_RUNNING).
    Running = OT_SRP_SERVER_STATE_RUNNING as isize,

    /// Functional equivalent of
    /// [`otsys::otSrpServerState_OT_SRP_SERVER_STATE_STOPPED`](crate::otsys::otSrpServerState_OT_SRP_SERVER_STATE_STOPPED).
    Stopped = OT_SRP_SERVER_STATE_STOPPED as isize,
}

/// Represents the SRP server address mode.
///
/// Functional equivalent of
/// [`otsys::otSrpServerAddressMode`](crate::otsys::otSrpServerAddressMode).
#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, num_derive::FromPrimitive)]
#[allow(missing_docs)]
pub enum SrpServerAddressMode {
    /// Functional equivalent of
    /// [`otsys::otSrpServerAddressMode_OT_SRP_SERVER_ADDRESS_MODE_UNICAST`](crate::otsys::otSrpServerAddressMode_OT_SRP_SERVER_ADDRESS_MODE_UNICAST).
    Unicast = OT_SRP_SERVER_ADDRESS_MODE_UNICAST as isize,

    /// Functional equivalent of
    /// [`otsys::otSrpServerAddressMode_OT_SRP_SERVER_ADDRESS_MODE_ANYCAST`](crate::otsys::otSrpServerAddressMode_OT_SRP_SERVER_ADDRESS_MODE_ANYCAST).
    Anycast = OT_SRP_SERVER_ADDRESS_MODE_ANYCAST as isize,
}

/// Iterates over the available SRP server hosts. See [`SrpServer::srp_server_get_hosts`].
pub struct SrpServerHostIterator<'a, T: SrpServer> {
    prev: Option<&'a SrpServerHost>,
    ot_instance: &'a T,
}

// This cannot be easily derived because T doesn't implement `Clone`,
// so we must implement it manually.
impl<T: SrpServer> Clone for SrpServerHostIterator<'_, T> {
    fn clone(&self) -> Self {
        SrpServerHostIterator { prev: self.prev, ot_instance: self.ot_instance }
    }
}

impl<T: SrpServer> std::fmt::Debug for SrpServerHostIterator<'_, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[")?;
        for item in self.clone() {
            item.fmt(f)?;
            write!(f, ",")?;
        }
        write!(f, "]")
    }
}

impl<'a, T: SrpServer> Iterator for SrpServerHostIterator<'a, T> {
    type Item = &'a SrpServerHost;

    fn next(&mut self) -> Option<Self::Item> {
        self.prev = self.ot_instance.srp_server_next_host(self.prev);
        self.prev
    }
}

/// Iterates over all the available SRP services.
#[derive(Clone)]
pub struct SrpServerServiceIterator<'a> {
    prev: Option<&'a SrpServerService>,
    host: &'a SrpServerHost,
}

impl std::fmt::Debug for SrpServerServiceIterator<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[")?;
        for item in self.clone() {
            item.fmt(f)?;
            write!(f, ",")?;
        }
        write!(f, "]")
    }
}

impl<'a> Iterator for SrpServerServiceIterator<'a> {
    type Item = &'a SrpServerService;

    fn next(&mut self) -> Option<Self::Item> {
        self.prev = self.host.next_service(self.prev);
        self.prev
    }
}

/// This opaque type (only used by reference) represents a SRP host.
///
/// Functional equivalent of [`otsys::otSrpServerHost`](crate::otsys::otSrpServerHost).
#[repr(transparent)]
pub struct SrpServerHost(otSrpServerHost);
impl_ot_castable!(opaque SrpServerHost, otSrpServerHost);

impl std::fmt::Debug for SrpServerHost {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("otSrpServerHost")
            .field("full_name", &self.full_name_cstr())
            .field("addresses", &self.addresses())
            .field("is_deleted", &self.is_deleted())
            .field("services", &self.services())
            .finish()
    }
}

impl SrpServerHost {
    /// Functional equivalent of
    /// [`otsys::otSrpServerHostGetAddresses`](crate::otsys::otSrpServerHostGetAddresses).
    pub fn addresses(&self) -> &[Ip6Address] {
        let mut addresses_len = 0u8;
        unsafe {
            let addresses_ptr =
                otSrpServerHostGetAddresses(self.as_ot_ptr(), &mut addresses_len as *mut u8);

            std::slice::from_raw_parts(addresses_ptr as *const Ip6Address, addresses_len as usize)
        }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerHostMatchesFullName`](crate::otsys::otSrpServerHostMatchesFullName).
    pub fn matches_full_name_cstr(&self, full_name: &CStr) -> bool {
        unsafe { otSrpServerHostMatchesFullName(self.as_ot_ptr(), full_name.as_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerHostGetFullName`](crate::otsys::otSrpServerHostGetFullName).
    pub fn full_name_cstr(&self) -> &CStr {
        unsafe { CStr::from_ptr(otSrpServerHostGetFullName(self.as_ot_ptr())) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerHostIsDeleted`](crate::otsys::otSrpServerHostIsDeleted).
    pub fn is_deleted(&self) -> bool {
        unsafe { otSrpServerHostIsDeleted(self.as_ot_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerHostGetLeaseInfo`](crate::otsys::otSrpServerHostGetLeaseInfo).
    pub fn get_lease_info(&self, lease_info: &mut SrpServerLeaseInfo) {
        unsafe { otSrpServerHostGetLeaseInfo(self.as_ot_ptr(), lease_info.as_ot_mut_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerHostGetNextService`](crate::otsys::otSrpServerHostGetNextService).
    pub fn next_service<'a>(
        &'a self,
        prev: Option<&'a SrpServerService>,
    ) -> Option<&'a SrpServerService> {
        let prev = prev.map(|x| x.as_ot_ptr()).unwrap_or(null());
        unsafe {
            SrpServerService::ref_from_ot_ptr(otSrpServerHostGetNextService(self.as_ot_ptr(), prev))
        }
    }

    /// Returns an iterator over all of the services for this host.
    pub fn services(&self) -> SrpServerServiceIterator<'_> {
        SrpServerServiceIterator { prev: None, host: self }
    }
}

/// Iterates over all the available SRP services subtypes.
#[derive(Clone)]
pub struct SrpServerServiceSubtypeIterator<'a> {
    service: &'a SrpServerService,
    next_i: u16,
}

impl std::fmt::Debug for SrpServerServiceSubtypeIterator<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "[")?;
        for item in self.clone() {
            item.fmt(f)?;
            write!(f, ",")?;
        }
        write!(f, "]")
    }
}

impl<'a> Iterator for SrpServerServiceSubtypeIterator<'a> {
    type Item = &'a CStr;

    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.service.subtype_service_name_at(self.next_i);
        if ret.is_some() {
            self.next_i += 1;
        }
        ret
    }
}

/// This opaque type (only used by reference) represents a SRP service.
///
/// Functional equivalent of [`otsys::otSrpServerService`](crate::otsys::otSrpServerService).
#[repr(transparent)]
pub struct SrpServerService(otSrpServerService);
impl_ot_castable!(opaque SrpServerService, otSrpServerService);

impl std::fmt::Debug for SrpServerService {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_deleted() {
            f.debug_struct("otSrpServerService")
                .field("service_name", &self.service_name_cstr())
                .field("instance_name", &self.instance_name_cstr())
                .field("is_deleted", &self.is_deleted())
                .finish()
        } else {
            f.debug_struct("otSrpServerService")
                .field("service_name", &self.service_name_cstr())
                .field("instance_name", &self.instance_name_cstr())
                .field("is_deleted", &self.is_deleted())
                .field("txt_data", &ascii_dump(self.txt_data()))
                .field("txt_entries", &self.txt_entries().collect::<Vec<_>>())
                .field("port", &self.port())
                .field("priority", &self.priority())
                .field("weight", &self.weight())
                .field("subtypes", &self.subtypes())
                .finish()
        }
    }
}

impl SrpServerService {
    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetPort`](crate::otsys::otSrpServerServiceGetPort).
    pub fn port(&self) -> u16 {
        unsafe { otSrpServerServiceGetPort(self.as_ot_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetPriority`](crate::otsys::otSrpServerServiceGetPriority).
    pub fn priority(&self) -> u16 {
        unsafe { otSrpServerServiceGetPriority(self.as_ot_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceIsDeleted`](crate::otsys::otSrpServerServiceIsDeleted).
    pub fn is_deleted(&self) -> bool {
        unsafe { otSrpServerServiceIsDeleted(self.as_ot_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetLeaseInfo`](crate::otsys::otSrpServerServiceGetLeaseInfo).
    pub fn get_lease_info(&self, lease_info: &mut SrpServerLeaseInfo) {
        unsafe { otSrpServerServiceGetLeaseInfo(self.as_ot_ptr(), lease_info.as_ot_mut_ptr()) }
    }

    /// Returns an iterator over all of the subtypes.
    pub fn subtypes(&self) -> SrpServerServiceSubtypeIterator<'_> {
        SrpServerServiceSubtypeIterator { service: self, next_i: 0 }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetNumberOfSubTypes`](crate::otsys::otSrpServerServiceGetNumberOfSubTypes).
    pub fn number_of_subtypes(&self) -> u16 {
        unsafe { otSrpServerServiceGetNumberOfSubTypes(self.as_ot_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetSubTypeServiceNameAt`](crate::otsys::otSrpServerServiceGetSubTypeServiceNameAt).
    pub fn subtype_service_name_at(&self, i: u16) -> Option<&CStr> {
        unsafe {
            let ptr = otSrpServerServiceGetSubTypeServiceNameAt(self.as_ot_ptr(), i);
            if ptr.is_null() {
                None
            } else {
                Some(CStr::from_ptr(ptr))
            }
        }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetTxtData`](crate::otsys::otSrpServerServiceGetTxtData).
    pub fn txt_data(&self) -> &[u8] {
        let mut txt_data_len = 0u16;
        unsafe {
            let txt_data_ptr =
                otSrpServerServiceGetTxtData(self.as_ot_ptr(), &mut txt_data_len as *mut u16);

            std::slice::from_raw_parts(txt_data_ptr, txt_data_len as usize)
        }
    }

    /// Returns iterator over all the DNS TXT entries.
    pub fn txt_entries(&self) -> DnsTxtEntryIterator<'_> {
        DnsTxtEntryIterator::try_new(self.txt_data()).unwrap()
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetWeight`](crate::otsys::otSrpServerServiceGetWeight).
    pub fn weight(&self) -> u16 {
        unsafe { otSrpServerServiceGetWeight(self.as_ot_ptr()) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetServiceName`](crate::otsys::otSrpServerServiceGetServiceName).
    pub fn service_name_cstr(&self) -> &CStr {
        unsafe { CStr::from_ptr(otSrpServerServiceGetServiceName(self.as_ot_ptr())) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetInstanceName`](crate::otsys::otSrpServerServiceGetInstanceName).
    pub fn instance_name_cstr(&self) -> &CStr {
        unsafe { CStr::from_ptr(otSrpServerServiceGetInstanceName(self.as_ot_ptr())) }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerServiceGetInstanceLabel`](crate::otsys::otSrpServerServiceGetInstanceLabel).
    pub fn instance_label_cstr(&self) -> &CStr {
        unsafe { CStr::from_ptr(otSrpServerServiceGetInstanceLabel(self.as_ot_ptr())) }
    }
}

/// Functional equivalent of
/// [`otsys::otSrpServerParseSubTypeServiceName`](crate::otsys::otSrpServerParseSubTypeServiceName).
pub fn parse_label_from_subtype_service_name(
    subtype_service_name: &CStr,
) -> Result<CString, Error> {
    let mut bytes = [0 as c_char; 256];

    // SAFETY: We are passing in valid pointers with a length one less than the array size.
    Error::from(unsafe {
        otSrpServerParseSubTypeServiceName(
            subtype_service_name.as_ptr(),
            (&mut bytes) as *mut c_char,
            255,
        )
    })
    .into_result()?;

    // SAFETY: `bytes` is guaranteed to be zero terminated because of the size of the array.
    Ok(unsafe { CStr::from_ptr(&bytes as *const c_char) }.to_owned())
}

/// The ID of a SRP service update transaction on the SRP Server.
///
/// This type will panic if dropped without being fed
/// to [`SrpServer::srp_server_handle_service_update_result`].
///
/// Functional equivalent of
/// [`otsys::otSrpServerServiceUpdateId`](crate::otsys::otSrpServerServiceUpdateId).
#[derive(Debug)]
pub struct SrpServerServiceUpdateId(otSrpServerServiceUpdateId);

impl SrpServerServiceUpdateId {
    fn new(x: otSrpServerServiceUpdateId) -> Self {
        Self(x)
    }

    fn take(self) -> otSrpServerServiceUpdateId {
        let ret = self.0;
        core::mem::forget(self);
        ret
    }
}

impl Drop for SrpServerServiceUpdateId {
    fn drop(&mut self) {
        panic!("SrpServerServiceUpdateId dropped without being passed to SrpServer::srp_server_handle_service_update_result");
    }
}

/// Server Methods from the [OpenThread SRP Module][1].
///
/// [1]: https://openthread.io/reference/group/api-srp
pub trait SrpServer {
    /// Functional equivalent of
    /// [`otsys::otSrpServerGetAddressMode`](crate::otsys::otSrpServerGetAddressMode).
    fn srp_server_get_address_mode(&self) -> SrpServerAddressMode;

    /// Functional equivalent of
    /// [`otsys::otSrpServerGetState`](crate::otsys::otSrpServerGetState).
    fn srp_server_get_state(&self) -> SrpServerState;

    /// Functional equivalent of
    /// [`otsys::otSrpServerGetPort`](crate::otsys::otSrpServerGetPort).
    fn srp_server_get_port(&self) -> u16;

    /// Functional equivalent of
    /// [`otsys::otSrpServerSetEnabled`](crate::otsys::otSrpServerSetEnabled).
    fn srp_server_set_enabled(&self, enabled: bool);

    /// Functional equivalent of
    /// [`otsys::otSrpServerSetEnabled`](crate::otsys::otSrpServerSetAutoEnableMode).
    fn srp_server_set_auto_enable_mode(&self, enabled: bool);

    /// Returns true if the SRP server is enabled.
    fn srp_server_is_enabled(&self) -> bool;

    /// Returns true if SRP server auto-enable mode is enabled.
    fn srp_server_is_auto_enable_mode(&self) -> bool;

    /// Returns true if the SRP server is running, false if it is stopped or disabled.
    fn srp_server_is_running(&self) -> bool;

    /// Functional equivalent of
    /// [`otsys::otSrpServerSetDomain`](crate::otsys::otSrpServerSetDomain).
    fn srp_server_set_domain(&self, domain: &CStr) -> Result;

    /// Functional equivalent of
    /// [`otsys::otSrpServerGetDomain`](crate::otsys::otSrpServerGetDomain).
    fn srp_server_get_domain(&self) -> &CStr;

    /// Functional equivalent of
    /// [`otsys::otSrpServerGetResponseCounters`](crate::otsys::otSrpServerGetResponseCounters).
    fn srp_server_get_response_counters(&self) -> &SrpServerResponseCounters;

    /// Functional equivalent of
    /// [`otsys::otSrpServerGetNextHost`](crate::otsys::otSrpServerGetNextHost).
    fn srp_server_next_host<'a>(
        &'a self,
        prev: Option<&'a SrpServerHost>,
    ) -> Option<&'a SrpServerHost>;

    /// Returns an iterator over the SRP hosts.
    fn srp_server_hosts(&self) -> SrpServerHostIterator<'_, Self>
    where
        Self: Sized,
    {
        SrpServerHostIterator { prev: None, ot_instance: self }
    }

    /// Functional equivalent of
    /// [`otsys::otSrpServerHandleServiceUpdateResult`](crate::otsys::otSrpServerHandleServiceUpdateResult).
    fn srp_server_handle_service_update_result(&self, id: SrpServerServiceUpdateId, result: Result);

    /// Functional equivalent of
    /// [`otsys::otSrpServerSetServiceUpdateHandler`](crate::otsys::otSrpServerSetServiceUpdateHandler).
    fn srp_server_set_service_update_fn<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(&'a ot::Instance, SrpServerServiceUpdateId, &'a SrpServerHost, u32) + 'a;
}

impl<T: SrpServer + Boxable> SrpServer for ot::Box<T> {
    fn srp_server_get_address_mode(&self) -> SrpServerAddressMode {
        self.as_ref().srp_server_get_address_mode()
    }

    fn srp_server_get_state(&self) -> SrpServerState {
        self.as_ref().srp_server_get_state()
    }

    fn srp_server_get_port(&self) -> u16 {
        self.as_ref().srp_server_get_port()
    }

    fn srp_server_set_auto_enable_mode(&self, enabled: bool) {
        self.as_ref().srp_server_set_auto_enable_mode(enabled)
    }

    fn srp_server_set_enabled(&self, enabled: bool) {
        self.as_ref().srp_server_set_enabled(enabled)
    }

    fn srp_server_is_enabled(&self) -> bool {
        self.as_ref().srp_server_is_enabled()
    }

    fn srp_server_is_auto_enable_mode(&self) -> bool {
        self.as_ref().srp_server_is_auto_enable_mode()
    }

    fn srp_server_is_running(&self) -> bool {
        self.as_ref().srp_server_is_running()
    }

    fn srp_server_set_domain(&self, domain: &CStr) -> Result {
        self.as_ref().srp_server_set_domain(domain)
    }

    fn srp_server_get_domain(&self) -> &CStr {
        self.as_ref().srp_server_get_domain()
    }

    fn srp_server_get_response_counters(&self) -> &SrpServerResponseCounters {
        self.as_ref().srp_server_get_response_counters()
    }

    fn srp_server_next_host<'a>(
        &'a self,
        prev: Option<&'a SrpServerHost>,
    ) -> Option<&'a SrpServerHost> {
        self.as_ref().srp_server_next_host(prev)
    }

    fn srp_server_handle_service_update_result(
        &self,
        id: SrpServerServiceUpdateId,
        result: Result,
    ) {
        self.as_ref().srp_server_handle_service_update_result(id, result)
    }

    fn srp_server_set_service_update_fn<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(&'a ot::Instance, SrpServerServiceUpdateId, &'a SrpServerHost, u32) + 'a,
    {
        self.as_ref().srp_server_set_service_update_fn(f)
    }
}

impl SrpServer for Instance {
    fn srp_server_get_address_mode(&self) -> SrpServerAddressMode {
        unsafe {
            SrpServerAddressMode::from_u32(otSrpServerGetAddressMode(self.as_ot_ptr())).unwrap()
        }
    }

    fn srp_server_get_state(&self) -> SrpServerState {
        unsafe { SrpServerState::from_u32(otSrpServerGetState(self.as_ot_ptr())).unwrap() }
    }

    fn srp_server_get_port(&self) -> u16 {
        unsafe { otSrpServerGetPort(self.as_ot_ptr()) }
    }

    fn srp_server_set_auto_enable_mode(&self, enabled: bool) {
        unsafe { otSrpServerSetAutoEnableMode(self.as_ot_ptr(), enabled) }
    }

    fn srp_server_set_enabled(&self, enabled: bool) {
        unsafe { otSrpServerSetEnabled(self.as_ot_ptr(), enabled) }
    }

    fn srp_server_is_enabled(&self) -> bool {
        #[allow(non_upper_case_globals)]
        match unsafe { otSrpServerGetState(self.as_ot_ptr()) } {
            OT_SRP_SERVER_STATE_DISABLED => false,
            OT_SRP_SERVER_STATE_RUNNING => true,
            OT_SRP_SERVER_STATE_STOPPED => true,
            _ => panic!("Unexpected value from otSrpServerGetState"),
        }
    }

    fn srp_server_is_auto_enable_mode(&self) -> bool {
        unsafe { otSrpServerIsAutoEnableMode(self.as_ot_ptr()) }
    }

    fn srp_server_is_running(&self) -> bool {
        #[allow(non_upper_case_globals)]
        match unsafe { otSrpServerGetState(self.as_ot_ptr()) } {
            OT_SRP_SERVER_STATE_DISABLED => false,
            OT_SRP_SERVER_STATE_RUNNING => true,
            OT_SRP_SERVER_STATE_STOPPED => false,
            _ => panic!("Unexpected value from otSrpServerGetState"),
        }
    }

    fn srp_server_set_domain(&self, domain: &CStr) -> Result {
        Error::from(unsafe { otSrpServerSetDomain(self.as_ot_ptr(), domain.as_ptr()) }).into()
    }

    fn srp_server_get_domain(&self) -> &CStr {
        unsafe { CStr::from_ptr(otSrpServerGetDomain(self.as_ot_ptr())) }
    }

    fn srp_server_get_response_counters(&self) -> &SrpServerResponseCounters {
        unsafe {
            SrpServerResponseCounters::ref_from_ot_ptr(otSrpServerGetResponseCounters(
                self.as_ot_ptr(),
            ))
            .unwrap()
        }
    }

    fn srp_server_next_host<'a>(
        &'a self,
        prev: Option<&'a SrpServerHost>,
    ) -> Option<&'a SrpServerHost> {
        let prev = prev.map(|x| x.as_ot_ptr()).unwrap_or(null());
        unsafe { SrpServerHost::ref_from_ot_ptr(otSrpServerGetNextHost(self.as_ot_ptr(), prev)) }
    }

    fn srp_server_handle_service_update_result(
        &self,
        id: SrpServerServiceUpdateId,
        result: Result,
    ) {
        unsafe {
            otSrpServerHandleServiceUpdateResult(
                self.as_ot_ptr(),
                id.take(),
                Error::from(result).into(),
            )
        }
    }

    fn srp_server_set_service_update_fn<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(&'a ot::Instance, SrpServerServiceUpdateId, &'a SrpServerHost, u32) + 'a,
    {
        unsafe extern "C" fn _ot_srp_server_service_update_handler<'a, F>(
            id: otSrpServerServiceUpdateId,
            host: *const otSrpServerHost,
            timeout: u32,
            context: *mut ::std::os::raw::c_void,
        ) where
            F: FnMut(SrpServerServiceUpdateId, &'a SrpServerHost, u32) + 'a,
        {
            // Reconstitute a reference to our closure.
            let sender = &mut *(context as *mut F);

            sender(
                SrpServerServiceUpdateId::new(id),
                SrpServerHost::ref_from_ot_ptr(host).unwrap(),
                timeout,
            )
        }

        // This helper func is just to allow us to get the type of the
        // wrapper closure (`f_wrapped`) so we can pass that type
        // as a type argument to `_ot_srp_server_service_update_handler`.
        fn get_service_update_handler<'a, X>(_: &X) -> otSrpServerServiceUpdateHandler
        where
            X: FnMut(SrpServerServiceUpdateId, &'a SrpServerHost, u32) + 'a,
        {
            Some(_ot_srp_server_service_update_handler::<X>)
        }

        let (fn_ptr, fn_box, cb): (_, _, otSrpServerServiceUpdateHandler) = if let Some(mut f) = f {
            // Grab a pointer to our ot::Instance for use below in the wrapper closure.
            let ot_instance_ptr = self.as_ot_ptr();

            // Wrap around `f` with a closure that fills in the `ot_instance` field, which we
            // won't have access to inside of `_ot_srp_server_service_update_handler::<_>`.
            let f_wrapped =
                move |id: SrpServerServiceUpdateId, host: &'a SrpServerHost, timeout: u32| {
                    // SAFETY: This ot::Instance will be passed to the original closure as
                    //         an argument. We know that it is valid because:
                    //         1. It is being called by the instance, so it must still be around.
                    //         2. By design there are no mutable references to the `ot::Instance`
                    //            in existence.
                    let ot_instance =
                        unsafe { ot::Instance::ref_from_ot_ptr(ot_instance_ptr) }.unwrap();
                    f(ot_instance, id, host, timeout)
                };

            // Since we don't have a way to directly refer to the type of the closure `f_wrapped`,
            // we need to use a helper function that can pass the type as a generic argument to
            // `_ot_srp_server_service_update_handler::<X>` as `X`.
            let service_update_handler = get_service_update_handler(&f_wrapped);

            let mut x = Box::new(f_wrapped);

            (
                x.as_mut() as *mut _ as *mut ::std::os::raw::c_void,
                Some(
                    x as Box<
                        dyn FnMut(ot::SrpServerServiceUpdateId, &'a ot::SrpServerHost, u32) + 'a,
                    >,
                ),
                service_update_handler,
            )
        } else {
            (std::ptr::null_mut() as *mut ::std::os::raw::c_void, None, None)
        };

        unsafe {
            otSrpServerSetServiceUpdateHandler(self.as_ot_ptr(), cb, fn_ptr);

            // Make sure our object eventually gets cleaned up.
            // Here we must also transmute our closure to have a 'static lifetime.
            // We need to do this because the borrow checker cannot infer the
            // proper lifetime for the singleton instance backing, but
            // this is guaranteed by the API.
            self.borrow_backing().srp_server_service_update_fn.set(std::mem::transmute::<
                Option<Box<dyn FnMut(SrpServerServiceUpdateId, &'a ot::SrpServerHost, u32) + 'a>>,
                Option<Box<dyn FnMut(SrpServerServiceUpdateId, &ot::SrpServerHost, u32) + 'static>>,
            >(fn_box));
        }
    }
}