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
use std::collections::{BTreeMap, BTreeSet};
use zb_core::Endpoint;
use zb_core::short_id::Device;
pub use zb_zdp::SimpleDescriptor;
use zb_zdp::{ActiveEpReq, SimpleDescReq};
use crate::api::Zdp;
use crate::{Error, StatusExt};
/// Trait for discovering active endpoints and simple descriptors.
pub trait Endpoints {
/// Read the active application endpoints from a device.
///
/// # Errors
///
/// Returns an [`Error`] if communication fails, the response is invalid, or the ZDP status is
/// not successful.
fn endpoints(
&self,
device: Device,
) -> impl Future<Output = Result<BTreeSet<Endpoint>, Error>> + Send;
/// Read a simple descriptor for one endpoint.
///
/// A successful response without a descriptor is returned as `Ok(None)`.
///
/// # Errors
///
/// Returns an [`Error`] if communication fails, the response is invalid, or the ZDP status is
/// not successful.
fn descriptor(
&self,
device: Device,
endpoint: Endpoint,
) -> impl Future<Output = Result<Option<SimpleDescriptor>, Error>> + Send;
/// Read simple descriptors for all active endpoints on a device.
///
/// This first calls [`Self::endpoints`] to discover the active endpoint set. If endpoint
/// discovery fails, the outer `Result` contains that error and no descriptor requests are sent.
///
/// If endpoint discovery succeeds, each endpoint receives its own descriptor result in the
/// returned map. This lets callers keep partial descriptor discovery results when one endpoint
/// fails but others succeed.
fn descriptors(
&self,
device: Device,
) -> impl Future<
Output = Result<BTreeMap<Endpoint, Result<Option<SimpleDescriptor>, Error>>, Error>,
> + Send
where
Self: Sync,
{
async move {
let mut results = BTreeMap::new();
for endpoint in self.endpoints(device).await? {
results.insert(endpoint, self.descriptor(device, endpoint).await);
}
Ok(results)
}
}
}
impl<T> Endpoints for T
where
T: Zdp + Sync,
{
async fn endpoints(&self, device: Device) -> Result<BTreeSet<Endpoint>, Error> {
let response = self
.communicate(device, ActiveEpReq::new(device.into()))
.await?
.await?;
response
.status()
.ensure_success()
.map(|()| response.into_active_eps().filter_map(Result::ok).collect())
}
async fn descriptor(
&self,
device: Device,
endpoint: Endpoint,
) -> Result<Option<SimpleDescriptor>, Error> {
let response = self
.communicate(device, SimpleDescReq::new(device.into(), endpoint))
.await?
.await?;
response
.status()
.ensure_success()
.map(|()| response.into_descriptor())
}
}