diff --git a/examples/speaker/src/dev_att.rs b/examples/speaker/src/dev_att.rs new file mode 100644 index 0000000..a16d53f --- /dev/null +++ b/examples/speaker/src/dev_att.rs @@ -0,0 +1,165 @@ +/* + * + * 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. + */ + +use matter::data_model::sdm::dev_att::{DataType, DevAttDataFetcher}; +use matter::error::Error; + +pub struct HardCodedDevAtt {} + +impl HardCodedDevAtt { + pub fn new() -> Self { + Self {} + } +} + +// credentials/examples/ExamplePAI.cpp FFF1 +const PAI_CERT: [u8; 463] = [ + 0x30, 0x82, 0x01, 0xcb, 0x30, 0x82, 0x01, 0x71, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x56, + 0xad, 0x82, 0x22, 0xad, 0x94, 0x5b, 0x64, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x04, 0x03, 0x02, 0x30, 0x30, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, + 0x4d, 0x61, 0x74, 0x74, 0x65, 0x72, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x50, 0x41, 0x41, 0x31, + 0x14, 0x30, 0x12, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa2, 0x7c, 0x02, 0x01, 0x0c, + 0x04, 0x46, 0x46, 0x46, 0x31, 0x30, 0x20, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x32, 0x30, 0x35, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x39, 0x39, 0x39, 0x39, 0x31, 0x32, 0x33, 0x31, + 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30, 0x3d, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, + 0x04, 0x03, 0x0c, 0x1c, 0x4d, 0x61, 0x74, 0x74, 0x65, 0x72, 0x20, 0x44, 0x65, 0x76, 0x20, 0x50, + 0x41, 0x49, 0x20, 0x30, 0x78, 0x46, 0x46, 0x46, 0x31, 0x20, 0x6e, 0x6f, 0x20, 0x50, 0x49, 0x44, + 0x31, 0x14, 0x30, 0x12, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa2, 0x7c, 0x02, 0x01, + 0x0c, 0x04, 0x46, 0x46, 0x46, 0x31, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, + 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, + 0x04, 0x41, 0x9a, 0x93, 0x15, 0xc2, 0x17, 0x3e, 0x0c, 0x8c, 0x87, 0x6d, 0x03, 0xcc, 0xfc, 0x94, + 0x48, 0x52, 0x64, 0x7f, 0x7f, 0xec, 0x5e, 0x50, 0x82, 0xf4, 0x05, 0x99, 0x28, 0xec, 0xa8, 0x94, + 0xc5, 0x94, 0x15, 0x13, 0x09, 0xac, 0x63, 0x1e, 0x4c, 0xb0, 0x33, 0x92, 0xaf, 0x68, 0x4b, 0x0b, + 0xaf, 0xb7, 0xe6, 0x5b, 0x3b, 0x81, 0x62, 0xc2, 0xf5, 0x2b, 0xf9, 0x31, 0xb8, 0xe7, 0x7a, 0xaa, + 0x82, 0xa3, 0x66, 0x30, 0x64, 0x30, 0x12, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, + 0x08, 0x30, 0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, + 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, + 0x04, 0x16, 0x04, 0x14, 0x63, 0x54, 0x0e, 0x47, 0xf6, 0x4b, 0x1c, 0x38, 0xd1, 0x38, 0x84, 0xa4, + 0x62, 0xd1, 0x6c, 0x19, 0x5d, 0x8f, 0xfb, 0x3c, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, + 0x18, 0x30, 0x16, 0x80, 0x14, 0x6a, 0xfd, 0x22, 0x77, 0x1f, 0x51, 0x1f, 0xec, 0xbf, 0x16, 0x41, + 0x97, 0x67, 0x10, 0xdc, 0xdc, 0x31, 0xa1, 0x71, 0x7e, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, + 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0xb2, 0xef, 0x27, + 0xf4, 0x9a, 0xe9, 0xb5, 0x0f, 0xb9, 0x1e, 0xea, 0xc9, 0x4c, 0x4d, 0x0b, 0xdb, 0xb8, 0xd7, 0x92, + 0x9c, 0x6c, 0xb8, 0x8f, 0xac, 0xe5, 0x29, 0x36, 0x8d, 0x12, 0x05, 0x4c, 0x0c, 0x02, 0x20, 0x65, + 0x5d, 0xc9, 0x2b, 0x86, 0xbd, 0x90, 0x98, 0x82, 0xa6, 0xc6, 0x21, 0x77, 0xb8, 0x25, 0xd7, 0xd0, + 0x5e, 0xdb, 0xe7, 0xc2, 0x2f, 0x9f, 0xea, 0x71, 0x22, 0x0e, 0x7e, 0xa7, 0x03, 0xf8, 0x91, +]; + +// credentials/examples/ExampleDACs.cpp FFF1-8000-0002-Cert +const DAC_CERT: [u8; 492] = [ + 0x30, 0x82, 0x01, 0xe8, 0x30, 0x82, 0x01, 0x8e, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x08, 0x52, + 0x72, 0x4d, 0x21, 0xe2, 0xc1, 0x74, 0xaf, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x04, 0x03, 0x02, 0x30, 0x3d, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x1c, + 0x4d, 0x61, 0x74, 0x74, 0x65, 0x72, 0x20, 0x44, 0x65, 0x76, 0x20, 0x50, 0x41, 0x49, 0x20, 0x30, + 0x78, 0x46, 0x46, 0x46, 0x31, 0x20, 0x6e, 0x6f, 0x20, 0x50, 0x49, 0x44, 0x31, 0x14, 0x30, 0x12, + 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa2, 0x7c, 0x02, 0x01, 0x0c, 0x04, 0x46, 0x46, + 0x46, 0x31, 0x30, 0x20, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x32, 0x30, 0x35, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x5a, 0x18, 0x0f, 0x39, 0x39, 0x39, 0x39, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, + 0x39, 0x35, 0x39, 0x5a, 0x30, 0x53, 0x31, 0x25, 0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, + 0x1c, 0x4d, 0x61, 0x74, 0x74, 0x65, 0x72, 0x20, 0x44, 0x65, 0x76, 0x20, 0x44, 0x41, 0x43, 0x20, + 0x30, 0x78, 0x46, 0x46, 0x46, 0x31, 0x2f, 0x30, 0x78, 0x38, 0x30, 0x30, 0x32, 0x31, 0x14, 0x30, + 0x12, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa2, 0x7c, 0x02, 0x01, 0x0c, 0x04, 0x46, + 0x46, 0x46, 0x31, 0x31, 0x14, 0x30, 0x12, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa2, + 0x7c, 0x02, 0x02, 0x0c, 0x04, 0x38, 0x30, 0x30, 0x32, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, + 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, + 0x03, 0x42, 0x00, 0x04, 0xda, 0x93, 0xf1, 0x67, 0x36, 0x25, 0x67, 0x50, 0xd9, 0x03, 0xb0, 0x34, + 0xba, 0x45, 0x88, 0xab, 0xaf, 0x58, 0x95, 0x4f, 0x77, 0xaa, 0x9f, 0xd9, 0x98, 0x9d, 0xfd, 0x40, + 0x0d, 0x7a, 0xb3, 0xfd, 0xc9, 0x75, 0x3b, 0x3b, 0x92, 0x1b, 0x29, 0x4c, 0x95, 0x0f, 0xd9, 0xd2, + 0x80, 0xd1, 0x4c, 0x43, 0x86, 0x2f, 0x16, 0xdc, 0x85, 0x4b, 0x00, 0xed, 0x39, 0xe7, 0x50, 0xba, + 0xbf, 0x1d, 0xc4, 0xca, 0xa3, 0x60, 0x30, 0x5e, 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, + 0x01, 0xff, 0x04, 0x02, 0x30, 0x00, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, + 0x04, 0x04, 0x03, 0x02, 0x07, 0x80, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, + 0x14, 0xef, 0x06, 0x56, 0x11, 0x9c, 0x1c, 0x91, 0xa7, 0x9a, 0x94, 0xe6, 0xdc, 0xf3, 0x79, 0x79, + 0xdb, 0xd0, 0x7f, 0xf8, 0xa3, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, + 0x80, 0x14, 0x63, 0x54, 0x0e, 0x47, 0xf6, 0x4b, 0x1c, 0x38, 0xd1, 0x38, 0x84, 0xa4, 0x62, 0xd1, + 0x6c, 0x19, 0x5d, 0x8f, 0xfb, 0x3c, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, + 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20, 0x46, 0x86, 0x81, 0x07, 0x33, 0xbf, 0x0d, + 0xc8, 0xff, 0x4c, 0xb5, 0x14, 0x5a, 0x6b, 0xfa, 0x1a, 0xec, 0xff, 0xa8, 0xb6, 0xda, 0xb6, 0xc3, + 0x51, 0xaa, 0xee, 0xcd, 0xaf, 0xb8, 0xbe, 0x95, 0x7d, 0x02, 0x21, 0x00, 0xe8, 0xc2, 0x8d, 0x6b, + 0xfc, 0xc8, 0x7a, 0x7d, 0x54, 0x2e, 0xad, 0x6e, 0xda, 0xca, 0x14, 0x8d, 0x5f, 0xa5, 0x06, 0x1e, + 0x51, 0x7c, 0xbe, 0x4f, 0x24, 0xa7, 0x20, 0xe1, 0xc0, 0x59, 0xde, 0x1a, +]; + +const DAC_PUBKEY: [u8; 65] = [ + 0x04, 0xda, 0x93, 0xf1, 0x67, 0x36, 0x25, 0x67, 0x50, 0xd9, 0x03, 0xb0, 0x34, 0xba, 0x45, 0x88, + 0xab, 0xaf, 0x58, 0x95, 0x4f, 0x77, 0xaa, 0x9f, 0xd9, 0x98, 0x9d, 0xfd, 0x40, 0x0d, 0x7a, 0xb3, + 0xfd, 0xc9, 0x75, 0x3b, 0x3b, 0x92, 0x1b, 0x29, 0x4c, 0x95, 0x0f, 0xd9, 0xd2, 0x80, 0xd1, 0x4c, + 0x43, 0x86, 0x2f, 0x16, 0xdc, 0x85, 0x4b, 0x00, 0xed, 0x39, 0xe7, 0x50, 0xba, 0xbf, 0x1d, 0xc4, + 0xca, +]; + +const DAC_PRIVKEY: [u8; 32] = [ + 0xda, 0xf2, 0x1a, 0x7e, 0xa4, 0x7a, 0x70, 0x48, 0x02, 0xa7, 0xe6, 0x6c, 0x50, 0xeb, 0x10, 0xba, + 0xc3, 0xbd, 0xd1, 0x68, 0x80, 0x39, 0x80, 0x66, 0xff, 0xda, 0xd7, 0xf5, 0x20, 0x98, 0xb6, 0x85, +]; + +// +const CERT_DECLARATION: [u8; 541] = [ + 0x30, 0x82, 0x02, 0x19, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0, + 0x82, 0x02, 0x0a, 0x30, 0x82, 0x02, 0x06, 0x02, 0x01, 0x03, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09, + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x30, 0x82, 0x01, 0x71, 0x06, 0x09, 0x2a, + 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, 0x01, 0x62, 0x04, 0x82, 0x01, 0x5e, + 0x15, 0x24, 0x00, 0x01, 0x25, 0x01, 0xf1, 0xff, 0x36, 0x02, 0x05, 0x00, 0x80, 0x05, 0x01, 0x80, + 0x05, 0x02, 0x80, 0x05, 0x03, 0x80, 0x05, 0x04, 0x80, 0x05, 0x05, 0x80, 0x05, 0x06, 0x80, 0x05, + 0x07, 0x80, 0x05, 0x08, 0x80, 0x05, 0x09, 0x80, 0x05, 0x0a, 0x80, 0x05, 0x0b, 0x80, 0x05, 0x0c, + 0x80, 0x05, 0x0d, 0x80, 0x05, 0x0e, 0x80, 0x05, 0x0f, 0x80, 0x05, 0x10, 0x80, 0x05, 0x11, 0x80, + 0x05, 0x12, 0x80, 0x05, 0x13, 0x80, 0x05, 0x14, 0x80, 0x05, 0x15, 0x80, 0x05, 0x16, 0x80, 0x05, + 0x17, 0x80, 0x05, 0x18, 0x80, 0x05, 0x19, 0x80, 0x05, 0x1a, 0x80, 0x05, 0x1b, 0x80, 0x05, 0x1c, + 0x80, 0x05, 0x1d, 0x80, 0x05, 0x1e, 0x80, 0x05, 0x1f, 0x80, 0x05, 0x20, 0x80, 0x05, 0x21, 0x80, + 0x05, 0x22, 0x80, 0x05, 0x23, 0x80, 0x05, 0x24, 0x80, 0x05, 0x25, 0x80, 0x05, 0x26, 0x80, 0x05, + 0x27, 0x80, 0x05, 0x28, 0x80, 0x05, 0x29, 0x80, 0x05, 0x2a, 0x80, 0x05, 0x2b, 0x80, 0x05, 0x2c, + 0x80, 0x05, 0x2d, 0x80, 0x05, 0x2e, 0x80, 0x05, 0x2f, 0x80, 0x05, 0x30, 0x80, 0x05, 0x31, 0x80, + 0x05, 0x32, 0x80, 0x05, 0x33, 0x80, 0x05, 0x34, 0x80, 0x05, 0x35, 0x80, 0x05, 0x36, 0x80, 0x05, + 0x37, 0x80, 0x05, 0x38, 0x80, 0x05, 0x39, 0x80, 0x05, 0x3a, 0x80, 0x05, 0x3b, 0x80, 0x05, 0x3c, + 0x80, 0x05, 0x3d, 0x80, 0x05, 0x3e, 0x80, 0x05, 0x3f, 0x80, 0x05, 0x40, 0x80, 0x05, 0x41, 0x80, + 0x05, 0x42, 0x80, 0x05, 0x43, 0x80, 0x05, 0x44, 0x80, 0x05, 0x45, 0x80, 0x05, 0x46, 0x80, 0x05, + 0x47, 0x80, 0x05, 0x48, 0x80, 0x05, 0x49, 0x80, 0x05, 0x4a, 0x80, 0x05, 0x4b, 0x80, 0x05, 0x4c, + 0x80, 0x05, 0x4d, 0x80, 0x05, 0x4e, 0x80, 0x05, 0x4f, 0x80, 0x05, 0x50, 0x80, 0x05, 0x51, 0x80, + 0x05, 0x52, 0x80, 0x05, 0x53, 0x80, 0x05, 0x54, 0x80, 0x05, 0x55, 0x80, 0x05, 0x56, 0x80, 0x05, + 0x57, 0x80, 0x05, 0x58, 0x80, 0x05, 0x59, 0x80, 0x05, 0x5a, 0x80, 0x05, 0x5b, 0x80, 0x05, 0x5c, + 0x80, 0x05, 0x5d, 0x80, 0x05, 0x5e, 0x80, 0x05, 0x5f, 0x80, 0x05, 0x60, 0x80, 0x05, 0x61, 0x80, + 0x05, 0x62, 0x80, 0x05, 0x63, 0x80, 0x18, 0x24, 0x03, 0x16, 0x2c, 0x04, 0x13, 0x5a, 0x49, 0x47, + 0x32, 0x30, 0x31, 0x34, 0x32, 0x5a, 0x42, 0x33, 0x33, 0x30, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x34, + 0x24, 0x05, 0x00, 0x24, 0x06, 0x00, 0x25, 0x07, 0x94, 0x26, 0x24, 0x08, 0x00, 0x18, 0x31, 0x7d, + 0x30, 0x7b, 0x02, 0x01, 0x03, 0x80, 0x14, 0x62, 0xfa, 0x82, 0x33, 0x59, 0xac, 0xfa, 0xa9, 0x96, + 0x3e, 0x1c, 0xfa, 0x14, 0x0a, 0xdd, 0xf5, 0x04, 0xf3, 0x71, 0x60, 0x30, 0x0b, 0x06, 0x09, 0x60, + 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, + 0x3d, 0x04, 0x03, 0x02, 0x04, 0x47, 0x30, 0x45, 0x02, 0x20, 0x24, 0xe5, 0xd1, 0xf4, 0x7a, 0x7d, + 0x7b, 0x0d, 0x20, 0x6a, 0x26, 0xef, 0x69, 0x9b, 0x7c, 0x97, 0x57, 0xb7, 0x2d, 0x46, 0x90, 0x89, + 0xde, 0x31, 0x92, 0xe6, 0x78, 0xc7, 0x45, 0xe7, 0xf6, 0x0c, 0x02, 0x21, 0x00, 0xf8, 0xaa, 0x2f, + 0xa7, 0x11, 0xfc, 0xb7, 0x9b, 0x97, 0xe3, 0x97, 0xce, 0xda, 0x66, 0x7b, 0xae, 0x46, 0x4e, 0x2b, + 0xd3, 0xff, 0xdf, 0xc3, 0xcc, 0xed, 0x7a, 0xa8, 0xca, 0x5f, 0x4c, 0x1a, 0x7c, +]; + +impl DevAttDataFetcher for HardCodedDevAtt { + fn get_devatt_data(&self, data_type: DataType, data: &mut [u8]) -> Result { + let src = match data_type { + DataType::CertDeclaration => &CERT_DECLARATION[..], + DataType::PAI => &PAI_CERT[..], + DataType::DAC => &DAC_CERT[..], + DataType::DACPubKey => &DAC_PUBKEY[..], + DataType::DACPrivKey => &DAC_PRIVKEY[..], + }; + if src.len() <= data.len() { + let data = &mut data[0..src.len()]; + data.copy_from_slice(src); + Ok(src.len()) + } else { + Err(Error::NoSpace) + } + } +} diff --git a/examples/speaker/src/lib.rs b/examples/speaker/src/lib.rs new file mode 100644 index 0000000..43ca1b1 --- /dev/null +++ b/examples/speaker/src/lib.rs @@ -0,0 +1,18 @@ +/* + * + * 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. + */ + +pub mod dev_att; diff --git a/examples/speaker/src/main.rs b/examples/speaker/src/main.rs new file mode 100644 index 0000000..de2a605 --- /dev/null +++ b/examples/speaker/src/main.rs @@ -0,0 +1,69 @@ +/* + * + * 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. + */ + +mod dev_att; +use matter::core::{self, CommissioningData}; +use matter::data_model::cluster_basic_information::BasicInfoConfig; +use matter::data_model::cluster_media_playback::{Commands, MediaPlaybackCluster}; +use matter::data_model::device_types::DEV_TYPE_ON_SMART_SPEAKER; +use matter::secure_channel::spake2p::VerifierData; + +fn main() { + env_logger::init(); + let comm_data = CommissioningData { + // TODO: Hard-coded for now + verifier: VerifierData::new_with_pw(123456), + discriminator: 250, + }; + + // vid/pid should match those in the DAC + let dev_info = BasicInfoConfig { + vid: 0xFFF1, + pid: 0x8002, + hw_ver: 2, + sw_ver: 1, + sw_ver_str: "1".to_string(), + serial_no: "aabbccdd".to_string(), + device_name: "Smart Speaker".to_string(), + }; + let dev_att = Box::new(dev_att::HardCodedDevAtt::new()); + + let mut matter = core::Matter::new(dev_info, dev_att, comm_data).unwrap(); + let dm = matter.get_data_model(); + { + let mut node = dm.node.write().unwrap(); + + let endpoint_audio = node.add_endpoint(DEV_TYPE_ON_SMART_SPEAKER).unwrap(); + let mut media_playback_cluster = MediaPlaybackCluster::new().unwrap(); + + // Add some callbacks + let play_callback = Box::new(|| log::info!("Comamnd [Play] handled with callback.")); + let pause_callback = Box::new(|| log::info!("Comamnd [Pause] handled with callback.")); + let stop_callback = Box::new(|| log::info!("Comamnd [Stop] handled with callback.")); + let start_over_callback = + Box::new(|| log::info!("Comamnd [StartOver] handled with callback.")); + media_playback_cluster.add_callback(Commands::Play, play_callback); + media_playback_cluster.add_callback(Commands::Pause, pause_callback); + media_playback_cluster.add_callback(Commands::Stop, stop_callback); + media_playback_cluster.add_callback(Commands::StartOver, start_over_callback); + + node.add_cluster(endpoint_audio, media_playback_cluster) + .unwrap(); + println!("Added Speaker type at endpoint id: {}", endpoint_audio) + } + matter.start_daemon().unwrap(); +} diff --git a/matter/Cargo.toml b/matter/Cargo.toml index 11bc35f..5a6787f 100644 --- a/matter/Cargo.toml +++ b/matter/Cargo.toml @@ -64,3 +64,8 @@ libmdns = { version = "0.7.4" } [[example]] name = "onoff_light" path = "../examples/onoff_light/src/main.rs" + + +[[example]] +name = "speaker" +path = "../examples/speaker/src/main.rs" diff --git a/matter/src/data_model/cluster_media_playback.rs b/matter/src/data_model/cluster_media_playback.rs new file mode 100644 index 0000000..6485a96 --- /dev/null +++ b/matter/src/data_model/cluster_media_playback.rs @@ -0,0 +1,411 @@ +/* + * + * 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. + */ + +use super::objects::*; +use crate::{ + error::*, + interaction_model::{ + command::CommandReq, + core::IMStatusCode, + messages::ib::{self}, + }, + tlv::{TLVWriter, TagType, ToTLV}, +}; +use chrono::{DateTime, NaiveDate}; +use num_derive::FromPrimitive; + +pub const ID: u32 = 0x0506; +#[derive(FromPrimitive)] +pub enum Attributes { + CurrentState = 0x0, + StartTime = 0x1, + Duration = 0x2, + SampledPosition = 0x3, + PlaybackSpeed = 0x4, + SeekRangeEnd = 0x5, + SeekRangeStart = 0x6, +} + +struct ClusterCallback { + name: Commands, + callback: Box, +} + +enum _FeatureMap { + AdvancedSeek = 0, + VariableSpeed = 1, +} +#[derive(FromPrimitive)] + +enum PlaybackState { + Playing = 0, + Paused = 1, + NotPlaying = 2, + BUFFERING = 3, +} +#[derive(FromPrimitive)] +enum CommandStatus { + Success = 0, + InvalidStateForCommand = 1, + NotAllowed = 2, + NotActive = 3, + SpeedOutOfRange = 4, + SeekOutOfRange = 5, +} + +impl CommandStatus { + fn u8(&self) -> u8 { + match self { + CommandStatus::Success => 0, + CommandStatus::InvalidStateForCommand => 1, + CommandStatus::NotAllowed => 2, + CommandStatus::NotActive => 3, + CommandStatus::SpeedOutOfRange => 4, + CommandStatus::SeekOutOfRange => 5, + } + } +} + +#[derive(FromPrimitive, PartialEq)] +pub enum Commands { + Play = 0x0, + Pause = 0x1, + Stop = 0x2, + StartOver = 0x3, + Previous = 0x4, + Next = 0x5, + Rewind = 0x6, + FastForward = 0x7, + SkipForward = 0x8, + SkipBackward = 0x9, + // Response is from us to server + PlaybackResponse = 0xa, + Seek = 0x0b, +} + +struct PlaybackPosition { + updated_at: u64, + position: u64, +} +// Get microseconds since 2000, Jan 1, 00:00:00 +pub fn get_epoch_us() -> u64 { + let epoch_start = NaiveDate::from_ymd_opt(2000, 1, 1) + .unwrap() + .and_hms_micro_opt(0, 0, 0, 0) + .unwrap() + .and_local_timezone(chrono::Utc) + .unwrap(); + DateTime::timestamp_micros(&epoch_start) as u64 +} + +pub struct MediaPlaybackCluster { + base: Cluster, + sampled_position: PlaybackPosition, + callbacks: Vec, +} + +impl MediaPlaybackCluster { + pub fn new() -> Result, Error> { + let mut cluster = Box::new(MediaPlaybackCluster { + base: Cluster::new(ID)?, + sampled_position: PlaybackPosition { + updated_at: 0, + position: 0, + }, + callbacks: vec![], + }); + + // List should be a Vec< + let attrs = [ + Attribute::new( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::NotPlaying as u8), + Access::RV, + Quality::PERSISTENT, + )?, + // epoch-us + Attribute::new( + Attributes::StartTime as u16, + AttrValue::Uint64(0), + Access::RV, + Quality::PERSISTENT, + )?, + Attribute::new( + Attributes::Duration as u16, + AttrValue::Uint64(1), + Access::RV, + Quality::PERSISTENT, + )?, + // Playback-Position + Attribute::new( + Attributes::SampledPosition as u16, + AttrValue::Custom, + Access::RV, + Quality::PERSISTENT, + )?, + // Float + Attribute::new( + Attributes::PlaybackSpeed as u16, + AttrValue::Custom, + Access::RV, + Quality::PERSISTENT, + )?, + Attribute::new( + Attributes::SeekRangeEnd as u16, + AttrValue::Uint64(0), + Access::RV, + Quality::PERSISTENT, + )?, + Attribute::new( + Attributes::SeekRangeStart as u16, + AttrValue::Uint64(0), + Access::RV, + Quality::PERSISTENT, + )?, + // Options - probably want a custom type here for mapping cluster options to TLV bitmask + ]; + cluster.base.add_attributes(&attrs)?; + + // For now disable all features by default + cluster.base.set_feature_map(0)?; + Ok(cluster) + } + + pub fn add_callback(&mut self, name: Commands, callback: Box) { + self.callbacks.push(ClusterCallback { + name, + callback: callback, + }); + } + + fn run_callback(&mut self, name: Commands) { + for cmd in self.callbacks.iter_mut() { + if cmd.name == name { + (cmd.callback)() + } + } + } + + fn _set_state_buffering(&mut self) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + Err(IMStatusCode::Sucess) + } + + fn _set_duration(&mut self, duration: u64) -> Result<(), IMStatusCode> { + self.base + .write_attribute_raw(Attributes::Duration as u16, AttrValue::Uint64(duration))?; + Err(IMStatusCode::Sucess) + } + + // When rewinding / changing stream / etc we need to change absolute position and updateAt + fn update_position(&mut self, new_pos: u64) -> Result<(), IMStatusCode> { + let now = get_epoch_us(); + + self.sampled_position.position = new_pos; + self.sampled_position.updated_at = now; + + Err(IMStatusCode::Sucess) + } + + fn enocde_sampled_position(&self, tag: TagType, tw: &mut TLVWriter) { + let _ = tw.start_struct(tag); + let _ = tw.u64(TagType::Context(0), self.sampled_position.position); + let _ = tw.u64(TagType::Context(1), self.sampled_position.updated_at); + let _ = tw.end_container(); + } +} + +// Commmands +impl MediaPlaybackCluster { + fn handle_play(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + + self.run_callback(Commands::Play); + self.send_playback_response(CommandStatus::Success, cmd_req); + Err(IMStatusCode::Sucess) + } + fn handle_pause(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Paused as u8), + )?; + + self.run_callback(Commands::Pause); + self.send_playback_response(CommandStatus::Success, cmd_req); + Err(IMStatusCode::Sucess) + } + fn handle_stop(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::NotPlaying as u8), + )?; + + self.run_callback(Commands::Stop); + self.send_playback_response(CommandStatus::Success, cmd_req); + Err(IMStatusCode::Sucess) + } + + // Start current thinbg over + fn handle_start_over(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + + self.update_position(0)?; + self.run_callback(Commands::StartOver); + self.send_playback_response(CommandStatus::Success, cmd_req); + Err(IMStatusCode::Sucess) + } + + fn handle_next(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + // self.update_position(0)?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + fn handle_previous(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + // self.update_position(0)?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + fn handle_rewind(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + fn handle_ff(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + fn handle_skip_forward(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + fn handle_skip_backward(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + fn handle_seek(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + self.base.write_attribute_raw( + Attributes::CurrentState as u16, + AttrValue::Uint8(PlaybackState::Playing as u8), + )?; + self.send_playback_response(CommandStatus::NotAllowed, cmd_req); + Err(IMStatusCode::UnsupportedCommand) + } + + // TODO: We send this to client + fn send_playback_response(&mut self, status: CommandStatus, cmd_req: &mut CommandReq) { + let mut playback_response = cmd_req.cmd; + playback_response.path.leaf = Some(Commands::PlaybackResponse as u32); + + let resp = status.u8(); + let cmd_data = |tag: TagType, t: &mut TLVWriter| { + let _ = t.start_struct(tag); + let _ = t.u8(TagType::Context(0), resp); + let _ = t.end_container(); + }; + + let invoke_resp = ib::InvResp::Cmd(ib::CmdData::new( + playback_response, + EncodeValue::Closure(&cmd_data), + )); + let _ = invoke_resp.to_tlv(cmd_req.resp, TagType::Anonymous); + cmd_req.trans.complete(); + } +} + +impl ClusterType for MediaPlaybackCluster { + fn base(&self) -> &Cluster { + &self.base + } + fn base_mut(&mut self) -> &mut Cluster { + &mut self.base + } + + fn read_custom_attribute(&self, encoder: &mut dyn Encoder, attr: &AttrDetails) { + match num::FromPrimitive::from_u16(attr.attr_id) { + Some(Attributes::SampledPosition) => { + encoder.encode(EncodeValue::Closure(&|tag, tw| { + log::warn!("Encoding sampled position of self!"); + self.enocde_sampled_position(tag, tw) + })) + } + _ => log::error!("Attribute not supported!"), + } + } + + fn handle_command(&mut self, cmd_req: &mut CommandReq) -> Result<(), IMStatusCode> { + let cmd = cmd_req + .cmd + .path + .leaf + .map(num::FromPrimitive::from_u32) + .ok_or(IMStatusCode::UnsupportedCommand)? + .ok_or(IMStatusCode::UnsupportedCommand)?; + + match cmd { + Commands::Play => self.handle_play(cmd_req), + Commands::Pause => self.handle_pause(cmd_req), + Commands::Stop => self.handle_stop(cmd_req), + Commands::StartOver => self.handle_start_over(cmd_req), + Commands::Previous => self.handle_previous(cmd_req), + Commands::Next => self.handle_next(cmd_req), + Commands::Rewind => self.handle_rewind(cmd_req), + Commands::FastForward => self.handle_ff(cmd_req), + Commands::SkipForward => self.handle_skip_forward(cmd_req), + Commands::SkipBackward => self.handle_skip_backward(cmd_req), + Commands::PlaybackResponse => Err(IMStatusCode::InvalidCommand), + Commands::Seek => self.handle_seek(cmd_req), + } + } +} diff --git a/matter/src/data_model/device_types.rs b/matter/src/data_model/device_types.rs index 12874f4..46d133e 100644 --- a/matter/src/data_model/device_types.rs +++ b/matter/src/data_model/device_types.rs @@ -73,6 +73,11 @@ const DEV_TYPE_ON_OFF_LIGHT: DeviceType = DeviceType { drev: 2, }; +pub const DEV_TYPE_ON_SMART_SPEAKER: DeviceType = DeviceType { + dtype: 0x0022, + drev: 2, +}; + pub fn device_type_add_on_off_light(node: &mut WriteNode) -> Result { let endpoint = node.add_endpoint(DEV_TYPE_ON_OFF_LIGHT)?; node.add_cluster(endpoint, OnOffCluster::new()?)?; diff --git a/matter/src/data_model/mod.rs b/matter/src/data_model/mod.rs index dfc80c9..c347941 100644 --- a/matter/src/data_model/mod.rs +++ b/matter/src/data_model/mod.rs @@ -20,6 +20,7 @@ pub mod device_types; pub mod objects; pub mod cluster_basic_information; +pub mod cluster_media_playback; pub mod cluster_on_off; pub mod cluster_template; pub mod sdm;