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
use crate::opc_da::{
com_utils::RemoteArray,
errors::{OpcError, OpcResult},
};
use windows_core::BOOL;
/// Item sampling management functionality (OPC DA 3.0).
///
/// Provides methods to control sampling rates and buffering behavior
/// for individual items in an OPC group.
pub trait ItemSamplingMgtTrait {
fn interface(&self) -> OpcResult<&crate::bindings::da::IOPCItemSamplingMgt>;
/// Sets sampling rates for specified items.
///
/// # Arguments
/// * `server_handles` - Array of server item handles
/// * `sampling_rates` - Array of requested sampling rates in milliseconds
///
/// # Returns
/// Tuple containing:
/// - Array of actual sampling rates set by server
/// - Array of per-item error codes
///
/// # Errors
/// Returns E_INVALIDARG if arrays have different lengths
fn set_item_sampling_rate(
&self,
server_handles: &[u32],
sampling_rates: &[u32],
) -> OpcResult<(RemoteArray<u32>, RemoteArray<windows::core::HRESULT>)> {
if server_handles.len() != sampling_rates.len() {
return Err(OpcError::InvalidState(
"server_handles and sampling_rates must have the same length".to_string(),
));
}
let len = server_handles.len().try_into()?;
let mut revised_rates = RemoteArray::new(len);
let mut errors = RemoteArray::new(len);
// SAFETY: Calling COM interface method SetItemSamplingRate with valid pointers and array lengths.
unsafe {
self.interface()?.SetItemSamplingRate(
len,
server_handles.as_ptr(),
sampling_rates.as_ptr(),
revised_rates.as_mut_ptr(),
errors.as_mut_ptr(),
)?;
}
Ok((revised_rates, errors))
}
/// Gets current sampling rates for specified items.
///
/// # Arguments
/// * `server_handles` - Array of server item handles
///
/// # Returns
/// Tuple containing:
/// - Array of current sampling rates in milliseconds
/// - Array of per-item error codes
fn get_item_sampling_rate(
&self,
server_handles: &[u32],
) -> OpcResult<(RemoteArray<u32>, RemoteArray<windows::core::HRESULT>)> {
let len = server_handles.len().try_into()?;
let mut sampling_rates = RemoteArray::new(len);
let mut errors = RemoteArray::new(len);
// SAFETY: Calling COM interface method GetItemSamplingRate with valid pointers and array lengths.
unsafe {
self.interface()?.GetItemSamplingRate(
len,
server_handles.as_ptr(),
sampling_rates.as_mut_ptr(),
errors.as_mut_ptr(),
)?;
}
Ok((sampling_rates, errors))
}
/// Removes custom sampling rates for specified items.
///
/// # Arguments
/// * `server_handles` - Array of server item handles
///
/// # Returns
/// Array of per-item error codes
fn clear_item_sampling_rate(
&self,
server_handles: &[u32],
) -> OpcResult<RemoteArray<windows::core::HRESULT>> {
let len = server_handles.len().try_into()?;
let mut errors = RemoteArray::new(len);
// SAFETY: Calling COM interface method ClearItemSamplingRate with valid pointers and array lengths.
unsafe {
self.interface()?.ClearItemSamplingRate(
len,
server_handles.as_ptr(),
errors.as_mut_ptr(),
)?;
}
Ok(errors)
}
/// Enables or disables data buffering for specified items.
///
/// # Arguments
/// * `server_handles` - Array of server item handles
/// * `enable` - Array of boolean values to enable/disable buffering
///
/// # Returns
/// Array of per-item error codes
///
/// # Errors
/// Returns E_INVALIDARG if arrays have different lengths
fn set_item_buffer_enable(
&self,
server_handles: &[u32],
enable: &[bool],
) -> OpcResult<RemoteArray<windows::core::HRESULT>> {
if server_handles.len() != enable.len() {
return Err(OpcError::InvalidState(
"server_handles and enable must have the same length".to_string(),
));
}
let len = server_handles.len().try_into()?;
let mut errors = RemoteArray::new(len);
let enable_bool: Vec<BOOL> = enable.iter().map(|&v| BOOL::from(v)).collect();
// SAFETY: Calling COM interface method SetItemBufferEnable with valid pointers and array lengths.
unsafe {
self.interface()?.SetItemBufferEnable(
len,
server_handles.as_ptr(),
enable_bool.as_ptr(),
errors.as_mut_ptr(),
)?;
}
Ok(errors)
}
/// Gets current buffer enable states for specified items.
///
/// # Arguments
/// * `server_handles` - Array of server item handles
///
/// # Returns
/// Tuple containing:
/// - Array of current buffer enable states
/// - Array of per-item error codes
fn get_item_buffer_enable(
&self,
server_handles: &[u32],
) -> OpcResult<(
RemoteArray<windows_core::BOOL>,
RemoteArray<windows::core::HRESULT>,
)> {
let len = server_handles.len().try_into()?;
let mut enable = RemoteArray::new(len);
let mut errors = RemoteArray::new(len);
// SAFETY: Calling COM interface method GetItemBufferEnable with valid pointers and array lengths.
unsafe {
self.interface()?.GetItemBufferEnable(
len,
server_handles.as_ptr(),
enable.as_mut_ptr(),
errors.as_mut_ptr(),
)?;
}
Ok((enable, errors))
}
}