-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdimmable_light.rs
More file actions
531 lines (456 loc) · 17.8 KB
/
dimmable_light.rs
File metadata and controls
531 lines (456 loc) · 17.8 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
*
* 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 the On/Off and LevelControl cluster over Ethernet.
#![allow(clippy::uninlined_format_args)]
use core::cell::Cell;
use core::pin::pin;
use std::fs;
use std::io::{Read, Write};
use std::net::UdpSocket;
use std::path::PathBuf;
use embassy_futures::select::{select3, select4};
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use async_signal::{Signal, Signals};
use log::{error, info, trace};
use futures_lite::StreamExt;
use rs_matter::dm::clusters::decl::level_control::{
AttributeId, CommandId, OptionsBitmap, FULL_CLUSTER as LEVEL_CONTROL_FULL_CLUSTER,
};
use rs_matter::dm::clusters::decl::on_off as on_off_cluster;
use rs_matter::dm::clusters::desc::{self, ClusterHandler as _};
use rs_matter::dm::clusters::level_control::{self, LevelControlHooks};
use rs_matter::dm::clusters::net_comm::NetworkType;
use rs_matter::dm::clusters::on_off::{self, OnOffHooks, StartUpOnOffEnum};
use rs_matter::dm::devices::test::{TEST_DEV_ATT, TEST_DEV_COMM, TEST_DEV_DET};
use rs_matter::dm::devices::DEV_TYPE_DIMMABLE_LIGHT;
use rs_matter::dm::endpoints;
use rs_matter::dm::networks::unix::UnixNetifs;
use rs_matter::dm::subscriptions::DefaultSubscriptions;
use rs_matter::dm::IMBuffer;
use rs_matter::dm::{
AsyncHandler, AsyncMetadata, Cluster, DataModel, Dataver, EmptyHandler, Endpoint, EpClMatcher,
Node,
};
use rs_matter::error::{Error, ErrorCode};
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::init::InitMaybeUninit;
use rs_matter::utils::select::Coalesce;
use rs_matter::utils::storage::pooled::PooledBuffers;
use rs_matter::{clusters, devices, with, Matter, MATTER_PORT};
use static_cell::StaticCell;
#[path = "../common/mdns.rs"]
mod mdns;
// Statically allocate in BSS the bigger objects
// `rs-matter` supports efficient initialization of BSS objects (with `init`)
// as well as just allocating the objects on-stack or on the heap.
static MATTER: StaticCell<Matter> = StaticCell::new();
static BUFFERS: StaticCell<PooledBuffers<10, NoopRawMutex, IMBuffer>> = StaticCell::new();
static SUBSCRIPTIONS: StaticCell<DefaultSubscriptions> = StaticCell::new();
static PSM: StaticCell<Psm<4096>> = StaticCell::new();
#[cfg(feature = "chip-test")]
const PERSIST_FILE_NAME: &str = "/tmp/chip_kvs";
fn main() -> Result<(), Error> {
let thread = std::thread::Builder::new()
// Increase the stack size until the example can work without stack blowups.
// Note that the used stack size increases exponentially by lowering the level of compiler optimizations,
// as lower optimization settings prevent the Rust compiler from inlining constructor functions
// which often results in (unnecessary) memory moves and increased stack utilization:
// e.g., an opt-level of "0" will require a several times' larger stack.
//
// Optimizing/lowering `rs-matter` memory consumption is an ongoing topic.
.stack_size(550 * 1024)
.spawn(run)
.unwrap();
thread.join().unwrap()
}
fn run() -> Result<(), Error> {
#[cfg(not(feature = "chip-test"))]
env_logger::init_from_env(
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "debug"),
);
#[cfg(feature = "chip-test")]
env_logger::builder()
.format(|buf, record| {
use std::io::Write;
writeln!(buf, "{}: {}", record.level(), record.args())
})
.target(env_logger::Target::Stdout)
.filter_level(::log::LevelFilter::Debug)
.init();
info!(
"Matter memory: Matter (BSS)={}B, IM Buffers (BSS)={}B, Subscriptions (BSS)={}B",
core::mem::size_of::<Matter>(),
core::mem::size_of::<PooledBuffers<10, NoopRawMutex, IMBuffer>>(),
core::mem::size_of::<DefaultSubscriptions>()
);
let matter = MATTER.uninit().init_with(Matter::init(
&TEST_DEV_DET,
TEST_DEV_COMM,
&TEST_DEV_ATT,
rs_matter::utils::epoch::sys_epoch,
rs_matter::utils::rand::sys_rand,
MATTER_PORT,
));
// Need to call this once
matter.initialize_transport_buffers()?;
// Create the transport buffers
let buffers = BUFFERS.uninit().init_with(PooledBuffers::init(0));
// Create the subscriptions
let subscriptions = SUBSCRIPTIONS
.uninit()
.init_with(DefaultSubscriptions::init());
// OnOff cluster setup
let on_off_handler =
on_off::OnOffHandler::new(Dataver::new_rand(matter.rand()), 1, OnOffDeviceLogic::new());
// LevelControl cluster setup
let level_control_handler = level_control::LevelControlHandler::new(
Dataver::new_rand(matter.rand()),
1,
LevelControlDeviceLogic::new(),
level_control::AttributeDefaults {
on_level: Nullable::some(42),
options: OptionsBitmap::from_bits(OptionsBitmap::EXECUTE_IF_OFF.bits()).unwrap(),
..Default::default()
},
);
// 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);
info!(
"Responder memory: Responder (stack)={}B, Runner fut (stack)={}B",
core::mem::size_of_val(&responder),
core::mem::size_of_val(&responder.run::<4, 4>())
);
// 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());
let socket = async_io::Async::<UdpSocket>::bind(MATTER_SOCKET_BIND_ADDR)?;
info!(
"Transport memory: Transport fut (stack)={}B, mDNS fut (stack)={}B",
core::mem::size_of_val(&matter.run(&socket, &socket)),
core::mem::size_of_val(&mdns::run_mdns(matter))
);
// 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 psm = PSM.uninit().init_with(Psm::init());
#[cfg(not(feature = "chip-test"))]
let path = std::env::temp_dir().join("rs-matter");
#[cfg(feature = "chip-test")]
let path = PathBuf::from(PERSIST_FILE_NAME);
info!(
"Persist memory: Persist (BSS)={}B, Persist fut (stack)={}B, Persist path={}",
core::mem::size_of::<Psm<4096>>(),
core::mem::size_of_val(&psm.run(&path, matter, NO_NETWORKS)),
path.as_path().to_str().unwrap_or("none")
);
psm.load(&path, matter, NO_NETWORKS)?;
// We need to always print the QR text, because the test runner expects it to be printed
// even if the device is already commissioned
matter.print_standard_qr_text(DiscoveryCapabilities::IP)?;
if !matter.is_commissioned() {
// If the device is not commissioned yet, print the QR code to the console
// and enable basic commissioning
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));
// Listen to SIGTERM because at the end of the test we'll receive it
let mut term_signal = Signals::new([Signal::Term])?;
let mut term = pin!(async {
term_signal.next().await;
Ok(())
});
// Combine all async tasks in a single one
let all = select4(
&mut transport,
&mut mdns,
&mut persist,
select3(&mut respond, &mut dm_job, &mut term).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_DIMMABLE_LIGHT),
clusters: clusters!(
desc::DescHandler::CLUSTER,
OnOffDeviceLogic::CLUSTER,
LevelControlDeviceLogic::CLUSTER,
),
},
],
};
/// The Data Model handler + meta-data for our Matter device.
/// The handler is the root endpoint 0 handler plus the on-off handler and its descriptor.
fn dm_handler<'a, LH: LevelControlHooks, OH: OnOffHooks>(
matter: &'a Matter<'a>,
on_off: &'a on_off::OnOffHandler<'a, OH, LH>,
level_control: &'a level_control::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(OnOffDeviceLogic::CLUSTER.id)),
on_off::HandlerAdaptor(on_off),
)
.chain(
EpClMatcher::new(Some(1), Some(LevelControlDeviceLogic::CLUSTER.id)),
level_control::HandlerAdaptor(level_control),
),
),
),
)
}
// Implementing the LevelControl business logic
pub struct LevelControlDeviceLogic {
current_level: Cell<Option<u8>>,
start_up_current_level: Cell<Option<u8>>,
}
impl Default for LevelControlDeviceLogic {
fn default() -> Self {
Self::new()
}
}
impl LevelControlDeviceLogic {
pub const fn new() -> Self {
Self {
current_level: Cell::new(Some(1)),
start_up_current_level: Cell::new(None),
}
}
}
impl LevelControlHooks for LevelControlDeviceLogic {
const MIN_LEVEL: u8 = 1;
const MAX_LEVEL: u8 = 254;
const FASTEST_RATE: u8 = 50;
const CLUSTER: Cluster<'static> = LEVEL_CONTROL_FULL_CLUSTER
.with_features(
level_control::Feature::LIGHTING.bits() | level_control::Feature::ON_OFF.bits(),
)
.with_attrs(with!(
required;
AttributeId::CurrentLevel
| AttributeId::RemainingTime
| AttributeId::MinLevel
| AttributeId::MaxLevel
| AttributeId::OnOffTransitionTime
| AttributeId::OnLevel
| AttributeId::OnTransitionTime
| AttributeId::OffTransitionTime
| AttributeId::DefaultMoveRate
| AttributeId::Options
| AttributeId::StartUpCurrentLevel
))
.with_cmds(with!(
CommandId::MoveToLevel
| CommandId::Move
| CommandId::Step
| CommandId::Stop
| CommandId::MoveToLevelWithOnOff
| CommandId::MoveWithOnOff
| CommandId::StepWithOnOff
| CommandId::StopWithOnOff
));
fn set_device_level(&self, level: u8) -> Result<Option<u8>, ()> {
// This is where business logic is implemented to physically change the level of the device.
Ok(Some(level))
}
fn current_level(&self) -> Option<u8> {
self.current_level.get()
}
fn set_current_level(&self, level: Option<u8>) {
info!(
"LevelControlDeviceLogic::set_current_level: setting level to {:?}",
level
);
self.current_level.set(level);
}
fn start_up_current_level(&self) -> Result<Option<u8>, Error> {
Ok(self.start_up_current_level.get())
}
fn set_start_up_current_level(&self, value: Option<u8>) -> Result<(), Error> {
self.start_up_current_level.set(value);
Ok(())
}
}
// Implementing the OnOff business logic
// A simple serializer and deserializer for persisting the OnOff state in a single byte.
// Stores the on_off state in the first bit.
// Stores the start_up_on_off state in the remaining bits.
#[derive(Default)]
struct OnOffPersistentState {
on_off: bool,
start_up_on_off: Option<StartUpOnOffEnum>,
}
impl OnOffPersistentState {
fn to_bytes_from_values(on_off: bool, start_up_on_off: Option<StartUpOnOffEnum>) -> u8 {
trace!(
"to_bytes_from_values: got on_off: {} | start_up_on_off: {:?}",
on_off,
start_up_on_off
);
let on_off = on_off as u8;
let start_up_on_off: u8 = match start_up_on_off {
Some(StartUpOnOffEnum::Off) => 0,
Some(StartUpOnOffEnum::On) => 1,
Some(StartUpOnOffEnum::Toggle) => 2,
None => 3,
};
trace!(
"to_bytes_from_values: vals before writing on_off: {} | start_up_on_off: {}",
on_off,
start_up_on_off
);
trace!("final val: {}", on_off + (start_up_on_off << 1));
on_off + (start_up_on_off << 1)
}
fn from_bytes(data: u8) -> Result<Self, Error> {
Ok(Self {
on_off: data & 1 != 0,
start_up_on_off: match data >> 1 {
0 => Some(StartUpOnOffEnum::Off),
1 => Some(StartUpOnOffEnum::On),
2 => Some(StartUpOnOffEnum::Toggle),
3 => None,
_ => return Err(ErrorCode::Failure.into()),
},
})
}
}
#[derive(Default)]
pub struct OnOffDeviceLogic {
on_off: Cell<bool>,
start_up_on_off: Cell<Option<StartUpOnOffEnum>>,
storage_path: PathBuf,
}
const STORAGE_FILE_NAME: &str = "rs-matter-on-off-state";
impl OnOffDeviceLogic {
pub fn new() -> Self {
let storage_path = std::env::temp_dir().join(STORAGE_FILE_NAME);
info!(
"OnOffDeviceLogic using storage path: {}",
storage_path.as_path().to_str().unwrap_or("none")
);
let persisted_state = match fs::File::open(storage_path.as_path()) {
Ok(mut file) => {
let mut buf: [u8; 1] = [0];
file.read_exact(&mut buf).unwrap();
trace!("OnOffDeviceLogic::new: read from storage: {:0x}", buf[0]);
OnOffPersistentState::from_bytes(buf[0]).unwrap()
}
Err(_) => OnOffPersistentState::default(),
};
Self {
on_off: Cell::new(persisted_state.on_off),
start_up_on_off: Cell::new(persisted_state.start_up_on_off),
storage_path,
}
}
fn save_state(&self) -> Result<(), Error> {
let mut file = fs::File::create(self.storage_path.as_path())?;
let value = OnOffPersistentState::to_bytes_from_values(
self.on_off.get(),
self.start_up_on_off.get(),
);
let buf = &[value];
trace!("save_storage: wrote {:0x}", value);
file.write_all(buf)?;
Ok(())
}
}
impl OnOffHooks for OnOffDeviceLogic {
const CLUSTER: Cluster<'static> = on_off_cluster::FULL_CLUSTER
.with_revision(6)
.with_features(on_off_cluster::Feature::LIGHTING.bits())
.with_attrs(with!(
required;
on_off_cluster::AttributeId::OnOff
| on_off_cluster::AttributeId::GlobalSceneControl
| on_off_cluster::AttributeId::OnTime
| on_off_cluster::AttributeId::OffWaitTime
| on_off_cluster::AttributeId::StartUpOnOff
))
.with_cmds(with!(
on_off_cluster::CommandId::Off
| on_off_cluster::CommandId::On
| on_off_cluster::CommandId::Toggle
| on_off_cluster::CommandId::OffWithEffect
| on_off_cluster::CommandId::OnWithRecallGlobalScene
| on_off_cluster::CommandId::OnWithTimedOff
));
fn on_off(&self) -> bool {
self.on_off.get()
}
fn set_on_off(&self, on: bool) {
self.on_off.set(on);
info!("OnOff state set to: {}", on);
if let Err(err) = self.save_state() {
error!("Error saving state: {}", err);
}
}
fn start_up_on_off(&self) -> Nullable<on_off::StartUpOnOffEnum> {
match self.start_up_on_off.get() {
Some(value) => Nullable::some(value),
None => Nullable::none(),
}
}
fn set_start_up_on_off(&self, value: Nullable<on_off::StartUpOnOffEnum>) -> Result<(), Error> {
self.start_up_on_off.set(value.into_option());
self.save_state()
}
async fn handle_off_with_effect(&self, _effect: on_off::EffectVariantEnum) {
// no effect
}
}