matc 0.1.2

Matter protocol library (controller side)
Documentation
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
use std::{
    sync::Arc,
    time::{self, Duration},
};

use anyhow::Result;
use clap::{Parser, Subcommand};
use matc::{
    certmanager::{self, FileCertManager},
    clusters::{self, defs::{CLUSTER_DOOR_LOCK_CMD_ID_GETUSER, CLUSTER_ID_DOOR_LOCK}},
    controller, discover, messages, onboarding, tlv::{self, TlvItem}, transport,
};

const DEFAULT_FABRIC: u64 = 0x110;
const DEFAULT_LOCAL_ADDRESS: &str = "0.0.0.0:5555";
const DEFAULT_CERT_PATH: &str = "./pem";

const DEFAULT_DEVICE_ADDRESS: &str = "192.168.5.108:5540";
#[derive(Parser, Debug)]
#[command()]
struct Cli {
    #[clap(long)]
    #[arg(global = true, default_value_t = false)]
    verbose: bool,

    #[clap(long)]
    #[arg(global = true, default_value_t = DEFAULT_CERT_PATH.to_string())]
    cert_path: String,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Commission device
    Commission {
        #[clap(long)]
        #[arg(default_value_t=DEFAULT_LOCAL_ADDRESS.to_string())]
        local_address: String,

        device_address: String,
        controller_id: u64,
        device_id: u64,
        pin: u32,
    },
    ListSupportedClusters {
        #[clap(long)]
        #[arg(default_value_t=DEFAULT_LOCAL_ADDRESS.to_string())]
        local_address: String,

        device_address: String,
        controller_id: u64,
        device_id: u64,
        endpoint: u16,
    },
    Discover {
        #[clap(long)]
        #[arg(global = true, default_value_t = 5)]
        timeout: u64,

        #[command(subcommand)]
        discover: DiscoverCommand,
    },
    /*ListFabrics {
        #[clap(long)]
        #[arg(default_value_t = DEFAULT_FABRIC)]
        fabric_id: u64,

        #[clap(long)]
        #[arg(default_value_t=DEFAULT_LOCAL_ADDRESS.to_string())]
        local_address: String,

        device_address: String,
        controller_id: u64,
        device_id: u64,
    },*/
    /// Initialize CA - generate CA keys and certificate
    CaBootstrap {
        #[clap(long)]
        #[arg(default_value_t = DEFAULT_FABRIC)]
        fabric_id: u64,
    },
    DecodeManualPairingCode {
        code: String,
    },
    /// Create key and certificate for controller
    CaCreateController {
        controller_id: u64,
    },
    Command {
        #[clap(long)]
        #[arg(global = true, default_value_t = DEFAULT_LOCAL_ADDRESS.to_string())]
        local_address: String,

        #[clap(long)]
        #[arg(global = true, default_value_t = DEFAULT_DEVICE_ADDRESS.to_string())]
        device_address: String,

        #[clap(long)]
        #[arg(global = true, default_value_t = 100)]
        controller_id: u64,

        #[clap(long)]
        #[arg(global = true, default_value_t = 300)]
        device_id: u64,

        #[clap(long)]
        #[arg(global = true, default_value_t = 1)]
        endpoint: u16,

        #[command(subcommand)]
        command: CommandCommand,
    },
}

#[derive(Subcommand, Debug)]
enum CommandCommand {
    Read {
        endpoint: u16,
        cluster: u32,
        attr: u32,
    },
    InvokeCommandOn {},
    InvokeCommandOff {},
    InvokeCommandMoveToLevel {
        level: u8,
    },
    InvokeCommandMoveToHue {
        hue: u8,
    },
    InvokeCommandUpdateFabricLabel {
        label: String,
    },
    InvokeCommandRemoveFabric {
        index: u8,
    },
    ListSupportedClusters {
        endpoint: u16,
    },
    ListSupportedClusters2 {
        endpoint: u16,
    },
    ListParts {},
    ListBridgedDevices {},
    ListAttributes {},
    StartCommissioning {
        pin: u32,

        #[arg(default_value_t = 1000)]
        iterations: u32,

        #[arg(default_value_t = 1000)]
        discriminator: u16,

        #[arg(default_value_t = 200)]
        timeout: u16,
    },
    MonitorDoorState{},
    Test2{},
}
#[derive(Subcommand, Debug)]
enum DiscoverCommand {
    Commissionable {},
    Commissioned {},
    Commissioned2 {
        #[arg(long)]
        device_id: Option<u64>,
    },
}

async fn create_connection(
    local_address: &str,
    device_address: &str,
    device_id: u64,
    controller_id: u64,
    cert_path: &str,
) -> Result<controller::Connection> {
    let cm: Arc<dyn certmanager::CertManager> = certmanager::FileCertManager::load(cert_path)?;
    let transport = transport::Transport::new(local_address).await?;
    let controller = controller::Controller::new(&cm, &transport, cm.get_fabric_id())?;
    let connection = transport.create_connection(device_address).await;
    let c = controller
        .auth_sigma(&connection, device_id, controller_id)
        .await?;
    Ok(c)
}

fn commission(
    controller_id: u64,
    device_address: &str,
    pin: u32,
    local_address: &str,
    device_id: u64,
    cert_path: &str,
) {
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();

    runtime.block_on(async {
        let cm: Arc<dyn certmanager::CertManager> =
            certmanager::FileCertManager::load(cert_path).unwrap();
        let transport = transport::Transport::new(local_address).await.unwrap();
        let controller = controller::Controller::new(&cm, &transport, cm.get_fabric_id()).unwrap();
        let connection = transport.create_connection(device_address).await;
        let con = controller
            .commission(&connection, pin, device_id, controller_id)
            .await
            .unwrap();
        println!("commissioning ok. now list supported clusters (endpoint 0):");
        let resptlv = con.read_request2(0, 0x1d, 1).await.unwrap();
        if let tlv::TlvItemValue::List(l) = resptlv {
            for c in l {
                if let tlv::TlvItemValue::Int(v) = c.value {
                    println!("{:?}", clusters::names::get_cluster_name(v as u32));
                }
            }
        }
    });
}

async fn progress(duration: Duration) {
    tokio::spawn(async move {
        let start_time = time::SystemTime::now();
        while start_time.elapsed().unwrap() < duration {
            println!(
                "remaining time: {:.2}sec",
                (duration - start_time.elapsed().unwrap()).as_secs_f32()
            );
            tokio::time::sleep(Duration::from_millis(500)).await;
        }
    });
}

fn discover_cmd(discover: DiscoverCommand, timeout: u64, cert_path: String) {
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();
    let time = Duration::from_secs(timeout);
    match discover {
        DiscoverCommand::Commissionable {} => runtime.block_on(async {
            progress(time).await;
            let infos = discover::discover_commissionable(time).await.unwrap();
            for info in infos {
                println!("{:#?}", info);
            }
        }),
        DiscoverCommand::Commissioned {} => runtime.block_on(async {
            progress(time).await;
            let infos = discover::discover_commissioned2(time, &None).await.unwrap();
            for info in infos {
                println!("{:#?}", info);
            }
        }),
        DiscoverCommand::Commissioned2 { device_id } => runtime.block_on(async {
            let device_str = if let Some(device_id) = device_id {
                let cm: Arc<dyn certmanager::CertManager> = certmanager::FileCertManager::load(&cert_path).unwrap();
                let fabric = matc::fabric::Fabric::new(cm.get_fabric_id(), 1, &cm.get_ca_public_key().unwrap());
                let c = fabric.compressed().unwrap();
                Some(format!("{}-{:016X}", hex::encode(c).to_uppercase(), device_id))
            } else {
                None
            };
            println!("discovering commissioned devices with device id filter: {:?}", device_str);
            progress(time).await;
            let infos = discover::discover_commissioned2(time, &device_str).await.unwrap();
            for info in infos {
                println!("{:#?}", info);
            }
        }),
    }
}

fn bridged_device_attr_name_from_id(id: u32) -> &'static str {
    match id {
        0x0000 => "DataModelRevision",
        0x0001 => "VendorName",
        0x0002 => "VendorID",
        0x0003 => "ProductName",
        0x0004 => "ProductID",
        0x0005 => "NodeLabel",
        0x0006 => "Location",
        0x0007 => "HardwareVersion",
        0x0008 => "HardwareVersionString",
        0x0009 => "SoftwareVersion",
        0x000A => "SoftwareVersionString",
        0x000B => "ManufacturingDate",
        0x000C => "PartNumber",
        0x000D => "ProductURL",
        0x000E => "ProductLabel",
        0x000F => "SerialNumber",
        0x0010 => "LocalConfigDisabled",
        0x0011 => "Reachable",
        0x0012 => "UniqueID",
        0x0013 => "CapabilityMinima",
        0x0014 => "ProductAppearance",
        0x0015 => "SpecificationVersion",
        0x0016 => "MaxPathsPerInvoke",
        0x0018 => "ConfigurationVersion",
        _ => "Unknown",
    }
}

async fn bridge_info(connection: &mut controller::Connection) {
    let resptlv = connection
        .read_request2(
            0,
            clusters::defs::CLUSTER_ID_DESCRIPTOR,
            clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_PARTSLIST,
        )
        .await
        .unwrap();
    if let tlv::TlvItemValue::List(l) = resptlv {
        for part in l {
            if let tlv::TlvItemValue::Int(v) = part.value {
                println!("endpoint {}", v);
                for n in 0..0x18 {
                    let out = connection
                        .read_request2(
                            v as u16,
                            clusters::defs::CLUSTER_ID_BRIDGED_DEVICE_BASIC_INFORMATION,
                            n,
                        )
                        .await;
                    if let Ok(out) = out {
                        println!("  attr 0x{:x} {}: {:?}", n, bridged_device_attr_name_from_id(n), out);
                    }
                }
                let supported_clusters = connection
                    .read_request2(v as u16, clusters::defs::CLUSTER_ID_DESCRIPTOR,
                    clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_SERVERLIST)
                    .await
                    .unwrap();
                println!("  Supported clusters:");
                if let tlv::TlvItemValue::List(l) = supported_clusters {
                    for c in l {
                        if let tlv::TlvItemValue::Int(v) = c.value {
                            match clusters::names::get_cluster_name(v as u32) {
                                Some(v) => println!("    {}", v),
                                None => println!(".   unknown cluster - id 0x{:x}", v),
                            }
                        }
                    }
                }
                let taglist = connection
                    .read_request2(v as u16, clusters::defs::CLUSTER_ID_DESCRIPTOR,
                    clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_TAGLIST)
                    .await;
                if let Ok(taglist) = taglist {
                    println!("  taglist: {:?}", taglist);
                }
                let devtypes = connection
                    .read_request2(v as u16, clusters::defs::CLUSTER_ID_DESCRIPTOR,
                    clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_DEVICETYPELIST)
                    .await;
                if let Ok(tlv::TlvItemValue::List(devtypes)) = devtypes {
                    for c in devtypes {
                            let typ = c.get_int(&[0]);
                            if let Some(typ) = typ {
                                if let Some(name) = clusters::dt_names::get_device_type_name(typ as u32) {
                                    println!("  device type: {} (0x{:x})", name, typ);
                                } else {
                                    println!("  device type: unknown (0x{:x})", typ);
                                }
                            }
                    }
                }
                let parts = connection
                    .read_request2(v as u16, clusters::defs::CLUSTER_ID_DESCRIPTOR,
                    clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_PARTSLIST)
                    .await;
                if let Ok(parts) = parts {
                    let mut parts_str = "".to_string();
                    if let tlv::TlvItemValue::List(l) = parts {
                        for c in l {
                            if let tlv::TlvItemValue::Int(v) = c.value {
                                parts_str += &format!("{} ", v);
                            }
                        }
                    }
                    if !parts_str.is_empty() {
                        println!("  parts: {}", parts_str);
                    }
                }
            }
        }
    }
}

async fn print_cluster_attributes(
    connection: &mut controller::Connection,
    endpoint: u16,
    cluster: u32,
) {
    match clusters::names::get_cluster_name(cluster) {
        Some(v) => println!("    {}", v),
        None => println!("    unknown cluster - id 0x{:x}", cluster),
    }
    let attrlist = clusters::codec::get_attribute_list(cluster);
    for attr in attrlist {
        let out = connection.read_request2(endpoint, cluster, attr.0).await;
        if let Ok(out) = out {
            println!(
                "      attr 0x{:x} {}: {}",
                attr.0,
                attr.1,
                clusters::codec::decode_attribute_json(cluster, attr.0, &out)
            );
        } else {
            //println!("      attr 0x{:x} {}: <read error> {:?}", attr.0, attr.1, out);
        }
    }
}
async fn print_endpoint_attributes(connection: &mut controller::Connection, endpoint: u16) {
    let resptlv = connection
        .read_request2(
            endpoint,
            clusters::defs::CLUSTER_ID_DESCRIPTOR,
            clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_SERVERLIST,
        )
        .await
        .unwrap();
    println!("  clusters:");
    if let tlv::TlvItemValue::List(l) = resptlv {
        for c in l {
            if let tlv::TlvItemValue::Int(cluster) = c.value {
                print_cluster_attributes(connection, endpoint, cluster as u32).await;
            }
        }
    }
}
async fn all_attributes(connection: &mut controller::Connection) {
    let resptlv = connection
        .read_request2(
            0,
            clusters::defs::CLUSTER_ID_DESCRIPTOR,
            clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_PARTSLIST,
        )
        .await
        .unwrap();
    if let tlv::TlvItemValue::List(l) = resptlv {
        for part in l {
            if let tlv::TlvItemValue::Int(v) = part.value {
                println!("endpoint {}", v);
                print_endpoint_attributes(connection, v as u16).await;
            }
        }
    }
    println!("endpoint 0");
    print_endpoint_attributes(connection, 0).await;
}

fn command_cmd(
    command: CommandCommand,
    local_address: &str,
    device_address: &str,
    controller_id: u64,
    device_id: u64,
    cert_path: &str,
    endpoint: u16,
) {
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();

    runtime.block_on(async {
        {
        let mut connection =
            create_connection(local_address, device_address, device_id, controller_id, cert_path)
                .await
                .unwrap();

        match command {
            CommandCommand::Read {
                endpoint,
                cluster,
                attr,
            } => {
                let res = connection
                    .read_request(endpoint, cluster, attr)
                    .await
                    .unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::InvokeCommandOn {} => {
                let res = connection.invoke_request(endpoint, 0x6, 1, &[]).await.unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::InvokeCommandOff {} => {
                let res = connection.invoke_request(endpoint, 0x6, 0, &[]).await.unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::InvokeCommandMoveToLevel { level } => {
                let tlv = tlv::TlvItemEnc {
                    tag: 0,
                    value: tlv::TlvItemValueEnc::StructInvisible(vec![
                        (0, tlv::TlvItemValueEnc::UInt8(level)).into(),
                        (1, tlv::TlvItemValueEnc::UInt16(10)).into(), // transition time
                        (2, tlv::TlvItemValueEnc::UInt8(0)).into(), // options mask
                        (3, tlv::TlvItemValueEnc::UInt8(0)).into(), // options override
                    ]),
                }
                .encode()
                .unwrap();
                let res = connection
                    .invoke_request(
                        endpoint,
                        clusters::defs::CLUSTER_ID_LEVEL_CONTROL,
                        clusters::defs::CLUSTER_LEVEL_CONTROL_CMD_ID_MOVETOLEVEL,
                        &tlv,
                    )
                    .await
                    .unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::InvokeCommandMoveToHue { hue } => {
                let tlv = tlv::TlvItemEnc {
                    tag: 0,
                    value: tlv::TlvItemValueEnc::StructInvisible(vec![
                        (0, tlv::TlvItemValueEnc::UInt8(hue)).into(),
                        (1, tlv::TlvItemValueEnc::UInt8(0)).into(), // direction
                        (2, tlv::TlvItemValueEnc::UInt16(10)).into(), // time
                        (3, tlv::TlvItemValueEnc::UInt8(0)).into(), // options mask
                        (4, tlv::TlvItemValueEnc::UInt8(0)).into(), // options override
                    ]),
                }
                .encode()
                .unwrap();
                let res = connection
                    .invoke_request(
                        endpoint,
                        clusters::defs::CLUSTER_ID_COLOR_CONTROL,
                        clusters::defs::CLUSTER_COLOR_CONTROL_CMD_ID_MOVETOHUE,
                        &tlv,
                    )
                    .await
                    .unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::InvokeCommandUpdateFabricLabel { label } => {
                let tlv = tlv::TlvItemEnc {
                    tag: 0,
                    value: tlv::TlvItemValueEnc::String(label),
                }
                .encode()
                .unwrap();
                let res = connection.invoke_request(0, 0x3e, 9, &tlv).await.unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::InvokeCommandRemoveFabric { index } => {
                let tlv = tlv::TlvItemEnc {
                    tag: 0,
                    value: tlv::TlvItemValueEnc::UInt8(index),
                }
                .encode()
                .unwrap();
                let res = connection.invoke_request(0, 0x3e, 0xa, &tlv).await.unwrap();
                res.tlv.dump(1);
            }
            CommandCommand::ListSupportedClusters { endpoint } => {
                let resptlv = connection.read_request2(endpoint, 0x1d, 1).await.unwrap();
                if let tlv::TlvItemValue::List(l) = resptlv {
                    for c in l {
                        if let tlv::TlvItemValue::Int(v) = c.value {
                            match clusters::names::get_cluster_name(v as u32) {
                                Some(v) => println!("{}", v),
                                None => println!("unknown cluster - id 0x{:x}", v),
                            }
                        }
                    }
                }
            }
            CommandCommand::ListSupportedClusters2 { endpoint } => {
                let resptlv = connection.read_request(endpoint, 0x1d, 1).await.unwrap();
                let r = resptlv.tlv.get(&[1]).unwrap();
                if let tlv::TlvItemValue::List(l) = r {
                    for r in l {
                        let v = r.get(&[1, 2]);
                        if let Some(tlv::TlvItemValue::Int(v)) = v {
                            match clusters::names::get_cluster_name(*v as u32) {
                                Some(v) => println!("{}", v),
                                None => println!("unknown cluster - id 0x{:x}", v),
                            }
                        }
                    }
                }
            }
            CommandCommand::ListParts {} => {
                let resptlv = connection
                    .read_request2(
                        0,
                        clusters::defs::CLUSTER_ID_DESCRIPTOR,
                        clusters::defs::CLUSTER_DESCRIPTOR_ATTR_ID_PARTSLIST,
                    )
                    .await
                    .unwrap();
                println!("{:?}", resptlv);
                if let tlv::TlvItemValue::List(l) = resptlv {
                    for c in l {
                        if let tlv::TlvItemValue::Int(v) = c.value {
                            println!("{}", v);
                        }
                    }
                }
            }
            CommandCommand::ListBridgedDevices {} => {
                bridge_info(&mut connection).await;
            }
            CommandCommand::ListAttributes {} => {
                all_attributes(&mut connection).await;
            }
            CommandCommand::StartCommissioning { pin, iterations, discriminator, timeout } => {
                let mut salt = [0; 32];
                rand::RngCore::fill_bytes(&mut rand::thread_rng(), &mut salt);
                let key = &matc::controller::pin_to_passcode(pin).unwrap();
                let data = matc::spake2p::Engine::create_passcode_verifier(key, &salt, iterations);
                let tlv = tlv::TlvItemEnc {
                            tag: 0,
                            value: tlv::TlvItemValueEnc::StructInvisible(vec![
                                    (0, tlv::TlvItemValueEnc::UInt16(timeout)).into(),
                                    (1, tlv::TlvItemValueEnc::OctetString(data)).into(),
                                    (2, tlv::TlvItemValueEnc::UInt16(discriminator)).into(),
                                    (3, tlv::TlvItemValueEnc::UInt32(iterations)).into(),
                                    (4, tlv::TlvItemValueEnc::OctetString(salt.to_vec())).into(),
                            ]),
                        }
                        .encode()
                        .unwrap();
                let res = connection.invoke_request_timed(0, clusters::defs::CLUSTER_ID_ADMINISTRATOR_COMMISSIONING, clusters::defs::CLUSTER_ADMINISTRATOR_COMMISSIONING_CMD_ID_OPENCOMMISSIONINGWINDOW, &tlv, 6000).await.unwrap();
                log::debug!("start commissioning response: {:?}", res);
                if res.protocol_header.protocol_id != messages::ProtocolMessageHeader::PROTOCOL_ID_INTERACTION
                    || res.protocol_header.opcode != messages::ProtocolMessageHeader::INTERACTION_OPCODE_INVOKE_RESP
                {
                    panic!("unexpected response {:?}", res);
                }
                let (_common_status, status) = messages::parse_im_invoke_resp(&res.tlv).unwrap();
                match status {
                    0 => log::info!("start commissioning status: success"),
                    2 => log::info!("start commissioning status: busy(2)"),
                    3 => log::info!("start commissioning status: pake error(3)"),
                    4 => log::info!("start commissioning status: window not open(4)"),
                    _ => log::info!("start commissioning status: {}", status),
                }
            },
            CommandCommand::MonitorDoorState{} => {
                fn decode_door_change_state_event(tlv: TlvItem) {
                    let tlv_stat = tlv.get(&[2]);
                    if let Some(tlv_stat) =  tlv_stat {
                        if let tlv::TlvItemValue::List(lst) = tlv_stat {
                            for item in lst {
                                let par = item.get(&[1, 7]);
                                if let Some(par) = par {
                                    println!("status: {:?}", clusters::codec::door_lock::decode_door_state_change_event(par));
                                }
                            }
                        } else {
                            println!("no events in report data");
                        }
                    }
                }
                let res = connection.im_subscribe_request(1, 0x101, 1).await.unwrap();
                if res.protocol_header.opcode != messages::ProtocolMessageHeader::INTERACTION_OPCODE_REPORT_DATA {
                    log::warn!("unexpected response opcode 0x{:x}", res.protocol_header.opcode);
                } else {
                    decode_door_change_state_event(res.tlv.clone());
                }
                connection.im_status_response(res.protocol_header.exchange_id, 1 | 2, res.message_header.message_counter).await.unwrap();
                loop {
                    let ev = connection.recv_event().await.unwrap();
                    match ev.protocol_header.opcode {
                        messages::ProtocolMessageHeader::INTERACTION_OPCODE_REPORT_DATA => {
                            println!("this is Event/Report Data {}", ev.protocol_header.exchange_id);
                            decode_door_change_state_event(ev.tlv.clone());
                            connection.im_status_response(ev.protocol_header.exchange_id, 2, ev.message_header.message_counter).await.unwrap();
                            println!("sent status response");
                        }
                        messages::ProtocolMessageHeader::INTERACTION_OPCODE_SUBSCRIBE_RESP => {
                            println!("this is Subscribe Response {}", ev.protocol_header.exchange_id);
                        }
                        _ => {
                            println!("unhandled event opcode 0x{:x}", ev.protocol_header.opcode);
                        }
                    }
                }
            }
            CommandCommand::Test2{} => {
                let tlv = clusters::codec::door_lock::encode_get_user(1).unwrap();
                let res = connection.invoke_request(1, CLUSTER_ID_DOOR_LOCK, CLUSTER_DOOR_LOCK_CMD_ID_GETUSER, &tlv).await.unwrap();
                let tlv = res.tlv.get(&[1, 0, 0, 1]).unwrap();
                let dec = clusters::codec::door_lock::decode_get_user_response(tlv).unwrap();
                println!("decoded get user response: {:?}", dec);

            }
        }
        }
    });
}

fn main() {
    let cli = Cli::parse();

    let log_level = {
        if cli.verbose {
            log::LevelFilter::Trace
        } else {
            log::LevelFilter::Error
        }
    };
    env_logger::Builder::new()
        .parse_default_env()
        .target(env_logger::Target::Stdout)
        .filter_level(log_level)
        .format_line_number(true)
        .format_file(true)
        .format_timestamp(Some(env_logger::TimestampPrecision::Millis))
        .init();

    let cert_path = cli.cert_path;

    match cli.command {
        Commands::Commission {
            controller_id,
            device_address,
            pin,
            local_address,
            device_id,
        } => {
            commission(
                controller_id,
                &device_address,
                pin,
                &local_address,
                device_id,
                &cert_path,
            );
        }
        Commands::CaBootstrap { fabric_id } => {
            let cm = FileCertManager::new(fabric_id, &cert_path);
            cm.bootstrap().unwrap();
        }
        Commands::CaCreateController { controller_id } => {
            let cm = FileCertManager::load(&cert_path).unwrap();
            cm.create_user(controller_id).unwrap();
        }
        Commands::ListSupportedClusters {
            local_address,
            device_address,
            controller_id,
            device_id,
            endpoint,
        } => {
            let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();

            runtime.block_on(async {
                let connection = create_connection(
                    &local_address,
                    &device_address,
                    device_id,
                    controller_id,
                    &cert_path,
                )
                .await
                .unwrap();
                let resptlv = connection.read_request2(endpoint, 0x1d, 1).await.unwrap();
                if let tlv::TlvItemValue::List(l) = resptlv {
                    for c in l {
                        if let tlv::TlvItemValue::Int(v) = c.value {
                            match clusters::names::get_cluster_name(v as u32) {
                                Some(v) => println!("{}", v),
                                None => println!("unknown cluster - id 0x{:x}", v),
                            }
                        }
                    }
                }
            });
        }
        /*Commands::ListFabrics {
            fabric_id,
            local_address,
            device_address,
            controller_id,
            device_id } => {
                let runtime = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .unwrap();

            runtime.block_on(async {
                let cm: Arc<dyn certmanager::CertManager> = certmanager::FileCertManager::new(fabric_id, CERT_PATH);
                let transport = transport::Transport::new(&local_address).await.unwrap();
                let controller = controller::Controller::new(&cm, &transport, fabric_id);
                let connection = transport.create_connection(&device_address).await;
                let mut connection = controller.auth_sigma(&connection, device_id, controller_id).await.unwrap();
                let response = connection.read_request(
                    0,
                    matc::clusters::OperationalCredentialCluster::CLUSTER_ID_OPERATIONAL_CREDENTIALS,
                    matc::clusters::OperationalCredentialCluster::ATTRIB_ID_FABRICS).await.unwrap();
                let resplist = response.tlv.get(&[1,0,1,2]).unwrap();
                if let tlv::TlvItemValue::List(l) = resplist {
                    for c in l {
                        println!("{:?}", matc::clusters::OperationalCredentialCluster::FabricDescriptorStruct::decode(c))
                    }
                }
            });
        },*/
        Commands::Command {
            command,
            local_address,
            device_address,
            controller_id,
            device_id,
            endpoint,
        } => {
            command_cmd(
                command,
                &local_address,
                &device_address,
                controller_id,
                device_id,
                &cert_path,
                endpoint,
            );
        }
        Commands::Discover { discover, timeout } => {
            discover_cmd(discover, timeout, cert_path);
        }
        Commands::DecodeManualPairingCode { code } => {
            let res = onboarding::decode_manual_pairing_code(&code).unwrap();
            println!(
                "discriminator: {}\npasscode: {}",
                res.discriminator, res.passcode
            )
        }
    }
}