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
use crate::handle_manager::PduHandleManager;
use crate::types::pdu_event::{PduEvent, PduEventTarget};
use crate::types::{PduCllHandle, PduModuleHandle, PduUniqueApiTag, PduUniqueCllTag};
use dpdu_api_types::PduEvtData;
use dpdu_api_types::bitflags::PduErrorFlag;
use std::ffi::c_void;
use tracing::{debug, error, trace, warn};
pub(crate) unsafe extern "system-unwind" fn event_callback(
_event_type: PduEvtData,
h_mod: PduModuleHandle,
h_cll: PduCllHandle,
p_cll_tag: *mut c_void,
p_api_tag: *mut c_void,
) {
let api = match PduUniqueApiTag::new(p_api_tag as _) {
Some(api_tag) => PduHandleManager::lookup_api_reference(api_tag),
None => PduHandleManager::get_single_api(),
};
let Some(api) = api else {
error!(
api_tag = p_api_tag as usize,
h_mod, h_cll, "PDUEventCallback: there were no suitable APIs for the event"
);
return;
};
let event_target = PduEventTarget::from_callback(h_mod, h_cll);
let mut events: Vec<PduEvent> = vec![];
loop {
api.modify_suppress_log_options(|options| {
options.get_event_item = PduErrorFlag::INVALID_HANDLE;
});
match api.pdu_get_event_item(&event_target) {
Ok(Some(v)) => events.push(v),
Ok(None) => {
break;
}
Err(err) => {
if event_target.is_logical_link() {
// I'm suppressing this because the ComLogicalLink handle can be destroyed,
// which will give difficult triggers.
//
// This may not be the best solution and I should have a list of deleted
// ComLogicalLinks, but I'll leave it that way.
} else {
error!(
api_tag = api.get_unique_tag(),
h_mod, h_cll, "PDUEventCallback: error reading an event: {err}"
);
}
break;
}
}
}
for event in events {
trace!("PDUEventCallback: {event:?}");
match event.target {
PduEventTarget::System => {
// System event.
let Some(api_event_tx) =
PduHandleManager::lookup_api_event_tx(api.get_unique_tag())
else {
warn!(
h_mod,
"PDUEventCallback: unable to lookup event_tx for the PduApi"
);
continue;
};
if api_event_tx.send(event).is_err() {
error!(
h_mod,
"PDUEventCallback: unable to send D-PDU event to the PduApi"
);
}
}
PduEventTarget::Module(h_mod) => {
// Module event.
let Some(module_event_tx) =
PduHandleManager::lookup_module_event_tx(api.get_unique_tag(), h_mod)
else {
warn!(
h_mod,
"PDUEventCallback: unable to lookup event_tx for the PduVci"
);
continue;
};
if module_event_tx.send(event).is_err() {
error!(
h_mod,
"PDUEventCallback: unable to send D-PDU event to the PduVci"
);
}
}
PduEventTarget::LogicalLink(h_mod, h_cll) => {
let Some(cll_tag) = PduUniqueCllTag::new(p_cll_tag as _) else {
warn!(
"PDUEventCallback: abnormally CLL creation: cll_tag is required when PduEventTarget = ComLogicalLink"
);
continue;
};
if let Some(h_cop) = event.h_cop {
// ComPrimitive event.
let Some(cop_tag) = event.cop_tag else {
warn!(
h_cop,
"PDUEventCallback: API does not provide a primitive tag"
);
continue;
};
let Some(cop_event_tx) =
PduHandleManager::lookup_cop_event_tx(api.get_unique_tag(), cop_tag)
else {
// I'm decreasing log level because the ComPrimitive handle can
// be destroyed, which will give difficult triggers.
//
// This may not be the best solution and I should have a list of deleted
// ComPrimitives, but I'll leave it that way.
debug!(
h_cll,
tag = cll_tag,
"PDUEventCallback: unable to lookup event_tx for the PduComPrimitive"
);
continue;
};
if cop_event_tx.send(event).is_err() {
error!(
h_mod,
h_cll,
h_cop,
"PDUEventCallback: unable to send D-PDU event to the PduComPrimitive"
);
}
} else {
// ComLogicalLink event.
let Some(cll_event_tx) =
PduHandleManager::lookup_cll_event_tx(api.get_unique_tag(), cll_tag)
else {
// I'm decreasing log level because the ComLogicalLink handle can
// be destroyed, which will give difficult triggers.
//
// This may not be the best solution and I should have a list of deleted
// ComLogicalLinks, but I'll leave it that way.
debug!(
h_cll,
tag = cll_tag,
"PDUEventCallback: unable to lookup event_tx for the PduLogicalLink"
);
continue;
};
if cll_event_tx.send(event).is_err() {
error!(
h_mod,
h_cll,
"PDUEventCallback: unable to send D-PDU event to the PduLogicalLink"
);
}
}
}
}
}
}