bacnet-server 0.11.0

BACnet server: APDU dispatch and service handlers
Documentation
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use super::*;

mod acknowledge_alarm;
mod audit_notification;
mod confirmed;
mod confirmed_response;
mod endpoint_responder;
#[cfg(test)]
#[path = "endpoint_shared_runtime_tests.rs"]
mod endpoint_shared_runtime_tests;
mod event_information;
#[cfg(test)]
mod executed;
mod unconfirmed;
#[cfg(test)]
mod unconfirmed_tests;
#[cfg(test)]
pub(crate) use self::{executed::EXECUTED_CONFIRMED, unconfirmed::EXECUTED_UNCONFIRMED};

impl<T: TransportPort + 'static> BACnetServer<T> {
    /// Handle one admitted confirmed request.
    #[allow(clippy::too_many_arguments)]
    async fn handle_admitted_confirmed_request(
        db: &Arc<RwLock<ObjectDatabase>>,
        network: &Arc<NetworkLayer<T>>,
        cov_table: &Arc<RwLock<CovSubscriptionTable>>,
        seg_ack_senders: &Arc<Mutex<HashMap<SegKey, Arc<SegmentedSendHandle>>>>,
        seg_send_permits: &Arc<Semaphore>,
        cov_in_flight: &Arc<Semaphore>,
        server_tsm: &Arc<Mutex<ServerTsm>>,
        notification_transactions: &Arc<NotificationTransactions>,
        device_bindings: &Arc<RwLock<DeviceBindingTable>>,
        comm_state: &Arc<AtomicU8>,
        dcc_timer: &Arc<Mutex<Option<JoinHandle<()>>>>,
        config: &ServerConfig,
        source_mac: &[u8],
        source_network: Option<NpduAddress>,
        req: bacnet_encoding::apdu::ConfirmedRequest,
        reply_tx: Option<tokio::sync::oneshot::Sender<Bytes>>,
    ) {
        enum InitialCovNotification {
            Single(CovSubscription),
            Multiple(Vec<CovSubscription>),
        }

        let invoke_id = req.invoke_id;
        let service_choice = req.service_choice;
        let client_max_apdu = req.max_apdu_length;
        let client_accepts_segmented = req.segmented_response_accepted;
        let client_max_segments = req.max_segments;
        let effective_max_apdu = event_information::limit(client_max_apdu, config.max_apdu_length);
        let device_transmits_segments =
            event_information::can_segment(config.segmentation_supported);
        let segmented_response_available = client_accepts_segmented && device_transmits_segments;
        let (mut written_oids, mut coarse_cov_oids) = (Vec::new(), Vec::new());
        let mut life_safety_cov_changes = Vec::new();
        let mut staging_plans = Vec::new();
        let mut initial_cov_notifications: Vec<InitialCovNotification> = Vec::new();
        let mut accepted_acknowledgment = None;

        let state = comm_state.load(Ordering::Acquire);
        if state == 1
            && service_choice != ConfirmedServiceChoice::DEVICE_COMMUNICATION_CONTROL
            && service_choice != ConfirmedServiceChoice::REINITIALIZE_DEVICE
        {
            debug!(
                service = service_choice.to_raw(),
                "DCC DISABLE: dropping confirmed request"
            );
            return;
        }

        let complex_ack = |ack_buf: BytesMut| -> Apdu {
            Apdu::ComplexAck(ComplexAck {
                segmented: false,
                more_follows: false,
                invoke_id,
                sequence_number: None,
                proposed_window_size: None,
                service_choice,
                service_ack: ack_buf.freeze(),
            })
        };
        let simple_ack = || -> Apdu {
            Apdu::SimpleAck(SimpleAck {
                invoke_id,
                service_choice,
            })
        };

        let mut ack_buf = BytesMut::with_capacity(512);
        let response = match service_choice {
            s if s == ConfirmedServiceChoice::READ_PROPERTY => {
                confirmed_response::read_property_response(db, &req).await
            }
            s if s == ConfirmedServiceChoice::WRITE_PROPERTY => {
                let (result, exact_changes, plans) = {
                    let mut db = db.write().await;
                    let snapshots =
                        crate::life_safety_cov::LifeSafetyCovSnapshots::capture_write_property(
                            &db,
                            &req.service_request,
                        );
                    let result = handlers::handle_write_property(&mut db, &req.service_request);
                    let changes = result
                        .as_ref()
                        .map(|oid| snapshots.changes(&db, std::slice::from_ref(oid)))
                        .unwrap_or_default();
                    let plans = result.as_ref().map_or_else(
                        |_| Vec::new(),
                        |oid| Self::take_staging_plans(&mut db, std::slice::from_ref(oid)),
                    );
                    (result, changes, plans)
                };
                staging_plans.extend(plans);
                match result {
                    Ok(oid) => {
                        written_oids.push(oid);
                        if crate::life_safety_cov::is_life_safety_object(oid) {
                            life_safety_cov_changes = exact_changes;
                        } else {
                            coarse_cov_oids.push(oid);
                        }
                        simple_ack()
                    }
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::READ_PROPERTY_MULTIPLE => {
                let db = db.read().await;
                match handlers::handle_read_property_multiple(
                    &db,
                    &req.service_request,
                    &mut ack_buf,
                ) {
                    Ok(()) => complex_ack(ack_buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::WRITE_PROPERTY_MULTIPLE => {
                let (outcome, exact_changes, plans) = {
                    let mut db = db.write().await;
                    let mut snapshots = crate::life_safety_cov::LifeSafetyCovSnapshots::default();
                    let outcome = handlers::handle_write_property_multiple_detailed(
                        &mut db,
                        &req.service_request,
                        &mut snapshots,
                    );
                    let committed_oids = match &outcome {
                        handlers::WritePropertyMultipleOutcome::Success { committed_oids }
                        | handlers::WritePropertyMultipleOutcome::Error {
                            committed_oids, ..
                        } => committed_oids.as_slice(),
                        handlers::WritePropertyMultipleOutcome::Reject { .. } => &[],
                    };
                    let changes = snapshots.changes(&db, committed_oids);
                    let plans = Self::take_staging_plans(&mut db, committed_oids);
                    (outcome, changes, plans)
                };
                staging_plans.extend(plans);
                let response = match outcome {
                    handlers::WritePropertyMultipleOutcome::Success { committed_oids } => {
                        written_oids = committed_oids;
                        simple_ack()
                    }
                    handlers::WritePropertyMultipleOutcome::Error {
                        error,
                        first_failed_write_attempt,
                        committed_oids,
                    } => {
                        written_oids = committed_oids;
                        let (error_class, error_code) = confirmed_response::error_fields(&error);
                        Apdu::Error(
                            bacnet_services::wpm::WritePropertyMultipleError {
                                error_class,
                                error_code,
                                first_failed_write_attempt,
                            }
                            .to_error_pdu(invoke_id),
                        )
                    }
                    handlers::WritePropertyMultipleOutcome::Reject { reason } => {
                        Apdu::Reject(RejectPdu {
                            invoke_id,
                            reject_reason: reason,
                        })
                    }
                };
                coarse_cov_oids.extend(
                    written_oids
                        .iter()
                        .copied()
                        .filter(|oid| !crate::life_safety_cov::is_life_safety_object(*oid)),
                );
                life_safety_cov_changes = exact_changes;
                response
            }
            s if s == ConfirmedServiceChoice::SUBSCRIBE_COV => {
                let db = db.read().await;
                let mut table = cov_table.write().await;
                match handlers::handle_subscribe_cov_with_initial_endpoint(
                    &mut table,
                    &db,
                    source_mac,
                    source_network.as_ref(),
                    &req.service_request,
                ) {
                    Ok(subscriptions) => {
                        initial_cov_notifications.extend(
                            subscriptions
                                .into_iter()
                                .map(InitialCovNotification::Single),
                        );
                        simple_ack()
                    }
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY => {
                let db = db.read().await;
                let mut table = cov_table.write().await;
                match handlers::handle_subscribe_cov_property_with_initial_endpoint(
                    &mut table,
                    &db,
                    source_mac,
                    source_network.as_ref(),
                    &req.service_request,
                ) {
                    Ok(subscriptions) => {
                        initial_cov_notifications.extend(
                            subscriptions
                                .into_iter()
                                .map(InitialCovNotification::Single),
                        );
                        simple_ack()
                    }
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::CREATE_OBJECT => {
                let result = {
                    let mut db = db.write().await;
                    handlers::handle_create_object(&mut db, &req.service_request, &mut ack_buf)
                };
                match result {
                    Ok(()) => complex_ack(ack_buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::DELETE_OBJECT => {
                let deleted_oid =
                    bacnet_services::object_mgmt::DeleteObjectRequest::decode(&req.service_request)
                        .ok()
                        .map(|r| r.object_identifier);

                let result = {
                    let mut db = db.write().await;
                    handlers::handle_delete_object(&mut db, &req.service_request)
                };
                match result {
                    Ok(()) => {
                        // Clean up COV subscriptions for the deleted object
                        if let Some(oid) = deleted_oid {
                            let mut table = cov_table.write().await;
                            table.remove_for_object(oid);
                        }
                        simple_ack()
                    }
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::DEVICE_COMMUNICATION_CONTROL => {
                match handlers::handle_device_communication_control(
                    &req.service_request,
                    comm_state,
                    &config.dcc_password,
                ) {
                    Ok((_state, duration)) => {
                        if let Some(prev) = dcc_timer.lock().await.take() {
                            prev.abort();
                        }
                        if let Some(minutes) = duration {
                            let comm = Arc::clone(comm_state);
                            let handle = tokio::spawn(async move {
                                tokio::time::sleep(std::time::Duration::from_secs(
                                    minutes as u64 * 60,
                                ))
                                .await;
                                comm.store(0, Ordering::Release);
                                tracing::debug!(
                                    "DCC timer expired after {} min, state reverted to ENABLE",
                                    minutes
                                );
                            });
                            *dcc_timer.lock().await = Some(handle);
                        }
                        simple_ack()
                    }
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::REINITIALIZE_DEVICE => {
                match handlers::handle_reinitialize_device(
                    &req.service_request,
                    &config.reinit_password,
                ) {
                    Ok(()) => simple_ack(),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::GET_EVENT_INFORMATION => {
                event_information::response(
                    db,
                    &req,
                    effective_max_apdu,
                    segmented_response_available,
                )
                .await
            }
            s if s == ConfirmedServiceChoice::ACKNOWLEDGE_ALARM => {
                acknowledge_alarm::response(db, &req, &mut accepted_acknowledgment).await
            }
            s if s == ConfirmedServiceChoice::READ_RANGE => {
                let db = db.read().await;
                match handlers::handle_read_range(&db, &req.service_request, &mut ack_buf) {
                    Ok(()) => complex_ack(ack_buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::ATOMIC_READ_FILE => {
                let db = db.read().await;
                match handlers::handle_atomic_read_file(&db, &req.service_request, &mut ack_buf) {
                    Ok(()) => complex_ack(ack_buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::ATOMIC_WRITE_FILE => {
                let result = {
                    let mut db = db.write().await;
                    handlers::handle_atomic_write_file(&mut db, &req.service_request, &mut ack_buf)
                };
                match result {
                    Ok(()) => complex_ack(ack_buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::ADD_LIST_ELEMENT => {
                let mut db = db.write().await;
                match handlers::handle_add_list_element(&mut db, &req.service_request) {
                    Ok(()) => simple_ack(),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::REMOVE_LIST_ELEMENT => {
                let mut db = db.write().await;
                match handlers::handle_remove_list_element(&mut db, &req.service_request) {
                    Ok(()) => simple_ack(),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::GET_ALARM_SUMMARY => {
                let mut buf = BytesMut::new();
                let db = db.read().await;
                match handlers::handle_get_alarm_summary(&db, &mut buf) {
                    Ok(()) => complex_ack(buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::GET_ENROLLMENT_SUMMARY => {
                let mut buf = BytesMut::new();
                let db = db.read().await;
                match handlers::handle_get_enrollment_summary(&db, &req.service_request, &mut buf) {
                    Ok(()) => complex_ack(buf),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::AUDIT_LOG_QUERY => {
                // Query under the read guard, then release it before ACK
                // construction/encoding and the generic segmentation path.
                let query_result = {
                    let db = db.read().await;
                    handlers::handle_audit_log_query(&db, &req.service_request)
                };
                match query_result {
                    Ok((audit_log, page)) => {
                        let ack = bacnet_services::audit::AuditLogQueryAck {
                            audit_log,
                            records: page.records,
                            no_more_items: page.no_more_items,
                        };
                        match ack.try_encode(&mut ack_buf) {
                            Ok(()) => complex_ack(ack_buf),
                            Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                        }
                    }
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::CONFIRMED_AUDIT_NOTIFICATION => {
                match audit_notification::receive_confirmed_audit_notification(
                    db,
                    config,
                    source_mac,
                    source_network.as_ref(),
                    &req,
                )
                .await
                {
                    Ok(audit_notification::Stored) => simple_ack(),
                    Ok(audit_notification::Duplicate) => return,
                    Err(error) => Self::error_apdu_from_error(invoke_id, service_choice, &error),
                }
            }
            s if s == ConfirmedServiceChoice::CONFIRMED_TEXT_MESSAGE => {
                match handlers::handle_text_message(&req.service_request) {
                    Ok(_msg) => simple_ack(),
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                }
            }
            s if s == ConfirmedServiceChoice::LIFE_SAFETY_OPERATION => {
                let request = bacnet_services::life_safety::LifeSafetyOperationRequest::decode(
                    &req.service_request,
                );
                match request {
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                    Ok(request) => {
                        let validation = handlers::validate_life_safety_operation(request.request);
                        let execution = if let Err(e) = validation {
                            Err(e)
                        } else {
                            let target_exists = match request.object_identifier {
                                Some(oid) => db.read().await.get(&oid).is_some(),
                                None => true,
                            };

                            if !target_exists {
                                Err(handlers::life_safety_error(
                                    ErrorClass::OBJECT,
                                    ErrorCode::UNKNOWN_OBJECT,
                                ))
                            } else {
                                let context = LifeSafetyOperationAuthorizationContext {
                                    source_mac: MacAddr::from_slice(source_mac),
                                    source_network: source_network.clone(),
                                    invoke_id,
                                    request: request.clone(),
                                };
                                let authorized = config
                                    .life_safety_operation_authorizer
                                    .as_ref()
                                    .is_some_and(|authorizer| {
                                        std::panic::catch_unwind(std::panic::AssertUnwindSafe(
                                            || authorizer(&context),
                                        ))
                                        .unwrap_or(false)
                                    });
                                if !authorized {
                                    Err(handlers::life_safety_error(
                                        ErrorClass::SERVICES,
                                        ErrorCode::SERVICE_REQUEST_DENIED,
                                    ))
                                } else {
                                    let mut db = db.write().await;
                                    handlers::handle_life_safety_operation_detailed(
                                        &mut db, &request,
                                    )
                                }
                            }
                        };

                        match execution {
                            Ok(result) => {
                                life_safety_cov_changes.extend(result.cov_changes);
                                simple_ack()
                            }
                            Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                        }
                    }
                }
            }
            s if s == ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY_MULTIPLE => {
                let decoded =
                    bacnet_services::cov_multiple::SubscribeCOVPropertyMultipleRequest::decode(
                        &req.service_request,
                    );
                match decoded {
                    Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                    Ok(request) => {
                        let db = db.read().await;
                        let mut table = cov_table.write().await;
                        match handlers::handle_subscribe_cov_property_multiple_request_endpoint(
                            &mut table,
                            &db,
                            source_mac,
                            source_network.as_ref(),
                            request,
                        ) {
                            Ok(subscriptions) => {
                                if !subscriptions.is_empty() {
                                    initial_cov_notifications
                                        .push(InitialCovNotification::Multiple(subscriptions));
                                }
                                simple_ack()
                            }
                            Err(e) => Self::error_apdu_from_error(invoke_id, service_choice, &e),
                        }
                    }
                }
            }
            _ => {
                debug!(
                    service = service_choice.to_raw(),
                    "Unsupported confirmed service"
                );
                Apdu::Reject(RejectPdu {
                    invoke_id,
                    reject_reason: RejectReason::UNRECOGNIZED_SERVICE,
                })
            }
        };

        Self::execute_staging_plans(
            db,
            network,
            cov_table,
            cov_in_flight,
            server_tsm,
            notification_transactions,
            device_bindings,
            comm_state,
            config,
            staging_plans,
        )
        .await;

        if let Apdu::ComplexAck(ref ack) = response {
            let mut full_buf = BytesMut::new();
            encode_apdu(&mut full_buf, &response).expect("valid APDU encoding");

            if full_buf.len() > effective_max_apdu as usize {
                // Clause 5.4.5.3 CannotSendSegmentedComplexACK reads both
                // sides of the exchange: case (a) — "this device does not
                // support the transmission of segmented messages" — and case
                // (b), the client not accepting one. Either way the response
                // "cannot be sent as one PDU or multiple PDUs" and draws the
                // same Abort; SendSegmentedComplexACK is available only when
                // the device supports transmitting segments (#381).
                if !client_accepts_segmented || !device_transmits_segments {
                    let abort = Apdu::Abort(AbortPdu {
                        sent_by_server: true,
                        invoke_id,
                        abort_reason: AbortReason::SEGMENTATION_NOT_SUPPORTED,
                    });
                    let mut buf = BytesMut::new();
                    encode_apdu(&mut buf, &abort).expect("valid APDU encoding");
                    if let Err(e) = Self::send_confirmed_response_apdu(
                        network,
                        &buf,
                        source_mac,
                        source_network.as_ref(),
                    )
                    .await
                    {
                        warn!(error = %e, "Failed to send Abort for segmentation-not-supported");
                    }
                } else {
                    let network = Arc::clone(network);
                    let seg_ack_senders = Arc::clone(seg_ack_senders);
                    let seg_send_permits = Arc::clone(seg_send_permits);
                    let source_mac = MacAddr::from_slice(source_mac);
                    let service_ack_data = ack.service_ack.clone();
                    tokio::spawn(async move {
                        Self::send_segmented_complex_ack(
                            &network,
                            &seg_ack_senders,
                            &seg_send_permits,
                            &source_mac,
                            source_network.as_ref(),
                            invoke_id,
                            service_choice,
                            &service_ack_data,
                            effective_max_apdu,
                            client_max_segments,
                        )
                        .await;
                    });
                }

                for oid in &written_oids {
                    Self::fire_event_notifications_with_bindings(
                        db,
                        network,
                        comm_state,
                        server_tsm,
                        notification_transactions,
                        device_bindings,
                        oid,
                        config.cov_retry_timeout_ms,
                    )
                    .await;
                }
                Self::fire_post_write_cov_notifications(
                    db,
                    network,
                    cov_table,
                    cov_in_flight,
                    notification_transactions,
                    comm_state,
                    config,
                    &coarse_cov_oids,
                    &life_safety_cov_changes,
                )
                .await;
                for notification in &initial_cov_notifications {
                    match notification {
                        InitialCovNotification::Single(subscription) => {
                            Self::fire_initial_cov_notification(
                                db,
                                network,
                                cov_table,
                                cov_in_flight,
                                notification_transactions,
                                comm_state,
                                config,
                                subscription,
                            )
                            .await;
                        }
                        InitialCovNotification::Multiple(subscriptions) => {
                            Self::fire_initial_cov_notification_multiple(
                                db,
                                network,
                                cov_table,
                                cov_in_flight,
                                notification_transactions,
                                comm_state,
                                config,
                                subscriptions,
                            )
                            .await;
                        }
                    }
                }
                return;
            }
        }

        confirmed_response::send_unsegmented_response(
            network,
            &response,
            source_mac,
            source_network.as_ref(),
            reply_tx,
        )
        .await;

        if let Some(accepted) = accepted_acknowledgment {
            Self::send_acknowledgment_notification_with_bindings(
                db,
                network,
                comm_state,
                server_tsm,
                notification_transactions,
                device_bindings,
                accepted,
                config.cov_retry_timeout_ms,
            )
            .await;
        }

        for oid in &written_oids {
            Self::fire_event_notifications_with_bindings(
                db,
                network,
                comm_state,
                server_tsm,
                notification_transactions,
                device_bindings,
                oid,
                config.cov_retry_timeout_ms,
            )
            .await;
        }

        Self::fire_post_write_cov_notifications(
            db,
            network,
            cov_table,
            cov_in_flight,
            notification_transactions,
            comm_state,
            config,
            &coarse_cov_oids,
            &life_safety_cov_changes,
        )
        .await;

        for notification in &initial_cov_notifications {
            match notification {
                InitialCovNotification::Single(subscription) => {
                    Self::fire_initial_cov_notification(
                        db,
                        network,
                        cov_table,
                        cov_in_flight,
                        notification_transactions,
                        comm_state,
                        config,
                        subscription,
                    )
                    .await;
                }
                InitialCovNotification::Multiple(subscriptions) => {
                    Self::fire_initial_cov_notification_multiple(
                        db,
                        network,
                        cov_table,
                        cov_in_flight,
                        notification_transactions,
                        comm_state,
                        config,
                        subscriptions,
                    )
                    .await;
                }
            }
        }
    }
}