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
use std::time::Duration;
use crate::{
session::{
process_unexpected_response,
request_builder::{builder_base, builder_debug, builder_error, RequestHeaderBuilder},
session_error,
},
AsyncSecureChannel, Session, UARequest,
};
use opcua_core::ResponseMessage;
use opcua_types::{
CallMethodRequest, CallMethodResult, CallRequest, CallResponse, IntegerId, MethodId, NodeId,
ObjectId, StatusCode, TryFromVariant, Variant,
};
#[derive(Debug, Clone)]
/// Calls a list of methods on the server by sending a [`CallRequest`] to the server.
///
/// See OPC UA Part 4 - Services 5.11.2 for complete description of the service and error responses.
pub struct Call {
methods: Vec<CallMethodRequest>,
header: RequestHeaderBuilder,
}
builder_base!(Call);
impl Call {
/// Create a new call to the `Call` service.
pub fn new(session: &Session) -> Self {
Self {
methods: Vec::new(),
header: RequestHeaderBuilder::new_from_session(session),
}
}
/// Construct a new call to the `Call` service, setting header parameters manually.
pub fn new_manual(
session_id: u32,
timeout: Duration,
auth_token: NodeId,
request_handle: IntegerId,
) -> Self {
Self {
methods: Vec::new(),
header: RequestHeaderBuilder::new(session_id, timeout, auth_token, request_handle),
}
}
/// Set the list of methods to call.
pub fn methods_to_call(mut self, methods: Vec<CallMethodRequest>) -> Self {
self.methods = methods;
self
}
/// Add a method to call.
pub fn method(mut self, method: impl Into<CallMethodRequest>) -> Self {
self.methods.push(method.into());
self
}
}
impl UARequest for Call {
type Out = CallResponse;
async fn send<'a>(self, channel: &'a AsyncSecureChannel) -> Result<Self::Out, StatusCode>
where
Self: 'a,
{
if self.methods.is_empty() {
builder_error!(self, "call(), was not supplied with any methods to call");
return Err(StatusCode::BadNothingToDo);
}
builder_debug!(self, "call()");
let cnt = self.methods.len();
let request = CallRequest {
request_header: self.header.header,
methods_to_call: Some(self.methods),
};
let response = channel.send(request, self.header.timeout).await?;
if let ResponseMessage::Call(response) = response {
if let Some(results) = &response.results {
if results.len() != cnt {
builder_error!(
self,
"call(), expecting {cnt} results from the call to the server, got {} results",
results.len()
);
Err(StatusCode::BadUnexpectedError)
} else {
Ok(*response)
}
} else {
builder_error!(
self,
"call(), expecting a result from the call to the server, got nothing"
);
Err(StatusCode::BadUnexpectedError)
}
} else {
Err(process_unexpected_response(response))
}
}
}
impl Session {
/// Calls a list of methods on the server by sending a [`CallRequest`] to the server.
///
/// See OPC UA Part 4 - Services 5.11.2 for complete description of the service and error responses.
///
/// # Arguments
///
/// * `methods` - The method to call.
///
/// # Returns
///
/// * `Ok(Vec<CallMethodResult>)` - A [`CallMethodResult`] for the Method call.
/// * `Err(StatusCode)` - Request failed, [Status code](StatusCode) is the reason for failure.
///
pub async fn call(
&self,
methods: Vec<CallMethodRequest>,
) -> Result<Vec<CallMethodResult>, StatusCode> {
Ok(Call::new(self)
.methods_to_call(methods)
.send(&self.channel)
.await?
.results
.unwrap_or_default())
}
/// Calls a single method on an object on the server by sending a [`CallRequest`] to the server.
///
/// See OPC UA Part 4 - Services 5.11.2 for complete description of the service and error responses.
///
/// # Arguments
///
/// * `method` - The method to call. Note this function takes anything that can be turned into
/// a [`CallMethodRequest`] which includes a ([`NodeId`], [`NodeId`], `Option<Vec<Variant>>`) tuple
/// which refers to the object id, method id, and input arguments respectively.
///
/// # Returns
///
/// * `Ok(CallMethodResult)` - A [`CallMethodResult`] for the Method call.
/// * `Err(StatusCode)` - Request failed, [Status code](StatusCode) is the reason for failure.
///
pub async fn call_one(
&self,
method: impl Into<CallMethodRequest>,
) -> Result<CallMethodResult, StatusCode> {
Ok(self
.call(vec![method.into()])
.await?
.into_iter()
.next()
.unwrap())
}
/// Calls GetMonitoredItems via call_method(), putting a sane interface on the input / output.
///
/// # Arguments
///
/// * `subscription_id` - Server allocated identifier for the subscription to return monitored items for.
///
/// # Returns
///
/// * `Ok((Vec<u32>, Vec<u32>))` - Result for call, consisting a list of (monitored_item_id, client_handle)
/// * `Err(StatusCode)` - Request failed, [Status code](StatusCode) is the reason for failure.
///
pub async fn call_get_monitored_items(
&self,
subscription_id: u32,
) -> Result<(Vec<u32>, Vec<u32>), StatusCode> {
let args = Some(vec![Variant::from(subscription_id)]);
let object_id: NodeId = ObjectId::Server.into();
let method_id: NodeId = MethodId::Server_GetMonitoredItems.into();
let request: CallMethodRequest = (object_id, method_id, args).into();
let response = self.call_one(request).await?;
if let Some(mut result) = response.output_arguments {
if result.len() == 2 {
let server_handles = <Vec<u32>>::try_from_variant(result.remove(0))
.map_err(|_| StatusCode::BadUnexpectedError)?;
let client_handles = <Vec<u32>>::try_from_variant(result.remove(0))
.map_err(|_| StatusCode::BadUnexpectedError)?;
Ok((server_handles, client_handles))
} else {
session_error!(
self,
"Expected a result with 2 args but got {}",
result.len()
);
Err(StatusCode::BadUnexpectedError)
}
} else {
session_error!(self, "Expected output arguments but got null");
Err(StatusCode::BadUnexpectedError)
}
}
}