-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathspeaker.rs
More file actions
206 lines (176 loc) · 7.3 KB
/
speaker.rs
File metadata and controls
206 lines (176 loc) · 7.3 KB
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
/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! An example Matter device that implements a Speaker device over Ethernet.
//! Demonstrates how to make use of the `rs_matter::import` macro for `LevelControl`.
use core::pin::pin;
use std::net::UdpSocket;
use embassy_futures::select::{select, select4};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use rs_matter::dm::clusters::desc::{self, ClusterHandler as _};
use rs_matter::dm::clusters::level_control::{
self, test::LevelControlDeviceLogic, AttributeDefaults, LevelControlHandler, LevelControlHooks,
OptionsBitmap,
};
use rs_matter::dm::clusters::net_comm::NetworkType;
use rs_matter::dm::clusters::on_off::{self, test::TestOnOffDeviceLogic, OnOffHandler, OnOffHooks};
use rs_matter::dm::devices::test::{TEST_DEV_ATT, TEST_DEV_COMM, TEST_DEV_DET};
use rs_matter::dm::devices::DEV_TYPE_SMART_SPEAKER;
use rs_matter::dm::endpoints;
use rs_matter::dm::networks::unix::UnixNetifs;
use rs_matter::dm::subscriptions::DefaultSubscriptions;
use rs_matter::dm::{
AsyncHandler, AsyncMetadata, DataModel, Dataver, EmptyHandler, Endpoint, EpClMatcher, Node,
};
use rs_matter::error::Error;
use rs_matter::pairing::qr::QrTextType;
use rs_matter::pairing::DiscoveryCapabilities;
use rs_matter::persist::{Psm, NO_NETWORKS};
use rs_matter::respond::DefaultResponder;
use rs_matter::sc::pake::MAX_COMM_WINDOW_TIMEOUT_SECS;
use rs_matter::tlv::Nullable;
use rs_matter::transport::MATTER_SOCKET_BIND_ADDR;
use rs_matter::utils::select::Coalesce;
use rs_matter::utils::storage::pooled::PooledBuffers;
use rs_matter::{clusters, devices, Matter, MATTER_PORT};
#[path = "../common/mdns.rs"]
mod mdns;
fn main() -> Result<(), Error> {
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);
// Create the Matter object
let matter = Matter::new_default(&TEST_DEV_DET, TEST_DEV_COMM, &TEST_DEV_ATT, MATTER_PORT);
// Need to call this once
matter.initialize_transport_buffers()?;
// Create the transport buffers
let buffers = PooledBuffers::<10, NoopRawMutex, _>::new(0);
// Create the subscriptions
let subscriptions = DefaultSubscriptions::new();
// OnOff cluster setup
let on_off_handler = on_off::OnOffHandler::new(
Dataver::new_rand(matter.rand()),
1,
TestOnOffDeviceLogic::new(true),
);
// LevelControl cluster setup
let level_control_handler = LevelControlHandler::new(
Dataver::new_rand(matter.rand()),
1,
LevelControlDeviceLogic::new(),
AttributeDefaults {
on_level: Nullable::some(42),
options: OptionsBitmap::from_bits(OptionsBitmap::EXECUTE_IF_OFF.bits()).unwrap(),
on_off_transition_time: 0,
on_transition_time: Nullable::none(),
off_transition_time: Nullable::none(),
default_move_rate: Nullable::none(),
},
);
// Cluster wiring, validation and initialisation
on_off_handler.init(Some(&level_control_handler));
level_control_handler.init(Some(&on_off_handler));
// Create the Data Model instance
let dm = DataModel::new(
&matter,
&buffers,
&subscriptions,
dm_handler(&matter, &on_off_handler, &level_control_handler),
);
// Create a default responder capable of handling up to 3 subscriptions
// All other subscription requests will be turned down with "resource exhausted"
let responder = DefaultResponder::new(&dm);
// Run the responder with up to 4 handlers (i.e. 4 exchanges can be handled simultaneously)
// Clients trying to open more exchanges than the ones currently running will get "I'm busy, please try again later"
let mut respond = pin!(responder.run::<4, 4>());
// Run the background job of the data model
let mut dm_job = pin!(dm.run());
// Create the Matter UDP socket
let socket = async_io::Async::<UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;
// Run the Matter and mDNS transports
let mut mdns = pin!(mdns::run_mdns(&matter));
let mut transport = pin!(matter.run(&socket, &socket));
// Create, load and run the persister
let mut psm: Psm<4096> = Psm::new();
let path = std::env::temp_dir().join("rs-matter");
psm.load(&path, &matter, NO_NETWORKS)?;
if !matter.is_commissioned() {
// If the device is not commissioned yet, print the QR text and code to the console
// and enable basic commissioning
matter.print_standard_qr_text(DiscoveryCapabilities::IP)?;
matter.print_standard_qr_code(QrTextType::Unicode, DiscoveryCapabilities::IP)?;
matter.open_basic_comm_window(MAX_COMM_WINDOW_TIMEOUT_SECS)?;
}
let mut persist = pin!(psm.run(&path, &matter, NO_NETWORKS));
// Combine all async tasks in a single one
let all = select4(
&mut transport,
&mut mdns,
&mut persist,
select(&mut respond, &mut dm_job).coalesce(),
);
// Run with a simple `block_on`. Any local executor would do.
futures_lite::future::block_on(all.coalesce())
}
/// The Node meta-data describing our Matter device.
const NODE: Node<'static> = Node {
id: 0,
endpoints: &[
endpoints::root_endpoint(NetworkType::Ethernet),
Endpoint {
id: 1,
device_types: devices!(DEV_TYPE_SMART_SPEAKER),
clusters: clusters!(
desc::DescHandler::CLUSTER,
TestOnOffDeviceLogic::CLUSTER,
LevelControlDeviceLogic::CLUSTER
),
},
],
};
/// The Data Model handler + meta-data for our Matter device.
/// The handler is the root endpoint 0 handler plus the Speaker handler.
fn dm_handler<'a, LH: LevelControlHooks, OH: OnOffHooks>(
matter: &Matter<'_>,
on_off: &'a OnOffHandler<'a, OH, LH>,
level_control: &'a LevelControlHandler<'a, LH, OH>,
) -> impl AsyncMetadata + AsyncHandler + 'a {
(
NODE,
endpoints::with_eth(
&(),
&UnixNetifs,
matter.rand(),
endpoints::with_sys(
&false,
matter.rand(),
EmptyHandler
.chain(
EpClMatcher::new(Some(1), Some(desc::DescHandler::CLUSTER.id)),
desc::DescHandler::new(Dataver::new_rand(matter.rand())).adapt(),
)
.chain(
EpClMatcher::new(Some(1), Some(LevelControlDeviceLogic::CLUSTER.id)),
level_control::HandlerAdaptor(level_control),
)
.chain(
EpClMatcher::new(Some(1), Some(TestOnOffDeviceLogic::CLUSTER.id)),
on_off::HandlerAdaptor(on_off),
),
),
),
)
}